1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.web.struts.form;
17
18 import org.apache.commons.lang.ArrayUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.api.util.Truth;
21 import org.kuali.rice.kns.lookup.LookupUtils;
22 import org.kuali.rice.kns.lookup.Lookupable;
23 import org.kuali.rice.kns.service.KNSServiceLocator;
24 import org.kuali.rice.kns.web.ui.Field;
25 import org.kuali.rice.kns.web.ui.Row;
26 import org.kuali.rice.krad.bo.BusinessObject;
27 import org.kuali.rice.krad.service.DataDictionaryService;
28 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29 import org.kuali.rice.krad.util.ExternalizableBusinessObjectUtils;
30 import org.kuali.rice.krad.util.KRADConstants;
31
32 import javax.servlet.http.HttpServletRequest;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.TreeMap;
38
39
40
41
42 public class LookupForm extends KualiForm {
43 private static final long serialVersionUID = 1L;
44
45 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupForm.class);
46 protected static final String HEADER_BAR_ENABLED_PARAM = "headerBarEnabled";
47 protected static final String SEARCH_CRITERIA_ENABLED_PARAM = "searchCriteriaEnabled";
48
49 private String formKey;
50 private Map<String, String> fields;
51 private Map<String, String> fieldsForLookup;
52 private String lookupableImplServiceName;
53 private String conversionFields;
54 private Map<String, String> fieldConversions;
55 private String businessObjectClassName;
56 private Lookupable lookupable;
57 private boolean hideReturnLink = false;
58 private boolean suppressActions = false;
59 private boolean multipleValues = false;
60 private String lookupAnchor;
61 private String readOnlyFields;
62 private List readOnlyFieldsList;
63 private String referencesToRefresh;
64 private boolean searchUsingOnlyPrimaryKeyValues;
65 private String primaryKeyFieldLabels;
66 private boolean showMaintenanceLinks = false;
67 private String docNum;
68 private String htmlDataType;
69 private String lookupObjectId;
70 private boolean lookupCriteriaEnabled = true;
71 private boolean supplementalActionsEnabled = false;
72 private boolean actionUrlsExist = false;
73 private boolean ddExtraButton = false;
74 private boolean headerBarEnabled = true;
75 private boolean disableSearchButtons = false;
76 private String isAdvancedSearch;
77
78
79
80
81 public void addRequiredNonEditableProperties(){
82 super.addRequiredNonEditableProperties();
83 registerRequiredNonEditableProperty(KRADConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME);
84 registerRequiredNonEditableProperty(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE);
85 registerRequiredNonEditableProperty(KRADConstants.DISPATCH_REQUEST_PARAMETER);
86 registerRequiredNonEditableProperty(KRADConstants.DOC_FORM_KEY);
87 registerRequiredNonEditableProperty(KRADConstants.REFRESH_CALLER);
88 registerRequiredNonEditableProperty(KRADConstants.DOC_NUM);
89 registerRequiredNonEditableProperty(KRADConstants.REFERENCES_TO_REFRESH);
90 registerRequiredNonEditableProperty(KRADConstants.FORM_KEY);
91 registerRequiredNonEditableProperty(KRADConstants.CONVERSION_FIELDS_PARAMETER);
92 registerRequiredNonEditableProperty(KRADConstants.FIELDS_CONVERSION_PARAMETER);
93 registerRequiredNonEditableProperty(KRADConstants.HIDE_LOOKUP_RETURN_LINK);
94 registerRequiredNonEditableProperty(KRADConstants.MULTIPLE_VALUE);
95 registerRequiredNonEditableProperty(KRADConstants.BACK_LOCATION);
96 registerRequiredNonEditableProperty(KRADConstants.LOOKUP_ANCHOR);
97 registerRequiredNonEditableProperty("searchUsingOnlyPrimaryKeyValues");
98 registerRequiredNonEditableProperty(KRADConstants.MULTIPLE_VALUE_LOOKUP_PREVIOUSLY_SELECTED_OBJ_IDS_PARAM);
99 registerRequiredNonEditableProperty(KRADConstants.TableRenderConstants.VIEWED_PAGE_NUMBER);
100 }
101
102
103
104
105 public String getHtmlDataType() {
106 return this.htmlDataType;
107 }
108
109
110
111
112 public void setHtmlDataType(String htmlDataType) {
113 this.htmlDataType = htmlDataType;
114 }
115
116
117
118
119 public String getDocNum() {
120 return this.docNum;
121 }
122
123
124
125
126 public void setDocNum(String docNum) {
127 this.docNum = docNum;
128 }
129
130
131
132
133 private boolean hasReturnableRow;
134
135
136
137 private Map requestParameters;
138
139
140
141
142 @Override
143 public void postprocessRequestParameters(Map requestParameters) {
144 this.requestParameters = requestParameters;
145 super.postprocessRequestParameters(requestParameters);
146 }
147
148
149
150
151 public void populate(HttpServletRequest request) {
152 super.populate(request);
153
154 DataDictionaryService ddService = KRADServiceLocatorWeb.getDataDictionaryService();
155
156 try {
157 Lookupable localLookupable = null;
158 if (StringUtils.isBlank(getParameter(request, KRADConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME)) && StringUtils.isBlank(getLookupableImplServiceName())) {
159
160 String localBusinessObjectClassName = getParameter(request, KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE);
161 if ( ExternalizableBusinessObjectUtils.isExternalizableBusinessObjectInterface(localBusinessObjectClassName) ) {
162 Class localBusinessObjectClass = Class.forName(localBusinessObjectClassName);
163 localBusinessObjectClassName = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(localBusinessObjectClass).getExternalizableBusinessObjectImplementation(localBusinessObjectClass).getName();
164 }
165 setBusinessObjectClassName(localBusinessObjectClassName);
166 if (StringUtils.isBlank(localBusinessObjectClassName)) {
167 LOG.error("Business object class not passed to lookup.");
168 throw new RuntimeException("Business object class not passed to lookup.");
169 }
170
171
172 String lookupImplID = KNSServiceLocator.getBusinessObjectDictionaryService().getLookupableID(Class.forName(localBusinessObjectClassName));
173 if (lookupImplID == null) {
174 lookupImplID = "kualiLookupable";
175 }
176
177 setLookupableImplServiceName(lookupImplID);
178 }
179
180 localLookupable = this.getLookupable(getLookupableImplServiceName());
181
182 if (localLookupable == null) {
183 LOG.error("Lookup impl not found for lookup impl name " + getLookupableImplServiceName());
184 throw new RuntimeException("Lookup impl not found for lookup impl name " + getLookupableImplServiceName());
185 }
186
187
188 Map<String, String[]> parameters = new TreeMap<String, String[]>();
189 parameters.putAll(requestParameters);
190 parameters.put(KRADConstants.ADVANCED_SEARCH_FIELD, new String[]{isAdvancedSearch});
191 localLookupable.setParameters(parameters);
192 requestParameters = null;
193
194 if (getParameter(request, KRADConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME) != null) {
195 setLookupableImplServiceName(getParameter(request, KRADConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME));
196 }
197
198 if (getParameter(request, KRADConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME) != null) {
199 setLookupableImplServiceName(getParameter(request, KRADConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME));
200 }
201
202
203 if (request.getAttribute(KRADConstants.DOC_FORM_KEY) != null && StringUtils.isBlank(this.getFormKey())) {
204 setFormKey((String) request.getAttribute(KRADConstants.DOC_FORM_KEY));
205 }
206 else if (getParameter(request, KRADConstants.DOC_FORM_KEY) != null && StringUtils.isBlank(this.getFormKey())) {
207 setFormKey(getParameter(request, KRADConstants.DOC_FORM_KEY));
208 }
209
210 if (getParameter(request, KRADConstants.DOC_NUM) != null) {
211 setDocNum(getParameter(request, KRADConstants.DOC_NUM));
212 }
213
214 String returnLocation = getParameter(request, "returnLocation");
215 if (StringUtils.isNotBlank(returnLocation)) {
216 setBackLocation(returnLocation);
217 localLookupable.getLookupableHelperService().setBackLocation(returnLocation);
218 }
219
220 if (getParameter(request, "conversionFields") != null) {
221 setConversionFields(getParameter(request, "conversionFields"));
222 }
223 if (getParameter(request, KRADConstants.EXTRA_BUTTON_SOURCE) != null) {
224
225 ddExtraButton=false;
226 setExtraButtonSource(getParameter(request, KRADConstants.EXTRA_BUTTON_SOURCE));
227 }
228 if (getParameter(request, KRADConstants.EXTRA_BUTTON_PARAMS) != null) {
229 setExtraButtonParams(getParameter(request, KRADConstants.EXTRA_BUTTON_PARAMS));
230 }
231 String value = getParameter(request, "multipleValues");
232 if (value != null) {
233 if ("YES".equals(value.toUpperCase())) {
234 setMultipleValues(true);
235 }
236 else {
237 setMultipleValues(new Boolean(getParameter(request, "multipleValues")).booleanValue());
238 }
239 }
240 if (getParameter(request, KRADConstants.REFERENCES_TO_REFRESH) != null) {
241 setReferencesToRefresh(getParameter(request, KRADConstants.REFERENCES_TO_REFRESH));
242 }
243
244 if (getParameter(request, "readOnlyFields") != null) {
245 setReadOnlyFields(getParameter(request, "readOnlyFields"));
246 setReadOnlyFieldsList(LookupUtils.translateReadOnlyFieldsToList(this.readOnlyFields));
247 localLookupable.setReadOnlyFieldsList(getReadOnlyFieldsList());
248 }
249
250
251
252
253
254
255
256 setHeaderBarEnabled(Truth.strToBooleanIgnoreCase(getParameter(request, HEADER_BAR_ENABLED_PARAM), Boolean.TRUE));
257 setLookupCriteriaEnabled(Truth.strToBooleanIgnoreCase(getParameter(request, SEARCH_CRITERIA_ENABLED_PARAM), Boolean.TRUE));
258
259
260 localLookupable.setBusinessObjectClass((Class<? extends BusinessObject>) Class.forName(getBusinessObjectClassName()));
261 Map<String, String> fieldValues = new HashMap<String, String>();
262 Map<String, String> formFields = getFields();
263 Class boClass = Class.forName(getBusinessObjectClassName());
264
265 for (Iterator iter = localLookupable.getRows().iterator(); iter.hasNext();) {
266 Row row = (Row) iter.next();
267
268 for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
269 Field field = (Field) iterator.next();
270
271
272 if (formFields != null && formFields.containsKey(field.getPropertyName())) {
273 field.setPropertyValue(formFields.get(field.getPropertyName()));
274 }
275
276
277 if (getParameter(request, field.getPropertyName()) != null) {
278 if(!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) {
279 field.setPropertyValue(getParameter(request, field.getPropertyName()).trim());
280 } else {
281
282 field.setPropertyValues(getParameterValues(request, field.getPropertyName()));
283 }
284 }
285
286 field.setPropertyValue(org.kuali.rice.krad.lookup.LookupUtils
287 .forceUppercase(boClass, field.getPropertyName(), field.getPropertyValue()));
288 fieldValues.put(field.getPropertyName(), field.getPropertyValue());
289
290 localLookupable.applyFieldAuthorizationsFromNestedLookups(field);
291 }
292 }
293
294 if (localLookupable.checkForAdditionalFields(fieldValues)) {
295 for (Iterator iter = localLookupable.getRows().iterator(); iter.hasNext();) {
296 Row row = (Row) iter.next();
297
298 for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
299 Field field = (Field) iterator.next();
300
301
302 if (formFields != null && formFields.containsKey(field.getPropertyName())) {
303 field.setPropertyValue(formFields.get(field.getPropertyName()));
304 }
305
306
307 if (getParameter(request, field.getPropertyName()) != null) {
308 if(!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) {
309 field.setPropertyValue(getParameter(request, field.getPropertyName()).trim());
310 } else {
311
312 field.setPropertyValues(getParameterValues(request, field.getPropertyName()));
313 }
314 }
315 fieldValues.put(field.getPropertyName(), field.getPropertyValue());
316 }
317 }
318
319 }
320 fieldValues.put(KRADConstants.DOC_FORM_KEY, this.getFormKey());
321 fieldValues.put(KRADConstants.BACK_LOCATION, this.getBackLocation());
322 if(this.getDocNum() != null){
323 fieldValues.put(KRADConstants.DOC_NUM, this.getDocNum());
324 }
325 if (StringUtils.isNotBlank(getReferencesToRefresh())) {
326 fieldValues.put(KRADConstants.REFERENCES_TO_REFRESH, this.getReferencesToRefresh());
327 }
328
329 this.setFields(fieldValues);
330
331 setFieldConversions(LookupUtils.translateFieldConversions(this.conversionFields));
332 localLookupable.setFieldConversions(getFieldConversions());
333 if(StringUtils.isNotEmpty(localLookupable.getExtraButtonSource())) {
334 setExtraButtonSource(localLookupable.getExtraButtonSource());
335
336 ddExtraButton=true;
337 }
338 if(StringUtils.isNotEmpty(localLookupable.getExtraButtonParams())) {
339 setExtraButtonParams(localLookupable.getExtraButtonParams());
340 }
341 setLookupable(localLookupable);
342 setFieldsForLookup(fieldValues);
343
344
345 if (!isShowMaintenanceLinks()) {
346 if (StringUtils.contains(getBackLocation(), "/"+ KRADConstants.PORTAL_ACTION)
347 || StringUtils.contains(getBackLocation(), "/index.html")) {
348 showMaintenanceLinks = true;
349 }
350 }
351 }
352 catch (ClassNotFoundException e) {
353 LOG.error("Business Object class " + getBusinessObjectClassName() + " not found");
354 throw new RuntimeException("Business Object class " + getBusinessObjectClassName() + " not found", e);
355 }
356 }
357
358
359
360
361 public String getLookupableImplServiceName() {
362 return lookupableImplServiceName;
363 }
364
365 protected Lookupable getLookupable(String beanName) {
366 return KNSServiceLocator.getLookupable(beanName);
367 }
368
369
370
371
372 public void setLookupableImplServiceName(String lookupableImplServiceName) {
373 this.lookupableImplServiceName = lookupableImplServiceName;
374 }
375
376
377
378
379 public String getFormKey() {
380 return formKey;
381 }
382
383
384
385
386 public void setFormKey(String formKey) {
387 this.formKey = formKey;
388 }
389
390
391
392
393 public Map<String, String> getFields() {
394 return fields;
395 }
396
397
398
399
400 public void setFields(Map<String, String> fields) {
401 this.fields = fields;
402 }
403
404
405
406
407 public String getConversionFields() {
408 return conversionFields;
409 }
410
411
412
413
414 public void setConversionFields(String conversionFields) {
415 this.conversionFields = conversionFields;
416 }
417
418
419
420
421 public Map<String, String> getFieldConversions() {
422 return fieldConversions;
423 }
424
425
426
427
428 public void setFieldConversions(Map<String, String> fieldConversions) {
429 this.fieldConversions = fieldConversions;
430 }
431
432
433
434
435 public String getBusinessObjectClassName() {
436 return businessObjectClassName;
437 }
438
439
440
441
442 public void setBusinessObjectClassName(String businessObjectClassName) {
443 this.businessObjectClassName = businessObjectClassName;
444 }
445
446
447
448
449
450 public Lookupable getLookupable() {
451 return lookupable;
452 }
453
454
455
456
457
458 public void setLookupable(Lookupable lookupable) {
459 this.lookupable = lookupable;
460 }
461
462
463
464
465
466 public boolean isHideReturnLink() {
467 return hideReturnLink;
468 }
469
470
471
472
473 public void setSuppressActions(boolean suppressActions) {
474 this.suppressActions = suppressActions;
475 }
476
477
478
479
480 public boolean isSuppressActions() {
481 return suppressActions;
482 }
483
484
485
486
487
488 public void setHideReturnLink(boolean hideReturnLink) {
489 this.hideReturnLink = hideReturnLink;
490 }
491
492
493 public String getExtraButtonParams() {
494 return extraButtons.get(0).getExtraButtonParams();
495 }
496
497
498 public void setExtraButtonParams(String extraButtonParams) {
499 extraButtons.get(0).setExtraButtonParams( extraButtonParams );
500 }
501
502
503 public String getExtraButtonSource() {
504 return extraButtons.get(0).getExtraButtonSource();
505 }
506
507
508 public void setExtraButtonSource(String extraButtonSource) {
509 extraButtons.get(0).setExtraButtonSource( extraButtonSource );
510 }
511
512
513
514
515
516
517 public boolean isMultipleValues() {
518 return multipleValues;
519 }
520
521
522
523
524
525 public void setMultipleValues(boolean multipleValues) {
526 this.multipleValues = multipleValues;
527 }
528
529 public String getLookupAnchor() {
530 return lookupAnchor;
531 }
532
533 public void setLookupAnchor(String lookupAnchor) {
534 this.lookupAnchor = lookupAnchor;
535 }
536
537
538
539
540
541 public Map getFieldsForLookup() {
542 return fieldsForLookup;
543 }
544
545
546
547
548
549 public void setFieldsForLookup(Map fieldsForLookup) {
550 this.fieldsForLookup = fieldsForLookup;
551 }
552
553
554
555
556
557 public String getReadOnlyFields() {
558 return readOnlyFields;
559 }
560
561
562
563
564
565 public void setReadOnlyFields(String readOnlyFields) {
566 this.readOnlyFields = readOnlyFields;
567 }
568
569
570
571
572
573 public List getReadOnlyFieldsList() {
574 return readOnlyFieldsList;
575 }
576
577
578
579
580
581 public void setReadOnlyFieldsList(List readOnlyFieldsList) {
582 this.readOnlyFieldsList = readOnlyFieldsList;
583 }
584
585 public String getReferencesToRefresh() {
586 return referencesToRefresh;
587 }
588
589 public void setReferencesToRefresh(String referencesToRefresh) {
590 this.referencesToRefresh = referencesToRefresh;
591 }
592
593 public String getPrimaryKeyFieldLabels() {
594 return primaryKeyFieldLabels;
595 }
596
597 public void setPrimaryKeyFieldLabels(String primaryKeyFieldLabels) {
598 this.primaryKeyFieldLabels = primaryKeyFieldLabels;
599 }
600
601 public boolean isSearchUsingOnlyPrimaryKeyValues() {
602 return searchUsingOnlyPrimaryKeyValues;
603 }
604
605 public void setSearchUsingOnlyPrimaryKeyValues(boolean searchUsingOnlyPrimaryKeyValues) {
606 this.searchUsingOnlyPrimaryKeyValues = searchUsingOnlyPrimaryKeyValues;
607 }
608
609
610
611
612
613 public boolean isShowMaintenanceLinks() {
614 return showMaintenanceLinks;
615 }
616
617
618
619
620
621 public void setShowMaintenanceLinks(boolean showMaintenanceLinks) {
622 this.showMaintenanceLinks = showMaintenanceLinks;
623 }
624
625
626
627
628
629
630 public boolean isHasReturnableRow() {
631 return this.hasReturnableRow;
632 }
633
634
635
636
637
638
639 public void setHasReturnableRow(boolean hasReturnableRow) {
640 this.hasReturnableRow = hasReturnableRow;
641 }
642
643
644
645
646 public String getLookupObjectId() {
647 return this.lookupObjectId;
648 }
649
650
651
652
653 public void setLookupObjectId(String lookupObjectId) {
654 this.lookupObjectId = lookupObjectId;
655 }
656
657
658
659
660 public boolean isLookupCriteriaEnabled() {
661 return this.lookupCriteriaEnabled;
662 }
663
664
665
666
667 public void setLookupCriteriaEnabled(boolean lookupCriteriaEnabled) {
668 this.lookupCriteriaEnabled = lookupCriteriaEnabled;
669 }
670
671
672
673
674 public boolean isSupplementalActionsEnabled() {
675 return this.supplementalActionsEnabled;
676 }
677
678
679
680
681 public void setSupplementalActionsEnabled(boolean supplementalActionsEnabled) {
682 this.supplementalActionsEnabled = supplementalActionsEnabled;
683 }
684
685
686
687
688
689 public void setActionUrlsExist(boolean actionUrlsExist) {
690 this.actionUrlsExist = actionUrlsExist;
691 }
692
693
694
695
696 public boolean isActionUrlsExist() {
697 return actionUrlsExist;
698 }
699
700
701
702
703 public boolean isDdExtraButton() {
704 return this.ddExtraButton;
705 }
706
707
708
709
710 public void setDdExtraButton(boolean ddExtraButton) {
711 this.ddExtraButton = ddExtraButton;
712 }
713
714 public boolean isHeaderBarEnabled() {
715 return headerBarEnabled;
716 }
717
718 public void setHeaderBarEnabled(boolean headerBarEnabled) {
719 this.headerBarEnabled = headerBarEnabled;
720 }
721
722 public boolean isDisableSearchButtons() {
723 return this.disableSearchButtons;
724 }
725
726 public void setDisableSearchButtons(boolean disableSearchButtons) {
727 this.disableSearchButtons = disableSearchButtons;
728 }
729
730
731
732
733
734
735
736
737
738
739
740
741 public String getIsAdvancedSearch() {
742 return this.isAdvancedSearch;
743 }
744
745
746
747
748
749
750 public void setIsAdvancedSearch(String advancedSearch) {
751 this.isAdvancedSearch = advancedSearch;
752 }
753
754
755
756
757
758
759
760 public boolean getRenderSearchButtons() {
761 boolean renderSearchButtons = true;
762
763 if (disableSearchButtons
764 || KNSServiceLocator.getBusinessObjectDictionaryService().disableSearchButtonsInLookup(
765 getLookupable().getBusinessObjectClass())) {
766 renderSearchButtons = false;
767 }
768
769 return renderSearchButtons;
770 }
771 }