Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CoreProgramStateChangeServiceImpl |
|
| 2.0;2 |
1 | package org.kuali.student.lum.program.server; | |
2 | ||
3 | import java.util.List; | |
4 | ||
5 | import org.kuali.student.common.dto.DtoConstants; | |
6 | import org.kuali.student.common.exceptions.DoesNotExistException; | |
7 | import org.kuali.student.common.exceptions.InvalidParameterException; | |
8 | import org.kuali.student.common.versionmanagement.dto.VersionDisplayInfo; | |
9 | import org.kuali.student.core.statement.dto.StatementTreeViewInfo; | |
10 | import org.kuali.student.lum.common.server.StatementUtil; | |
11 | import org.kuali.student.lum.program.dto.CoreProgramInfo; | |
12 | import org.kuali.student.lum.program.dto.ProgramRequirementInfo; | |
13 | import org.kuali.student.lum.program.service.ProgramService; | |
14 | import org.kuali.student.lum.program.service.ProgramServiceConstants; | |
15 | import org.springframework.transaction.annotation.Transactional; | |
16 | ||
17 | /** | |
18 | * This class is called whenever the state of a major discipline changes. | |
19 | * <p> | |
20 | * We have a separate class because the operations need to be marked with the @Transactional annotation. | |
21 | * <p> | |
22 | * THIS CLASS IS DUPLICATED FOR MAJOR DISCIPLINE, CORE, AND CREDENTIAL PROGRAMS SINCE THERE ARE DIFFERENT SERVICE METHODS FOR | |
23 | * EACH OF THESE TYPES (THOUGH THEY ARE SIMILAR) | |
24 | * <P> | |
25 | * | |
26 | * @author Kuali Rice Team (kuali-rice@googlegroups.com) | |
27 | */ | |
28 | @Transactional(noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class}) | |
29 | 0 | public class CoreProgramStateChangeServiceImpl implements StateChangeService { |
30 | ||
31 | ||
32 | private ProgramService programService; | |
33 | ||
34 | /** | |
35 | * This method is called by workflow when the state changes. | |
36 | * | |
37 | * @param majorDisciplineId | |
38 | * @param state | |
39 | * @return | |
40 | * @throws Exception | |
41 | */ | |
42 | public void changeState(String coreProgramId, String newState) throws Exception { | |
43 | // This method will be called from workflow. | |
44 | // Since we cannot activate a program from the workflow we do not need to add endEntryTerm and endEnrollTerm | |
45 | 0 | changeState(null, null, null, coreProgramId, newState); |
46 | 0 | } |
47 | ||
48 | /** | |
49 | * This method is called from the UI (servlet) when state changes. | |
50 | * | |
51 | * @param endEntryTerm | |
52 | * @param endEnrollTerm | |
53 | * @param programType | |
54 | * @param majorDisciplineId | |
55 | * @param newState | |
56 | * @return | |
57 | * @throws Exception | |
58 | */ | |
59 | public void changeState(String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm, String coreProgramId, String newState) throws Exception { | |
60 | ||
61 | // New state must not be null | |
62 | 0 | if (newState == null) |
63 | 0 | throw new InvalidParameterException("new state cannot be null"); |
64 | ||
65 | // The version selected in the UI | |
66 | 0 | CoreProgramInfo selectedVersion = programService.getCoreProgram(coreProgramId); |
67 | ||
68 | // If we are activating this version we need to mark the previous version superseded, | |
69 | // update the previous version end terms, and make the selected version current. | |
70 | 0 | if (newState.equals(DtoConstants.STATE_ACTIVE)) { |
71 | ||
72 | // Find the previous version | |
73 | 0 | CoreProgramInfo previousVersion = findPreviousVersion(selectedVersion); |
74 | ||
75 | 0 | if (previousVersion != null) { |
76 | ||
77 | // Set end terms on previous version | |
78 | 0 | setEndTerms(previousVersion, endEntryTerm, endEnrollTerm); |
79 | ||
80 | // Mark previous version as superseded and update state on all associated objects | |
81 | 0 | updateCoreProgramInfoState(previousVersion, DtoConstants.STATE_SUPERSEDED); |
82 | } | |
83 | ||
84 | // Update state of all associated objects for current version | |
85 | // NOTE: we must update state BEFORE making the version current | |
86 | 0 | updateCoreProgramInfoState(selectedVersion, newState); |
87 | ||
88 | // Make this the current version | |
89 | 0 | makeCurrent(selectedVersion); |
90 | 0 | } else { |
91 | ||
92 | // Update state of all associated objects for current version | |
93 | 0 | updateCoreProgramInfoState(selectedVersion, newState); |
94 | } | |
95 | ||
96 | 0 | } |
97 | ||
98 | /** | |
99 | * This method updates the end terms for the major discipline passed into it. | |
100 | * <p> | |
101 | * You must still call updateState() to save the object using the web service. | |
102 | * | |
103 | * @param coreProgramInfo | |
104 | * @param endEntryTerm | |
105 | * @param endEnrollTerm | |
106 | */ | |
107 | private void setEndTerms(CoreProgramInfo coreProgramInfo, String endEntryTerm, String endEnrollTerm) { | |
108 | 0 | coreProgramInfo.setEndProgramEntryTerm(endEntryTerm); |
109 | 0 | coreProgramInfo.setEndTerm(endEnrollTerm); |
110 | 0 | } |
111 | ||
112 | /** | |
113 | * This method will update the state of this object and all associated objects. | |
114 | * <p> | |
115 | * It is needed because we need to make separate web service calls to update the state of these objects. | |
116 | * | |
117 | * @param coreProgramInfo | |
118 | * @param newState | |
119 | */ | |
120 | private void updateCoreProgramInfoState(CoreProgramInfo coreProgramInfo, String newState) throws Exception { | |
121 | // Update the statement tree | |
122 | 0 | List<String> programRequirementIds = coreProgramInfo.getProgramRequirements(); |
123 | 0 | updateRequirementsState(programRequirementIds, newState); |
124 | ||
125 | // Credential and core programs do not have variations | |
126 | ||
127 | ||
128 | // Update major discipline | |
129 | 0 | coreProgramInfo.setState(newState); |
130 | 0 | programService.updateCoreProgram(coreProgramInfo); |
131 | 0 | } |
132 | ||
133 | /** | |
134 | * This method will make this version of the major discipline the current one. | |
135 | * | |
136 | * @param coreProgramInfo | |
137 | */ | |
138 | private void makeCurrent(CoreProgramInfo coreProgramInfo) throws Exception { | |
139 | ||
140 | // Check if this is the current version before trying to make it current | |
141 | // (the web service will error if you try to make a version current that is already current) | |
142 | 0 | VersionDisplayInfo currentVersion = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, coreProgramInfo.getVersionInfo().getVersionIndId()); |
143 | ||
144 | // If this is not the current version, then make it current | |
145 | 0 | if (!currentVersion.getSequenceNumber().equals(coreProgramInfo.getVersionInfo().getSequenceNumber())) { |
146 | 0 | programService.setCurrentCoreProgramVersion(coreProgramInfo.getId(), null); |
147 | } | |
148 | 0 | } |
149 | ||
150 | /** | |
151 | * This method finds the previous version (the version right before this one). | |
152 | * <p> | |
153 | * e.g. v1 = ACTIVE v2 = APPROVED | |
154 | * <p> | |
155 | * If you passed v2 into this method, it would return v1. | |
156 | * <p> | |
157 | * If there is only one major discipline this method will return null. | |
158 | * | |
159 | * @param coreProgramInfo | |
160 | * @return | |
161 | */ | |
162 | private CoreProgramInfo findPreviousVersion(CoreProgramInfo coreProgramInfo) throws Exception { | |
163 | // Find all previous versions using the version independent indicator | |
164 | 0 | List<VersionDisplayInfo> versions = programService.getVersions(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, coreProgramInfo.getVersionInfo().getVersionIndId()); |
165 | ||
166 | // Take the sequence number for this version | |
167 | 0 | Long sequenceNumber = coreProgramInfo.getVersionInfo().getSequenceNumber(); |
168 | ||
169 | // And subtract 1 from the sequence number to get the previous version | |
170 | 0 | sequenceNumber -= 1; |
171 | ||
172 | // Loop over all versions and find the previous version based on the sequence number | |
173 | /* | |
174 | * NOTE: Dan suggested we loop over all versions and change any version with state=active to state=superseded. | |
175 | * However, we decided not to go that route because we would need to pull back all data for each version to determine | |
176 | * if a version is active, since versioninfo does not have a getState() method | |
177 | */ | |
178 | 0 | CoreProgramInfo previousVersion = null; |
179 | 0 | for (VersionDisplayInfo versionInfo : versions) { |
180 | 0 | if (versionInfo.getSequenceNumber().equals(sequenceNumber)) { |
181 | 0 | previousVersion = programService.getCoreProgram(versionInfo.getId()); |
182 | 0 | break; |
183 | } | |
184 | } | |
185 | 0 | return previousVersion; |
186 | } | |
187 | ||
188 | /** | |
189 | * This method will update the requirement state. | |
190 | * <p> | |
191 | * Note that it uses StatementUtil to update the statement tree. | |
192 | * | |
193 | * @param coreProgramInfo | |
194 | * @param newState | |
195 | * @throws Exception | |
196 | */ | |
197 | public void updateRequirementsState(List<String> programRequirementIds, String newState) throws Exception { | |
198 | ||
199 | 0 | for (String programRequirementId : programRequirementIds) { |
200 | ||
201 | // Get program requirement from the program service | |
202 | 0 | ProgramRequirementInfo programRequirementInfo = programService.getProgramRequirement(programRequirementId, null, null); |
203 | ||
204 | // Look in the requirement for the statement tree | |
205 | 0 | StatementTreeViewInfo statementTree = programRequirementInfo.getStatement(); |
206 | ||
207 | // And recursively update the entire tree with the new state | |
208 | 0 | StatementUtil.updateStatementTreeViewInfoState(newState, statementTree); |
209 | ||
210 | // Update the state of the requirement object | |
211 | 0 | programRequirementInfo.setState(newState); |
212 | ||
213 | // The write the requirement back to the program service | |
214 | 0 | programService.updateProgramRequirement(programRequirementInfo); |
215 | ||
216 | 0 | } |
217 | 0 | } |
218 | ||
219 | /** | |
220 | * This method is used by Spring to inject the program service into this bean. | |
221 | * | |
222 | * @param programService | |
223 | */ | |
224 | public void setProgramService(ProgramService programService) { | |
225 | 0 | this.programService = programService; |
226 | 0 | } |
227 | ||
228 | } |