| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
package org.kuali.student.enrollment.class2.courseoffering.service.decorators; |
| 6 | |
|
| 7 | |
import org.apache.oro.text.regex.PatternMatcher; |
| 8 | |
import org.kuali.student.common.versionmanagement.dto.VersionDisplayInfo; |
| 9 | |
import org.kuali.student.enrollment.acal.dto.TermInfo; |
| 10 | |
import org.kuali.student.enrollment.acal.service.AcademicCalendarService; |
| 11 | |
import org.kuali.student.lum.course.dto.CourseInfo; |
| 12 | |
import org.kuali.student.lum.course.service.CourseService; |
| 13 | |
import org.kuali.student.lum.course.service.CourseServiceConstants; |
| 14 | |
import org.kuali.student.r2.common.dto.ContextInfo; |
| 15 | |
import org.kuali.student.r2.common.exceptions.DoesNotExistException; |
| 16 | |
import org.kuali.student.r2.common.exceptions.InvalidParameterException; |
| 17 | |
import org.kuali.student.r2.common.exceptions.MissingParameterException; |
| 18 | |
import org.kuali.student.r2.common.exceptions.OperationFailedException; |
| 19 | |
import org.kuali.student.r2.common.exceptions.PermissionDeniedException; |
| 20 | |
|
| 21 | |
import java.util.ArrayList; |
| 22 | |
import java.util.List; |
| 23 | |
import java.util.regex.Matcher; |
| 24 | |
import java.util.regex.Pattern; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
public class R1CourseServiceHelper { |
| 31 | |
|
| 32 | |
private CourseService courseService; |
| 33 | |
private AcademicCalendarService acalService; |
| 34 | |
|
| 35 | |
public AcademicCalendarService getAcalService() { |
| 36 | 0 | return acalService; |
| 37 | |
} |
| 38 | |
|
| 39 | |
public void setAcalService(AcademicCalendarService acalService) { |
| 40 | 0 | this.acalService = acalService; |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
public CourseService getCourseService() { |
| 44 | 0 | return courseService; |
| 45 | |
} |
| 46 | |
|
| 47 | |
public void setCourseService(CourseService courseService) { |
| 48 | 0 | this.courseService = courseService; |
| 49 | 0 | } |
| 50 | |
|
| 51 | 0 | public R1CourseServiceHelper() { |
| 52 | 0 | } |
| 53 | |
|
| 54 | 0 | public R1CourseServiceHelper(CourseService courseService, AcademicCalendarService acalService) { |
| 55 | 0 | this.courseService = courseService; |
| 56 | 0 | this.acalService = acalService; |
| 57 | 0 | } |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public CourseInfo getCourse(String courseId) throws DoesNotExistException, OperationFailedException { |
| 67 | 0 | CourseInfo course = null; |
| 68 | |
try { |
| 69 | 0 | course = courseService.getCourse(courseId); |
| 70 | 0 | } catch (org.kuali.student.common.exceptions.DoesNotExistException e) { |
| 71 | 0 | throw new DoesNotExistException("The course does not exist. course: " + courseId, e); |
| 72 | 0 | } catch (Exception e) { |
| 73 | 0 | throw new OperationFailedException("unxpected trying to get course " + courseId, e); |
| 74 | 0 | } |
| 75 | 0 | return course; |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public List<CourseInfo> getCoursesForTerm(String courseId, String targetTermId, ContextInfo context) |
| 89 | |
throws DoesNotExistException, OperationFailedException { |
| 90 | 0 | List<CourseInfo> list = new ArrayList<CourseInfo>(); |
| 91 | 0 | CourseInfo sourceCourse = this.getCourse(courseId); |
| 92 | 0 | String versionIndCourseId = sourceCourse.getVersionInfo().getVersionIndId(); |
| 93 | |
TermInfo targetTerm; |
| 94 | |
try { |
| 95 | 0 | targetTerm = acalService.getTerm(targetTermId, context); |
| 96 | 0 | } catch (InvalidParameterException ex) { |
| 97 | 0 | throw new OperationFailedException("unexpected", ex); |
| 98 | 0 | } catch (MissingParameterException ex) { |
| 99 | 0 | throw new OperationFailedException("unexpected", ex); |
| 100 | 0 | } catch (PermissionDeniedException ex) { |
| 101 | 0 | throw new OperationFailedException("unexpected", ex); |
| 102 | 0 | } |
| 103 | |
|
| 104 | |
|
| 105 | |
List<VersionDisplayInfo> versions; |
| 106 | |
try { |
| 107 | 0 | versions = courseService.getVersions(CourseServiceConstants.COURSE_NAMESPACE_URI, versionIndCourseId); |
| 108 | 0 | } catch (org.kuali.student.common.exceptions.DoesNotExistException e) { |
| 109 | |
|
| 110 | 0 | throw new DoesNotExistException("The course does not exist. course: " + versionIndCourseId, e); |
| 111 | 0 | } catch (Exception e) { |
| 112 | 0 | throw new OperationFailedException("unexpected trying to get course " + versionIndCourseId, e); |
| 113 | 0 | } |
| 114 | |
|
| 115 | 0 | for (VersionDisplayInfo version : versions) { |
| 116 | 0 | CourseInfo course = this.getCourse(version.getId()); |
| 117 | 0 | if (isCourseValidToBeOfferendInTerm(course, targetTerm, context)) { |
| 118 | 0 | list.add(course); |
| 119 | |
} |
| 120 | 0 | } |
| 121 | 0 | return list; |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
private String _hackTerm(String termId) { |
| 126 | 0 | if (termId.startsWith("kuali")) { |
| 127 | 0 | Pattern p = Pattern.compile("\\d\\d\\d\\d"); |
| 128 | 0 | Matcher m = p.matcher(termId); |
| 129 | 0 | if (m.find()) { |
| 130 | 0 | String s = m.group(); |
| 131 | 0 | return s + "FALL"; |
| 132 | |
} |
| 133 | |
} |
| 134 | 0 | return termId; |
| 135 | |
} |
| 136 | |
|
| 137 | |
private boolean isCourseValidToBeOfferendInTerm(CourseInfo course, TermInfo targetTerm, ContextInfo context) |
| 138 | |
throws OperationFailedException { |
| 139 | |
|
| 140 | |
|
| 141 | 0 | if (targetTerm.getId().equals(course.getStartTerm())) { |
| 142 | 0 | return true; |
| 143 | |
} |
| 144 | 0 | if (targetTerm.getId().equals(course.getEndTerm())) { |
| 145 | 0 | return true; |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
TermInfo startTerm; |
| 150 | 0 | String startTermStr = course.getStartTerm(); |
| 151 | 0 | startTermStr = _hackTerm(startTermStr); |
| 152 | |
try { |
| 153 | 0 | startTerm = acalService.getTerm(startTermStr, 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().before(startTerm.getStartDate())) { |
| 164 | 0 | return false; |
| 165 | |
} |
| 166 | |
|
| 167 | 0 | if (course.getEndTerm() == null || course.getEndTerm().indexOf("9999") != -1) { |
| 168 | 0 | return true; |
| 169 | |
} |
| 170 | |
TermInfo endTerm; |
| 171 | |
try { |
| 172 | 0 | endTerm = acalService.getTerm(course.getEndTerm(), context); |
| 173 | 0 | } catch (DoesNotExistException ex) { |
| 174 | 0 | throw new OperationFailedException("unexpected", ex); |
| 175 | 0 | } catch (InvalidParameterException ex) { |
| 176 | 0 | throw new OperationFailedException("unexpected", ex); |
| 177 | 0 | } catch (MissingParameterException ex) { |
| 178 | 0 | throw new OperationFailedException("unexpected", ex); |
| 179 | 0 | } catch (PermissionDeniedException ex) { |
| 180 | 0 | throw new OperationFailedException("unexpected", ex); |
| 181 | 0 | } |
| 182 | 0 | if (targetTerm.getStartDate().after(endTerm.getEndDate())) { |
| 183 | 0 | return false; |
| 184 | |
} |
| 185 | 0 | return false; |
| 186 | |
|
| 187 | |
} |
| 188 | |
} |