Coverage Report - org.kuali.rice.kew.docsearch.DocumentLookupCriteriaBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentLookupCriteriaBuilder
0%
0/76
0%
0/54
12
 
 1  
 /*
 2  
  * Copyright 2007-2009 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.apache.commons.beanutils.PropertyUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 21  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 22  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 23  
 import org.kuali.rice.kns.util.KNSConstants;
 24  
 import org.kuali.rice.kns.web.ui.Field;
 25  
 import org.kuali.rice.kns.web.ui.Row;
 26  
 
 27  
 import java.lang.reflect.InvocationTargetException;
 28  
 import java.util.*;
 29  
 
 30  
 /**
 31  
  * Helper class Used for building a Document Search criteria for the lookup
 32  
  *
 33  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 34  
  *
 35  
  */
 36  0
 public class DocumentLookupCriteriaBuilder  {
 37  
 
 38  
         /**
 39  
          * This method populates the criteria given a map of fields from the lookup
 40  
          *
 41  
          * @param fieldsForLookup map of fields
 42  
          * @return constructed criteria
 43  
          */
 44  
         public static DocSearchCriteriaDTO populateCriteria(Map<String,String[]> fieldsForLookup) {
 45  0
             DocSearchCriteriaDTO criteria = new DocSearchCriteriaDTO();
 46  0
             Map<String,String[]> fieldsToSet = new HashMap<String,String[]>();
 47  0
                 for (String formKey : fieldsForLookup.keySet()) {
 48  0
                         if(!(formKey.equalsIgnoreCase(KNSConstants.BACK_LOCATION) ||
 49  
                            formKey.equalsIgnoreCase(KNSConstants.DOC_FORM_KEY)) && fieldsForLookup.get(formKey)!=null && fieldsForLookup.get(formKey).length!=0) {
 50  0
                                 fieldsToSet.put(formKey, fieldsForLookup.get(formKey));
 51  
                         }
 52  
                 }
 53  0
             for (String fieldToSet : fieldsToSet.keySet()) {
 54  0
                      String valueToSet = "";
 55  0
                      String[] valuesToSet = fieldsToSet.get(fieldToSet);
 56  
                      // some inputs are now multi-select
 57  0
                      if(valuesToSet.length >= 1){
 58  0
                              for(String value: valuesToSet){
 59  0
                                      valueToSet += value + ",";
 60  
                              }
 61  0
                              valueToSet = valueToSet.substring(0, valueToSet.length()-1);
 62  
                      }else{
 63  0
                              valueToSet = valuesToSet[0];
 64  
                      }
 65  
 
 66  
                         try {
 67  0
                                 PropertyUtils.setNestedProperty(criteria, fieldToSet, valueToSet);
 68  0
                         } catch (IllegalAccessException e) {
 69  0
                                 e.printStackTrace();
 70  0
                         } catch (InvocationTargetException e) {
 71  0
                                 e.printStackTrace();
 72  0
                         } catch (NoSuchMethodException e) {
 73  
                                 //ignore this
 74  
                                 //                                e.printStackTrace();
 75  0
                         }
 76  0
                 }
 77  
 
 78  
             // This will make sure that the docType is case insensitive after this point.
 79  0
             criteria.setDocTypeFullName(getValidDocumentTypeName(criteria.getDocTypeFullName()));
 80  
 
 81  0
             if(StringUtils.isNotEmpty(criteria.getDocTypeFullName())) {
 82  0
                     addSearchableAttributesToCriteria(criteria, fieldsForLookup);
 83  
             }
 84  0
                 return criteria;
 85  
         }
 86  
 
 87  
         /**
 88  
          * TODO: Chris, Should be reevaluated in whole after released for KFS
 89  
          * This method ...
 90  
          *
 91  
          * @param criteria
 92  
          * @param propertyFields
 93  
          */
 94  
         public static void addSearchableAttributesToCriteria(DocSearchCriteriaDTO criteria, Map<String,String[]> propertyFields) {
 95  0
                 if (criteria != null) {
 96  0
                         DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(criteria.getDocTypeFullName());
 97  0
                         if (docType == null) {
 98  0
                                 return;
 99  
                         }
 100  0
                         criteria.getSearchableAttributes().clear();
 101  0
                         if (!propertyFields.isEmpty()) {
 102  0
                                 Map<String, SearchAttributeCriteriaComponent> criteriaComponentsByFormKey = new HashMap<String, SearchAttributeCriteriaComponent>();
 103  0
                                 for (SearchableAttribute searchableAttribute : docType.getSearchableAttributes()) {
 104  0
                                         for (Row row : searchableAttribute.getSearchingRows(
 105  
                                                         DocSearchUtils.getDocumentSearchContext("", docType.getName(), ""))) {
 106  0
                                                 for (org.kuali.rice.kns.web.ui.Field field : row.getFields()) {
 107  0
                                                         if (field instanceof Field) {
 108  0
                                 SearchableAttributeValue searchableAttributeValue = DocSearchUtils.getSearchableAttributeValueByDataTypeString(field.getFieldDataType());
 109  0
                                                                 SearchAttributeCriteriaComponent sacc = new SearchAttributeCriteriaComponent(field.getPropertyName(), null, field.getPropertyName(), searchableAttributeValue);
 110  0
                                                                 sacc.setRangeSearch(field.isMemberOfRange());
 111  0
                                                                 sacc.setCaseSensitive(!field.isUpperCase());
 112  
 
 113  
                                                                 //FIXME: don't force this when dd changes are in, instead delete line 1 row below and uncomment one two lines below
 114  0
                                                                 sacc.setAllowInlineRange(true);
 115  
 //                                                                sacc.setAllowInlineRange(dsField.isAllowInlineRange());
 116  
 
 117  0
                                                                 sacc.setSearchInclusive(field.isInclusive());
 118  0
                                                                 sacc.setLookupableFieldType(field.getFieldType());
 119  0
                                                                 sacc.setSearchable(field.isIndexedForSearch());
 120  0
                                                                 sacc.setCanHoldMultipleValues(Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType()));
 121  0
                                                                 criteriaComponentsByFormKey.put(field.getPropertyName(), sacc);
 122  0
                                                         } else {
 123  0
                                                                 throw new RiceRuntimeException("Fields must be of type org.kuali.rice.kew.docsearch.Field");
 124  
                                                         }
 125  
                                                 }
 126  
                                         }
 127  
                                 }
 128  0
                 for (String propertyField : propertyFields.keySet())
 129  
                 {
 130  0
                     SearchAttributeCriteriaComponent sacc = (SearchAttributeCriteriaComponent) criteriaComponentsByFormKey.get(propertyField);
 131  0
                     if (sacc != null)
 132  
                     {
 133  0
                         if (sacc.getSearchableAttributeValue() == null)
 134  
                         {
 135  0
                             String errorMsg = "Searchable attribute with form field key " + sacc.getFormKey() + " does not have a valid SearchableAttributeValue";
 136  
                             //                            LOG.error("addSearchableAttributesToCriteria() " + errorMsg);
 137  0
                             throw new RuntimeException(errorMsg);
 138  
                         }
 139  0
                         String[] values = propertyFields.get(propertyField);
 140  0
                         if (Field.MULTI_VALUE_FIELD_TYPES.contains(sacc.getLookupableFieldType()))
 141  
                         {
 142  
                             // set the multivalue lookup indicator
 143  0
                             sacc.setCanHoldMultipleValues(true);
 144  0
                             if (propertyField == null)
 145  
                             {
 146  0
                                 sacc.setValues(new ArrayList<String>());
 147  
                             } else
 148  
                             {
 149  0
                                 if (values != null)
 150  
                                 {
 151  0
                                     sacc.setValues(Arrays.asList(values));
 152  
                                 }
 153  
                             }
 154  
                         } else
 155  
                         {
 156  0
                             sacc.setValue(values[0]);
 157  
                         }
 158  0
                         criteria.addSearchableAttribute(sacc);
 159  
                     }
 160  0
                 }
 161  
                         }
 162  
                 }
 163  0
         }
 164  
 
 165  
         private static String getValidDocumentTypeName(String docTypeName) {
 166  0
                 if (StringUtils.isNotEmpty(docTypeName)) {
 167  0
                         DocumentType dTypeCriteria = new DocumentType();
 168  0
                     dTypeCriteria.setName(docTypeName.trim());
 169  0
                     dTypeCriteria.setActive(true);
 170  0
                     Collection<DocumentType> docTypeList = KEWServiceLocator.getDocumentTypeService().find(dTypeCriteria, null, false);
 171  
                     
 172  
                     // Return the valid doc type.
 173  0
                     if(docTypeList != null){
 174  0
                             for(DocumentType dType: docTypeList){
 175  0
                                 if (StringUtils.equals(docTypeName.toUpperCase(), dType.getName().toUpperCase())) {
 176  0
                                     return dType.getName();
 177  
                                 }
 178  
                             }
 179  
                     }
 180  
                 }
 181  0
                 return docTypeName;
 182  
         } 
 183  
 }