| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ActionField |
|
| 1.7727272727272727;1.773 |
| 1 | /* | |
| 2 | * Copyright 2007 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 1.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.opensource.org/licenses/ecl1.php | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package org.kuali.rice.kns.uif.field; | |
| 17 | ||
| 18 | import java.util.HashMap; | |
| 19 | import java.util.Map; | |
| 20 | ||
| 21 | import org.apache.commons.lang.StringUtils; | |
| 22 | import org.kuali.rice.kns.uif.UifConstants; | |
| 23 | import org.kuali.rice.kns.uif.UifParameters; | |
| 24 | import org.kuali.rice.kns.uif.UifPropertyPaths; | |
| 25 | import org.kuali.rice.kns.uif.container.View; | |
| 26 | import org.kuali.rice.kns.uif.core.Component; | |
| 27 | import org.kuali.rice.kns.uif.widget.LightBoxLookup; | |
| 28 | ||
| 29 | /** | |
| 30 | * Field that presents an action that can be taken on the UI such as submitting | |
| 31 | * the form or invoking a script | |
| 32 | * | |
| 33 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 34 | */ | |
| 35 | public class ActionField extends FieldBase { | |
| 36 | private static final long serialVersionUID = 1025672792657238829L; | |
| 37 | ||
| 38 | private String methodToCall; | |
| 39 | private String navigateToPageId; | |
| 40 | ||
| 41 | private boolean clientSideCall; | |
| 42 | private boolean scriptFormSubmit; | |
| 43 | ||
| 44 | private String actionLabel; | |
| 45 | private ImageField actionImageField; | |
| 46 | ||
| 47 | private Map<String, String> actionParameters; | |
| 48 | ||
| 49 | private LightBoxLookup lightBoxLookup; | |
| 50 | ||
| 51 | 0 | public ActionField() { |
| 52 | 0 | clientSideCall = false; |
| 53 | 0 | scriptFormSubmit = false; |
| 54 | ||
| 55 | 0 | actionParameters = new HashMap<String, String>(); |
| 56 | 0 | } |
| 57 | ||
| 58 | /** | |
| 59 | * <p> | |
| 60 | * The following initialization is performed: | |
| 61 | * | |
| 62 | * <ul> | |
| 63 | * <li>Set the actionLabel if blank to the Field label</li> | |
| 64 | * </ul> | |
| 65 | * </p> | |
| 66 | * | |
| 67 | * @see org.kuali.rice.kns.uif.core.ComponentBase#performInitialization(org.kuali.rice.kns.uif.container.View) | |
| 68 | */ | |
| 69 | @Override | |
| 70 | public void performInitialization(View view) { | |
| 71 | 0 | super.performInitialization(view); |
| 72 | ||
| 73 | 0 | if (StringUtils.isBlank(actionLabel)) { |
| 74 | 0 | actionLabel = this.getLabel(); |
| 75 | } | |
| 76 | 0 | } |
| 77 | ||
| 78 | /** | |
| 79 | * <p> | |
| 80 | * The following finalization is performed: | |
| 81 | * | |
| 82 | * <ul> | |
| 83 | * <li>Add methodToCall action parameter if set and setup event code for | |
| 84 | * setting action parameters</li> | |
| 85 | * </ul> | |
| 86 | * </p> | |
| 87 | * | |
| 88 | * @see org.kuali.rice.kns.uif.core.ComponentBase#performFinalize(org.kuali.rice.kns.uif.container.View, | |
| 89 | * java.lang.Object, org.kuali.rice.kns.uif.core.Component) | |
| 90 | */ | |
| 91 | @Override | |
| 92 | public void performFinalize(View view, Object model, Component parent) { | |
| 93 | 0 | super.performFinalize(view, model, parent); |
| 94 | ||
| 95 | 0 | if (StringUtils.isNotBlank(navigateToPageId)) { |
| 96 | 0 | actionParameters.put(UifParameters.NAVIGATE_TO_PAGE_ID, navigateToPageId); |
| 97 | 0 | if (StringUtils.isBlank(methodToCall)) { |
| 98 | 0 | actionParameters.put(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME, |
| 99 | UifConstants.MethodToCallNames.NAVIGATE); | |
| 100 | } | |
| 101 | } | |
| 102 | ||
| 103 | 0 | if (!actionParameters.containsKey(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME) |
| 104 | && StringUtils.isNotBlank(methodToCall) && !clientSideCall) { | |
| 105 | 0 | actionParameters.put(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME, methodToCall); |
| 106 | } | |
| 107 | ||
| 108 | 0 | if (!actionParameters.isEmpty()) { |
| 109 | ||
| 110 | // If there is no lightBox then create the on click script | |
| 111 | 0 | if (lightBoxLookup == null) { |
| 112 | 0 | String prefixScript = this.getOnClickScript(); |
| 113 | 0 | if (prefixScript == null) { |
| 114 | 0 | prefixScript = ""; |
| 115 | } | |
| 116 | ||
| 117 | 0 | String writeParamsScript = ""; |
| 118 | 0 | for (String key : actionParameters.keySet()) { |
| 119 | 0 | String parameterPath = key; |
| 120 | 0 | if (!key.equals(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME)) { |
| 121 | 0 | parameterPath = UifPropertyPaths.ACTION_PARAMETERS + "[" + key + "]"; |
| 122 | } | |
| 123 | ||
| 124 | 0 | writeParamsScript = writeParamsScript + "writeHiddenToForm('" + parameterPath + "' , '" |
| 125 | + actionParameters.get(key) + "'); "; | |
| 126 | 0 | } |
| 127 | ||
| 128 | 0 | String postScript = ""; |
| 129 | 0 | if (scriptFormSubmit) { |
| 130 | 0 | postScript = "submitForm();"; |
| 131 | } | |
| 132 | ||
| 133 | 0 | this.setOnClickScript(prefixScript + writeParamsScript + postScript); |
| 134 | 0 | }else{ |
| 135 | // When there is a light box - don't add the on click script as it will be prevented from executing | |
| 136 | // Create a script map object which will be used to build the on click script | |
| 137 | // Could use eval() instead and just pass the script? | |
| 138 | 0 | StringBuffer sb = new StringBuffer(); |
| 139 | 0 | sb.append("{"); |
| 140 | 0 | for (String key : actionParameters.keySet()) { |
| 141 | 0 | String optionValue = actionParameters.get(key); |
| 142 | 0 | if (sb.length() > 1) { |
| 143 | 0 | sb.append(","); |
| 144 | } | |
| 145 | 0 | if (!key.equals(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME)) { |
| 146 | 0 | sb.append("\"" + UifPropertyPaths.ACTION_PARAMETERS + "[" + key + "]" + "\""); |
| 147 | }else{ | |
| 148 | 0 | sb.append("\"" + key + "\""); |
| 149 | } | |
| 150 | 0 | sb.append(":"); |
| 151 | 0 | sb.append("\"" + optionValue + "\""); |
| 152 | 0 | } |
| 153 | 0 | sb.append("}"); |
| 154 | 0 | lightBoxLookup.setActionParameterMapString(sb.toString()); |
| 155 | } | |
| 156 | } | |
| 157 | 0 | } |
| 158 | ||
| 159 | /** | |
| 160 | * Name of the method that should be called when the action is selected | |
| 161 | * | |
| 162 | * <p> | |
| 163 | * For a server side call (clientSideCall is false), gives the name of the | |
| 164 | * method in the mapped controller that should be invoked when the action is | |
| 165 | * selected. For client side calls gives the name of the script function | |
| 166 | * that should be invoked when the action is selected | |
| 167 | * </p> | |
| 168 | * | |
| 169 | * @return String name of method to call | |
| 170 | */ | |
| 171 | public String getMethodToCall() { | |
| 172 | 0 | return this.methodToCall; |
| 173 | } | |
| 174 | ||
| 175 | /** | |
| 176 | * Setter for the actions method to call | |
| 177 | * | |
| 178 | * @param methodToCall | |
| 179 | */ | |
| 180 | public void setMethodToCall(String methodToCall) { | |
| 181 | 0 | this.methodToCall = methodToCall; |
| 182 | 0 | } |
| 183 | ||
| 184 | /** | |
| 185 | * Indicates whether the action invokes a client side function or a server | |
| 186 | * side. For server side calls this will typically be a form post | |
| 187 | * | |
| 188 | * @return boolean true if action makes a client side call, false if the | |
| 189 | * call is server side | |
| 190 | */ | |
| 191 | public boolean isClientSideCall() { | |
| 192 | 0 | return this.clientSideCall; |
| 193 | } | |
| 194 | ||
| 195 | /** | |
| 196 | * Setter for the client side call indicator | |
| 197 | * | |
| 198 | * @param clientSideCall | |
| 199 | */ | |
| 200 | public void setClientSideCall(boolean clientSideCall) { | |
| 201 | 0 | this.clientSideCall = clientSideCall; |
| 202 | 0 | } |
| 203 | ||
| 204 | /** | |
| 205 | * Builds up the client side JavaScript code for calling the action's method | |
| 206 | * | |
| 207 | * @return String JavaScript handing code | |
| 208 | */ | |
| 209 | public String getClientSideEventCode() { | |
| 210 | 0 | String eventCode = ""; |
| 211 | ||
| 212 | // TODO: need client side method parameters once el support is in | |
| 213 | 0 | if (clientSideCall) { |
| 214 | 0 | eventCode = methodToCall + "();"; |
| 215 | } | |
| 216 | ||
| 217 | 0 | return eventCode; |
| 218 | } | |
| 219 | ||
| 220 | /** | |
| 221 | * Indicates whether the form should be submitted with a JavaScript call | |
| 222 | * (for instance in cases where we have an action link) | |
| 223 | * | |
| 224 | * @return boolean true if form should be submitted with script, false if | |
| 225 | * not (regular input or image submit) | |
| 226 | */ | |
| 227 | public boolean isScriptFormSubmit() { | |
| 228 | 0 | return this.scriptFormSubmit; |
| 229 | } | |
| 230 | ||
| 231 | /** | |
| 232 | * Setter for the script submit indicator | |
| 233 | * | |
| 234 | * @param scriptFormSubmit | |
| 235 | */ | |
| 236 | public void setScriptFormSubmit(boolean scriptFormSubmit) { | |
| 237 | 0 | this.scriptFormSubmit = scriptFormSubmit; |
| 238 | 0 | } |
| 239 | ||
| 240 | /** | |
| 241 | * Label text for the action | |
| 242 | * | |
| 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 | * | |
| 267 | * <p> | |
| 268 | * When the action image field is set (and render is true) the image will be | |
| 269 | * used to present the action as opposed to the default (input submit). For | |
| 270 | * action link templates the image is used for the link instead of the | |
| 271 | * action link text | |
| 272 | * </p> | |
| 273 | * | |
| 274 | * @return ImageField action image | |
| 275 | */ | |
| 276 | public ImageField getActionImageField() { | |
| 277 | 0 | return this.actionImageField; |
| 278 | } | |
| 279 | ||
| 280 | /** | |
| 281 | * Setter for the action image field | |
| 282 | * | |
| 283 | * @param actionImageField | |
| 284 | */ | |
| 285 | public void setActionImageField(ImageField actionImageField) { | |
| 286 | 0 | this.actionImageField = actionImageField; |
| 287 | 0 | } |
| 288 | ||
| 289 | /** | |
| 290 | * For an <code>ActionField</code> that is part of a | |
| 291 | * <code>NavigationGroup</code, the navigate to page id can be set to | |
| 292 | * configure the page that should be navigated to when the action is | |
| 293 | * selected | |
| 294 | * | |
| 295 | * <p> | |
| 296 | * Support exists in the <code>UifControllerBase</code> for handling | |
| 297 | * navigation between pages | |
| 298 | * </p> | |
| 299 | * | |
| 300 | * @return String id of page that should be rendered when the action item is | |
| 301 | * selected | |
| 302 | */ | |
| 303 | public String getNavigateToPageId() { | |
| 304 | 0 | return this.navigateToPageId; |
| 305 | } | |
| 306 | ||
| 307 | /** | |
| 308 | * Setter for the navigate to page id | |
| 309 | * | |
| 310 | * @param navigateToPageId | |
| 311 | */ | |
| 312 | public void setNavigateToPageId(String navigateToPageId) { | |
| 313 | 0 | this.navigateToPageId = navigateToPageId; |
| 314 | 0 | actionParameters.put(UifParameters.NAVIGATE_TO_PAGE_ID, navigateToPageId); |
| 315 | 0 | this.methodToCall = UifConstants.MethodToCallNames.NAVIGATE; |
| 316 | 0 | } |
| 317 | ||
| 318 | /** | |
| 319 | * Parameters that should be sent when the action is invoked | |
| 320 | * | |
| 321 | * <p> | |
| 322 | * Action renderer will decide how the parameters are sent for the action | |
| 323 | * (via script generated hiddens, or script parameters, ...) | |
| 324 | * </p> | |
| 325 | * | |
| 326 | * <p> | |
| 327 | * Can be set by other components such as the <code>CollectionGroup</code> | |
| 328 | * to provide the context the action is in (such as the collection name and | |
| 329 | * line the action applies to) | |
| 330 | * </p> | |
| 331 | * | |
| 332 | * @return Map<String, String> action parameters | |
| 333 | */ | |
| 334 | public Map<String, String> getActionParameters() { | |
| 335 | 0 | return this.actionParameters; |
| 336 | } | |
| 337 | ||
| 338 | /** | |
| 339 | * Setter for the action parameters | |
| 340 | * | |
| 341 | * @param actionParameters | |
| 342 | */ | |
| 343 | public void setActionParameters(Map<String, String> actionParameters) { | |
| 344 | 0 | this.actionParameters = actionParameters; |
| 345 | 0 | } |
| 346 | ||
| 347 | /** | |
| 348 | * Convenience method to add a parameter to the action parameters Map | |
| 349 | * | |
| 350 | * @param parameterName | |
| 351 | * - name of parameter to add | |
| 352 | * @param parameterValue | |
| 353 | * - value of parameter to add | |
| 354 | */ | |
| 355 | public void addActionParameter(String parameterName, String parameterValue) { | |
| 356 | 0 | if (actionParameters == null) { |
| 357 | 0 | this.actionParameters = new HashMap<String, String>(); |
| 358 | } | |
| 359 | ||
| 360 | 0 | this.actionParameters.put(parameterName, parameterValue); |
| 361 | 0 | } |
| 362 | ||
| 363 | /** | |
| 364 | * @see org.kuali.rice.kns.uif.core.ComponentBase#getSupportsOnClick() | |
| 365 | */ | |
| 366 | @Override | |
| 367 | public boolean getSupportsOnClick() { | |
| 368 | 0 | return true; |
| 369 | } | |
| 370 | ||
| 371 | /** | |
| 372 | * @param lightBox the lightBox to set | |
| 373 | */ | |
| 374 | public void setLightBoxLookup(LightBoxLookup lightBoxLookup) { | |
| 375 | 0 | this.lightBoxLookup = lightBoxLookup; |
| 376 | 0 | } |
| 377 | ||
| 378 | /** | |
| 379 | * @return the lightBox | |
| 380 | */ | |
| 381 | public LightBoxLookup getLightBoxLookup() { | |
| 382 | 0 | return lightBoxLookup; |
| 383 | } | |
| 384 | ||
| 385 | } |