View Javadoc

1   /**
2    * Copyright 2011 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  
16  package org.kuali.student.enrollment.class2.examoffering.krms.termresolver;
17  
18  import org.kuali.rice.krms.api.engine.TermResolutionException;
19  import org.kuali.rice.krms.api.engine.TermResolver;
20  import org.kuali.student.enrollment.courseoffering.infc.CourseOffering;
21  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
22  import org.kuali.student.r2.common.dto.ContextInfo;
23  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
24  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
25  import org.kuali.student.r2.common.exceptions.MissingParameterException;
26  import org.kuali.student.r2.common.exceptions.OperationFailedException;
27  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
28  import org.kuali.student.r2.common.krms.util.KSKRMSExecutionUtil;
29  import org.kuali.student.r2.core.constants.KSKRMSServiceConstants;
30  import org.kuali.student.r2.lum.course.infc.Course;
31  import org.kuali.student.r2.lum.course.service.CourseService;
32  
33  import java.util.Collections;
34  import java.util.HashSet;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Set;
38  
39  /**
40   * Return true if student has an unsubmitted registration request that includes
41   * the given course or if the student is currently enrolled for the given course.
42   *
43   * Example rule statement:
44   * 1) Must be concurrently enrolled in all courses from <courses>
45   *
46   * @author Kuali Student Team
47   */
48  public class MatchingCourseSetTermResolver implements TermResolver<Boolean> {
49  
50      private CourseOfferingService courseOfferingService;
51      private CourseService courseService;
52      private TermResolver<List<String>> cluIdsInCluSetTermResolver;
53  
54      @Override
55      public Set<String> getPrerequisites() {
56          Set<String> prereqs = new HashSet<String>(2);
57          prereqs.add(KSKRMSServiceConstants.TERM_PREREQUISITE_CO_ID);
58          prereqs.add(KSKRMSServiceConstants.TERM_PREREQUISITE_CONTEXTINFO);
59          return Collections.unmodifiableSet(prereqs);
60      }
61  
62      @Override
63      public String getOutput() {
64          return KSKRMSServiceConstants.TERM_RESOLVER_MATCHINGCOURSESET;
65      }
66  
67      @Override
68      public Set<String> getParameterNames() {
69          Set<String> temp = new HashSet<String>(3);
70          temp.add(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_CLUSET_KEY);
71          return Collections.unmodifiableSet(temp);
72      }
73  
74      @Override
75      public int getCost() {
76          return 5;
77      }
78  
79      @Override
80      public Boolean resolve(Map<String, Object> resolvedPrereqs, Map<String, String> parameters) throws TermResolutionException {
81          ContextInfo context = (ContextInfo) resolvedPrereqs.get(KSKRMSServiceConstants.TERM_PREREQUISITE_CONTEXTINFO);
82  
83          try {
84              CourseOffering co = this.retrieveCourseOffering(resolvedPrereqs, context);
85              Course course = this.getCourseService().getCourse(co.getCourseId(), context);
86  
87              List<String> versionIndIds = this.getCluIdsInCluSetTermResolver().resolve(resolvedPrereqs, parameters);
88              if(versionIndIds.contains(course.getVersion().getVersionIndId())){
89                  return true;
90              }
91  
92          } catch (Exception e) {
93              KSKRMSExecutionUtil.convertExceptionsToTermResolutionException(parameters, e, this);
94          }
95  
96          return false;
97      }
98  
99      private CourseOffering retrieveCourseOffering(Map<String, Object> resolvedPrereqs, ContextInfo context)
100             throws PermissionDeniedException, MissingParameterException, InvalidParameterException, OperationFailedException,
101             DoesNotExistException {
102 
103         CourseOffering co = (CourseOffering) resolvedPrereqs.get(KSKRMSServiceConstants.TERM_PREREQUISITE_CO);
104         if(co == null){
105             String coId = (String) resolvedPrereqs.get(KSKRMSServiceConstants.TERM_PREREQUISITE_CO_ID);
106             co = this.getCourseOfferingService().getCourseOffering(coId, context);
107             resolvedPrereqs.put(KSKRMSServiceConstants.TERM_PREREQUISITE_CO, co);
108         }
109 
110         return co;
111     }
112 
113     public CourseOfferingService getCourseOfferingService() {
114         return courseOfferingService;
115     }
116 
117     public void setCourseOfferingService(CourseOfferingService courseOfferingService) {
118         this.courseOfferingService = courseOfferingService;
119     }
120 
121     public CourseService getCourseService() {
122         return courseService;
123     }
124 
125     public void setCourseService(CourseService courseService) {
126         this.courseService = courseService;
127     }
128 
129     public TermResolver<List<String>> getCluIdsInCluSetTermResolver() {
130         return cluIdsInCluSetTermResolver;
131     }
132 
133     public void setCluIdsInCluSetTermResolver(TermResolver<List<String>> cluIdsInCluSetTermResolver) {
134         this.cluIdsInCluSetTermResolver = cluIdsInCluSetTermResolver;
135     }
136 
137 }