1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.student.enrollment.class2.courseoffering.service.impl;
18
19 import org.kuali.rice.core.api.criteria.PredicateFactory;
20 import org.kuali.rice.core.api.criteria.QueryByCriteria;
21 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22 import org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl;
23 import org.kuali.rice.krad.util.GlobalVariables;
24 import org.kuali.student.r2.common.util.constants.AcademicCalendarServiceConstants;
25 import org.kuali.student.enrollment.acal.dto.TermInfo;
26 import org.kuali.student.enrollment.acal.service.AcademicCalendarService;
27 import org.kuali.student.enrollment.class2.courseoffering.form.CourseOfferingRolloverManagementForm;
28 import org.kuali.student.enrollment.class2.courseoffering.form.DeleteTargetTermForm;
29 import org.kuali.student.enrollment.class2.courseoffering.service.CourseOfferingViewHelperService;
30 import org.kuali.student.enrollment.class2.courseoffering.service.transformer.CourseOfferingTransformer;
31 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingConstants;
32 import org.kuali.student.enrollment.class2.courseofferingset.service.impl.DeleteTargetTermRolloverRunner;
33 import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
34 import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
35 import org.kuali.student.enrollment.courseoffering.dto.FinalExam;
36 import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo;
37 import org.kuali.student.enrollment.courseoffering.dto.OfferingInstructorInfo;
38 import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
39 import org.kuali.student.enrollment.courseofferingset.dto.SocInfo;
40 import org.kuali.student.enrollment.courseofferingset.dto.SocRolloverResultInfo;
41 import org.kuali.student.enrollment.courseofferingset.dto.SocRolloverResultItemInfo;
42 import org.kuali.student.enrollment.courseofferingset.service.CourseOfferingSetService;
43 import org.kuali.student.r2.lum.course.dto.CourseInfo;
44 import org.kuali.student.r2.common.constants.CommonServiceConstants;
45 import org.kuali.student.r2.common.dto.ContextInfo;
46 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
47 import org.kuali.student.r2.common.exceptions.MissingParameterException;
48 import org.kuali.student.r2.common.exceptions.OperationFailedException;
49 import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
50 import org.kuali.student.r2.common.util.ContextUtils;
51 import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants;
52 import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants;
53 import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
54 import org.kuali.student.r2.lum.course.service.CourseService;
55
56 import javax.xml.namespace.QName;
57 import java.text.SimpleDateFormat;
58 import java.util.ArrayList;
59 import java.util.Date;
60 import java.util.List;
61
62
63
64
65
66
67 public class CourseOfferingViewHelperServiceImpl extends ViewHelperServiceImpl implements CourseOfferingViewHelperService {
68 private AcademicCalendarService acalService = null;
69 private CourseOfferingService coService = null;
70 private CourseOfferingSetService socService = null;
71 private CourseService courseService = null;
72
73 @Override
74 public List<TermInfo> findTermByTermCode(String termCode) throws Exception {
75
76
77 QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
78
79 qbcBuilder.setPredicates(PredicateFactory.equal(CourseOfferingConstants.ATP_CODE, termCode));
80
81 QueryByCriteria criteria = qbcBuilder.build();
82
83
84 AcademicCalendarService acalService = _getAcalService();
85 List<TermInfo> terms = acalService.searchForTerms(criteria, new ContextInfo());
86 return terms;
87 }
88
89 private CourseOfferingInfo _createCourseOffering(String termId) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
90 ContextInfo contextInfo = ContextUtils.getContextInfo();
91
92 CourseOfferingService coService = _getCourseOfferingService();
93 CourseOfferingTransformer coTrans = new CourseOfferingTransformer();
94 CourseService courseService = _getCourseService();
95 CourseInfo courseInfo = null;
96 try {
97 courseInfo = courseService.getCourse("b509ad01-6ef3-44a4-8857-f5df8631f79e", contextInfo);
98 } catch (Exception e) {
99 e.printStackTrace();
100 return null;
101 }
102
103 CourseOfferingInfo coInfo = new CourseOfferingInfo();
104 List<String> copyOptions = new ArrayList<String>();
105 copyOptions.add(CourseOfferingSetServiceConstants.NOT_GRADING_CREDIT_OPTION_KEY);
106
107 coTrans.copyFromCanonical(courseInfo, coInfo, copyOptions, contextInfo);
108 coInfo.setCourseOfferingTitle("Intro to Finite Math");
109 coInfo.setTypeKey(LuiServiceConstants.COURSE_OFFERING_TYPE_KEY);
110 coInfo.setStateKey(LuiServiceConstants.LUI_CO_STATE_OFFERED_KEY);
111 coInfo.setMinimumEnrollment(5);
112 coInfo.setMaximumEnrollment(40);
113
114
115 coInfo.setCourseOfferingCode("CHEM241");
116 coInfo.setCourseNumberSuffix("241");
117 coInfo.setIsEvaluated(Boolean.TRUE);
118 coInfo.setFinalExamType(FinalExam.STANDARD.toString());
119 coInfo.setTermId(termId);
120 coInfo.setIsFeeAtActivityOffering(Boolean.FALSE);
121
122 coInfo.setInstructors(new ArrayList<OfferingInstructorInfo>());
123 try {
124 String courseId = coInfo.getCourseId();
125 String infoTermId = coInfo.getTermId();
126 String typeKey = coInfo.getTypeKey();
127 List<String> options = new ArrayList<String>();
128
129 CourseOfferingInfo result = coService.createCourseOffering(courseId, infoTermId, typeKey, coInfo,
130 options, contextInfo);
131 return result;
132 } catch (Exception e) {
133 e.printStackTrace();
134 return null;
135 }
136 }
137
138 private FormatOfferingInfo _createFormatOffering(CourseOfferingInfo coInfo) {
139 FormatOfferingInfo foInfo = new FormatOfferingInfo();
140 foInfo.setName("DEVTEST_format");
141 foInfo.setCourseOfferingId(coInfo.getId());
142 foInfo.setFormatId("10f433ba-50e4-4037-a727-4ea7747c3e6b");
143 foInfo.setTypeKey(LuiServiceConstants.FORMAT_OFFERING_TYPE_KEY);
144 foInfo.setStateKey(LuiServiceConstants.LUI_FO_STATE_OFFERED_KEY);
145 try {
146 FormatOfferingInfo result =
147 coService.createFormatOffering(coInfo.getId(), foInfo.getFormatId(), foInfo.getTypeKey(), foInfo, new ContextInfo());
148 return result;
149 } catch (Exception e) {
150 e.printStackTrace();
151 return null;
152 }
153 }
154
155 private ActivityOfferingInfo _createActivityOffering(FormatOfferingInfo foInfo, CourseOfferingInfo coInfo) {
156 ActivityOfferingInfo aoInfo = new ActivityOfferingInfo();
157 aoInfo.setActivityId("f0072e90-3aed-4d9b-8a5a-e7efe317a686");
158 aoInfo.setName("DEVTEST_activity");
159 aoInfo.setTypeKey(LuiServiceConstants.LECTURE_ACTIVITY_OFFERING_TYPE_KEY);
160 aoInfo.setStateKey(LuiServiceConstants.LUI_AO_STATE_OFFERED_KEY);
161 aoInfo.setActivityCode("A");
162 aoInfo.setCourseOfferingCode(coInfo.getCourseOfferingCode());
163 aoInfo.setCourseOfferingTitle(coInfo.getCourseOfferingTitle());
164 aoInfo.setFormatOfferingId(foInfo.getId());
165 aoInfo.setMinimumEnrollment(5);
166 aoInfo.setMaximumEnrollment(40);
167 try {
168 ActivityOfferingInfo result =
169 coService.createActivityOffering(foInfo.getId(), aoInfo.getActivityId(), aoInfo.getTypeKey(), aoInfo, new ContextInfo());
170 return result;
171 } catch (Exception e) {
172 e.printStackTrace();
173 return null;
174 }
175 }
176
177 private List<CourseOfferingInfo> _getCourseOfferingsByTerm(String termId) {
178 CourseOfferingSetService socService = _getSocService();
179 try {
180 List<String> socIds = socService.getSocIdsByTerm(termId, new ContextInfo());
181 SocInfo soc = null;
182 if (socIds != null && socIds.size() > 0) {
183 soc = socService.getSoc(socIds.get(0), new ContextInfo());
184 }
185 List<String> coIds = socService.getCourseOfferingIdsBySoc(soc.getId(), new ContextInfo());
186 List<CourseOfferingInfo> coInfos = new ArrayList<CourseOfferingInfo>();
187 for (String id: coIds) {
188 CourseOfferingInfo coInfo = coService.getCourseOffering(id, new ContextInfo());
189 coInfos.add(coInfo);
190 }
191 return coInfos;
192 } catch (Exception e) {
193 return null;
194 }
195 }
196
197 private void _deleteCourseOfferingById(String coId) {
198 CourseOfferingService coService = _getCourseOfferingService();
199 try {
200 coService.deleteCourseOffering(coId, new ContextInfo());
201 } catch (Exception e) {
202 }
203 }
204
205 private void _verify(CourseOfferingInfo coInfo, FormatOfferingInfo foInfo, ActivityOfferingInfo aoInfo) {
206 CourseOfferingService coService = _getCourseOfferingService();
207 CourseOfferingInfo coFetched;
208 FormatOfferingInfo foFetched;
209 ActivityOfferingInfo aoFetched;
210 try {
211 coFetched = coService.getCourseOffering(coInfo.getId(), new ContextInfo());
212 foFetched = coService.getFormatOffering(foInfo.getId(), new ContextInfo());
213 aoFetched = coService.getActivityOffering(aoInfo.getId(), new ContextInfo());
214 System.err.println("Success 1");
215 } catch (Exception e) {
216 System.err.println("Error");
217 }
218 System.err.println("Success");
219 }
220 @Override
221 public SocInfo createSocCoFoAoForTerm(String termId, CourseOfferingRolloverManagementForm form) {
222 CourseOfferingInfo coOffering;
223
224 try{
225 coOffering = _createCourseOffering(termId);
226 }catch(Exception e){
227 throw new RuntimeException("Failed to create Course Offering from Course",e);
228 }
229
230 if (coOffering == null) {
231 form.setStatusField("createSocCoFoAoForTerm: Course offering not created");
232 }
233 FormatOfferingInfo foOffering = _createFormatOffering(coOffering);
234 ActivityOfferingInfo aoOffering = _createActivityOffering(foOffering, coOffering);
235 _verify(coOffering, foOffering, aoOffering);
236 CourseOfferingSetService socService = _getSocService();
237
238 SocInfo socInfo = new SocInfo();
239 socInfo.setTypeKey(CourseOfferingSetServiceConstants.MAIN_SOC_TYPE_KEY);
240 socInfo.setStateKey(CourseOfferingSetServiceConstants.DRAFT_SOC_STATE_KEY);
241 socInfo.setTermId(termId);
242 try {
243 String socTermId = socInfo.getTermId();
244 String typeKey = socInfo.getTypeKey();
245 ContextInfo contextInfo = new ContextInfo();
246 SocInfo result = socService.createSoc(socTermId, typeKey, socInfo, contextInfo);
247 return result;
248 } catch (Exception e) {
249 return null;
250 }
251 }
252
253 private String _deleteSocAndCourseOfferingsByTerm(String termId, String termCode) {
254 CourseOfferingSetService socService = _getSocService();
255 CourseOfferingService coService = _getCourseOfferingService();
256 String mesg = "";
257 try {
258 List<String> socIds = socService.getSocIdsByTerm(termId, new ContextInfo());
259 if (socIds == null || socIds.isEmpty()) {
260 mesg = "NO_SOC";
261 }
262
263
264 List<String> coIds = coService.getCourseOfferingIdsByTerm(termId, Boolean.FALSE, new ContextInfo());
265 if (coIds.size() > 3) {
266 return "MANYCOS";
267 } else {
268
269 for (String coId: coIds) {
270 coService.deleteCourseOffering(coId, new ContextInfo());
271 }
272 if (!mesg.equals("NO_SOC")) {
273 String socId = socIds.get(0);
274 socService.deleteSoc(socId, new ContextInfo());
275 }
276 }
277 return mesg;
278 } catch (Exception e) {
279 e.printStackTrace();
280 return e.getMessage();
281 }
282 }
283
284 private int _mainSocCount(List<String> socIds) {
285 int mainSocCount = 0;
286 try {
287 for (String socId: socIds) {
288 SocInfo socInfo = socService.getSoc(socId, new ContextInfo());
289 if (socInfo.getTypeKey().equals(CourseOfferingSetServiceConstants.MAIN_SOC_TYPE_KEY)) {
290 mainSocCount++;
291 }
292 }
293 } catch (Exception e) {
294 return -1;
295 }
296 return mainSocCount;
297 }
298
299 private SocInfo _getUniqueMainSoc(List<String> socIds) {
300 SocInfo mainSoc = null;
301 int mainSocCount = 0;
302 try {
303 for (String socId: socIds) {
304 SocInfo socInfo = socService.getSoc(socId, new ContextInfo());
305 if (socInfo.getTypeKey().equals(CourseOfferingSetServiceConstants.MAIN_SOC_TYPE_KEY)) {
306 mainSocCount++;
307 if (mainSocCount > 1) {
308 mainSoc = null;
309 } else if (mainSocCount == 1) {
310 mainSoc = socInfo;
311 }
312 }
313 }
314 } catch (Exception e) {
315 return null;
316 }
317 return mainSoc;
318 }
319
320 @Override
321 public boolean termHasSoc(String termId, CourseOfferingRolloverManagementForm form) {
322 CourseOfferingSetService socService = _getSocService();
323 try {
324 List<String> socIds = socService.getSocIdsByTerm(termId, new ContextInfo());
325 if (socIds == null || socIds.isEmpty()) {
326 if (form != null) {
327 form.setStatusField("No SOCS in source term");
328 }
329 return false;
330 } else {
331 int mainSocCount = _mainSocCount(socIds);
332 if (mainSocCount != 1) {
333 if (form != null) {
334 form.setStatusField("Wrong number of SOCS in source term: " + socIds.size());
335 }
336 return false;
337 }
338 }
339 return true;
340 } catch (Exception e) {
341 return false;
342 }
343 }
344
345 @Override
346 public SocInfo getMainSoc(String termId) {
347 CourseOfferingSetService socService = _getSocService();
348 try {
349 List<String> socIds = socService.getSocIdsByTerm(termId, new ContextInfo());
350 return _getUniqueMainSoc(socIds);
351 } catch (Exception e) {
352 return null;
353 }
354 }
355
356 @Override
357 public SocRolloverResultInfo performReverseRollover(String sourceTermId, String targetTermId, CourseOfferingRolloverManagementForm form) {
358 CourseOfferingSetService socService = _getSocService();
359 CourseOfferingService coService = _getCourseOfferingService();
360 try {
361 List<String> socIds = socService.getSocIdsByTerm(sourceTermId, new ContextInfo());
362 if (socIds == null || socIds.isEmpty()) {
363 form.setStatusField("No SOCS in source term");
364 return null;
365 } else if (socIds.size() > 1) {
366 form.setStatusField("Too many SOCS in source term: " + socIds.size());
367 return null;
368 } else {
369 String sourceSocId = socIds.get(0);
370 List<String> resultIds = socService.getSocRolloverResultIdsBySourceSoc(sourceSocId, new ContextInfo());
371 if (resultIds == null || resultIds.isEmpty()) {
372 form.setStatusField("No rollover results for source term");
373 return null;
374 } else if (resultIds.size() > 1) {
375 form.setStatusField("Too many rollover results for source term: " + resultIds.size());
376 return null;
377 } else {
378 String socResultId = resultIds.get(0);
379 List<String> options = new ArrayList<String>();
380 SocRolloverResultInfo info = socService.reverseRollover(socResultId, options, new ContextInfo());
381 return info;
382 }
383 }
384 } catch (Exception e) {
385 return null;
386 }
387 }
388
389 @Override
390 public boolean performRollover(String sourceTermId, String targetTermId, CourseOfferingRolloverManagementForm form) {
391 CourseOfferingSetService socService = _getSocService();
392 try {
393 ContextInfo context = ContextUtils.getContextInfo();
394 List<String> socIds = socService.getSocIdsByTerm(sourceTermId, context);
395 SocInfo socInfo = _getUniqueMainSoc(socIds);
396 if (socInfo == null) {
397 GlobalVariables.getMessageMap().putError("sourceTermCode", "error.rollover.sourceTerm.noSoc");
398 } else {
399 String sourceSocId = socInfo.getId();
400 List<String> options = new ArrayList<String>();
401
402
403 options.add(CourseOfferingSetServiceConstants.LOG_SUCCESSES_OPTION_KEY);
404 SocInfo result = socService.rolloverSoc(sourceSocId, targetTermId, options, context);
405 return true;
406 }
407 } catch (Exception e) {
408 System.err.println("--------- rollover exception in performRollover [START]");
409 e.printStackTrace();
410 System.err.println("--------- rollover exception in performRollover [END]");
411 form.setStatusField("performRollover: Exception thrown: " + e.getMessage());
412 }
413 return false;
414 }
415
416 @Override
417 public void deleteTargetTerm(String targetTermId, DeleteTargetTermForm form) {
418
419 DeleteTargetTermRolloverRunner runner = new DeleteTargetTermRolloverRunner();
420 runner.setSocService(_getSocService());
421 runner.setCoService(_getCourseOfferingService());
422 runner.setTermId(targetTermId);
423 runner.run();
424 }
425
426 @Override
427 public boolean cleanSourceTerm(String sourceTermId, CourseOfferingRolloverManagementForm form) {
428
429 CourseOfferingSetService socService = _getSocService();
430 CourseOfferingService coService = _getCourseOfferingService();
431
432 try {
433
434 List<String> coIds = coService.getCourseOfferingIdsByTerm(sourceTermId, Boolean.TRUE, new ContextInfo());
435 if (coIds != null) {
436 if (coIds.size() > 3) {
437
438 form.setStatusField("Too many course offerings to delete: " + coIds.size());
439 return false;
440 } else {
441
442 for (String coId : coIds) {
443 List<FormatOfferingInfo> foInfos =
444 coService.getFormatOfferingsByCourseOffering(coId, new ContextInfo());
445 for (FormatOfferingInfo foInfo: foInfos) {
446 String foId = foInfo.getId();
447
448 List<ActivityOfferingInfo> aoInfos =
449 coService.getActivityOfferingsByFormatOffering(foId, new ContextInfo());
450 for (ActivityOfferingInfo aoInfo: aoInfos) {
451 coService.deleteActivityOffering(aoInfo.getId(), new ContextInfo());
452 }
453
454 coService.deleteFormatOffering(foInfo.getId(), new ContextInfo());
455 }
456 coService.deleteCourseOffering(coId, new ContextInfo());
457 }
458 }
459 }
460
461 List<String> socIds = socService.getSocIdsByTerm(sourceTermId, new ContextInfo());
462 if (socIds != null) {
463 ContextInfo contextInfo = new ContextInfo();
464 for (String sourceSocId: socIds) {
465 SocInfo socInfo = socService.getSoc(sourceSocId, new ContextInfo());
466 if (socInfo.getTypeKey().equals(CourseOfferingSetServiceConstants.MAIN_SOC_TYPE_KEY)) {
467
468 List<String> resultIds = socService.getSocRolloverResultIdsBySourceSoc(sourceSocId, contextInfo);
469 if (resultIds != null) {
470 for (String resultId: resultIds) {
471 List<SocRolloverResultItemInfo> items = socService.getSocRolloverResultItemsByResultId(resultId, contextInfo);
472
473 for (SocRolloverResultItemInfo item: items) {
474 socService.deleteSocRolloverResultItem(item.getId(), contextInfo);
475 }
476
477 socService.deleteSocRolloverResult(resultId, contextInfo);
478 }
479 }
480
481 socService.deleteSoc(sourceSocId, contextInfo);
482 }
483 }
484 }
485 return true;
486 } catch (Exception ex) {
487 form.setStatusField("Exception in cleanTargetTerm");
488 return false;
489 }
490 }
491
492 private List<SocInfo> _getSocsByTerm(String termId) {
493 CourseOfferingSetService socService = _getSocService();
494 try {
495 List<String> socIds = socService.getSocIdsByTerm(termId, new ContextInfo());
496 List<SocInfo> socInfos = new ArrayList<SocInfo>();
497 for (String id: socIds) {
498 SocInfo info = socService.getSoc(id, new ContextInfo());
499 socInfos.add(info);
500 }
501 return socInfos;
502 } catch (Exception e) {
503 return null;
504 }
505 }
506
507 private AcademicCalendarService _getAcalService() {
508 if (acalService == null) {
509 acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE,
510 AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
511 }
512 return acalService;
513 }
514
515
516 @Override
517 public List<SocRolloverResultInfo> findRolloverByTerm(String termId) throws Exception{
518 List<SocRolloverResultInfo> socRolloverResultInfos = new ArrayList<SocRolloverResultInfo>();
519 QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
520 qbcBuilder.setPredicates(PredicateFactory.equal("targetTermId", termId));
521 ContextInfo contextInfo = new ContextInfo();
522 QueryByCriteria criteria = qbcBuilder.build();
523 socRolloverResultInfos = _getSocService().searchForSocRolloverResults(criteria, contextInfo);
524 return socRolloverResultInfos;
525 }
526
527 @Override
528 public String formatDate(Date date) {
529 SimpleDateFormat format = new SimpleDateFormat("EEE, MMMMM d, yyyy");
530 String startDateStr = format.format(date);
531 return startDateStr;
532 }
533
534 @Override
535 public String formatDateAndTime(Date date) {
536 if (date == null) {
537 return "";
538 }
539 SimpleDateFormat format = new SimpleDateFormat("MMMMM d, yyyy, h:mm a");
540 String startDateStr = format.format(date);
541 return startDateStr;
542 }
543
544 @Override
545 public String getTermDesc(String termId) {
546 try {
547 TermInfo termInfo = _getAcalService().getTerm(termId, new ContextInfo());
548 return termInfo.getDescr().getPlain();
549 } catch (Exception e) {
550 return "NO TERM DATA";
551 }
552 }
553
554 private CourseOfferingService _getCourseOfferingService() {
555 if (coService == null) {
556 coService = (CourseOfferingService) GlobalResourceLoader.getService(new QName(CourseOfferingServiceConstants.NAMESPACE,
557 CourseOfferingServiceConstants.SERVICE_NAME_LOCAL_PART));
558 }
559 return coService;
560 }
561
562 private CourseOfferingSetService _getSocService() {
563 if (socService == null) {
564 socService = (CourseOfferingSetService) GlobalResourceLoader.getService(new QName(CourseOfferingSetServiceConstants.NAMESPACE,
565 CourseOfferingSetServiceConstants.SERVICE_NAME_LOCAL_PART));
566 }
567 return socService;
568 }
569
570 private CourseService _getCourseService() {
571 if (courseService == null) {
572 Object o = GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + "course",
573 "CourseService"));
574 courseService = (CourseService) o;
575 }
576 return courseService;
577 }
578 }