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.apache.commons.lang.StringUtils; |
20 | |
import org.apache.log4j.Logger; |
21 | |
import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; |
22 | |
import org.kuali.rice.core.api.exception.RiceRemoteServiceConnectionException; |
23 | |
import org.kuali.rice.core.api.uif.RemotableAttributeError; |
24 | |
import org.kuali.rice.core.api.uif.RemotableAttributeField; |
25 | |
import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria; |
26 | |
import org.kuali.rice.kew.api.document.search.DocumentSearchResult; |
27 | |
import org.kuali.rice.kew.api.extension.ExtensionDefinition; |
28 | |
import org.kuali.rice.kew.api.extension.ExtensionRepositoryService; |
29 | |
import org.kuali.rice.kew.api.extension.ExtensionUtils; |
30 | |
import org.kuali.rice.kew.framework.document.attribute.SearchableAttribute; |
31 | |
import org.kuali.rice.kew.framework.document.search.AttributeFields; |
32 | |
import org.kuali.rice.kew.framework.document.search.DocumentSearchCriteriaConfiguration; |
33 | |
import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomization; |
34 | |
import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomizationHandlerService; |
35 | |
import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomizer; |
36 | |
import org.kuali.rice.kew.framework.document.search.DocumentSearchResultSetConfiguration; |
37 | |
import org.kuali.rice.kew.framework.document.search.DocumentSearchResultValues; |
38 | |
|
39 | |
import java.util.ArrayList; |
40 | |
import java.util.Collections; |
41 | |
import java.util.HashSet; |
42 | |
import java.util.List; |
43 | |
import java.util.Set; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | public class DocumentSearchCustomizationHandlerServiceImpl implements DocumentSearchCustomizationHandlerService { |
51 | |
|
52 | 0 | private static final Logger LOG = Logger.getLogger(DocumentSearchCustomizationHandlerServiceImpl.class); |
53 | |
|
54 | |
private ExtensionRepositoryService extensionRepositoryService; |
55 | |
|
56 | |
@Override |
57 | |
public DocumentSearchCriteriaConfiguration getDocumentSearchConfiguration(String documentTypeName, |
58 | |
List<String> searchableAttributeNames) { |
59 | 0 | if (StringUtils.isBlank(documentTypeName)) { |
60 | 0 | throw new RiceIllegalArgumentException("documentTypeName was null or blank"); |
61 | |
} |
62 | |
|
63 | 0 | if (searchableAttributeNames == null) { |
64 | 0 | throw new RiceIllegalArgumentException("searchableAttributeNames was null"); |
65 | |
} |
66 | 0 | DocumentSearchCriteriaConfiguration.Builder configBuilder = DocumentSearchCriteriaConfiguration.Builder.create(); |
67 | 0 | if (CollectionUtils.isNotEmpty(searchableAttributeNames)) { |
68 | |
try { |
69 | 0 | List<AttributeFields> searchAttributeFields = new ArrayList<AttributeFields>(); |
70 | 0 | for (String searchableAttributeName : searchableAttributeNames) { |
71 | 0 | ExtensionDefinition extensionDefinition = getExtensionRepositoryService().getExtensionByName(searchableAttributeName); |
72 | 0 | if (extensionDefinition == null) { |
73 | 0 | throw new RiceIllegalArgumentException("Failed to locate a SearchableAttribute with the given name: " + searchableAttributeName); |
74 | |
} |
75 | 0 | SearchableAttribute searchableAttribute = loadSearchableAttribute(extensionDefinition); |
76 | 0 | List<RemotableAttributeField> attributeSearchFields = searchableAttribute.getSearchFields(extensionDefinition, documentTypeName); |
77 | 0 | if (CollectionUtils.isNotEmpty(attributeSearchFields)) { |
78 | 0 | searchAttributeFields.add(AttributeFields.create(searchableAttributeName, attributeSearchFields)); |
79 | |
} |
80 | 0 | } |
81 | 0 | configBuilder.setSearchAttributeFields(searchAttributeFields); |
82 | 0 | } catch (RiceRemoteServiceConnectionException e) { |
83 | 0 | LOG.warn("Unable to connect to load searchable attributes for document type: " + documentTypeName, e); |
84 | 0 | } |
85 | |
} |
86 | 0 | return configBuilder.build(); |
87 | |
} |
88 | |
|
89 | |
@Override |
90 | |
public List<RemotableAttributeError> validateCriteria(DocumentSearchCriteria documentSearchCriteria, |
91 | |
List<String> searchableAttributeNames) { |
92 | 0 | if (documentSearchCriteria == null) { |
93 | 0 | throw new RiceIllegalArgumentException("documentSearchCriteria was null or blank"); |
94 | |
} |
95 | 0 | if (searchableAttributeNames == null) { |
96 | 0 | throw new RiceIllegalArgumentException("searchableAttributeNames was null"); |
97 | |
} |
98 | |
try { |
99 | 0 | List<RemotableAttributeError> searchFieldErrors = new ArrayList<RemotableAttributeError>(); |
100 | 0 | for (String searchableAttributeName : searchableAttributeNames) { |
101 | 0 | ExtensionDefinition extensionDefinition = getExtensionRepositoryService().getExtensionByName(searchableAttributeName); |
102 | 0 | if (extensionDefinition == null) { |
103 | 0 | throw new RiceIllegalArgumentException("Failed to locate a SearchableAttribute with the given name: " + searchableAttributeName); |
104 | |
} |
105 | 0 | SearchableAttribute searchableAttribute = loadSearchableAttribute(extensionDefinition); |
106 | 0 | List<RemotableAttributeError> errors = searchableAttribute.validateDocumentAttributeCriteria(extensionDefinition, |
107 | |
documentSearchCriteria); |
108 | 0 | if (!CollectionUtils.isEmpty(errors)) { |
109 | 0 | searchFieldErrors.addAll(errors); |
110 | |
} |
111 | 0 | } |
112 | 0 | return Collections.unmodifiableList(searchFieldErrors); |
113 | 0 | } catch (RiceRemoteServiceConnectionException e) { |
114 | 0 | LOG.warn("Unable to connect to load searchable attributes for criteria: " + documentSearchCriteria, e); |
115 | 0 | return Collections.emptyList(); |
116 | |
} |
117 | |
} |
118 | |
|
119 | |
@Override |
120 | |
public DocumentSearchCriteria customizeCriteria(DocumentSearchCriteria documentSearchCriteria, String customizerName) throws RiceIllegalArgumentException { |
121 | 0 | if (documentSearchCriteria == null) { |
122 | 0 | throw new RiceIllegalArgumentException("documentSearchCriteria was null"); |
123 | |
} |
124 | 0 | if (StringUtils.isBlank(customizerName)) { |
125 | 0 | throw new RiceIllegalArgumentException("customizerName was null or blank"); |
126 | |
} |
127 | 0 | DocumentSearchCustomizer customizer = loadCustomizer(customizerName); |
128 | 0 | return customizer.customizeCriteria(documentSearchCriteria); |
129 | |
} |
130 | |
|
131 | |
@Override |
132 | |
public DocumentSearchCriteria customizeClearCriteria(DocumentSearchCriteria documentSearchCriteria, String customizerName) |
133 | |
throws RiceIllegalArgumentException { |
134 | 0 | if (documentSearchCriteria == null) { |
135 | 0 | throw new RiceIllegalArgumentException("documentSearchCriteria was null"); |
136 | |
} |
137 | 0 | if (StringUtils.isBlank(customizerName)) { |
138 | 0 | throw new RiceIllegalArgumentException("customizerName was null or blank"); |
139 | |
} |
140 | 0 | DocumentSearchCustomizer customizer = loadCustomizer(customizerName); |
141 | 0 | return customizer.customizeClearCriteria(documentSearchCriteria); |
142 | |
} |
143 | |
|
144 | |
@Override |
145 | |
public DocumentSearchResultValues customizeResults(DocumentSearchCriteria documentSearchCriteria, |
146 | |
List<DocumentSearchResult> defaultResults, |
147 | |
String customizerName) throws RiceIllegalArgumentException { |
148 | 0 | if (documentSearchCriteria == null) { |
149 | 0 | throw new RiceIllegalArgumentException("documentSearchCriteria was null"); |
150 | |
} |
151 | 0 | if (defaultResults == null) { |
152 | 0 | throw new RiceIllegalArgumentException("defaultResults was null"); |
153 | |
} |
154 | 0 | if (StringUtils.isBlank(customizerName)) { |
155 | 0 | throw new RiceIllegalArgumentException("customizerName was null or blank"); |
156 | |
} |
157 | 0 | DocumentSearchCustomizer customizer = loadCustomizer(customizerName); |
158 | 0 | return customizer.customizeResults(documentSearchCriteria, defaultResults); |
159 | |
} |
160 | |
|
161 | |
@Override |
162 | |
public DocumentSearchResultSetConfiguration customizeResultSetConfiguration( |
163 | |
DocumentSearchCriteria documentSearchCriteria, String customizerName) throws RiceIllegalArgumentException { |
164 | 0 | if (documentSearchCriteria == null) { |
165 | 0 | throw new RiceIllegalArgumentException("documentSearchCriteria was null"); |
166 | |
} |
167 | 0 | if (StringUtils.isBlank(customizerName)) { |
168 | 0 | throw new RiceIllegalArgumentException("customizerName was null or blank"); |
169 | |
} |
170 | 0 | DocumentSearchCustomizer customizer = loadCustomizer(customizerName); |
171 | 0 | return customizer.customizeResultSetConfiguration(documentSearchCriteria); |
172 | |
} |
173 | |
|
174 | |
@Override |
175 | |
public Set<DocumentSearchCustomization> getEnabledCustomizations(String documentTypeName, String customizerName) |
176 | |
throws RiceIllegalArgumentException { |
177 | 0 | if (StringUtils.isBlank(documentTypeName)) { |
178 | 0 | throw new RiceIllegalArgumentException("documentTypeName was null or blank"); |
179 | |
} |
180 | 0 | if (StringUtils.isBlank(customizerName)) { |
181 | 0 | throw new RiceIllegalArgumentException("customizerName was null or blank"); |
182 | |
} |
183 | 0 | DocumentSearchCustomizer customizer = loadCustomizer(documentTypeName); |
184 | 0 | Set<DocumentSearchCustomization> customizations = new HashSet<DocumentSearchCustomization>(); |
185 | 0 | if (customizer.isCustomizeCriteriaEnabled(documentTypeName)) { |
186 | 0 | customizations.add(DocumentSearchCustomization.CRITERIA); |
187 | |
} |
188 | 0 | if (customizer.isCustomizeClearCriteriaEnabled(documentTypeName)) { |
189 | 0 | customizations.add(DocumentSearchCustomization.CLEAR_CRITERIA); |
190 | |
} |
191 | 0 | if (customizer.isCustomizeResultsEnabled(documentTypeName)) { |
192 | 0 | customizations.add(DocumentSearchCustomization.RESULTS); |
193 | |
} |
194 | 0 | if (customizer.isCustomizeResultSetFieldsEnabled(documentTypeName)) { |
195 | 0 | customizations.add(DocumentSearchCustomization.RESULT_SET_FIELDS); |
196 | |
} |
197 | 0 | return Collections.unmodifiableSet(customizations); |
198 | |
} |
199 | |
|
200 | |
private SearchableAttribute loadSearchableAttribute(ExtensionDefinition extensionDefinition) { |
201 | 0 | Object searchableAttribute = ExtensionUtils.loadExtension(extensionDefinition); |
202 | 0 | if (searchableAttribute == null) { |
203 | 0 | throw new RiceIllegalArgumentException("Failed to load SearchableAttribute for: " + extensionDefinition); |
204 | |
} |
205 | 0 | return (SearchableAttribute)searchableAttribute; |
206 | |
} |
207 | |
|
208 | |
private DocumentSearchCustomizer loadCustomizer(String customizerName) { |
209 | 0 | ExtensionDefinition extensionDefinition = getExtensionRepositoryService().getExtensionByName(customizerName); |
210 | 0 | if (extensionDefinition == null) { |
211 | 0 | throw new RiceIllegalArgumentException("Failed to locate a DocumentSearchCustomizer with the given name: " + customizerName); |
212 | |
} |
213 | 0 | DocumentSearchCustomizer customizer = ExtensionUtils.loadExtension(extensionDefinition); |
214 | 0 | if (customizer == null) { |
215 | 0 | throw new RiceIllegalArgumentException("Failed to load DocumentSearchCustomizer for: " + extensionDefinition); |
216 | |
} |
217 | 0 | return customizer; |
218 | |
} |
219 | |
|
220 | |
protected ExtensionRepositoryService getExtensionRepositoryService() { |
221 | 0 | return extensionRepositoryService; |
222 | |
} |
223 | |
|
224 | |
public void setExtensionRepositoryService(ExtensionRepositoryService extensionRepositoryService) { |
225 | 0 | this.extensionRepositoryService = extensionRepositoryService; |
226 | 0 | } |
227 | |
|
228 | |
} |