1 package org.kuali.student.lum.program.server;
2
3 import java.util.List;
4
5 import org.kuali.student.lum.common.server.StatementUtil;
6 import org.kuali.student.lum.program.client.ProgramConstants;
7 import org.kuali.student.r1.core.statement.dto.StatementTreeViewInfo;
8 import org.kuali.student.r2.common.dto.AttributeInfo;
9 import org.kuali.student.r2.common.dto.DtoConstants;
10 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
11 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
12 import org.kuali.student.r2.common.util.ContextUtils;
13 import org.kuali.student.r2.common.util.constants.ProgramServiceConstants;
14 import org.kuali.student.r2.core.versionmanagement.dto.VersionDisplayInfo;
15 import org.kuali.student.r2.lum.program.dto.MajorDisciplineInfo;
16 import org.kuali.student.r2.lum.program.dto.ProgramRequirementInfo;
17 import org.kuali.student.r2.lum.program.service.ProgramService;
18 import org.springframework.transaction.annotation.Transactional;
19
20
21
22
23
24
25
26
27
28
29
30 @Transactional(noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class})
31 public class ProgramStateChangeServiceImpl {
32
33
34
35
36 private ProgramService programService;
37
38
39
40
41
42
43
44
45
46
47
48 public void changeState( String majorDisciplineId, String newState) throws Exception {
49
50
51 changeState(null, null, null, majorDisciplineId, newState);
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65 public void changeState(String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm, String majorDisciplineId, String newState) throws Exception {
66
67
68 if (newState == null)
69 throw new InvalidParameterException("new state cannot be null");
70
71
72 MajorDisciplineInfo selectedVersion = programService.getMajorDiscipline(majorDisciplineId,ContextUtils.getContextInfo());
73
74
75
76 if (newState.equals(DtoConstants.STATE_ACTIVE)) {
77
78
79 MajorDisciplineInfo previousVersion = findPreviousVersion(selectedVersion);
80
81 if (previousVersion != null) {
82
83
84 setEndTerms(previousVersion, endEntryTerm, endEnrollTerm, endInstAdmitTerm);
85
86
87 updateMajorDisciplineInfoState(previousVersion, DtoConstants.STATE_SUPERSEDED);
88 }
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 setEndTerms(MajorDisciplineInfo majorDisciplineInfo, String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm) {
116 majorDisciplineInfo.setEndProgramEntryTerm(endEntryTerm);
117 majorDisciplineInfo.setEndTerm(endEnrollTerm);
118 majorDisciplineInfo.getAttributes().add(new AttributeInfo(ProgramConstants.END_INSTITUTIONAL_ADMIT_TERM, endInstAdmitTerm));
119 }
120
121
122
123
124
125
126
127
128
129 private void updateMajorDisciplineInfoState(MajorDisciplineInfo majorDisciplineInfo, String newState) throws Exception {
130
131 updateRequirementsState(majorDisciplineInfo, newState);
132
133
134 majorDisciplineInfo.setStateKey(newState);
135 programService.updateMajorDiscipline(majorDisciplineInfo.getId(), majorDisciplineInfo, ContextUtils.getContextInfo());
136 }
137
138
139
140
141
142
143 private void makeCurrent(MajorDisciplineInfo majorDisciplineInfo) throws Exception {
144
145
146
147 VersionDisplayInfo currentVersion = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorDisciplineInfo.getVersion().getVersionIndId(),ContextUtils.getContextInfo());
148
149
150 if (!currentVersion.getSequenceNumber().equals(majorDisciplineInfo.getVersion().getSequenceNumber())) { programService.setCurrentMajorDisciplineVersion(majorDisciplineInfo.getId(), null,ContextUtils.getContextInfo()); }
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165 private MajorDisciplineInfo findPreviousVersion(MajorDisciplineInfo majorDisciplineInfo) throws Exception {
166
167 List<VersionDisplayInfo> versions = programService.getVersions(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorDisciplineInfo.getVersion().getVersionIndId(),ContextUtils.getContextInfo());
168
169
170 Long sequenceNumber = null;
171
172 sequenceNumber = majorDisciplineInfo.getVersion().getSequenceNumber();
173
174
175 sequenceNumber -= 1;
176
177
178
179
180
181
182
183
184 MajorDisciplineInfo previousVersion = null;
185 for (VersionDisplayInfo versionInfo : versions) {
186 if (versionInfo.getSequenceNumber().equals(sequenceNumber)) {
187 previousVersion = programService.getMajorDiscipline(versionInfo.getId(),ContextUtils.getContextInfo());
188 break;
189 }
190 }
191 return previousVersion;
192 }
193
194
195
196
197
198
199
200
201
202
203 public void updateRequirementsState(MajorDisciplineInfo majorDisciplineInfo, String newState) throws Exception {
204
205
206 List<String> programRequirementIds = majorDisciplineInfo.getProgramRequirements();
207 for (String programRequirementId : programRequirementIds) {
208
209
210 ProgramRequirementInfo programRequirementInfo = null;
211 programRequirementInfo = programService.getProgramRequirement(programRequirementId, ContextUtils.getContextInfo());
212
213
214 StatementTreeViewInfo statementTree = programRequirementInfo.getStatement();
215
216
217 StatementUtil.updateStatementTreeViewInfoState(newState, statementTree);
218
219
220 programRequirementInfo.setStateKey(newState);
221
222
223
224 programService.updateProgramRequirement(programRequirementInfo.getId(), programRequirementInfo.getTypeKey(), programRequirementInfo, ContextUtils.getContextInfo());
225
226 }
227 }
228
229
230
231
232
233
234 public void setProgramService(ProgramService programService) {
235 this.programService = programService;
236 }
237
238 }