1 /*
2 * Copyright 2007 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 1.0 (the
5 * "License"); 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16 package org.kuali.rice.kns.web.spring.form;
17
18 import java.io.Serializable;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Properties;
22 import java.util.UUID;
23
24 import javax.servlet.http.HttpServletRequest;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
28 import org.kuali.rice.kns.uif.UifConstants;
29 import org.kuali.rice.kns.uif.container.View;
30 import org.kuali.rice.kns.uif.history.History;
31 import org.kuali.rice.kns.uif.service.ViewService;
32 import org.springframework.web.multipart.MultipartFile;
33
34 /**
35 * Base form class for views within the KRAD User Interface Framework
36 *
37 * <p>
38 * Holds properties necessary to determine the <code>View</code> instance that
39 * will be used to render the UI
40 * </p>
41 *
42 * @author Kuali Rice Team (rice.collab@kuali.org)
43 */
44 public class UifFormBase implements Serializable {
45 private static final long serialVersionUID = 8432543267099454434L;
46
47 private History formHistory;
48
49 // current view
50 protected String viewId;
51 protected String viewName;
52 protected String viewTypeName;
53 protected String pageId;
54 protected String methodToCall;
55 protected String formKey;
56 protected String jumpToId;
57 protected String jumpToName;
58 protected String focusId;
59 protected String formPostUrl;
60
61 protected boolean defaultsApplied;
62
63 protected View view;
64 protected View previousView;
65 protected Map<String, String> viewRequestParameters;
66
67 protected Map<String, Object> newCollectionLines;
68 protected Map<String, String> actionParameters;
69
70 protected MultipartFile attachmentFile;
71
72 // navigation
73 protected String returnLocation;
74 protected String returnFormKey;
75 protected String hubLocation;
76 protected String hubFormKey;
77 protected String homeLocation;
78
79 protected boolean renderFullView;
80 protected boolean validateDirty;
81
82 public UifFormBase() {
83 formKey = generateFormKey();
84 renderFullView = true;
85 defaultsApplied = false;
86
87 viewRequestParameters = new HashMap<String, String>();
88 newCollectionLines = new HashMap<String, Object>();
89 actionParameters = new HashMap<String, String>();
90 //formHistory = new History();
91 }
92
93 /**
94 * Creates the unique id used to store this "conversation" in the session.
95 * The default method generates a java UUID.
96 *
97 * @return
98 */
99 protected String generateFormKey() {
100 return UUID.randomUUID().toString();
101 }
102
103 /**
104 * Called after Spring binds the request to the form and before the
105 * controller method is invoked.
106 *
107 * @param request
108 * - request object containing the query parameters
109 */
110 public void postBind(HttpServletRequest request) {
111 // default form post URL to request URL
112 formPostUrl = request.getRequestURL().toString();
113
114 //history.pushToHistory(viewId, pageId, view.getTitle(), formPostUrl, formKey);
115 }
116
117 /**
118 * Unique Id for the <code>View</code> instance. This is specified for a
119 * view in its definition by setting the 'id' property.
120 *
121 * @return String view id
122 */
123 public String getViewId() {
124 return this.viewId;
125 }
126
127 /**
128 * Setter for the unique view id
129 *
130 * @param viewId
131 */
132 public void setViewId(String viewId) {
133 this.viewId = viewId;
134 }
135
136 /**
137 * Name for the <code>View</code> instance. This is specified for a view in
138 * its definition by setting the 'id' property. The name is not necessary
139 * unique and cannot be used by itself to retrieve a view. Typically it is
140 * used with other parameters to identify a view with a certain type (view
141 * type)
142 *
143 * @return String view name
144 */
145 public String getViewName() {
146 return this.viewName;
147 }
148
149 /**
150 * Setter for the view name
151 *
152 * @param viewName
153 */
154 public void setViewName(String viewName) {
155 this.viewName = viewName;
156 }
157
158 /**
159 * Name for the type of view being requested. This can be used to find
160 * <code>View</code> instances by request parameters (not necessary the
161 * unique id)
162 *
163 * @return String view type name
164 */
165 public String getViewTypeName() {
166 return this.viewTypeName;
167 }
168
169 /**
170 * Setter for the view type name
171 *
172 * @param viewTypeName
173 */
174 public void setViewTypeName(String viewTypeName) {
175 this.viewTypeName = viewTypeName;
176 }
177
178 /**
179 * Id for the current page being displayed within the view
180 *
181 * @return String page id
182 */
183 public String getPageId() {
184 return this.pageId;
185 }
186
187 /**
188 * Setter for the current page id
189 *
190 * @param pageId
191 */
192 public void setPageId(String pageId) {
193 this.pageId = pageId;
194 }
195
196 public String getFormPostUrl() {
197 return this.formPostUrl;
198 }
199
200 public void setFormPostUrl(String formPostUrl) {
201 this.formPostUrl = formPostUrl;
202 }
203
204 public String getReturnLocation() {
205 return this.returnLocation;
206 }
207
208 public void setReturnLocation(String returnLocation) {
209 this.returnLocation = returnLocation;
210 }
211
212 public String getReturnFormKey() {
213 return this.returnFormKey;
214 }
215
216 public void setReturnFormKey(String returnFormKey) {
217 this.returnFormKey = returnFormKey;
218 }
219
220 public String getHubLocation() {
221 return this.hubLocation;
222 }
223
224 public void setHubLocation(String hubLocation) {
225 this.hubLocation = hubLocation;
226 }
227
228 public String getHubFormKey() {
229 return this.hubFormKey;
230 }
231
232 public void setHubFormKey(String hubFormKey) {
233 this.hubFormKey = hubFormKey;
234 }
235
236 public String getHomeLocation() {
237 return this.homeLocation;
238 }
239
240 public void setHomeLocation(String homeLocation) {
241 this.homeLocation = homeLocation;
242 }
243
244 /**
245 * Identifies the controller method that should be invoked to fulfill a
246 * request. The value will be matched up against the 'params' setting on the
247 * <code>RequestMapping</code> annotation for the controller method
248 *
249 * @return String method to call
250 */
251 public String getMethodToCall() {
252 return this.methodToCall;
253 }
254
255 /**
256 * Setter for the method to call
257 *
258 * @param methodToCall
259 */
260 public void setMethodToCall(String methodToCall) {
261 this.methodToCall = methodToCall;
262 }
263
264 /**
265 * Map of parameters that was used to configured the <code>View</code>.
266 * Maintained on the form to rebuild the view on posts and session timeout
267 *
268 * @return Map<String, String> view parameters
269 * @see org.kuali.rice.kns.uif.container.View.getViewRequestParameters()
270 */
271 public Map<String, String> getViewRequestParameters() {
272 return this.viewRequestParameters;
273 }
274
275 /**
276 * Setter for the view's request parameter map
277 *
278 * @param viewRequestParameters
279 */
280 public void setViewRequestParameters(Map<String, String> viewRequestParameters) {
281 this.viewRequestParameters = viewRequestParameters;
282 }
283
284 /**
285 * Holds instances for collection add lines. The key of the Map gives the
286 * collection name the line instance applies to, the Map value is an
287 * instance of the collection object class that holds the new line data
288 *
289 * @return Map<String, Object> new collection lines
290 */
291 public Map<String, Object> getNewCollectionLines() {
292 return this.newCollectionLines;
293 }
294
295 /**
296 * Setter for the new collection lines Map
297 *
298 * @param newCollectionLines
299 */
300 public void setNewCollectionLines(Map<String, Object> newCollectionLines) {
301 this.newCollectionLines = newCollectionLines;
302 }
303
304 /**
305 * Map of parameters sent for the invoked action
306 * <p>
307 * Many times besides just setting the method to call actions need to send
308 * additional parameters. For instance the method being called might do a
309 * redirect, in which case the action needs to send parameters for the
310 * redirect URL. An example of this is redirecting to a <code>Lookup</code>
311 * view. In some cases the parameters that need to be sent conflict with
312 * properties already on the form, and putting all the action parameters as
313 * form properties would grow massive (in addition to adds an additional
314 * step from the XML config). So this general map solves those issues.
315 * </p>
316 *
317 * @return Map<String, String> action parameters
318 */
319 public Map<String, String> getActionParameters() {
320 return this.actionParameters;
321 }
322
323 /**
324 * Returns the action parameters map as a <code>Properties</code> instance
325 *
326 * @return Properties action parameters
327 */
328 public Properties getActionParametersAsProperties() {
329 Properties actionProperties = new Properties();
330
331 if (actionParameters != null) {
332 for (Map.Entry<String, String> actionParameter : actionParameters.entrySet()) {
333 actionProperties.put(actionParameter.getKey(), actionParameter.getValue());
334 }
335 }
336
337 return actionProperties;
338 }
339
340 /**
341 * Setter for the action parameters map
342 *
343 * @param actionParameters
344 */
345 public void setActionParameters(Map<String, String> actionParameters) {
346 this.actionParameters = actionParameters;
347 }
348
349 /**
350 * Retrieves the value for the given action parameter, or empty string if
351 * not found
352 *
353 * @param actionParameterName
354 * - name of the action parameter to retrieve value for
355 * @return String parameter value or empty string
356 */
357 public String getActionParamaterValue(String actionParameterName) {
358 if ((actionParameters != null) && actionParameters.containsKey(actionParameterName)) {
359 return actionParameters.get(actionParameterName);
360 }
361
362 return "";
363 }
364
365 /**
366 * Key string that identifies the form instance in session storage
367 * <p>
368 * When the view is posted, the previous form instance is retrieved and then
369 * populated from the request parameters. This key string is retrieve the
370 * session form from the session service
371 * </p>
372 *
373 * @return String form session key
374 */
375 public String getFormKey() {
376 return this.formKey;
377 }
378
379 /**
380 * Setter for the form's session key
381 *
382 * @param formKey
383 */
384 public void setFormKey(String formKey) {
385 this.formKey = formKey;
386 }
387
388 /**
389 * Indicates whether the form has had default values from the configured
390 * <code>View</code> applied. This happens only once for each form instance
391 *
392 * @return boolean true if default values have been applied, false if not
393 */
394 public boolean isDefaultsApplied() {
395 return this.defaultsApplied;
396 }
397
398 /**
399 * Setter for the defaults applied indicator
400 *
401 * @param defaultsApplied
402 */
403 public void setDefaultsApplied(boolean defaultsApplied) {
404 this.defaultsApplied = defaultsApplied;
405 }
406
407 /**
408 * Holder for files that are attached through the view
409 *
410 * @return MultipartFile representing the attachment
411 */
412 public MultipartFile getAttachmentFile() {
413 return this.attachmentFile;
414 }
415
416 /**
417 * Setter for the form's attachment file
418 *
419 * @param attachmentFile
420 */
421 public void setAttachmentFile(MultipartFile attachmentFile) {
422 this.attachmentFile = attachmentFile;
423 }
424
425 /**
426 * @return the renderFullView
427 */
428 public boolean isRenderFullView() {
429 return this.renderFullView;
430 }
431
432 /**
433 * @param renderFullView
434 */
435 public void setRenderFullView(boolean renderFullView) {
436 this.renderFullView = renderFullView;
437 }
438
439 /**
440 * View instance associated with the form. Used to render the user interface
441 *
442 * @return View
443 */
444 public View getView() {
445 return this.view;
446 }
447
448 /**
449 * Setter for the view instance
450 *
451 * @param view
452 */
453 public void setView(View view) {
454 this.view = view;
455 }
456
457 /**
458 * View instance for the page that made a request. Since a new view instance
459 * gets initialized for each request before the controller logic is invoked,
460 * any state about the previous view is lost. This could be needed to read
461 * metadata from the view for such things as collection processing. When
462 * this is necessary the previous view instance can be retrieved
463 *
464 * @return View instance
465 */
466 public View getPreviousView() {
467 return this.previousView;
468 }
469
470 /**
471 * Setter for the previous view instance
472 *
473 * @param previousView
474 */
475 public void setPreviousView(View previousView) {
476 this.previousView = previousView;
477 }
478
479 /**
480 * Instance of the <code>ViewService</code> that can be used to retrieve
481 * <code>View</code> instances
482 *
483 * @return ViewService implementation
484 */
485 protected ViewService getViewService() {
486 return KNSServiceLocatorWeb.getViewService();
487 }
488
489 /**
490 * The jumpToId for this form, the element with this id will be jumped to automatically
491 * when the form is loaded in the view.
492 * Using "TOP" or "BOTTOM" will jump to the top or the bottom of the resulting page.
493 * jumpToId always takes precedence over jumpToName, if set.
494 *
495 * @return the jumpToId
496 */
497 public String getJumpToId() {
498 return this.jumpToId;
499 }
500
501 /**
502 * @param jumpToId the jumpToId to set
503 */
504 public void setJumpToId(String jumpToId) {
505 this.jumpToId = jumpToId;
506 }
507
508 /**
509 * The jumpToName for this form, the element with this name will be jumped to automatically
510 * when the form is loaded in the view.
511 * WARNING: jumpToId always takes precedence over jumpToName, if set.
512 *
513 * @return the jumpToName
514 */
515 public String getJumpToName() {
516 return this.jumpToName;
517 }
518
519 /**
520 * @param jumpToName the jumpToName to set
521 */
522 public void setJumpToName(String jumpToName) {
523 this.jumpToName = jumpToName;
524 }
525
526 /**
527 * Field to place focus on when the page loads
528 * An empty focusId will result in focusing on the first visible input element by default.
529 *
530 * @return the focusId
531 */
532 public String getFocusId() {
533 return this.focusId;
534 }
535
536 /**
537 * @param focusId the focusId to set
538 */
539 public void setFocusId(String focusId) {
540 this.focusId = focusId;
541 }
542
543 /**
544 * History parameter representing the History of views that have come before the
545 * viewing of the current view. Used for breadcrumb widget generation on the view.
546 *
547 * @param history the history to set
548 */
549 public void setFormHistory(History history) {
550 this.formHistory = history;
551 }
552
553 /**
554 * @return the history
555 */
556 public History getFormHistory() {
557 return formHistory;
558 }
559
560 public boolean isValidateDirty() {
561 return this.validateDirty;
562 }
563
564 /**
565 * Setter for dirty validation.
566 */
567 public void setValidateDirty(boolean validateDirty) {
568 this.validateDirty = validateDirty;
569 }
570
571 }