Clover Coverage Report - KS LUM 1.3.0-SNAPSHOT (Aggregated)
Coverage timestamp: Thu Apr 28 2011 06:51:40 EDT
../../../../../../../img/srcFileCovDistChart0.png 0% of files have more coverage
23   78   8   11.5
10   53   0.35   2
2     4  
1    
 
  RevenuePercentValidator       Line # 18 23 0% 8 35 0% 0.0
 
No Tests
 
1    package org.kuali.student.lum.course.service.utils;
2   
3    import java.util.ArrayList;
4    import java.util.Collection;
5    import java.util.List;
6    import java.util.Stack;
7   
8    import org.kuali.student.common.dictionary.dto.FieldDefinition;
9    import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition;
10    import org.kuali.student.common.util.MessageUtils;
11    import org.kuali.student.common.validation.dto.ValidationResultInfo;
12    import org.kuali.student.common.validator.BeanConstraintDataProvider;
13    import org.kuali.student.common.validator.ConstraintDataProvider;
14    import org.kuali.student.common.validator.DefaultValidatorImpl;
15    import org.kuali.student.lum.course.dto.CourseRevenueInfo;
16    import org.kuali.student.lum.lu.dto.AffiliatedOrgInfo;
17   
 
18    public class RevenuePercentValidator extends DefaultValidatorImpl {
19   
20    private static final String COURSE_REVENUE_FIELD = "revenues";
21   
 
22  0 toggle @Override
23    public List<ValidationResultInfo> validateObject(Object data, ObjectStructureDefinition objStructure) {
24    // Custom validators are required to only override the other validateObject method
25  0 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  0 toggle @Override
33    public List<ValidationResultInfo> validateObject(FieldDefinition field, Object data, ObjectStructureDefinition objStructure, Stack<String> elementStack) {
34   
35  0 List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>();
36   
37    // Check to see if the validation is called on the right field
38  0 if (!COURSE_REVENUE_FIELD.equalsIgnoreCase(field.getName())) {
39  0 throw new RuntimeException("Custom Validator " + this.getClass().getName() + " was not called on the right field: revenues");
40    }
41   
42  0 ConstraintDataProvider dataProvider = new BeanConstraintDataProvider();
43  0 dataProvider.initialize(data);
44   
45    // Get revenues data
46  0 Object revenuesObj = dataProvider.getValue(field.getName());
47   
48  0 if (!(revenuesObj instanceof Collection)) {
49  0 throw new RuntimeException("Custom Validator " + this.getClass().getName() + " was not called with right data: CourseRevenueInfo Collection");
50    }
51   
52  0 long totalOrgPercent = 0l;
53    // Sum all org percents and make sure they add up to 100
54  0 for (Object o : (Collection<?>) revenuesObj) {
55   
56  0 if (!(o instanceof CourseRevenueInfo)) {
57  0 throw new RuntimeException("Custom Validator " + this.getClass().getName() + " was not called with right data: CourseRevenueInfo");
58    }
59   
60  0 CourseRevenueInfo courseRevenue = (CourseRevenueInfo) o;
61   
62  0 if (courseRevenue.getAffiliatedOrgs().size() > 0) {
63  0 for (AffiliatedOrgInfo org : courseRevenue.getAffiliatedOrgs()) {
64  0 totalOrgPercent += org.getPercentage();
65    }
66    }
67    }
68   
69  0 if (((Collection<?>) revenuesObj).size() > 0 && totalOrgPercent != 100l) {
70  0 ValidationResultInfo valRes = new ValidationResultInfo(getElementXpath(elementStack));
71  0 valRes.setElement("/revenues");
72  0 valRes.setError(MessageUtils.interpolate(getMessage("validation.revenueTotal"), toMap(field)));
73  0 results.add(valRes);
74    }
75   
76  0 return results;
77    }
78    }