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.framework.engine.PropositionResult;
22  import org.kuali.rice.krms.framework.engine.result.BasicResult;
23  import org.kuali.student.common.util.krms.RulesExecutionConstants;
24  
25  import java.util.Collection;
26  
27  /**
28   * Parent class for all course enrollment propositions
29   *
30   * @author alubbers
31   */
32  public abstract class CourseEnrollmentProposition extends AbstractLeafProposition {
33  
34      protected final boolean checkForAllEnrolled;
35  
36      protected Integer minToEnroll;
37  
38      public CourseEnrollmentProposition(Integer minToEnroll) {
39          this.checkForAllEnrolled = false;
40          this.minToEnroll = minToEnroll;
41      }
42  
43      public CourseEnrollmentProposition() {
44          checkForAllEnrolled = true;
45      }
46  
47  
48      @Override
49      public PropositionResult evaluate(ExecutionEnvironment environment) {
50  
51          Collection<String> enrolledCourses = environment.resolveTerm(RulesExecutionConstants.enrolledCourseIdsTerm, this);
52  
53          Collection<String> requiredCourseIds = getRequiredCourseIds(environment);
54  
55          PropositionResult result = null;
56  
57          if(checkForAllEnrolled) {
58              result = new PropositionResult(enrolledCourses.containsAll(requiredCourseIds));
59          }
60  
61          else {
62              result = new PropositionResult(CollectionUtils.intersection(enrolledCourses, requiredCourseIds).size() >= minToEnroll);
63          }
64  
65          environment.getEngineResults().addResult(new BasicResult(ResultEvent.PROPOSITION_EVALUATED, this, environment, result.getResult()));
66  
67          return result;
68  
69      }
70  
71      protected abstract Collection<String> getRequiredCourseIds(ExecutionEnvironment environment);
72  }