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.util.MessageUtils; |
9 |
|
import org.kuali.student.common.validator.BeanConstraintDataProvider; |
10 |
|
import org.kuali.student.common.validator.ConstraintDataProvider; |
11 |
|
import org.kuali.student.common.validator.DefaultValidatorImpl; |
12 |
|
import org.kuali.student.core.dictionary.dto.FieldDefinition; |
13 |
|
import org.kuali.student.core.dictionary.dto.ObjectStructureDefinition; |
14 |
|
import org.kuali.student.core.validation.dto.ValidationResultInfo; |
15 |
|
import org.kuali.student.lum.course.dto.CourseRevenueInfo; |
16 |
|
import org.kuali.student.lum.lu.dto.AffiliatedOrgInfo; |
17 |
|
|
|
|
| 74.3% |
Uncovered Elements: 9 (35) |
Complexity: 8 |
Complexity Density: 0.35 |
|
18 |
|
public class RevenuePercentValidator extends DefaultValidatorImpl { |
19 |
|
|
20 |
|
private static final String COURSE_REVENUE_FIELD = "revenues"; |
21 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
22 |
0
|
@Override... |
23 |
|
public List<ValidationResultInfo> validateObject(Object data, ObjectStructureDefinition objStructure) { |
24 |
|
|
25 |
0
|
return null; |
26 |
|
} |
27 |
|
|
28 |
|
|
29 |
|
@see |
30 |
|
|
31 |
|
|
|
|
| 78.1% |
Uncovered Elements: 7 (32) |
Complexity: 7 |
Complexity Density: 0.32 |
|
32 |
7
|
@Override... |
33 |
|
public List<ValidationResultInfo> validateObject(FieldDefinition field, Object data, ObjectStructureDefinition objStructure, Stack<String> elementStack) { |
34 |
|
|
35 |
7
|
List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>(); |
36 |
|
|
37 |
|
|
38 |
7
|
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 |
7
|
ConstraintDataProvider dataProvider = new BeanConstraintDataProvider(); |
43 |
7
|
dataProvider.initialize(data); |
44 |
|
|
45 |
|
|
46 |
7
|
Object revenuesObj = dataProvider.getValue(field.getName()); |
47 |
|
|
48 |
7
|
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 |
7
|
long totalOrgPercent = 0l; |
53 |
|
|
54 |
7
|
for (Object o : (Collection<?>) revenuesObj) { |
55 |
|
|
56 |
2
|
if (!(o instanceof CourseRevenueInfo)) { |
57 |
0
|
throw new RuntimeException("Custom Validator " + this.getClass().getName() + " was not called with right data: CourseRevenueInfo"); |
58 |
|
} |
59 |
|
|
60 |
2
|
CourseRevenueInfo courseRevenue = (CourseRevenueInfo) o; |
61 |
|
|
62 |
2
|
if (courseRevenue.getAffiliatedOrgs().size() > 0) { |
63 |
2
|
for (AffiliatedOrgInfo org : courseRevenue.getAffiliatedOrgs()) { |
64 |
4
|
totalOrgPercent += org.getPercentage(); |
65 |
|
} |
66 |
|
} |
67 |
|
} |
68 |
|
|
69 |
7
|
if (((Collection<?>) revenuesObj).size() > 0 && totalOrgPercent != 100l) { |
70 |
1
|
ValidationResultInfo valRes = new ValidationResultInfo(getElementXpath(elementStack)); |
71 |
1
|
valRes.setElement("/revenues"); |
72 |
1
|
valRes.setError(MessageUtils.interpolate(getMessage("validation.revenueTotal"), toMap(field))); |
73 |
1
|
results.add(valRes); |
74 |
|
} |
75 |
|
|
76 |
7
|
return results; |
77 |
|
} |
78 |
|
} |