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