1 package org.kuali.student.enrollment.class2.acal.service;
2
3 import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
4 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
5 import org.kuali.rice.krad.maintenance.MaintainableImpl;
6 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
7 import org.kuali.rice.krad.util.KRADConstants;
8 import org.kuali.student.enrollment.acal.dto.KeyDateInfo;
9 import org.kuali.student.enrollment.acal.dto.TermInfo;
10 import org.kuali.student.enrollment.acal.service.AcademicCalendarService;
11 import org.kuali.student.enrollment.class2.acal.dto.TermWrapper;
12 import org.kuali.student.r2.common.dto.ContextInfo;
13 import org.kuali.student.r2.common.exceptions.*;
14 import org.kuali.student.r2.core.constants.AtpServiceConstants;
15
16 import javax.xml.namespace.QName;
17 import java.util.Calendar;
18 import java.util.Date;
19 import java.util.List;
20 import java.util.Map;
21
22
23 @Deprecated
24 public class TermWrapperMaintainableImpl extends MaintainableImpl {
25 private static final long serialVersionUID = 1L;
26
27 public final static String TERM_KEY = "key";
28
29 public final static String TERM_KEY_PREFIX = "kuali.term.";
30
31 public final static String TERM_TYPE_KEY_PREFIX = "kuali.atp.type.";
32 public final static String MILESTONE_TYPE_KEY_PREFIX = "kuali.atp.milestone.";
33 public final static String KEY_DATE_INFO_KEY_PREFIX = "kuali.milestone.";
34
35
36 private transient AcademicCalendarService academicCalendarService;
37
38 @Override
39 public void saveDataObject() {
40 System.out.println(">>In TermWrapperMaintainableImpl.saveDataObject()");
41 TermWrapper termWrapper = (TermWrapper)getDataObject();
42 TermInfo termInfo = termWrapper.getTermInfo();
43 String termKey = getTermInfoKey (termInfo);
44 System.out.println(">>>termKey = "+termKey);
45 termInfo.setStateKey(AtpServiceConstants.ATP_OFFICIAL_STATE_KEY);
46
47 KeyDateInfo classesMeetDates = termWrapper.getClassesMeetDates();
48 classesMeetDates.setStateKey(AtpServiceConstants.MILESTONE_OFFICIAL_STATE_KEY);
49 classesMeetDates.setTypeKey(AtpServiceConstants.MILESTONE_INSTRUCTIONAL_PERIOD_TYPE_KEY);
50 String classesMeetDatesKey = getKeyDateInfoKey(classesMeetDates, termKey);
51 classesMeetDates.setId(classesMeetDatesKey);
52
53 KeyDateInfo registrationPeriod = termWrapper.getRegistrationPeriod();
54 registrationPeriod.setStateKey(AtpServiceConstants.MILESTONE_OFFICIAL_STATE_KEY);
55 registrationPeriod.setTypeKey(AtpServiceConstants.MILESTONE_REGISTRATION_PERIOD_TYPE_KEY);
56 String registrationPeriodKey = getKeyDateInfoKey(registrationPeriod, termKey);
57 registrationPeriod.setId(registrationPeriodKey);
58
59
60
61 KeyDateInfo dropPeriodEndsDate = termWrapper.getDropPeriodEndsDate();
62 dropPeriodEndsDate.setStateKey(AtpServiceConstants.MILESTONE_OFFICIAL_STATE_KEY);
63 dropPeriodEndsDate.setTypeKey(AtpServiceConstants.MILESTONE_DROP_DATE_TYPE_KEY);
64 String dropPeriodEndsDateKey = getKeyDateInfoKey(dropPeriodEndsDate, termKey);
65 dropPeriodEndsDate.setId(dropPeriodEndsDateKey);
66
67
68 KeyDateInfo finalExaminationsDates = termWrapper.getFinalExaminationsDates();
69 finalExaminationsDates.setStateKey(AtpServiceConstants.MILESTONE_OFFICIAL_STATE_KEY);
70 finalExaminationsDates.setTypeKey(AtpServiceConstants.MILESTONE_FINAL_EXAM_PERIOD_TYPE_KEY);
71 String finalExaminationsDatesKey = getKeyDateInfoKey(finalExaminationsDates, termKey);
72 finalExaminationsDates.setId(finalExaminationsDatesKey);
73
74
75 KeyDateInfo gradesDueDate = termWrapper.getGradesDueDate();
76 gradesDueDate.setStateKey(AtpServiceConstants.MILESTONE_OFFICIAL_STATE_KEY);
77 gradesDueDate.setTypeKey(AtpServiceConstants.MILESTONE_GRADES_DUE_TYPE_KEY);
78 String gradesDueDateKey = getKeyDateInfoKey(gradesDueDate, termKey);
79 gradesDueDate.setId(gradesDueDateKey);
80
81 academicCalendarService = getAcademicCalendarService();
82 ContextInfo context = new ContextInfo();
83
84 try{
85 if(getMaintenanceAction().equals(KRADConstants.MAINTENANCE_NEW_ACTION) ||
86 getMaintenanceAction().equals(KRADConstants.MAINTENANCE_COPY_ACTION)) {
87 academicCalendarService.createTerm(termKey, termInfo, context);
88 academicCalendarService.createKeyDate(termKey, classesMeetDatesKey, classesMeetDates, context);
89 academicCalendarService.createKeyDate(termKey, registrationPeriodKey, registrationPeriod, context);
90 academicCalendarService.createKeyDate(termKey, dropPeriodEndsDateKey, dropPeriodEndsDate, context);
91 academicCalendarService.createKeyDate(termKey, finalExaminationsDatesKey, finalExaminationsDates, context);
92 academicCalendarService.createKeyDate(termKey, gradesDueDateKey, gradesDueDate, context);
93
94 }
95 else {
96 getAcademicCalendarService().updateTerm(termKey, termInfo, new ContextInfo());
97 academicCalendarService.updateKeyDate(classesMeetDatesKey, classesMeetDates, context);
98 academicCalendarService.updateKeyDate(registrationPeriodKey, registrationPeriod, context);
99 academicCalendarService.updateKeyDate(dropPeriodEndsDateKey, dropPeriodEndsDate, context);
100 academicCalendarService.updateKeyDate(finalExaminationsDatesKey, finalExaminationsDates, context);
101 academicCalendarService.updateKeyDate(gradesDueDateKey, gradesDueDate, context);
102 }
103 }catch (DataValidationErrorException dvee){
104
105 }catch (InvalidParameterException ipe){
106
107 }catch (MissingParameterException mpe){
108
109 }catch (OperationFailedException ofe){
110
111 }catch (PermissionDeniedException pde){
112
113 }catch (ReadOnlyException roe){
114
115 }catch (DoesNotExistException dee){
116
117 }catch (VersionMismatchException vme){
118
119 }
120
121 }
122
123 @Override
124 public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
125
126 Object dataObject = null;
127 try {
128
129 TermWrapper termWrapper = new TermWrapper();
130 ContextInfo context = new ContextInfo();
131 String termKey = dataObjectKeys.get(TERM_KEY);
132
133 try {
134 TermInfo termInfo = getAcademicCalendarService().getTerm(termKey, context);
135
136
137 if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
138
139
140
141 termInfo.setId(null);
142 document.setFieldsClearedOnCopy(true);
143 }
144 termWrapper.setTermInfo(termInfo);
145
146 List<KeyDateInfo> keyDateInfoList = getAcademicCalendarService().getKeyDatesForTerm(termKey, new ContextInfo());
147
148 for (KeyDateInfo keyDateInfo : keyDateInfoList){
149 if(AtpServiceConstants.MILESTONE_INSTRUCTIONAL_PERIOD_TYPE_KEY.equals(keyDateInfo.getTypeKey())){
150 termWrapper.setClassesMeetDates(keyDateInfo);
151 }
152 else if(AtpServiceConstants.MILESTONE_REGISTRATION_PERIOD_TYPE_KEY.equals(keyDateInfo.getTypeKey())){
153 termWrapper.setRegistrationPeriod(keyDateInfo);
154 }
155 else if(AtpServiceConstants.MILESTONE_DROP_DATE_TYPE_KEY.equals(keyDateInfo.getTypeKey())){
156 termWrapper.setDropPeriodEndsDate(keyDateInfo);
157 }
158 else if(AtpServiceConstants.MILESTONE_FINAL_EXAM_PERIOD_TYPE_KEY.equals(keyDateInfo.getTypeKey())){
159 termWrapper.setFinalExaminationsDates(keyDateInfo);
160 }
161 else if(AtpServiceConstants.MILESTONE_GRADES_DUE_TYPE_KEY.equals(keyDateInfo.getTypeKey())){
162 termWrapper.setGradesDueDate(keyDateInfo);
163 }
164 }
165 }catch (DoesNotExistException dnee){
166 System.out.println("call getAcademicCalendarService().getKeyDatesForTerm(termKey, context), and get DoesNotExistException: "+dnee.toString());
167 }catch (InvalidParameterException ipe){
168 System.out.println("call getAcademicCalendarService().getKeyDatesForTerm(termKey, context), and get InvalidParameterException: "+ipe.toString());
169 }catch (MissingParameterException mpe){
170 System.out.println("call getAcademicCalendarService().getKeyDatesForTerm(termKey, context), and get MissingParameterException: "+mpe.toString());
171 }catch (OperationFailedException ofe){
172 System.out.println("call getAcademicCalendarService().getKeyDatesForTerm(termKey, context), and get OperationFailedException: "+ofe.toString());
173 }catch (PermissionDeniedException pde){
174 System.out.println("call getAcademicCalendarService().getKeyDatesForTerm(termKey, context), and get PermissionDeniedException: "+pde.toString());
175 }
176 dataObject = termWrapper;
177
178 } catch (ClassNotPersistenceCapableException ex) {
179 if (!document.getOldMaintainableObject().isExternalBusinessObject()) {
180 throw new RuntimeException("Data Object Class: " + getDataObjectClass() +
181 " is not persistable and is not externalizable - configuration error");
182 }
183
184 }
185
186 return dataObject;
187
188 }
189
190 protected AcademicCalendarService getAcademicCalendarService() {
191 if(academicCalendarService == null) {
192 academicCalendarService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/acal","AcademicCalendarService"));
193 }
194 return academicCalendarService;
195 }
196
197
198
199
200
201
202
203
204 private String getTermInfoKey(TermInfo termInfo){
205 String termKey = new String (TERM_KEY_PREFIX);
206 String theType;
207
208 String theTypeKey = termInfo.getTypeKey();
209 if (theTypeKey.startsWith(TERM_TYPE_KEY_PREFIX)){
210 theType = theTypeKey.substring(15);
211 }
212 else {
213 theType = theTypeKey;
214 }
215 String yearOfStartDate = getYearFromDate(termInfo.getStartDate());
216 String yearOfEndDate = getYearFromDate(termInfo.getEndDate());
217 termKey = termKey.concat("."+yearOfStartDate+"-"+yearOfEndDate+"."+theType.toLowerCase());
218 return termKey;
219
220 }
221
222 private String getYearFromDate(Date date){
223 Calendar cal = Calendar.getInstance();
224 cal.setTime(date);
225 int year = cal.get(Calendar.YEAR);
226 return new Integer(year).toString();
227 }
228
229
230
231
232
233
234
235
236 private String getKeyDateInfoKey(KeyDateInfo keyDateInfo, String termKey){
237 String keyDateInfoKey = new String (KEY_DATE_INFO_KEY_PREFIX);
238
239 String theKeyDateInfoType;
240
241 String theKeyDateInfoTypeKey = keyDateInfo.getTypeKey();
242 if (theKeyDateInfoTypeKey.startsWith(MILESTONE_TYPE_KEY_PREFIX)){
243 theKeyDateInfoType = theKeyDateInfoTypeKey.substring(MILESTONE_TYPE_KEY_PREFIX.length());
244 }
245 else {
246 theKeyDateInfoType = theKeyDateInfoTypeKey;
247 }
248
249 keyDateInfoKey = keyDateInfoKey.concat("."+theKeyDateInfoType.toLowerCase()+"."+termKey.substring(6));
250 return keyDateInfoKey;
251
252 }
253
254 }