View Javadoc
1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not
3    * use this file except in compliance with the License. You may obtain a copy of the License at
4    *
5    * http://www.osedu.org/licenses/ECL-2.0
6    *
7    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
9    * governing permissions and limitations under the License.
10   */
11  package org.kuali.student.enrollment.krms.proposition;
12  
13  import org.joda.time.DateTime;
14  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
15  import org.kuali.rice.krms.framework.engine.PropositionResult;
16  import org.kuali.rice.krms.framework.engine.Rule;
17  import org.kuali.student.common.util.krms.RulesExecutionConstants;
18  import org.kuali.student.common.util.krms.proposition.AbstractLeafProposition;
19  import org.kuali.student.core.process.evaluator.KRMSEvaluator;
20  import org.kuali.student.enrollment.class2.courseoffering.krms.service.RequisitesService;
21  import org.kuali.student.enrollment.courseoffering.infc.RegistrationGroup;
22  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
23  import org.kuali.student.enrollment.courseregistration.dto.RegistrationRequestItemInfo;
24  import org.kuali.student.enrollment.registration.client.service.impl.util.RegistrationValidationResultsUtil;
25  import org.kuali.student.enrollment.registration.engine.util.RegEnginePerformanceUtil;
26  import org.kuali.student.r2.common.dto.ContextInfo;
27  import org.kuali.student.r2.common.dto.ValidationResultInfo;
28  import org.kuali.student.r2.common.infc.ValidationResult;
29  import org.kuali.student.r2.common.util.constants.LprServiceConstants;
30  
31  /**
32   * This proposition evaluates all the instructions associated with a process
33   *
34   * @author nwright
35   */
36  public class RequisitesProposition extends AbstractLeafProposition {
37  
38      private String agendaType;
39      private String ruleType;
40  
41      public RequisitesProposition() {
42      }
43  
44      public RequisitesProposition(String agendaType, String ruleType) {
45          this.agendaType = agendaType;
46          this.ruleType = ruleType;
47      }
48  
49      @Override
50      public PropositionResult evaluate(ExecutionEnvironment environment) {
51  
52          DateTime startTime = new DateTime();
53  
54          ContextInfo contextInfo = environment.resolveTerm(RulesExecutionConstants.CONTEXT_INFO_TERM, this);
55          RegistrationRequestItemInfo requestItem = environment.resolveTerm(RulesExecutionConstants.REGISTRATION_REQUEST_ITEM_TERM, this);
56  
57          // get all the needed services from the execution context
58          // we do this so we can locally cache some services and the cache lives just for the length of krms execution
59          CourseOfferingService courseOfferingService = environment.resolveTerm(RulesExecutionConstants.COURSE_OFFERING_SERVICE_TERM, this);
60          try {
61              RegistrationGroup regGroup = courseOfferingService.getRegistrationGroup(requestItem.getRegistrationGroupId(), contextInfo);
62  
63              if (!executeRuleForRegGroupAndType(environment, regGroup)) {
64                  ValidationResultInfo vr = new ValidationResultInfo();
65                  vr.setLevel(ValidationResult.ErrorLevel.ERROR);
66                  vr.setElement("registrationRequestItems['" + requestItem.getId() + "']");
67                  vr.setMessage(RegistrationValidationResultsUtil.marshallSimpleMessage(LprServiceConstants.LPRTRANS_ITEM_FAILED_ANTI_REQUISITES_MESSAGE_KEY));
68                  return KRMSEvaluator.buildPropositionResult(environment, vr, false, this);
69              }
70  
71          } catch (Exception ex) {
72              return KRMSEvaluator.constructExceptionPropositionResult(environment, ex, this);
73          }
74  
75          DateTime endTime = new DateTime();
76          RegEnginePerformanceUtil.putStatistics(RegEnginePerformanceUtil.OTHER, "RequisitesProposition", startTime, endTime);
77  
78          return KRMSEvaluator.buildPropositionResult(environment, true, this);
79      }
80  
81      private boolean executeRuleForRegGroupAndType(ExecutionEnvironment environment, RegistrationGroup regGroup) {
82  
83          RequisitesService requisitesService = environment.resolveTerm(RulesExecutionConstants.REQUISITES_SERVICE_TERM, this);
84          boolean isCoRuleEvaluated = false;
85          for (String activityOfferingId : regGroup.getActivityOfferingIds()) {
86              // Get the rule for the ao.
87              Rule rule = requisitesService.getRuleForActivityOfferingIdAndType(activityOfferingId, agendaType, ruleType);
88  
89              if ((rule == null) && (!isCoRuleEvaluated)) {
90                  rule = requisitesService.getRuleForCourseOfferingIdAndType(regGroup.getCourseOfferingId(), agendaType, ruleType);
91              }
92  
93              if ((rule != null) && (!rule.evaluate(environment))) {
94                  return false;
95              }
96  
97          }
98  
99          return true;
100 
101     }
102 
103     public String getAgendaType() {
104         return agendaType;
105     }
106 
107     public void setAgendaType(String agendaType) {
108         this.agendaType = agendaType;
109     }
110 
111     public String getRuleType() {
112         return ruleType;
113     }
114 
115     public void setRuleType(String ruleType) {
116         this.ruleType = ruleType;
117     }
118 
119 }