View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This class adapts the RemotableAttributeField instances from the various attributes
42   * associated with a document type and combines with the "default" rows for the search,
43   * returning the final List of Row objects to render for the document search.
44   *
45   * <p>Implementation note:</p>
46   * <p>
47   * This implementation relies on applicationDocumentStatus, and dateApplicationDocumentStatusChanged conditional fields
48   * being defined in the DD for basic display purposes.  These fields are conditionally shown depending on whether
49   * a document supporting application document statuses has been specified.  Further, the applicationDocumentStatus field
50   * is dynamically switched to a dropdown when the document specifies an explicit enumeration of valid statuses (this
51   * control switching is something that is not possible via declarative DD, at the time of this writing).
52   * </p>
53   * <p>
54   * In addition the routeNodeName field is dynamically populated with the list of route nodes for the specified document
55   * type.
56   * <p>
57   * Note: an alternative to programmatically providing dynamic select values is to define a value finder declaratively in
58   * DD.  KeyValueFinder however does not have access to request state, including the required document type, which would mean
59   * resorting to GlobalVariables inspection.  In reluctance to add yet another dependency on this external state, the fixups
60   * are done programmatically in this class. (see {@link #applyApplicationDocumentStatusCustomizations(org.kuali.rice.kns.web.ui.Field, org.kuali.rice.kew.doctype.bo.DocumentType)},
61   * {@link #applyRouteNodeNameCustomizations(org.kuali.rice.kns.web.ui.Field, org.kuali.rice.kew.doctype.bo.DocumentType)}).
62   * </p>
63   * @author Kuali Rice Team (rice.collab@kuali.org)
64   *
65   */
66  public class DocumentSearchCriteriaProcessorKEWAdapter implements DocumentSearchCriteriaProcessor {
67      /**
68       * Name if the hidden input field containing basic/detailed search toggle state
69       */
70      public static final String ADVANCED_SEARCH_FIELD = "isAdvancedSearch";
71      /**
72       * Name if the hidden input field containing non-superuser/superuser search toggle state
73       */
74      public static final String SUPERUSER_SEARCH_FIELD = "superUserSearch";
75      /**
76       * Name if the hidden input field containing the clear saved search flag
77       */
78      public static final String CLEARSAVED_SEARCH_FIELD = "resetSavedSearch";
79  
80      /**
81       * Indicates where document attributes should be placed inside search criteria
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      * Fields that are only applicable if a document type has been specified
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      * Fields that are only applicable if application document status is in use (assumes documenttype dependency)
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      * Generates the document search form fields given the DataDictionary-defined fields, the DocumentType,
159      * and whether basic, detailed, or superuser search is being rendered.
160      * If the document type policy DOCUMENT_STATUS_POLICY is set to "app", or "both"
161      * Then display the doc search criteria fields.
162      * If the documentType.validApplicationStatuses are defined, then the criteria field is a drop down.
163      * If the validApplication statuses are NOT defined, then the criteria field is a text input.
164      * @param rowsToLoad the list of rows to update
165      * @param defaultRows the DataDictionary-derived default form rows
166      * @param fieldNames a list of field names corresponding to the fields to render according to the current document search state
167      * @param documentType the document type, if specified in the search form
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             // assuming DOCSTATUS_DEPENDENT_FIELDS are also documentType-dependent, this block is only executed when documentType is present
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             // now add all matching rows given
185             // 1) the field is doc type and doc status independent
186             // 2) the field is doc type dependent and the doctype is specified
187             // 3) the field is doc status dependent and the doctype is specified and doc status is in use
188             for (Row row : defaultRows) {
189                 boolean matched = false;
190                 // we must iterate over each field without short-circuiting to make sure to inspect the
191                 // APPLICATION_DOCUMENT_STATUS field, which needs customizations
192                 for (Field field : row.getFields()) {
193                     // dp "endsWith" here because lower bounds properties come
194                     // across like "rangeLowerBoundKeyPrefix_dateCreated"
195                     if (field.getPropertyName().equals(fieldName) || field.getPropertyName().endsWith("_" + fieldName)) {
196                         matched = true;
197                         if (APPLICATION_DOCUMENT_STATUS.equals(field.getPropertyName())) {
198                             // If Application Document Status policy is in effect for this document type,
199                             // add search attributes for document status, and transition dates.
200                             // Note: document status field is a drop down if valid statuses are defined, a text input field otherwise.
201                             applyApplicationDocumentStatusCustomizations(field, documentType);
202                             break;
203                         } else if (ROUTE_NODE_NAME.equals(field.getPropertyName())) {
204                             // populates routenodename dropdown with documenttype nodes
205                             applyRouteNodeNameCustomizations(field, documentType);
206                         }
207                     }
208                 }
209                 if (matched) {
210                     rowsToLoad.add(row);
211                 }
212             }
213         }
214     }
215 
216     /**
217      * Returns fields for the search attributes defined on the document
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                 //force the max length for now if not set
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                 // prepend all document attribute field names with "documentAttribute."
245                 field.setPropertyName(KewApiConstants.DOCUMENT_ATTRIBUTE_FIELD_PREFIX + field.getPropertyName());
246             }
247             fixedDocumentAttributeRows.add(row);
248         }
249 
250         return fixedDocumentAttributeRows;
251     }
252 
253     /**
254      * Modifies the DataDictionary-defined applicationDocumentStatus field control to reflect whether the DocumentType
255      * has specified a list of valid application document statuses (in which case a select control is rendered), or whether
256      * it is free form (in which case a text control is rendered)
257      *
258      * @param field the applicationDocumentStatus field
259      * @param documentType the document type
260      */
261     protected void applyApplicationDocumentStatusCustomizations(Field field, DocumentType documentType) {
262         List<ApplicationDocumentStatus> validStatuses = documentType.getValidApplicationStatuses();
263         if (validStatuses == null || validStatuses.size() == 0){
264             // use a text input field
265             // StandardSearchCriteriaField(String fieldKey, String propertyName, String fieldType, String datePickerKey, String labelMessageKey, String helpMessageKeyArgument, boolean hidden, String displayOnlyPropertyName, String lookupableImplServiceName, boolean lookupTypeRequired)
266             // new StandardSearchCriteriaField(DocumentSearchCriteriaProcessor.CRITERIA_KEY_APP_DOC_STATUS,"criteria.appDocStatus",StandardSearchCriteriaField.TEXT,null,null,"DocSearchApplicationDocStatus",false,null,null,false));
267             // String fieldKey DocumentSearchCriteriaProcessor.CRITERIA_KEY_APP_DOC_STATUS
268             field.setFieldType(Field.TEXT);
269         } else {
270             // dropdown
271             // String fieldKey DocumentSearchCriteriaProcessor.CRITERIA_KEY_APP_DOC_STATUS + "_VALUES"
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             //dropDown.setOptionsCollectionProperty("validApplicationStatuses");
279             //dropDown.setCollectionKeyProperty("statusName");
280             //dropDown.setCollectionLabelProperty("statusName");
281             //dropDown.setEmptyCollectionMessage("Select a document status.");
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 }