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 com.google.common.collect.Iterables;
19 import org.kuali.rice.core.api.uif.RemotableAttributeField;
20 import org.kuali.rice.core.api.util.ConcreteKeyValue;
21 import org.kuali.rice.core.api.util.KeyValue;
22 import org.kuali.rice.kew.doctype.ApplicationDocumentStatus;
23 import org.kuali.rice.kew.doctype.bo.DocumentType;
24 import org.kuali.rice.kew.engine.node.RouteNode;
25 import org.kuali.rice.kew.framework.document.search.DocumentSearchCriteriaConfiguration;
26 import org.kuali.rice.kew.service.KEWServiceLocator;
27 import org.kuali.rice.kew.api.KewApiConstants;
28 import org.kuali.rice.kns.util.FieldUtils;
29 import org.kuali.rice.kns.web.ui.Field;
30 import org.kuali.rice.kns.web.ui.Row;
31
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class DocumentSearchCriteriaProcessorKEWAdapter implements DocumentSearchCriteriaProcessor {
67
68
69
70 public static final String ADVANCED_SEARCH_FIELD = "isAdvancedSearch";
71
72
73
74 public static final String SUPERUSER_SEARCH_FIELD = "superUserSearch";
75
76
77
78 public static final String CLEARSAVED_SEARCH_FIELD = "resetSavedSearch";
79
80
81
82
83 private static final String DOCUMENT_ATTRIBUTE_FIELD_MARKER = "DOCUMENT_ATTRIBUTE_FIELD_MARKER";
84
85 private static final String APPLICATION_DOCUMENT_STATUS = "applicationDocumentStatus";
86 private static final String DATE_APP_DOC_STATUS_CHANGED_FROM = "rangeLowerBoundKeyPrefix_dateApplicationDocumentStatusChanged";
87 private static final String DATE_APP_DOC_STATUS_CHANGED = "dateApplicationDocumentStatusChanged";
88 private static final String ROUTE_NODE_NAME = "routeNodeName";
89 private static final String ROUTE_NODE_LOGIC = "routeNodeLogic";
90
91 private static final String[] BASIC_FIELD_NAMES = {
92 "documentTypeName",
93 "initiatorPrincipalName",
94 "documentId",
95 "groupViewerName",
96 "dateCreated",
97 DOCUMENT_ATTRIBUTE_FIELD_MARKER,
98 "saveName"
99 };
100
101 private static final String[] ADVANCED_FIELD_NAMES = {
102 "documentTypeName",
103 "initiatorPrincipalName",
104 "approverPrincipalName",
105 "viewerPrincipalName",
106 "groupViewerName",
107 "groupViewerId",
108 "documentId",
109 "applicationDocumentId",
110 "statusCode",
111 APPLICATION_DOCUMENT_STATUS,
112 DATE_APP_DOC_STATUS_CHANGED,
113 ROUTE_NODE_NAME,
114 ROUTE_NODE_LOGIC,
115 "dateCreated",
116 "dateApproved",
117 "dateLastModified",
118 "dateFinalized",
119 "title",
120 DOCUMENT_ATTRIBUTE_FIELD_MARKER,
121 "saveName"
122 };
123
124
125
126
127 private static final Collection<String> DOCUMENTTYPE_DEPENDENT_FIELDS = Arrays.asList(new String[] { DOCUMENT_ATTRIBUTE_FIELD_MARKER, APPLICATION_DOCUMENT_STATUS, DATE_APP_DOC_STATUS_CHANGED_FROM, DATE_APP_DOC_STATUS_CHANGED, ROUTE_NODE_NAME, ROUTE_NODE_LOGIC });
128
129
130
131 private static final Collection<String> DOCSTATUS_DEPENDENT_FIELDS = Arrays.asList(new String[] { APPLICATION_DOCUMENT_STATUS, DATE_APP_DOC_STATUS_CHANGED_FROM, DATE_APP_DOC_STATUS_CHANGED });
132
133
134 @Override
135 public List<Row> getRows(DocumentType documentType, List<Row> defaultRows, boolean advancedSearch, boolean superUserSearch) {
136 List<Row> rows = null;
137 if(advancedSearch) {
138 rows = loadRowsForAdvancedSearch(defaultRows, documentType);
139 } else {
140 rows = loadRowsForBasicSearch(defaultRows, documentType);
141 }
142 addHiddenFields(rows, advancedSearch, superUserSearch);
143 return rows;
144 }
145
146 protected List<Row> loadRowsForAdvancedSearch(List<Row> defaultRows, DocumentType documentType) {
147 List<Row> rows = new ArrayList<Row>();
148 loadRowsWithFields(rows, defaultRows, ADVANCED_FIELD_NAMES, documentType);
149 return rows;
150 }
151
152 protected List<Row> loadRowsForBasicSearch(List<Row> defaultRows, DocumentType documentType) {
153 List<Row> rows = new ArrayList<Row>();
154 loadRowsWithFields(rows, defaultRows, BASIC_FIELD_NAMES, documentType);
155 return rows;
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170 protected void loadRowsWithFields(List<Row> rowsToLoad, List<Row> defaultRows, String[] fieldNames,
171 DocumentType documentType) {
172
173 for (String fieldName : fieldNames) {
174 if (DOCUMENTTYPE_DEPENDENT_FIELDS.contains(fieldName) && documentType == null) {
175 continue;
176 }
177
178 if (DOCSTATUS_DEPENDENT_FIELDS.contains(fieldName) && !documentType.isAppDocStatusInUse()) {
179 continue;
180 }
181 if (fieldName.equals(DOCUMENT_ATTRIBUTE_FIELD_MARKER)) {
182 rowsToLoad.addAll(getDocumentAttributeRows(documentType));
183 continue;
184 }
185
186
187
188
189 for (Row row : defaultRows) {
190 boolean matched = false;
191
192
193 for (Field field : row.getFields()) {
194
195
196 if (field.getPropertyName().equals(fieldName) || field.getPropertyName().endsWith("_" + fieldName)) {
197 matched = true;
198 if (APPLICATION_DOCUMENT_STATUS.equals(field.getPropertyName())) {
199
200
201
202 applyApplicationDocumentStatusCustomizations(field, documentType);
203 break;
204 } else if (ROUTE_NODE_NAME.equals(field.getPropertyName())) {
205
206 applyRouteNodeNameCustomizations(field, documentType);
207 }
208 }
209 }
210 if (matched) {
211 rowsToLoad.add(row);
212 }
213 }
214 }
215 }
216
217
218
219
220 protected List<Row> getDocumentAttributeRows(DocumentType documentType) {
221 List<Row> documentAttributeRows = new ArrayList<Row>();
222 DocumentSearchCriteriaConfiguration configuration =
223 KEWServiceLocator.getDocumentSearchCustomizationMediator().
224 getDocumentSearchCriteriaConfiguration(documentType);
225 if (configuration != null) {
226 List<RemotableAttributeField> remotableAttributeFields = configuration.getFlattenedSearchAttributeFields();
227 if (remotableAttributeFields != null && !remotableAttributeFields.isEmpty()) {
228 documentAttributeRows.addAll(FieldUtils.convertRemotableAttributeFields(remotableAttributeFields));
229 }
230 }
231 List<Row> fixedDocumentAttributeRows = new ArrayList<Row>();
232 for (Row row : documentAttributeRows) {
233 List<Field> fields = row.getFields();
234 for (Field field : fields) {
235
236 if(field.getMaxLength() == 0) {
237 field.setMaxLength(100);
238 }
239 if(field.isDatePicker() && field.isRanged()) {
240 Field newDate = FieldUtils.createRangeDateField(field);
241 List<Field> newFields = new ArrayList<Field>();
242 newFields.add(newDate);
243 fixedDocumentAttributeRows.addAll(FieldUtils.wrapFields(newFields));
244 }
245
246 field.setPropertyName(KewApiConstants.DOCUMENT_ATTRIBUTE_FIELD_PREFIX + field.getPropertyName());
247 }
248 fixedDocumentAttributeRows.add(row);
249 }
250
251 return fixedDocumentAttributeRows;
252 }
253
254
255
256
257
258
259
260
261
262 protected void applyApplicationDocumentStatusCustomizations(Field field, DocumentType documentType) {
263 List<ApplicationDocumentStatus> validStatuses = documentType.getValidApplicationStatuses();
264 if (validStatuses == null || validStatuses.size() == 0){
265
266
267
268
269 field.setFieldType(Field.TEXT);
270 } else {
271
272
273 field.setFieldType(Field.DROPDOWN);
274 List<KeyValue> validValues = new ArrayList<KeyValue>();
275 for (ApplicationDocumentStatus status: validStatuses) {
276 validValues.add(new ConcreteKeyValue(status.getStatusName(), status.getStatusName()));
277 }
278 field.setFieldValidValues(validValues);
279
280
281
282
283 }
284 }
285
286 protected void applyRouteNodeNameCustomizations(Field field, DocumentType documentType) {
287 List<RouteNode> nodes = KEWServiceLocator.getRouteNodeService().getFlattenedNodes(documentType, true);
288 List<KeyValue> values = new ArrayList<KeyValue>(nodes.size());
289 for (RouteNode node: nodes) {
290 values.add(new ConcreteKeyValue(node.getName(), node.getName()));
291 }
292 field.setFieldValidValues(values);
293 }
294
295 protected void addHiddenFields(List<Row> rows, boolean advancedSearch, boolean superUserSearch) {
296 Row hiddenRow = new Row();
297 hiddenRow.setHidden(true);
298
299 Field detailedField = new Field();
300 detailedField.setPropertyName(ADVANCED_SEARCH_FIELD);
301 detailedField.setPropertyValue(advancedSearch ? "YES" : "NO");
302 detailedField.setFieldType(Field.HIDDEN);
303
304 Field superUserSearchField = new Field();
305 superUserSearchField.setPropertyName(SUPERUSER_SEARCH_FIELD);
306 superUserSearchField.setPropertyValue(superUserSearch ? "YES" : "NO");
307 superUserSearchField.setFieldType(Field.HIDDEN);
308
309 Field clearSavedSearchField = new Field();
310 clearSavedSearchField .setPropertyName(CLEARSAVED_SEARCH_FIELD);
311 clearSavedSearchField .setPropertyValue(superUserSearch ? "YES" : "NO");
312 clearSavedSearchField .setFieldType(Field.HIDDEN);
313
314 List<Field> hiddenFields = new ArrayList<Field>();
315 hiddenFields.add(detailedField);
316 hiddenFields.add(superUserSearchField);
317 hiddenFields.add(clearSavedSearchField);
318 hiddenRow.setFields(hiddenFields);
319 rows.add(hiddenRow);
320
321 }
322
323 }