1 | |
package org.kuali.student.lum.program.service.validation; |
2 | |
|
3 | |
import java.text.MessageFormat; |
4 | |
import java.util.ArrayList; |
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.search.dto.SearchRequest; |
11 | |
import org.kuali.student.common.search.dto.SearchResult; |
12 | |
import org.kuali.student.common.search.dto.SearchResultCell; |
13 | |
import org.kuali.student.common.search.dto.SearchResultRow; |
14 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
15 | |
import org.kuali.student.common.validator.DefaultValidatorImpl; |
16 | |
import org.kuali.student.lum.program.dto.MajorDisciplineInfo; |
17 | |
|
18 | 2 | public class ProgramManagingBodiesValidator extends DefaultValidatorImpl { |
19 | |
|
20 | |
@Override |
21 | |
public List<ValidationResultInfo> validateObject(FieldDefinition field, |
22 | |
Object o, ObjectStructureDefinition objStructure, |
23 | |
Stack<String> elementStack) { |
24 | 22 | List<ValidationResultInfo> validationResults = new ArrayList<ValidationResultInfo>(); |
25 | |
|
26 | 22 | String element = getElementXpath(elementStack) + "/" + field.getName(); |
27 | |
|
28 | 22 | if (o instanceof MajorDisciplineInfo) { |
29 | 10 | MajorDisciplineInfo majorDisciplineInfo = (MajorDisciplineInfo) o; |
30 | |
|
31 | 10 | if (field.getName().equalsIgnoreCase("unitsContentOwner") && null != majorDisciplineInfo.getUnitsContentOwner() |
32 | |
&& !majorDisciplineInfo.getUnitsContentOwner().isEmpty()) { |
33 | 1 | validationResults = validateObject(element, majorDisciplineInfo.getUnitsContentOwner(), majorDisciplineInfo.getDivisionsContentOwner()); |
34 | 9 | } else if (field.getName().equalsIgnoreCase("unitsDeployment") && null != majorDisciplineInfo.getUnitsDeployment() |
35 | |
&& !majorDisciplineInfo.getUnitsDeployment().isEmpty()) { |
36 | 1 | validationResults = validateObject(element, majorDisciplineInfo.getUnitsDeployment(), majorDisciplineInfo.getDivisionsDeployment()); |
37 | 8 | } else if (field.getName().equalsIgnoreCase("unitsFinancialControl") && null != majorDisciplineInfo.getUnitsFinancialControl() |
38 | |
&& !majorDisciplineInfo.getUnitsFinancialControl().isEmpty()) { |
39 | 1 | validationResults = validateObject(element, majorDisciplineInfo.getUnitsFinancialControl(), majorDisciplineInfo.getDivisionsFinancialControl()); |
40 | 7 | } else if (field.getName().equalsIgnoreCase("unitsFinancialResources") && null != majorDisciplineInfo.getUnitsFinancialResources() |
41 | |
&& !majorDisciplineInfo.getUnitsFinancialResources().isEmpty()) { |
42 | 1 | validationResults = validateObject(element, majorDisciplineInfo.getUnitsFinancialResources(), majorDisciplineInfo.getDivisionsFinancialResources()); |
43 | 6 | } else if (field.getName().equalsIgnoreCase("unitsStudentOversight") && null != majorDisciplineInfo.getUnitsStudentOversight() |
44 | |
&& !majorDisciplineInfo.getUnitsStudentOversight().isEmpty()) { |
45 | 1 | validationResults = validateObject(element, majorDisciplineInfo.getUnitsStudentOversight(), majorDisciplineInfo.getDivisionsStudentOversight()); |
46 | |
} |
47 | |
} |
48 | |
|
49 | 22 | return validationResults; |
50 | |
} |
51 | |
|
52 | |
public List<ValidationResultInfo> validateObject(String element, List<String> departmentIds, List<String> collegeIds) { |
53 | 5 | List<ValidationResultInfo> validationResults = new ArrayList<ValidationResultInfo>(); |
54 | |
|
55 | 5 | List<String> departmentRelatedCollegeIds = getDepartmentRelatedColleges(departmentIds); |
56 | |
|
57 | 5 | if (null != collegeIds) { |
58 | 5 | for (String collegeId : collegeIds) { |
59 | 10 | if (!departmentRelatedCollegeIds.contains(collegeId)) { |
60 | 10 | validationResults.addAll(getValidationResultInfo(element, collegeId, departmentIds)); |
61 | |
} |
62 | |
} |
63 | |
} |
64 | |
|
65 | 5 | return validationResults; |
66 | |
} |
67 | |
|
68 | |
private List<String> getDepartmentRelatedColleges(List<String> departmentIds) { |
69 | 5 | List<String> departmentRelatedCollegeIds = new ArrayList<String>(); |
70 | 5 | SearchRequest searchRequest = new SearchRequest("org.search.orgQuickViewByRelationTypeOrgTypeRelatedOrgIds"); |
71 | |
|
72 | 5 | List<String> orgTypes = new ArrayList<String>(); |
73 | 5 | orgTypes.add("kuali.org.College"); |
74 | |
|
75 | 5 | searchRequest.addParam("org.queryParam.optionalOrgTypeList", orgTypes); |
76 | 5 | searchRequest.addParam("org.queryParam.optionalRelationType", "kuali.org.Contain"); |
77 | 5 | searchRequest.addParam("org.queryParam.relatedOrgIds", departmentIds); |
78 | |
|
79 | 5 | SearchResult searchResult = getSearchDispatcher().dispatchSearch(searchRequest); |
80 | |
|
81 | 5 | if (null != searchResult) { |
82 | 5 | for (SearchResultRow row : searchResult.getRows()) { |
83 | 5 | for (SearchResultCell cell : row.getCells()) { |
84 | 5 | if ("org.resultColumn.orgId".equals(cell.getKey())) { |
85 | 0 | departmentRelatedCollegeIds.add(cell.getValue()); |
86 | |
} |
87 | |
} |
88 | |
} |
89 | |
} |
90 | |
|
91 | 5 | return departmentRelatedCollegeIds; |
92 | |
} |
93 | |
|
94 | |
private List<ValidationResultInfo> getValidationResultInfo(String element, String collegeId, List<String> departmentIds) { |
95 | 10 | List<ValidationResultInfo> validationResults = new ArrayList<ValidationResultInfo>(); |
96 | |
|
97 | 10 | String message = getMessage("validation.programManagingBodiesMatch"); |
98 | 10 | String collegeName = getCollegeName(collegeId); |
99 | 10 | List<String> departments = getDepartments(departmentIds); |
100 | |
|
101 | 10 | for (String departmentName : departments) { |
102 | 0 | ValidationResultInfo validationResultInfo = new ValidationResultInfo(element); |
103 | 0 | validationResultInfo.setWarning(MessageFormat.format(message, collegeName, departmentName)); |
104 | 0 | validationResults.add(validationResultInfo); |
105 | 0 | } |
106 | |
|
107 | 10 | return validationResults; |
108 | |
} |
109 | |
|
110 | |
private String getCollegeName(String collegeId) { |
111 | 10 | String collegeName = ""; |
112 | 10 | SearchRequest searchRequest = new SearchRequest("org.search.generic"); |
113 | |
|
114 | 10 | List<String> orgTypes = new ArrayList<String>(); |
115 | 10 | orgTypes.add("kuali.org.College"); |
116 | |
|
117 | 10 | searchRequest.addParam("org.queryParam.orgOptionalType", orgTypes); |
118 | 10 | searchRequest.addParam("org.queryParam.orgOptionalId", collegeId); |
119 | |
|
120 | 10 | SearchResult searchResult = getSearchDispatcher().dispatchSearch(searchRequest); |
121 | 10 | if (null != searchResult) { |
122 | 10 | for (SearchResultRow result : searchResult.getRows()) { |
123 | 10 | for (SearchResultCell resultCell : result.getCells()) { |
124 | 10 | if ("org.resultColumn.orgOptionalLongName".equals(resultCell.getKey())) { |
125 | 0 | collegeName = resultCell.getValue(); |
126 | |
} |
127 | |
} |
128 | |
} |
129 | |
} |
130 | |
|
131 | 10 | return collegeName; |
132 | |
} |
133 | |
|
134 | |
private List<String> getDepartments(List<String> departmentIds) { |
135 | 10 | List<String> departments = new ArrayList<String>(); |
136 | 10 | SearchRequest searchRequest = new SearchRequest("org.search.generic"); |
137 | |
|
138 | 10 | List<String> orgTypes = new ArrayList<String>(); |
139 | 10 | orgTypes.add("kuali.org.Department"); |
140 | |
|
141 | 10 | searchRequest.addParam("org.queryParam.orgOptionalType", orgTypes); |
142 | 10 | searchRequest.addParam("org.queryParam.orgOptionalIds", departmentIds); |
143 | |
|
144 | 10 | SearchResult searchResult = getSearchDispatcher().dispatchSearch(searchRequest); |
145 | |
|
146 | 10 | if (null != searchResult) { |
147 | 10 | for (SearchResultRow result : searchResult.getRows()) { |
148 | 10 | for (SearchResultCell resultCell : result.getCells()) { |
149 | 10 | if ("org.resultColumn.orgOptionalLongName".equals(resultCell.getKey())) { |
150 | 0 | departments.add(resultCell.getValue()); |
151 | |
} |
152 | |
} |
153 | |
} |
154 | |
} |
155 | |
|
156 | 10 | return departments; |
157 | |
} |
158 | |
} |