View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.enrollment.class1.krms.builder;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20  import org.kuali.rice.krad.util.GlobalVariables;
21  import org.kuali.rice.krms.util.KRMSConstants;
22  import org.kuali.rice.krms.util.PropositionTreeUtil;
23  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
24  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
25  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
26  import org.kuali.student.lum.lu.ui.krms.builder.CourseComponentBuilder;
27  import org.kuali.student.lum.lu.ui.krms.dto.LUPropositionEditor;
28  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
29  import org.kuali.student.r2.common.util.ContextUtils;
30  import org.kuali.student.r2.core.acal.dto.TermInfo;
31  import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
32  import org.kuali.student.r2.core.atp.service.AtpService;
33  import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
34  import org.kuali.student.r2.core.constants.KSKRMSServiceConstants;
35  
36  import javax.xml.namespace.QName;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  /**
42   * @author Kuali Student Team
43   */
44  public class EnrolCourseComponentBuilder extends CourseComponentBuilder {
45  
46      private final static Logger LOG = Logger.getLogger(EnrolCourseComponentBuilder.class);
47      private transient AtpService atpService;
48      private transient CourseOfferingService courseOfferingService;
49      private AcademicCalendarService acalService = null;
50  
51      @Override
52      public Map<String, String> buildTermParameters(LUPropositionEditor propositionEditor) {
53          Map<String, String> termParameters = new HashMap<String, String>();
54          if (propositionEditor.getCourseInfo() != null) {
55              termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_CLU_KEY, propositionEditor.getCourseInfo().getVersion().getVersionIndId());
56          }
57  
58          if (propositionEditor.getTermInfo() != null) {
59              termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_TERM_KEY, propositionEditor.getTermInfo().getId());
60              termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_TERMCODE_KEY, propositionEditor.getTermCode());
61              String propName = PropositionTreeUtil.getBindingPath(propositionEditor, "termCode");
62              loadCourseOfferingsByTermAndCourseCode(propositionEditor, propName);
63          }
64  
65          if (propositionEditor.getTermInfo2() != null) {
66              termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_TERM2_KEY, propositionEditor.getTermInfo2().getId());
67              termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_TERMCODE2_KEY, propositionEditor.getTermCode2());
68              String propName = PropositionTreeUtil.getBindingPath(propositionEditor, "termCode2");
69              loadCourseOfferingsByTermAndCourseCode(propositionEditor,propName );
70          }
71  
72          return termParameters;
73      }
74  
75      /**
76       * Method used to
77       * find THE course based on termId and courseCode. If find more than one CO or don't find
78       * any CO, log and report an error message.
79       *
80       * @param propositionEditor
81       * @param propName
82       */
83      public void loadCourseOfferingsByTermAndCourseCode(LUPropositionEditor propositionEditor, String propName ) {
84  
85          boolean foundPriorTerm = true;
86          boolean foundAsOfTerm = true;
87          boolean foundBetweenTerm = true;
88          List<CourseOfferingInfo> courseOfferings;
89          try {
90              courseOfferings = this.getCourseOfferingService().getCourseOfferingsByCourse(propositionEditor.getCourseInfo().getId(), ContextUtils.createDefaultContextInfo());
91  
92              for (CourseOfferingInfo courseOffering : courseOfferings) {
93  
94                  TermInfo term;
95                  term = this.getAcalService().getTerm(courseOffering.getTermId(), ContextUtils.createDefaultContextInfo());
96  
97                  if (propositionEditor.getType().equals(KSKRMSServiceConstants.PROPOSITION_TYPE_SUCCESS_COMPL_PRIOR_TO_TERM)) {
98                      foundPriorTerm = false;
99                      if ((term.getEndDate().before(propositionEditor.getTermInfo().getEndDate()))) {
100                         foundPriorTerm = true;
101                         break;
102                     }
103                 } else if (propositionEditor.getType().equals(KSKRMSServiceConstants.PROPOSITION_TYPE_SUCCESS_COMPL_COURSE_AS_OF_TERM)) {
104                     foundAsOfTerm = false;
105                     if ((term.getStartDate().after(propositionEditor.getTermInfo().getStartDate()))) {
106 
107                         foundAsOfTerm = true;
108                         break;
109                     }
110                 } else if (propositionEditor.getType().equals(KSKRMSServiceConstants.PROPOSITION_TYPE_SUCCESS_COMPL_COURSE_BETWEEN_TERMS)) {
111                     foundBetweenTerm = false;
112                     if ((term.getStartDate().before(propositionEditor.getTermInfo().getStartDate())) || (term.getStartDate().after(propositionEditor.getTermInfo2().getEndDate()))) {
113                         foundBetweenTerm = true;
114                         break;
115                     }
116                 }
117 
118             }
119 
120         } catch (DoesNotExistException e) {
121             throw new RuntimeException("CO does not exist");
122         } catch (Exception e) {
123             throw new RuntimeException(e);
124         }
125         if (!foundPriorTerm) {
126             GlobalVariables.getMessageMap().putError(propName, KRMSConstants.KSKRMS_MSG_ERROR_NO_PRIOR_COURSEOFFERING_TERM_FOUND, "Course", propositionEditor.getCourseInfo().getCode(), propositionEditor.getTermInfo().getName());
127         }
128         if (!foundAsOfTerm) {
129             GlobalVariables.getMessageMap().putError(propName, KRMSConstants.KSKRMS_MSG_ERROR_NO_ASOF_COURSEOFFERING_TERM_FOUND, "Course", propositionEditor.getCourseInfo().getCode(), propositionEditor.getTermInfo().getName());
130         }
131         if (!foundBetweenTerm) {
132             GlobalVariables.getMessageMap().putError(propName, KRMSConstants.KSKRMS_MSG_ERROR_NO_BETWEEN_COURSEOFFERING_TERM_FOUND, "Course", propositionEditor.getCourseInfo().getCode(), propositionEditor.getTermInfo().getName(), propositionEditor.getTermInfo2().getName());
133         }
134     }
135 
136     private AtpService getAtpService() {
137         if (atpService == null) {
138             atpService = CourseOfferingResourceLoader.loadAtpService();
139         }
140         return atpService;
141     }
142 
143     private CourseOfferingService getCourseOfferingService() {
144         if (courseOfferingService == null) {
145             courseOfferingService = CourseOfferingResourceLoader.loadCourseOfferingService();
146         }
147         return courseOfferingService;
148     }
149 
150     private AcademicCalendarService getAcalService() {
151         if (acalService == null) {
152             acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE,
153                     AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
154         }
155         return acalService;
156     }
157 
158 }