View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by vgadiyak on 5/29/12
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   * This class //TODO ...
37   *
38   * @author Kuali Student Team
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              // Only perform validateDuplicateSuffix check when CO code suffix part is changed, the code itself is readOnly and can't be modified at all.
55              // also notice a problem: the suffix of a CO from OldMaintainableObject (the DB reference dataset) could be null
56              // while even we didn't modify suffix in edit CO page, the suffix value in NewMaintainableObject became an empty string
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          // Catalog course code is case INSENSITIVE, but the suffix is case SENSITIVE
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  }