Coverage Report - org.kuali.rice.kew.docsearch.DocumentSearchCustomizationMediatorImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentSearchCustomizationMediatorImpl
0%
0/34
0%
0/16
3.5
 
 1  
 package org.kuali.rice.kew.docsearch;
 2  
 
 3  
 import org.aspectj.lang.reflect.FieldSignature;
 4  
 import org.kuali.rice.core.api.uif.RemotableAttributeField;
 5  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 6  
 import org.kuali.rice.kew.api.document.attribute.AttributeFields;
 7  
 import org.kuali.rice.kew.doctype.DocumentTypeAttribute;
 8  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 9  
 import org.kuali.rice.kew.framework.KewFrameworkServiceLocator;
 10  
 import org.kuali.rice.kew.framework.docsearch.DocumentSearchCustomizationService;
 11  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 12  
 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
 13  
 
 14  
 import java.util.ArrayList;
 15  
 import java.util.HashMap;
 16  
 import java.util.LinkedHashMap;
 17  
 import java.util.List;
 18  
 import java.util.Map;
 19  
 
 20  
 /**
 21  
  * Reference implementation of {@code DocumentSearchCustomizationMediator}.
 22  
  *
 23  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 24  
  */
 25  0
 public class DocumentSearchCustomizationMediatorImpl implements DocumentSearchCustomizationMediator {
 26  
 
 27  
     @Override
 28  
     public List<RemotableAttributeField> getSearchFields(DocumentType documentType) {
 29  
 
 30  0
         List<DocumentTypeAttribute> searchableAttributes = documentType.getSearchableAttributes();
 31  
 
 32  
         // This first map is used to partition our attributes by application id.  It maps an application id to the
 33  
         // list of searchable attribute names that are associated with that application id.  Note that 'null' is a
 34  
         // valid key in this map for those attributes that have no application id.
 35  0
         LinkedHashMap<String, List<String>> applicationIdToAttributeNameMap = new LinkedHashMap<String, List<String>>();
 36  
 
 37  
         // This second map is used to map the searchable attribute name to the List of RemotableAttributeFields
 38  
         // that are returned by invocations of it's getSearchFields method.  This is a LinkedHashMap because it
 39  
         // preserves the order of the keys as they entered.  This allows us to return attribute fields in the
 40  
         // proper order as defined by the order of searchable attributes on the doc type, despite the partitioning
 41  
         // of our attributes by application id.
 42  0
         LinkedHashMap<String, List<RemotableAttributeField>> orderedFieldMap = new LinkedHashMap<String, List<RemotableAttributeField>>();
 43  
 
 44  0
         for (DocumentTypeAttribute searchableAttribute : searchableAttributes) {
 45  0
             RuleAttribute ruleAttribute = searchableAttribute.getRuleAttribute();
 46  0
             String attributeName = ruleAttribute.getName();
 47  0
             String applicationId = ruleAttribute.getApplicationId();
 48  0
             if (!applicationIdToAttributeNameMap.containsKey(applicationId)) {
 49  0
                 applicationIdToAttributeNameMap.put(applicationId, new ArrayList<String>());
 50  
             }
 51  0
             applicationIdToAttributeNameMap.get(applicationId).add(attributeName);
 52  
             // reserve a spot in the field map
 53  0
             orderedFieldMap.put(attributeName, null);
 54  0
         }
 55  
 
 56  0
         for (String applicationId : applicationIdToAttributeNameMap.keySet()) {
 57  0
             DocumentSearchCustomizationService documentSearchCustomizationService = loadCustomizationService(applicationId);
 58  0
             List<String> searchableAttributeNames = applicationIdToAttributeNameMap.get(applicationId);
 59  0
             List<AttributeFields> attributeFieldsList = documentSearchCustomizationService.getSearchAttributeFields(documentType.getName(), searchableAttributeNames);
 60  0
             mergeAttributeFields(attributeFieldsList, orderedFieldMap);
 61  0
         }
 62  
 
 63  0
         return flattenOrderedFieldMap(orderedFieldMap);
 64  
         
 65  
     }
 66  
 
 67  
     protected DocumentSearchCustomizationService loadCustomizationService(String applicationId) {
 68  0
         DocumentSearchCustomizationService service = KewFrameworkServiceLocator.getDocumentSearchCustomizationService(applicationId);
 69  0
         if (service == null) {
 70  0
             throw new WorkflowRuntimeException("Failed to locate DocumentSearchCustomizationService for applicationId: " + applicationId);
 71  
         }
 72  0
         return service;
 73  
     }
 74  
 
 75  
     protected void mergeAttributeFields(List<AttributeFields> attributeFieldsList, LinkedHashMap<String, List<RemotableAttributeField>> orderedFieldMap) {
 76  0
         if (attributeFieldsList == null) {
 77  0
             return;
 78  
         }
 79  0
         for (AttributeFields attributeFields : attributeFieldsList) {
 80  0
             orderedFieldMap.put(attributeFields.getAttributeName(), attributeFields.getRemotableAttributeFields());
 81  
         }
 82  0
     }
 83  
 
 84  
     protected List<RemotableAttributeField> flattenOrderedFieldMap(LinkedHashMap<String, List<RemotableAttributeField>> orderedFieldMap) {
 85  0
         List<RemotableAttributeField> flattenedFields = new ArrayList<RemotableAttributeField>();
 86  0
         for (List<RemotableAttributeField> fields : orderedFieldMap.values()) {
 87  0
             if (fields != null) {
 88  0
                 flattenedFields.addAll(fields);
 89  
             }
 90  
         }
 91  0
         return flattenedFields;
 92  
     }
 93  
 
 94  
 }