View Javadoc

1   /**
2    * Copyright 2005-2012 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.apache.commons.lang.StringUtils;
20  import org.apache.log4j.Logger;
21  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
22  import org.kuali.rice.core.api.exception.RiceRemoteServiceConnectionException;
23  import org.kuali.rice.core.api.uif.RemotableAttributeError;
24  import org.kuali.rice.core.api.uif.RemotableAttributeField;
25  import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
26  import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
27  import org.kuali.rice.kew.api.extension.ExtensionDefinition;
28  import org.kuali.rice.kew.api.extension.ExtensionRepositoryService;
29  import org.kuali.rice.kew.api.extension.ExtensionUtils;
30  import org.kuali.rice.kew.framework.document.attribute.SearchableAttribute;
31  import org.kuali.rice.kew.framework.document.search.AttributeFields;
32  import org.kuali.rice.kew.framework.document.search.DocumentSearchCriteriaConfiguration;
33  import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomization;
34  import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomizationHandlerService;
35  import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomizer;
36  import org.kuali.rice.kew.framework.document.search.DocumentSearchResultSetConfiguration;
37  import org.kuali.rice.kew.framework.document.search.DocumentSearchResultValues;
38  
39  import java.util.ArrayList;
40  import java.util.Collections;
41  import java.util.HashSet;
42  import java.util.List;
43  import java.util.Set;
44  
45  /**
46   * TODO...
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  public class DocumentSearchCustomizationHandlerServiceImpl implements DocumentSearchCustomizationHandlerService {
51  
52      private static final Logger LOG = Logger.getLogger(DocumentSearchCustomizationHandlerServiceImpl.class);
53  
54      private ExtensionRepositoryService extensionRepositoryService;
55  
56      @Override
57      public DocumentSearchCriteriaConfiguration getDocumentSearchConfiguration(String documentTypeName,
58              List<String> searchableAttributeNames) {
59          if (StringUtils.isBlank(documentTypeName)) {
60              throw new RiceIllegalArgumentException("documentTypeName was null or blank");
61          }
62  
63          if (searchableAttributeNames == null) {
64              throw new RiceIllegalArgumentException("searchableAttributeNames was null");
65          }
66          DocumentSearchCriteriaConfiguration.Builder configBuilder = DocumentSearchCriteriaConfiguration.Builder.create();
67          if (CollectionUtils.isNotEmpty(searchableAttributeNames)) {
68              try {
69                  List<AttributeFields> searchAttributeFields = new ArrayList<AttributeFields>();
70                  for (String searchableAttributeName : searchableAttributeNames) {
71                      ExtensionDefinition extensionDefinition = getExtensionRepositoryService().getExtensionByName(searchableAttributeName);
72                      if (extensionDefinition == null) {
73                         throw new RiceIllegalArgumentException("Failed to locate a SearchableAttribute with the given name: " + searchableAttributeName);
74                      }
75                      SearchableAttribute searchableAttribute = loadSearchableAttribute(extensionDefinition);
76                      List<RemotableAttributeField> attributeSearchFields = searchableAttribute.getSearchFields(extensionDefinition, documentTypeName);
77                      if (CollectionUtils.isNotEmpty(attributeSearchFields)) {
78                          searchAttributeFields.add(AttributeFields.create(searchableAttributeName, attributeSearchFields));
79                      }
80                  }
81                  configBuilder.setSearchAttributeFields(searchAttributeFields);
82              } catch (RiceRemoteServiceConnectionException e) {
83                  LOG.warn("Unable to connect to load searchable attributes for document type: " + documentTypeName, e);
84              }
85          }
86          return configBuilder.build();
87      }
88  
89      @Override
90      public List<RemotableAttributeError> validateCriteria(DocumentSearchCriteria documentSearchCriteria,
91              List<String> searchableAttributeNames) {
92          if (documentSearchCriteria == null) {
93              throw new RiceIllegalArgumentException("documentSearchCriteria was null or blank");
94          }
95          if (searchableAttributeNames == null) {
96              throw new RiceIllegalArgumentException("searchableAttributeNames was null");
97          }
98          try {
99              List<RemotableAttributeError> searchFieldErrors = new ArrayList<RemotableAttributeError>();
100             for (String searchableAttributeName : searchableAttributeNames) {
101                 ExtensionDefinition extensionDefinition = getExtensionRepositoryService().getExtensionByName(searchableAttributeName);
102                 if (extensionDefinition == null) {
103                    throw new RiceIllegalArgumentException("Failed to locate a SearchableAttribute with the given name: " + searchableAttributeName);
104                 }
105                 SearchableAttribute searchableAttribute = loadSearchableAttribute(extensionDefinition);
106                 List<RemotableAttributeError> errors = searchableAttribute.validateDocumentAttributeCriteria(extensionDefinition,
107                         documentSearchCriteria);
108                 if (!CollectionUtils.isEmpty(errors)) {
109                     searchFieldErrors.addAll(errors);
110                 }
111             }
112             return Collections.unmodifiableList(searchFieldErrors);
113         } catch (RiceRemoteServiceConnectionException e) {
114             LOG.warn("Unable to connect to load searchable attributes for criteria: " + documentSearchCriteria, e);
115             return Collections.emptyList();
116         }
117     }
118 
119     @Override
120     public DocumentSearchCriteria customizeCriteria(DocumentSearchCriteria documentSearchCriteria, String customizerName) throws RiceIllegalArgumentException {
121         if (documentSearchCriteria == null) {
122             throw new RiceIllegalArgumentException("documentSearchCriteria was null");
123         }
124         if (StringUtils.isBlank(customizerName)) {
125             throw new RiceIllegalArgumentException("customizerName was null or blank");
126         }
127         DocumentSearchCustomizer customizer = loadCustomizer(customizerName);
128         return customizer.customizeCriteria(documentSearchCriteria);
129     }
130 
131     @Override
132     public DocumentSearchCriteria customizeClearCriteria(DocumentSearchCriteria documentSearchCriteria, String customizerName)
133             throws RiceIllegalArgumentException {
134         if (documentSearchCriteria == null) {
135             throw new RiceIllegalArgumentException("documentSearchCriteria was null");
136         }
137         if (StringUtils.isBlank(customizerName)) {
138             throw new RiceIllegalArgumentException("customizerName was null or blank");
139         }
140         DocumentSearchCustomizer customizer = loadCustomizer(customizerName);
141         return customizer.customizeClearCriteria(documentSearchCriteria);
142     }
143 
144     @Override
145     public DocumentSearchResultValues customizeResults(DocumentSearchCriteria documentSearchCriteria,
146             List<DocumentSearchResult> defaultResults,
147             String customizerName) throws RiceIllegalArgumentException {
148         if (documentSearchCriteria == null) {
149             throw new RiceIllegalArgumentException("documentSearchCriteria was null");
150         }
151         if (defaultResults == null) {
152             throw new RiceIllegalArgumentException("defaultResults was null");
153         }
154         if (StringUtils.isBlank(customizerName)) {
155             throw new RiceIllegalArgumentException("customizerName was null or blank");
156         }
157         DocumentSearchCustomizer customizer = loadCustomizer(customizerName);
158         return customizer.customizeResults(documentSearchCriteria, defaultResults);
159     }
160 
161     @Override
162     public DocumentSearchResultSetConfiguration customizeResultSetConfiguration(
163             DocumentSearchCriteria documentSearchCriteria, String customizerName) throws RiceIllegalArgumentException {
164         if (documentSearchCriteria == null) {
165             throw new RiceIllegalArgumentException("documentSearchCriteria was null");
166         }
167         if (StringUtils.isBlank(customizerName)) {
168             throw new RiceIllegalArgumentException("customizerName was null or blank");
169         }
170         DocumentSearchCustomizer customizer = loadCustomizer(customizerName);
171         return customizer.customizeResultSetConfiguration(documentSearchCriteria);
172     }
173 
174     @Override
175     public Set<DocumentSearchCustomization> getEnabledCustomizations(String documentTypeName, String customizerName)
176             throws RiceIllegalArgumentException {
177         if (StringUtils.isBlank(documentTypeName)) {
178             throw new RiceIllegalArgumentException("documentTypeName was null or blank");
179         }
180         if (StringUtils.isBlank(customizerName)) {
181             throw new RiceIllegalArgumentException("customizerName was null or blank");
182         }
183         DocumentSearchCustomizer customizer = loadCustomizer(customizerName);
184         Set<DocumentSearchCustomization> customizations = new HashSet<DocumentSearchCustomization>();
185         if (customizer.isCustomizeCriteriaEnabled(documentTypeName)) {
186             customizations.add(DocumentSearchCustomization.CRITERIA);
187         }
188         if (customizer.isCustomizeClearCriteriaEnabled(documentTypeName)) {
189             customizations.add(DocumentSearchCustomization.CLEAR_CRITERIA);
190         }
191         if (customizer.isCustomizeResultsEnabled(documentTypeName)) {
192             customizations.add(DocumentSearchCustomization.RESULTS);
193         }
194         if (customizer.isCustomizeResultSetFieldsEnabled(documentTypeName)) {
195             customizations.add(DocumentSearchCustomization.RESULT_SET_FIELDS);
196         }
197         return Collections.unmodifiableSet(customizations);
198     }
199 
200     private SearchableAttribute loadSearchableAttribute(ExtensionDefinition extensionDefinition) {
201         Object searchableAttribute = ExtensionUtils.loadExtension(extensionDefinition);
202         if (searchableAttribute == null) {
203             throw new RiceIllegalArgumentException("Failed to load SearchableAttribute for: " + extensionDefinition);
204         }
205         return (SearchableAttribute)searchableAttribute;
206     }
207 
208     private DocumentSearchCustomizer loadCustomizer(String customizerName) {
209         ExtensionDefinition extensionDefinition = getExtensionRepositoryService().getExtensionByName(customizerName);
210         if (extensionDefinition == null) {
211             throw new RiceIllegalArgumentException("Failed to locate a DocumentSearchCustomizer with the given name: " + customizerName);
212         }
213         DocumentSearchCustomizer customizer = ExtensionUtils.loadExtension(extensionDefinition);
214         if (customizer == null) {
215             throw new RiceIllegalArgumentException("Failed to load DocumentSearchCustomizer for: " + extensionDefinition);
216         }
217         return customizer;
218     }
219 
220     protected ExtensionRepositoryService getExtensionRepositoryService() {
221         return extensionRepositoryService;
222     }
223 
224     public void setExtensionRepositoryService(ExtensionRepositoryService extensionRepositoryService) {
225         this.extensionRepositoryService = extensionRepositoryService;
226     }
227 
228 }