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