1 | |
package org.kuali.student.lum.lu.ui.course.server.gwt; |
2 | |
|
3 | |
import java.util.Date; |
4 | |
import java.util.List; |
5 | |
|
6 | |
import org.kuali.student.core.dto.StatusInfo; |
7 | |
import org.kuali.student.core.exceptions.DoesNotExistException; |
8 | |
import org.kuali.student.core.exceptions.InvalidParameterException; |
9 | |
import org.kuali.student.core.versionmanagement.dto.VersionDisplayInfo; |
10 | |
import org.kuali.student.lum.common.client.lu.LUUIConstants; |
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.springframework.transaction.annotation.Transactional; |
15 | |
|
16 | |
@Transactional(noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class}) |
17 | 0 | public class CourseStateChangeServiceImpl { |
18 | |
private CourseService courseService; |
19 | |
|
20 | |
public StatusInfo changeState(String courseId, String newState, Date currentVersionStart) throws Exception { |
21 | |
|
22 | 0 | CourseInfo thisVerCourse = courseService.getCourse(courseId); |
23 | 0 | String prevState = thisVerCourse.getState(); |
24 | 0 | String verIndId = thisVerCourse.getVersionInfo().getVersionIndId(); |
25 | 0 | VersionDisplayInfo curVerDisplayInfo = courseService.getCurrentVersion(CourseServiceConstants.COURSE_NAMESPACE_URI, verIndId); |
26 | 0 | String curVerId = curVerDisplayInfo.getId(); |
27 | 0 | CourseInfo currVerCourse = courseService.getCourse(curVerId); |
28 | 0 | String currVerState = currVerCourse.getState(); |
29 | 0 | boolean isCurrVer = (courseId.equals(currVerCourse.getId())); |
30 | |
|
31 | 0 | StatusInfo ret = new StatusInfo(); |
32 | |
try { |
33 | 0 | if (newState.equals(LUUIConstants.LU_STATE_ACTIVE)) { |
34 | 0 | if (prevState.equals(LUUIConstants.LU_STATE_APPROVED)) { |
35 | |
|
36 | 0 | if (isCurrVer) { |
37 | |
|
38 | 0 | updateCourseVersionStates(thisVerCourse, newState, currVerCourse, null, true, currentVersionStart); |
39 | 0 | } else if (currVerState.equals(LUUIConstants.LU_STATE_ACTIVE) || |
40 | |
currVerState.equals(LUUIConstants.LU_STATE_INACTIVE)) { |
41 | 0 | updateCourseVersionStates(thisVerCourse, newState, currVerCourse, LUUIConstants.LU_STATE_SUPERSEDED, true, currentVersionStart); |
42 | |
} |
43 | |
|
44 | 0 | } else if (prevState.equals(LUUIConstants.LU_STATE_INACTIVE)) { |
45 | |
|
46 | |
} |
47 | |
} |
48 | 0 | ret.setSuccess(new Boolean(true)); |
49 | 0 | } catch (Exception e) { |
50 | 0 | ret.setSuccess(new Boolean(false)); |
51 | 0 | ret.setMessage(e.getMessage()); |
52 | 0 | } |
53 | |
|
54 | 0 | return ret; |
55 | |
} |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
@Transactional(readOnly = false) |
69 | |
private void updateCourseVersionStates (CourseInfo thisVerCourse, String thisVerNewState, CourseInfo currVerCourse, String currVerNewState, boolean makeCurrent, Date currentVersionStart) throws Exception { |
70 | 0 | String thisVerPrevState = thisVerCourse.getState(); |
71 | 0 | String currVerPrevState = currVerCourse.getState(); |
72 | |
|
73 | |
|
74 | 0 | boolean isCurrent = thisVerCourse.getId().equals(currVerCourse.getId()); |
75 | 0 | makeCurrent &= !isCurrent; |
76 | |
|
77 | 0 | if (thisVerNewState == null) { |
78 | 0 | throw new InvalidParameterException("new state cannot be null"); |
79 | |
} else { |
80 | 0 | thisVerCourse.setState(thisVerNewState); |
81 | 0 | courseService.updateCourse(thisVerCourse); |
82 | |
} |
83 | |
|
84 | |
|
85 | 0 | if (currVerNewState != null) { |
86 | 0 | currVerCourse.setState(currVerNewState); |
87 | 0 | courseService.updateCourse(currVerCourse); |
88 | |
|
89 | |
} |
90 | |
|
91 | 0 | if (makeCurrent == true) { |
92 | 0 | courseService.setCurrentCourseVersion(thisVerCourse.getId(), currentVersionStart); |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | 0 | if (thisVerPrevState.equals(LUUIConstants.LU_STATE_APPROVED) && |
100 | |
thisVerNewState.equals(LUUIConstants.LU_STATE_ACTIVE)) { |
101 | |
|
102 | 0 | List<VersionDisplayInfo> versions = courseService.getVersions(CourseServiceConstants.COURSE_NAMESPACE_URI, thisVerCourse.getVersionInfo().getVersionIndId()); |
103 | 0 | Long startSeq = new Long(1); |
104 | |
|
105 | 0 | if (!isCurrent && (currVerCourse.getId() != thisVerCourse.getId())) { |
106 | 0 | startSeq = currVerCourse.getVersionInfo().getSequenceNumber() + 1; |
107 | |
} |
108 | |
|
109 | 0 | for (VersionDisplayInfo versionInfo: versions) { |
110 | 0 | if (versionInfo.getSequenceNumber() >= startSeq) { |
111 | 0 | CourseInfo otherCourse = courseService.getCourse(versionInfo.getId()); |
112 | 0 | if (otherCourse.getState().equals(LUUIConstants.LU_STATE_APPROVED) || |
113 | |
otherCourse.getState().equals(LUUIConstants.LU_STATE_SUBMITTED) || |
114 | |
otherCourse.getState().equals(LUUIConstants.LU_STATE_DRAFT)) { |
115 | 0 | otherCourse.setState(LUUIConstants.LU_STATE_SUPERSEDED); |
116 | 0 | courseService.updateCourse(otherCourse); |
117 | |
} |
118 | 0 | } |
119 | |
} |
120 | |
} |
121 | |
|
122 | 0 | } |
123 | |
|
124 | |
public void setCourseService(CourseService courseService) { |
125 | 0 | this.courseService = courseService; |
126 | 0 | } |
127 | |
} |