Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MajorDisciplineStateChangeServiceImpl |
|
| 2.7;2.7 |
1 | package org.kuali.student.lum.workflow; | |
2 | ||
3 | import java.util.Date; | |
4 | import java.util.List; | |
5 | ||
6 | import org.kuali.student.common.dto.DtoConstants; | |
7 | import org.kuali.student.common.exceptions.DoesNotExistException; | |
8 | import org.kuali.student.common.exceptions.InvalidParameterException; | |
9 | import org.kuali.student.common.exceptions.MissingParameterException; | |
10 | import org.kuali.student.common.exceptions.OperationFailedException; | |
11 | import org.kuali.student.common.versionmanagement.dto.VersionDisplayInfo; | |
12 | import org.kuali.student.core.atp.dto.AtpInfo; | |
13 | import org.kuali.student.core.atp.service.AtpService; | |
14 | import org.kuali.student.core.statement.dto.StatementTreeViewInfo; | |
15 | import org.kuali.student.lum.program.dto.MajorDisciplineInfo; | |
16 | import org.kuali.student.lum.program.dto.ProgramRequirementInfo; | |
17 | import org.kuali.student.lum.program.dto.ProgramVariationInfo; | |
18 | import org.kuali.student.lum.program.service.ProgramService; | |
19 | import org.kuali.student.lum.program.service.ProgramServiceConstants; | |
20 | import org.springframework.transaction.annotation.Transactional; | |
21 | ||
22 | /** | |
23 | * This class is called whenever the state of a major discipline changes. | |
24 | * <p> | |
25 | * We have a separate class because the operations need to be marked with the @Transactional annotation. | |
26 | * <p> | |
27 | * THIS CLASS IS DUPLICATED FOR MAJOR DISCIPLINE, CORE, AND CREDENTIAL PROGRAMS SINCE THERE ARE | |
28 | * DIFFERENT SERVICE METHODS FOR EACH OF THESE TYPES (THOUGH THEY ARE SIMILAR) | |
29 | * <P> | |
30 | * | |
31 | * @author Kuali Rice Team (kuali-rice@googlegroups.com) | |
32 | */ | |
33 | @Transactional(noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class}) | |
34 | 0 | public class MajorDisciplineStateChangeServiceImpl implements StateChangeService{ |
35 | ||
36 | /** | |
37 | * The program service - injected by spring. | |
38 | */ | |
39 | private ProgramService programService; | |
40 | private AtpService atpService; | |
41 | ||
42 | /** | |
43 | * This method is called by workflow when the state changes. | |
44 | * | |
45 | * @param majorDisciplineId | |
46 | * @param state | |
47 | * @return | |
48 | * @throws Exception | |
49 | */ | |
50 | public void changeState(String majorDisciplineId, String newState) throws Exception { | |
51 | // This method will be called from workflow. | |
52 | // Since we cannot activate a program from the workflow we do not need to add endEntryTerm and endEnrollTerm | |
53 | 0 | changeState(null, null, null, majorDisciplineId, newState); |
54 | 0 | } |
55 | ||
56 | /** | |
57 | * This method is called from the UI (servlet) when state changes. | |
58 | * | |
59 | * @param endEntryTerm | |
60 | * @param endEnrollTerm | |
61 | * @param programType | |
62 | * @param majorDisciplineId | |
63 | * @param newState | |
64 | * @return | |
65 | * @throws Exception | |
66 | */ | |
67 | public void changeState(String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm, String majorDisciplineId, String newState) throws Exception { | |
68 | ||
69 | // A null state is valid in some cases! | |
70 | // If rice work flow returned a code that LUM is not going to process, then | |
71 | // our ProgramPostProcessorBase will return null | |
72 | // So, if we see a null, assume we are not supposed to process the code and simply return from | |
73 | // the method without changing state | |
74 | // 1. Blanket Approve Proposal | |
75 | // 1.1 When blanket approved is selected, rice will change status through the following codes: | |
76 | // 1.2 Workflow Status Code = R ... Change LUM state to "Draft", create new version, update state of all objects | |
77 | // 1.3 Workflow Status Code = A ... Code 'A' is not processed. Do not change state in LUM. | |
78 | // 1.4 Workflow Status Code = P ... Change LUM state to "Approved", do not create new version, update requirements and variations to state "Approved" | |
79 | ||
80 | 0 | if (newState == null){ |
81 | 0 | return; |
82 | } | |
83 | ||
84 | ||
85 | ||
86 | ||
87 | ||
88 | // The version selected in the UI | |
89 | 0 | MajorDisciplineInfo selectedVersion = programService.getMajorDiscipline(majorDisciplineId); |
90 | ||
91 | // If we are activating this version we need to mark the previous version superseded, | |
92 | // update the previous version end terms, and make the selected version current. | |
93 | 0 | if (newState.equals(DtoConstants.STATE_ACTIVE)) { |
94 | ||
95 | // Find the previous version | |
96 | 0 | MajorDisciplineInfo previousVersion = findPreviousVersion(selectedVersion); |
97 | ||
98 | 0 | if (previousVersion != null) { |
99 | ||
100 | // Set end terms on previous version | |
101 | 0 | setEndTerms(previousVersion, endEntryTerm, endEnrollTerm, endInstAdmitTerm); |
102 | ||
103 | // Mark previous version as superseded and update state on all associated objects | |
104 | 0 | updateMajorDisciplineInfoState(previousVersion, DtoConstants.STATE_SUPERSEDED); |
105 | } | |
106 | ||
107 | // Update state of all associated objects for current version | |
108 | // NOTE: we must update state BEFORE making the version current | |
109 | 0 | updateMajorDisciplineInfoState(selectedVersion, newState); |
110 | ||
111 | // Make this the current version | |
112 | 0 | makeCurrent(selectedVersion); |
113 | 0 | } else { |
114 | ||
115 | // Update state of all associated objects for current version | |
116 | 0 | updateMajorDisciplineInfoState(selectedVersion, newState); |
117 | } | |
118 | ||
119 | ||
120 | ||
121 | 0 | } |
122 | ||
123 | /** | |
124 | * This method updates the end terms for the major discipline passed into it. | |
125 | * <p> | |
126 | * You must still call updateState() to save the object using the web service. | |
127 | * | |
128 | * @param majorDisciplineInfo | |
129 | * @param endEntryTerm | |
130 | * @param endEnrollTerm | |
131 | * @param endInstAdmitTerm | |
132 | * @throws OperationFailedException | |
133 | * @throws MissingParameterException | |
134 | * @throws InvalidParameterException | |
135 | * @throws DoesNotExistException | |
136 | */ | |
137 | private void setEndTerms(MajorDisciplineInfo majorDisciplineInfo, String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm) throws InvalidParameterException, MissingParameterException, OperationFailedException, DoesNotExistException { | |
138 | ||
139 | //Set the end terms on the major discipline | |
140 | 0 | majorDisciplineInfo.setEndProgramEntryTerm(endEntryTerm); |
141 | 0 | majorDisciplineInfo.setEndTerm(endEnrollTerm); |
142 | 0 | majorDisciplineInfo.getAttributes().put("endInstAdmitTerm", endInstAdmitTerm); |
143 | ||
144 | //Check if there are variations to process | |
145 | 0 | if(!majorDisciplineInfo.getVariations().isEmpty()){ |
146 | ||
147 | //Find the major's end term atps and obtain their date information | |
148 | 0 | AtpInfo majorEndEntryTermAtp = atpService.getAtp(endEntryTerm); |
149 | 0 | Date majorEndEntryTermEndDate = majorEndEntryTermAtp.getEndDate(); |
150 | 0 | AtpInfo majorEndEnrollTermAtp = atpService.getAtp(endEnrollTerm); |
151 | 0 | Date majorEndEnrollTermEndDate = majorEndEnrollTermAtp.getEndDate(); |
152 | 0 | AtpInfo majorEndInstAdmitTermAtp = atpService.getAtp(endInstAdmitTerm); |
153 | 0 | Date majorEndInstAdmitTermEndDate = majorEndInstAdmitTermAtp.getEndDate(); |
154 | ||
155 | //Loop through the variations | |
156 | 0 | for(ProgramVariationInfo variation:majorDisciplineInfo.getVariations()){ |
157 | //compare dates to get the older of the two end terms | |
158 | 0 | if(variation.getEndProgramEntryTerm() != null){ |
159 | 0 | AtpInfo variationEndEntryTermAtp = atpService.getAtp(variation.getEndProgramEntryTerm()); |
160 | 0 | Date variationEndEntryTermEndDate = variationEndEntryTermAtp.getEndDate(); |
161 | 0 | if(majorEndEnrollTermEndDate.compareTo(variationEndEntryTermEndDate)<=0){ |
162 | 0 | variation.setEndProgramEntryTerm(endEntryTerm); |
163 | } | |
164 | 0 | }else{ |
165 | 0 | variation.setEndProgramEntryTerm(endEntryTerm); |
166 | } | |
167 | //compare dates to get the older of the two end terms | |
168 | 0 | if(variation.getEndTerm() != null){ |
169 | 0 | AtpInfo variationEndTermAtp = atpService.getAtp(variation.getEndTerm()); |
170 | 0 | Date variationEndTermEndDate = variationEndTermAtp.getEndDate(); |
171 | 0 | if(majorEndEntryTermEndDate.compareTo(variationEndTermEndDate)<=0){ |
172 | 0 | variation.setEndTerm(endEnrollTerm); |
173 | } | |
174 | 0 | }else{ |
175 | 0 | variation.setEndTerm(endEnrollTerm); |
176 | } | |
177 | //compare dates to get the older of the two end terms | |
178 | 0 | if(variation.getAttributes().get("endInstAdmitTerm") != null){ |
179 | 0 | AtpInfo variationEndInstAdmitAtp = atpService.getAtp(variation.getAttributes().get("endInstAdmitTerm")); |
180 | 0 | Date variationEndInstAdmitEndDate = variationEndInstAdmitAtp.getEndDate(); |
181 | 0 | if(majorEndInstAdmitTermEndDate.compareTo(variationEndInstAdmitEndDate)<=0){ |
182 | 0 | variation.getAttributes().put("endInstAdmitTerm", endInstAdmitTerm); |
183 | } | |
184 | 0 | }else{ |
185 | 0 | variation.getAttributes().put("endInstAdmitTerm", endInstAdmitTerm); |
186 | } | |
187 | ||
188 | } | |
189 | } | |
190 | 0 | } |
191 | ||
192 | /** | |
193 | * This method will update the state of this object and all associated objects. | |
194 | * <p> | |
195 | * It is needed because we need to make separate web service calls to update the state of these objects. | |
196 | * | |
197 | * @param majorDisciplineInfo | |
198 | * @param newState | |
199 | */ | |
200 | private void updateMajorDisciplineInfoState(MajorDisciplineInfo majorDisciplineInfo, String newState) throws Exception { | |
201 | // Update the statement tree | |
202 | 0 | List<String> programRequirementIds = majorDisciplineInfo.getProgramRequirements(); |
203 | 0 | updateRequirementsState(programRequirementIds, newState); |
204 | ||
205 | ||
206 | // Update any variations | |
207 | 0 | List<ProgramVariationInfo> variationList = majorDisciplineInfo.getVariations(); |
208 | 0 | updateVariationsRequirementsState(variationList, newState); |
209 | ||
210 | ||
211 | // Update major discipline | |
212 | 0 | majorDisciplineInfo.setState(newState); |
213 | 0 | programService.updateMajorDiscipline(majorDisciplineInfo); |
214 | 0 | } |
215 | ||
216 | /** | |
217 | * This method will make this version of the major discipline the current one. | |
218 | * | |
219 | * @param majorDisciplineInfo | |
220 | */ | |
221 | private void makeCurrent(MajorDisciplineInfo majorDisciplineInfo) throws Exception { | |
222 | ||
223 | // Check if this is the current version before trying to make it current | |
224 | // (the web service will error if you try to make a version current that is already current) | |
225 | 0 | VersionDisplayInfo currentVersion = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorDisciplineInfo.getVersionInfo().getVersionIndId()); |
226 | ||
227 | // If this is not the current version, then make it current | |
228 | 0 | if (!currentVersion.getSequenceNumber().equals(majorDisciplineInfo.getVersionInfo().getSequenceNumber())) { |
229 | 0 | programService.setCurrentMajorDisciplineVersion(majorDisciplineInfo.getId(), null); |
230 | } | |
231 | 0 | } |
232 | ||
233 | /** | |
234 | * This method finds the previous version (the version right before this one). | |
235 | * <p> | |
236 | * e.g. v1 = ACTIVE v2 = APPROVED | |
237 | * <p> | |
238 | * If you passed v2 into this method, it would return v1. | |
239 | * <p> | |
240 | * If there is only one major discipline this method will return null. | |
241 | * | |
242 | * @param majorDisciplineInfo | |
243 | * @return | |
244 | */ | |
245 | private MajorDisciplineInfo findPreviousVersion(MajorDisciplineInfo majorDisciplineInfo) throws Exception { | |
246 | // Find all previous versions using the version independent indicator | |
247 | 0 | List<VersionDisplayInfo> versions = programService.getVersions(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorDisciplineInfo.getVersionInfo().getVersionIndId()); |
248 | ||
249 | // Take the sequence number for this version | |
250 | 0 | Long sequenceNumber = majorDisciplineInfo.getVersionInfo().getSequenceNumber(); |
251 | ||
252 | // And subtract 1 from the sequence number to get the previous version | |
253 | 0 | sequenceNumber -= 1; |
254 | ||
255 | // Loop over all versions and find the previous version based on the sequence number | |
256 | /* | |
257 | * NOTE: Dan suggested we loop over all versions and change any version with state=active to state=superseded. | |
258 | * However, we decided not to go that route because we would need to pull back all data for each version to determine | |
259 | * if a version is active, since versioninfo does not have a getState() method | |
260 | */ | |
261 | 0 | MajorDisciplineInfo previousVersion = null; |
262 | 0 | for (VersionDisplayInfo versionInfo : versions) { |
263 | 0 | if (versionInfo.getSequenceNumber().equals(sequenceNumber)) { |
264 | 0 | previousVersion = programService.getMajorDiscipline(versionInfo.getId()); |
265 | 0 | break; |
266 | } | |
267 | } | |
268 | 0 | return previousVersion; |
269 | } | |
270 | ||
271 | /** | |
272 | * This method will update the requirement state. | |
273 | * <p> | |
274 | * Note that it uses StatementUtil to update the statement tree. | |
275 | * | |
276 | * @param majorDisciplineInfo | |
277 | * @param newState | |
278 | * @throws Exception | |
279 | */ | |
280 | public void updateRequirementsState(List<String> programRequirementIds, String newState) throws Exception { | |
281 | ||
282 | 0 | for (String programRequirementId : programRequirementIds) { |
283 | ||
284 | // Get program requirement from the program service | |
285 | 0 | ProgramRequirementInfo programRequirementInfo = programService.getProgramRequirement(programRequirementId, null, null); |
286 | ||
287 | // Look in the requirement for the statement tree | |
288 | 0 | StatementTreeViewInfo statementTree = programRequirementInfo.getStatement(); |
289 | ||
290 | // And recursively update the entire tree with the new state | |
291 | 0 | StatementUtil.updateStatementTreeViewInfoState(newState, statementTree); |
292 | ||
293 | // Update the state of the requirement object | |
294 | 0 | programRequirementInfo.setState(newState); |
295 | ||
296 | // The write the requirement back to the program service | |
297 | 0 | programService.updateProgramRequirement(programRequirementInfo); |
298 | ||
299 | 0 | } |
300 | 0 | } |
301 | ||
302 | /** | |
303 | * This method will update the requirements of each variation. | |
304 | * <p> | |
305 | * We need to do this here as opposed to in the assemblers, since we need | |
306 | * to make calls out to a separate web service. Also, this needs to be done here | |
307 | * because changing state no longer calls the save function. | |
308 | * <p> | |
309 | * Note that core and credential programs do not have variations so | |
310 | * this method isn't necessary. | |
311 | * <p> | |
312 | * | |
313 | * @param majorDisciplineInfo | |
314 | * @param newState | |
315 | * @throws Exception | |
316 | */ | |
317 | public void updateVariationsRequirementsState(List<ProgramVariationInfo> variationList, String newState) throws Exception { | |
318 | ||
319 | // Iterate over all variations | |
320 | 0 | for (ProgramVariationInfo variation : variationList) { |
321 | ||
322 | // Get the requirements | |
323 | 0 | List<String> programRequirementIds = variation.getProgramRequirements(); |
324 | ||
325 | // Call the method that will update the requirements state for the program | |
326 | // This will also update the statement tree | |
327 | 0 | updateRequirementsState(programRequirementIds, newState); |
328 | 0 | } |
329 | 0 | } |
330 | ||
331 | ||
332 | /** | |
333 | * This method is used by Spring to inject the program service into this bean. | |
334 | * | |
335 | * @param programService | |
336 | */ | |
337 | public void setProgramService(ProgramService programService) { | |
338 | 0 | this.programService = programService; |
339 | 0 | } |
340 | ||
341 | public void setAtpService(AtpService atpService) { | |
342 | 0 | this.atpService = atpService; |
343 | 0 | } |
344 | ||
345 | } |