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.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.courseregistration.dto.CourseRegistrationInfo;
21  import org.kuali.student.enrollment.courseregistration.service.CourseRegistrationService;
22  import org.kuali.student.krms.util.KSKRMSExecutionConstants;
23  import org.kuali.student.r2.common.dto.ContextInfo;
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  
29  import java.util.Collections;
30  import java.util.HashSet;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.Set;
34  
35  public class EnrolledCourseTermResolver implements TermResolver<Boolean> {
36  
37      private CourseRegistrationService courseRegistrationService;
38  
39      private final static Set<String> prerequisites = new HashSet<String>(2);
40  
41      static {
42          prerequisites.add(KSKRMSExecutionConstants.PERSON_ID_TERM_PROPERTY);
43          prerequisites.add(KSKRMSExecutionConstants.CONTEXT_INFO_TERM_NAME);
44      }
45  
46      public CourseRegistrationService getCourseRegistrationService() {
47          return courseRegistrationService;
48      }
49  
50      public void setCourseRegistrationService(CourseRegistrationService courseRegistrationService) {
51          this.courseRegistrationService = courseRegistrationService;
52      }
53  
54      @Override
55      public Set<String> getPrerequisites() {
56          return prerequisites;
57      }
58  
59      @Override
60      public String getOutput() {
61          return KSKRMSExecutionConstants.ENROLLED_COURSE_TERM_NAME;
62      }
63  
64      @Override
65      public Set<String> getParameterNames() {
66          return Collections.singleton(KSKRMSExecutionConstants.COURSE_ID_TERM_PROPERTY);
67      }
68  
69      @Override
70      public int getCost() {
71          // TODO Analyze, though probably not much to check here
72          return 1;
73      }
74  
75      @Override
76      public Boolean resolve(Map<String, Object> resolvedPrereqs, Map<String, String> parameters) throws TermResolutionException {
77  
78          String courseOfferingIds = parameters.get(KSKRMSExecutionConstants.COURSE_ID_TERM_PROPERTY);
79          Boolean result = false;
80  
81          courseOfferingIds = courseOfferingIds.trim();
82          String[] courseOfferingId = courseOfferingIds.split(",");
83          int testNumber = 0;
84  
85          List<CourseRegistrationInfo> registrationInfos = this.getCourseRegistrationsForStudent(resolvedPrereqs, parameters);
86  
87          if(courseOfferingIds.contains(",")) {
88              for(CourseRegistrationInfo cri : registrationInfos) {
89                  for(String cc : courseOfferingId) {
90                      if(cc.equals(cri.getCourseOfferingId())){
91                          testNumber++;
92                      }
93                  }
94              }
95              if(courseOfferingId.length == testNumber){
96                  result = true;
97              }
98          } else {
99              for(CourseRegistrationInfo temp : registrationInfos) {
100                 if(temp.getCourseOfferingId().equals(courseOfferingIds)){
101                     result = true;
102                 }
103             }
104         }
105 
106         return result;
107     }
108 
109     private List<CourseRegistrationInfo> getCourseRegistrationsForStudent(Map<String, Object> resolvedPrereqs, Map<String, String> parameters){
110 
111         ContextInfo context = (ContextInfo) resolvedPrereqs.get(KSKRMSExecutionConstants.CONTEXT_INFO_TERM_NAME);
112         String personId = (String) resolvedPrereqs.get(KSKRMSExecutionConstants.PERSON_ID_TERM_PROPERTY);
113 
114         List<CourseRegistrationInfo> registrationInfos = null;
115         try {
116             registrationInfos = this.getCourseRegistrationService().getCourseRegistrationsByStudent(personId, context);
117         } catch (InvalidParameterException e) {
118             throw createTermResolutionException(e.getMessage(), parameters);
119         } catch (MissingParameterException e) {
120             throw createTermResolutionException(e.getMessage(), parameters);
121         } catch (OperationFailedException e) {
122             throw createTermResolutionException(e.getMessage(), parameters);
123         } catch (PermissionDeniedException e) {
124             throw createTermResolutionException(e.getMessage(), parameters);
125         }
126 
127         return registrationInfos;
128     }
129 
130     private TermResolutionException createTermResolutionException(String message, Map<String, String> parameters){
131         return new TermResolutionException(message, this, parameters);
132     }
133 }