1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.docsearch; |
17 | |
|
18 | |
import org.apache.commons.collections.CollectionUtils; |
19 | |
import org.kuali.rice.core.api.uif.RemotableAttributeError; |
20 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
21 | |
import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria; |
22 | |
import org.kuali.rice.kew.api.document.search.DocumentSearchResults; |
23 | |
import org.kuali.rice.kew.framework.document.search.AttributeFields; |
24 | |
import org.kuali.rice.kew.framework.document.search.DocumentSearchCriteriaConfiguration; |
25 | |
import org.kuali.rice.kew.framework.document.search.DocumentSearchResultSetConfiguration; |
26 | |
import org.kuali.rice.kew.framework.document.search.DocumentSearchResultValues; |
27 | |
import org.kuali.rice.kew.doctype.DocumentTypeAttribute; |
28 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
29 | |
import org.kuali.rice.kew.framework.KewFrameworkServiceLocator; |
30 | |
import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomization; |
31 | |
import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomizationHandlerService; |
32 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
33 | |
|
34 | |
import java.util.ArrayList; |
35 | |
import java.util.LinkedHashMap; |
36 | |
import java.util.List; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class DocumentSearchCustomizationMediatorImpl implements DocumentSearchCustomizationMediator { |
44 | |
|
45 | |
@Override |
46 | |
public DocumentSearchCriteriaConfiguration getDocumentSearchCriteriaConfiguration(DocumentType documentType) { |
47 | |
|
48 | 0 | List<DocumentTypeAttribute> searchableAttributes = documentType.getSearchableAttributes(); |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | LinkedHashMap<String, List<String>> applicationIdToAttributeNameMap = new LinkedHashMap<String, List<String>>(); |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 0 | LinkedHashMap<String, AttributeFields> orderedSearchFieldMap = new LinkedHashMap<String, AttributeFields>(); |
61 | 0 | LinkedHashMap<String, AttributeFields> orderedResultSetFieldMap = new LinkedHashMap<String, AttributeFields>(); |
62 | |
|
63 | 0 | for (DocumentTypeAttribute searchableAttribute : searchableAttributes) { |
64 | 0 | RuleAttribute ruleAttribute = searchableAttribute.getRuleAttribute(); |
65 | 0 | String attributeName = ruleAttribute.getName(); |
66 | 0 | String applicationId = ruleAttribute.getApplicationId(); |
67 | 0 | if (!applicationIdToAttributeNameMap.containsKey(applicationId)) { |
68 | 0 | applicationIdToAttributeNameMap.put(applicationId, new ArrayList<String>()); |
69 | |
} |
70 | 0 | applicationIdToAttributeNameMap.get(applicationId).add(attributeName); |
71 | |
|
72 | 0 | orderedSearchFieldMap.put(attributeName, null); |
73 | 0 | } |
74 | |
|
75 | 0 | for (String applicationId : applicationIdToAttributeNameMap.keySet()) { |
76 | 0 | DocumentSearchCustomizationHandlerService documentSearchCustomizationService = loadCustomizationService( |
77 | |
applicationId); |
78 | 0 | List<String> searchableAttributeNames = applicationIdToAttributeNameMap.get(applicationId); |
79 | 0 | DocumentSearchCriteriaConfiguration documentSearchConfiguration = documentSearchCustomizationService.getDocumentSearchConfiguration( |
80 | |
documentType.getName(), searchableAttributeNames); |
81 | 0 | mergeAttributeFields(documentSearchConfiguration.getSearchAttributeFields(), orderedSearchFieldMap); |
82 | 0 | } |
83 | |
|
84 | 0 | DocumentSearchCriteriaConfiguration.Builder configBuilder = DocumentSearchCriteriaConfiguration.Builder.create(); |
85 | 0 | configBuilder.setSearchAttributeFields(flattenOrderedFieldMap(orderedSearchFieldMap)); |
86 | 0 | return configBuilder.build(); |
87 | |
} |
88 | |
|
89 | |
@Override |
90 | |
public List<RemotableAttributeError> validateLookupFieldParameters(DocumentType documentType, |
91 | |
DocumentSearchCriteria documentSearchCriteria) { |
92 | |
|
93 | 0 | List<DocumentTypeAttribute> searchableAttributes = documentType.getSearchableAttributes(); |
94 | 0 | LinkedHashMap<String, List<String>> applicationIdToAttributeNameMap = new LinkedHashMap<String, List<String>>(); |
95 | |
|
96 | 0 | for (DocumentTypeAttribute searchableAttribute : searchableAttributes) { |
97 | 0 | RuleAttribute ruleAttribute = searchableAttribute.getRuleAttribute(); |
98 | 0 | String attributeName = ruleAttribute.getName(); |
99 | 0 | String applicationId = ruleAttribute.getApplicationId(); |
100 | 0 | if (!applicationIdToAttributeNameMap.containsKey(applicationId)) { |
101 | 0 | applicationIdToAttributeNameMap.put(applicationId, new ArrayList<String>()); |
102 | |
} |
103 | 0 | applicationIdToAttributeNameMap.get(applicationId).add(attributeName); |
104 | 0 | } |
105 | |
|
106 | 0 | List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>(); |
107 | 0 | for (String applicationId : applicationIdToAttributeNameMap.keySet()) { |
108 | 0 | DocumentSearchCustomizationHandlerService documentSearchCustomizationService = loadCustomizationService(applicationId); |
109 | 0 | List<String> searchableAttributeNames = applicationIdToAttributeNameMap.get(applicationId); |
110 | 0 | List<RemotableAttributeError> searchErrors = documentSearchCustomizationService.validateCriteria( |
111 | |
documentSearchCriteria, searchableAttributeNames); |
112 | 0 | if (!CollectionUtils.isEmpty(searchErrors)) { |
113 | 0 | errors.addAll(searchErrors); |
114 | |
} |
115 | 0 | } |
116 | |
|
117 | 0 | return errors; |
118 | |
} |
119 | |
|
120 | |
@Override |
121 | |
public DocumentSearchCriteria customizeCriteria(DocumentType documentType, DocumentSearchCriteria documentSearchCriteria) { |
122 | 0 | DocumentTypeAttribute customizerAttribute = documentType.getCustomizerAttribute(); |
123 | 0 | if (customizerAttribute != null) { |
124 | 0 | DocumentSearchCustomizationHandlerService service = loadCustomizationService(customizerAttribute.getRuleAttribute().getApplicationId()); |
125 | 0 | if (service.getEnabledCustomizations(documentType.getName(), customizerAttribute.getRuleAttribute().getName()).contains( |
126 | |
DocumentSearchCustomization.CRITERIA)) { |
127 | 0 | DocumentSearchCriteria customizedCriteria = service.customizeCriteria(documentSearchCriteria, customizerAttribute.getRuleAttribute().getName()); |
128 | 0 | if (customizedCriteria != null) { |
129 | 0 | return customizedCriteria; |
130 | |
} |
131 | |
} |
132 | |
} |
133 | 0 | return null; |
134 | |
} |
135 | |
|
136 | |
@Override |
137 | |
public DocumentSearchCriteria customizeClearCriteria(DocumentType documentType, DocumentSearchCriteria documentSearchCriteria) { |
138 | 0 | DocumentTypeAttribute customizerAttribute = documentType.getCustomizerAttribute(); |
139 | 0 | if (customizerAttribute != null) { |
140 | 0 | DocumentSearchCustomizationHandlerService service = loadCustomizationService(customizerAttribute.getRuleAttribute().getApplicationId()); |
141 | 0 | if (service.getEnabledCustomizations(documentType.getName(), customizerAttribute.getRuleAttribute().getName()).contains( |
142 | |
DocumentSearchCustomization.CLEAR_CRITERIA)) { |
143 | 0 | DocumentSearchCriteria customizedCriteria = service.customizeClearCriteria(documentSearchCriteria, customizerAttribute.getRuleAttribute().getName()); |
144 | 0 | if (customizedCriteria != null) { |
145 | 0 | return customizedCriteria; |
146 | |
} |
147 | |
} |
148 | |
} |
149 | 0 | return documentSearchCriteria; |
150 | |
} |
151 | |
|
152 | |
@Override |
153 | |
public DocumentSearchResultValues customizeResults(DocumentType documentType, |
154 | |
DocumentSearchCriteria documentSearchCriteria, DocumentSearchResults results) { |
155 | 0 | DocumentTypeAttribute customizerAttribute = documentType.getCustomizerAttribute(); |
156 | 0 | if (customizerAttribute != null) { |
157 | 0 | DocumentSearchCustomizationHandlerService service = loadCustomizationService(customizerAttribute.getRuleAttribute().getApplicationId()); |
158 | 0 | if (service.getEnabledCustomizations(documentType.getName(), customizerAttribute.getRuleAttribute().getName()).contains( |
159 | |
DocumentSearchCustomization.RESULTS)) { |
160 | 0 | DocumentSearchResultValues customizedResults = service.customizeResults(documentSearchCriteria, results.getSearchResults(), customizerAttribute.getRuleAttribute().getName()); |
161 | 0 | if (customizedResults != null) { |
162 | 0 | return customizedResults; |
163 | |
} |
164 | |
} |
165 | |
} |
166 | 0 | return null; |
167 | |
} |
168 | |
|
169 | |
@Override |
170 | |
public DocumentSearchResultSetConfiguration customizeResultSetConfiguration(DocumentType documentType, |
171 | |
DocumentSearchCriteria documentSearchCriteria) { |
172 | 0 | DocumentTypeAttribute customizerAttribute = documentType.getCustomizerAttribute(); |
173 | 0 | if (customizerAttribute != null) { |
174 | 0 | DocumentSearchCustomizationHandlerService service = loadCustomizationService(customizerAttribute.getRuleAttribute().getApplicationId()); |
175 | 0 | if (service.getEnabledCustomizations(documentType.getName(), customizerAttribute.getRuleAttribute().getName()).contains( |
176 | |
DocumentSearchCustomization.RESULT_SET_FIELDS)) { |
177 | 0 | DocumentSearchResultSetConfiguration resultSetConfiguration = service.customizeResultSetConfiguration( |
178 | |
documentSearchCriteria, customizerAttribute.getRuleAttribute().getName()); |
179 | 0 | if (resultSetConfiguration != null) { |
180 | 0 | return resultSetConfiguration; |
181 | |
} |
182 | |
} |
183 | |
} |
184 | 0 | return null; |
185 | |
} |
186 | |
|
187 | |
protected DocumentSearchCustomizationHandlerService loadCustomizationService(String applicationId) { |
188 | 0 | DocumentSearchCustomizationHandlerService service = KewFrameworkServiceLocator.getDocumentSearchCustomizationHandlerService( |
189 | |
applicationId); |
190 | 0 | if (service == null) { |
191 | 0 | throw new WorkflowRuntimeException("Failed to locate DocumentSearchCustomizationService for applicationId: " + applicationId); |
192 | |
} |
193 | 0 | return service; |
194 | |
} |
195 | |
|
196 | |
protected void mergeAttributeFields(List<AttributeFields> attributeFieldsList, LinkedHashMap<String, AttributeFields> orderedFieldMap) { |
197 | 0 | if (attributeFieldsList == null) { |
198 | 0 | return; |
199 | |
} |
200 | 0 | for (AttributeFields attributeFields : attributeFieldsList) { |
201 | 0 | orderedFieldMap.put(attributeFields.getAttributeName(), attributeFields); |
202 | |
} |
203 | 0 | } |
204 | |
|
205 | |
protected List<AttributeFields> flattenOrderedFieldMap(LinkedHashMap<String, AttributeFields> orderedFieldMap) { |
206 | 0 | return new ArrayList<AttributeFields>(orderedFieldMap.values()); |
207 | |
} |
208 | |
|
209 | |
} |