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