001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.docsearch;
017    
018    import org.apache.commons.collections.CollectionUtils;
019    import org.apache.commons.lang.StringUtils;
020    import org.apache.log4j.Logger;
021    import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
022    import org.kuali.rice.core.api.exception.RiceRemoteServiceConnectionException;
023    import org.kuali.rice.core.api.uif.RemotableAttributeError;
024    import org.kuali.rice.core.api.uif.RemotableAttributeField;
025    import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
026    import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
027    import org.kuali.rice.kew.api.extension.ExtensionDefinition;
028    import org.kuali.rice.kew.api.extension.ExtensionRepositoryService;
029    import org.kuali.rice.kew.api.extension.ExtensionUtils;
030    import org.kuali.rice.kew.framework.document.attribute.SearchableAttribute;
031    import org.kuali.rice.kew.framework.document.search.AttributeFields;
032    import org.kuali.rice.kew.framework.document.search.DocumentSearchCriteriaConfiguration;
033    import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomization;
034    import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomizationHandlerService;
035    import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomizer;
036    import org.kuali.rice.kew.framework.document.search.DocumentSearchResultSetConfiguration;
037    import org.kuali.rice.kew.framework.document.search.DocumentSearchResultValues;
038    
039    import java.util.ArrayList;
040    import java.util.Collections;
041    import java.util.HashSet;
042    import java.util.List;
043    import java.util.Set;
044    
045    /**
046     * TODO...
047     *
048     * @author Kuali Rice Team (rice.collab@kuali.org)
049     */
050    public class DocumentSearchCustomizationHandlerServiceImpl implements DocumentSearchCustomizationHandlerService {
051    
052        private static final Logger LOG = Logger.getLogger(DocumentSearchCustomizationHandlerServiceImpl.class);
053    
054        private ExtensionRepositoryService extensionRepositoryService;
055    
056        @Override
057        public DocumentSearchCriteriaConfiguration getDocumentSearchConfiguration(String documentTypeName,
058                List<String> searchableAttributeNames) {
059            if (StringUtils.isBlank(documentTypeName)) {
060                throw new RiceIllegalArgumentException("documentTypeName was null or blank");
061            }
062    
063            if (searchableAttributeNames == null) {
064                throw new RiceIllegalArgumentException("searchableAttributeNames was null");
065            }
066            DocumentSearchCriteriaConfiguration.Builder configBuilder = DocumentSearchCriteriaConfiguration.Builder.create();
067            if (CollectionUtils.isNotEmpty(searchableAttributeNames)) {
068                try {
069                    List<AttributeFields> searchAttributeFields = new ArrayList<AttributeFields>();
070                    for (String searchableAttributeName : searchableAttributeNames) {
071                        ExtensionDefinition extensionDefinition = getExtensionRepositoryService().getExtensionByName(searchableAttributeName);
072                        if (extensionDefinition == null) {
073                           throw new RiceIllegalArgumentException("Failed to locate a SearchableAttribute with the given name: " + searchableAttributeName);
074                        }
075                        SearchableAttribute searchableAttribute = loadSearchableAttribute(extensionDefinition);
076                        List<RemotableAttributeField> attributeSearchFields = searchableAttribute.getSearchFields(extensionDefinition, documentTypeName);
077                        if (CollectionUtils.isNotEmpty(attributeSearchFields)) {
078                            searchAttributeFields.add(AttributeFields.create(searchableAttributeName, attributeSearchFields));
079                        }
080                    }
081                    configBuilder.setSearchAttributeFields(searchAttributeFields);
082                } catch (RiceRemoteServiceConnectionException e) {
083                    LOG.warn("Unable to connect to load searchable attributes for document type: " + documentTypeName, e);
084                }
085            }
086            return configBuilder.build();
087        }
088    
089        @Override
090        public List<RemotableAttributeError> validateCriteria(DocumentSearchCriteria documentSearchCriteria,
091                List<String> searchableAttributeNames) {
092            if (documentSearchCriteria == null) {
093                throw new RiceIllegalArgumentException("documentSearchCriteria was null or blank");
094            }
095            if (searchableAttributeNames == null) {
096                throw new RiceIllegalArgumentException("searchableAttributeNames was null");
097            }
098            try {
099                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(documentTypeName);
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    }