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