Coverage Report - org.kuali.rice.kew.docsearch.DocumentSearchCustomizationMediatorImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentSearchCustomizationMediatorImpl
0%
0/84
0%
0/44
4.111
 
 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.apache.commons.collections.CollectionUtils;
 19  
 import org.kuali.rice.core.api.uif.RemotableAttributeError;
 20  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 21  
 import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
 22  
 import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
 23  
 import org.kuali.rice.kew.framework.document.search.AttributeFields;
 24  
 import org.kuali.rice.kew.framework.document.search.DocumentSearchCriteriaConfiguration;
 25  
 import org.kuali.rice.kew.framework.document.search.DocumentSearchResultSetConfiguration;
 26  
 import org.kuali.rice.kew.framework.document.search.DocumentSearchResultValues;
 27  
 import org.kuali.rice.kew.doctype.DocumentTypeAttribute;
 28  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 29  
 import org.kuali.rice.kew.framework.KewFrameworkServiceLocator;
 30  
 import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomization;
 31  
 import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomizationHandlerService;
 32  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 33  
 
 34  
 import java.util.ArrayList;
 35  
 import java.util.LinkedHashMap;
 36  
 import java.util.List;
 37  
 
 38  
 /**
 39  
  * Reference implementation of {@code DocumentSearchCustomizationMediator}.
 40  
  *
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  */
 43  0
 public class DocumentSearchCustomizationMediatorImpl implements DocumentSearchCustomizationMediator {
 44  
 
 45  
     @Override
 46  
     public DocumentSearchCriteriaConfiguration getDocumentSearchCriteriaConfiguration(DocumentType documentType) {
 47  
 
 48  0
         List<DocumentTypeAttribute> searchableAttributes = documentType.getSearchableAttributes();
 49  
 
 50  
         // This first map is used to partition our attributes by application id.  It maps an application id to the
 51  
         // list of searchable attribute names that are associated with that application id.  Note that 'null' is a
 52  
         // valid key in this map for those attributes that have no application id.
 53  0
         LinkedHashMap<String, List<String>> applicationIdToAttributeNameMap = new LinkedHashMap<String, List<String>>();
 54  
 
 55  
         // This second map is used to map the searchable attribute name to the List of RemotableAttributeFields
 56  
         // that are returned by invocations of it's getSearchFields method.  This is a LinkedHashMap because it
 57  
         // preserves the order of the keys as they are entered.  This allows us to return attribute fields in the
 58  
         // proper order as defined by the order of searchable attributes on the doc type, despite the partitioning
 59  
         // of our attributes by application id.
 60  0
         LinkedHashMap<String, AttributeFields> orderedSearchFieldMap = new LinkedHashMap<String, AttributeFields>();
 61  0
         LinkedHashMap<String, AttributeFields> orderedResultSetFieldMap = new LinkedHashMap<String, AttributeFields>();
 62  
 
 63  0
         for (DocumentTypeAttribute searchableAttribute : searchableAttributes) {
 64  0
             RuleAttribute ruleAttribute = searchableAttribute.getRuleAttribute();
 65  0
             String attributeName = ruleAttribute.getName();
 66  0
             String applicationId = ruleAttribute.getApplicationId();
 67  0
             if (!applicationIdToAttributeNameMap.containsKey(applicationId)) {
 68  0
                 applicationIdToAttributeNameMap.put(applicationId, new ArrayList<String>());
 69  
             }
 70  0
             applicationIdToAttributeNameMap.get(applicationId).add(attributeName);
 71  
             // reserve a spot in the field map
 72  0
             orderedSearchFieldMap.put(attributeName, null);
 73  0
         }
 74  
 
 75  0
         for (String applicationId : applicationIdToAttributeNameMap.keySet()) {
 76  0
             DocumentSearchCustomizationHandlerService documentSearchCustomizationService = loadCustomizationService(
 77  
                     applicationId);
 78  0
             List<String> searchableAttributeNames = applicationIdToAttributeNameMap.get(applicationId);
 79  0
             DocumentSearchCriteriaConfiguration documentSearchConfiguration = documentSearchCustomizationService.getDocumentSearchConfiguration(
 80  
                     documentType.getName(), searchableAttributeNames);
 81  0
             mergeAttributeFields(documentSearchConfiguration.getSearchAttributeFields(), orderedSearchFieldMap);
 82  0
         }
 83  
 
 84  0
         DocumentSearchCriteriaConfiguration.Builder configBuilder = DocumentSearchCriteriaConfiguration.Builder.create();
 85  0
         configBuilder.setSearchAttributeFields(flattenOrderedFieldMap(orderedSearchFieldMap));
 86  0
         return configBuilder.build();
 87  
     }
 88  
 
 89  
     @Override
 90  
     public List<RemotableAttributeError> validateLookupFieldParameters(DocumentType documentType,
 91  
             DocumentSearchCriteria documentSearchCriteria) {
 92  
 
 93  0
         List<DocumentTypeAttribute> searchableAttributes = documentType.getSearchableAttributes();
 94  0
         LinkedHashMap<String, List<String>> applicationIdToAttributeNameMap = new LinkedHashMap<String, List<String>>();
 95  
 
 96  0
         for (DocumentTypeAttribute searchableAttribute : searchableAttributes) {
 97  0
             RuleAttribute ruleAttribute = searchableAttribute.getRuleAttribute();
 98  0
             String attributeName = ruleAttribute.getName();
 99  0
             String applicationId = ruleAttribute.getApplicationId();
 100  0
             if (!applicationIdToAttributeNameMap.containsKey(applicationId)) {
 101  0
                 applicationIdToAttributeNameMap.put(applicationId, new ArrayList<String>());
 102  
             }
 103  0
             applicationIdToAttributeNameMap.get(applicationId).add(attributeName);
 104  0
         }
 105  
 
 106  0
         List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>();
 107  0
         for (String applicationId : applicationIdToAttributeNameMap.keySet()) {
 108  0
             DocumentSearchCustomizationHandlerService documentSearchCustomizationService = loadCustomizationService(applicationId);
 109  0
             List<String> searchableAttributeNames = applicationIdToAttributeNameMap.get(applicationId);
 110  0
             List<RemotableAttributeError> searchErrors = documentSearchCustomizationService.validateCriteria(
 111  
                     documentSearchCriteria, searchableAttributeNames);
 112  0
             if (!CollectionUtils.isEmpty(searchErrors)) {
 113  0
                 errors.addAll(searchErrors);
 114  
             }
 115  0
         }
 116  
 
 117  0
         return errors;
 118  
     }
 119  
 
 120  
     @Override
 121  
     public DocumentSearchCriteria customizeCriteria(DocumentType documentType, DocumentSearchCriteria documentSearchCriteria) {
 122  0
         DocumentTypeAttribute customizerAttribute = documentType.getCustomizerAttribute();
 123  0
         if (customizerAttribute != null) {
 124  0
             DocumentSearchCustomizationHandlerService service = loadCustomizationService(customizerAttribute.getRuleAttribute().getApplicationId());
 125  0
             if (service.getEnabledCustomizations(documentType.getName(), customizerAttribute.getRuleAttribute().getName()).contains(
 126  
                     DocumentSearchCustomization.CRITERIA)) {
 127  0
                 DocumentSearchCriteria customizedCriteria = service.customizeCriteria(documentSearchCriteria, customizerAttribute.getRuleAttribute().getName());
 128  0
                 if (customizedCriteria != null) {
 129  0
                     return customizedCriteria;
 130  
                 }
 131  
             }
 132  
         }
 133  0
         return null;
 134  
     }
 135  
 
 136  
     @Override
 137  
     public DocumentSearchCriteria customizeClearCriteria(DocumentType documentType, DocumentSearchCriteria documentSearchCriteria) {
 138  0
         DocumentTypeAttribute customizerAttribute = documentType.getCustomizerAttribute();
 139  0
         if (customizerAttribute != null) {
 140  0
             DocumentSearchCustomizationHandlerService service = loadCustomizationService(customizerAttribute.getRuleAttribute().getApplicationId());
 141  0
             if (service.getEnabledCustomizations(documentType.getName(), customizerAttribute.getRuleAttribute().getName()).contains(
 142  
                     DocumentSearchCustomization.CLEAR_CRITERIA)) {
 143  0
                 DocumentSearchCriteria customizedCriteria = service.customizeClearCriteria(documentSearchCriteria, customizerAttribute.getRuleAttribute().getName());
 144  0
                 if (customizedCriteria != null) {
 145  0
                     return customizedCriteria;
 146  
                 }
 147  
             }
 148  
         }
 149  0
         return documentSearchCriteria;
 150  
     }
 151  
 
 152  
     @Override
 153  
     public DocumentSearchResultValues customizeResults(DocumentType documentType,
 154  
             DocumentSearchCriteria documentSearchCriteria, DocumentSearchResults results) {
 155  0
         DocumentTypeAttribute customizerAttribute = documentType.getCustomizerAttribute();
 156  0
         if (customizerAttribute != null) {
 157  0
             DocumentSearchCustomizationHandlerService service = loadCustomizationService(customizerAttribute.getRuleAttribute().getApplicationId());
 158  0
             if (service.getEnabledCustomizations(documentType.getName(), customizerAttribute.getRuleAttribute().getName()).contains(
 159  
                     DocumentSearchCustomization.RESULTS)) {
 160  0
                 DocumentSearchResultValues customizedResults = service.customizeResults(documentSearchCriteria, results.getSearchResults(), customizerAttribute.getRuleAttribute().getName());
 161  0
                 if (customizedResults != null) {
 162  0
                     return customizedResults;
 163  
                 }
 164  
             }
 165  
         }
 166  0
         return null;
 167  
     }
 168  
 
 169  
     @Override
 170  
     public DocumentSearchResultSetConfiguration customizeResultSetConfiguration(DocumentType documentType,
 171  
             DocumentSearchCriteria documentSearchCriteria) {
 172  0
         DocumentTypeAttribute customizerAttribute = documentType.getCustomizerAttribute();
 173  0
         if (customizerAttribute != null) {
 174  0
             DocumentSearchCustomizationHandlerService service = loadCustomizationService(customizerAttribute.getRuleAttribute().getApplicationId());
 175  0
             if (service.getEnabledCustomizations(documentType.getName(), customizerAttribute.getRuleAttribute().getName()).contains(
 176  
                     DocumentSearchCustomization.RESULT_SET_FIELDS)) {
 177  0
                 DocumentSearchResultSetConfiguration resultSetConfiguration = service.customizeResultSetConfiguration(
 178  
                         documentSearchCriteria, customizerAttribute.getRuleAttribute().getName());
 179  0
                 if (resultSetConfiguration != null) {
 180  0
                     return resultSetConfiguration;
 181  
                 }
 182  
             }
 183  
         }
 184  0
         return null;
 185  
     }
 186  
 
 187  
     protected DocumentSearchCustomizationHandlerService loadCustomizationService(String applicationId) {
 188  0
         DocumentSearchCustomizationHandlerService service = KewFrameworkServiceLocator.getDocumentSearchCustomizationHandlerService(
 189  
                 applicationId);
 190  0
         if (service == null) {
 191  0
             throw new WorkflowRuntimeException("Failed to locate DocumentSearchCustomizationService for applicationId: " + applicationId);
 192  
         }
 193  0
         return service;
 194  
     }
 195  
 
 196  
     protected void mergeAttributeFields(List<AttributeFields> attributeFieldsList, LinkedHashMap<String, AttributeFields> orderedFieldMap) {
 197  0
         if (attributeFieldsList == null) {
 198  0
             return;
 199  
         }
 200  0
         for (AttributeFields attributeFields : attributeFieldsList) {
 201  0
             orderedFieldMap.put(attributeFields.getAttributeName(), attributeFields);
 202  
         }
 203  0
     }
 204  
 
 205  
     protected List<AttributeFields> flattenOrderedFieldMap(LinkedHashMap<String, AttributeFields> orderedFieldMap) {
 206  0
         return new ArrayList<AttributeFields>(orderedFieldMap.values());
 207  
     }
 208  
 
 209  
 }