View Javadoc

1   /*
2    * Copyright 2012 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.courseoffering.krms.proposition;
17  
18  import java.util.ArrayList;
19  import java.util.Date;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
25  import org.kuali.rice.krms.api.engine.Term;
26  import org.kuali.rice.krms.framework.engine.Proposition;
27  import org.kuali.rice.krms.framework.engine.PropositionResult;
28  import org.kuali.student.enrollment.academicrecord.dto.StudentCourseRecordInfo;
29  import org.kuali.student.enrollment.academicrecord.service.AcademicRecordService;
30  import org.kuali.student.r2.core.constants.KSKRMSServiceConstants;
31  import org.kuali.student.r2.common.dto.ContextInfo;
32  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
33  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
34  import org.kuali.student.r2.common.exceptions.MissingParameterException;
35  import org.kuali.student.r2.common.exceptions.OperationFailedException;
36  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
37  import org.kuali.student.r2.core.atp.dto.AtpInfo;
38  import org.kuali.student.r2.core.atp.service.AtpService;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  /**
43   * 
44   * @author Kuali Student Team
45   * 
46   * Rule Name: "Must have successfully completed <course> between <term1> and <term2>"
47   * Proposition Type Key: kuali.krms.proposition.type.success.compl.course.between.terms
48   * 
49   * Terms: 
50   * 
51   * kuali.krms.term.type.person.student.specific.id
52   * kuali.krms.term.type.course.specific.id
53   * kuali.krms.term.type.date.start
54   * kuali.krms.term.type.date.end
55   * 
56   * 
57   */
58  public class CompletedCourseBetweenTermsProposition implements Proposition {
59  
60      private static final Logger log = LoggerFactory.getLogger(CompletedCourseBetweenTermsProposition.class);
61      
62  	private Term studentTerm = new Term(KSKRMSServiceConstants.TERM_TYPE_STUDENT_ID);
63  	
64  	private Term courseTerm = new Term(KSKRMSServiceConstants.TERM_TYPE_COURSE_ID);
65  	
66  	private Term startDateTerm = new Term (KSKRMSServiceConstants.TERM_TYPE_START_DATE);
67  	
68  	private Term endDateTerm = new Term (KSKRMSServiceConstants.TERM_TYPE_END_DATE);
69  	
70  	private final AcademicRecordService recordService;
71  	
72  	private final AtpService atpService;
73  	
74  	
75  
76  	/**
77       * @param recordService
78       * @param atpService
79       */
80      public CompletedCourseBetweenTermsProposition(AtpService atpService, AcademicRecordService recordService) {
81          super();
82          this.recordService = recordService;
83          this.atpService = atpService;
84      }
85  
86  
87  
88      /* (non-Javadoc)
89  	 * @see org.kuali.rice.krms.framework.engine.Proposition#evaluate(org.kuali.rice.krms.api.engine.ExecutionEnvironment)
90  	 */
91  	@Override
92  	public PropositionResult evaluate(ExecutionEnvironment environment) {
93  		
94          
95  		String studentId = environment.resolveTerm(studentTerm, this);
96  		
97  		String courseId = environment.resolveTerm(courseTerm, this);
98  		
99  		Date startDateRange = environment.resolveTerm(startDateTerm, this);
100 		
101 		Date endDateRange = environment.resolveTerm(endDateTerm, this);
102 		
103 		ContextInfo contextInfo = new ContextInfo();
104 		contextInfo.setCurrentDate(new Date());
105 		contextInfo.setAuthenticatedPrincipalId("test-krms");
106 		contextInfo.setPrincipalId("test-krms");
107 		
108 		boolean exception = false;
109 		boolean matchedTerm = false;
110 		
111 		try {
112 			List<StudentCourseRecordInfo> completedCourses = recordService.getCompletedCourseRecordsForCourse(studentId, courseId, contextInfo);
113 			
114 			// this is the list of time periods that we want to constrain the results by
115 			List<AtpInfo> atps = atpService.getAtpsByDates(startDateRange, endDateRange, contextInfo);
116 			
117 			Set<String>atpNames = new HashSet<String>();
118 			
119 			
120 			for (AtpInfo atpInfo : atps) {
121 				
122 				// extract the names to make the comparisons easier.
123 				atpNames.add(atpInfo.getName());
124 			}
125 			
126 			
127 			
128 			for (StudentCourseRecordInfo courseRecordInfo : completedCourses) {
129 				
130 				/*
131 				 * We want to find out if any of the completed courses were completed between the terms given.
132 				 */
133 				
134 				if (atpNames.contains(courseRecordInfo.getTermName())) {
135 					// we take the first match we find
136 				    matchedTerm = true;
137 					break;
138 				}
139 					
140 			}
141 			
142 			// intentionally fall through
143 			
144 		} catch (DoesNotExistException e) {
145 		    log.error("exception = ", e);
146 			exception = true;
147 		} catch (InvalidParameterException e) {
148 		    log.error("exception = ", e);
149 			exception = true;
150 		} catch (MissingParameterException e) {
151 		    log.error("exception = ", e);
152 			exception = true;
153 		} catch (OperationFailedException e) {
154 		    log.error("exception = ", e);
155 			exception = true;
156 		} catch (PermissionDeniedException e) {
157 		    log.error("exception = ", e);
158 			exception = true;
159 		
160 		}
161 		
162 		if (exception) {
163 		   
164 			return new PropositionResult(false);
165 		}
166 		else {
167 		    
168 		    if (matchedTerm)
169 		        return new PropositionResult(true);
170 		    else
171 		        return new PropositionResult(false);
172 		}
173 	}
174 
175 	
176 
177 	/* (non-Javadoc)
178 	 * @see org.kuali.rice.krms.framework.engine.Proposition#getChildren()
179 	 */
180 	@Override
181 	public List<Proposition> getChildren() {
182 		// no child propositions
183 		return new ArrayList<Proposition>();
184 	}
185 
186 	/* (non-Javadoc)
187 	 * @see org.kuali.rice.krms.framework.engine.Proposition#isCompound()
188 	 */
189 	@Override
190 	public boolean isCompound() {
191 		// false because we do not embed other propositions
192 		return false;
193 	}
194 
195 }