1 package org.kuali.student.lum.workflow;
2
3 import org.kuali.student.r1.core.statement.dto.StatementTreeViewInfo;
4 import org.kuali.student.r2.common.dto.AttributeInfo;
5 import org.kuali.student.r2.common.dto.ContextInfo;
6 import org.kuali.student.r2.common.dto.DtoConstants;
7 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
8 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
9 import org.kuali.student.r2.common.exceptions.MissingParameterException;
10 import org.kuali.student.r2.common.exceptions.OperationFailedException;
11 import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
12 import org.kuali.student.r2.common.util.constants.ProgramServiceConstants;
13 import org.kuali.student.r2.core.atp.dto.AtpInfo;
14 import org.kuali.student.r2.core.atp.service.AtpService;
15 import org.kuali.student.r2.core.versionmanagement.dto.VersionDisplayInfo;
16 import org.kuali.student.r2.lum.program.dto.MajorDisciplineInfo;
17 import org.kuali.student.r2.lum.program.dto.ProgramRequirementInfo;
18 import org.kuali.student.r2.lum.program.dto.ProgramVariationInfo;
19 import org.kuali.student.r2.lum.program.service.ProgramService;
20 import org.springframework.transaction.annotation.Transactional;
21
22 import java.util.Date;
23 import java.util.List;
24
25
26
27
28
29
30
31
32
33
34
35
36 @Transactional(noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class})
37 public class MajorDisciplineStateChangeServiceImpl implements StateChangeService{
38
39
40
41
42 private ProgramService programService;
43 private AtpService atpService;
44
45
46
47
48
49
50
51
52
53 public void changeState(String majorDisciplineId, String newState, ContextInfo contextInfo) throws Exception {
54
55
56 changeState(null, null, null, majorDisciplineId, newState, contextInfo);
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70 public void changeState(String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm, String majorDisciplineId, String newState, ContextInfo contextInfo) throws Exception {
71
72
73
74
75
76
77
78
79
80
81
82
83 if (newState == null){
84 return;
85 }
86
87
88 MajorDisciplineInfo selectedVersion = programService.getMajorDiscipline(majorDisciplineId, contextInfo);
89
90
91
92 if (newState.equals(DtoConstants.STATE_ACTIVE)) {
93
94
95 updatePreviousVersions(selectedVersion, endEntryTerm, endEnrollTerm, endInstAdmitTerm, contextInfo);
96
97
98
99 updateMajorDisciplineInfoState(selectedVersion, newState, contextInfo);
100
101
102 makeCurrent(selectedVersion, contextInfo);
103 } else {
104
105
106 updateMajorDisciplineInfoState(selectedVersion, newState, contextInfo);
107 }
108
109
110
111 }
112
113
114
115
116
117
118
119
120
121
122 private void updateMajorDisciplineInfoState(MajorDisciplineInfo majorDisciplineInfo, String newState, ContextInfo contextInfo) throws Exception {
123
124 List<String> programRequirementIds = majorDisciplineInfo.getProgramRequirements();
125 updateRequirementsState(programRequirementIds, newState, contextInfo);
126
127
128
129 List<ProgramVariationInfo> variationList = majorDisciplineInfo.getVariations();
130 updateVariationsRequirementsState(variationList, newState, contextInfo);
131
132
133
134 majorDisciplineInfo.setStateKey(newState);
135 programService.updateMajorDiscipline(majorDisciplineInfo.getId(), majorDisciplineInfo, contextInfo);
136 }
137
138
139
140
141
142
143
144 private void makeCurrent(MajorDisciplineInfo majorDisciplineInfo, ContextInfo contextInfo) throws Exception {
145
146
147
148 VersionDisplayInfo currentVersion = null;
149 currentVersion = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorDisciplineInfo.getVersion().getVersionIndId(), contextInfo);
150
151
152 if (!currentVersion.getSequenceNumber().equals(majorDisciplineInfo.getVersion().getSequenceNumber())) {
153 programService.setCurrentMajorDisciplineVersion(majorDisciplineInfo.getId(), null, contextInfo);
154 }
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168 private void updatePreviousVersions (MajorDisciplineInfo selectedVersion, String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm, ContextInfo contextInfo) throws Exception {
169
170 MajorDisciplineInfo currentVersion = getCurrentVersion(selectedVersion, contextInfo);
171
172 boolean isSelectedVersionCurrent = selectedVersion.getId().equals(currentVersion.getId());
173
174
175 setEndTerms(currentVersion, endEntryTerm, endEnrollTerm, endInstAdmitTerm, contextInfo);
176 updateMajorDisciplineInfoState(currentVersion, DtoConstants.STATE_SUPERSEDED, contextInfo);
177
178
179
180
181
182 List<VersionDisplayInfo> versions = null;
183 versions = programService.getVersions(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, selectedVersion.getVersion().getVersionIndId(), contextInfo);
184
185 Long startSeq = new Long(1);
186
187 if (!isSelectedVersionCurrent) {
188 startSeq = currentVersion.getVersion().getSequenceNumber() + 1;
189 }
190
191 for (VersionDisplayInfo versionInfo : versions) {
192 if (versionInfo.getSequenceNumber() >= startSeq && versionInfo.getSequenceNumber() != selectedVersion.getVersion().getSequenceNumber()) {
193 MajorDisciplineInfo otherProgram = null;
194 otherProgram = programService.getMajorDiscipline(versionInfo.getId(), contextInfo);
195 if (otherProgram.getStateKey().equals(DtoConstants.STATE_APPROVED) ||
196 otherProgram.getStateKey().equals(DtoConstants.STATE_ACTIVE)){
197 updateMajorDisciplineInfoState(otherProgram, DtoConstants.STATE_SUPERSEDED, contextInfo);
198 }
199 }
200 }
201
202 }
203
204
205
206
207
208
209
210 protected MajorDisciplineInfo getCurrentVersion(MajorDisciplineInfo majorDisciplineInfo, ContextInfo contextInfo)
211 throws Exception {
212
213 String verIndId = majorDisciplineInfo.getVersion().getVersionIndId();
214
215
216 VersionDisplayInfo curVerDisplayInfo = null;
217 curVerDisplayInfo = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, verIndId, contextInfo);
218
219 String curVerId = null;
220
221 curVerId = curVerDisplayInfo.getId();
222
223
224 MajorDisciplineInfo currentVersion = programService.getMajorDiscipline(curVerId, contextInfo);
225
226 return currentVersion;
227 }
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 private void setEndTerms(MajorDisciplineInfo majorDisciplineInfo, String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm, ContextInfo contextInfo) throws InvalidParameterException, MissingParameterException, OperationFailedException, DoesNotExistException, PermissionDeniedException {
244
245
246 majorDisciplineInfo.setEndProgramEntryTerm(endEntryTerm);
247 majorDisciplineInfo.setEndTerm(endEnrollTerm);
248 majorDisciplineInfo.getAttributes().add(new AttributeInfo("endInstAdmitTerm", endInstAdmitTerm));
249
250
251 if(!majorDisciplineInfo.getVariations().isEmpty()){
252
253
254 Date majorEndEntryTermEndDate = null;
255 Date majorEndEnrollTermEndDate = null;
256 Date majorEndInstAdmitTermEndDate = null;
257 if(endEntryTerm != null){
258 AtpInfo majorEndEntryTermAtp = atpService.getAtp(endEntryTerm, contextInfo);
259 majorEndEntryTermEndDate = majorEndEntryTermAtp.getEndDate();
260 }
261 if(endEnrollTerm != null){
262 AtpInfo majorEndEnrollTermAtp = atpService.getAtp(endEnrollTerm, contextInfo);
263 majorEndEnrollTermEndDate = majorEndEnrollTermAtp.getEndDate();
264 }
265 if(endInstAdmitTerm != null){
266 AtpInfo majorEndInstAdmitTermAtp = atpService.getAtp(endInstAdmitTerm, contextInfo);
267 majorEndInstAdmitTermEndDate = majorEndInstAdmitTermAtp.getEndDate();
268 }
269
270 for(ProgramVariationInfo variation:majorDisciplineInfo.getVariations()){
271
272 if(variation.getEndProgramEntryTerm() != null && majorEndEntryTermEndDate != null){
273 AtpInfo variationEndEntryTermAtp = atpService.getAtp(variation.getEndProgramEntryTerm(), contextInfo);
274 Date variationEndEntryTermEndDate = variationEndEntryTermAtp.getEndDate();
275 if(majorEndEntryTermEndDate.compareTo(variationEndEntryTermEndDate)<=0){
276 variation.setEndProgramEntryTerm(endEntryTerm);
277 }
278 }else{
279 variation.setEndProgramEntryTerm(endEntryTerm);
280 }
281
282 if(variation.getEndTerm() != null && majorEndEnrollTermEndDate != null){
283 AtpInfo variationEndTermAtp = atpService.getAtp(variation.getEndTerm(), contextInfo);
284 Date variationEndTermEndDate = variationEndTermAtp.getEndDate();
285 if(majorEndEnrollTermEndDate.compareTo(variationEndTermEndDate)<=0){
286 variation.setEndTerm(endEnrollTerm);
287 }
288 }else{
289 variation.setEndTerm(endEnrollTerm);
290 }
291
292 endInstAdmitTerm = variation.getAttributeValue("endInstAdmitTerm");
293 if(endInstAdmitTerm != null && majorEndInstAdmitTermEndDate != null){
294 AtpInfo variationEndInstAdmitAtp = atpService.getAtp(endInstAdmitTerm, contextInfo);
295 Date variationEndInstAdmitEndDate = variationEndInstAdmitAtp.getEndDate();
296 if(majorEndInstAdmitTermEndDate.compareTo(variationEndInstAdmitEndDate)<=0){
297 variation.getAttributes().add(new AttributeInfo("endInstAdmitTerm", endInstAdmitTerm));
298 }
299 }else{
300 variation.getAttributes().add(new AttributeInfo("endInstAdmitTerm", endInstAdmitTerm));
301 }
302
303 }
304 }
305 }
306
307
308
309
310
311
312
313
314
315
316
317 public void updateRequirementsState(List<String> programRequirementIds, String newState, ContextInfo contextInfo) throws Exception {
318
319 for (String programRequirementId : programRequirementIds) {
320
321
322 ProgramRequirementInfo programRequirementInfo = programService.getProgramRequirement(programRequirementId, contextInfo);
323
324
325 StatementTreeViewInfo statementTree = programRequirementInfo.getStatement();
326
327
328 StatementUtil.updateStatementTreeViewInfoState(newState, statementTree);
329
330
331 programRequirementInfo.setStateKey(newState);
332
333
334 programService.updateProgramRequirement(programRequirementId, programRequirementInfo.getTypeKey(), programRequirementInfo, contextInfo);
335
336 }
337 }
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354 public void updateVariationsRequirementsState(List<ProgramVariationInfo> variationList, String newState, ContextInfo contextInfo) throws Exception {
355
356
357 for (ProgramVariationInfo variation : variationList) {
358
359
360 List<String> programRequirementIds = variation.getProgramRequirements();
361
362
363
364 updateRequirementsState(programRequirementIds, newState, contextInfo);
365 }
366 }
367
368
369
370
371
372
373
374 public void setProgramService(ProgramService programService) {
375 this.programService = programService;
376 }
377
378 public void setAtpService(AtpService atpService) {
379 this.atpService = atpService;
380 }
381 }