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.kuali.rice.core.api.uif.RemotableAttributeField;
19 import org.kuali.rice.kew.doctype.bo.DocumentType;
20 import org.kuali.rice.kew.framework.document.search.DocumentSearchCriteriaConfiguration;
21 import org.kuali.rice.kew.service.KEWServiceLocator;
22 import org.kuali.rice.kew.api.KewApiConstants;
23 import org.kuali.rice.kns.util.FieldUtils;
24 import org.kuali.rice.kns.web.ui.Field;
25 import org.kuali.rice.kns.web.ui.Row;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30
31
32
33
34
35
36
37
38 public class DocumentSearchCriteriaProcessorKEWAdapter implements DocumentSearchCriteriaProcessor {
39
40
41
42 public static final String ADVANCED_SEARCH_FIELD = "isAdvancedSearch";
43
44
45
46 public static final String SUPERUSER_SEARCH_FIELD = "superUserSearch";
47
48
49
50 public static final String CLEARSAVED_SEARCH_FIELD = "resetSavedSearch";
51
52
53
54
55 private static final String DOCUMENT_ATTRIBUTE_FIELD_MARKER = "DOCUMENT_ATTRIBUTE_FIELD_MARKER";
56
57 private static final String APPLICATION_DOCUMENT_STATUS_CODE = "applicationDocumentStatusCode";
58 private static final String ROUTE_NODE_NAME = "routeNodeName";
59 private static final String ROUTE_NODE_LOGIC = "routeNodeLogic";
60
61 private static final String[] BASIC_FIELD_NAMES = {
62 "documentTypeName",
63 "initiatorPrincipalName",
64 "documentId",
65 "dateCreated",
66 DOCUMENT_ATTRIBUTE_FIELD_MARKER,
67 "saveName"
68 };
69
70 private static final String[] ADVANCED_FIELD_NAMES = {
71 "documentTypeName",
72 "initiatorPrincipalName",
73 "approverPrincipalName",
74 "viewerPrincipalName",
75 "groupViewerName",
76 "groupViewerId",
77 "documentId",
78 "applicationDocumentId",
79 "statusCode",
80 APPLICATION_DOCUMENT_STATUS_CODE,
81 ROUTE_NODE_NAME,
82 ROUTE_NODE_LOGIC,
83 "dateCreated",
84 "dateApproved",
85 "dateLastModified",
86 "dateFinalized",
87 "title",
88 DOCUMENT_ATTRIBUTE_FIELD_MARKER,
89 "saveName"
90 };
91
92 @Override
93 public List<Row> getRows(DocumentType documentType, List<Row> defaultRows, boolean advancedSearch, boolean superUserSearch) {
94 List<Row> rows = null;
95 if(advancedSearch) {
96 rows = loadRowsForAdvancedSearch(defaultRows, documentType);
97 } else {
98 rows = loadRowsForBasicSearch(defaultRows, documentType);
99 }
100 addHiddenFields(rows, advancedSearch, superUserSearch);
101 return rows;
102 }
103
104 protected List<Row> loadRowsForAdvancedSearch(List<Row> defaultRows, DocumentType documentType) {
105 List<Row> rows = new ArrayList<Row>();
106 loadRowsWithFields(rows, defaultRows, ADVANCED_FIELD_NAMES, documentType);
107 return rows;
108 }
109
110 protected List<Row> loadRowsForBasicSearch(List<Row> defaultRows, DocumentType documentType) {
111 List<Row> rows = new ArrayList<Row>();
112 loadRowsWithFields(rows, defaultRows, BASIC_FIELD_NAMES, documentType);
113 return rows;
114 }
115
116 protected void loadRowsWithFields(List<Row> rowsToLoad, List<Row> defaultRows, String[] fieldNames,
117 DocumentType documentType) {
118 for (String fieldName : fieldNames) {
119 if (fieldName.equals(DOCUMENT_ATTRIBUTE_FIELD_MARKER) && documentType != null) {
120 rowsToLoad.addAll(getDocumentAttributeRows(documentType));
121 }
122 else {
123 for (Row defaultRow : defaultRows) {
124 for (Field defaultField : defaultRow.getFields()) {
125
126
127 if (defaultField.getPropertyName().endsWith(fieldName)) {
128
129 if (fieldName.equals(APPLICATION_DOCUMENT_STATUS_CODE) ||
130 fieldName.equals(ROUTE_NODE_NAME) ||
131 fieldName.equals(ROUTE_NODE_LOGIC)) {
132 if (documentType == null) {
133 continue;
134 }
135 }
136 rowsToLoad.add(defaultRow);
137 }
138 }
139 }
140 }
141 }
142 }
143
144 protected List<Row> getDocumentAttributeRows(DocumentType documentType) {
145 List<Row> documentAttributeRows = new ArrayList<Row>();
146 DocumentSearchCriteriaConfiguration configuration =
147 KEWServiceLocator.getDocumentSearchCustomizationMediator().
148 getDocumentSearchCriteriaConfiguration(documentType);
149 if (configuration != null) {
150 List<RemotableAttributeField> remotableAttributeFields = configuration.getFlattenedSearchAttributeFields();
151 if (remotableAttributeFields != null && !remotableAttributeFields.isEmpty()) {
152 documentAttributeRows.addAll(FieldUtils.convertRemotableAttributeFields(remotableAttributeFields));
153 }
154 }
155 List<Row> fixedDocumentAttributeRows = new ArrayList<Row>();
156 for (Row row : documentAttributeRows) {
157 List<Field> fields = row.getFields();
158 for (Field field : fields) {
159
160 if(field.getMaxLength() == 0) {
161 field.setMaxLength(100);
162 }
163 if(field.isDatePicker() && field.isRanged()) {
164 Field newDate = FieldUtils.createRangeDateField(field);
165 List<Field> newFields = new ArrayList<Field>();
166 newFields.add(newDate);
167 fixedDocumentAttributeRows.addAll(FieldUtils.wrapFields(newFields));
168 }
169
170 field.setPropertyName(KewApiConstants.DOCUMENT_ATTRIBUTE_FIELD_PREFIX + field.getPropertyName());
171 }
172 fixedDocumentAttributeRows.add(row);
173 }
174
175
176
177
178
179
180
181
182 return fixedDocumentAttributeRows;
183 }
184
185 protected void addHiddenFields(List<Row> rows, boolean advancedSearch, boolean superUserSearch) {
186 Row hiddenRow = new Row();
187 hiddenRow.setHidden(true);
188
189 Field detailedField = new Field();
190 detailedField.setPropertyName(ADVANCED_SEARCH_FIELD);
191 detailedField.setPropertyValue(advancedSearch ? "YES" : "NO");
192 detailedField.setFieldType(Field.HIDDEN);
193
194 Field superUserSearchField = new Field();
195 superUserSearchField.setPropertyName(SUPERUSER_SEARCH_FIELD);
196 superUserSearchField.setPropertyValue(superUserSearch ? "YES" : "NO");
197 superUserSearchField.setFieldType(Field.HIDDEN);
198
199 Field clearSavedSearchField = new Field();
200 clearSavedSearchField .setPropertyName(CLEARSAVED_SEARCH_FIELD);
201 clearSavedSearchField .setPropertyValue(superUserSearch ? "YES" : "NO");
202 clearSavedSearchField .setFieldType(Field.HIDDEN);
203
204 List<Field> hiddenFields = new ArrayList<Field>();
205 hiddenFields.add(detailedField);
206 hiddenFields.add(superUserSearchField);
207 hiddenFields.add(clearSavedSearchField);
208 hiddenRow.setFields(hiddenFields);
209 rows.add(hiddenRow);
210
211 }
212
213 }