1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.lookup;
17
18 import org.apache.commons.beanutils.PropertyUtils;
19 import org.apache.commons.lang.BooleanUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.kuali.rice.core.api.CoreApiServiceLocator;
22 import org.kuali.rice.core.api.config.property.ConfigurationService;
23 import org.kuali.rice.core.api.encryption.EncryptionService;
24 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
25 import org.kuali.rice.core.api.search.SearchOperator;
26 import org.kuali.rice.core.api.util.RiceKeyConstants;
27 import org.kuali.rice.core.api.util.type.TypeUtils;
28 import org.kuali.rice.kim.api.identity.Person;
29 import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
30 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
31 import org.kuali.rice.krad.datadictionary.RelationshipDefinition;
32 import org.kuali.rice.krad.service.DataObjectAuthorizationService;
33 import org.kuali.rice.krad.service.DataObjectMetaDataService;
34 import org.kuali.rice.krad.service.DocumentDictionaryService;
35 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
36 import org.kuali.rice.krad.service.LookupService;
37 import org.kuali.rice.krad.service.ModuleService;
38 import org.kuali.rice.krad.uif.UifConstants;
39 import org.kuali.rice.krad.uif.UifParameters;
40 import org.kuali.rice.krad.uif.UifPropertyPaths;
41 import org.kuali.rice.krad.uif.control.Control;
42 import org.kuali.rice.krad.uif.control.HiddenControl;
43 import org.kuali.rice.krad.uif.control.ValueConfiguredControl;
44 import org.kuali.rice.krad.uif.element.Link;
45 import org.kuali.rice.krad.uif.field.InputField;
46 import org.kuali.rice.krad.uif.field.LinkField;
47 import org.kuali.rice.krad.uif.field.LookupInputField;
48 import org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl;
49 import org.kuali.rice.krad.uif.util.ComponentUtils;
50 import org.kuali.rice.krad.uif.util.LookupInquiryUtils;
51 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
52 import org.kuali.rice.krad.uif.view.History;
53 import org.kuali.rice.krad.uif.view.HistoryEntry;
54 import org.kuali.rice.krad.uif.view.LookupView;
55 import org.kuali.rice.krad.uif.view.View;
56 import org.kuali.rice.krad.util.BeanPropertyComparator;
57 import org.kuali.rice.krad.util.GlobalVariables;
58 import org.kuali.rice.krad.util.KRADConstants;
59 import org.kuali.rice.krad.util.KRADUtils;
60 import org.kuali.rice.krad.util.ObjectUtils;
61 import org.kuali.rice.krad.util.UrlFactory;
62 import org.kuali.rice.krad.web.form.LookupForm;
63 import org.kuali.rice.krad.web.form.UifFormBase;
64
65 import java.security.GeneralSecurityException;
66 import java.util.ArrayList;
67 import java.util.Collection;
68 import java.util.Collections;
69 import java.util.HashMap;
70 import java.util.List;
71 import java.util.Map;
72 import java.util.Properties;
73
74
75
76
77
78
79 public class LookupableImpl extends ViewHelperServiceImpl implements Lookupable {
80 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupableImpl.class);
81
82 private Class<?> dataObjectClass;
83
84 private transient ConfigurationService configurationService;
85 private transient DataObjectAuthorizationService dataObjectAuthorizationService;
86 private transient DataObjectMetaDataService dataObjectMetaDataService;
87 private transient DocumentDictionaryService documentDictionaryService;
88 private transient LookupService lookupService;
89 private transient EncryptionService encryptionService;
90
91
92
93
94
95
96
97
98
99 @Override
100 public void performInitialization(View view, Object model) {
101 if (!LookupView.class.isAssignableFrom(view.getClass())) {
102 throw new IllegalArgumentException(
103 "View class '" + view.getClass() + " is not assignable from the '" + LookupView.class + "'");
104 }
105
106 LookupView lookupView = (LookupView) view;
107 setDataObjectClass(lookupView.getDataObjectClassName());
108
109 super.performInitialization(view, model);
110 }
111
112
113
114
115 @Override
116 public void initSuppressAction(LookupForm lookupForm) {
117 LookupViewAuthorizerBase lookupAuthorizer = (LookupViewAuthorizerBase) lookupForm.getView().getAuthorizer();
118 Person user = GlobalVariables.getUserSession().getPerson();
119 ((LookupView) lookupForm.getView()).setSuppressActions(!lookupAuthorizer.canInitiateDocument(lookupForm, user));
120 }
121
122
123
124
125 @Override
126 public Collection<?> performSearch(LookupForm form, Map<String, String> searchCriteria, boolean bounded) {
127 Collection<?> displayList;
128
129
130 displayList = getSearchResults(form, LookupUtils.forceUppercase(getDataObjectClass(), searchCriteria),
131 !bounded);
132
133
134 for (Object object : displayList) {
135 if (isResultReturnable(object)) {
136 form.setAtLeastOneRowReturnable(true);
137 }
138 }
139
140 return displayList;
141 }
142
143
144
145
146
147
148
149
150
151
152 protected List<?> getSearchResults(LookupForm form, Map<String, String> searchCriteria, boolean unbounded) {
153 List<?> searchResults;
154
155
156 Map<String, String> nonBlankSearchCriteria = processSearchCriteria(form, searchCriteria);
157
158
159
160 if (nonBlankSearchCriteria == null) {
161 return new ArrayList<Object>();
162 }
163
164
165 if (ExternalizableBusinessObject.class.isAssignableFrom(getDataObjectClass())) {
166 return getSearchResultsForEBO(nonBlankSearchCriteria, unbounded);
167 }
168
169
170
171 try {
172 if (LookupUtils.hasExternalBusinessObjectProperty(getDataObjectClass(), nonBlankSearchCriteria)) {
173 Map<String, String> eboSearchCriteria = adjustCriteriaForNestedEBOs(nonBlankSearchCriteria, unbounded);
174
175 if (LOG.isDebugEnabled()) {
176 LOG.debug("Passing these results into the lookup service: " + eboSearchCriteria);
177 }
178
179
180 searchResults = (List<?>) getLookupService().findCollectionBySearchHelper(getDataObjectClass(),
181 eboSearchCriteria, unbounded);
182 } else {
183 searchResults = (List<?>) getLookupService().findCollectionBySearchHelper(getDataObjectClass(),
184 nonBlankSearchCriteria, unbounded);
185 }
186 } catch (IllegalAccessException e) {
187 throw new RuntimeException("Error trying to perform search", e);
188 } catch (InstantiationException e1) {
189 throw new RuntimeException("Error trying to perform search", e1);
190 }
191
192 if (searchResults == null) {
193 searchResults = new ArrayList<Object>();
194 } else {
195 sortSearchResults(form, searchResults);
196 }
197
198 return searchResults;
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216 protected void sortSearchResults(LookupForm form, List<?> searchResults) {
217 List<String> defaultSortColumns = null;
218
219
220
221 if (form.getPostedView() != null) {
222 defaultSortColumns = ((LookupView) form.getPostedView()).getDefaultSortAttributeNames();
223 }
224
225 else if (form.getView() != null) {
226 defaultSortColumns = ((LookupView) form.getView()).getDefaultSortAttributeNames();
227
228 boolean hasExpression = false;
229 if (defaultSortColumns != null) {
230 for (String sortColumn : defaultSortColumns) {
231 if (sortColumn == null) {
232 hasExpression = true;
233 }
234 }
235 }
236
237 if (hasExpression) {
238 defaultSortColumns = null;
239 }
240 }
241
242 if ((defaultSortColumns != null) && (defaultSortColumns.size() > 0)) {
243 Collections.sort(searchResults, new BeanPropertyComparator(defaultSortColumns, true));
244 }
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258
259 protected Map<String, String> processSearchCriteria(LookupForm lookupForm, Map<String, String> searchCriteria) {
260 Map<String, InputField> criteriaFields = new HashMap<String, InputField>();
261 if (lookupForm.getPostedView() != null) {
262 criteriaFields = getCriteriaFieldsForValidation((LookupView) lookupForm.getPostedView(), lookupForm);
263 }
264
265 Map<String, String> filteredSearchCriteria = new HashMap<String,String>(searchCriteria);
266 for (String fieldName: searchCriteria.keySet()) {
267 InputField inputField = criteriaFields.get(fieldName);
268 if ((inputField == null) || !(inputField instanceof LookupInputField)) {
269 continue;
270 }
271
272 filteredSearchCriteria = ((LookupInputField) inputField).filterSearchCriteria(filteredSearchCriteria);
273 if (filteredSearchCriteria == null) {
274 return null;
275 }
276 }
277
278 Map<String, String> nonBlankSearchCriteria = new HashMap<String, String>();
279 for (String fieldName : filteredSearchCriteria.keySet()) {
280 String fieldValue = filteredSearchCriteria.get(fieldName);
281
282
283 InputField inputField = criteriaFields.get(fieldName);
284 if ((inputField != null) && (inputField.getControl() instanceof HiddenControl)) {
285 continue;
286 }
287
288
289 if (StringUtils.isNotBlank(fieldValue)) {
290 if (fieldValue.endsWith(EncryptionService.ENCRYPTION_POST_PREFIX)) {
291 String encryptedValue = StringUtils.removeEnd(fieldValue, EncryptionService.ENCRYPTION_POST_PREFIX);
292 try {
293 if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
294 fieldValue = getEncryptionService().decrypt(encryptedValue);
295 }
296 } catch (GeneralSecurityException e) {
297 LOG.error("Error decrypting value for business object class " + getDataObjectClass() +
298 " attribute " + fieldName, e);
299 throw new RuntimeException(
300 "Error decrypting value for business object class " + getDataObjectClass() +
301 " attribute " + fieldName, e);
302 }
303 }
304
305 nonBlankSearchCriteria.put(fieldName, fieldValue);
306 }
307 }
308
309 return nonBlankSearchCriteria;
310 }
311
312
313
314
315
316
317
318
319
320 protected List<?> getSearchResultsForEBO(Map<String, String> searchCriteria, boolean unbounded) {
321 ModuleService eboModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(
322 getDataObjectClass());
323 BusinessObjectEntry ddEntry = eboModuleService.getExternalizableBusinessObjectDictionaryEntry(
324 getDataObjectClass());
325
326 Map<String, String> filteredFieldValues = new HashMap<String, String>();
327 for (String fieldName : searchCriteria.keySet()) {
328 if (ddEntry.getAttributeNames().contains(fieldName)) {
329 filteredFieldValues.put(fieldName, searchCriteria.get(fieldName));
330 }
331 }
332
333 List<?> searchResults = eboModuleService.getExternalizableBusinessObjectsListForLookup(
334 (Class<? extends ExternalizableBusinessObject>) getDataObjectClass(), (Map) filteredFieldValues,
335 unbounded);
336
337 return searchResults;
338 }
339
340
341
342
343
344
345
346
347
348 protected Map<String, String> adjustCriteriaForNestedEBOs(Map<String, String> searchCriteria,
349 boolean unbounded) throws InstantiationException, IllegalAccessException {
350 if (LOG.isDebugEnabled()) {
351 LOG.debug("has EBO reference: " + getDataObjectClass());
352 LOG.debug("properties: " + searchCriteria);
353 }
354
355
356 Map<String, String> nonEboFieldValues = LookupUtils.removeExternalizableBusinessObjectFieldValues(
357 getDataObjectClass(), searchCriteria);
358 if (LOG.isDebugEnabled()) {
359 LOG.debug("Non EBO properties removed: " + nonEboFieldValues);
360 }
361
362
363 List<String> eboPropertyNames = LookupUtils.getExternalizableBusinessObjectProperties(getDataObjectClass(),
364 searchCriteria);
365 if (LOG.isDebugEnabled()) {
366 LOG.debug("EBO properties: " + eboPropertyNames);
367 }
368
369
370 for (String eboPropertyName : eboPropertyNames) {
371
372 Map<String, String> eboFieldValues = LookupUtils.getExternalizableBusinessObjectFieldValues(eboPropertyName,
373 searchCriteria);
374 if (LOG.isDebugEnabled()) {
375 LOG.debug("EBO properties for master EBO property: " + eboPropertyName);
376 LOG.debug("properties: " + eboFieldValues);
377 }
378
379
380 ModuleService eboModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(
381 LookupUtils.getExternalizableBusinessObjectClass(getDataObjectClass(), eboPropertyName));
382
383
384
385 List<?> eboResults = Collections.emptyList();
386 if (eboModuleService != null) {
387 eboResults = eboModuleService.getExternalizableBusinessObjectsListForLookup(
388 LookupUtils.getExternalizableBusinessObjectClass(getDataObjectClass(), eboPropertyName),
389 (Map) eboFieldValues, unbounded);
390 } else {
391 LOG.debug("EBO ModuleService is null: " + eboPropertyName);
392 }
393
394
395
396
397
398 Class<?> eboParentClass;
399 String eboParentPropertyName;
400 if (ObjectUtils.isNestedAttribute(eboPropertyName)) {
401 eboParentPropertyName = StringUtils.substringBeforeLast(eboPropertyName, ".");
402 try {
403 eboParentClass = PropertyUtils.getPropertyType(getDataObjectClass().newInstance(),
404 eboParentPropertyName);
405 } catch (Exception ex) {
406 throw new RuntimeException(
407 "Unable to create an instance of the business object class: " + getDataObjectClass()
408 .getName(), ex);
409 }
410 } else {
411 eboParentClass = getDataObjectClass();
412 eboParentPropertyName = null;
413 }
414
415 if (LOG.isDebugEnabled()) {
416 LOG.debug("determined EBO parent class/property name: " + eboParentClass + "/" + eboParentPropertyName);
417 }
418
419
420
421
422
423 RelationshipDefinition rd = getDataObjectMetaDataService().getDictionaryRelationship(eboParentClass,
424 eboPropertyName);
425 if (LOG.isDebugEnabled()) {
426 LOG.debug("Obtained RelationshipDefinition for " + eboPropertyName);
427 LOG.debug(rd);
428 }
429
430
431
432
433
434
435
436 if (ObjectUtils.isNotNull(rd)) {
437 if (rd.getPrimitiveAttributes().size() > 1) {
438 throw new RuntimeException(
439 "EBO Links don't work for relationships with multiple-field primary keys.");
440 }
441 String boProperty = rd.getPrimitiveAttributes().get(0).getSourceName();
442 String eboProperty = rd.getPrimitiveAttributes().get(0).getTargetName();
443 StringBuffer boPropertyValue = new StringBuffer();
444
445
446
447
448 for (Object ebo : eboResults) {
449 if (boPropertyValue.length() != 0) {
450 boPropertyValue.append(SearchOperator.OR.op());
451 }
452 try {
453 boPropertyValue.append(PropertyUtils.getProperty(ebo, eboProperty).toString());
454 } catch (Exception ex) {
455 LOG.warn("Unable to get value for " + eboProperty + " on " + ebo);
456 }
457 }
458
459 if (eboParentPropertyName == null) {
460
461 nonEboFieldValues.put(boProperty, boPropertyValue.toString());
462 } else {
463
464
465 nonEboFieldValues.put(eboParentPropertyName + "." + boProperty, boPropertyValue.toString());
466 }
467 }
468 }
469
470 return nonEboFieldValues;
471 }
472
473
474
475
476 @Override
477 public Map<String, String> performClear(LookupForm form, Map<String, String> searchCriteria) {
478 Map<String, InputField> criteriaFieldMap = new HashMap<String, InputField>();
479 if (form.getPostedView() == null) {
480 criteriaFieldMap = getCriteriaFieldsForValidation((LookupView) form.getPostedView(), form);
481 }
482
483 Map<String, String> clearedSearchCriteria = new HashMap<String, String>();
484 for (Map.Entry<String, String> searchKeyValue : searchCriteria.entrySet()) {
485 String searchPropertyName = searchKeyValue.getKey();
486
487 InputField inputField = criteriaFieldMap.get(searchPropertyName);
488 if (inputField != null) {
489
490
491
492
493
494
495
496
497
498 clearedSearchCriteria.put(searchPropertyName, inputField.getDefaultValue());
499 } else {
500 clearedSearchCriteria.put(searchPropertyName, "");
501 }
502 }
503
504 return clearedSearchCriteria;
505 }
506
507
508
509
510 @Override
511 public boolean validateSearchParameters(LookupForm form, Map<String, String> searchCriteria) {
512 boolean valid = true;
513
514
515
516 if (form.getPostedView() == null) {
517 return valid;
518 }
519
520 Map<String, InputField> criteriaFields = getCriteriaFieldsForValidation((LookupView) form.getPostedView(),
521 form);
522
523
524 List<String> hiddenCriteria = new ArrayList<String>();
525 for (InputField field : criteriaFields.values()) {
526 if (field.getAdditionalHiddenPropertyNames() != null) {
527 hiddenCriteria.addAll(field.getAdditionalHiddenPropertyNames());
528 }
529 }
530
531
532
533 for (Map.Entry<String, String> searchKeyValue : searchCriteria.entrySet()) {
534 String searchPropertyName = searchKeyValue.getKey();
535 String searchPropertyValue = searchKeyValue.getValue();
536
537 InputField inputField = criteriaFields.get(searchPropertyName);
538
539 String adjustedSearchPropertyPath = UifPropertyPaths.LOOKUP_CRITERIA + "[" + searchPropertyName + "]";
540 if (inputField == null && hiddenCriteria.contains(adjustedSearchPropertyPath)) {
541 return valid;
542 }
543
544
545
546 if ((inputField == null) && !searchPropertyName.contains(
547 KRADConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX)) {
548 throw new RuntimeException("Invalid search field sent for property name: " + searchPropertyName);
549 }
550
551 if (inputField != null) {
552 if (StringUtils.isBlank(searchPropertyValue) && BooleanUtils.isTrue(inputField.getRequired())) {
553 GlobalVariables.getMessageMap().putError(inputField.getPropertyName(), RiceKeyConstants.ERROR_REQUIRED,
554 inputField.getLabel());
555 }
556
557 validateSearchParameterWildcardAndOperators(inputField, searchPropertyValue);
558 }
559 }
560
561 if (GlobalVariables.getMessageMap().hasErrors()) {
562 valid = false;
563 }
564
565 return valid;
566 }
567
568
569
570
571
572
573
574
575 protected Map<String, InputField> getCriteriaFieldsForValidation(LookupView lookupView, LookupForm form) {
576 Map<String, InputField> criteriaFieldMap = new HashMap<String, InputField>();
577
578 if (lookupView.getCriteriaFields() == null) {
579 return criteriaFieldMap;
580 }
581
582
583
584 List<InputField> fields = ComponentUtils.getComponentsOfTypeDeep(lookupView.getCriteriaFields(),
585 InputField.class);
586 for (InputField field : fields) {
587 criteriaFieldMap.put(field.getPropertyName(), field);
588 }
589
590 return criteriaFieldMap;
591 }
592
593
594
595
596
597
598
599
600 protected void validateSearchParameterWildcardAndOperators(InputField inputField, String searchPropertyValue) {
601 if (StringUtils.isBlank(searchPropertyValue)) {
602 return;
603 }
604
605
606 boolean found = false;
607 for (SearchOperator op : SearchOperator.QUERY_CHARACTERS) {
608 String queryCharacter = op.op();
609
610 if (searchPropertyValue.contains(queryCharacter)) {
611 found = true;
612 }
613 }
614
615 if (!found) {
616 return;
617 }
618
619 String attributeLabel = inputField.getLabel();
620 if ((LookupInputField.class.isAssignableFrom(inputField.getClass())) && (((LookupInputField) inputField)
621 .isDisableWildcardsAndOperators())) {
622 Object dataObjectExample = null;
623 try {
624 dataObjectExample = getDataObjectClass().newInstance();
625 } catch (Exception e) {
626 LOG.error("Exception caught instantiating " + getDataObjectClass().getName(), e);
627 throw new RuntimeException("Cannot instantiate " + getDataObjectClass().getName(), e);
628 }
629
630 Class<?> propertyType = ObjectPropertyUtils.getPropertyType(getDataObjectClass(),
631 inputField.getPropertyName());
632 if (TypeUtils.isIntegralClass(propertyType) || TypeUtils.isDecimalClass(propertyType) ||
633 TypeUtils.isTemporalClass(propertyType)) {
634 GlobalVariables.getMessageMap().putError(inputField.getPropertyName(),
635 RiceKeyConstants.ERROR_WILDCARDS_AND_OPERATORS_NOT_ALLOWED_ON_FIELD, attributeLabel);
636 }
637
638 if (TypeUtils.isStringClass(propertyType)) {
639 GlobalVariables.getMessageMap().putInfo(inputField.getPropertyName(),
640 RiceKeyConstants.INFO_WILDCARDS_AND_OPERATORS_TREATED_LITERALLY, attributeLabel);
641 }
642 } else {
643 if (getDataObjectAuthorizationService().attributeValueNeedsToBeEncryptedOnFormsAndLinks(
644 getDataObjectClass(), inputField.getPropertyName())) {
645 if (!searchPropertyValue.endsWith(EncryptionService.ENCRYPTION_POST_PREFIX)) {
646
647
648
649
650
651
652 GlobalVariables.getMessageMap().putError(inputField.getPropertyName(),
653 RiceKeyConstants.ERROR_SECURE_FIELD, attributeLabel);
654 }
655 }
656 }
657 }
658
659
660
661
662 public void getReturnUrlForResults(LinkField returnLinkField, Object model) {
663 LookupForm lookupForm = (LookupForm) model;
664 LookupView lookupView = (LookupView) returnLinkField.getContext().get(UifConstants.ContextVariableNames.VIEW);
665
666 Object dataObject = returnLinkField.getContext().get(UifConstants.ContextVariableNames.LINE);
667
668
669 if ((dataObject == null) || (!isResultReturnable(dataObject))) {
670 returnLinkField.setRender(false);
671 return;
672 }
673
674
675 String href = getReturnUrl(lookupView, lookupForm, dataObject);
676 if (StringUtils.isBlank(href)) {
677 returnLinkField.setRender(false);
678 return;
679 }
680
681
682 List<HistoryEntry> historyEntries = null;
683 History history = lookupForm.getFormHistory();
684
685 if(history != null){
686 historyEntries = history.getGeneratedBreadcrumbs();
687 }
688
689 String historyParams = "";
690 if(historyEntries != null && !historyEntries.isEmpty()){
691
692 String url = historyEntries.get(historyEntries.size() - 1).getUrl();
693 if(url != null && url.indexOf('?') > -1 && (url.indexOf('?') + 1) < url.length()){
694 historyParams = url.substring(url.indexOf('?') + 1);
695
696 historyParams = historyParams.replaceFirst("(^|&)" +
697 KRADConstants.DISPATCH_REQUEST_PARAMETER + "=.*?($|&)","");
698 historyParams = historyParams.replaceFirst("(^|&)" +
699 KRADConstants.FORM_KEY + "=.*?($|&)","");
700 }
701 }
702
703
704 if(StringUtils.isNotBlank(historyParams)){
705 href = href + "&" + historyParams;
706 }
707
708
709 returnLinkField.setHref(href);
710
711
712 String linkLabel = getConfigurationService().getPropertyValueAsString(
713 KRADConstants.Lookup.TITLE_RETURN_URL_PREPENDTEXT_PROPERTY);
714 returnLinkField.setLinkText(linkLabel);
715
716 List<String> returnKeys = getReturnKeys(lookupView, lookupForm, dataObject);
717 Map<String, String> returnKeyValues = KRADUtils.getPropertyKeyValuesFromDataObject(returnKeys, dataObject);
718
719 String title = LookupInquiryUtils.getLinkTitleText(linkLabel, getDataObjectClass(), returnKeyValues);
720 returnLinkField.setTitle(title);
721
722
723 String returnTarget = lookupView.getReturnTarget();
724 if (returnTarget != null) {
725 returnLinkField.setTarget(returnTarget);
726
727
728 if (!returnTarget.equals("_self")) {
729
730 if (lookupView.isReturnByScript()) {
731 Properties props = getReturnUrlParameters(lookupView, lookupForm, dataObject);
732
733 StringBuilder script = new StringBuilder("e.preventDefault();");
734 for (String returnField : lookupForm.getFieldConversions().values()) {
735 if (props.containsKey(returnField)) {
736 Object fieldName = returnField.replace("'", "\\'");
737 Object value = props.get(returnField);
738 script = script.append(
739 "returnLookupResultByScript(\"" + returnField + "\", '" + value + "');");
740 }
741 }
742 returnLinkField.getLink().setOnClickScript(script.append("closeLightbox();").toString());
743 } else {
744
745 returnLinkField.getLink().setOnClickScript(
746 "e.preventDefault();closeLightbox();showLoading();" +
747 "returnLookupResultReload(jQuery(this));");
748 }
749 }
750 } else {
751
752
753 returnLinkField.setTarget("_self");
754 }
755 }
756
757
758
759
760
761
762
763
764
765
766
767
768
769 protected String getReturnUrl(LookupView lookupView, LookupForm lookupForm, Object dataObject) {
770 Properties props = getReturnUrlParameters(lookupView, lookupForm, dataObject);
771
772 String href = "";
773 if (StringUtils.isNotBlank(lookupForm.getReturnLocation())) {
774 href = UrlFactory.parameterizeUrl(lookupForm.getReturnLocation(), props);
775 }
776
777 return href;
778 }
779
780
781
782
783
784
785
786
787
788
789 protected Properties getReturnUrlParameters(LookupView lookupView, LookupForm lookupForm, Object dataObject) {
790 Properties props = new Properties();
791 props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.RETURN_METHOD_TO_CALL);
792
793 if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
794 props.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
795 }
796
797 props.put(KRADConstants.REFRESH_CALLER, lookupView.getId());
798 props.put(KRADConstants.REFRESH_DATA_OBJECT_CLASS, getDataObjectClass().getName());
799
800 if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
801 props.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
802 }
803
804 if (StringUtils.isNotBlank(lookupForm.getReferencesToRefresh())) {
805 props.put(KRADConstants.REFERENCES_TO_REFRESH, lookupForm.getReferencesToRefresh());
806 }
807
808 List<String> returnKeys = getReturnKeys(lookupView, lookupForm, dataObject);
809 Map<String, String> returnKeyValues = KRADUtils.getPropertyKeyValuesFromDataObject(returnKeys, dataObject);
810
811 for (String returnKey : returnKeyValues.keySet()) {
812 String returnValue = returnKeyValues.get(returnKey);
813 if (lookupForm.getFieldConversions().containsKey(returnKey)) {
814 returnKey = lookupForm.getFieldConversions().get(returnKey);
815 }
816
817 props.put(returnKey, returnValue);
818 }
819
820 return props;
821 }
822
823
824
825
826
827
828
829
830
831
832
833 protected List<String> getReturnKeys(LookupView lookupView, LookupForm lookupForm, Object dataObject) {
834 List<String> returnKeys;
835 if (lookupForm.getFieldConversions() != null && !lookupForm.getFieldConversions().isEmpty()) {
836 returnKeys = new ArrayList<String>(lookupForm.getFieldConversions().keySet());
837 } else {
838 returnKeys = getDataObjectMetaDataService().listPrimaryKeyFieldNames(getDataObjectClass());
839 }
840
841 return returnKeys;
842 }
843
844
845
846
847 public void getMaintenanceActionLink(Link actionLink, Object model, String maintenanceMethodToCall) {
848 LookupForm lookupForm = (LookupForm) model;
849 LookupView lookupView = (LookupView) actionLink.getContext().get(UifConstants.ContextVariableNames.VIEW);
850 Object dataObject = actionLink.getContext().get(UifConstants.ContextVariableNames.LINE);
851
852 List<String> pkNames = getDataObjectMetaDataService().listPrimaryKeyFieldNames(getDataObjectClass());
853
854
855 String href = getActionUrlHref(lookupForm, dataObject, maintenanceMethodToCall, pkNames);
856 if (StringUtils.isBlank(href)) {
857 actionLink.setRender(false);
858 return;
859 }
860
861 actionLink.setHref(href);
862
863
864 String prependTitleText = actionLink.getLinkText() + " " +
865 getDataDictionaryService().getDataDictionary().getDataObjectEntry(getDataObjectClass().getName())
866 .getObjectLabel() + " " +
867 getConfigurationService().getPropertyValueAsString(
868 KRADConstants.Lookup.TITLE_ACTION_URL_PREPENDTEXT_PROPERTY);
869
870 Map<String, String> primaryKeyValues = KRADUtils.getPropertyKeyValuesFromDataObject(pkNames, dataObject);
871 String title = LookupInquiryUtils.getLinkTitleText(prependTitleText, getDataObjectClass(), primaryKeyValues);
872 actionLink.setTitle(title);
873
874 actionLink.setTarget("_self");
875 lookupForm.setAtLeastOneRowHasActions(true);
876 }
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892 protected String getActionUrlHref(LookupForm lookupForm, Object dataObject, String methodToCall,
893 List<String> pkNames) {
894 LookupView lookupView = (LookupView) lookupForm.getView();
895
896 Properties props = new Properties();
897 props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
898
899 Map<String, String> primaryKeyValues = KRADUtils.getPropertyKeyValuesFromDataObject(pkNames, dataObject);
900 for (String primaryKey : primaryKeyValues.keySet()) {
901 String primaryKeyValue = primaryKeyValues.get(primaryKey);
902
903 props.put(primaryKey, primaryKeyValue);
904 }
905
906 if (StringUtils.isNotBlank(lookupForm.getReturnLocation())) {
907 props.put(KRADConstants.RETURN_LOCATION_PARAMETER, lookupForm.getReturnLocation());
908 }
909
910 props.put(UifParameters.DATA_OBJECT_CLASS_NAME, lookupForm.getDataObjectClassName());
911 props.put(UifParameters.VIEW_TYPE_NAME, UifConstants.ViewType.MAINTENANCE.name());
912
913 String maintenanceMapping = KRADConstants.Maintenance.REQUEST_MAPPING_MAINTENANCE;
914 if (StringUtils.isNotBlank(lookupView.getMaintenanceUrlMapping())) {
915 maintenanceMapping = lookupView.getMaintenanceUrlMapping();
916 }
917
918 return UrlFactory.parameterizeUrl(maintenanceMapping, props);
919 }
920
921
922
923
924
925
926 @Override
927 public void setMultiValueLookupSelect(InputField selectField, Object model) {
928 LookupForm lookupForm = (LookupForm) model;
929 Object lineDataObject = selectField.getContext().get(UifConstants.ContextVariableNames.LINE);
930 if (lineDataObject == null) {
931 throw new RuntimeException("Unable to get data object for line from component: " + selectField.getId());
932 }
933
934 Control selectControl = ((InputField) selectField).getControl();
935 if ((selectControl != null) && (selectControl instanceof ValueConfiguredControl)) {
936 String lineIdentifier = "";
937
938
939 Map<String, String> fieldConversions = lookupForm.getFieldConversions();
940 List<String> fromFieldNames = new ArrayList<String>(fieldConversions.keySet());
941 Collections.sort(fromFieldNames);
942 for (String fromFieldName : fromFieldNames) {
943 Object fromFieldValue = ObjectPropertyUtils.getPropertyValue(lineDataObject, fromFieldName);
944 if (fromFieldValue != null) {
945 lineIdentifier += fromFieldValue;
946 }
947 lineIdentifier += ":";
948 }
949 lineIdentifier = StringUtils.removeEnd(lineIdentifier, ":");
950
951 ((ValueConfiguredControl) selectControl).setValue(lineIdentifier);
952 }
953 }
954
955
956
957
958
959
960
961
962
963 public boolean allowsMaintenanceNewOrCopyAction() {
964 boolean allowsNewOrCopy = false;
965
966 String maintDocTypeName = getMaintenanceDocumentTypeName();
967 if (StringUtils.isNotBlank(maintDocTypeName)) {
968 allowsNewOrCopy = getDataObjectAuthorizationService().canCreate(getDataObjectClass(),
969 GlobalVariables.getUserSession().getPerson(), maintDocTypeName);
970 }
971
972 return allowsNewOrCopy;
973 }
974
975
976
977
978
979
980
981 public boolean allowsMaintenanceEditAction(Object dataObject) {
982 boolean allowsEdit = false;
983
984 String maintDocTypeName = getMaintenanceDocumentTypeName();
985 if (StringUtils.isNotBlank(maintDocTypeName)) {
986 allowsEdit = getDataObjectAuthorizationService().canMaintain(dataObject,
987 GlobalVariables.getUserSession().getPerson(), maintDocTypeName);
988 }
989
990 return allowsEdit;
991 }
992
993
994
995
996
997
998
999 public boolean allowsMaintenanceDeleteAction(Object dataObject) {
1000 boolean allowsMaintain = false;
1001 boolean allowsDelete = false;
1002
1003 String maintDocTypeName = getMaintenanceDocumentTypeName();
1004 if (StringUtils.isNotBlank(maintDocTypeName)) {
1005 allowsMaintain = getDataObjectAuthorizationService().canMaintain(dataObject,
1006 GlobalVariables.getUserSession().getPerson(), maintDocTypeName);
1007 }
1008
1009 allowsDelete = getDocumentDictionaryService().getAllowsRecordDeletion(getDataObjectClass());
1010
1011 return allowsDelete && allowsMaintain;
1012 }
1013
1014
1015
1016
1017
1018
1019 protected String getMaintenanceDocumentTypeName() {
1020 DocumentDictionaryService dd = getDocumentDictionaryService();
1021 String maintDocTypeName = dd.getMaintenanceDocumentTypeName(getDataObjectClass());
1022
1023 return maintDocTypeName;
1024 }
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039 protected boolean isResultReturnable(Object dataObject) {
1040 return true;
1041 }
1042
1043
1044
1045
1046 @Override
1047 public void setDataObjectClass(Class<?> dataObjectClass) {
1048 this.dataObjectClass = dataObjectClass;
1049 }
1050
1051
1052
1053
1054 @Override
1055 public Class<?> getDataObjectClass() {
1056 return this.dataObjectClass;
1057 }
1058
1059 public void setConfigurationService(ConfigurationService configurationService) {
1060 this.configurationService = configurationService;
1061 }
1062
1063 protected DataObjectAuthorizationService getDataObjectAuthorizationService() {
1064 if (dataObjectAuthorizationService == null) {
1065 this.dataObjectAuthorizationService = KRADServiceLocatorWeb.getDataObjectAuthorizationService();
1066 }
1067 return dataObjectAuthorizationService;
1068 }
1069
1070 public void setDataObjectAuthorizationService(DataObjectAuthorizationService dataObjectAuthorizationService) {
1071 this.dataObjectAuthorizationService = dataObjectAuthorizationService;
1072 }
1073
1074 protected DataObjectMetaDataService getDataObjectMetaDataService() {
1075 if (dataObjectMetaDataService == null) {
1076 this.dataObjectMetaDataService = KRADServiceLocatorWeb.getDataObjectMetaDataService();
1077 }
1078 return dataObjectMetaDataService;
1079 }
1080
1081 public void setDataObjectMetaDataService(DataObjectMetaDataService dataObjectMetaDataService) {
1082 this.dataObjectMetaDataService = dataObjectMetaDataService;
1083 }
1084
1085 public DocumentDictionaryService getDocumentDictionaryService() {
1086 if (documentDictionaryService == null) {
1087 documentDictionaryService = KRADServiceLocatorWeb.getDocumentDictionaryService();
1088 }
1089 return documentDictionaryService;
1090 }
1091
1092 public void setDocumentDictionaryService(DocumentDictionaryService documentDictionaryService) {
1093 this.documentDictionaryService = documentDictionaryService;
1094 }
1095
1096 protected LookupService getLookupService() {
1097 if (lookupService == null) {
1098 this.lookupService = KRADServiceLocatorWeb.getLookupService();
1099 }
1100 return lookupService;
1101 }
1102
1103 public void setLookupService(LookupService lookupService) {
1104 this.lookupService = lookupService;
1105 }
1106
1107 protected EncryptionService getEncryptionService() {
1108 if (encryptionService == null) {
1109 this.encryptionService = CoreApiServiceLocator.getEncryptionService();
1110 }
1111 return encryptionService;
1112 }
1113
1114 public void setEncryptionService(EncryptionService encryptionService) {
1115 this.encryptionService = encryptionService;
1116 }
1117 }