View Javadoc

1   /*
2    * Copyright 2012 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 1.0 (the
5    * "License"); you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl1.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.kuali.student.r2.common.krms.data;
17  
18  import java.util.ArrayList;
19  import java.util.Date;
20  import java.util.List;
21  
22  import javax.annotation.Resource;
23  
24  import org.joda.time.DateTime;
25  import org.kuali.student.common.test.mock.data.AbstractMockServicesAwareDataLoader;
26  import org.kuali.student.enrollment.academicrecord.dto.StudentCourseRecordInfo;
27  import org.kuali.student.enrollment.academicrecord.dto.StudentProgramRecordInfo;
28  import org.kuali.student.enrollment.class2.academicrecord.service.impl.AcademicRecordServiceClass2MockImpl;
29  import org.kuali.student.enrollment.class2.courseoffering.service.impl.CourseOfferingServiceTestDataUtils;
30  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
31  import org.kuali.student.enrollment.courseoffering.dto.RegistrationGroupInfo;
32  import org.kuali.student.enrollment.courseoffering.infc.CourseOffering;
33  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
34  import org.kuali.student.enrollment.courseregistration.dto.RegistrationRequestInfo;
35  import org.kuali.student.enrollment.courseregistration.dto.RegistrationRequestItemInfo;
36  import org.kuali.student.enrollment.courseregistration.service.CourseRegistrationService;
37  import org.kuali.student.r2.common.dto.ContextInfo;
38  import org.kuali.student.r2.common.exceptions.AlreadyExistsException;
39  import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
40  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
41  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
42  import org.kuali.student.r2.common.exceptions.MissingParameterException;
43  import org.kuali.student.r2.common.exceptions.OperationFailedException;
44  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
45  import org.kuali.student.r2.common.exceptions.ReadOnlyException;
46  import org.kuali.student.r2.common.util.constants.LprServiceConstants;
47  import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
48  import org.kuali.student.r2.core.atp.dto.AtpInfo;
49  import org.kuali.student.r2.core.atp.service.AtpService;
50  import org.kuali.student.r2.core.constants.AtpServiceConstants;
51  import org.kuali.student.r2.lum.clu.dto.CluInfo;
52  import org.kuali.student.r2.lum.clu.service.CluService;
53  import org.kuali.student.r2.lum.course.dto.CourseInfo;
54  import org.kuali.student.r2.lum.course.service.CourseService;
55  import org.kuali.student.r2.lum.lrc.dto.ResultValuesGroupInfo;
56  import org.kuali.student.r2.lum.util.constants.LrcServiceConstants;
57  import org.slf4j.Logger;
58  import org.slf4j.LoggerFactory;
59  
60  /**
61   *
62   * @author Kuali Student Team 
63   *
64   */
65  public class KRMSEnrollmentEligibilityDataLoader extends AbstractMockServicesAwareDataLoader {
66      
67      private static final Logger log = LoggerFactory
68              .getLogger(KRMSEnrollmentEligibilityDataLoader.class);
69  
70      @Resource(name = "cluService")
71      private CluService cluService;
72  
73      @Resource(name = "courseService")
74      private CourseService courseService;
75  
76      @Resource(name = "courseOfferingService")
77      private CourseOfferingService courseOfferingService;
78  
79      @Resource
80      private AcademicRecordServiceClass2MockImpl recordService;
81  
82      @Resource
83      private CourseRegistrationService registrationService;
84  
85      @Resource
86      private AtpService atpService;
87  
88      public static final Date START_WINTER_TERM_DATE = new DateTime().withDate(2012, 1, 1).toDate();
89      public static final Date END_WINTER_TERM_DATE = new DateTime().withDate(2012, 3, 31).toDate();
90      
91      public static final Date START_SPRING_TERM_DATE = new DateTime().withDate(2012, 4, 1).toDate();
92      public static final Date END_SPRING_TERM_DATE = new DateTime().withDate(2012, 6, 30).toDate();
93  
94      public static final Date START_SUMMER_TERM_DATE = new DateTime().withDate(2012, 7, 1).toDate();
95      public static final Date END_SUMMER_TERM_DATE = new DateTime().withDate(2012, 9, 30).toDate();
96      
97      public static final Date START_FALL_TERM_DATE = new DateTime().withDate(2012, 10, 1).toDate();
98      public static final Date END_FALL_TERM_DATE = new DateTime().withDate(2012, 12, 31).toDate();
99      
100     public static final String STUDENT_ONE_ID = "student1";
101     public static final String STUDENT_TWO_ID = "student2";
102     public static final String STUDENT_THREE_ID = "student3";
103     
104     public static final String FAKE_COURSE_ID = "course1";
105     public static final String FAKE_COURSE2_ID = "course2";
106     public static final String FAKE_COURSE3_ID = "course3";
107 
108     private AtpInfo winterAtpInfo;
109     private AtpInfo springAtpInfo;
110     private AtpInfo summerAtpInfo;
111     private AtpInfo fallAtpInfo;
112 
113     public ContextInfo contextInfo = null;
114     
115     
116     /**
117      * 
118      */
119     public KRMSEnrollmentEligibilityDataLoader() {
120         super();
121     }
122 
123     /* (non-Javadoc)
124      * @see org.kuali.student.common.test.mock.data.AbstractMockServicesAwareDataLoader#initializeData()
125      */
126     @Override
127     protected void initializeData() throws Exception {
128 
129         winterAtpInfo = atpService.createAtp(AtpServiceConstants.ATP_WINTER_TYPE_KEY, createTerm (AtpServiceConstants.ATP_WINTER_TYPE_KEY,
130                 START_WINTER_TERM_DATE, END_WINTER_TERM_DATE, "Winter 2012"), context);
131         springAtpInfo = atpService.createAtp(AtpServiceConstants.ATP_SPRING_TYPE_KEY, createTerm (AtpServiceConstants.ATP_SPRING_TYPE_KEY,
132                 START_SPRING_TERM_DATE, END_SPRING_TERM_DATE, "Spring 2012"), context);
133         summerAtpInfo = atpService.createAtp(AtpServiceConstants.ATP_SUMMER_TYPE_KEY, createTerm (AtpServiceConstants.ATP_SUMMER_TYPE_KEY,
134                 START_SUMMER_TERM_DATE, END_SUMMER_TERM_DATE, "Summer 2012"), context);
135         fallAtpInfo = atpService.createAtp(AtpServiceConstants.ATP_FALL_TYPE_KEY, createTerm (AtpServiceConstants.ATP_FALL_TYPE_KEY,
136                 START_FALL_TERM_DATE, END_FALL_TERM_DATE, "Fall 2012"), context);
137     }
138     
139     public String getWinterTermId() {
140         return winterAtpInfo.getId();
141     }
142 
143     public String getSpringTermId() {
144         return springAtpInfo.getId();
145     }
146 
147     public String getSummerTermId() {
148         return summerAtpInfo.getId();
149     }
150     
151     public String getFallTermId() {
152         return fallAtpInfo.getId();
153     }
154     
155     /**
156      * Helper to create a new StudentCourseRecordInfo object built using the provided details.
157      * 
158      * @param studentId the student that took the course
159      * @param termId the term when the course was taken
160      * @param courseCode the course code of the course that was taken
161      * @param courseTitle the title of the course
162      * @return the non-saved new course record built using the provided details
163      * @throws DoesNotExistException the term does not exist
164      * @throws InvalidParameterException the
165      * @throws MissingParameterException
166      * @throws OperationFailedException
167      * @throws PermissionDeniedException
168      */
169     public StudentCourseRecordInfo createStudentCourseRecord(String studentId, String termId, String courseCode, String courseTitle,
170                                                              String gradeValue, String gradeScaleKey) throws DoesNotExistException, OperationFailedException {
171         
172         StudentCourseRecordInfo courseRecord = new StudentCourseRecordInfo();
173         
174         courseRecord.setCourseRegistrationId("courseRegistrationId");
175         courseRecord.setPersonId(studentId);
176         courseRecord.setCourseTitle(courseTitle);
177         courseRecord.setCourseCode(courseCode);
178         courseRecord.setActivityCode("dummyActivityCode");
179         
180         AtpInfo term;
181         try {
182             term = atpService.getAtp(termId, context);
183         } catch (Exception e) {
184            throw new OperationFailedException("failed to get atp with id = " + termId, e);
185         } 
186         
187         // note this is not the term id
188         courseRecord.setTermName(term.getName());
189         courseRecord.setCourseBeginDate(term.getStartDate());
190         courseRecord.setCourseEndDate(term.getEndDate());
191         
192         courseRecord.setAssignedGradeValue(gradeValue);
193         courseRecord.setAssignedGradeScaleKey(gradeScaleKey);
194         courseRecord.setAdministrativeGradeValue(gradeValue);
195         courseRecord.setAdministrativeGradeScaleKey(gradeScaleKey);
196         courseRecord.setCalculatedGradeValue("3.0");
197         courseRecord.setCalculatedGradeScaleKey("1");
198         courseRecord.setCreditsForGPA("3");
199         courseRecord.setCreditsEarned("3");
200         
201         return courseRecord;
202         
203     }
204 
205     /**
206      * Helper to create a new StudentProgramRecordInfo object built using the provided details.
207      *
208      * @param studentId the student that completed the program
209      * @param program the program
210      * @return the non-saved new course record built using the provided details
211      * @throws DoesNotExistException the term does not exist
212      * @throws InvalidParameterException the
213      * @throws MissingParameterException
214      * @throws OperationFailedException
215      * @throws PermissionDeniedException
216      */
217     public StudentProgramRecordInfo createStudentProgramRecord(String studentId, CluInfo program) throws DoesNotExistException, OperationFailedException {
218 
219         StudentProgramRecordInfo programRecord = new StudentProgramRecordInfo();
220 
221         programRecord.setProgramId(program.getId());
222         programRecord.setProgramCode(program.getOfficialIdentifier().getCode());
223         programRecord.setProgramTitle(program.getOfficialIdentifier().getLongName());
224 
225         return programRecord;
226 
227     }
228 
229     /**
230      * Store a new CourseRecord for the student in the term given.
231      * @param studentId
232      * @param termId
233      * @param courseRecord
234      * @throws DoesNotExistException if the term does not exist.
235      * @throws OperationFailedException  if an exception occurs that prevents the execution of the method.
236      */
237     public void storeStudentCourseRecord (String studentId, String termId, String courseId, StudentCourseRecordInfo courseRecord) throws DoesNotExistException, OperationFailedException {
238         
239         try {
240             
241             atpService.getAtp(termId, context);
242             
243         } catch (Exception e) {
244             
245             if (e instanceof DoesNotExistException)
246                 throw (DoesNotExistException)e;
247             else
248                 throw new OperationFailedException("", e);
249         }
250         
251         recordService.storeStudentCourseRecord(studentId, termId, courseId, courseRecord);
252     }
253 
254     /**
255      * Store a new ProgramRecord for the student in the term given.
256      * @param studentId
257      * @param programId
258      * @param programRecord
259      * @throws DoesNotExistException if the term does not exist.
260      * @throws OperationFailedException  if an exception occurs that prevents the execution of the method.
261      */
262     public void storeStudentProgramRecord (String studentId, String programId, StudentProgramRecordInfo programRecord) throws DoesNotExistException, OperationFailedException {
263         recordService.storeStudentProgramRecord(studentId, programId, programRecord);
264     }
265 
266     private AtpInfo createTerm(String atpSpringTypeKey, Date startDate, Date endDate, String name) {
267         
268         AtpInfo atp = new AtpInfo();
269         
270         atp.setTypeKey(atpSpringTypeKey);
271         
272         atp.setName(name);
273         
274         atp.setStartDate(startDate);
275         atp.setEndDate(endDate);
276         
277         return atp;
278     }
279 
280     /**
281      * Helper to create a new StudentCourseRecordInfo object built using the provided details.
282      *
283      * @param studentId the student that took the course
284      * @param termId the term when the course was taken
285      * @return the non-saved new course record built using the provided details
286      */
287     public RegistrationRequestInfo createRegistrationRequest(String studentId, String termId) {
288 
289         RegistrationRequestInfo request = new RegistrationRequestInfo();
290         request.setRequestorId(studentId);
291         request.setTermId(termId);
292         request.setTypeKey(LprServiceConstants.LPRTRANS_REGISTER_TYPE_KEY);
293         return request;
294     }
295 
296     /**
297      * Helper to create a new StudentCourseRecordInfo object built using the provided details.
298      *
299      * @param regGroupId the student that took the course
300      * @return the non-saved new course record built using the provided details
301      */
302     public RegistrationRequestItemInfo createRegistrationItem(String studentId, String regGroupId) {
303 
304         RegistrationRequestItemInfo requestItem = new RegistrationRequestItemInfo();
305         requestItem.setStudentId(studentId);
306         requestItem.setNewRegistrationGroupId(regGroupId);
307         requestItem.setCredits("3.0");
308         requestItem.setGradingOptionId(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_LETTER);
309         requestItem.setTypeKey(LprServiceConstants.LPRTRANS_ITEM_ADD_TYPE_KEY);
310         return requestItem;
311     }
312 
313     /**
314      * Store a new CourseRecord for the student in the term given.
315      * @param request
316      * @throws DoesNotExistException if the term does not exist.
317      * @throws OperationFailedException  if an exception occurs that prevents the execution of the method.
318      */
319     public RegistrationRequestInfo persistRegistrationRequest(RegistrationRequestInfo request) throws InvalidParameterException, PermissionDeniedException, OperationFailedException, AlreadyExistsException, MissingParameterException, DoesNotExistException, ReadOnlyException, DataValidationErrorException {
320         return registrationService.createRegistrationRequest(request.getTypeKey(), request, context);
321     }
322 
323     public void submitRegistrationRequest(String requestId) throws InvalidParameterException, PermissionDeniedException, OperationFailedException, AlreadyExistsException, MissingParameterException, DoesNotExistException, ReadOnlyException, DataValidationErrorException {
324         registrationService.submitRegistrationRequest(requestId, context);
325     }
326 
327     public CourseOffering getCourseOffering(String courseId, String termId) throws Exception {
328         CourseInfo course = this.getCourse(courseId);
329         String coId = "CO:" + course.getId() + ":" + termId;
330         CourseOfferingInfo courseOffering = null;
331         try {
332             courseOffering = courseOfferingService.getCourseOffering(coId, contextInfo);
333         } catch (DoesNotExistException dne) {
334             //Create a course offering from the course if it does not exist.
335             courseOffering = CourseOfferingServiceTestDataUtils.createCourseOffering(course, termId);
336             courseOffering.setId(coId);
337             courseOffering = courseOfferingService.createCourseOffering(course.getId(), termId, LuiServiceConstants.COURSE_OFFERING_TYPE_KEY,
338                     courseOffering, new ArrayList<String>(), contextInfo);
339             courseOffering.getUnitsContentOwnerOrgIds().add("ORG1");
340             RegistrationGroupInfo regGroup = new RegistrationGroupInfo();
341             regGroup.setCourseOfferingId(courseOffering.getId());
342             regGroup.setTypeKey("atype");
343             courseOfferingService.createRegistrationGroup(regGroup.getFormatOfferingId(), regGroup.getActivityOfferingClusterId(), regGroup.getTypeKey(), regGroup, contextInfo);
344         }
345         return courseOffering;
346     }
347 
348     public CourseInfo getCourse(String courseId) throws Exception {
349         try {
350             return courseService.getCourse(courseId, contextInfo);
351         } catch (DoesNotExistException dne) {
352             //Create a course from the cluService if it does not exist.
353             CluInfo clu = cluService.getClu(courseId, contextInfo);
354             CourseInfo course = new CourseInfo();
355             course.setId(clu.getId());
356             course.setCode(clu.getOfficialIdentifier().getCode());
357             course.setCourseTitle(clu.getOfficialIdentifier().getLongName());
358             course.setCourseNumberSuffix(clu.getOfficialIdentifier().getSuffixCode());
359             ResultValuesGroupInfo rvg = new ResultValuesGroupInfo();
360             rvg.setKey(LrcServiceConstants.RESULT_GROUP_KEY_KUALI_CREDITTYPE_CREDIT_1_0);
361             course.getCreditOptions().add(rvg);
362 
363             return courseService.createCourse(course, contextInfo);
364         }
365     }
366 
367     public CluInfo getProgram(String programId) throws Exception {
368         return cluService.getClu(programId, contextInfo);
369     }
370 
371     public RegistrationGroupInfo getRegistrationGroup(String courseId, String termId) throws Exception {
372         CourseOffering courseOffering = this.getCourseOffering(courseId, termId);
373         List<RegistrationGroupInfo> regGroups = courseOfferingService.getRegistrationGroupsForCourseOffering(courseOffering.getId(), contextInfo);
374         for (RegistrationGroupInfo regGroup : regGroups) {
375             return regGroup;
376         }
377         return null;
378     }
379 
380     public void setContextInfo(ContextInfo contextInfo) {
381         this.contextInfo = contextInfo;
382     }
383 }