1 | |
package org.kuali.student.enrollment.class2.courseoffering.validation.impl; |
2 | |
|
3 | |
import javax.xml.namespace.QName; |
4 | |
import java.util.ArrayList; |
5 | |
import java.util.List; |
6 | |
import java.util.Map; |
7 | |
import java.util.HashMap; |
8 | |
|
9 | |
import org.hibernate.mapping.Index; |
10 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
11 | |
import org.kuali.rice.kim.api.identity.IdentityService; |
12 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
13 | |
import org.kuali.rice.krad.bo.PersistableBusinessObject; |
14 | |
import org.kuali.rice.krad.maintenance.MaintenanceDocument; |
15 | |
import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase; |
16 | |
import org.kuali.rice.krad.util.GlobalVariables; |
17 | |
import org.kuali.rice.krad.util.KRADConstants; |
18 | |
|
19 | |
import org.kuali.rice.krad.web.form.LookupForm; |
20 | |
import org.kuali.student.common.search.dto.*; |
21 | |
import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo; |
22 | |
import org.kuali.student.enrollment.courseoffering.dto.OfferingInstructorInfo; |
23 | |
import org.kuali.student.lum.lu.service.LuService; |
24 | |
import org.kuali.student.lum.lu.service.LuServiceConstants; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
@Deprecated |
30 | 0 | public class CourseOfferingRule extends MaintenanceDocumentRuleBase { |
31 | |
private static final String COURSE_CODE_PROPERTY_PATH = "document.newMaintainableObject.dataObject.courseOfferingCode"; |
32 | |
|
33 | |
private transient LuService luService; |
34 | |
private transient IdentityService identityService; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
@Override |
40 | |
protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) { |
41 | 0 | boolean isValid = true; |
42 | |
|
43 | |
|
44 | 0 | CourseOfferingInfo courseOfferingInfo = (CourseOfferingInfo)document.getDocumentDataObject(); |
45 | 0 | String courseCode = courseOfferingInfo.getCourseOfferingCode(); |
46 | 0 | String courseId = retrieveCourseIdFromCourseCode(courseCode); |
47 | 0 | if (courseId == null) { |
48 | 0 | GlobalVariables.getMessageMap().putError( |
49 | |
COURSE_CODE_PROPERTY_PATH,"error.enroll.courseoffering.courseOfferingCode.notExists"); |
50 | 0 | isValid = false; |
51 | |
} |
52 | |
else{ |
53 | 0 | courseOfferingInfo.setCourseId(courseId); |
54 | |
} |
55 | |
|
56 | |
|
57 | 0 | List<OfferingInstructorInfo> instructors = courseOfferingInfo.getInstructors(); |
58 | 0 | int index = 0; |
59 | 0 | for(OfferingInstructorInfo instructor : instructors){ |
60 | 0 | String principalId = instructor.getPersonId(); |
61 | 0 | if (getIdentityService().getPrincipal(principalId) == null) { |
62 | 0 | GlobalVariables.getMessageMap().putError( |
63 | |
"document.newMaintainableObject.dataObject.instructors["+index+"]", "error.enroll.courseoffering.instructors.notExists", "The instructor: "+principalId+" does not exist."); |
64 | 0 | isValid = false; |
65 | |
} |
66 | 0 | index++; |
67 | 0 | } |
68 | 0 | isValid &= super.processCustomRouteDocumentBusinessRules(document); |
69 | 0 | isValid &= GlobalVariables.getMessageMap().hasNoErrors(); |
70 | |
|
71 | 0 | return isValid; |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
protected LuService getLuService() { |
96 | 0 | if(luService == null) { |
97 | 0 | luService = (LuService)GlobalResourceLoader.getService(new QName(LuServiceConstants.LU_NAMESPACE,"LuService")); |
98 | |
} |
99 | 0 | return this.luService; |
100 | |
} |
101 | |
|
102 | |
protected IdentityService getIdentityService() { |
103 | 0 | if ( identityService == null ) { |
104 | 0 | identityService = KimApiServiceLocator.getIdentityService(); |
105 | |
} |
106 | 0 | return identityService; |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
private String retrieveCourseIdFromCourseCode (String courseCode){ |
113 | 0 | Map<String,String> fieldValues = new HashMap<String, String>(); |
114 | 0 | fieldValues.put("code",courseCode); |
115 | 0 | List courseIds = getSearchResults(null, fieldValues, false); |
116 | 0 | if(courseIds.size() != 1){ |
117 | 0 | return null; |
118 | |
} |
119 | |
else{ |
120 | 0 | return (String) courseIds.get(0); |
121 | |
} |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
private List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) { |
127 | 0 | List <String> courseIds = new ArrayList<String>(); |
128 | 0 | List<SearchParam> searchParams = new ArrayList<SearchParam>(); |
129 | 0 | SearchParam qpv1 = new SearchParam(); |
130 | 0 | qpv1.setKey("lu.queryParam.luOptionalType"); |
131 | 0 | qpv1.setValue("kuali.lu.type.CreditCourse"); |
132 | 0 | searchParams.add(qpv1); |
133 | 0 | SearchParam qpv2 = new SearchParam(); |
134 | 0 | qpv1.setKey("lu.queryParam.luOptionalCode"); |
135 | 0 | qpv1.setValue(fieldValues.get("code")); |
136 | 0 | searchParams.add(qpv2); |
137 | |
|
138 | 0 | SearchRequest searchRequest = new SearchRequest(); |
139 | 0 | searchRequest.setParams(searchParams); |
140 | 0 | searchRequest.setSearchKey("lu.search.mostCurrent.union"); |
141 | |
|
142 | |
try { |
143 | 0 | SearchResult searchResult = getLuService().search(searchRequest); |
144 | |
|
145 | 0 | if (searchResult.getRows().size() > 0) { |
146 | 0 | for(SearchResultRow srrow : searchResult.getRows()){ |
147 | 0 | List<SearchResultCell> srCells = srrow.getCells(); |
148 | 0 | if(srCells != null && srCells.size() > 0){ |
149 | 0 | for(SearchResultCell srcell : srCells){ |
150 | 0 | if (srcell.getKey().equals("lu.resultColumn.cluId")) { |
151 | 0 | String courseId = srcell.getValue(); |
152 | 0 | courseIds.add(courseId); |
153 | 0 | } |
154 | |
} |
155 | |
} |
156 | 0 | } |
157 | |
} |
158 | |
|
159 | 0 | return courseIds; |
160 | 0 | } catch (Exception e) { |
161 | 0 | throw new RuntimeException(e); |
162 | |
} |
163 | |
} |
164 | |
|
165 | |
} |