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