Coverage Report - org.kuali.rice.core.impl.parameter.ParameterEvaluatorImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ParameterEvaluatorImpl
0%
0/26
0%
0/10
1.429
 
 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.core.impl.parameter;
 17  
 
 18  
 import java.util.List;
 19  
 
 20  
 import org.kuali.rice.core.api.parameter.Parameter;
 21  
 import org.kuali.rice.core.api.parameter.ParameterEvaluator;
 22  
 import org.kuali.rice.core.util.RiceKeyConstants;
 23  
 import org.kuali.rice.krad.service.DataDictionaryService;
 24  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 25  
 import org.kuali.rice.krad.util.GlobalVariables;
 26  
 
 27  0
 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  0
                 if (constraintIsAllow()) {
 48  0
                         return values.contains(constrainedValue);
 49  
                 } else {
 50  0
                         return !values.contains(constrainedValue);
 51  
                 }
 52  
         }
 53  
 
 54  
         public boolean evaluateAndAddError(Class<? extends Object> businessObjectOrDocumentClass, String constrainedPropertyName) {
 55  0
                 return evaluateAndAddError(businessObjectOrDocumentClass, constrainedPropertyName, constrainedPropertyName);
 56  
         }
 57  
 
 58  
         /**
 59  
          * This method uses the evaluationSucceeds method to evaluate the
 60  
          * constrainedValue. If evaluation does not succeed, it adds an error to
 61  
          * GlobalVariables.getErrorMap(). The businessObjectOrDocumentClass,
 62  
          * nameOfConstrainedProperty and userEditablePropertyName are used to
 63  
          * retrieve the appropriate labels from the DataDictionary.
 64  
          * 
 65  
          * @param businessObjectOrDocumentClass
 66  
          * @return boolean indicating whether evaluation succeeded (see
 67  
          *         evaluationSucceeds)
 68  
          */
 69  
         public boolean evaluateAndAddError(Class<? extends Object> businessObjectOrDocumentClass,
 70  
                         String constrainedPropertyName, String userEditablePropertyName) {
 71  0
                 if (!evaluationSucceeds()) {
 72  0
                         GlobalVariables.getMessageMap().putError(
 73  
                                         userEditablePropertyName,
 74  
                                         constraintIsAllow() ? RiceKeyConstants.ERROR_DOCUMENT_INVALID_VALUE_ALLOWED_VALUES_PARAMETER : RiceKeyConstants.ERROR_DOCUMENT_INVALID_VALUE_DENIED_VALUES_PARAMETER,
 75  
                                         new String[] {
 76  
                                                         getDataDictionaryService().getAttributeLabel( businessObjectOrDocumentClass, constrainedPropertyName),
 77  
                                                         constrainedValue,
 78  
                                                         toStringForMessage(),
 79  
                                                         getParameterValuesForMessage(),
 80  
                                                         getDataDictionaryService().getAttributeLabel( businessObjectOrDocumentClass, userEditablePropertyName) 
 81  
                                                         } );
 82  0
                         return false;
 83  
                 }
 84  0
                 return true;
 85  
         }
 86  
 
 87  
         public boolean constraintIsAllow() {
 88  0
                 return constraintIsAllow;
 89  
         }
 90  
 
 91  
         /**
 92  
          * This method uses the List toString method and eliminates the [].
 93  
          * 
 94  
          * @return user-friendly String representation of Parameter values
 95  
          */
 96  
         public String getParameterValuesForMessage() {
 97  0
                 return values.toString().replace("[", "").replace("]", "");
 98  
         }
 99  
 
 100  
         public String getValue() {
 101  0
                 return parameter.getValue();
 102  
         }
 103  
 
 104  
         public String toString() {
 105  0
                 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  0
                 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  0
                 return parameter.getNamespaceCode() + ": " + parameter.getComponentCode();
 125  
         }
 126  
 
 127  
         public void setConstrainedValue(String constrainedValue) {
 128  0
                 this.constrainedValue = constrainedValue;
 129  0
         }
 130  
 
 131  
         public void setConstraintIsAllow(boolean constraintIsAllow) {
 132  0
                 this.constraintIsAllow = constraintIsAllow;
 133  0
         }
 134  
 
 135  
         public void setParameter(Parameter parameter) {
 136  0
                 this.parameter = parameter;
 137  0
         }
 138  
 
 139  
         public void setValues(List<String> values) {
 140  0
                 this.values = values;
 141  0
         }
 142  
 
 143  
         /**
 144  
          * @return the dataDictionaryService
 145  
          */
 146  
         protected DataDictionaryService getDataDictionaryService() {
 147  0
                 if ( dataDictionaryService == null ) {
 148  0
                         dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
 149  
                 }
 150  0
                 return dataDictionaryService;
 151  
         }
 152  
 }