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.kfs.lookup;
017:
018: import java.util.Collection;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.regex.Pattern;
023: import java.util.regex.PatternSyntaxException;
024:
025: import org.apache.commons.lang.StringUtils;
026: import org.kuali.core.bo.BusinessObject;
027: import org.kuali.core.bo.Parameter;
028: import org.kuali.core.bo.ParameterDetailType;
029: import org.kuali.core.lookup.KualiLookupableHelperServiceImpl;
030: import org.kuali.core.util.ObjectUtils;
031: import org.kuali.kfs.service.ParameterService;
032:
033: /**
034: * Customizes the lookup of parameter objects to handle ParameterDetailTypes which are not in the database.
035: */
036: public class ParameterLookupableHelperServiceImpl extends
037: KualiLookupableHelperServiceImpl {
038:
039: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
040: .getLogger(ParameterLookupableHelperServiceImpl.class);
041: private ParameterService parameterService;
042:
043: @Override
044: public List<? extends BusinessObject> getSearchResults(
045: java.util.Map<String, String> fieldValues) {
046: List<? extends BusinessObject> results;
047: List<ParameterDetailType> ddDetailTypes = parameterService
048: .getNonDatabaseDetailTypes();
049: if (fieldValues
050: .containsKey("parameterDetailType.parameterDetailTypeName")
051: && !StringUtils
052: .isBlank(fieldValues
053: .get("parameterDetailType.parameterDetailTypeName"))) {
054: // perform a basic database lookup for detail types codes
055: Map<String, String> detailTypeCriteria = new HashMap<String, String>(
056: 2);
057: String parameterNamespaceCode = fieldValues
058: .get("parameterNamespaceCode");
059: String parameterDetailTypeName = fieldValues
060: .get("parameterDetailType.parameterDetailTypeName");
061: detailTypeCriteria.put("parameterNamespaceCode",
062: parameterNamespaceCode);
063: detailTypeCriteria.put("parameterDetailTypeName",
064: parameterDetailTypeName);
065: Collection<ParameterDetailType> databaseDetailTypes = getBusinessObjectService()
066: .findMatching(ParameterDetailType.class,
067: detailTypeCriteria);
068: // get all detail types from the data dictionary and filter by their name
069: StringBuffer parameterDetailTypeCodeCriteria = new StringBuffer(
070: (databaseDetailTypes.size() + ddDetailTypes.size()) * 30);
071:
072: Pattern nameRegex = null;
073: if (StringUtils.isNotBlank(parameterDetailTypeName)) {
074: String patternStr = parameterDetailTypeName.replace(
075: "*", ".*").toUpperCase();
076: try {
077: nameRegex = Pattern.compile(patternStr);
078: } catch (PatternSyntaxException ex) {
079: LOG
080: .error(
081: "Unable to parse parameterDetailTypeName pattern, ignoring.",
082: ex);
083: }
084: }
085:
086: // loop over matches from the DD, adding to a Lookup string
087: for (ParameterDetailType detailType : ddDetailTypes) {
088: if (StringUtils.isBlank(parameterNamespaceCode)
089: || detailType.getParameterNamespaceCode()
090: .equals(parameterNamespaceCode)) {
091: if (nameRegex == null
092: || (detailType.getParameterDetailTypeName() != null && nameRegex
093: .matcher(
094: detailType
095: .getParameterDetailTypeName()
096: .toUpperCase())
097: .matches())) {
098: if (parameterDetailTypeCodeCriteria.length() > 0) {
099: parameterDetailTypeCodeCriteria.append("|");
100: }
101: parameterDetailTypeCodeCriteria
102: .append(detailType
103: .getParameterDetailTypeCode());
104: }
105: }
106: }
107:
108: // loop over matches from the database, adding to a Lookup string
109: for (ParameterDetailType detailType : databaseDetailTypes) {
110: if (parameterDetailTypeCodeCriteria.length() > 0) {
111: parameterDetailTypeCodeCriteria.append("|");
112: }
113: parameterDetailTypeCodeCriteria.append(detailType
114: .getParameterDetailTypeCode());
115: }
116:
117: // remove the original parameter so it is not applied
118: fieldValues
119: .remove("parameterDetailType.parameterDetailTypeName");
120: // add the new detail type code criteria string to the lookup
121: fieldValues.put("parameterDetailTypeCode",
122: parameterDetailTypeCodeCriteria.toString());
123: results = super .getSearchResults(fieldValues);
124: } else {
125: results = super .getSearchResults(fieldValues);
126: }
127: // attach the non-database parameterDetailTypes
128: Map<String, ParameterDetailType> ddDetailTypeMap = new HashMap<String, ParameterDetailType>(
129: ddDetailTypes.size());
130: for (ParameterDetailType detailType : ddDetailTypes) {
131: ddDetailTypeMap
132: .put(detailType.getParameterDetailTypeCode(),
133: detailType);
134: }
135: for (BusinessObject obj : results) {
136: if (ObjectUtils.isNull(((Parameter) obj)
137: .getParameterDetailType())) {
138: ((Parameter) obj)
139: .setParameterDetailType(ddDetailTypeMap
140: .get(((Parameter) obj)
141: .getParameterDetailTypeCode()));
142: }
143: }
144: return results;
145: }
146:
147: public void setParameterService(ParameterService parameterService) {
148: this.parameterService = parameterService;
149: }
150: }
|