View Javadoc

1   /*
2    * Copyright 2007-2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.service.impl;
17  
18  import java.util.List;
19  
20  import org.kuali.rice.kns.bo.Parameter;
21  import org.kuali.rice.kns.service.DataDictionaryService;
22  import org.kuali.rice.kns.service.KNSServiceLocator;
23  import org.kuali.rice.kns.service.ParameterEvaluator;
24  import org.kuali.rice.kns.util.GlobalVariables;
25  import org.kuali.rice.kns.util.RiceKeyConstants;
26  
27  public class ParameterEvaluatorImpl implements ParameterEvaluator {
28  	private static final long serialVersionUID = -758645169354452022L;
29  	private Parameter parameter;
30  	private boolean constraintIsAllow;
31  	private String constrainedValue;
32  	private List<String> values;
33  
34  	private static DataDictionaryService dataDictionaryService;
35  	
36  	/**
37  	 * If the constraint is allow and the constrainedValue is in the list of
38  	 * allowed values specified by the parameter this will return true, and if
39  	 * the constraint is deny and the constrainedValue is not in the list of
40  	 * denied values specified by the parameter this method will return true.
41  	 * 
42  	 * @return boolean indicating whether the constrained value adheres to the
43  	 *         restriction specified by the combination of the parameter
44  	 *         constraint and the parameter value
45  	 */
46  	public boolean evaluationSucceeds() {
47  		if (constraintIsAllow()) {
48  			return values.contains(constrainedValue);
49  		} else {
50  			return !values.contains(constrainedValue);
51  		}
52  	}
53  
54  	/**
55  	 * @see org.kuali.kfs.sys.service.ParameterEvaluator#evaluateAndAddError(java.lang.Class
56  	 *      businessObjectOrDocumentClass, java.lang.String
57  	 *      constrainedPropertyName)
58  	 */
59  	public boolean evaluateAndAddError(Class<? extends Object> businessObjectOrDocumentClass, String constrainedPropertyName) {
60  		return evaluateAndAddError(businessObjectOrDocumentClass, constrainedPropertyName, constrainedPropertyName);
61  	}
62  
63  	/**
64  	 * This method uses the evaluationSucceeds method to evaluate the
65  	 * constrainedValue. If evaluation does not succeed, it adds an error to
66  	 * GlobalVariables.getErrorMap(). The businessObjectOrDocumentClass,
67  	 * nameOfConstrainedProperty and userEditablePropertyName are used to
68  	 * retrieve the appropriate labels from the DataDictionary.
69  	 * 
70  	 * @param businessObjectOrDocumentClass
71  	 * @param userEditableFieldToHighlight
72  	 * @param nameOfconstrainedProperty
73  	 * @return boolean indicating whether evaluation succeeded (see
74  	 *         evaluationSucceeds)
75  	 */
76  	public boolean evaluateAndAddError(Class<? extends Object> businessObjectOrDocumentClass,
77  			String constrainedPropertyName, String userEditablePropertyName) {
78  		if (!evaluationSucceeds()) {
79  			GlobalVariables.getMessageMap().putError(
80  					userEditablePropertyName,
81  					constraintIsAllow() ? RiceKeyConstants.ERROR_DOCUMENT_INVALID_VALUE_ALLOWED_VALUES_PARAMETER : RiceKeyConstants.ERROR_DOCUMENT_INVALID_VALUE_DENIED_VALUES_PARAMETER,
82  					new String[] {
83  							getDataDictionaryService().getAttributeLabel( businessObjectOrDocumentClass, constrainedPropertyName),
84  							constrainedValue,
85  							toStringForMessage(),
86  							getParameterValuesForMessage(),
87  							getDataDictionaryService().getAttributeLabel( businessObjectOrDocumentClass, userEditablePropertyName) 
88  							} );
89  			return false;
90  		}
91  		return true;
92  	}
93  
94  	/**
95  	 * @see org.kuali.kfs.sys.service.ParameterEvaluator#constraintIsAllow()
96  	 */
97  	public boolean constraintIsAllow() {
98  		return constraintIsAllow;
99  	}
100 
101 	/**
102 	 * This method uses the List toString method and eliminates the [].
103 	 * 
104 	 * @return user-friendly String representation of Parameter values
105 	 */
106 	public String getParameterValuesForMessage() {
107 		return values.toString().replace("[", "").replace("]", "");
108 	}
109 
110 	/**
111 	 * @see org.kuali.kfs.sys.service.ParameterEvaluator#getValue()
112 	 */
113 	public String getValue() {
114 		return parameter.getParameterValue();
115 	}
116 
117 	public String toString() {
118 		return new StringBuffer("ParameterEvaluator").append("\n\tParameter: ")
119 				.append("module=").append(parameter.getParameterNamespaceCode())
120 				.append(", component=").append(parameter.getParameterDetailTypeCode())
121 				.append(", name=").append(parameter.getParameterName())
122 				.append(", value=").append(parameter.getParameterValue())
123 				.append("\n\tConstraint Is Allow: ").append(constraintIsAllow)
124 				.append("\n\tConstrained Value: ").append(constrainedValue)
125 				.append("\n\tValues: ").append(values.toString())
126 				.toString();
127 	}
128 
129 	private String toStringForMessage() {
130 		return new StringBuffer("parameter: ").append(parameter.getParameterName())
131 				.append(", module: ").append(parameter.getParameterNamespaceCode())
132 				.append(", component: ").append(parameter.getParameterDetailTypeCode())
133 				.toString();
134 	}
135 
136 	public String getModuleAndComponent() {
137 		return parameter.getParameterNamespaceCode() + ": " + parameter.getParameterDetailTypeCode();
138 	}
139 
140 	public void setConstrainedValue(String constrainedValue) {
141 		this.constrainedValue = constrainedValue;
142 	}
143 
144 	public void setConstraintIsAllow(boolean constraintIsAllow) {
145 		this.constraintIsAllow = constraintIsAllow;
146 	}
147 
148 	public void setParameter(Parameter parameter) {
149 		this.parameter = parameter;
150 	}
151 
152 	public void setValues(List<String> values) {
153 		this.values = values;
154 	}
155 
156 	/**
157 	 * @return the dataDictionaryService
158 	 */
159 	protected DataDictionaryService getDataDictionaryService() {
160 		if ( dataDictionaryService == null ) {
161 			dataDictionaryService = KNSServiceLocator.getDataDictionaryService();
162 		}
163 		return dataDictionaryService;
164 	}
165 }