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