Coverage Report - org.kuali.student.enrollment.class2.courseoffering.service.decorators.R1CourseServiceHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
R1CourseServiceHelper
0%
0/74
0%
0/14
5.667
 
 1  
 /*
 2  
  * To change this template, choose Tools | Templates
 3  
  * and open the template in the editor.
 4  
  */
 5  
 package org.kuali.student.enrollment.class2.courseoffering.service.decorators;
 6  
 
 7  
 import java.util.ArrayList;
 8  
 import java.util.List;
 9  
 import org.kuali.student.common.versionmanagement.dto.VersionDisplayInfo;
 10  
 import org.kuali.student.enrollment.acal.dto.TermInfo;
 11  
 import org.kuali.student.enrollment.acal.service.AcademicCalendarService;
 12  
 import org.kuali.student.lum.course.dto.CourseInfo;
 13  
 import org.kuali.student.lum.course.service.CourseService;
 14  
 import org.kuali.student.lum.lu.service.LuServiceConstants;
 15  
 import org.kuali.student.r2.common.dto.ContextInfo;
 16  
 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
 17  
 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
 18  
 import org.kuali.student.r2.common.exceptions.MissingParameterException;
 19  
 import org.kuali.student.r2.common.exceptions.OperationFailedException;
 20  
 import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
 21  
 
 22  
 /**
 23  
  *
 24  
  * @author nwright
 25  
  */
 26  
 public class R1CourseServiceHelper {
 27  
 
 28  
     private CourseService courseService;
 29  
     private AcademicCalendarService acalService;
 30  
 
 31  
     public AcademicCalendarService getAcalService() {
 32  0
         return acalService;
 33  
     }
 34  
 
 35  
     public void setAcalService(AcademicCalendarService acalService) {
 36  0
         this.acalService = acalService;
 37  0
     }
 38  
 
 39  
     public CourseService getCourseService() {
 40  0
         return courseService;
 41  
     }
 42  
 
 43  
     public void setCourseService(CourseService courseService) {
 44  0
         this.courseService = courseService;
 45  0
     }
 46  
 
 47  0
     public R1CourseServiceHelper() {
 48  0
     }
 49  
 
 50  0
     public R1CourseServiceHelper(CourseService courseService, AcademicCalendarService acalService) {
 51  0
         this.courseService = courseService;
 52  0
         this.acalService = acalService;
 53  0
     }
 54  
 
 55  
     /**
 56  
      * Get the course 
 57  
      * @param courseId
 58  
      * @return
 59  
      * @throws DoesNotExistException
 60  
      * @throws OperationFailedException 
 61  
      */
 62  
     public CourseInfo getCourse(String courseId) throws DoesNotExistException, OperationFailedException {
 63  0
         CourseInfo course = null;
 64  
         try {
 65  0
             course = courseService.getCourse(courseId);
 66  0
         } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 67  0
             throw new DoesNotExistException("The course does not exist. course: " + courseId, e);
 68  0
         } catch (Exception e) {
 69  0
             throw new OperationFailedException("unxpected trying to get course " + courseId, e);
 70  0
         }
 71  0
         return course;
 72  
     }
 73  
 
 74  
     /**
 75  
      * Get the versions of the course that are valid for the specific term.
 76  
      * Most of the time there is just zero or one but some school's configuration may allow for multiple
 77  
      * @param courseId
 78  
      * @param targetTermId
 79  
      * @param context
 80  
      * @return
 81  
      * @throws DoesNotExistException
 82  
      * @throws OperationFailedException 
 83  
      */
 84  
     public List<CourseInfo> getCoursesForTerm(String courseId, String targetTermId, ContextInfo context)
 85  
             throws DoesNotExistException, OperationFailedException {
 86  0
         List<CourseInfo> list = new ArrayList<CourseInfo>();
 87  0
         CourseInfo sourceCourse = this.getCourse(courseId);
 88  0
         String versionIndCourseId = sourceCourse.getVersionInfo().getVersionIndId();
 89  
         TermInfo targetTerm;
 90  
         try {
 91  0
             targetTerm = acalService.getTerm(targetTermId, context);
 92  0
         } catch (InvalidParameterException ex) {
 93  0
             throw new OperationFailedException("unexpected", ex);
 94  0
         } catch (MissingParameterException ex) {
 95  0
             throw new OperationFailedException("unexpected", ex);
 96  0
         } catch (PermissionDeniedException ex) {
 97  0
             throw new OperationFailedException("unexpected", ex);
 98  0
         }
 99  
         // TODO: Consider adding a shortcut by getting the current version of the course and comparing that first instead of 
 100  
         // all versions of the course
 101  
         List<VersionDisplayInfo> versions;
 102  
         try {
 103  0
             versions = courseService.getVersions(LuServiceConstants.CLU_NAMESPACE_URI, versionIndCourseId);
 104  0
         } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 105  
             // TODO: if no version exists for the target term should we return null instead?
 106  0
             throw new DoesNotExistException("The course does not exist. course: " + versionIndCourseId, e);
 107  0
         } catch (Exception e) {
 108  0
             throw new OperationFailedException("unxpected trying to get course " + versionIndCourseId, e);
 109  0
         }
 110  
         // TODO: consider sorting this in reverse order so the latest versions are checked first
 111  0
         for (VersionDisplayInfo version : versions) {
 112  0
             CourseInfo course = this.getCourse(version.getId());
 113  0
             if (isCourseValidToBeOfferendInTerm(course, targetTerm, context)) {
 114  0
                 list.add(course);
 115  
             }
 116  0
         }
 117  0
         return list;
 118  
     }
 119  
 
 120  
     private boolean isCourseValidToBeOfferendInTerm(CourseInfo course, TermInfo targetTerm, ContextInfo context)
 121  
             throws OperationFailedException {
 122  
         // TODO: check the status of the course to make sure it is active, superseded or retired but it cannot be draft or otherwise in-active
 123  
         // shortcut if the terms match
 124  0
         if (targetTerm.getId().equals(course.getStartTerm())) {
 125  0
             return true;
 126  
         }
 127  0
         if (targetTerm.getId().equals(course.getEndTerm())) {
 128  0
             return true;
 129  
         }
 130  
         // TODO: find out if the course's Effective and expiration dates can be used so I don't have to fetch all the terms to 
 131  
         // compare start/end dates
 132  
         TermInfo startTerm;
 133  
         try {
 134  0
             startTerm = acalService.getTerm(course.getStartTerm(), context);
 135  0
         } catch (DoesNotExistException ex) {
 136  0
             throw new OperationFailedException("unexpected", ex);
 137  0
         } catch (InvalidParameterException ex) {
 138  0
             throw new OperationFailedException("unexpected", ex);
 139  0
         } catch (MissingParameterException ex) {
 140  0
             throw new OperationFailedException("unexpected", ex);
 141  0
         } catch (PermissionDeniedException ex) {
 142  0
             throw new OperationFailedException("unexpected", ex);
 143  0
         }
 144  0
         if (targetTerm.getStartDate().before(startTerm.getStartDate())) {
 145  0
             return false;
 146  
         }
 147  
         // if no end term the all done
 148  0
         if (course.getEndTerm() == null) {
 149  0
             return true;
 150  
         }
 151  
         TermInfo endTerm;
 152  
         try {
 153  0
             endTerm = acalService.getTerm(course.getEndTerm(), context);
 154  0
         } catch (DoesNotExistException ex) {
 155  0
             throw new OperationFailedException("unexpected", ex);
 156  0
         } catch (InvalidParameterException ex) {
 157  0
             throw new OperationFailedException("unexpected", ex);
 158  0
         } catch (MissingParameterException ex) {
 159  0
             throw new OperationFailedException("unexpected", ex);
 160  0
         } catch (PermissionDeniedException ex) {
 161  0
             throw new OperationFailedException("unexpected", ex);
 162  0
         }
 163  0
         if (targetTerm.getStartDate().after(endTerm.getEndDate())) {
 164  0
             return false;
 165  
         }
 166  0
         return false;
 167  
 
 168  
     }
 169  
 }