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 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   * This class adapts the RemotableAttributeField instances from the various attributes
32   * associated with a document type and combines with the "default" rows for the search,
33   * returning the final List of Row objects to render for the document search.
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   */
38  public class DocumentSearchCriteriaProcessorKEWAdapter implements DocumentSearchCriteriaProcessor {
39      /**
40       * Name if the hidden input field containing basic/detailed search toggle state
41       */
42      public static final String ADVANCED_SEARCH_FIELD = "isAdvancedSearch";
43      /**
44       * Name if the hidden input field containing non-superuser/superuser search toggle state
45       */
46      public static final String SUPERUSER_SEARCH_FIELD = "superUserSearch";
47      /**
48       * Name if the hidden input field containing the clear saved search flag
49       */
50      public static final String CLEARSAVED_SEARCH_FIELD = "resetSavedSearch";
51  
52      /**
53       * Indicates where document attributes should be placed inside search criteria
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                         // dp "endsWith" here because lower bounds properties come
126                         // across like "rangeLowerBoundKeyPrefix_dateCreated"
127                         if (defaultField.getPropertyName().endsWith(fieldName)) {
128                             // don't show the following fields if there is no document type
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                 //force the max length for now if not set
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                 // prepend all document attribute field names with "documentAttribute."
170                 field.setPropertyName(KewApiConstants.DOCUMENT_ATTRIBUTE_FIELD_PREFIX + field.getPropertyName());
171             }
172             fixedDocumentAttributeRows.add(row);
173         }
174 
175         // TODO - KULRICE-5635 - need to add back in the building of application document status row, commented out for now because this code is weird!
176         // If Application Document Status policy is in effect for this document type,
177         // add search attributes for document status, and transition dates.
178         // Note: document status field is a drop down if valid statuses are defined,
179         //       a text input field otherwise.
180         // fixedDocumentAttributeRows.addAll( buildAppDocStatusRows(documentType) );
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 }