1 package org.kuali.student.enrollment.class2.scheduleofclasses.util;
2
3 import org.kuali.rice.core.api.criteria.Predicate;
4 import org.kuali.rice.core.api.criteria.QueryByCriteria;
5 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
6 import org.kuali.rice.kim.api.identity.PersonService;
7 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
8 import org.kuali.rice.krms.api.KrmsConstants;
9 import org.kuali.rice.krms.api.repository.RuleManagementService;
10 import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
11 import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
12 import org.kuali.student.common.collection.KSCollectionUtils;
13 import org.kuali.student.enrollment.class2.courseoffering.dto.RegistrationGroupWrapper;
14 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
15 import org.kuali.student.enrollment.class2.scheduleofclasses.form.ScheduleOfClassesSearchForm;
16 import org.kuali.student.enrollment.class2.scheduleofclasses.service.ScheduleOfClassesViewHelperService;
17 import org.kuali.student.enrollment.courseofferingset.dto.SocInfo;
18 import org.kuali.student.enrollment.courseofferingset.service.CourseOfferingSetService;
19 import org.kuali.student.r2.common.constants.CommonServiceConstants;
20 import org.kuali.student.r2.common.dto.ContextInfo;
21 import org.kuali.student.r2.common.exceptions.OperationFailedException;
22 import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants;
23 import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
24 import org.kuali.student.r2.core.atp.dto.AtpInfo;
25 import org.kuali.student.r2.core.atp.service.AtpService;
26
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.apache.log4j.Logger;
34 import org.kuali.student.r2.core.class1.type.service.TypeService;
35 import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
36 import org.kuali.student.r2.core.constants.AtpServiceConstants;
37 import org.kuali.student.r2.core.organization.service.OrganizationService;
38
39 import javax.xml.namespace.QName;
40
41 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
42
43
44
45
46
47
48
49
50 public class ScheduleOfClassesUtil {
51
52 private static final Logger LOG = Logger.getLogger(ScheduleOfClassesUtil.class);
53
54 private static ScheduleOfClassesViewHelperService viewHelperService;
55 private static AcademicCalendarService acalService;
56 private static CourseOfferingSetService courseOfferingSetService;
57 private static AtpService atpService;
58 private static TypeService typeService;
59 private static PersonService personService;
60 private static OrganizationService organizationService;
61 private static RuleManagementService ruleManagementService;
62 private static KrmsTypeRepositoryService krmsTypeRepositoryService;
63
64 public static ScheduleOfClassesViewHelperService getViewHelperService(ScheduleOfClassesSearchForm theForm) {
65 if (viewHelperService == null) {
66 if (theForm.getView().getViewHelperService() != null) {
67 viewHelperService = (ScheduleOfClassesViewHelperService) theForm.getView().getViewHelperService();
68 } else {
69 viewHelperService = (ScheduleOfClassesViewHelperService) theForm.getPostedView().getViewHelperService();
70 }
71 }
72 return viewHelperService;
73 }
74
75 public static AcademicCalendarService getAcademicCalendarService() {
76 if (acalService == null) {
77 acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
78 }
79 return acalService;
80 }
81
82 public static CourseOfferingSetService getCourseOfferingSetService() {
83 if (courseOfferingSetService == null) {
84
85 courseOfferingSetService = (CourseOfferingSetService) GlobalResourceLoader.getService(new QName(CourseOfferingSetServiceConstants.NAMESPACE, CourseOfferingSetServiceConstants.SERVICE_NAME_LOCAL_PART));
86 }
87 return courseOfferingSetService;
88 }
89
90 public static AtpService getAtpService() {
91 if (atpService == null) {
92 atpService = (AtpService) GlobalResourceLoader.getService(new QName(AtpServiceConstants.NAMESPACE, AtpServiceConstants.SERVICE_NAME_LOCAL_PART));
93 }
94 return atpService;
95 }
96
97 public static TypeService getTypeService() {
98 if (typeService == null) {
99 typeService = CourseOfferingResourceLoader.loadTypeService();
100 }
101 return typeService;
102 }
103
104 public static OrganizationService getOrganizationService() {
105 if (organizationService == null) {
106 organizationService = (OrganizationService) GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + "organization", "OrganizationService"));
107 }
108 return organizationService;
109 }
110
111 public static PersonService getPersonService() {
112 if (personService == null) {
113 personService = KimApiServiceLocator.getPersonService();
114 }
115 return personService;
116 }
117
118 public static RuleManagementService getRuleManagementService() {
119 if (ruleManagementService == null) {
120 ruleManagementService = (RuleManagementService) GlobalResourceLoader.getService(new QName(KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0, "ruleManagementService"));
121 }
122 return ruleManagementService;
123 }
124
125 public static KrmsTypeRepositoryService getKrmsTypeRepositoryService() {
126 if (krmsTypeRepositoryService == null) {
127 krmsTypeRepositoryService = (KrmsTypeRepositoryService) GlobalResourceLoader.getService(new QName(KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0, "krmsTypeRepositoryService"));
128 }
129 return krmsTypeRepositoryService;
130 }
131
132
133
134
135 private ScheduleOfClassesUtil() {}
136
137
138
139
140
141
142
143
144
145 public static List<AtpInfo> getValidSocTerms(CourseOfferingSetService courseOfferingSetService, AtpService atpService, ContextInfo context) {
146 List<SocInfo> socs;
147 List<String> termIds = new ArrayList<String>();
148 List<AtpInfo> atps = new ArrayList<AtpInfo>();
149
150
151 QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
152 qBuilder.setPredicates();
153 Predicate pred = equal(CourseOfferingSetServiceConstants.SearchParameters.SOC_STATE, CourseOfferingSetServiceConstants.PUBLISHED_SOC_STATE_KEY);
154 qBuilder.setPredicates(pred);
155 try {
156 socs = courseOfferingSetService.searchForSocs(qBuilder.build(), context);
157 if (socs != null && socs.size() > 0) {
158 for(SocInfo soc: socs){
159
160 termIds.add(soc.getTermId());
161 }
162
163 atps = atpService.getAtpsByIds(termIds, context);
164 } else {
165 return atps;
166 }
167 } catch (Exception e) {
168 throw new RuntimeException("Error getting Valid SOC Terms", e);
169 }
170 return atps;
171 }
172
173
174
175
176
177
178
179 public static AtpInfo getClosestAtp(List<AtpInfo> atpInfos) {
180 Date now = new Date();
181 AtpInfo closestAtp = null;
182
183 for (AtpInfo atp : atpInfos) {
184 if (isAtpValid(atp)) {
185
186 boolean isFuture = atp.getStartDate().after(now);
187 if (isFuture) {
188 if (closestAtp == null) {
189 closestAtp = atp;
190 } else {
191
192 boolean isCloser = atp.getStartDate().before(closestAtp.getStartDate());
193 if (isCloser) {
194 closestAtp = atp;
195 }
196 }
197 }
198 }
199 }
200 return closestAtp;
201 }
202
203
204
205
206
207
208 private static boolean isAtpValid(AtpInfo atp){
209 if (atp != null && atp.getStartDate() != null && atp.getEndDate() != null) {
210 return true;
211 } else {
212 LOG.error(String.format("ATP %s has a null start or end date.", atp.getId()));
213 return false;
214 }
215 }
216
217
218
219
220
221
222 public static void loadRequisites(SOCRequisiteWrapper reqWrapper, List<String> aoIds) {
223
224
225 for (String ruleType : reqWrapper.getRuleTypes()) {
226
227 boolean overridden = false;
228 RuleDefinition coRule = getRuleForType(reqWrapper.getCoRules(), ruleType);
229 if(coRule!=null){
230
231
232 for (Map.Entry<String, List<RuleDefinition>> aoEntry : reqWrapper.getAoToRulesMap().entrySet()) {
233 if(getRuleForType(aoEntry.getValue(), ruleType)!=null){
234 overridden = true;
235 break;
236 }
237 }
238
239 if(!overridden){
240 reqWrapper.getCoRequisite().append(reqWrapper.getNlMap().get(coRule.getId()));
241 }
242 }
243
244
245 for (String aoId : aoIds) {
246 RuleDefinition aoRule = getRuleForType(reqWrapper.getAoToRulesMap().get(aoId), ruleType);
247 if(aoRule!=null){
248 if(aoRule.getPropId()==null){
249 continue;
250 } else {
251 addAORequisite(reqWrapper, aoId, ruleType, reqWrapper.getNlMap().get(aoRule.getId()));
252 }
253 } else if (overridden) {
254 addAORequisite(reqWrapper, aoId, ruleType, reqWrapper.getNlMap().get(coRule.getId()));
255 }
256 }
257 }
258
259 }
260
261 private static RuleDefinition getRuleForType(List<RuleDefinition> rules, String ruleType){
262 if(rules==null){
263 return null;
264 }
265
266 for(RuleDefinition rule : rules){
267 if(ruleType.equals(rule.getTypeId())){
268 return rule;
269 }
270 }
271
272 return null;
273 }
274
275 private static void addAORequisite(SOCRequisiteWrapper reqWrapper, String aoId, String ruleType, String aoRequisite){
276 if (reqWrapper.getAoRequisiteMap().containsKey(aoId)) {
277 reqWrapper.getAoRequisiteMap().get(aoId).put(ruleType, aoRequisite);
278 } else {
279 Map<String, String> temp = new LinkedHashMap<String, String>();
280 temp.put(ruleType, aoRequisite);
281 reqWrapper.getAoRequisiteMap().put(aoId, temp);
282 }
283 }
284
285
286
287
288
289
290 public static void loadRegRequisites(SOCRequisiteWrapper reqWrapper, List<RegistrationGroupWrapper> registrationGroupWrapperList) {
291 StringBuilder firstReq;
292 StringBuilder secondReq;
293 StringBuilder commonReq;
294
295
296 for(int i = 0; i < registrationGroupWrapperList.size(); i++) {
297 RegistrationGroupWrapper registrationGroupWrapper = registrationGroupWrapperList.get(i);
298
299 firstReq = new StringBuilder();
300 secondReq = new StringBuilder();
301 commonReq = new StringBuilder();
302
303
304 RegistrationGroupWrapper partnerRegGroup = getRegistrationGroupWrapper(registrationGroupWrapperList, registrationGroupWrapper.getRgInfo().getName(), registrationGroupWrapper.getAoActivityCodeText());
305
306 String regAoId, partnerAoId = null;
307 try{
308 regAoId = KSCollectionUtils.getOptionalZeroElement(registrationGroupWrapper.getRgInfo().getActivityOfferingIds());
309 if (!partnerRegGroup.getAoActivityCodeText().equals("null")) {
310 i++;
311 partnerAoId = KSCollectionUtils.getOptionalZeroElement(partnerRegGroup.getRgInfo().getActivityOfferingIds());
312 }
313 } catch (OperationFailedException e) {
314 throw new RuntimeException("Unable to retrieve activity offering id.", e);
315 }
316
317
318 if(reqWrapper.getAoRequisiteMap().containsKey(regAoId) ||
319 reqWrapper.getAoRequisiteMap().containsKey(partnerAoId)) {
320
321 for(String rule : reqWrapper.getRuleTypes()) {
322 Map<String, String> firstReqMap = reqWrapper.getAoRequisiteMap().get(regAoId);
323 Map<String, String> secondReqMap = reqWrapper.getAoRequisiteMap().get(partnerAoId);
324
325 if (firstReqMap != null && secondReqMap != null) {
326 if (firstReqMap.containsKey(rule) && secondReqMap.containsKey(rule)) {
327 if (firstReqMap.get(rule).equals(secondReqMap.get(rule))) {
328 commonReq.append(firstReqMap.get(rule));
329 continue;
330 }
331 }
332 }
333 if (firstReqMap != null) {
334 if (firstReqMap.containsKey(rule)) {
335 firstReq.append(firstReqMap.get(rule));
336 }
337 }
338 if (secondReqMap != null) {
339 if (secondReqMap.containsKey(rule)) {
340 secondReq.append(secondReqMap.get(rule));
341 }
342 }
343 }
344
345 if (!commonReq.toString().isEmpty()) {
346 registrationGroupWrapper.setCommonRequisite(commonReq.toString());
347 }
348 if (!firstReq.toString().isEmpty()) {
349 registrationGroupWrapper.setRequisite(firstReq.toString());
350 }
351 if (!secondReq.toString().isEmpty()) {
352 partnerRegGroup.setRequisite(secondReq.toString());
353 }
354 }
355 }
356 }
357
358 private static RegistrationGroupWrapper getRegistrationGroupWrapper(List<RegistrationGroupWrapper> registrationGroupWrapperList, String name, String aoCode) {
359 for (RegistrationGroupWrapper registrationGroupWrapper : registrationGroupWrapperList) {
360 if(registrationGroupWrapper.getRgInfo().getName().equals(name) && !registrationGroupWrapper.getAoActivityCodeText().equals(aoCode)) {
361 return registrationGroupWrapper;
362 }
363 }
364 RegistrationGroupWrapper emptyRegistrationGroupWrapper = new RegistrationGroupWrapper();
365 emptyRegistrationGroupWrapper.setAoActivityCodeText("null");
366 return emptyRegistrationGroupWrapper;
367 }
368
369 }