Coverage Report - org.kuali.rice.kew.docsearch.DocumentSearchCriteriaProcessorKEWAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentSearchCriteriaProcessorKEWAdapter
0%
0/68
0%
0/44
4.667
 
 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  0
 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  0
     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  0
     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  0
         List<Row> rows = null;
 95  0
         if(advancedSearch) {
 96  0
             rows = loadRowsForAdvancedSearch(defaultRows, documentType);
 97  
         } else {
 98  0
             rows = loadRowsForBasicSearch(defaultRows, documentType);
 99  
         }
 100  0
         addHiddenFields(rows, advancedSearch, superUserSearch);
 101  0
         return rows;
 102  
     }
 103  
 
 104  
     protected List<Row> loadRowsForAdvancedSearch(List<Row> defaultRows, DocumentType documentType) {
 105  0
         List<Row> rows = new ArrayList<Row>();
 106  0
         loadRowsWithFields(rows, defaultRows, ADVANCED_FIELD_NAMES, documentType);
 107  0
         return rows;
 108  
     }
 109  
 
 110  
     protected List<Row> loadRowsForBasicSearch(List<Row> defaultRows, DocumentType documentType) {
 111  0
         List<Row> rows = new ArrayList<Row>();
 112  0
         loadRowsWithFields(rows, defaultRows, BASIC_FIELD_NAMES, documentType);
 113  0
         return rows;
 114  
     }
 115  
 
 116  
     protected void loadRowsWithFields(List<Row> rowsToLoad, List<Row> defaultRows, String[] fieldNames,
 117  
             DocumentType documentType) {
 118  0
         for (String fieldName : fieldNames) {
 119  0
             if (fieldName.equals(DOCUMENT_ATTRIBUTE_FIELD_MARKER) && documentType != null) {
 120  0
                 rowsToLoad.addAll(getDocumentAttributeRows(documentType));
 121  
             }
 122  
             else {
 123  0
                 for (Row defaultRow : defaultRows) {
 124  0
                     for (Field defaultField : defaultRow.getFields()) {
 125  
                         // dp "endsWith" here because lower bounds properties come
 126  
                         // across like "rangeLowerBoundKeyPrefix_dateCreated"
 127  0
                         if (defaultField.getPropertyName().endsWith(fieldName)) {
 128  
                             // don't show the following fields if there is no document type
 129  0
                             if (fieldName.equals(APPLICATION_DOCUMENT_STATUS_CODE) ||
 130  
                                     fieldName.equals(ROUTE_NODE_NAME) ||
 131  
                                     fieldName.equals(ROUTE_NODE_LOGIC)) {
 132  0
                                 if (documentType == null) {
 133  0
                                     continue;
 134  
                                 }
 135  
                             }
 136  0
                             rowsToLoad.add(defaultRow);
 137  
                         }
 138  
                     }
 139  
                 }
 140  
             }
 141  
         }
 142  0
     }
 143  
 
 144  
     protected List<Row> getDocumentAttributeRows(DocumentType documentType) {
 145  0
         List<Row> documentAttributeRows = new ArrayList<Row>();
 146  0
         DocumentSearchCriteriaConfiguration configuration =
 147  
                 KEWServiceLocator.getDocumentSearchCustomizationMediator().
 148  
                         getDocumentSearchCriteriaConfiguration(documentType);
 149  0
         if (configuration != null) {
 150  0
             List<RemotableAttributeField> remotableAttributeFields = configuration.getFlattenedSearchAttributeFields();
 151  0
             if (remotableAttributeFields != null && !remotableAttributeFields.isEmpty()) {
 152  0
                 documentAttributeRows.addAll(FieldUtils.convertRemotableAttributeFields(remotableAttributeFields));
 153  
             }
 154  
         }
 155  0
         List<Row> fixedDocumentAttributeRows = new ArrayList<Row>();
 156  0
         for (Row row : documentAttributeRows) {
 157  0
             List<Field> fields = row.getFields();
 158  0
             for (Field field : fields) {
 159  
                 //force the max length for now if not set
 160  0
                 if(field.getMaxLength() == 0) {
 161  0
                     field.setMaxLength(100);
 162  
                 }
 163  0
                 if(field.isDatePicker() && field.isRanged()) {
 164  0
                     Field newDate = FieldUtils.createRangeDateField(field);
 165  0
                     List<Field> newFields = new ArrayList<Field>();
 166  0
                     newFields.add(newDate);
 167  0
                     fixedDocumentAttributeRows.addAll(FieldUtils.wrapFields(newFields));
 168  
                 }
 169  
                 // prepend all document attribute field names with "documentAttribute."
 170  0
                 field.setPropertyName(KewApiConstants.DOCUMENT_ATTRIBUTE_FIELD_PREFIX + field.getPropertyName());
 171  
             }
 172  0
             fixedDocumentAttributeRows.add(row);
 173  0
         }
 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  0
         return fixedDocumentAttributeRows;
 183  
     }
 184  
 
 185  
     protected void addHiddenFields(List<Row> rows, boolean advancedSearch, boolean superUserSearch) {
 186  0
         Row hiddenRow = new Row();
 187  0
         hiddenRow.setHidden(true);
 188  
 
 189  0
         Field detailedField = new Field();
 190  0
         detailedField.setPropertyName(ADVANCED_SEARCH_FIELD);
 191  0
         detailedField.setPropertyValue(advancedSearch ? "YES" : "NO");
 192  0
         detailedField.setFieldType(Field.HIDDEN);
 193  
 
 194  0
         Field superUserSearchField = new Field();
 195  0
         superUserSearchField.setPropertyName(SUPERUSER_SEARCH_FIELD);
 196  0
         superUserSearchField.setPropertyValue(superUserSearch ? "YES" : "NO");
 197  0
         superUserSearchField.setFieldType(Field.HIDDEN);
 198  
 
 199  0
         Field clearSavedSearchField = new Field();
 200  0
         clearSavedSearchField .setPropertyName(CLEARSAVED_SEARCH_FIELD);
 201  0
         clearSavedSearchField .setPropertyValue(superUserSearch ? "YES" : "NO");
 202  0
         clearSavedSearchField .setFieldType(Field.HIDDEN);
 203  
 
 204  0
         List<Field> hiddenFields = new ArrayList<Field>();
 205  0
         hiddenFields.add(detailedField);
 206  0
         hiddenFields.add(superUserSearchField);
 207  0
         hiddenFields.add(clearSavedSearchField);
 208  0
         hiddenRow.setFields(hiddenFields);
 209  0
         rows.add(hiddenRow);
 210  
 
 211  0
     }
 212  
 
 213  
 }