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 "dateCreated",
96 DOCUMENT_ATTRIBUTE_FIELD_MARKER,
97 "saveName"
98 };
99
100 private static final String[] ADVANCED_FIELD_NAMES = {
101 "documentTypeName",
102 "initiatorPrincipalName",
103 "approverPrincipalName",
104 "viewerPrincipalName",
105 "groupViewerName",
106 "groupViewerId",
107 "documentId",
108 "applicationDocumentId",
109 "statusCode",
110 APPLICATION_DOCUMENT_STATUS,
111 DATE_APP_DOC_STATUS_CHANGED,
112 ROUTE_NODE_NAME,
113 ROUTE_NODE_LOGIC,
114 "dateCreated",
115 "dateApproved",
116 "dateLastModified",
117 "dateFinalized",
118 "title",
119 DOCUMENT_ATTRIBUTE_FIELD_MARKER,
120 "saveName"
121 };
122
123
124
125
126 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 });
127
128
129
130 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 });
131
132
133 @Override
134 public List<Row> getRows(DocumentType documentType, List<Row> defaultRows, boolean advancedSearch, boolean superUserSearch) {
135 List<Row> rows = null;
136 if(advancedSearch) {
137 rows = loadRowsForAdvancedSearch(defaultRows, documentType);
138 } else {
139 rows = loadRowsForBasicSearch(defaultRows, documentType);
140 }
141 addHiddenFields(rows, advancedSearch, superUserSearch);
142 return rows;
143 }
144
145 protected List<Row> loadRowsForAdvancedSearch(List<Row> defaultRows, DocumentType documentType) {
146 List<Row> rows = new ArrayList<Row>();
147 loadRowsWithFields(rows, defaultRows, ADVANCED_FIELD_NAMES, documentType);
148 return rows;
149 }
150
151 protected List<Row> loadRowsForBasicSearch(List<Row> defaultRows, DocumentType documentType) {
152 List<Row> rows = new ArrayList<Row>();
153 loadRowsWithFields(rows, defaultRows, BASIC_FIELD_NAMES, documentType);
154 return rows;
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169 protected void loadRowsWithFields(List<Row> rowsToLoad, List<Row> defaultRows, String[] fieldNames,
170 DocumentType documentType) {
171
172 for (String fieldName : fieldNames) {
173 if (DOCUMENTTYPE_DEPENDENT_FIELDS.contains(fieldName) && documentType == null) {
174 continue;
175 }
176
177 if (DOCSTATUS_DEPENDENT_FIELDS.contains(fieldName) && !documentType.isAppDocStatusInUse()) {
178 continue;
179 }
180 if (fieldName.equals(DOCUMENT_ATTRIBUTE_FIELD_MARKER)) {
181 rowsToLoad.addAll(getDocumentAttributeRows(documentType));
182 continue;
183 }
184
185
186
187
188 for (Row row : defaultRows) {
189 boolean matched = false;
190
191
192 for (Field field : row.getFields()) {
193
194
195 if (field.getPropertyName().equals(fieldName) || field.getPropertyName().endsWith("_" + fieldName)) {
196 matched = true;
197 if (APPLICATION_DOCUMENT_STATUS.equals(field.getPropertyName())) {
198
199
200
201 applyApplicationDocumentStatusCustomizations(field, documentType);
202 break;
203 } else if (ROUTE_NODE_NAME.equals(field.getPropertyName())) {
204
205 applyRouteNodeNameCustomizations(field, documentType);
206 }
207 }
208 }
209 if (matched) {
210 rowsToLoad.add(row);
211 }
212 }
213 }
214 }
215
216
217
218
219 protected List<Row> getDocumentAttributeRows(DocumentType documentType) {
220 List<Row> documentAttributeRows = new ArrayList<Row>();
221 DocumentSearchCriteriaConfiguration configuration =
222 KEWServiceLocator.getDocumentSearchCustomizationMediator().
223 getDocumentSearchCriteriaConfiguration(documentType);
224 if (configuration != null) {
225 List<RemotableAttributeField> remotableAttributeFields = configuration.getFlattenedSearchAttributeFields();
226 if (remotableAttributeFields != null && !remotableAttributeFields.isEmpty()) {
227 documentAttributeRows.addAll(FieldUtils.convertRemotableAttributeFields(remotableAttributeFields));
228 }
229 }
230 List<Row> fixedDocumentAttributeRows = new ArrayList<Row>();
231 for (Row row : documentAttributeRows) {
232 List<Field> fields = row.getFields();
233 for (Field field : fields) {
234
235 if(field.getMaxLength() == 0) {
236 field.setMaxLength(100);
237 }
238 if(field.isDatePicker() && field.isRanged()) {
239 Field newDate = FieldUtils.createRangeDateField(field);
240 List<Field> newFields = new ArrayList<Field>();
241 newFields.add(newDate);
242 fixedDocumentAttributeRows.addAll(FieldUtils.wrapFields(newFields));
243 }
244
245 field.setPropertyName(KewApiConstants.DOCUMENT_ATTRIBUTE_FIELD_PREFIX + field.getPropertyName());
246 }
247 fixedDocumentAttributeRows.add(row);
248 }
249
250 return fixedDocumentAttributeRows;
251 }
252
253
254
255
256
257
258
259
260
261 protected void applyApplicationDocumentStatusCustomizations(Field field, DocumentType documentType) {
262 List<ApplicationDocumentStatus> validStatuses = documentType.getValidApplicationStatuses();
263 if (validStatuses == null || validStatuses.size() == 0){
264
265
266
267
268 field.setFieldType(Field.TEXT);
269 } else {
270
271
272 field.setFieldType(Field.DROPDOWN);
273 List<KeyValue> validValues = new ArrayList<KeyValue>();
274 for (ApplicationDocumentStatus status: validStatuses) {
275 validValues.add(new ConcreteKeyValue(status.getStatusName(), status.getStatusName()));
276 }
277 field.setFieldValidValues(validValues);
278
279
280
281
282 }
283 }
284
285 protected void applyRouteNodeNameCustomizations(Field field, DocumentType documentType) {
286 List<RouteNode> nodes = KEWServiceLocator.getRouteNodeService().getFlattenedNodes(documentType, true);
287 List<KeyValue> values = new ArrayList<KeyValue>(nodes.size());
288 for (RouteNode node: nodes) {
289 values.add(new ConcreteKeyValue(node.getName(), node.getName()));
290 }
291 field.setFieldValidValues(values);
292 }
293
294 protected void addHiddenFields(List<Row> rows, boolean advancedSearch, boolean superUserSearch) {
295 Row hiddenRow = new Row();
296 hiddenRow.setHidden(true);
297
298 Field detailedField = new Field();
299 detailedField.setPropertyName(ADVANCED_SEARCH_FIELD);
300 detailedField.setPropertyValue(advancedSearch ? "YES" : "NO");
301 detailedField.setFieldType(Field.HIDDEN);
302
303 Field superUserSearchField = new Field();
304 superUserSearchField.setPropertyName(SUPERUSER_SEARCH_FIELD);
305 superUserSearchField.setPropertyValue(superUserSearch ? "YES" : "NO");
306 superUserSearchField.setFieldType(Field.HIDDEN);
307
308 Field clearSavedSearchField = new Field();
309 clearSavedSearchField .setPropertyName(CLEARSAVED_SEARCH_FIELD);
310 clearSavedSearchField .setPropertyValue(superUserSearch ? "YES" : "NO");
311 clearSavedSearchField .setFieldType(Field.HIDDEN);
312
313 List<Field> hiddenFields = new ArrayList<Field>();
314 hiddenFields.add(detailedField);
315 hiddenFields.add(superUserSearchField);
316 hiddenFields.add(clearSavedSearchField);
317 hiddenRow.setFields(hiddenFields);
318 rows.add(hiddenRow);
319
320 }
321
322 }