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.r2.core.versionmanagement.dto.VersionDisplayInfo;
7 import org.kuali.student.r1.core.statement.dto.StatementTreeViewInfo;
8 import org.kuali.student.r2.common.dto.DtoConstants;
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.util.ContextUtils;
12 import org.kuali.student.r2.common.util.constants.ProgramServiceConstants;
13 import org.kuali.student.r2.lum.program.dto.CredentialProgramInfo;
14 import org.kuali.student.r2.lum.program.dto.ProgramRequirementInfo;
15 import org.kuali.student.r2.lum.program.service.ProgramService;
16 import org.springframework.transaction.annotation.Transactional;
17
18
19
20
21
22
23
24
25
26
27
28
29 @Transactional(noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class})
30 public class CredentialProgramStateChangeServiceImpl implements StateChangeService {
31
32
33
34
35 private ProgramService programService;
36
37
38
39
40
41
42
43
44
45 public void changeState(String credentialProgramId, String newState) throws Exception {
46
47
48 changeState(null, null, null, credentialProgramId, newState);
49 }
50
51
52
53
54
55
56
57
58
59
60
61
62 public void changeState(String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm, String credentialProgramId, String newState) throws Exception {
63
64
65 if (newState == null)
66 throw new InvalidParameterException("new state cannot be null");
67
68
69 CredentialProgramInfo selectedVersion = programService.getCredentialProgram(credentialProgramId,ContextUtils.getContextInfo());
70
71
72
73 if (newState.equals(DtoConstants.STATE_ACTIVE)) {
74
75
76 updatePreviousVersions(selectedVersion, endEntryTerm, endEnrollTerm, endInstAdmitTerm);
77
78
79
80 updateCredentialProgramInfoState(selectedVersion, newState);
81
82
83 makeCurrent(selectedVersion);
84 } else {
85
86
87 updateCredentialProgramInfoState(selectedVersion, newState);
88 }
89
90 }
91
92
93
94
95
96
97
98
99
100
101 private void updatePreviousVersions (CredentialProgramInfo selectedVersion, String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm) throws Exception {
102
103 CredentialProgramInfo currentVersion = getCurrentVersion(selectedVersion);
104
105 boolean isSelectedVersionCurrent = selectedVersion.getId().equals(currentVersion.getId());
106
107
108 setEndTerms(currentVersion, endEntryTerm, endEnrollTerm);
109 updateCredentialProgramInfoState(currentVersion, DtoConstants.STATE_SUPERSEDED);
110
111
112
113
114
115 List<VersionDisplayInfo> versions = programService.getVersions(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI,
116 selectedVersion.getVersionInfo(ContextUtils.getContextInfo()).getVersionIndId(),ContextUtils.getContextInfo());
117 Long startSeq = new Long(1);
118
119 if (!isSelectedVersionCurrent) {
120 startSeq = currentVersion.getVersionInfo(ContextUtils.getContextInfo()).getSequenceNumber() + 1;
121 }
122
123 for (VersionDisplayInfo versionInfo : versions) {
124 boolean isVersionNewerThanCurrentVersion = versionInfo.getSequenceNumber() >= startSeq;
125 boolean isVersionSelectedVersion = versionInfo.getSequenceNumber().equals(selectedVersion.getVersionInfo(ContextUtils.getContextInfo()).getSequenceNumber());
126 boolean updateState = isVersionNewerThanCurrentVersion && !isVersionSelectedVersion;
127 if (updateState) {
128 CredentialProgramInfo otherProgram = programService.getCredentialProgram(versionInfo.getId(),ContextUtils.getContextInfo());
129 if (otherProgram.getStateKey().equals(DtoConstants.STATE_APPROVED) ||
130 otherProgram.getStateKey().equals(DtoConstants.STATE_ACTIVE)){
131 updateCredentialProgramInfoState(otherProgram, DtoConstants.STATE_SUPERSEDED);
132 }
133 }
134 }
135
136 }
137
138
139
140
141
142
143 protected CredentialProgramInfo getCurrentVersion(CredentialProgramInfo credentialProgramInfo)
144 throws Exception {
145
146 String verIndId = credentialProgramInfo.getVersionInfo(ContextUtils.getContextInfo()).getVersionIndId();
147
148
149 VersionDisplayInfo curVerDisplayInfo = programService.getCurrentVersion( ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, verIndId,ContextUtils.getContextInfo());
150 String curVerId = curVerDisplayInfo.getId();
151
152
153 CredentialProgramInfo currentVersion = programService.getCredentialProgram(curVerId,ContextUtils.getContextInfo());
154
155 return currentVersion;
156 }
157
158
159
160
161
162
163
164
165
166
167 private void setEndTerms(CredentialProgramInfo credentialProgramInfo, String endEntryTerm, String endEnrollTerm) {
168 credentialProgramInfo.setEndProgramEntryTerm(endEntryTerm);
169 credentialProgramInfo.setEndTerm(endEnrollTerm);
170 }
171
172
173
174
175
176
177
178
179
180 private void updateCredentialProgramInfoState(CredentialProgramInfo credentialProgramInfo, String newState) throws Exception {
181
182 List<String> programRequirementIds = credentialProgramInfo.getProgramRequirements();
183 updateRequirementsState(programRequirementIds, newState);
184
185
186
187
188 credentialProgramInfo.setStateKey(newState);
189 programService.updateCredentialProgram(credentialProgramInfo.getId(), credentialProgramInfo, ContextUtils.getContextInfo());
190 }
191
192
193
194
195
196
197 private void makeCurrent(CredentialProgramInfo credentialProgramInfo) throws Exception {
198
199
200
201 VersionDisplayInfo currentVersion = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, credentialProgramInfo.getVersionInfo(ContextUtils.getContextInfo()).getVersionIndId(),ContextUtils.getContextInfo());
202
203
204 if (!currentVersion.getSequenceNumber().equals(credentialProgramInfo.getVersionInfo(ContextUtils.getContextInfo()).getSequenceNumber())) {
205 programService.setCurrentCredentialProgramVersion(credentialProgramInfo.getId(), null,ContextUtils.getContextInfo());
206 }
207 }
208
209
210
211
212
213
214
215
216
217
218 public void updateRequirementsState(List<String> programRequirementIds, String newState) throws Exception {
219
220 for (String programRequirementId : programRequirementIds) {
221
222
223 ProgramRequirementInfo programRequirementInfo = null;
224 programRequirementInfo = programService.getProgramRequirement(programRequirementId, ContextUtils.getContextInfo());
225
226
227 StatementTreeViewInfo statementTree = programRequirementInfo.getStatement();
228
229
230 StatementUtil.updateStatementTreeViewInfoState(newState, statementTree);
231
232
233 programRequirementInfo.setStateKey(newState);
234
235
236 programService.updateProgramRequirement(programRequirementInfo.getId(), programRequirementInfo.getTypeKey(), programRequirementInfo, ContextUtils.getContextInfo());
237
238 }
239 }
240
241
242
243
244
245
246 public void setProgramService(ProgramService programService) {
247 this.programService = programService;
248 }
249
250 }