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.common.util.krms.proposition;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
20  import org.kuali.rice.krms.api.engine.ResultEvent;
21  import org.kuali.rice.krms.api.engine.Term;
22  import org.kuali.rice.krms.framework.engine.PropositionResult;
23  import org.kuali.rice.krms.framework.engine.result.BasicResult;
24  import org.kuali.student.common.util.krms.RulesExecutionConstants;
25  
26  import java.util.Collection;
27  import java.util.Collections;
28  
29  /**
30   * Proposition class that checks for completion of no more than N of the given courses
31   */
32  public class MaxCourseCompletionProposition extends AbstractLeafProposition {
33  
34      private final String courseSetId;
35  
36      private final String singleCourseId;
37  
38      private final Integer maxCoursesCompleted;
39  
40      public MaxCourseCompletionProposition(String singleCourseId) {
41          this.courseSetId = null;
42          this.singleCourseId = singleCourseId;
43          this.maxCoursesCompleted = 0;
44      }
45  
46      public MaxCourseCompletionProposition(String courseSetId, Integer maxCourses) {
47          this.courseSetId = courseSetId;
48          this.singleCourseId = null;
49          this.maxCoursesCompleted = maxCourses;
50      }
51  
52      @Override
53      public PropositionResult evaluate(ExecutionEnvironment environment) {
54          Collection<String> completedCourses = environment.resolveTerm(RulesExecutionConstants.completedCourseIdsTerm, this);
55          Collection<String> coursesToCheck;
56  
57          if(singleCourseId == null) {
58              Term term = new Term(RulesExecutionConstants.COURSE_SET_TERM_NAME, Collections.singletonMap(RulesExecutionConstants.COURSE_SET_ID_TERM_PROPERTY, courseSetId));
59              coursesToCheck = environment.resolveTerm(term, this);
60          }
61          else {
62              coursesToCheck = Collections.singleton(singleCourseId);
63          }
64  
65  
66          PropositionResult result = new PropositionResult(CollectionUtils.intersection(completedCourses, coursesToCheck).size() <= maxCoursesCompleted);
67  
68          environment.getEngineResults().addResult(new BasicResult(ResultEvent.PROPOSITION_EVALUATED, this, environment, result.getResult()));
69  
70          return result;
71      }
72  }