001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.workflow.module.cg.docsearch;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.apache.commons.lang.ArrayUtils;
024: import org.apache.commons.lang.StringUtils;
025: import org.kuali.module.cg.lookup.keyvalues.DocumentSearchTypeOfSearchValuesFinder;
026:
027: import edu.iu.uis.eden.docsearch.DocSearchCriteriaVO;
028: import edu.iu.uis.eden.docsearch.SearchAttributeCriteriaComponent;
029: import edu.iu.uis.eden.docsearch.StandardDocumentSearchGenerator;
030: import edu.iu.uis.eden.doctype.DocumentType;
031:
032: /**
033: * This class...
034: */
035: public class KualiContractsAndGrantsDocSearchGenerator extends
036: StandardDocumentSearchGenerator {
037:
038: private static final String SEARCH_TYPE_ATTRIBUTE_FIELD_DEF_NAME = "searchType";
039:
040: /*
041: * (non-Javadoc)
042: *
043: * @see edu.iu.uis.eden.docsearch.StandardDocumentSearchGenerator#clearSearch(edu.iu.uis.eden.docsearch.DocSearchCriteriaVO)
044: */
045: @Override
046: public DocSearchCriteriaVO clearSearch(
047: DocSearchCriteriaVO searchCriteria) {
048: DocSearchCriteriaVO docSearchCriteriaVO = new DocSearchCriteriaVO();
049: docSearchCriteriaVO.setDocTypeFullName(searchCriteria
050: .getDocTypeFullName());
051: return docSearchCriteriaVO;
052: }
053:
054: /*
055: * Need to override this method because user could have selected a document type that is a proposal type but they could also
056: * then select the checkbox to "only search for award documents". To adjust for this we change the main doc type the search will
057: * use. (non-Javadoc)
058: *
059: * @see edu.iu.uis.eden.docsearch.StandardDocumentSearchGenerator#getDocTypeFullNameWhereSql(java.lang.String, java.lang.String)
060: */
061: @Override
062: protected String getDocTypeFullNameWhereSql(String docTypeFullName,
063: String whereClausePredicatePrefix) {
064: // get names from search critera attribute
065: List documentTypeNames = getDocTypeNames(SEARCH_TYPE_ATTRIBUTE_FIELD_DEF_NAME);
066: String docTypeToSearchOn = docTypeFullName;
067: boolean hasProposalDocType = false;
068: boolean hasAwardDocType = false;
069: // loop through attribute doc types to see if we have at least one award type as well as at least one proposal type
070: for (Iterator iter = documentTypeNames.iterator(); iter
071: .hasNext();) {
072: String docTypeName = (String) iter.next();
073: hasProposalDocType |= (ArrayUtils
074: .contains(
075: DocumentSearchTypeOfSearchValuesFinder.PROPOSAL_DOCUMENT_TYPE_NAMES,
076: docTypeName));
077: hasAwardDocType |= (ArrayUtils
078: .contains(
079: DocumentSearchTypeOfSearchValuesFinder.AWARD_DOCUMENT_TYPE_NAMES,
080: docTypeName));
081: if (hasAwardDocType && hasProposalDocType) {
082: break;
083: }
084: }
085: // here we check to see if search criteria says only to search proposal documents and if the given doc type to search is an
086: // award type
087: if (hasProposalDocType
088: && (!hasAwardDocType)
089: && (ArrayUtils
090: .contains(
091: DocumentSearchTypeOfSearchValuesFinder.AWARD_DOCUMENT_TYPE_NAMES,
092: docTypeFullName))) {
093: return super
094: .getDocTypeFullNameWhereSql(
095: DocumentSearchTypeOfSearchValuesFinder.PROPOSAL_DOCUMENT_TYPE_NAMES[0],
096: whereClausePredicatePrefix);
097: } // here we check to see if search criteria says only to search award documents and if the given doc type to search is a
098: // proposal type
099: else if ((!hasProposalDocType)
100: && hasAwardDocType
101: && (ArrayUtils
102: .contains(
103: DocumentSearchTypeOfSearchValuesFinder.PROPOSAL_DOCUMENT_TYPE_NAMES,
104: docTypeFullName))) {
105: return super
106: .getDocTypeFullNameWhereSql(
107: DocumentSearchTypeOfSearchValuesFinder.AWARD_DOCUMENT_TYPE_NAMES[0],
108: whereClausePredicatePrefix);
109: }
110: // here we are safe to use the given document type
111: return super .getDocTypeFullNameWhereSql(docTypeToSearchOn,
112: whereClausePredicatePrefix);
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see edu.iu.uis.eden.docsearch.StandardDocumentSearchGenerator#addExtraDocumentTypesToSearch(java.lang.StringBuffer,
119: * edu.iu.uis.eden.doctype.DocumentType)
120: */
121: @Override
122: protected void addExtraDocumentTypesToSearch(StringBuffer whereSql,
123: DocumentType docType) {
124: // static name below is from xml definition of search attribute
125: List documentTypeNames = getDocTypeNames(SEARCH_TYPE_ATTRIBUTE_FIELD_DEF_NAME);
126: for (Iterator iter = documentTypeNames.iterator(); iter
127: .hasNext();) {
128: String docTypeName = (String) iter.next();
129: if (StringUtils.isNotBlank(docTypeName)) {
130: addDocumentTypeNameToSearchOn(whereSql, docTypeName);
131: }
132: }
133: }
134:
135: private List<String> getDocTypeNames(String criteriaKeyName) {
136: List documentTypes = new ArrayList();
137: SearchAttributeCriteriaComponent criteriaComponent = getSearchableAttributeByFieldName(criteriaKeyName);
138: if (criteriaComponent != null) {
139: // we know that this particular criteria component is a multibox therefor we need to use the list of values
140: for (String value : criteriaComponent.getValues()) {
141: if (StringUtils.isNotBlank(value)) {
142: documentTypes
143: .addAll(Arrays
144: .asList(value
145: .split(DocumentSearchTypeOfSearchValuesFinder.DOCUMENT_TYPE_SEPARATOR)));
146: }
147: }
148: }
149: return documentTypes;
150: }
151:
152: }
|