001/*
002 * Copyright 2012 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 */
016package org.kuali.ole.sys.document.workflow;
017//RICE20 Hook to document type is not working right now but needs to be changed to support pre-rice2.0 release
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.lang.StringUtils;
024import org.kuali.rice.core.api.uif.RemotableAttributeError;
025import org.kuali.rice.core.api.uif.RemotableAttributeField;
026import org.kuali.rice.kew.api.document.DocumentWithContent;
027import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
028import org.kuali.rice.kew.api.document.attribute.WorkflowAttributeDefinition;
029import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
030import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
031import org.kuali.rice.kew.api.extension.ExtensionDefinition;
032import org.kuali.rice.kew.framework.document.attribute.SearchableAttribute;
033import org.kuali.rice.kew.framework.document.search.DocumentSearchCustomizer;
034import org.kuali.rice.kew.framework.document.search.DocumentSearchResultSetConfiguration;
035import org.kuali.rice.kew.framework.document.search.DocumentSearchResultValues;
036import org.kuali.rice.kew.framework.document.search.StandardResultField;
037
038public class OLEDocumentSearchCustomizer implements SearchableAttribute, DocumentSearchCustomizer {
039    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OLEDocumentSearchCustomizer.class);
040
041    protected SearchableAttribute searchableAttribute;
042
043    public OLEDocumentSearchCustomizer() {
044        this(new OLESearchableAttribute());
045    }
046
047    public OLEDocumentSearchCustomizer(SearchableAttribute searchableAttribute) {
048        this.searchableAttribute = searchableAttribute;
049    }
050
051    @Override
052    public DocumentSearchResultValues customizeResults(DocumentSearchCriteria documentSearchCriteria, List<DocumentSearchResult> defaultResults) {
053        if ( LOG.isDebugEnabled() ) {
054            LOG.debug( "customizeResults( " + documentSearchCriteria + ", " + defaultResults + " )" );
055        }
056        return null;
057    }
058
059    @Override
060    public final String generateSearchContent(ExtensionDefinition extensionDefinition,
061            String documentTypeName,
062            WorkflowAttributeDefinition attributeDefinition) {
063        if ( LOG.isDebugEnabled() ) {
064            LOG.debug( "generateSearchContent( " + extensionDefinition + ", " + documentTypeName + ", " + attributeDefinition + " )" );
065        }
066        return getSearchableAttribute().generateSearchContent(extensionDefinition, documentTypeName, attributeDefinition);
067    }
068
069    @Override
070    public final List<DocumentAttribute> extractDocumentAttributes(ExtensionDefinition extensionDefinition,
071            DocumentWithContent documentWithContent) {
072        if ( LOG.isDebugEnabled() ) {
073            LOG.debug( "extractDocumentAttributes( " + extensionDefinition + ", " + documentWithContent + " )" );
074        }
075        return getSearchableAttribute().extractDocumentAttributes(extensionDefinition, documentWithContent);
076    }
077
078    @Override
079    public final List<RemotableAttributeField> getSearchFields(ExtensionDefinition extensionDefinition,
080            String documentTypeName) {
081        if ( LOG.isDebugEnabled() ) {
082            LOG.debug( "getSearchFields( " + extensionDefinition + ", " + documentTypeName + " )" );
083        }
084        return getSearchableAttribute().getSearchFields(extensionDefinition, documentTypeName);
085    }
086
087    @Override
088    public final List<RemotableAttributeError> validateDocumentAttributeCriteria(ExtensionDefinition extensionDefinition,
089            DocumentSearchCriteria documentSearchCriteria) {
090        if ( LOG.isDebugEnabled() ) {
091            LOG.debug( "validateDocumentAttributeCriteria( " + extensionDefinition + ", " + documentSearchCriteria + " )" );
092        }
093        return getSearchableAttribute().validateDocumentAttributeCriteria(extensionDefinition, documentSearchCriteria);
094    }
095
096    protected SearchableAttribute getSearchableAttribute() {
097        return this.searchableAttribute;
098    }
099
100    public void setSearchableAttribute(SearchableAttribute searchableAttribute) {
101        this.searchableAttribute = searchableAttribute;
102    }
103
104    @Override
105    public DocumentSearchCriteria customizeCriteria(DocumentSearchCriteria documentSearchCriteria) {
106        if ( LOG.isDebugEnabled() ) {
107            LOG.debug( "customizeCriteria( " + documentSearchCriteria + " )" );
108        }
109        // since this is a result display option, we need to remove it from the criteria to prevent
110        // the query from blowing up or returning no results.
111        if ( documentSearchCriteria.getDocumentAttributeValues().containsKey( OLESearchableAttribute.DISPLAY_TYPE_SEARCH_ATTRIBUTE_NAME ) ) {
112            DocumentSearchCriteria.Builder newCriteria = DocumentSearchCriteria.Builder.create(documentSearchCriteria);
113            Map<String, List<String>> searchOptions = new HashMap<String, List<String>>();
114            searchOptions.put(OLESearchableAttribute.DISPLAY_TYPE_SEARCH_ATTRIBUTE_NAME,
115                            newCriteria.getDocumentAttributeValues().get(OLESearchableAttribute.DISPLAY_TYPE_SEARCH_ATTRIBUTE_NAME));
116            newCriteria.setSearchOptions(searchOptions);
117            newCriteria.getDocumentAttributeValues().remove( OLESearchableAttribute.DISPLAY_TYPE_SEARCH_ATTRIBUTE_NAME );
118            return newCriteria.build();
119        }
120        return null;
121    }
122
123    @Override
124    public DocumentSearchCriteria customizeClearCriteria(DocumentSearchCriteria documentSearchCriteria) {
125        if ( LOG.isDebugEnabled() ) {
126            LOG.debug( "customizeClearCriteria( " + documentSearchCriteria + " )" );
127        }
128        DocumentSearchCriteria.Builder newCriteria = DocumentSearchCriteria.Builder.create();
129        newCriteria.setDocumentTypeName(documentSearchCriteria.getDocumentTypeName());
130        return newCriteria.build();
131    }
132
133    protected static final List<StandardResultField> standardResultsToRemove = new ArrayList<StandardResultField>();
134    static {
135        standardResultsToRemove.add(StandardResultField.DOCUMENT_TYPE);
136        standardResultsToRemove.add(StandardResultField.TITLE);
137        //standardResultsToRemove.add(StandardResultField.STATUS);
138        standardResultsToRemove.add(StandardResultField.INITIATOR);
139        standardResultsToRemove.add(StandardResultField.DATE_CREATED);
140    }
141
142    @Override
143    public DocumentSearchResultSetConfiguration customizeResultSetConfiguration(DocumentSearchCriteria documentSearchCriteria) {
144        if ( LOG.isDebugEnabled() ) {
145            LOG.debug( "customizeResultSetConfiguration( " + documentSearchCriteria + " )" );
146        }
147        DocumentSearchResultSetConfiguration.Builder config = DocumentSearchResultSetConfiguration.Builder.create();
148        config.setOverrideSearchableAttributes(false);
149        config.setStandardResultFieldsToRemove(standardResultsToRemove);
150
151        List<String> displayTypeList = documentSearchCriteria.getSearchOptions().get(OLESearchableAttribute.DISPLAY_TYPE_SEARCH_ATTRIBUTE_NAME);
152        if ( displayTypeList != null && !displayTypeList.isEmpty() ) {
153
154            String displayType =  displayTypeList.get(0);
155            if ( StringUtils.equals(displayType, OLESearchableAttribute.WORKFLOW_DISPLAY_TYPE_VALUE)) {
156                config.setOverrideSearchableAttributes(true);
157                config.setStandardResultFieldsToRemove(null);
158            }
159        }
160        return config.build();
161    }
162
163    @Override
164    public boolean isCustomizeCriteriaEnabled(String documentTypeName) {
165        return true;
166    }
167
168    @Override
169    public boolean isCustomizeClearCriteriaEnabled(String documentTypeName) {
170        return true;
171    }
172
173    @Override
174    public boolean isCustomizeResultsEnabled(String documentTypeName) {
175        return false;
176    }
177
178    @Override
179    public boolean isCustomizeResultSetFieldsEnabled(String documentTypeName) {
180        return true;
181    }
182
183
184}