001: /*
002: * Copyright 2006-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.core.lookup;
017:
018: import java.util.Collections;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.kuali.RiceConstants;
024: import org.kuali.core.bo.BusinessObject;
025: import org.kuali.core.bo.user.UniversalUser;
026: import org.kuali.core.util.BeanPropertyComparator;
027:
028: /**
029: * This class...
030: */
031: public class KualiLookupableHelperServiceImpl extends
032: AbstractLookupableHelperServiceImpl {
033:
034: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
035: .getLogger(KualiLookupableHelperServiceImpl.class);
036: private boolean searchUsingOnlyPrimaryKeyValues = false;
037:
038: /**
039: * Uses Lookup Service to provide a basic search.
040: *
041: * @param fieldValues - Map containing prop name keys and search values
042: *
043: * @return List found business objects
044: * @see org.kuali.core.lookup.LookupableHelperService#getSearchResults(java.util.Map)
045: */
046: public List<? extends BusinessObject> getSearchResults(
047: Map<String, String> fieldValues) {
048: return getSearchResultsHelper(LookupUtils.forceUppercase(
049: getBusinessObjectClass(), fieldValues), false);
050: }
051:
052: /**
053: * Uses Lookup Service to provide a basic unbounded search.
054: *
055: * @param fieldValues - Map containing prop name keys and search values
056: *
057: * @return List found business objects
058: * @see org.kuali.core.lookup.LookupableHelperService#getSearchResultsUnbounded(java.util.Map)
059: */
060: public List<? extends BusinessObject> getSearchResultsUnbounded(
061: Map<String, String> fieldValues) {
062: return getSearchResultsHelper(LookupUtils.forceUppercase(
063: getBusinessObjectClass(), fieldValues), true);
064: }
065:
066: /**
067: *
068: * This method does the actual search, with the parameters specified, and returns the result.
069: *
070: * NOTE that it will not do any upper-casing based on the DD forceUppercase. That is handled through an external call to
071: * LookupUtils.forceUppercase().
072: *
073: * @param fieldValues A Map of the fieldNames and fieldValues to be searched on.
074: * @param unbounded Whether the results should be bounded or not to a certain max size.
075: * @return A List of search results.
076: *
077: */
078: protected List<? extends BusinessObject> getSearchResultsHelper(
079: Map<String, String> fieldValues, boolean unbounded) {
080: // remove hidden fields
081: LookupUtils.removeHiddenCriteriaFields(
082: getBusinessObjectClass(), fieldValues);
083:
084: searchUsingOnlyPrimaryKeyValues = getLookupService()
085: .allPrimaryKeyValuesPresentAndNotWildcard(
086: getBusinessObjectClass(), fieldValues);
087:
088: setBackLocation(fieldValues.get(RiceConstants.BACK_LOCATION));
089: setDocFormKey(fieldValues.get(RiceConstants.DOC_FORM_KEY));
090: setReferencesToRefresh(fieldValues
091: .get(RiceConstants.REFERENCES_TO_REFRESH));
092: List searchResults;
093: if (UniversalUser.class.equals(getBusinessObjectClass())) {
094: searchResults = (List) getUniversalUserService()
095: .findUniversalUsers(fieldValues);
096: } else if (getUniversalUserService().hasUniversalUserProperty(
097: getBusinessObjectClass(), fieldValues)) {
098: // TODO WARNING: this does not support nested joins, because i don't have a test case
099: searchResults = (List) getUniversalUserService()
100: .findWithUniversalUserJoin(
101: getBusinessObjectClass(), fieldValues,
102: unbounded);
103: } else {
104: searchResults = (List) getLookupService()
105: .findCollectionBySearchHelper(
106: getBusinessObjectClass(), fieldValues,
107: unbounded);
108: }
109: // sort list if default sort column given
110: List defaultSortColumns = getDefaultSortColumns();
111: if (defaultSortColumns.size() > 0) {
112: Collections.sort(searchResults, new BeanPropertyComparator(
113: getDefaultSortColumns(), true));
114: }
115: return searchResults;
116: }
117:
118: /**
119: * @see LookupableHelperService#isSearchUsingOnlyPrimaryKeyValues()
120: */
121: @Override
122: public boolean isSearchUsingOnlyPrimaryKeyValues() {
123: return searchUsingOnlyPrimaryKeyValues;
124: }
125:
126: /**
127: * Returns a comma delimited list of primary key field labels, to be used on the UI to tell the user which fields were used to search
128: *
129: * These labels are generated from the DD definitions for the lookup fields
130: *
131: * @return a comma separated list of field attribute names. If no fields found, returns "N/A"
132: * @see LookupableHelperService#isSearchUsingOnlyPrimaryKeyValues()
133: * @see LookupableHelperService#getPrimaryKeyFieldLabels()
134: */
135: @Override
136: public String getPrimaryKeyFieldLabels() {
137: StringBuilder buf = new StringBuilder();
138: List primaryKeyFieldNames = getPersistenceStructureService()
139: .getPrimaryKeys(getBusinessObjectClass());
140: Iterator pkIter = primaryKeyFieldNames.iterator();
141: while (pkIter.hasNext()) {
142: String pkFieldName = (String) pkIter.next();
143: buf.append(getDataDictionaryService().getAttributeLabel(
144: getBusinessObjectClass(), pkFieldName));
145: if (pkIter.hasNext()) {
146: buf.append(", ");
147: }
148: }
149: return buf.length() == 0 ? RiceConstants.NOT_AVAILABLE_STRING
150: : buf.toString();
151: }
152:
153: }
|