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.purap.docsearch;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import org.kuali.core.bo.user.UniversalUser;
027: import org.kuali.core.bo.user.UuId;
028: import org.kuali.core.exceptions.UserNotFoundException;
029: import org.kuali.core.service.UniversalUserService;
030: import org.kuali.kfs.context.SpringContext;
031: import org.kuali.kfs.service.ParameterService;
032: import org.kuali.kfs.service.impl.ParameterConstants;
033: import org.kuali.module.purap.PurapParameterConstants;
034:
035: import edu.iu.uis.eden.WorkflowServiceError;
036: import edu.iu.uis.eden.docsearch.DocSearchCriteriaVO;
037: import edu.iu.uis.eden.docsearch.SearchAttributeCriteriaComponent;
038: import edu.iu.uis.eden.docsearch.StandardDocumentSearchGenerator;
039: import edu.iu.uis.eden.user.WorkflowUser;
040:
041: /**
042: * This class...
043: */
044: public abstract class PurApDocumentSearchGenerator extends
045: StandardDocumentSearchGenerator {
046: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
047: .getLogger(PurApDocumentSearchGenerator.class);
048:
049: private Map<String, SearchAttributeCriteriaComponent> searchComponentsByFormKey;
050:
051: public Map<String, SearchAttributeCriteriaComponent> getSearchComponentsByFormKey() {
052: if (searchComponentsByFormKey.isEmpty()) {
053: this .generateSearchComponentsByFormKeyMap();
054: }
055: return searchComponentsByFormKey;
056: }
057:
058: public void setSearchComponentsByFormKey(
059: Map<String, SearchAttributeCriteriaComponent> searchComponentsByFormKey) {
060: this .searchComponentsByFormKey = searchComponentsByFormKey;
061: }
062:
063: protected Map generateSearchComponentsByFormKeyMap() {
064: Map criteriaMap = new HashMap();
065: for (Iterator iter = getCriteria().getSearchableAttributes()
066: .iterator(); iter.hasNext();) {
067: SearchAttributeCriteriaComponent component = (SearchAttributeCriteriaComponent) iter
068: .next();
069: criteriaMap.put(component.getFormKey(), component);
070: }
071: this .searchComponentsByFormKey = criteriaMap;
072: return criteriaMap;
073: }
074:
075: public abstract List<String> getSpecificSearchCriteriaFormFieldNames();
076:
077: public abstract List<String> getGeneralSearchUserRequiredFormFieldNames();
078:
079: public abstract List<String> getSearchAttributeFormFieldNamesToIgnore();
080:
081: public abstract String getErrorMessageForNonSpecialUserInvalidCriteria();
082:
083: public boolean isSpecialAccessSearchUser(WorkflowUser workflowUser) {
084: String uuid = workflowUser.getUuId().getUuId();
085: try {
086: String searchSpecialAccess = getSpecialAccessSearchUserWorkgroupName();
087: UniversalUser currentUser = SpringContext.getBean(
088: UniversalUserService.class).getUniversalUser(
089: new UuId(uuid));
090: return currentUser.isMember(searchSpecialAccess);
091: } catch (UserNotFoundException e) {
092: String errorMessage = "Error attempting to find user with UUID '"
093: + uuid + "'";
094: LOG.error(errorMessage, e);
095: throw new RuntimeException(errorMessage, e);
096: }
097: }
098:
099: public String getSpecialAccessSearchUserWorkgroupName() {
100: return SpringContext
101: .getBean(ParameterService.class)
102: .getParameterValue(
103: ParameterConstants.PURCHASING_DOCUMENT.class,
104: PurapParameterConstants.Workgroups.SEARCH_SPECIAL_ACCESS);
105: }
106:
107: /**
108: * @see edu.iu.uis.eden.docsearch.StandardDocumentSearchGenerator#clearSearch(edu.iu.uis.eden.docsearch.DocSearchCriteriaVO)
109: */
110: @Override
111: public DocSearchCriteriaVO clearSearch(
112: DocSearchCriteriaVO searchCriteria) {
113: DocSearchCriteriaVO docSearchCriteriaVO = new DocSearchCriteriaVO();
114: docSearchCriteriaVO.setDocTypeFullName(searchCriteria
115: .getDocTypeFullName());
116: return docSearchCriteriaVO;
117: }
118:
119: /**
120: * @see edu.iu.uis.eden.docsearch.StandardDocumentSearchGenerator#performPreSearchConditions(edu.iu.uis.eden.user.WorkflowUser,
121: * edu.iu.uis.eden.docsearch.DocSearchCriteriaVO)
122: */
123: @Override
124: public List<WorkflowServiceError> performPreSearchConditions(
125: WorkflowUser user, DocSearchCriteriaVO searchCriteria) {
126: this .setCriteria(searchCriteria);
127: this .generateSearchComponentsByFormKeyMap();
128: List<WorkflowServiceError> errors = super
129: .performPreSearchConditions(user, searchCriteria);
130: if (isStandardCriteriaConsideredEmpty()
131: && isSearchAttributeCriteriaConsideredEmpty()) {
132: // error out for empty criteria
133: addErrorMessageToList(
134: errors,
135: "The search criteria entered is not sufficient to search for documents of this type.");
136: }
137: /*
138: * Error out if all of following are true: 1) standard search criteria are empty 2) search criteria does not contain at
139: * least one 'specific criteria element' (ie: doc id or purap id) 3) user is not special-access user as defined by each
140: * document 4) criteria is missing one or more required criteria elements of a non-special-access user search
141: */
142: else if ((isStandardCriteriaConsideredEmpty())
143: && (!isSpecialAccessSearchUser(user))
144: && (!containsAllGeneralSearchUserRequiredFields())
145: && (!containsOneOrMoreSpecificSearchCriteriaFields())) {
146: // error out for non special user with invalid criteria
147: addErrorMessageToList(errors,
148: getErrorMessageForNonSpecialUserInvalidCriteria());
149: }
150: return errors;
151: }
152:
153: protected boolean isSearchAttributeCriteriaConsideredEmpty() {
154: Set<String> formFieldNamesToIgnore = new HashSet(
155: getSearchAttributeFormFieldNamesToIgnore());
156: List<String> formKeyFieldsToUse = new ArrayList<String>();
157: for (Iterator iter = getSearchComponentsByFormKey().keySet()
158: .iterator(); iter.hasNext();) {
159: String formKey = (String) iter.next();
160: if (formFieldNamesToIgnore.contains(formKey)) {
161: continue;
162: }
163: formKeyFieldsToUse.add(formKey);
164: }
165: // returns true if it finds a non-blank attribute with one of the given form key field names
166: return !this .containsSearchCriteriaForListFormFieldNames(
167: formKeyFieldsToUse, false);
168: }
169:
170: protected boolean isStandardCriteriaConsideredEmpty() {
171: return getCriteria().isStandardCriteriaConsideredEmpty(true);
172: }
173:
174: protected boolean containsAllGeneralSearchUserRequiredFields() {
175: return this .containsSearchCriteriaForListFormFieldNames(
176: getGeneralSearchUserRequiredFormFieldNames(), true);
177: }
178:
179: public boolean containsOneOrMoreSpecificSearchCriteriaFields() {
180: return this .containsSearchCriteriaForListFormFieldNames(
181: getSpecificSearchCriteriaFormFieldNames(), false);
182: }
183:
184: private boolean containsSearchCriteriaForListFormFieldNames(
185: List<String> listOfFormFieldNames, boolean checkForAllInList) {
186: for (String formKey : listOfFormFieldNames) {
187: SearchAttributeCriteriaComponent component = getSearchComponentsByFormKey()
188: .get(formKey);
189: if (component == null) {
190: throw new RuntimeException(
191: "Criteria Component for search using form key '"
192: + formKey
193: + "' cannot be found in search criteria searchable attributes");
194: }
195: if (component.isNonBlankValueGiven()) {
196: if (!checkForAllInList) {
197: // checking for at least one element of list that is filled in and we found one
198: return true;
199: }
200: } else {
201: if (checkForAllInList) {
202: // checking to make sure all list elements are filled in and at least one is not
203: return false;
204: }
205: }
206: }
207: if (checkForAllInList) {
208: // checking to make sure all list elements are filled in and they are
209: return true;
210: } else {
211: // checking for at least one element of list that is filled in and we found none
212: return false;
213: }
214: }
215:
216: }
|