1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.student.enrollment.class2.courseoffering.rule;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.api.util.RiceKeyConstants;
21 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
22 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
23 import org.kuali.rice.krad.util.GlobalVariables;
24 import org.kuali.rice.krad.util.KRADConstants;
25 import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingCreateWrapper;
26 import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingEditWrapper;
27 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingConstants;
28 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
29 import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
30 import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
31 import org.kuali.student.r2.common.util.ContextUtils;
32
33 import java.util.List;
34
35
36
37
38
39
40 public class CourseOfferingEditRule extends MaintenanceDocumentRuleBase {
41
42 private CourseOfferingService courseOfferingService;
43
44 private final static String EXISTING_CO_CODE_FOUND_ERROR = "That Course Offering Code is already in use. Please enter a different, unique Course Offering Code for ";
45
46 @Override
47 protected boolean processGlobalSaveDocumentBusinessRules(MaintenanceDocument document) {
48 boolean valid = true;
49
50 if (document.getNewMaintainableObject().getDataObject() instanceof CourseOfferingEditWrapper){
51 CourseOfferingEditWrapper newCOWrapper = (CourseOfferingEditWrapper)document.getNewMaintainableObject().getDataObject();
52 CourseOfferingEditWrapper oldCOWrapper = (CourseOfferingEditWrapper)document.getOldMaintainableObject().getDataObject();
53
54
55
56
57 String newSuffix = newCOWrapper.getCoInfo().getCourseNumberSuffix();
58 String oldSuffix = oldCOWrapper.getCoInfo().getCourseNumberSuffix();
59 if ((oldSuffix == null || oldSuffix.isEmpty()) &&
60 (newSuffix == null || newSuffix.isEmpty())) {
61 return valid;
62 }else if ((newSuffix != null) && !newSuffix.equals(oldSuffix) ) {
63 validateDuplicateSuffix(newCOWrapper);
64 }
65 }
66
67 return valid;
68 }
69
70
71 protected boolean validateDuplicateSuffix(CourseOfferingEditWrapper coWrapper){
72
73 String newCoCode = (coWrapper.getCourse().getCode().toUpperCase()) + coWrapper.getCoInfo().getCourseNumberSuffix();
74
75 try {
76 List<CourseOfferingInfo> wrapperList = getCourseOfferingService().getCourseOfferingsByCourseAndTerm(coWrapper.getCourse().getId(), coWrapper.getCoInfo().getTermId(), ContextUtils.createDefaultContextInfo());
77 for (CourseOfferingInfo courseOfferingInfo : wrapperList) {
78
79 if (StringUtils.equals(newCoCode, courseOfferingInfo.getCourseOfferingCode())) {
80 StringBuilder sb = new StringBuilder(EXISTING_CO_CODE_FOUND_ERROR);
81 sb.append(coWrapper.getCourse().getCode());
82 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, sb.toString());
83 return false;
84 }
85 }
86 } catch (Exception e) {
87 throw new RuntimeException(e);
88 }
89 return true;
90 }
91
92 protected CourseOfferingService getCourseOfferingService() {
93 if (courseOfferingService == null) {
94 courseOfferingService = CourseOfferingResourceLoader.loadCourseOfferingService();
95 }
96 return courseOfferingService;
97 }
98 }