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.service.impl;
017:
018: import java.util.List;
019:
020: import org.kuali.core.bo.Parameter;
021: import org.kuali.core.service.DataDictionaryService;
022: import org.kuali.core.util.GlobalVariables;
023: import org.kuali.kfs.KFSKeyConstants;
024: import org.kuali.kfs.context.SpringContext;
025: import org.kuali.kfs.service.ParameterEvaluator;
026:
027: public class ParameterEvaluatorImpl implements ParameterEvaluator {
028: private Parameter parameter;
029: private boolean constraintIsAllow;
030: private String constrainedValue;
031: private List<String> values;
032:
033: /**
034: * If the constraint is allow and the constrainedValue is in the list of allowed values specified by the parameter this will
035: * return true, and if the constraint is deny and the constrainedValue is not in the list of denied values specified by the
036: * parameter this method will return true.
037: *
038: * @return boolean indicating whether the constrained value adheres to the restriction specified by the combination of the
039: * parameter constraint and the parameter value
040: */
041: public boolean evaluationSucceeds() {
042: if (constraintIsAllow()) {
043: return values.contains(constrainedValue);
044: } else {
045: return !values.contains(constrainedValue);
046: }
047: }
048:
049: /**
050: * @see org.kuali.kfs.service.ParameterEvaluator#evaluateAndAddError(java.lang.Class businessObjectOrDocumentClass,
051: * java.lang.String constrainedPropertyName)
052: */
053: public boolean evaluateAndAddError(
054: Class businessObjectOrDocumentClass,
055: String constrainedPropertyName) {
056: return evaluateAndAddError(businessObjectOrDocumentClass,
057: constrainedPropertyName, constrainedPropertyName);
058: }
059:
060: /**
061: * This method uses the evaluationSucceeds method to evaluate the constrainedValue. If evaluation does not succeed, it adds an
062: * error to GlobalVariables.getErrorMap(). The businessObjectOrDocumentClass, nameOfConstrainedProperty and
063: * userEditablePropertyName are used to retrieve the appropriate labels from the DataDictionary.
064: *
065: * @param businessObjectOrDocumentClass
066: * @param userEditableFieldToHighlight
067: * @param nameOfconstrainedProperty
068: * @return boolean indicating whether evaluation succeeded (see evaluationSucceeds)
069: */
070: public boolean evaluateAndAddError(
071: Class businessObjectOrDocumentClass,
072: String constrainedPropertyName,
073: String userEditablePropertyName) {
074: if (!evaluationSucceeds()) {
075: GlobalVariables
076: .getErrorMap()
077: .putError(
078: userEditablePropertyName,
079: KFSKeyConstants.ERROR_DOCUMENT_INVALID_VALUE,
080: new String[] {
081: SpringContext
082: .getBean(
083: DataDictionaryService.class)
084: .getAttributeLabel(
085: businessObjectOrDocumentClass,
086: constrainedPropertyName),
087: constrainedValue,
088: toStringForMessage(),
089: constraintIsAllow() ? "allowed"
090: : "not allowed",
091: getParameterValuesForMessage(),
092: SpringContext
093: .getBean(
094: DataDictionaryService.class)
095: .getAttributeLabel(
096: businessObjectOrDocumentClass,
097: userEditablePropertyName) });
098: return false;
099: }
100: return true;
101: }
102:
103: /**
104: * @see org.kuali.kfs.service.ParameterEvaluator#constraintIsAllow()
105: */
106: public boolean constraintIsAllow() {
107: return constraintIsAllow;
108: }
109:
110: /**
111: * This method uses the List toString method and eliminates the [].
112: *
113: * @return user-friendly String representation of Parameter values
114: */
115: public String getParameterValuesForMessage() {
116: return values.toString().replace("[", "").replace("]", "");
117: }
118:
119: /**
120: * @see org.kuali.kfs.service.ParameterEvaluator#getValue()
121: */
122: public String getValue() {
123: return parameter.getParameterValue();
124: }
125:
126: public String toString() {
127: return new StringBuffer("ParameterEvaluator").append(
128: "\n\tParameter: ").append("module=").append(
129: parameter.getParameterNamespaceCode()).append(
130: ", component=").append(
131: parameter.getParameterDetailTypeCode()).append(
132: ", name=").append(parameter.getParameterName()).append(
133: ", value=").append(parameter.getParameterValue())
134: .append("\n\tConstraint Is Allow: ").append(
135: constraintIsAllow).append(
136: "\n\tConstrained Value: ").append(
137: constrainedValue).append("\n\tValues: ")
138: .append(values.toString()).toString();
139: }
140:
141: private String toStringForMessage() {
142: return new StringBuffer("parameter ").append(
143: parameter.getParameterName()).append(" (module: ")
144: .append(parameter.getParameterNamespaceCode()).append(
145: " / component: ").append(
146: parameter.getParameterDetailTypeCode()).append(
147: ")").toString();
148: }
149:
150: private String getModuleAndComponent() {
151: return parameter.getParameterNamespaceCode() + ": "
152: + parameter.getParameterDetailTypeCode();
153: }
154:
155: public void setConstrainedValue(String constrainedValue) {
156: this .constrainedValue = constrainedValue;
157: }
158:
159: public void setConstraintIsAllow(boolean constraintIsAllow) {
160: this .constraintIsAllow = constraintIsAllow;
161: }
162:
163: public void setParameter(Parameter parameter) {
164: this .parameter = parameter;
165: }
166:
167: public void setValues(List<String> values) {
168: this.values = values;
169: }
170: }
|