001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License"); you may not use this file except in
005: * compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.opensource.org/licenses/ecl1.php
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
010: * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
011: * language governing permissions and limitations under the License.
012: */
013: package org.kuali.core.service.impl;
014:
015: import java.util.ArrayList;
016: import java.util.Arrays;
017: import java.util.Collections;
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021:
022: import org.apache.commons.lang.StringUtils;
023: import org.kuali.RiceConstants;
024: import org.kuali.core.bo.Parameter;
025: import org.kuali.core.service.BusinessObjectService;
026: import org.kuali.core.service.KualiConfigurationService;
027: import org.kuali.rice.KNSServiceLocator;
028: import org.springframework.transaction.annotation.Transactional;
029:
030: @Transactional
031: public class KualiConfigurationServiceImpl extends
032: AbstractStaticConfigurationServiceImpl implements
033: KualiConfigurationService {
034: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
035: .getLogger(KualiConfigurationServiceImpl.class);
036:
037: public List<String> getParameterValues(Parameter parameter) {
038: if (parameter == null
039: || StringUtils.isBlank(parameter.getParameterValue())) {
040: return Collections.EMPTY_LIST;
041: }
042: return Arrays.asList(parameter.getParameterValue().split(";"));
043: }
044:
045: public boolean constraintIsAllow(Parameter parameter) {
046: return RiceConstants.APC_ALLOWED_OPERATOR.equals(parameter
047: .getParameterConstraintCode());
048: }
049:
050: public Parameter getParameter(String namespaceCode,
051: String detailTypeCode, String parameterName) {
052: if (StringUtils.isBlank(namespaceCode)
053: || StringUtils.isBlank(detailTypeCode)
054: || StringUtils.isBlank(parameterName)) {
055: throw new IllegalArgumentException(
056: "The getParameter method of KualiConfigurationServiceImpl requires a non-blank namespaceCode, parameterDetailTypeCode, and parameterName");
057: }
058: Parameter param = getParameterWithoutExceptions(namespaceCode,
059: detailTypeCode, parameterName);
060: if (param == null) {
061: throw new IllegalArgumentException(
062: "The getParameter method of KualiConfigurationServiceImpl was unable to find parameter: "
063: + namespaceCode
064: + " / "
065: + detailTypeCode
066: + " / " + parameterName);
067: }
068: return param;
069: }
070:
071: public List<Parameter> getParameters(Map<String, String> criteria) {
072: ArrayList<Parameter> parameters = new ArrayList<Parameter>();
073: parameters.addAll(getBusinessObjectService().findMatching(
074: Parameter.class, criteria));
075: return parameters;
076: }
077:
078: public boolean evaluateConstrainedValue(String namespace,
079: String detailType, String parameterName,
080: String constrainedValue) {
081: return evaluateConstrainedValue(getParameter(namespace,
082: detailType, parameterName), constrainedValue);
083: }
084:
085: public boolean evaluateConstrainedValue(Parameter parameter,
086: String constrainedValue) {
087: checkParameterArgument(parameter,
088: "evaluateConstrainedValue(Parameter parameter, String constrainedValue)");
089: if (constraintIsAllow(parameter)) {
090: return getParameterValues(parameter).contains(
091: constrainedValue);
092: } else {
093: return !getParameterValues(parameter).contains(
094: constrainedValue);
095: }
096: }
097:
098: public List<String> getParameterValues(String namespaceCode,
099: String parameterDetailTypeCode, String parameterName) {
100: return getParameterValues(getParameter(namespaceCode,
101: parameterDetailTypeCode, parameterName));
102: }
103:
104: public boolean getIndicatorParameter(String namespaceCode,
105: String parameterDetailTypeCode, String parameterName) {
106: return "Y".equals(getParameterValue(namespaceCode,
107: parameterDetailTypeCode, parameterName));
108: }
109:
110: public String getParameterValue(String namespaceCode,
111: String parameterDetailTypeCode, String parameterName) {
112: List<String> parameterValues = getParameterValues(getParameter(
113: namespaceCode, parameterDetailTypeCode, parameterName));
114: return parameterValues.isEmpty() ? "" : parameterValues
115: .iterator().next();
116: }
117:
118: public boolean parameterExists(String namespaceCode,
119: String parameterDetailTypeCode, String parameterName) {
120: return getParameterWithoutExceptions(namespaceCode,
121: parameterDetailTypeCode, parameterName) != null;
122: }
123:
124: private void checkParameterArgument(Parameter parameter,
125: String methodName) {
126: if (parameter == null) {
127: throw new IllegalArgumentException(
128: "The "
129: + methodName
130: + " method of KualiConfigurationServiceImpl requires a non-null parameter");
131: }
132: }
133:
134: private Parameter getParameterWithoutExceptions(
135: String namespaceCode, String detailTypeCode,
136: String parameterName) {
137: HashMap<String, String> crit = new HashMap<String, String>(3);
138: crit.put("parameterNamespaceCode", namespaceCode);
139: crit.put("parameterDetailTypeCode", detailTypeCode);
140: crit.put("parameterName", parameterName);
141: Parameter param = (Parameter) getBusinessObjectService()
142: .findByPrimaryKey(Parameter.class, crit);
143: return param;
144: }
145:
146: // using this instead of private variable with spring initialization because of recurring issues with circular
147: // references
148: // resulting in this error: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean
149: // with
150: // name 'businessObjectService': Bean with name 'businessObjectService' has been injected into other beans
151: // [kualiConfigurationService]
152: // in its raw version as part of a circular reference, but has eventually been wrapped (for example as part of
153: // auto-proxy creation).
154: // This means that said other beans do not use the final version of the bean. This is often the result of over-eager
155: // type matching
156: // - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
157: private BusinessObjectService getBusinessObjectService() {
158: return KNSServiceLocator.getBusinessObjectService();
159: }
160: }
|