1 package org.kuali.student.lum.lu.ui.course.server.gwt;
2
3 import java.util.Iterator;
4 import java.util.List;
5
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.r1.core.statement.dto.StatementTreeViewInfo;
19 import org.kuali.student.lum.common.server.StatementUtil;
20 import org.kuali.student.r2.lum.course.dto.CourseInfo;
21 import org.kuali.student.r2.lum.course.service.CourseService;
22 import org.kuali.student.r2.lum.util.constants.CourseServiceConstants;
23 import org.springframework.transaction.annotation.Transactional;
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 public StatusInfo changeState(String courseId, String newState, String prevEndTermAtpId,ContextInfo contextInfo) throws Exception {
39
40 CourseInfo courseInfo = courseService.getCourse(courseId,contextInfo);
41
42 StatusInfo ret = new StatusInfo();
43 try {
44 if (newState.equals(DtoConstants.STATE_ACTIVE)) {
45 activateCourse(courseInfo, prevEndTermAtpId,contextInfo);
46 } else if (newState.equals(DtoConstants.STATE_RETIRED)) {
47 retireCourse(courseInfo,contextInfo);
48 }
49
50 ret.setSuccess(new Boolean(true));
51 } catch (Exception e) {
52 ret.setSuccess(new Boolean(false));
53 ret.setMessage(e.getMessage());
54 }
55
56 return ret;
57 }
58
59
60
61
62
63
64
65 protected void activateCourse(CourseInfo courseToActivate, String prevEndTermAtpId,ContextInfo contextInfo) throws Exception{
66 CourseInfo currVerCourse = getCurrentVersionOfCourse(courseToActivate,contextInfo);
67 String existingState = courseToActivate.getStateKey();
68 String currVerState = currVerCourse.getStateKey();
69 boolean isCurrVer = (courseToActivate.getId().equals(currVerCourse.getId()));
70
71 if (existingState.equals(DtoConstants.STATE_DRAFT)) {
72
73 if (isCurrVer) {
74
75 updateCourseVersionStates(courseToActivate, DtoConstants.STATE_ACTIVE, currVerCourse, null, true, prevEndTermAtpId,contextInfo);
76 } else if (currVerState.equals(DtoConstants.STATE_ACTIVE) ||
77 currVerState.equals(DtoConstants.STATE_SUSPENDED)) {
78 updateCourseVersionStates(courseToActivate, DtoConstants.STATE_ACTIVE, currVerCourse, DtoConstants.STATE_SUPERSEDED, true, prevEndTermAtpId,contextInfo);
79 }
80 }
81 }
82
83
84
85
86
87
88 protected void retireCourse(CourseInfo courseToRetire,ContextInfo contextInfo) throws Exception{
89 String existingState = courseToRetire.getStateKey();
90
91 if (existingState.equals(DtoConstants.STATE_ACTIVE) || existingState.equals(DtoConstants.STATE_SUSPENDED)){
92 courseToRetire.setStateKey(DtoConstants.STATE_RETIRED);
93
94 courseService.updateCourse(courseToRetire.getId(),courseToRetire, contextInfo);
95 updateStatementTreeViewInfoState(courseToRetire,contextInfo);
96 }
97 }
98
99
100
101
102
103
104 protected CourseInfo getCurrentVersionOfCourse(CourseInfo course,ContextInfo contextInfo)
105 throws Exception {
106
107 String verIndId = course.getVersion().getVersionIndId();
108
109
110 VersionDisplayInfo curVerDisplayInfo = courseService.getCurrentVersion(
111 CourseServiceConstants.COURSE_NAMESPACE_URI, verIndId, contextInfo);
112 String curVerId = curVerDisplayInfo.getId();
113
114
115 CourseInfo currVerCourse = courseService.getCourse(curVerId,contextInfo);
116
117 return currVerCourse;
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 @Transactional(readOnly = false)
143 private void updateCourseVersionStates(CourseInfo thisVerCourse,
144 String thisVerNewState, CourseInfo currVerCourse,
145 String currVerNewState, boolean makeCurrent,
146 String prevEndTermAtpId,ContextInfo contextInfo) throws Exception {
147 String thisVerPrevState = thisVerCourse.getStateKey();
148
149
150
151 boolean isCurrent = thisVerCourse.getId().equals(currVerCourse.getId());
152 if(!makeCurrent || !isCurrent || !thisVerCourse.getVersion().getSequenceNumber().equals(1)){
153 makeCurrent &= !isCurrent;
154 }
155
156 if (thisVerNewState == null) {
157 throw new InvalidParameterException("new state cannot be null");
158 } else {
159 thisVerCourse.setStateKey(thisVerNewState);
160 courseService.updateCourse(thisVerCourse.getId(),thisVerCourse,contextInfo);
161 updateStatementTreeViewInfoState(thisVerCourse,contextInfo);
162 }
163
164
165 if (currVerNewState != null) {
166 currVerCourse.setStateKey(currVerNewState);
167 if(currVerCourse.getEndTerm()==null){
168 currVerCourse.setEndTerm(prevEndTermAtpId);
169 }
170 courseService.updateCourse(currVerCourse.getId(),currVerCourse,contextInfo);
171 updateStatementTreeViewInfoState(currVerCourse,contextInfo);
172 }
173
174 if (makeCurrent == true) {
175 courseService.setCurrentCourseVersion(thisVerCourse.getId(),
176 null,contextInfo);
177 }
178
179
180
181
182
183 if (thisVerPrevState.equals(DtoConstants.STATE_APPROVED)
184 && thisVerNewState.equals(DtoConstants.STATE_ACTIVE)) {
185
186 List<VersionDisplayInfo> versions = courseService.getVersions(
187 CourseServiceConstants.COURSE_NAMESPACE_URI, thisVerCourse
188 .getVersion().getVersionIndId(), contextInfo);
189 Long startSeq = new Long(1);
190
191 if (!isCurrent && (currVerCourse.getId() != thisVerCourse.getId())) {
192 startSeq = currVerCourse.getVersion().getSequenceNumber() + 1;
193 }
194
195 for (VersionDisplayInfo versionInfo : versions) {
196 if (versionInfo.getSequenceNumber() >= startSeq) {
197 CourseInfo otherCourse = courseService
198 .getCourse(""+versionInfo.getId(),contextInfo);
199 if (otherCourse.getStateKey().equals(
200 DtoConstants.STATE_APPROVED)
201 || otherCourse.getStateKey().equals(
202 DtoConstants.STATE_SUBMITTED)
203 || otherCourse.getStateKey().equals(
204 DtoConstants.STATE_DRAFT)) {
205 otherCourse.setStateKey(DtoConstants.STATE_SUPERSEDED);
206 courseService.updateCourse(otherCourse.getId(),otherCourse,contextInfo);
207 updateStatementTreeViewInfoState(otherCourse,contextInfo);
208 }
209 }
210 }
211 }
212
213 }
214
215 public void setCourseService(CourseService courseService) {
216 this.courseService = courseService;
217 }
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 public void updateStatementTreeViewInfoState(CourseInfo courseInfo,ContextInfo contextInfo)
238 throws DoesNotExistException, InvalidParameterException,
239 MissingParameterException, OperationFailedException,
240 PermissionDeniedException, DataValidationErrorException,
241 CircularReferenceException, VersionMismatchException {
242
243
244
245 List<StatementTreeViewInfo> statementTreeViewInfos = courseService
246 .getCourseStatements(courseInfo.getId(), null, null,contextInfo);
247
248 if (statementTreeViewInfos != null){
249
250 for (Iterator<StatementTreeViewInfo> it = statementTreeViewInfos
251 .iterator(); it.hasNext();)
252 StatementUtil.updateStatementTreeViewInfoState(courseInfo
253 .getStateKey(), it.next());
254
255
256
257 for (Iterator<StatementTreeViewInfo> it = statementTreeViewInfos
258 .iterator(); it.hasNext();)
259 courseService.updateCourseStatement(courseInfo.getId(),null, it.next(),contextInfo);
260 }
261 }
262
263 }