1 package org.kuali.student.r2.lum.course.service.utils;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.Set;
7 import java.util.Stack;
8
9 import org.kuali.student.r1.common.dictionary.dto.FieldDefinition;
10 import org.kuali.student.r1.common.dictionary.dto.ObjectStructureDefinition;
11 import org.kuali.student.r2.common.dto.ContextInfo;
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 import org.kuali.student.r1.common.search.service.SearchDispatcher;
17 import org.kuali.student.r2.common.dto.ValidationResultInfo;
18 import org.kuali.student.r2.common.validator.DefaultValidatorImpl;
19 import org.kuali.student.r2.lum.course.dto.CourseInfo;
20
21
22
23
24
25
26 public class SubjectAreaUnitOwnerValidator extends DefaultValidatorImpl {
27
28 private SearchDispatcher searchDispatcher;
29
30 @Override
31 public List<ValidationResultInfo> validateObject(FieldDefinition field,
32 Object o, ObjectStructureDefinition objStructure,
33 Stack<String> elementStack,ContextInfo contextInfo) {
34
35 List<ValidationResultInfo> validationResults = new ArrayList<ValidationResultInfo>();
36
37 if (o instanceof CourseInfo) {
38 CourseInfo course = (CourseInfo) o;
39 if(course.getSubjectArea()!=null && !course.getUnitsContentOwner().isEmpty()){
40
41 SearchRequest searchRequest = new SearchRequest("subjectCode.search.orgsForSubjectCode");
42 searchRequest.addParam("subjectCode.queryParam.code", course.getSubjectArea());
43
44 SearchResult result = searchDispatcher.dispatchSearch(searchRequest);
45
46 Set<String> orgIds = new HashSet<String>();
47 boolean useageAllOf = true;
48
49 if(result!=null){
50
51
52 for(SearchResultRow row:result.getRows()){
53 for(SearchResultCell cell:row.getCells()){
54 if("subjectCode.resultColumn.orgId".equals(cell.getKey())){
55 orgIds.add(cell.getValue());
56 }else if("subjectCode.resultColumn.type".equals(cell.getKey())&&"ks.core.subjectcode.usage.one".equals(cell.getValue())){
57 useageAllOf = false;
58 }
59 }
60 }
61 }
62
63 List<String> units = new ArrayList<String>(course.getUnitsContentOwner());
64 if(useageAllOf){
65
66 if(!units.containsAll(orgIds)){
67 ValidationResultInfo validationResult = new ValidationResultInfo(getElementXpath(elementStack) + "/" + field.getName());
68 validationResult.setWarning(getMessage("validation.course.subjectAreaUsage.all",contextInfo));
69 validationResults.add(validationResult);
70 }
71 }else{
72
73 units.retainAll(orgIds);
74 if(units.size()!=1){
75 ValidationResultInfo validationResult = new ValidationResultInfo(getElementXpath(elementStack) + "/" + field.getName());
76 validationResult.setWarning(getMessage("validation.course.subjectAreaUsage.one", contextInfo));
77 validationResults.add(validationResult);
78 }
79 }
80 }
81 }
82
83 return validationResults;
84 }
85
86 public void setSearchDispatcher(SearchDispatcher searchDispatcher) {
87 this.searchDispatcher = searchDispatcher;
88 }
89
90 }