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