Coverage Report - org.kuali.rice.krad.datadictionary.validation.processor.CaseConstraintProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
CaseConstraintProcessor
41%
20/48
40%
17/42
7.5
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.krad.datadictionary.validation.processor;
 17  
 
 18  
 import java.util.List;
 19  
 
 20  
 import org.kuali.rice.core.api.uif.DataType;
 21  
 import org.kuali.rice.krad.datadictionary.DataDictionaryEntry;
 22  
 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
 23  
 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
 24  
 import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
 25  
 import org.kuali.rice.krad.datadictionary.validation.ValidationUtils;
 26  
 import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
 27  
 import org.kuali.rice.krad.datadictionary.validation.capability.HierarchicallyConstrainable;
 28  
 import org.kuali.rice.krad.datadictionary.validation.constraint.CaseConstraint;
 29  
 import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
 30  
 import org.kuali.rice.krad.datadictionary.validation.constraint.DataTypeConstraint;
 31  
 import org.kuali.rice.krad.datadictionary.validation.constraint.WhenConstraint;
 32  
 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
 33  
 import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult;
 34  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 35  
 
 36  
 /**
 37  
  * This object processes 'case constraints', which are constraints that are imposed only in specific cases, for example, when a value is
 38  
  * equal to some constant, or greater than some limit. 
 39  
  * 
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org) 
 41  
  */
 42  6
 public class CaseConstraintProcessor extends MandatoryElementConstraintProcessor<CaseConstraint> {
 43  
 
 44  
         private static final String CONSTRAINT_NAME = "case constraint";
 45  
 
 46  
         /**
 47  
          * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#process(DictionaryValidationResult, Object, org.kuali.rice.krad.datadictionary.validation.capability.Constrainable, org.kuali.rice.krad.datadictionary.validation.AttributeValueReader)
 48  
          */
 49  
         @Override
 50  
         public ProcessorResult process(DictionaryValidationResult result, Object value, CaseConstraint caseConstraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
 51  
 
 52  
                 // Don't process this constraint if it's null
 53  2
         if (null == caseConstraint) {
 54  0
             return new ProcessorResult(result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME));
 55  
         }
 56  2
         AttributeValueReader constraintAttributeReader = ((DictionaryObjectAttributeValueReader)attributeValueReader).clone();
 57  
         //AttributeValueReader constraintAttributeReader = attributeValueReader;
 58  
         
 59  
         
 60  2
         String operator = (ValidationUtils.hasText(caseConstraint.getOperator())) ? caseConstraint.getOperator() : "EQUALS";
 61  2
         AttributeValueReader fieldPathReader = (ValidationUtils.hasText(caseConstraint.getPropertyName())) ? getChildAttributeValueReader(caseConstraint.getPropertyName(), attributeValueReader) : attributeValueReader;
 62  
 
 63  2
         Constrainable caseField = (null != fieldPathReader) ? fieldPathReader.getDefinition(fieldPathReader.getAttributeName()) : null;
 64  2
         Object fieldValue = (null != fieldPathReader) ? fieldPathReader.getValue(fieldPathReader.getAttributeName()) : value;
 65  2
         DataType fieldDataType = (null != caseField && caseField instanceof DataTypeConstraint) ? ((DataTypeConstraint)caseField).getDataType() : null;
 66  
 
 67  
         // Default to a string comparison
 68  2
         if (fieldDataType == null)
 69  2
                 fieldDataType = DataType.STRING;
 70  
         
 71  
         // If fieldValue is null then skip Case check
 72  2
         if (null == fieldValue) {
 73  
                 // FIXME: not sure if the definition and attribute value reader should change under this case
 74  0
             return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME), caseField, fieldPathReader);
 75  
         }
 76  
 
 77  
         // Extract value for field Key
 78  2
         for (WhenConstraint wc : caseConstraint.getWhenConstraint()) {
 79  
             
 80  2
             if (ValidationUtils.hasText(wc.getValuePath())){
 81  0
                 Object whenValue = null;
 82  0
                 AttributeValueReader whenValueReader = getChildAttributeValueReader(wc.getValuePath(), attributeValueReader);
 83  0
                 whenValue = whenValueReader.getValue(whenValueReader.getAttributeName());
 84  0
                 if (ValidationUtils.compareValues(fieldValue, whenValue, fieldDataType, operator, caseConstraint.isCaseSensitive(), dateTimeService) && null != wc.getConstraint()) {                    
 85  0
                     return new ProcessorResult(result.addSuccess(attributeValueReader, CONSTRAINT_NAME), null, constraintAttributeReader, wc.getConstraint());
 86  
                 }
 87  0
             } else {
 88  2
                     List<Object> whenValueList = wc.getValues();
 89  
     
 90  2
                     for (Object whenValue : whenValueList) {
 91  2
                             if (ValidationUtils.compareValues(fieldValue, whenValue, fieldDataType, operator, caseConstraint.isCaseSensitive(), dateTimeService) && null != wc.getConstraint()) {
 92  1
                                     return new ProcessorResult(result.addSuccess(attributeValueReader, CONSTRAINT_NAME), null, constraintAttributeReader, wc.getConstraint());
 93  
                             }
 94  
                     }
 95  1
             }
 96  
         }
 97  
 
 98  
         // Assuming that not finding any case constraints is equivalent to 'skipping' the constraint
 99  1
         return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME));
 100  
         }
 101  
         
 102  
         @Override 
 103  
         public String getName() {
 104  2
                 return CONSTRAINT_NAME;
 105  
         }
 106  
         
 107  
         /**
 108  
          * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#getConstraintType()
 109  
          */
 110  
         @Override
 111  
         public Class<? extends Constraint> getConstraintType() {
 112  0
                 return CaseConstraint.class;
 113  
         }
 114  
         
 115  
         private AttributeValueReader getChildAttributeValueReader(String key, AttributeValueReader attributeValueReader) throws AttributeValidationException {
 116  0
                 String[] lookupPathTokens = ValidationUtils.getPathTokens(key);
 117  
                 
 118  0
                 AttributeValueReader localAttributeValueReader = attributeValueReader;
 119  0
                 for(int i = 0; i < lookupPathTokens.length; i++) {
 120  0
                         for (Constrainable definition : localAttributeValueReader.getDefinitions()) {
 121  0
                                 String attributeName = definition.getName();
 122  0
                                 if (attributeName.equals(lookupPathTokens[i])) {
 123  0
                                         if(i==lookupPathTokens.length-1){
 124  0
                                                 localAttributeValueReader.setAttributeName(attributeName);
 125  0
                                                 return localAttributeValueReader;
 126  
                                         }
 127  0
                                         if (definition instanceof HierarchicallyConstrainable) {
 128  0
                                                 String childEntryName = ((HierarchicallyConstrainable)definition).getChildEntryName();
 129  0
                                                 DataDictionaryEntry entry = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDictionaryObjectEntry(childEntryName);
 130  0
                                                 Object value = attributeValueReader.getValue(attributeName);
 131  0
                                                 attributeValueReader.setAttributeName(attributeName);
 132  0
                                                 String attributePath = attributeValueReader.getPath();
 133  0
                                                 localAttributeValueReader = new DictionaryObjectAttributeValueReader(value, childEntryName, entry, attributePath);
 134  0
                                         } 
 135  
                                         break;
 136  
                                 }
 137  0
                         }
 138  
                  }
 139  0
                 return null;
 140  
         }
 141  
         
 142  
 }