001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */ 016package org.kuali.rice.core.impl.parameter; 017 018import org.kuali.rice.coreservice.api.parameter.Parameter; 019import org.kuali.rice.core.api.parameter.ParameterEvaluator; 020import org.kuali.rice.core.api.util.RiceKeyConstants; 021import org.kuali.rice.krad.service.DataDictionaryService; 022import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 023import org.kuali.rice.krad.util.GlobalVariables; 024 025import java.util.List; 026 027public class ParameterEvaluatorImpl implements ParameterEvaluator { 028 private static final long serialVersionUID = -758645169354452022L; 029 private Parameter parameter; 030 private boolean constraintIsAllow; 031 private String constrainedValue; 032 private List<String> values; 033 034 private static DataDictionaryService dataDictionaryService; 035 036 /** 037 * If the constraint is allow and the constrainedValue is in the list of 038 * allowed values specified by the parameter this will return true, and if 039 * the constraint is deny and the constrainedValue is not in the list of 040 * denied values specified by the parameter this method will return true. 041 * 042 * @return boolean indicating whether the constrained value adheres to the 043 * restriction specified by the combination of the parameter 044 * constraint and the parameter value 045 */ 046 public boolean evaluationSucceeds() { 047 if (constraintIsAllow()) { 048 return values.contains(constrainedValue); 049 } else { 050 return !values.contains(constrainedValue); 051 } 052 } 053 054 public boolean evaluateAndAddError(Class<? extends Object> businessObjectOrDocumentClass, String constrainedPropertyName) { 055 return evaluateAndAddError(businessObjectOrDocumentClass, constrainedPropertyName, constrainedPropertyName); 056 } 057 058 /** 059 * This method uses the evaluationSucceeds method to evaluate the 060 * constrainedValue. If evaluation does not succeed, it adds an error to 061 * GlobalVariables.getErrorMap(). The businessObjectOrDocumentClass, 062 * nameOfConstrainedProperty and userEditablePropertyName are used to 063 * retrieve the appropriate labels from the DataDictionary. 064 * 065 * @param businessObjectOrDocumentClass 066 * @return boolean indicating whether evaluation succeeded (see 067 * evaluationSucceeds) 068 */ 069 public boolean evaluateAndAddError(Class<? extends Object> businessObjectOrDocumentClass, 070 String constrainedPropertyName, String userEditablePropertyName) { 071 if (!evaluationSucceeds()) { 072 GlobalVariables.getMessageMap().putError( 073 userEditablePropertyName, 074 constraintIsAllow() ? RiceKeyConstants.ERROR_DOCUMENT_INVALID_VALUE_ALLOWED_VALUES_PARAMETER : RiceKeyConstants.ERROR_DOCUMENT_INVALID_VALUE_DENIED_VALUES_PARAMETER, 075 new String[] { 076 getDataDictionaryService().getAttributeLabel( businessObjectOrDocumentClass, constrainedPropertyName), 077 constrainedValue, 078 toStringForMessage(), 079 getParameterValuesForMessage(), 080 getDataDictionaryService().getAttributeLabel( businessObjectOrDocumentClass, userEditablePropertyName) 081 } ); 082 return false; 083 } 084 return true; 085 } 086 087 public boolean constraintIsAllow() { 088 return constraintIsAllow; 089 } 090 091 /** 092 * This method uses the List toString method and eliminates the []. 093 * 094 * @return user-friendly String representation of Parameter values 095 */ 096 public String getParameterValuesForMessage() { 097 return values.toString().replace("[", "").replace("]", ""); 098 } 099 100 public String getValue() { 101 return parameter.getValue(); 102 } 103 104 public String toString() { 105 return new StringBuffer("ParameterEvaluator").append("\n\tParameter: ") 106 .append("module=").append(parameter.getNamespaceCode()) 107 .append(", component=").append(parameter.getComponentCode()) 108 .append(", name=").append(parameter.getName()) 109 .append(", value=").append(parameter.getValue()) 110 .append("\n\tConstraint Is Allow: ").append(constraintIsAllow) 111 .append("\n\tConstrained Value: ").append(constrainedValue) 112 .append("\n\tValues: ").append(values.toString()) 113 .toString(); 114 } 115 116 private String toStringForMessage() { 117 return new StringBuffer("parameter: ").append(parameter.getName()) 118 .append(", module: ").append(parameter.getNamespaceCode()) 119 .append(", component: ").append(parameter.getComponentCode()) 120 .toString(); 121 } 122 123 public String getModuleAndComponent() { 124 return parameter.getNamespaceCode() + ": " + parameter.getComponentCode(); 125 } 126 127 public void setConstrainedValue(String constrainedValue) { 128 this.constrainedValue = constrainedValue; 129 } 130 131 public void setConstraintIsAllow(boolean constraintIsAllow) { 132 this.constraintIsAllow = constraintIsAllow; 133 } 134 135 public void setParameter(Parameter parameter) { 136 this.parameter = parameter; 137 } 138 139 public void setValues(List<String> values) { 140 this.values = values; 141 } 142 143 /** 144 * @return the dataDictionaryService 145 */ 146 protected DataDictionaryService getDataDictionaryService() { 147 if ( dataDictionaryService == null ) { 148 dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService(); 149 } 150 return dataDictionaryService; 151 } 152}