Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ActionField |
|
| 2.033333333333333;2.033 |
1 | /* | |
2 | * Copyright 2007 The Kuali Foundation Licensed under the Educational Community | |
3 | * License, Version 1.0 (the "License"); you may not use this file except in | |
4 | * compliance with the License. You may obtain a copy of the License at | |
5 | * http://www.opensource.org/licenses/ecl1.php Unless required by applicable law | |
6 | * or agreed to in writing, software distributed under the License is | |
7 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
8 | * KIND, either express or implied. See the License for the specific language | |
9 | * governing permissions and limitations under the License. | |
10 | */ | |
11 | package org.kuali.rice.krad.uif.field; | |
12 | ||
13 | import org.apache.commons.lang.StringUtils; | |
14 | import org.kuali.rice.krad.uif.UifConstants; | |
15 | import org.kuali.rice.krad.uif.UifParameters; | |
16 | import org.kuali.rice.krad.uif.UifPropertyPaths; | |
17 | import org.kuali.rice.krad.uif.container.FormView; | |
18 | import org.kuali.rice.krad.uif.container.View; | |
19 | import org.kuali.rice.krad.uif.core.Component; | |
20 | import org.kuali.rice.krad.uif.widget.LightBox; | |
21 | import org.kuali.rice.krad.uif.widget.LightBoxLookup; | |
22 | ||
23 | import java.util.HashMap; | |
24 | import java.util.Map; | |
25 | ||
26 | /** | |
27 | * Field that presents an action that can be taken on the UI such as submitting | |
28 | * the form or invoking a script | |
29 | * | |
30 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
31 | */ | |
32 | public class ActionField extends FieldBase { | |
33 | private static final long serialVersionUID = 1025672792657238829L; | |
34 | ||
35 | private String methodToCall; | |
36 | private String navigateToPageId; | |
37 | ||
38 | private String clientSideJs; | |
39 | ||
40 | private String jumpToIdAfterSubmit; | |
41 | private String jumpToNameAfterSubmit; | |
42 | private String focusOnAfterSubmit; | |
43 | ||
44 | private String actionLabel; | |
45 | private ImageField actionImageField; | |
46 | ||
47 | private Map<String, String> actionParameters; | |
48 | ||
49 | private LightBoxLookup lightBoxLookup; | |
50 | ||
51 | private LightBox lightBox; | |
52 | ||
53 | private boolean blockValidateDirty; | |
54 | ||
55 | public ActionField() { | |
56 | 0 | super(); |
57 | 0 | actionParameters = new HashMap<String, String>(); |
58 | 0 | } |
59 | ||
60 | /** | |
61 | * The following initialization is performed: | |
62 | * <ul> | |
63 | * <li>Set the actionLabel if blank to the Field label</li> | |
64 | * </ul> | |
65 | * | |
66 | * @see org.kuali.rice.krad.uif.core.ComponentBase#performInitialization(org.kuali.rice.krad.uif.container.View) | |
67 | */ | |
68 | @Override | |
69 | public void performInitialization(View view) { | |
70 | 0 | super.performInitialization(view); |
71 | ||
72 | 0 | if (StringUtils.isBlank(actionLabel)) { |
73 | 0 | actionLabel = this.getLabel(); |
74 | } | |
75 | 0 | } |
76 | ||
77 | /** | |
78 | * The following finalization is performed: | |
79 | * <ul> | |
80 | * <li>Add methodToCall action parameter if set and setup event code for | |
81 | * setting action parameters</li> | |
82 | * </ul> | |
83 | * | |
84 | * @see org.kuali.rice.krad.uif.core.ComponentBase#performFinalize(org.kuali.rice.krad.uif.container.View, | |
85 | * java.lang.Object, org.kuali.rice.krad.uif.core.Component) | |
86 | */ | |
87 | @Override | |
88 | public void performFinalize(View view, Object model, Component parent) { | |
89 | 0 | super.performFinalize(view, model, parent); |
90 | ||
91 | 0 | actionParameters.put(UifConstants.UrlParams.SHOW_HOME, "false"); |
92 | 0 | actionParameters.put(UifConstants.UrlParams.SHOW_HISTORY, "false"); |
93 | ||
94 | 0 | if (StringUtils.isNotBlank(navigateToPageId)) { |
95 | 0 | actionParameters.put(UifParameters.NAVIGATE_TO_PAGE_ID, navigateToPageId); |
96 | 0 | if (StringUtils.isBlank(methodToCall)) { |
97 | 0 | actionParameters.put(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME, |
98 | UifConstants.MethodToCallNames.NAVIGATE); | |
99 | } | |
100 | } | |
101 | ||
102 | 0 | if (!actionParameters.containsKey(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME) |
103 | && StringUtils.isNotBlank(methodToCall)) { | |
104 | 0 | actionParameters.put(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME, methodToCall); |
105 | } | |
106 | ||
107 | // If there is no lightBox then create the on click script | |
108 | 0 | if (lightBoxLookup == null) { |
109 | 0 | String prefixScript = this.getOnClickScript(); |
110 | 0 | if (prefixScript == null) { |
111 | 0 | prefixScript = ""; |
112 | } | |
113 | ||
114 | 0 | boolean validateFormDirty = false; |
115 | 0 | if (view instanceof FormView && !isBlockValidateDirty()) { |
116 | 0 | validateFormDirty = ((FormView) view).isValidateDirty(); |
117 | } | |
118 | ||
119 | 0 | boolean includeDirtyCheckScript = false; |
120 | 0 | String writeParamsScript = ""; |
121 | 0 | if (!actionParameters.isEmpty()) { |
122 | 0 | for (String key : actionParameters.keySet()) { |
123 | 0 | String parameterPath = key; |
124 | 0 | if (!key.equals(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME)) { |
125 | 0 | parameterPath = UifPropertyPaths.ACTION_PARAMETERS + "[" + key + "]"; |
126 | } | |
127 | ||
128 | 0 | writeParamsScript = writeParamsScript + "writeHiddenToForm('" + parameterPath + "' , '" |
129 | + actionParameters.get(key) + "'); "; | |
130 | ||
131 | // Include dirtycheck js function call if the method to call | |
132 | // is refresh, navigate, cancel or close | |
133 | 0 | if (validateFormDirty && !includeDirtyCheckScript |
134 | && key.equals(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME)) { | |
135 | 0 | String keyValue = (String) actionParameters.get(key); |
136 | 0 | if (StringUtils.equals(keyValue, UifConstants.MethodToCallNames.REFRESH) |
137 | || StringUtils.equals(keyValue, UifConstants.MethodToCallNames.NAVIGATE) | |
138 | || StringUtils.equals(keyValue, UifConstants.MethodToCallNames.CANCEL) | |
139 | || StringUtils.equals(keyValue, UifConstants.MethodToCallNames.CLOSE)) { | |
140 | 0 | includeDirtyCheckScript = true; |
141 | } | |
142 | } | |
143 | 0 | } |
144 | } | |
145 | ||
146 | // TODO possibly fix some other way - this is a workaround, prevents | |
147 | // showing history and showing home again on actions which submit | |
148 | // the form | |
149 | 0 | writeParamsScript = writeParamsScript + "writeHiddenToForm('" + UifConstants.UrlParams.SHOW_HISTORY |
150 | + "', '" + "false" + "'); "; | |
151 | 0 | writeParamsScript = writeParamsScript + "writeHiddenToForm('" + UifConstants.UrlParams.SHOW_HOME + "' , '" |
152 | + "false" + "'); "; | |
153 | ||
154 | 0 | if (StringUtils.isBlank(focusOnAfterSubmit)) { |
155 | // if this is blank focus this actionField by default | |
156 | 0 | focusOnAfterSubmit = this.getId(); |
157 | 0 | writeParamsScript = writeParamsScript + "writeHiddenToForm('focusId' , '" + this.getId() + "'); "; |
158 | 0 | } else if (!focusOnAfterSubmit.equalsIgnoreCase(UifConstants.Order.FIRST)) { |
159 | // Use the id passed in | |
160 | 0 | writeParamsScript = writeParamsScript + "writeHiddenToForm('focusId' , '" + focusOnAfterSubmit + "'); "; |
161 | } else { | |
162 | // First input will be focused, must be first field set to empty | |
163 | // string | |
164 | 0 | writeParamsScript = writeParamsScript + "writeHiddenToForm('focusId' , ''); "; |
165 | } | |
166 | ||
167 | 0 | if (StringUtils.isBlank(jumpToIdAfterSubmit) && StringUtils.isBlank(jumpToNameAfterSubmit)) { |
168 | 0 | jumpToIdAfterSubmit = this.getId(); |
169 | 0 | writeParamsScript = writeParamsScript + "writeHiddenToForm('jumpToId' , '" + this.getId() + "'); "; |
170 | 0 | } else if (StringUtils.isNotBlank(jumpToIdAfterSubmit)) { |
171 | 0 | writeParamsScript = writeParamsScript + "writeHiddenToForm('jumpToId' , '" + jumpToIdAfterSubmit |
172 | + "'); "; | |
173 | } else { | |
174 | 0 | writeParamsScript = writeParamsScript + "writeHiddenToForm('jumpToName' , '" + jumpToNameAfterSubmit |
175 | + "'); "; | |
176 | } | |
177 | ||
178 | 0 | String postScript = ""; |
179 | 0 | if (StringUtils.isNotBlank(clientSideJs)) { |
180 | 0 | postScript = clientSideJs; |
181 | } else { | |
182 | 0 | postScript = "jq('#kualiForm').submit();"; |
183 | } | |
184 | ||
185 | 0 | if (includeDirtyCheckScript) { |
186 | 0 | this.setOnClickScript("e.preventDefault(); if (checkDirty(e) == false) { " + prefixScript |
187 | + writeParamsScript + postScript + " ; } "); | |
188 | } else { | |
189 | 0 | this.setOnClickScript("e.preventDefault();" + prefixScript + writeParamsScript + postScript); |
190 | } | |
191 | ||
192 | 0 | } else { |
193 | // When there is a light box - don't add the on click script as it | |
194 | // will be prevented from executing | |
195 | // Create a script map object which will be written to the form on | |
196 | // click event | |
197 | 0 | StringBuffer sb = new StringBuffer(); |
198 | 0 | sb.append("{"); |
199 | 0 | for (String key : actionParameters.keySet()) { |
200 | 0 | String optionValue = actionParameters.get(key); |
201 | 0 | if (sb.length() > 1) { |
202 | 0 | sb.append(","); |
203 | } | |
204 | 0 | if (!key.equals(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME)) { |
205 | 0 | sb.append("\"" + UifPropertyPaths.ACTION_PARAMETERS + "[" + key + "]" + "\""); |
206 | } else { | |
207 | 0 | sb.append("\"" + key + "\""); |
208 | } | |
209 | 0 | sb.append(":"); |
210 | 0 | sb.append("\"" + optionValue + "\""); |
211 | 0 | } |
212 | 0 | sb.append("}"); |
213 | 0 | lightBoxLookup.setActionParameterMapString(sb.toString()); |
214 | } | |
215 | 0 | } |
216 | ||
217 | /** | |
218 | * Name of the method that should be called when the action is selected | |
219 | * <p> | |
220 | * For a server side call (clientSideCall is false), gives the name of the | |
221 | * method in the mapped controller that should be invoked when the action is | |
222 | * selected. For client side calls gives the name of the script function | |
223 | * that should be invoked when the action is selected | |
224 | * </p> | |
225 | * | |
226 | * @return String name of method to call | |
227 | */ | |
228 | public String getMethodToCall() { | |
229 | 0 | return this.methodToCall; |
230 | } | |
231 | ||
232 | /** | |
233 | * Setter for the actions method to call | |
234 | * | |
235 | * @param methodToCall | |
236 | */ | |
237 | public void setMethodToCall(String methodToCall) { | |
238 | 0 | this.methodToCall = methodToCall; |
239 | 0 | } |
240 | ||
241 | /** | |
242 | * Label text for the action | |
243 | * <p> | |
244 | * The label text is used by the template renderers to give a human readable | |
245 | * label for the action. For buttons this generally is the button text, | |
246 | * while for an action link it would be the links displayed text | |
247 | * </p> | |
248 | * | |
249 | * @return String label for action | |
250 | */ | |
251 | public String getActionLabel() { | |
252 | 0 | return this.actionLabel; |
253 | } | |
254 | ||
255 | /** | |
256 | * Setter for the actions label | |
257 | * | |
258 | * @param actionLabel | |
259 | */ | |
260 | public void setActionLabel(String actionLabel) { | |
261 | 0 | this.actionLabel = actionLabel; |
262 | 0 | } |
263 | ||
264 | /** | |
265 | * Image to use for the action | |
266 | * <p> | |
267 | * When the action image field is set (and render is true) the image will be | |
268 | * used to present the action as opposed to the default (input submit). For | |
269 | * action link templates the image is used for the link instead of the | |
270 | * action link text | |
271 | * </p> | |
272 | * | |
273 | * @return ImageField action image | |
274 | */ | |
275 | public ImageField getActionImageField() { | |
276 | 0 | return this.actionImageField; |
277 | } | |
278 | ||
279 | /** | |
280 | * Setter for the action image field | |
281 | * | |
282 | * @param actionImageField | |
283 | */ | |
284 | public void setActionImageField(ImageField actionImageField) { | |
285 | 0 | this.actionImageField = actionImageField; |
286 | 0 | } |
287 | ||
288 | /** | |
289 | * For an <code>ActionField</code> that is part of a | |
290 | * <code>NavigationGroup</code, the navigate to page id can be set to | |
291 | * configure the page that should be navigated to when the action is | |
292 | * selected | |
293 | * <p> | |
294 | * Support exists in the <code>UifControllerBase</code> for handling | |
295 | * navigation between pages | |
296 | * </p> | |
297 | * | |
298 | * @return String id of page that should be rendered when the action item is | |
299 | * selected | |
300 | */ | |
301 | public String getNavigateToPageId() { | |
302 | 0 | return this.navigateToPageId; |
303 | } | |
304 | ||
305 | /** | |
306 | * Setter for the navigate to page id | |
307 | * | |
308 | * @param navigateToPageId | |
309 | */ | |
310 | public void setNavigateToPageId(String navigateToPageId) { | |
311 | 0 | this.navigateToPageId = navigateToPageId; |
312 | 0 | actionParameters.put(UifParameters.NAVIGATE_TO_PAGE_ID, navigateToPageId); |
313 | 0 | this.methodToCall = UifConstants.MethodToCallNames.NAVIGATE; |
314 | 0 | } |
315 | ||
316 | /** | |
317 | * Parameters that should be sent when the action is invoked | |
318 | * <p> | |
319 | * Action renderer will decide how the parameters are sent for the action | |
320 | * (via script generated hiddens, or script parameters, ...) | |
321 | * </p> | |
322 | * <p> | |
323 | * Can be set by other components such as the <code>CollectionGroup</code> | |
324 | * to provide the context the action is in (such as the collection name and | |
325 | * line the action applies to) | |
326 | * </p> | |
327 | * | |
328 | * @return Map<String, String> action parameters | |
329 | */ | |
330 | public Map<String, String> getActionParameters() { | |
331 | 0 | return this.actionParameters; |
332 | } | |
333 | ||
334 | /** | |
335 | * Setter for the action parameters | |
336 | * | |
337 | * @param actionParameters | |
338 | */ | |
339 | public void setActionParameters(Map<String, String> actionParameters) { | |
340 | 0 | this.actionParameters = actionParameters; |
341 | 0 | } |
342 | ||
343 | /** | |
344 | * Convenience method to add a parameter to the action parameters Map | |
345 | * | |
346 | * @param parameterName | |
347 | * - name of parameter to add | |
348 | * @param parameterValue | |
349 | * - value of parameter to add | |
350 | */ | |
351 | public void addActionParameter(String parameterName, String parameterValue) { | |
352 | 0 | if (actionParameters == null) { |
353 | 0 | this.actionParameters = new HashMap<String, String>(); |
354 | } | |
355 | ||
356 | 0 | this.actionParameters.put(parameterName, parameterValue); |
357 | 0 | } |
358 | ||
359 | /** | |
360 | * Get an actionParameter by name | |
361 | */ | |
362 | public String getActionParameter(String parameterName) { | |
363 | ||
364 | 0 | return this.actionParameters.get(parameterName); |
365 | } | |
366 | ||
367 | /** | |
368 | * @see org.kuali.rice.krad.uif.core.ComponentBase#getSupportsOnClick() | |
369 | */ | |
370 | @Override | |
371 | public boolean getSupportsOnClick() { | |
372 | 0 | return true; |
373 | } | |
374 | ||
375 | /** | |
376 | * Setter for the light box lookup widget | |
377 | * | |
378 | * @param the | |
379 | * <code>LightBoxLookup</code> widget to set | |
380 | */ | |
381 | public void setLightBoxLookup(LightBoxLookup lightBoxLookup) { | |
382 | 0 | this.lightBoxLookup = lightBoxLookup; |
383 | 0 | } |
384 | ||
385 | /** | |
386 | * LightBoxLookup widget for the field | |
387 | * <p> | |
388 | * The light box lookup widget will change the lookup behaviour to open the | |
389 | * lookup in a light box. | |
390 | * </p> | |
391 | * | |
392 | * @return the <code>DirectInquiry</code> field DirectInquiry | |
393 | */ | |
394 | public LightBoxLookup getLightBoxLookup() { | |
395 | 0 | return lightBoxLookup; |
396 | } | |
397 | ||
398 | /** | |
399 | * @return the jumpToIdAfterSubmit | |
400 | */ | |
401 | public String getJumpToIdAfterSubmit() { | |
402 | 0 | return this.jumpToIdAfterSubmit; |
403 | } | |
404 | ||
405 | /** | |
406 | * The id to jump to in the next page, the element with this id will be | |
407 | * jumped to automatically when the new page is retrieved after a submit. | |
408 | * Using "TOP" or "BOTTOM" will jump to the top or the bottom of the | |
409 | * resulting page. Passing in nothing for both jumpToIdAfterSubmit and | |
410 | * jumpToNameAfterSubmit will result in this ActionField being jumped to by | |
411 | * default if it is present on the new page. WARNING: jumpToIdAfterSubmit | |
412 | * always takes precedence over jumpToNameAfterSubmit, if set. | |
413 | * | |
414 | * @param jumpToIdAfterSubmit | |
415 | * the jumpToIdAfterSubmit to set | |
416 | */ | |
417 | public void setJumpToIdAfterSubmit(String jumpToIdAfterSubmit) { | |
418 | 0 | this.jumpToIdAfterSubmit = jumpToIdAfterSubmit; |
419 | 0 | } |
420 | ||
421 | /** | |
422 | * The name to jump to in the next page, the element with this name will be | |
423 | * jumped to automatically when the new page is retrieved after a submit. | |
424 | * Passing in nothing for both jumpToIdAfterSubmit and jumpToNameAfterSubmit | |
425 | * will result in this ActionField being jumped to by default if it is | |
426 | * present on the new page. WARNING: jumpToIdAfterSubmit always takes | |
427 | * precedence over jumpToNameAfterSubmit, if set. | |
428 | * | |
429 | * @return the jumpToNameAfterSubmit | |
430 | */ | |
431 | public String getJumpToNameAfterSubmit() { | |
432 | 0 | return this.jumpToNameAfterSubmit; |
433 | } | |
434 | ||
435 | /** | |
436 | * @param jumpToNameAfterSubmit | |
437 | * the jumpToNameAfterSubmit to set | |
438 | */ | |
439 | public void setJumpToNameAfterSubmit(String jumpToNameAfterSubmit) { | |
440 | 0 | this.jumpToNameAfterSubmit = jumpToNameAfterSubmit; |
441 | 0 | } |
442 | ||
443 | /** | |
444 | * The id of the field to place focus on in the new page after the new page | |
445 | * is retrieved. Passing in "FIRST" will focus on the first visible input | |
446 | * element on the form. Passing in the empty string will result in this | |
447 | * ActionField being focused. | |
448 | * | |
449 | * @return the focusOnAfterSubmit | |
450 | */ | |
451 | public String getFocusOnAfterSubmit() { | |
452 | 0 | return this.focusOnAfterSubmit; |
453 | } | |
454 | ||
455 | /** | |
456 | * @param focusOnAfterSubmit | |
457 | * the focusOnAfterSubmit to set | |
458 | */ | |
459 | public void setFocusOnAfterSubmit(String focusOnAfterSubmit) { | |
460 | 0 | this.focusOnAfterSubmit = focusOnAfterSubmit; |
461 | 0 | } |
462 | ||
463 | /** | |
464 | * Client side javascript to be executed when this actionField is clicked. | |
465 | * This overrides the default action for this ActionField so the method | |
466 | * called must explicitly submit, navigate, etc. through js, if necessary. | |
467 | * In addition, this js occurs AFTER onClickScripts set on this field, it | |
468 | * will be the last script executed by the click event. Sidenote: This js is | |
469 | * always called after hidden actionParameters and methodToCall methods are | |
470 | * written by the js to the html form. | |
471 | * | |
472 | * @return the clientSideJs | |
473 | */ | |
474 | public String getClientSideJs() { | |
475 | 0 | return this.clientSideJs; |
476 | } | |
477 | ||
478 | /** | |
479 | * @param clientSideJs | |
480 | * the clientSideJs to set | |
481 | */ | |
482 | public void setClientSideJs(String clientSideJs) { | |
483 | 0 | if (!StringUtils.endsWith(clientSideJs, ";")) { |
484 | 0 | clientSideJs = clientSideJs + ";"; |
485 | } | |
486 | 0 | this.clientSideJs = clientSideJs; |
487 | 0 | } |
488 | ||
489 | /** | |
490 | * Setter for the light box widget | |
491 | * | |
492 | * @param the | |
493 | * <code>LightBox</code> widget to set | |
494 | */ | |
495 | public void setLightBox(LightBox lightBox) { | |
496 | 0 | this.lightBox = lightBox; |
497 | 0 | } |
498 | ||
499 | /** | |
500 | * LightBox widget for the field | |
501 | * <p> | |
502 | * The light box widget will change the direct inquiry behaviour to open up | |
503 | * in a light box. | |
504 | * </p> | |
505 | * | |
506 | * @return the <code>LightBox</code> field LightBox | |
507 | */ | |
508 | public LightBox getLightBox() { | |
509 | 0 | return lightBox; |
510 | } | |
511 | ||
512 | /** | |
513 | * @param blockValidateDirty | |
514 | * the blockValidateDirty to set | |
515 | */ | |
516 | public void setBlockValidateDirty(boolean blockValidateDirty) { | |
517 | 0 | this.blockValidateDirty = blockValidateDirty; |
518 | 0 | } |
519 | ||
520 | /** | |
521 | * @return the blockValidateDirty | |
522 | */ | |
523 | public boolean isBlockValidateDirty() { | |
524 | 0 | return blockValidateDirty; |
525 | } | |
526 | ||
527 | } |