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 | |
|
24 | 0 | return null; |
25 | |
} |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
@Override |
32 | |
public List<ValidationResultInfo> validateObject(FieldDefinition field, Object data, ObjectStructureDefinition objStructure, Stack<String> elementStack) { |
33 | |
|
34 | 0 | List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>(); |
35 | |
|
36 | |
|
37 | 0 | 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 | 0 | ConstraintDataProvider dataProvider = new BeanConstraintDataProvider(); |
42 | 0 | dataProvider.initialize(data); |
43 | |
|
44 | |
|
45 | 0 | Object expenditureObj = dataProvider.getValue(field.getName()); |
46 | |
|
47 | |
|
48 | 0 | if(null == expenditureObj) { |
49 | 0 | return results; |
50 | |
} |
51 | |
|
52 | 0 | if (!(expenditureObj instanceof CourseExpenditureInfo)) { |
53 | 0 | throw new RuntimeException("Custom Validator " + this.getClass().getName() + " was not called with right data: CourseExpenditureInfo"); |
54 | |
} |
55 | |
|
56 | 0 | CourseExpenditureInfo courseExpInfo = (CourseExpenditureInfo) expenditureObj; |
57 | |
|
58 | 0 | long totalOrgPercent = 0l; |
59 | 0 | if (courseExpInfo.getAffiliatedOrgs().size() > 0) { |
60 | 0 | for (AffiliatedOrgInfo org : courseExpInfo.getAffiliatedOrgs()) { |
61 | 0 | totalOrgPercent += org.getPercentage(); |
62 | |
} |
63 | |
} |
64 | |
|
65 | 0 | if (courseExpInfo.getAffiliatedOrgs().size() > 0 && totalOrgPercent != 100l) { |
66 | 0 | ValidationResultInfo valRes = new ValidationResultInfo(getElementXpath(elementStack)); |
67 | 0 | valRes.setElement("/expenditure/affiliatedOrgs"); |
68 | 0 | valRes.setError(MessageUtils.interpolate(getMessage("validation.expenditureTotal"), toMap(field))); |
69 | 0 | results.add(valRes); |
70 | |
} |
71 | |
|
72 | 0 | return results; |
73 | |
} |
74 | |
} |