Coverage Report - org.kuali.rice.kns.datadictionary.validation.processor.CaseConstraintProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
CaseConstraintProcessor
0%
0/42
0%
0/40
7
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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.datadictionary.validation.processor;
 17  
 
 18  
 import java.util.List;
 19  
 
 20  
 import org.kuali.rice.kns.datadictionary.DataDictionaryEntry;
 21  
 import org.kuali.rice.kns.datadictionary.exception.AttributeValidationException;
 22  
 import org.kuali.rice.kns.datadictionary.validation.AttributeValueReader;
 23  
 import org.kuali.rice.kns.datadictionary.validation.DataType;
 24  
 import org.kuali.rice.kns.datadictionary.validation.DictionaryObjectAttributeValueReader;
 25  
 import org.kuali.rice.kns.datadictionary.validation.ValidationUtils;
 26  
 import org.kuali.rice.kns.datadictionary.validation.capability.Constrainable;
 27  
 import org.kuali.rice.kns.datadictionary.validation.capability.HierarchicallyConstrainable;
 28  
 import org.kuali.rice.kns.datadictionary.validation.constraint.CaseConstraint;
 29  
 import org.kuali.rice.kns.datadictionary.validation.constraint.Constraint;
 30  
 import org.kuali.rice.kns.datadictionary.validation.constraint.DataTypeConstraint;
 31  
 import org.kuali.rice.kns.datadictionary.validation.constraint.WhenConstraint;
 32  
 import org.kuali.rice.kns.datadictionary.validation.result.DictionaryValidationResult;
 33  
 import org.kuali.rice.kns.datadictionary.validation.result.ProcessorResult;
 34  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 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  0
 public class CaseConstraintProcessor extends MandatoryElementConstraintProcessor<CaseConstraint> {
 43  
 
 44  
         private static final String CONSTRAINT_NAME = "case constraint";
 45  
 
 46  
         /**
 47  
          * @see org.kuali.rice.kns.datadictionary.validation.processor.ConstraintProcessor#process(DictionaryValidationResult, Object, org.kuali.rice.kns.datadictionary.validation.capability.Constrainable, org.kuali.rice.kns.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  0
         if (null == caseConstraint) {
 54  0
             return new ProcessorResult(result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME));
 55  
         }
 56  
 
 57  0
         String operator = (ValidationUtils.hasText(caseConstraint.getOperator())) ? caseConstraint.getOperator() : "EQUALS";
 58  0
         AttributeValueReader nestedReader = (ValidationUtils.hasText(caseConstraint.getFieldPath())) ? getChildAttributeValueReader(caseConstraint.getFieldPath(), attributeValueReader) : attributeValueReader;
 59  
 
 60  
         // TODO: What happens when the field is not in the dataProvider?
 61  0
         Constrainable caseField = (null != nestedReader) ? nestedReader.getDefinition(nestedReader.getAttributeName()) : null;
 62  0
         Object fieldValue = (null != nestedReader) ? nestedReader.getValue(nestedReader.getAttributeName()) : value;
 63  0
         DataType fieldDataType = (null != caseField && caseField instanceof DataTypeConstraint) ? ((DataTypeConstraint)caseField).getDataType() : null;
 64  
 
 65  
         // Default to a string comparison
 66  0
         if (fieldDataType == null)
 67  0
                 fieldDataType = DataType.STRING;
 68  
         
 69  
         // If fieldValue is null then skip Case check
 70  0
         if (null == fieldValue) {
 71  
                 // FIXME: not sure if the definition and attribute value reader should change under this case
 72  0
             return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME), caseField, nestedReader);
 73  
         }
 74  
 
 75  
         // Extract value for field Key
 76  0
         for (WhenConstraint wc : caseConstraint.getWhenConstraint()) {
 77  
 
 78  0
                 List<Object> whenValueList = wc.getValues();
 79  
 
 80  0
                 for (Object whenValue : whenValueList) {
 81  0
                         if (ValidationUtils.compareValues(fieldValue, whenValue, fieldDataType, operator, caseConstraint.isCaseSensitive(), dateTimeService) && null != wc.getConstraint()) {
 82  0
                                 if (nestedReader != null && wc.getValuePath() != null)
 83  0
                                         nestedReader.setAttributeName(wc.getValuePath());
 84  0
                                 return new ProcessorResult(result.addSuccess(nestedReader, CONSTRAINT_NAME), null, nestedReader, wc.getConstraint());
 85  
                         }
 86  
                 }
 87  0
         }
 88  
 
 89  
         // Assuming that not finding any case constraints is equivalent to 'skipping' the constraint
 90  0
         return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME));
 91  
         }
 92  
         
 93  
         @Override 
 94  
         public String getName() {
 95  0
                 return CONSTRAINT_NAME;
 96  
         }
 97  
         
 98  
         /**
 99  
          * @see org.kuali.rice.kns.datadictionary.validation.processor.ConstraintProcessor#getConstraintType()
 100  
          */
 101  
         @Override
 102  
         public Class<? extends Constraint> getConstraintType() {
 103  0
                 return CaseConstraint.class;
 104  
         }
 105  
         
 106  
         private AttributeValueReader getChildAttributeValueReader(String key, AttributeValueReader attributeValueReader) throws AttributeValidationException {
 107  0
                 String[] lookupPathTokens = ValidationUtils.getPathTokens(key);
 108  
                 
 109  0
                 AttributeValueReader localAttributeValueReader = attributeValueReader;
 110  0
                 for(int i = 0; i < lookupPathTokens.length; i++) {
 111  0
                         for (Constrainable definition : localAttributeValueReader.getDefinitions()) {
 112  0
                                 String attributeName = definition.getName();
 113  0
                                 if (attributeName.equals(lookupPathTokens[i])) {
 114  0
                                         if(i==lookupPathTokens.length-1){
 115  0
                                                 localAttributeValueReader.setAttributeName(attributeName);
 116  0
                                                 return localAttributeValueReader;
 117  
                                         }
 118  0
                                         if (definition instanceof HierarchicallyConstrainable) {
 119  0
                                                 String childEntryName = ((HierarchicallyConstrainable)definition).getChildEntryName();
 120  0
                                                 DataDictionaryEntry entry = KNSServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDictionaryObjectEntry(childEntryName);
 121  0
                                                 Object value = attributeValueReader.getValue(attributeName);
 122  0
                                                 attributeValueReader.setAttributeName(attributeName);
 123  0
                                                 String attributePath = attributeValueReader.getPath();
 124  0
                                                 localAttributeValueReader = new DictionaryObjectAttributeValueReader(value, childEntryName, entry, attributePath);
 125  0
                                         } 
 126  
                                         break;
 127  
                                 }
 128  0
                         }
 129  
                  }
 130  0
                 return null;
 131  
         }
 132  
         
 133  
 }