View Javadoc

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