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.beanutils.PropertyUtils; |
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
21 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
22 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
23 | |
import org.kuali.rice.kns.web.ui.Field; |
24 | |
import org.kuali.rice.kns.web.ui.Row; |
25 | |
import org.kuali.rice.krad.util.KRADConstants; |
26 | |
|
27 | |
import java.lang.reflect.InvocationTargetException; |
28 | |
import java.util.*; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class DocumentLookupCriteriaBuilder { |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public static DocSearchCriteriaDTO populateCriteria(Map<String,String[]> fieldsForLookup) { |
45 | 0 | DocSearchCriteriaDTO criteria = new DocSearchCriteriaDTO(); |
46 | 0 | Map<String,String[]> fieldsToSet = new HashMap<String,String[]>(); |
47 | 0 | for (String formKey : fieldsForLookup.keySet()) { |
48 | 0 | if(!(formKey.equalsIgnoreCase(KRADConstants.BACK_LOCATION) || |
49 | |
formKey.equalsIgnoreCase(KRADConstants.DOC_FORM_KEY)) && fieldsForLookup.get(formKey)!=null && fieldsForLookup.get(formKey).length!=0) { |
50 | 0 | fieldsToSet.put(formKey, fieldsForLookup.get(formKey)); |
51 | |
} |
52 | |
} |
53 | 0 | for (String fieldToSet : fieldsToSet.keySet()) { |
54 | 0 | String valueToSet = ""; |
55 | 0 | String[] valuesToSet = fieldsToSet.get(fieldToSet); |
56 | |
|
57 | 0 | if(valuesToSet.length >= 1){ |
58 | 0 | for(String value: valuesToSet){ |
59 | 0 | valueToSet += value + ","; |
60 | |
} |
61 | 0 | valueToSet = valueToSet.substring(0, valueToSet.length()-1); |
62 | |
}else{ |
63 | 0 | valueToSet = valuesToSet[0]; |
64 | |
} |
65 | |
|
66 | |
try { |
67 | 0 | PropertyUtils.setNestedProperty(criteria, fieldToSet, valueToSet); |
68 | 0 | } catch (IllegalAccessException e) { |
69 | 0 | e.printStackTrace(); |
70 | 0 | } catch (InvocationTargetException e) { |
71 | 0 | e.printStackTrace(); |
72 | 0 | } catch (NoSuchMethodException e) { |
73 | |
|
74 | |
|
75 | 0 | } |
76 | 0 | } |
77 | |
|
78 | |
|
79 | 0 | criteria.setDocTypeFullName(getValidDocumentTypeName(criteria.getDocTypeFullName())); |
80 | |
|
81 | 0 | if(StringUtils.isNotEmpty(criteria.getDocTypeFullName())) { |
82 | 0 | addSearchableAttributesToCriteria(criteria, fieldsForLookup); |
83 | |
} |
84 | 0 | return criteria; |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public static void addSearchableAttributesToCriteria(DocSearchCriteriaDTO criteria, Map<String,String[]> propertyFields) { |
95 | 0 | if (criteria != null) { |
96 | 0 | DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(criteria.getDocTypeFullName()); |
97 | 0 | if (docType == null) { |
98 | 0 | return; |
99 | |
} |
100 | 0 | criteria.getSearchableAttributes().clear(); |
101 | 0 | if (!propertyFields.isEmpty()) { |
102 | 0 | Map<String, SearchAttributeCriteriaComponent> criteriaComponentsByFormKey = new HashMap<String, SearchAttributeCriteriaComponent>(); |
103 | 0 | for (SearchableAttribute searchableAttribute : docType.getSearchableAttributes()) { |
104 | 0 | for (Row row : searchableAttribute.getSearchingRows( |
105 | |
DocSearchUtils.getDocumentSearchContext("", docType.getName(), ""))) { |
106 | 0 | for (Field field : row.getFields()) { |
107 | 0 | if (field instanceof Field) { |
108 | 0 | SearchableAttributeValue searchableAttributeValue = DocSearchUtils.getSearchableAttributeValueByDataTypeString(field.getFieldDataType()); |
109 | 0 | SearchAttributeCriteriaComponent sacc = new SearchAttributeCriteriaComponent(field.getPropertyName(), null, field.getPropertyName(), searchableAttributeValue); |
110 | 0 | sacc.setRangeSearch(field.isMemberOfRange()); |
111 | 0 | sacc.setCaseSensitive(!field.isUpperCase()); |
112 | |
|
113 | |
|
114 | 0 | sacc.setAllowInlineRange(true); |
115 | |
|
116 | |
|
117 | 0 | sacc.setSearchInclusive(field.isInclusive()); |
118 | 0 | sacc.setLookupableFieldType(field.getFieldType()); |
119 | 0 | sacc.setSearchable(field.isIndexedForSearch()); |
120 | 0 | sacc.setCanHoldMultipleValues(Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())); |
121 | 0 | criteriaComponentsByFormKey.put(field.getPropertyName(), sacc); |
122 | 0 | } else { |
123 | 0 | throw new RiceRuntimeException("Fields must be of type org.kuali.rice.kew.docsearch.Field"); |
124 | |
} |
125 | |
} |
126 | |
} |
127 | |
} |
128 | 0 | for (String propertyField : propertyFields.keySet()) |
129 | |
{ |
130 | 0 | SearchAttributeCriteriaComponent sacc = (SearchAttributeCriteriaComponent) criteriaComponentsByFormKey.get(propertyField); |
131 | 0 | if (sacc != null) |
132 | |
{ |
133 | 0 | if (sacc.getSearchableAttributeValue() == null) |
134 | |
{ |
135 | 0 | String errorMsg = "Searchable attribute with form field key " + sacc.getFormKey() + " does not have a valid SearchableAttributeValue"; |
136 | |
|
137 | 0 | throw new RuntimeException(errorMsg); |
138 | |
} |
139 | 0 | String[] values = propertyFields.get(propertyField); |
140 | 0 | if (Field.MULTI_VALUE_FIELD_TYPES.contains(sacc.getLookupableFieldType())) |
141 | |
{ |
142 | |
|
143 | 0 | sacc.setCanHoldMultipleValues(true); |
144 | 0 | if (propertyField == null) |
145 | |
{ |
146 | 0 | sacc.setValues(new ArrayList<String>()); |
147 | |
} else |
148 | |
{ |
149 | 0 | if (values != null) |
150 | |
{ |
151 | 0 | sacc.setValues(Arrays.asList(values)); |
152 | |
} |
153 | |
} |
154 | |
} else |
155 | |
{ |
156 | 0 | sacc.setValue(values[0]); |
157 | |
} |
158 | 0 | criteria.addSearchableAttribute(sacc); |
159 | |
} |
160 | 0 | } |
161 | |
} |
162 | |
} |
163 | 0 | } |
164 | |
|
165 | |
private static String getValidDocumentTypeName(String docTypeName) { |
166 | 0 | if (StringUtils.isNotEmpty(docTypeName)) { |
167 | 0 | DocumentType dTypeCriteria = new DocumentType(); |
168 | 0 | dTypeCriteria.setName(docTypeName.trim()); |
169 | 0 | dTypeCriteria.setActive(true); |
170 | 0 | Collection<DocumentType> docTypeList = KEWServiceLocator.getDocumentTypeService().find(dTypeCriteria, null, false); |
171 | |
|
172 | |
|
173 | 0 | if(docTypeList != null){ |
174 | 0 | for(DocumentType dType: docTypeList){ |
175 | 0 | if (StringUtils.equals(docTypeName.toUpperCase(), dType.getName().toUpperCase())) { |
176 | 0 | return dType.getName(); |
177 | |
} |
178 | |
} |
179 | |
} |
180 | |
} |
181 | 0 | return docTypeName; |
182 | |
} |
183 | |
} |