1 package org.kuali.student.lum.workflow;
2
3 import org.kuali.student.r1.core.statement.dto.StatementTreeViewInfo;
4 import org.kuali.student.r1.lum.course.service.CourseServiceConstants;
5 import org.kuali.student.r2.common.dto.AttributeInfo;
6 import org.kuali.student.r2.common.dto.ContextInfo;
7 import org.kuali.student.r2.common.dto.DtoConstants;
8 import org.kuali.student.r2.common.dto.StatusInfo;
9 import org.kuali.student.r2.common.exceptions.CircularReferenceException;
10 import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
11 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
12 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
13 import org.kuali.student.r2.common.exceptions.MissingParameterException;
14 import org.kuali.student.r2.common.exceptions.OperationFailedException;
15 import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
16 import org.kuali.student.r2.common.exceptions.VersionMismatchException;
17 import org.kuali.student.r2.core.versionmanagement.dto.VersionDisplayInfo;
18 import org.kuali.student.r2.lum.course.dto.CourseInfo;
19 import org.kuali.student.r2.lum.course.service.CourseService;
20 import org.springframework.transaction.annotation.Transactional;
21
22 import java.util.Iterator;
23 import java.util.List;
24
25 @Transactional(noRollbackFor = { DoesNotExistException.class }, rollbackFor = { Throwable.class })
26 public class CourseStateChangeServiceImpl {
27 private CourseService courseService;
28
29
30
31
32
33
34
35
36
37
38
39
40 public StatusInfo changeState(String courseId, String newState, String prevEndTermAtpId,ContextInfo contextInfo) throws Exception {
41
42
43
44 CourseInfo courseInfo = courseService.getCourse(courseId,contextInfo);
45
46
47
48
49
50 CourseInfo currVerCourse = getCurrentVersionOfCourse(courseInfo, contextInfo);
51
52
53
54 boolean isOnlyVersion = (courseInfo.getId().equals(currVerCourse.getId()));
55
56
57 StatusInfo ret = new StatusInfo();
58
59
60 if ((newState!=null) && (newState.equals(DtoConstants.STATE_ACTIVE))) {
61
62
63
64
65 if ((courseInfo!=null) && courseInfo.isPilotCourse()){
66
67
68
69 if (!isOnlyVersion){
70 currVerCourse.setStateKey(DtoConstants.STATE_SUPERSEDED);
71 currVerCourse.setEndTerm(prevEndTermAtpId);
72 courseService.updateCourse(currVerCourse.getId(), currVerCourse, contextInfo);
73 updateStatementTreeViewInfoState(currVerCourse, contextInfo);
74
75 }
76
77
78
79
80 courseInfo.getAttributes().add(new AttributeInfo("retirementRationale", "Pilot Course"));
81 courseInfo.getAttributes().add(new AttributeInfo("lastTermOffered", courseInfo.getEndTerm()));
82 courseInfo.setStateKey(DtoConstants.STATE_ACTIVE);
83 retireCourse(courseInfo, contextInfo);
84
85
86
87 if (!isOnlyVersion){
88
89
90
91 courseInfo = courseService.getCourse(courseId, contextInfo);
92
93
94 courseService.setCurrentCourseVersion(courseInfo.getId(),
95 null, contextInfo);
96
97 }
98
99 }else{
100
101 activateCourse(courseInfo, prevEndTermAtpId, contextInfo);
102 }
103 } else if (newState.equals(DtoConstants.STATE_RETIRED)) {
104
105 retireCourse(courseInfo, contextInfo);
106 }
107
108 ret.setSuccess(true);
109
110 return ret;
111 }
112
113
114
115
116
117
118
119 protected void activateCourse(CourseInfo courseToActivate, String prevEndTermAtpId,ContextInfo contextInfo) throws Exception{
120 CourseInfo currVerCourse = getCurrentVersionOfCourse(courseToActivate,contextInfo);
121 String existingState = courseToActivate.getStateKey();
122 String currVerState = currVerCourse.getStateKey();
123 boolean isCurrVer = (courseToActivate.getId().equals(currVerCourse.getId()));
124
125 if (existingState.equals(DtoConstants.STATE_DRAFT)) {
126
127 if (isCurrVer) {
128
129 updateCourseVersionStates(courseToActivate, DtoConstants.STATE_ACTIVE, currVerCourse, null, true, prevEndTermAtpId,contextInfo);
130 } else if (currVerState.equals(DtoConstants.STATE_ACTIVE) ||
131 currVerState.equals(DtoConstants.STATE_SUSPENDED) ||
132 currVerState.equals(DtoConstants.STATE_RETIRED)) {
133 updateCourseVersionStates(courseToActivate, DtoConstants.STATE_ACTIVE, currVerCourse, DtoConstants.STATE_SUPERSEDED, true, prevEndTermAtpId,contextInfo);
134 }
135 }
136 }
137
138
139
140
141
142
143 protected void retireCourse(CourseInfo courseToRetire,ContextInfo contextInfo) throws Exception{
144 String existingState = courseToRetire.getStateKey();
145
146 if (existingState.equals(DtoConstants.STATE_ACTIVE) || existingState.equals(DtoConstants.STATE_SUSPENDED)){
147 courseToRetire.setStateKey(DtoConstants.STATE_RETIRED);
148
149 courseService.updateCourse(courseToRetire.getId(),courseToRetire,contextInfo);
150 updateStatementTreeViewInfoState(courseToRetire,contextInfo);
151 }
152 }
153
154
155
156
157
158
159 protected CourseInfo getCurrentVersionOfCourse(CourseInfo course,ContextInfo contextInfo)
160 throws Exception {
161
162 String verIndId = course.getVersion().getVersionIndId();
163
164
165 VersionDisplayInfo curVerDisplayInfo = courseService.getCurrentVersion(
166 CourseServiceConstants.COURSE_NAMESPACE_URI, verIndId,contextInfo);
167 String curVerId = curVerDisplayInfo.getId();
168
169
170 CourseInfo currVerCourse = courseService.getCourse(curVerId,contextInfo);
171
172 return currVerCourse;
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 private void updateCourseVersionStates(CourseInfo thisVerCourse,
198 String thisVerNewState, CourseInfo currVerCourse,
199 String currVerNewState, boolean makeCurrent,
200 String prevEndTermAtpId,ContextInfo contextInfo) throws Exception {
201 String thisVerPrevState = thisVerCourse.getStateKey();
202
203
204
205 boolean isCurrent = thisVerCourse.getId().equals(currVerCourse.getId());
206 if(!makeCurrent || !isCurrent || !thisVerCourse.getVersion().getSequenceNumber().equals(1)){
207 makeCurrent &= !isCurrent;
208 }
209
210 if (thisVerNewState == null) {
211 throw new InvalidParameterException("new state cannot be null");
212 } else {
213 thisVerCourse.setStateKey(thisVerNewState);
214 courseService.updateCourse(thisVerCourse.getId(),thisVerCourse,contextInfo);
215 updateStatementTreeViewInfoState(thisVerCourse,contextInfo);
216 }
217
218
219 if (currVerNewState != null) {
220 currVerCourse.setStateKey(currVerNewState);
221 if(currVerCourse.getEndTerm()==null){
222 currVerCourse.setEndTerm(prevEndTermAtpId);
223 }
224 courseService.updateCourse(currVerCourse.getId(),currVerCourse,contextInfo);
225 updateStatementTreeViewInfoState(currVerCourse,contextInfo);
226 }
227
228 if (makeCurrent == true) {
229 courseService.setCurrentCourseVersion(thisVerCourse.getId(),
230 null,contextInfo);
231 }
232
233
234
235
236
237 if (thisVerPrevState.equals(DtoConstants.STATE_APPROVED)
238 && thisVerNewState.equals(DtoConstants.STATE_ACTIVE)) {
239
240 List<VersionDisplayInfo> versions = courseService.getVersions(
241 CourseServiceConstants.COURSE_NAMESPACE_URI, thisVerCourse
242 .getVersion().getVersionIndId(),contextInfo);
243
244 Long startSeq = Long.valueOf(1);
245 if (!isCurrent && !currVerCourse.getId().equals(thisVerCourse.getId())) {
246 startSeq = currVerCourse.getVersion().getSequenceNumber() + 1;
247 }
248
249 for (VersionDisplayInfo versionInfo : versions) {
250 if (versionInfo.getSequenceNumber() >= startSeq) {
251 CourseInfo otherCourse = courseService
252 .getCourse(versionInfo.getId(),contextInfo);
253 if (otherCourse.getStateKey().equals(
254 DtoConstants.STATE_APPROVED)
255 || otherCourse.getStateKey().equals(
256 DtoConstants.STATE_SUBMITTED)
257 || otherCourse.getStateKey().equals(
258 DtoConstants.STATE_DRAFT)) {
259 otherCourse.setStateKey(DtoConstants.STATE_SUPERSEDED);
260 courseService.updateCourse(otherCourse.getId(),otherCourse,contextInfo);
261 updateStatementTreeViewInfoState(otherCourse,contextInfo);
262 }
263 }
264 }
265 }
266
267 }
268
269 public void setCourseService(CourseService courseService) {
270 this.courseService = courseService;
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291 public void updateStatementTreeViewInfoState(CourseInfo courseInfo,ContextInfo contextInfo)
292 throws DoesNotExistException, InvalidParameterException,
293 MissingParameterException, OperationFailedException,
294 PermissionDeniedException, DataValidationErrorException,
295 CircularReferenceException, VersionMismatchException {
296
297
298
299 List<StatementTreeViewInfo> statementTreeViewInfos = courseService
300 .getCourseStatements(courseInfo.getId(), null, null,contextInfo);
301
302 if(statementTreeViewInfos != null){
303
304 for (Iterator<StatementTreeViewInfo> it = statementTreeViewInfos.iterator(); it.hasNext();){
305 StatementUtil.updateStatementTreeViewInfoState(courseInfo.getStateKey(), it.next());
306 }
307
308
309
310 for (Iterator<StatementTreeViewInfo> it = statementTreeViewInfos.iterator(); it.hasNext();){
311 courseService.updateCourseStatement(courseInfo.getId(), null, it.next(), contextInfo);
312 }
313 }
314 }
315
316 }