View Javadoc

1   package org.kuali.student.enrollment.class2.acal.service;
2   
3   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
4   import org.kuali.rice.krad.maintenance.MaintainableImpl;
5   import org.kuali.rice.krad.maintenance.MaintenanceDocument;
6   import org.kuali.rice.krad.util.KRADConstants;
7   import org.kuali.student.enrollment.acal.dto.AcademicCalendarInfo;
8   import org.kuali.student.enrollment.acal.service.AcademicCalendarService;
9   import org.kuali.student.r2.common.dto.ContextInfo;
10  import org.kuali.student.r2.common.exceptions.*;
11  import org.kuali.student.r2.core.constants.AtpServiceConstants;
12  
13  import javax.xml.namespace.QName;
14  import java.util.Calendar;
15  import java.util.Date;
16  import java.util.Map;
17  
18  //Core slice class.
19  @Deprecated
20  public class AcademicCalendarInfoMaintainableImpl extends MaintainableImpl {
21  	private static final long serialVersionUID = 1L;	
22  	
23      public final static String ACADEMIC_CALENDAR_KEY_PREFIX = "kuali.academic.calendar.";
24      public final static String CREDENTIAL_PROGRAM_TYPE_KEY_PREFIX = "kuali.lu.type.credential.";
25      
26      private transient AcademicCalendarService academicCalendarService;
27    
28      @Override
29      public void saveDataObject() {
30          AcademicCalendarInfo academicCalendarInfo = (AcademicCalendarInfo)getDataObject();
31          String academicCalendarKey = getAcademicCalendarKey (academicCalendarInfo);
32          academicCalendarInfo.setId(academicCalendarKey);
33          academicCalendarInfo.setStateKey(AtpServiceConstants.ATP_OFFICIAL_STATE_KEY);
34          System.out.println(">>>>adminOrgId = "+academicCalendarInfo.getAdminOrgId());
35          try{
36          	if(getMaintenanceAction().equals(KRADConstants.MAINTENANCE_NEW_ACTION) ||
37                  getMaintenanceAction().equals(KRADConstants.MAINTENANCE_COPY_ACTION)) {   
38          		getAcademicCalendarService().createAcademicCalendar(academicCalendarKey, academicCalendarInfo, new ContextInfo());
39          	}
40          	else {
41          		getAcademicCalendarService().updateAcademicCalendar(academicCalendarKey, academicCalendarInfo, new ContextInfo());
42          	}
43          }catch (DataValidationErrorException dvee){
44              
45          }catch (InvalidParameterException ipe){
46  
47          }catch (MissingParameterException mpe){
48  
49          }catch (OperationFailedException ofe){
50             
51          }catch (PermissionDeniedException pde){
52  
53          }catch (ReadOnlyException roe){
54  
55          }catch (DoesNotExistException dee){
56              
57          }catch (VersionMismatchException vme){
58              
59          }       
60          
61      }
62  
63      @Override
64      public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
65      	ContextInfo context = new ContextInfo();
66      	try{
67      		return getAcademicCalendarService().getAcademicCalendar(dataObjectKeys.get("key"), context);
68              
69          }catch (InvalidParameterException ipe){
70              
71          }catch (MissingParameterException mpe){
72              
73          }catch (OperationFailedException ofe){
74             
75          }catch (PermissionDeniedException pde){
76              
77          }catch (DoesNotExistException dee){
78              
79          }
80          return null;
81    
82      }
83      
84    
85      /**
86       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#prepareForSave()
87       */
88      @Override
89      public void prepareForSave() {
90      	System.out.println (">>> in prepareForSave ");
91          if (getMaintenanceAction().equalsIgnoreCase(KRADConstants.MAINTENANCE_NEW_ACTION)) {
92          	AcademicCalendarInfo newAcal = (AcademicCalendarInfo)getDataObject();   	
93          	newAcal.setTypeKey("kuali.atp.type.AcademicCalendar");
94              newAcal.setStateKey(AtpServiceConstants.ATP_OFFICIAL_STATE_KEY);
95          }
96          super.prepareForSave();
97      }
98      
99      protected AcademicCalendarService getAcademicCalendarService() {
100          if(academicCalendarService == null) {
101         	 academicCalendarService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/acal","AcademicCalendarService"));
102         }
103 
104         return academicCalendarService;
105     }
106     /*
107      *  Based on Norm's suggestion at 
108      *  https://wiki.kuali.org/display/STUDENT/How+to+Calculate+Keys+for+Academic+Calendar+Entities
109      *  AcademicCalendarKey should be 
110      *  kuali.academic.calendar.<last part of credentialProgramTypeKey>.<yearOfStartDate>-<yearOfEndDate>
111      */
112     private String getAcademicCalendarKey(AcademicCalendarInfo academicCalendarInfo){
113         String academicCalendarKey = new String (ACADEMIC_CALENDAR_KEY_PREFIX);
114         String adminOrg;
115         
116         String adminOrgId = academicCalendarInfo.getAdminOrgId();
117         if (adminOrgId.startsWith(CREDENTIAL_PROGRAM_TYPE_KEY_PREFIX)){
118             adminOrg  = adminOrgId.substring(25);
119         }
120         else {
121             adminOrg = adminOrgId;
122         }        
123         String yearOfStartDate = getYearFromDate(academicCalendarInfo.getStartDate());
124         String yearOfEndDate = getYearFromDate(academicCalendarInfo.getEndDate());
125         academicCalendarKey = academicCalendarKey.concat(adminOrg.toLowerCase()+"."+yearOfStartDate+"-"+yearOfEndDate);
126         return academicCalendarKey;       
127         
128     }
129     
130     private String getYearFromDate(Date date){
131     	Calendar cal = Calendar.getInstance();
132     	cal.setTime(date);
133     	int year = cal.get(Calendar.YEAR);
134     	return new Integer(year).toString();
135     }
136 }