1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.docsearch; |
18 | |
|
19 | |
import java.sql.Date; |
20 | |
import java.sql.Timestamp; |
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Arrays; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
import java.util.StringTokenizer; |
28 | |
|
29 | |
import org.apache.commons.lang.StringUtils; |
30 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
31 | |
import org.kuali.rice.core.api.reflect.ObjectDefinition; |
32 | |
import org.kuali.rice.core.framework.resourceloader.ObjectDefinitionResolver; |
33 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
34 | |
import org.kuali.rice.core.util.RiceConstants; |
35 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
36 | |
import org.kuali.rice.kew.docsearch.web.SearchAttributeFormContainer; |
37 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
38 | |
import org.kuali.rice.kew.doctype.service.DocumentTypeService; |
39 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
40 | |
import org.kuali.rice.kew.user.UserUtils; |
41 | |
import org.kuali.rice.kns.web.ui.Field; |
42 | |
import org.kuali.rice.kns.web.ui.Row; |
43 | |
import org.kuali.rice.krad.UserSession; |
44 | |
import org.kuali.rice.krad.util.GlobalVariables; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public final class DocSearchUtils { |
53 | |
|
54 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocSearchUtils.class); |
55 | |
|
56 | 0 | private DocSearchUtils() { |
57 | 0 | throw new UnsupportedOperationException("do not call"); |
58 | |
} |
59 | |
|
60 | |
public static List<SearchableAttributeValue> getSearchableAttributeValueObjectTypes() { |
61 | 0 | List<SearchableAttributeValue> searchableAttributeValueClasses = new ArrayList<SearchableAttributeValue>(); |
62 | 0 | for (Object aSEARCHABLE_ATTRIBUTE_BASE_CLASS_LIST : SearchableAttribute.SEARCHABLE_ATTRIBUTE_BASE_CLASS_LIST) |
63 | |
{ |
64 | 0 | Class searchAttributeValueClass = (Class) aSEARCHABLE_ATTRIBUTE_BASE_CLASS_LIST; |
65 | 0 | ObjectDefinition objDef = new ObjectDefinition(searchAttributeValueClass); |
66 | 0 | SearchableAttributeValue attributeValue = (SearchableAttributeValue) ObjectDefinitionResolver.createObject(objDef, ClassLoaderUtils.getDefaultClassLoader(), false); |
67 | 0 | searchableAttributeValueClasses.add(attributeValue); |
68 | 0 | } |
69 | 0 | return searchableAttributeValueClasses; |
70 | |
} |
71 | |
|
72 | |
public static SearchableAttributeValue getSearchableAttributeValueByDataTypeString(String dataType) { |
73 | 0 | SearchableAttributeValue returnableValue = null; |
74 | 0 | if (StringUtils.isBlank(dataType)) { |
75 | 0 | return returnableValue; |
76 | |
} |
77 | 0 | for (SearchableAttributeValue attValue : getSearchableAttributeValueObjectTypes()) |
78 | |
{ |
79 | 0 | if (dataType.equalsIgnoreCase(attValue.getAttributeDataType())) |
80 | |
{ |
81 | 0 | if (returnableValue != null) |
82 | |
{ |
83 | 0 | String errorMsg = "Found two SearchableAttributeValue objects with same data type string ('" + dataType + "' while ignoring case): " + returnableValue.getClass().getName() + " and " + attValue.getClass().getName(); |
84 | 0 | LOG.error("getSearchableAttributeValueByDataTypeString() " + errorMsg); |
85 | 0 | throw new RuntimeException(errorMsg); |
86 | |
} |
87 | 0 | LOG.debug("getSearchableAttributeValueByDataTypeString() SearchableAttributeValue class name is " + attValue.getClass().getName() + "... ojbConcreteClassName is " + attValue.getOjbConcreteClass()); |
88 | 0 | ObjectDefinition objDef = new ObjectDefinition(attValue.getClass()); |
89 | 0 | returnableValue = (SearchableAttributeValue) ObjectDefinitionResolver.createObject(objDef, ClassLoaderUtils.getDefaultClassLoader(), false); |
90 | 0 | } |
91 | |
} |
92 | 0 | return returnableValue; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
public static String getDisplayValueWithDateOnly(Timestamp value) { |
97 | 0 | return RiceConstants.getDefaultDateFormat().format(new Date(value.getTime())); |
98 | |
} |
99 | |
|
100 | |
public static String getDisplayValueWithDateTime(Timestamp value) { |
101 | 0 | return RiceConstants.getDefaultDateAndTimeFormat().format(new Date(value.getTime())); |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
private static final String CURRENT_USER_PREFIX = "CURRENT_USER."; |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public static List<SearchAttributeCriteriaComponent> buildSearchableAttributesFromString(String searchableAttributeString, String documentTypeName) { |
117 | 0 | List<SearchAttributeCriteriaComponent> searchableAttributes = new ArrayList<SearchAttributeCriteriaComponent>(); |
118 | 0 | Map<String, SearchAttributeCriteriaComponent> criteriaComponentsByKey = new HashMap<String, SearchAttributeCriteriaComponent>(); |
119 | |
|
120 | 0 | DocumentType docType = getDocumentType(documentTypeName); |
121 | |
|
122 | 0 | if (docType != null) { |
123 | |
|
124 | 0 | for (SearchableAttribute searchableAttribute : docType.getSearchableAttributes()) { |
125 | |
|
126 | 0 | for (Row row : searchableAttribute.getSearchingRows( |
127 | |
DocSearchUtils.getDocumentSearchContext("", docType.getName(), ""))) { |
128 | 0 | for (Field field : row.getFields()) { |
129 | 0 | if (field instanceof Field) { |
130 | 0 | SearchableAttributeValue searchableAttributeValue = DocSearchUtils.getSearchableAttributeValueByDataTypeString(field.getFieldDataType()); |
131 | 0 | SearchAttributeCriteriaComponent sacc = new SearchAttributeCriteriaComponent(field.getPropertyName(), null, field.getPropertyName(), searchableAttributeValue); |
132 | 0 | sacc.setRangeSearch(field.isMemberOfRange()); |
133 | 0 | sacc.setSearchInclusive(field.isInclusive()); |
134 | 0 | sacc.setSearchable(field.isIndexedForSearch()); |
135 | 0 | sacc.setLookupableFieldType(field.getFieldType()); |
136 | 0 | sacc.setCanHoldMultipleValues(Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())); |
137 | 0 | criteriaComponentsByKey.put(field.getPropertyName(), sacc); |
138 | 0 | } else { |
139 | 0 | throw new RiceRuntimeException("Fields must be of type org.kuali.rice.kew.docsearch.Field"); |
140 | |
} |
141 | |
} |
142 | |
} |
143 | |
} |
144 | |
} |
145 | |
|
146 | 0 | Map<String, List<String>> checkForMultiValueSearchableAttributes = new HashMap<String, List<String>>(); |
147 | 0 | if ((searchableAttributeString != null) && (searchableAttributeString.trim().length() > 0)) { |
148 | 0 | StringTokenizer tokenizer = new StringTokenizer(searchableAttributeString, ","); |
149 | 0 | while (tokenizer.hasMoreTokens()) { |
150 | 0 | String searchableAttribute = tokenizer.nextToken(); |
151 | 0 | int index = searchableAttribute.indexOf(":"); |
152 | 0 | if (index != -1) { |
153 | 0 | String key = searchableAttribute.substring(0, index); |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | 0 | String value = searchableAttribute.substring(index + 1); |
161 | 0 | if (value.startsWith(CURRENT_USER_PREFIX)) { |
162 | 0 | String idType = value.substring(CURRENT_USER_PREFIX.length()); |
163 | 0 | UserSession session = GlobalVariables.getUserSession(); |
164 | 0 | String idValue = UserUtils.getIdValue(idType, session.getPerson()); |
165 | 0 | if (!StringUtils.isBlank(idValue)) { |
166 | 0 | value = idValue; |
167 | |
} |
168 | |
} |
169 | 0 | SearchAttributeCriteriaComponent critComponent = criteriaComponentsByKey.get(key); |
170 | 0 | if (critComponent == null) { |
171 | |
|
172 | |
|
173 | 0 | continue; |
174 | |
} |
175 | 0 | if (critComponent.getSearchableAttributeValue() == null) { |
176 | 0 | String errorMsg = "Cannot find SearchableAttributeValue for given key '" + key + "'"; |
177 | 0 | LOG.error("buildSearchableAttributesFromString() " + errorMsg); |
178 | 0 | throw new RuntimeException(errorMsg); |
179 | |
} |
180 | 0 | if (critComponent.isCanHoldMultipleValues()) { |
181 | |
|
182 | 0 | if (checkForMultiValueSearchableAttributes.containsKey(key)) { |
183 | 0 | List<String> keyList = checkForMultiValueSearchableAttributes.get(key); |
184 | 0 | keyList.add(value); |
185 | 0 | checkForMultiValueSearchableAttributes.put(key, keyList); |
186 | 0 | } else { |
187 | 0 | List<String> tempList = new ArrayList<String>(); |
188 | 0 | tempList.add(value); |
189 | |
|
190 | 0 | checkForMultiValueSearchableAttributes.put(key, tempList); |
191 | 0 | searchableAttributes.add(critComponent); |
192 | 0 | } |
193 | |
} else { |
194 | |
|
195 | 0 | if (checkForMultiValueSearchableAttributes.containsKey(key)) { |
196 | |
|
197 | 0 | String error = "Attempting to add multiple values to a search attribute (key: '" + key + "') that does not suppor them"; |
198 | 0 | LOG.error("buildSearchableAttributesFromString() " + error); |
199 | |
|
200 | |
|
201 | |
} |
202 | 0 | critComponent.setValue(value); |
203 | 0 | searchableAttributes.add(critComponent); |
204 | |
} |
205 | |
|
206 | |
} |
207 | 0 | } |
208 | 0 | for (SearchAttributeCriteriaComponent criteriaComponent : searchableAttributes) |
209 | |
{ |
210 | 0 | if (criteriaComponent.isCanHoldMultipleValues()) |
211 | |
{ |
212 | 0 | List<String> values = checkForMultiValueSearchableAttributes.get(criteriaComponent.getFormKey()); |
213 | 0 | criteriaComponent.setValue(null); |
214 | 0 | criteriaComponent.setValues(values); |
215 | 0 | } |
216 | |
} |
217 | |
} |
218 | |
|
219 | 0 | return searchableAttributes; |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
public static void addSearchableAttributesToCriteria(DocSearchCriteriaDTO criteria, List propertyFields, String searchAttributesString) { |
239 | 0 | addSearchableAttributesToCriteria(criteria, propertyFields, searchAttributesString, false); |
240 | 0 | } |
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
public static void addSearchableAttributesToCriteria(DocSearchCriteriaDTO criteria, List propertyFields, boolean setAttributesStrictly) { |
261 | 0 | addSearchableAttributesToCriteria(criteria, propertyFields, null, setAttributesStrictly); |
262 | 0 | } |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
public static void addSearchableAttributesToCriteria(DocSearchCriteriaDTO criteria, List propertyFields, String searchAttributesString, boolean setAttributesStrictly) { |
282 | 0 | if (criteria != null) { |
283 | 0 | DocumentType docType = getDocumentType(criteria.getDocTypeFullName()); |
284 | 0 | if (docType == null) { |
285 | 0 | return; |
286 | |
} |
287 | 0 | criteria.getSearchableAttributes().clear(); |
288 | 0 | Map<String, SearchAttributeCriteriaComponent> urlParameterSearchAttributesByFormKey = new HashMap<String, SearchAttributeCriteriaComponent>(); |
289 | 0 | if (!StringUtils.isBlank(searchAttributesString)) { |
290 | 0 | List<SearchAttributeCriteriaComponent> components = buildSearchableAttributesFromString(searchAttributesString, docType.getName()); |
291 | 0 | for (SearchAttributeCriteriaComponent component : components) { |
292 | 0 | urlParameterSearchAttributesByFormKey.put(component.getFormKey(), component); |
293 | 0 | criteria.addSearchableAttribute(component); |
294 | |
} |
295 | |
|
296 | |
} |
297 | 0 | if (!propertyFields.isEmpty()) { |
298 | 0 | Map<String, SearchAttributeCriteriaComponent> criteriaComponentsByFormKey = new HashMap<String, SearchAttributeCriteriaComponent>(); |
299 | 0 | for (SearchableAttribute searchableAttribute : docType.getSearchableAttributes()) { |
300 | |
|
301 | 0 | for (Row row : searchableAttribute.getSearchingRows( |
302 | |
DocSearchUtils.getDocumentSearchContext("", docType.getName(), ""))) { |
303 | 0 | for (Field field : row.getFields()) { |
304 | 0 | if (field instanceof Field) { |
305 | 0 | SearchableAttributeValue searchableAttributeValue = DocSearchUtils.getSearchableAttributeValueByDataTypeString(field.getFieldDataType()); |
306 | 0 | SearchAttributeCriteriaComponent sacc = new SearchAttributeCriteriaComponent(field.getPropertyName(), null, field.getPropertyName(), searchableAttributeValue); |
307 | 0 | sacc.setRangeSearch(field.isMemberOfRange()); |
308 | 0 | sacc.setSearchInclusive(field.isInclusive()); |
309 | 0 | sacc.setLookupableFieldType(field.getFieldType()); |
310 | 0 | sacc.setSearchable(field.isIndexedForSearch()); |
311 | 0 | sacc.setCanHoldMultipleValues(Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())); |
312 | 0 | criteriaComponentsByFormKey.put(field.getPropertyName(), sacc); |
313 | 0 | } else { |
314 | 0 | throw new RiceRuntimeException("Fields must be of type org.kuali.rice.kew.docsearch.Field"); |
315 | |
} |
316 | |
} |
317 | |
} |
318 | |
} |
319 | 0 | for (Iterator iterator = propertyFields.iterator(); iterator.hasNext();) { |
320 | 0 | SearchAttributeFormContainer propertyField = (SearchAttributeFormContainer) iterator.next(); |
321 | 0 | SearchAttributeCriteriaComponent sacc = criteriaComponentsByFormKey.get(propertyField.getKey()); |
322 | 0 | if (sacc != null) { |
323 | 0 | if (sacc.getSearchableAttributeValue() == null) { |
324 | 0 | String errorMsg = "Searchable attribute with form field key " + sacc.getFormKey() + " does not have a valid SearchableAttributeValue"; |
325 | 0 | LOG.error("addSearchableAttributesToCriteria() " + errorMsg); |
326 | 0 | throw new RuntimeException(errorMsg); |
327 | |
} |
328 | |
|
329 | 0 | if (urlParameterSearchAttributesByFormKey.containsKey(sacc.getFormKey())) { |
330 | 0 | setupPropertyField(urlParameterSearchAttributesByFormKey.get(sacc.getFormKey()), propertyFields); |
331 | |
} else { |
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | 0 | if (Field.MULTI_VALUE_FIELD_TYPES.contains(sacc.getLookupableFieldType())) { |
338 | |
|
339 | 0 | sacc.setCanHoldMultipleValues(true); |
340 | 0 | if (propertyField.getValues() == null) { |
341 | 0 | sacc.setValues(new ArrayList<String>()); |
342 | |
} else { |
343 | 0 | sacc.setValues(Arrays.asList(propertyField.getValues())); |
344 | |
} |
345 | |
} else { |
346 | 0 | sacc.setValue(propertyField.getValue()); |
347 | |
} |
348 | 0 | criteria.addSearchableAttribute(sacc); |
349 | |
} |
350 | |
} else { |
351 | 0 | if (setAttributesStrictly) { |
352 | 0 | String message = "Cannot find matching search attribute with key '" + propertyField.getKey() + "' on document type '" + docType.getName() + "'"; |
353 | 0 | LOG.error(message); |
354 | 0 | throw new WorkflowRuntimeException(message); |
355 | |
} |
356 | |
} |
357 | 0 | } |
358 | |
} |
359 | |
} |
360 | 0 | } |
361 | |
|
362 | |
public static void setupPropertyField(SearchAttributeCriteriaComponent searchableAttribute, List propertyFields) { |
363 | 0 | SearchAttributeFormContainer propertyField = getPropertyField(searchableAttribute.getFormKey(), propertyFields); |
364 | 0 | if (propertyField != null) { |
365 | 0 | propertyField.setValue(searchableAttribute.getValue()); |
366 | 0 | if (searchableAttribute.getValues() != null) { |
367 | 0 | propertyField.setValues(searchableAttribute.getValues().toArray(new String[searchableAttribute.getValues().size()])); |
368 | |
} |
369 | |
} |
370 | 0 | } |
371 | |
|
372 | |
public static SearchAttributeFormContainer getPropertyField(String key, List propertyFields) { |
373 | 0 | if (StringUtils.isBlank(key)) { |
374 | 0 | return null; |
375 | |
} |
376 | 0 | for (Iterator iter = propertyFields.iterator(); iter.hasNext();) { |
377 | 0 | SearchAttributeFormContainer container = (SearchAttributeFormContainer) iter.next(); |
378 | 0 | if (key.equals(container.getKey())) { |
379 | 0 | return container; |
380 | |
} |
381 | 0 | } |
382 | 0 | return null; |
383 | |
} |
384 | |
|
385 | |
private static DocumentType getDocumentType(String docTypeName) { |
386 | 0 | if ((docTypeName != null && !"".equals(docTypeName))) { |
387 | 0 | return ((DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE)).findByName(docTypeName); |
388 | |
} |
389 | 0 | return null; |
390 | |
} |
391 | |
|
392 | |
public static DocumentSearchContext getDocumentSearchContext(String documentId, String documentTypeName, String documentContent){ |
393 | 0 | DocumentSearchContext documentSearchContext = new DocumentSearchContext(); |
394 | 0 | documentSearchContext.setDocumentId(documentId); |
395 | 0 | documentSearchContext.setDocumentTypeName(documentTypeName); |
396 | 0 | documentSearchContext.setDocumentContent(documentContent); |
397 | 0 | return documentSearchContext; |
398 | |
} |
399 | |
} |