1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.web.form;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.codehaus.jackson.map.ObjectMapper;
22 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
23 import org.kuali.rice.krad.uif.UifConstants;
24 import org.kuali.rice.krad.uif.UifParameters;
25 import org.kuali.rice.krad.uif.view.History;
26 import org.kuali.rice.krad.uif.view.HistoryEntry;
27 import org.kuali.rice.krad.uif.view.View;
28 import org.kuali.rice.krad.uif.service.ViewService;
29 import org.kuali.rice.krad.uif.view.ViewModel;
30 import org.kuali.rice.krad.util.KRADUtils;
31 import org.springframework.web.multipart.MultipartFile;
32 import org.kuali.rice.krad.uif.UifConstants.ViewType;
33
34 import javax.servlet.http.HttpServletRequest;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Properties;
41 import java.util.Set;
42 import java.util.UUID;
43
44
45
46
47
48
49
50
51
52
53
54 public class UifFormBase implements ViewModel {
55 private static final long serialVersionUID = 8432543267099454434L;
56
57
58 private static final Log LOG = LogFactory.getLog(UifFormBase.class);
59
60
61 protected String viewId;
62 protected String viewName;
63 protected ViewType viewTypeName;
64 protected String pageId;
65 protected String methodToCall;
66 protected String formKey;
67 protected String jumpToId;
68 protected String jumpToName;
69 protected String focusId;
70 protected String formPostUrl;
71
72 protected boolean defaultsApplied;
73 protected boolean skipViewInit;
74
75 protected View view;
76 protected View postedView;
77
78 protected Map<String, String> viewRequestParameters;
79 protected List<String> readOnlyFieldsList;
80
81 protected Map<String, Object> newCollectionLines;
82 protected Map<String, String> actionParameters;
83 protected Map<String, Object> clientStateForSyncing;
84 protected Map<String, Set<String>> selectedCollectionLines;
85
86 protected MultipartFile attachmentFile;
87
88
89 protected String returnLocation;
90 protected String returnFormKey;
91
92 protected History formHistory;
93
94 protected boolean renderFullView;
95 protected boolean validateDirty;
96
97 public UifFormBase() {
98 formKey = generateFormKey();
99 renderFullView = true;
100 defaultsApplied = false;
101 skipViewInit = false;
102
103 formHistory = new History();
104
105 readOnlyFieldsList = new ArrayList<String>();
106 viewRequestParameters = new HashMap<String, String>();
107 newCollectionLines = new HashMap<String, Object>();
108 actionParameters = new HashMap<String, String>();
109 clientStateForSyncing = new HashMap<String, Object>();
110 selectedCollectionLines = new HashMap<String, Set<String>>();
111 }
112
113
114
115
116
117
118
119 protected String generateFormKey() {
120 return UUID.randomUUID().toString();
121 }
122
123
124
125
126
127
128
129 public void postBind(HttpServletRequest request) {
130
131 formPostUrl = request.getRequestURL().toString();
132
133
134 if (request.getParameterMap().containsKey(UifParameters.CLIENT_VIEW_STATE)) {
135 String clientStateJSON = request.getParameter(UifParameters.CLIENT_VIEW_STATE);
136 if (StringUtils.isNotBlank(clientStateJSON)) {
137
138 clientStateJSON = StringUtils.replace(clientStateJSON, "'", "\"");
139
140 ObjectMapper mapper = new ObjectMapper();
141 try {
142 clientStateForSyncing = mapper.readValue(clientStateJSON, Map.class);
143 } catch (IOException e) {
144 throw new RuntimeException("Unable to decode client side state JSON", e);
145 }
146 }
147 }
148
149
150 if (request.getParameter(UifParameters.READ_ONLY_FIELDS) != null) {
151 String readOnlyFields = request.getParameter(UifParameters.READ_ONLY_FIELDS);
152 setReadOnlyFieldsList(KRADUtils.convertStringParameterToList(readOnlyFields));
153 }
154
155
156 if (!request.getParameterMap().containsKey(UifParameters.SKIP_VIEW_INIT)) {
157 skipViewInit = false;
158 }
159 }
160
161
162
163
164 public String getViewId() {
165 return this.viewId;
166 }
167
168
169
170
171 public void setViewId(String viewId) {
172 this.viewId = viewId;
173 }
174
175
176
177
178 public String getViewName() {
179 return this.viewName;
180 }
181
182
183
184
185 public void setViewName(String viewName) {
186 this.viewName = viewName;
187 }
188
189
190
191
192 public ViewType getViewTypeName() {
193 return this.viewTypeName;
194 }
195
196
197
198
199 public void setViewTypeName(ViewType viewTypeName) {
200 this.viewTypeName = viewTypeName;
201 }
202
203
204
205
206 public String getPageId() {
207 return this.pageId;
208 }
209
210
211
212
213 public void setPageId(String pageId) {
214 this.pageId = pageId;
215 }
216
217
218
219
220 public String getFormPostUrl() {
221 return this.formPostUrl;
222 }
223
224
225
226
227 public void setFormPostUrl(String formPostUrl) {
228 this.formPostUrl = formPostUrl;
229 }
230
231 public String getReturnLocation() {
232 return this.returnLocation;
233 }
234
235 public void setReturnLocation(String returnLocation) {
236 this.returnLocation = returnLocation;
237 }
238
239 public String getReturnFormKey() {
240 return this.returnFormKey;
241 }
242
243 public void setReturnFormKey(String returnFormKey) {
244 this.returnFormKey = returnFormKey;
245 }
246
247
248
249
250
251
252
253
254 public String getMethodToCall() {
255 return this.methodToCall;
256 }
257
258
259
260
261
262
263 public void setMethodToCall(String methodToCall) {
264 this.methodToCall = methodToCall;
265 }
266
267
268
269
270 public Map<String, String> getViewRequestParameters() {
271 return this.viewRequestParameters;
272 }
273
274
275
276
277 public void setViewRequestParameters(Map<String, String> viewRequestParameters) {
278 this.viewRequestParameters = viewRequestParameters;
279 }
280
281
282
283
284 public List<String> getReadOnlyFieldsList() {
285 return readOnlyFieldsList;
286 }
287
288
289
290
291 public void setReadOnlyFieldsList(List<String> readOnlyFieldsList) {
292 this.readOnlyFieldsList = readOnlyFieldsList;
293 }
294
295
296
297
298 public Map<String, Object> getNewCollectionLines() {
299 return this.newCollectionLines;
300 }
301
302
303
304
305 public void setNewCollectionLines(Map<String, Object> newCollectionLines) {
306 this.newCollectionLines = newCollectionLines;
307 }
308
309
310
311
312 public Map<String, String> getActionParameters() {
313 return this.actionParameters;
314 }
315
316
317
318
319
320
321 public Properties getActionParametersAsProperties() {
322 return KRADUtils.convertMapToProperties(actionParameters);
323 }
324
325
326
327
328 public void setActionParameters(Map<String, String> actionParameters) {
329 this.actionParameters = actionParameters;
330 }
331
332
333
334
335
336
337
338
339 public String getActionParamaterValue(String actionParameterName) {
340 if ((actionParameters != null) && actionParameters.containsKey(actionParameterName)) {
341 return actionParameters.get(actionParameterName);
342 }
343
344 return "";
345 }
346
347
348
349
350
351
352
353
354
355
356
357
358
359 public String getActionEvent() {
360 if ((actionParameters != null) && actionParameters.containsKey(UifConstants.UrlParams.ACTION_EVENT)) {
361 return actionParameters.get(UifConstants.UrlParams.ACTION_EVENT);
362 }
363
364 return "";
365 }
366
367
368
369
370 public Map<String, Object> getClientStateForSyncing() {
371 return clientStateForSyncing;
372 }
373
374
375
376
377 public Map<String, Set<String>> getSelectedCollectionLines() {
378 return selectedCollectionLines;
379 }
380
381
382
383
384 public void setSelectedCollectionLines(Map<String, Set<String>> selectedCollectionLines) {
385 this.selectedCollectionLines = selectedCollectionLines;
386 }
387
388
389
390
391
392
393
394
395
396
397
398
399 public String getFormKey() {
400 return this.formKey;
401 }
402
403
404
405
406
407
408 public void setFormKey(String formKey) {
409 this.formKey = formKey;
410 }
411
412
413
414
415 public boolean isDefaultsApplied() {
416 return this.defaultsApplied;
417 }
418
419
420
421
422 public void setDefaultsApplied(boolean defaultsApplied) {
423 this.defaultsApplied = defaultsApplied;
424 }
425
426
427
428
429
430
431 public boolean isSkipViewInit() {
432 return skipViewInit;
433 }
434
435
436
437
438
439
440 public void setSkipViewInit(boolean skipViewInit) {
441 this.skipViewInit = skipViewInit;
442 }
443
444
445
446
447
448
449 public MultipartFile getAttachmentFile() {
450 return this.attachmentFile;
451 }
452
453
454
455
456
457
458 public void setAttachmentFile(MultipartFile attachmentFile) {
459 this.attachmentFile = attachmentFile;
460 }
461
462
463
464
465 public boolean isRenderFullView() {
466 return this.renderFullView;
467 }
468
469
470
471
472 public void setRenderFullView(boolean renderFullView) {
473 this.renderFullView = renderFullView;
474 }
475
476
477
478
479 public View getView() {
480 return this.view;
481 }
482
483
484
485
486 public void setView(View view) {
487 this.view = view;
488 initHomewardPathList();
489 }
490
491
492
493
494 private void initHomewardPathList() {
495 if (getReturnLocation() == null) {
496 LOG.warn("Could not init homewardPathList. returnLocation is null.");
497 return;
498 }
499
500 List<HistoryEntry> homewardPathList = new ArrayList<HistoryEntry>();
501 if ((view != null) && (view.getBreadcrumbs() != null) && (view.getBreadcrumbs().getHomewardPathList() != null)) {
502 homewardPathList = view.getBreadcrumbs().getHomewardPathList();
503 }
504
505 HistoryEntry historyEntry = new HistoryEntry("","","Home",getReturnLocation(),"");
506 if (homewardPathList.isEmpty()) {
507 homewardPathList.add(historyEntry);
508 } else if (StringUtils.equals(homewardPathList.get(0).getTitle(), "Home")) {
509 homewardPathList.set(0, historyEntry);
510 } else {
511 homewardPathList.add(0, historyEntry);
512 }
513 }
514
515
516
517
518 public View getPostedView() {
519 return this.postedView;
520 }
521
522
523
524
525 public void setPostedView(View postedView) {
526 this.postedView = postedView;
527 }
528
529
530
531
532
533
534
535 protected ViewService getViewService() {
536 return KRADServiceLocatorWeb.getViewService();
537 }
538
539
540
541
542
543
544
545
546
547 public String getJumpToId() {
548 return this.jumpToId;
549 }
550
551
552
553
554 public void setJumpToId(String jumpToId) {
555 this.jumpToId = jumpToId;
556 }
557
558
559
560
561
562
563
564
565 public String getJumpToName() {
566 return this.jumpToName;
567 }
568
569
570
571
572 public void setJumpToName(String jumpToName) {
573 this.jumpToName = jumpToName;
574 }
575
576
577
578
579
580
581
582 public String getFocusId() {
583 return this.focusId;
584 }
585
586
587
588
589 public void setFocusId(String focusId) {
590 this.focusId = focusId;
591 }
592
593
594
595
596
597
598
599
600
601
602
603
604 public History getFormHistory() {
605 return formHistory;
606 }
607
608
609
610
611
612
613 public void setFormHistory(History history) {
614 this.formHistory = history;
615 }
616
617
618
619
620
621
622
623
624
625
626
627
628
629 public boolean isValidateDirty() {
630 return this.validateDirty;
631 }
632
633
634
635
636
637
638 public void setValidateDirty(boolean validateDirty) {
639 this.validateDirty = validateDirty;
640 }
641
642 }