1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.web.component; |
17 | |
|
18 | |
import org.apache.commons.collections.CollectionUtils; |
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.kuali.rice.core.framework.parameter.ParameterService; |
21 | |
import org.kuali.rice.core.impl.component.ComponentBo; |
22 | |
import org.kuali.rice.core.impl.component.DerivedComponentBo; |
23 | |
import org.kuali.rice.kns.lookup.HtmlData; |
24 | |
import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl; |
25 | |
import org.kuali.rice.krad.bo.BusinessObject; |
26 | |
import org.kuali.rice.krad.lookup.CollectionIncomplete; |
27 | |
import org.kuali.rice.krad.lookup.LookupUtils; |
28 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
29 | |
|
30 | |
import java.util.Collection; |
31 | |
import java.util.HashMap; |
32 | |
import java.util.List; |
33 | |
import java.util.Map; |
34 | |
import java.util.regex.Pattern; |
35 | |
import java.util.regex.PatternSyntaxException; |
36 | |
|
37 | 0 | public class ComponentLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl { |
38 | |
|
39 | |
private static final long serialVersionUID = -3978422770535345525L; |
40 | |
|
41 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ComponentLookupableHelperServiceImpl.class); |
42 | |
|
43 | |
private static final String ACTIVE = "active"; |
44 | |
private static final String CODE = "code"; |
45 | |
private static final String NAMESPACE_CODE = "namespaceCode"; |
46 | |
private static final String NAME = "name"; |
47 | |
|
48 | |
@Override |
49 | |
public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) { |
50 | |
|
51 | 0 | List<BusinessObject> baseLookup = (List<BusinessObject>) super.getSearchResults(fieldValues); |
52 | |
|
53 | 0 | String activeCheck = fieldValues.get(ACTIVE); |
54 | 0 | if (activeCheck == null) { |
55 | 0 | activeCheck = ""; |
56 | |
} |
57 | 0 | int maxResultsCount = LookupUtils.getSearchResultsLimit(ComponentBo.class); |
58 | |
|
59 | 0 | if (baseLookup instanceof CollectionIncomplete && !activeCheck.equals("N")) { |
60 | 0 | long originalCount = Math.max(baseLookup.size(), ((CollectionIncomplete) baseLookup).getActualSizeIfTruncated()); |
61 | 0 | long totalCount = originalCount; |
62 | 0 | Pattern detailTypeRegex = null; |
63 | 0 | Pattern namespaceRegex = null; |
64 | 0 | Pattern nameRegex = null; |
65 | |
|
66 | 0 | if (StringUtils.isNotBlank(fieldValues.get(CODE))) { |
67 | 0 | String patternStr = fieldValues.get(CODE).replace("*", ".*").toUpperCase(); |
68 | |
try { |
69 | 0 | detailTypeRegex = Pattern.compile(patternStr); |
70 | |
} |
71 | 0 | catch (PatternSyntaxException ex) { |
72 | 0 | LOG.error("Unable to parse code pattern, ignoring.", ex); |
73 | 0 | } |
74 | |
} |
75 | 0 | if (StringUtils.isNotBlank(fieldValues.get(NAMESPACE_CODE))) { |
76 | 0 | String patternStr = fieldValues.get(NAMESPACE_CODE).replace("*", ".*").toUpperCase(); |
77 | |
try { |
78 | 0 | namespaceRegex = Pattern.compile(patternStr); |
79 | |
} |
80 | 0 | catch (PatternSyntaxException ex) { |
81 | 0 | LOG.error("Unable to parse namespaceCode pattern, ignoring.", ex); |
82 | 0 | } |
83 | |
} |
84 | 0 | if (StringUtils.isNotBlank(fieldValues.get(NAME))) { |
85 | 0 | String patternStr = fieldValues.get(NAME).replace("*", ".*").toUpperCase(); |
86 | |
try { |
87 | 0 | nameRegex = Pattern.compile(patternStr); |
88 | |
} |
89 | 0 | catch (PatternSyntaxException ex) { |
90 | 0 | LOG.error("Unable to parse name pattern, ignoring.", ex); |
91 | 0 | } |
92 | |
} |
93 | |
|
94 | |
|
95 | 0 | Map<String, Object> derivedComponentCriteria = new HashMap<String, Object>(); |
96 | 0 | if (fieldValues.containsKey(NAMESPACE_CODE) && StringUtils.isNotBlank(fieldValues.get(NAMESPACE_CODE))) { |
97 | 0 | derivedComponentCriteria.put(NAMESPACE_CODE, fieldValues.get(NAMESPACE_CODE)); |
98 | |
} |
99 | 0 | if (fieldValues.containsKey(CODE) && StringUtils.isNotBlank(fieldValues.get(CODE))) { |
100 | 0 | derivedComponentCriteria.put(CODE, fieldValues.get(CODE)); |
101 | |
} |
102 | 0 | if (fieldValues.containsKey(NAME) && StringUtils.isNotBlank(fieldValues.get(NAME))) { |
103 | 0 | derivedComponentCriteria.put(NAME, fieldValues.get(NAME)); |
104 | |
} |
105 | 0 | Collection<DerivedComponentBo> derivedComponentBos = null; |
106 | 0 | if (derivedComponentCriteria.isEmpty()) { |
107 | 0 | derivedComponentBos = KRADServiceLocator.getBusinessObjectService().findAll(DerivedComponentBo.class); |
108 | |
} else { |
109 | 0 | derivedComponentBos = KRADServiceLocator.getBusinessObjectService().findMatching(DerivedComponentBo.class, derivedComponentCriteria); |
110 | |
} |
111 | 0 | if (CollectionUtils.isNotEmpty(derivedComponentBos)) { |
112 | 0 | for (DerivedComponentBo derivedComponentBo : derivedComponentBos) { |
113 | 0 | if (totalCount++ < maxResultsCount) { |
114 | 0 | baseLookup.add(DerivedComponentBo.toComponentBo(derivedComponentBo)); |
115 | |
} else { |
116 | |
break; |
117 | |
} |
118 | |
} |
119 | |
} |
120 | |
|
121 | 0 | if (totalCount > maxResultsCount) { |
122 | 0 | ((CollectionIncomplete) baseLookup).setActualSizeIfTruncated(totalCount); |
123 | |
} |
124 | |
else { |
125 | 0 | ((CollectionIncomplete) baseLookup).setActualSizeIfTruncated(0L); |
126 | |
} |
127 | |
} |
128 | |
|
129 | 0 | return baseLookup; |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
@Override |
136 | |
public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) { |
137 | 0 | if ( ((ComponentBo)businessObject).getObjectId() == null ) { |
138 | 0 | return super.getEmptyActionUrls(); |
139 | |
} |
140 | 0 | return super.getCustomActionUrls(businessObject, pkNames); |
141 | |
} |
142 | |
|
143 | |
} |