1 | |
|
2 | |
|
3 | |
|
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 | |
|
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 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
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 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
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 | |
|
100 | |
|
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 | |
|
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 | |
|
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 | |
|
123 | |
|
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 | |
|
131 | |
|
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 | |
|
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 | |
} |