Coverage Report - org.kuali.student.lum.course.service.utils.ExpenditurePercentValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpenditurePercentValidator
86%
20/23
64%
9/14
6
 
 1  
 package org.kuali.student.lum.course.service.utils;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.List;
 5  
 import java.util.Stack;
 6  
 
 7  
 import org.kuali.student.common.dictionary.dto.FieldDefinition;
 8  
 import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition;
 9  
 import org.kuali.student.common.util.MessageUtils;
 10  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 11  
 import org.kuali.student.common.validator.BeanConstraintDataProvider;
 12  
 import org.kuali.student.common.validator.ConstraintDataProvider;
 13  
 import org.kuali.student.common.validator.DefaultValidatorImpl;
 14  
 import org.kuali.student.lum.course.dto.CourseExpenditureInfo;
 15  
 import org.kuali.student.lum.lu.dto.AffiliatedOrgInfo;
 16  
 
 17  1
 public class ExpenditurePercentValidator extends DefaultValidatorImpl {
 18  
 
 19  
     private static final String COURSE_EXPENDITURE_FIELD = "expenditure";
 20  
 
 21  
     @Override
 22  
     public List<ValidationResultInfo> validateObject(Object data, ObjectStructureDefinition objStructure) {
 23  
         // Custom validators are required to only override the other validateObject method
 24  0
         return null;
 25  
     }
 26  
 
 27  
     /***
 28  
      * @see org.kuali.student.common.validator.Validator#validateObject(org.kuali.student.common.dictionary.dto.FieldDefinition,
 29  
      *      java.lang.Object, org.kuali.student.common.dictionary.dto.ObjectStructureDefinition, java.util.Stack)
 30  
      */
 31  
     @Override
 32  
     public List<ValidationResultInfo> validateObject(FieldDefinition field, Object data, ObjectStructureDefinition objStructure, Stack<String> elementStack) {
 33  
 
 34  7
         List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>();
 35  
 
 36  
         // Check to see if the validation is called on the right field
 37  7
         if (!COURSE_EXPENDITURE_FIELD.equalsIgnoreCase(field.getName())) {
 38  0
             throw new RuntimeException("Custom Validator " + this.getClass().getName() + " was not called on the right field: expenditure");
 39  
         }
 40  
 
 41  7
         ConstraintDataProvider dataProvider = new BeanConstraintDataProvider();
 42  7
         dataProvider.initialize(data);
 43  
 
 44  
         // Get revenues data
 45  7
         Object expenditureObj = dataProvider.getValue(field.getName());
 46  
 
 47  
         // Return if no data to validate
 48  7
         if(null == expenditureObj) {
 49  6
             return results;
 50  
         }
 51  
         
 52  1
         if (!(expenditureObj instanceof CourseExpenditureInfo)) {
 53  0
             throw new RuntimeException("Custom Validator " + this.getClass().getName() + " was not called with right data: CourseExpenditureInfo");
 54  
         }
 55  
 
 56  1
         CourseExpenditureInfo courseExpInfo = (CourseExpenditureInfo) expenditureObj;
 57  
 
 58  1
         long totalOrgPercent = 0l;
 59  1
         if (courseExpInfo.getAffiliatedOrgs().size() > 0) {
 60  1
             for (AffiliatedOrgInfo org : courseExpInfo.getAffiliatedOrgs()) {
 61  2
                 totalOrgPercent += org.getPercentage();
 62  
             }
 63  
         }
 64  
 
 65  1
         if (courseExpInfo.getAffiliatedOrgs().size() > 0 && totalOrgPercent != 100l) {
 66  1
             ValidationResultInfo valRes = new ValidationResultInfo(getElementXpath(elementStack));
 67  1
             valRes.setElement("/expenditure/affiliatedOrgs");
 68  1
             valRes.setError(MessageUtils.interpolate(getMessage("validation.expenditureTotal"), toMap(field)));
 69  1
             results.add(valRes);
 70  
         }
 71  
 
 72  1
         return results;
 73  
     }
 74  
 }