Coverage Report - org.kuali.student.enrollment.class2.acal.service.TermWrapperMaintainableImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
TermWrapperMaintainableImpl
0%
0/118
0%
0/26
5.667
 
 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.common.util.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  
 //Core slice class.
 23  
 @Deprecated
 24  0
 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  
     //Type keys for term are "kuali.atp.type.Fall", "kuali.atp.type.Winter", "kuali.atp.type.Spring", or "kuali.atp.type.Summer".
 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  0
             System.out.println(">>In TermWrapperMaintainableImpl.saveDataObject()");
 41  0
         TermWrapper termWrapper = (TermWrapper)getDataObject();
 42  0
         TermInfo termInfo = termWrapper.getTermInfo();
 43  0
         String termKey = getTermInfoKey (termInfo);
 44  0
         System.out.println(">>>termKey = "+termKey);
 45  0
         termInfo.setStateKey(AtpServiceConstants.ATP_OFFICIAL_STATE_KEY);
 46  
         
 47  0
         KeyDateInfo classesMeetDates = termWrapper.getClassesMeetDates();
 48  0
         classesMeetDates.setStateKey(AtpServiceConstants.MILESTONE_OFFICIAL_STATE_KEY);
 49  0
         classesMeetDates.setTypeKey(AtpServiceConstants.MILESTONE_INSTRUCTIONAL_PERIOD_TYPE_KEY);
 50  0
         String classesMeetDatesKey = getKeyDateInfoKey(classesMeetDates, termKey);
 51  0
         classesMeetDates.setId(classesMeetDatesKey);
 52  
         
 53  0
         KeyDateInfo registrationPeriod = termWrapper.getRegistrationPeriod();
 54  0
         registrationPeriod.setStateKey(AtpServiceConstants.MILESTONE_OFFICIAL_STATE_KEY);
 55  0
         registrationPeriod.setTypeKey(AtpServiceConstants.MILESTONE_REGISTRATION_PERIOD_TYPE_KEY);
 56  0
         String registrationPeriodKey = getKeyDateInfoKey(registrationPeriod, termKey);
 57  0
         registrationPeriod.setId(registrationPeriodKey);
 58  
 
 59  
         
 60  
   
 61  0
         KeyDateInfo dropPeriodEndsDate = termWrapper.getDropPeriodEndsDate();
 62  0
         dropPeriodEndsDate.setStateKey(AtpServiceConstants.MILESTONE_OFFICIAL_STATE_KEY);
 63  0
         dropPeriodEndsDate.setTypeKey(AtpServiceConstants.MILESTONE_DROP_DATE_TYPE_KEY);
 64  0
         String dropPeriodEndsDateKey = getKeyDateInfoKey(dropPeriodEndsDate, termKey);
 65  0
         dropPeriodEndsDate.setId(dropPeriodEndsDateKey);
 66  
 
 67  
 
 68  0
         KeyDateInfo finalExaminationsDates = termWrapper.getFinalExaminationsDates();
 69  0
         finalExaminationsDates.setStateKey(AtpServiceConstants.MILESTONE_OFFICIAL_STATE_KEY);
 70  0
         finalExaminationsDates.setTypeKey(AtpServiceConstants.MILESTONE_FINAL_EXAM_PERIOD_TYPE_KEY);
 71  0
         String finalExaminationsDatesKey = getKeyDateInfoKey(finalExaminationsDates, termKey);
 72  0
         finalExaminationsDates.setId(finalExaminationsDatesKey);
 73  
 
 74  
         
 75  0
         KeyDateInfo gradesDueDate = termWrapper.getGradesDueDate();
 76  0
         gradesDueDate.setStateKey(AtpServiceConstants.MILESTONE_OFFICIAL_STATE_KEY);
 77  0
         gradesDueDate.setTypeKey(AtpServiceConstants.MILESTONE_GRADES_DUE_TYPE_KEY);
 78  0
         String gradesDueDateKey = getKeyDateInfoKey(gradesDueDate, termKey);
 79  0
         gradesDueDate.setId(gradesDueDateKey);
 80  
   
 81  0
                 academicCalendarService = getAcademicCalendarService();
 82  0
                 ContextInfo context = new ContextInfo();
 83  
         
 84  
         try{
 85  0
                 if(getMaintenanceAction().equals(KRADConstants.MAINTENANCE_NEW_ACTION) ||
 86  
                 getMaintenanceAction().equals(KRADConstants.MAINTENANCE_COPY_ACTION)) {                          
 87  0
                         academicCalendarService.createTerm(termKey, termInfo, context);
 88  0
                         academicCalendarService.createKeyDate(termKey, classesMeetDatesKey, classesMeetDates, context);
 89  0
                         academicCalendarService.createKeyDate(termKey, registrationPeriodKey, registrationPeriod, context);
 90  0
                         academicCalendarService.createKeyDate(termKey, dropPeriodEndsDateKey, dropPeriodEndsDate, context);
 91  0
                         academicCalendarService.createKeyDate(termKey, finalExaminationsDatesKey, finalExaminationsDates, context);
 92  0
                         academicCalendarService.createKeyDate(termKey, gradesDueDateKey, gradesDueDate, context);
 93  
                        
 94  
                 }
 95  
                 else {
 96  0
                         getAcademicCalendarService().updateTerm(termKey, termInfo, new ContextInfo());
 97  0
                         academicCalendarService.updateKeyDate(classesMeetDatesKey, classesMeetDates, context);
 98  0
                         academicCalendarService.updateKeyDate(registrationPeriodKey, registrationPeriod, context);
 99  0
                         academicCalendarService.updateKeyDate(dropPeriodEndsDateKey, dropPeriodEndsDate, context);
 100  0
                         academicCalendarService.updateKeyDate(finalExaminationsDatesKey, finalExaminationsDates, context);
 101  0
                         academicCalendarService.updateKeyDate(gradesDueDateKey, gradesDueDate, context);
 102  
                 }
 103  0
         }catch (DataValidationErrorException dvee){
 104  
             
 105  0
         }catch (InvalidParameterException ipe){
 106  
             
 107  0
         }catch (MissingParameterException mpe){
 108  
             
 109  0
         }catch (OperationFailedException ofe){
 110  
            
 111  0
         }catch (PermissionDeniedException pde){
 112  
 
 113  0
         }catch (ReadOnlyException roe){
 114  
             
 115  0
         }catch (DoesNotExistException dee){
 116  
             
 117  0
         }catch (VersionMismatchException vme){
 118  
             
 119  0
         }       
 120  
         
 121  0
     }
 122  
 
 123  
     @Override
 124  
     public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
 125  
 
 126  0
        Object dataObject = null;
 127  
        try {
 128  
            // Since the dataObject is a wrapper class we need to build it and populate with the agenda bo.
 129  0
            TermWrapper termWrapper = new TermWrapper();
 130  0
            ContextInfo context = new ContextInfo();
 131  0
            String termKey =  dataObjectKeys.get(TERM_KEY);
 132  
 
 133  
            try {
 134  0
                 TermInfo termInfo = getAcademicCalendarService().getTerm(termKey, context);
 135  
                 // getLookupService().findObjectBySearch(((TermWrapper) getDataObject()).getTermInfo().getClass(), dataObjectKeys);
 136  
 
 137  0
                 if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
 138  
                     // If we don't clear the primary key and set the fieldsClearedOnCopy flag then the
 139  
                     // MaintenanceDocumentServiceImpl.processMaintenanceObjectForCopy() will try to locate the primary keys in
 140  
                     // an attempt to clear them which again would cause an exception due to the wrapper class.
 141  0
                     termInfo.setId(null);
 142  0
                     document.setFieldsClearedOnCopy(true);
 143  
                 }
 144  0
                 termWrapper.setTermInfo(termInfo);
 145  
 
 146  0
                 List<KeyDateInfo> keyDateInfoList = getAcademicCalendarService().getKeyDatesForTerm(termKey, new ContextInfo());
 147  
 
 148  0
                 for (KeyDateInfo keyDateInfo : keyDateInfoList){
 149  0
                                     if(AtpServiceConstants.MILESTONE_INSTRUCTIONAL_PERIOD_TYPE_KEY.equals(keyDateInfo.getTypeKey())){
 150  0
                                             termWrapper.setClassesMeetDates(keyDateInfo);
 151  
                                     }
 152  0
                                     else if(AtpServiceConstants.MILESTONE_REGISTRATION_PERIOD_TYPE_KEY.equals(keyDateInfo.getTypeKey())){
 153  0
                                             termWrapper.setRegistrationPeriod(keyDateInfo);
 154  
                                     }
 155  0
                                     else if(AtpServiceConstants.MILESTONE_DROP_DATE_TYPE_KEY.equals(keyDateInfo.getTypeKey())){
 156  0
                                             termWrapper.setDropPeriodEndsDate(keyDateInfo);
 157  
                                     }
 158  0
                                     else if(AtpServiceConstants.MILESTONE_FINAL_EXAM_PERIOD_TYPE_KEY.equals(keyDateInfo.getTypeKey())){
 159  0
                                             termWrapper.setFinalExaminationsDates(keyDateInfo);
 160  
                                      }
 161  0
                                     else if(AtpServiceConstants.MILESTONE_GRADES_DUE_TYPE_KEY.equals(keyDateInfo.getTypeKey())){
 162  0
                                             termWrapper.setGradesDueDate(keyDateInfo);
 163  
                                     }
 164  
                 }
 165  0
                }catch (DoesNotExistException dnee){
 166  0
                 System.out.println("call getAcademicCalendarService().getKeyDatesForTerm(termKey, context), and get DoesNotExistException:  "+dnee.toString());
 167  0
            }catch (InvalidParameterException ipe){
 168  0
                 System.out.println("call getAcademicCalendarService().getKeyDatesForTerm(termKey, context), and get InvalidParameterException:  "+ipe.toString());
 169  0
            }catch (MissingParameterException mpe){
 170  0
                 System.out.println("call getAcademicCalendarService().getKeyDatesForTerm(termKey, context), and get MissingParameterException:  "+mpe.toString());
 171  0
            }catch (OperationFailedException ofe){
 172  0
                 System.out.println("call getAcademicCalendarService().getKeyDatesForTerm(termKey, context), and get OperationFailedException:  "+ofe.toString());
 173  0
            }catch (PermissionDeniedException pde){
 174  0
                 System.out.println("call getAcademicCalendarService().getKeyDatesForTerm(termKey, context), and get PermissionDeniedException:  "+pde.toString());
 175  0
            }
 176  0
            dataObject = termWrapper;
 177  
 
 178  0
        } catch (ClassNotPersistenceCapableException ex) {
 179  0
            if (!document.getOldMaintainableObject().isExternalBusinessObject()) {
 180  0
                throw new RuntimeException("Data Object Class: " + getDataObjectClass() +
 181  
                        " is not persistable and is not externalizable - configuration error");
 182  
            }
 183  
            // otherwise, let fall through
 184  0
        }
 185  
 
 186  0
        return dataObject;
 187  
 
 188  
     }
 189  
     
 190  
     protected AcademicCalendarService getAcademicCalendarService() {
 191  0
        if(academicCalendarService == null) {
 192  0
                 academicCalendarService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/acal","AcademicCalendarService"));
 193  
        }
 194  0
        return academicCalendarService;
 195  
    }
 196  
     
 197  
     /*
 198  
      *  Based on Norm's suggestion at 
 199  
      *  https://wiki.kuali.org/display/STUDENT/How+to+Calculate+Keys+for+Academic+Calendar+Entities
 200  
      *  Term Keys should be 
 201  
      *  kuali.term.<yearOfStartDate>-<yearOfEndDate>.
 202  
      *  <The last part of the type key of the term selected (when split using ".") converted to lower case>
 203  
      */
 204  
     private String getTermInfoKey(TermInfo termInfo){
 205  0
         String termKey = new String (TERM_KEY_PREFIX);
 206  
         String theType;
 207  
         
 208  0
         String theTypeKey = termInfo.getTypeKey();      
 209  0
         if (theTypeKey.startsWith(TERM_TYPE_KEY_PREFIX)){
 210  0
                 theType = theTypeKey.substring(15);
 211  
         }
 212  
         else {
 213  0
                 theType = theTypeKey;
 214  
         }        
 215  0
         String yearOfStartDate = getYearFromDate(termInfo.getStartDate());
 216  0
         String yearOfEndDate = getYearFromDate(termInfo.getEndDate());
 217  0
         termKey = termKey.concat("."+yearOfStartDate+"-"+yearOfEndDate+"."+theType.toLowerCase());
 218  0
         return termKey;       
 219  
         
 220  
     }
 221  
     
 222  
     private String getYearFromDate(Date date){
 223  0
             Calendar cal = Calendar.getInstance();
 224  0
             cal.setTime(date);
 225  0
             int year = cal.get(Calendar.YEAR);
 226  0
             return new Integer(year).toString();
 227  
     }
 228  
         
 229  
     /*
 230  
      *  Based on Norm's suggestion at 
 231  
      *  https://wiki.kuali.org/display/STUDENT/How+to+Calculate+Keys+for+Academic+Calendar+Entities#HowtoCalculateKeysforAcademicCalendarEntities-MilestoneKeys
 232  
      *  KeyDateInfo Key should be 
 233  
      *  kuali.milestone.<The last part of the type key of the milestone selected (when split using ".") converted to lower case>.
 234  
      *  <The term key to which this milestone is expected to be connected with the "kuali." prefix removed>
 235  
      */
 236  
     private String getKeyDateInfoKey(KeyDateInfo keyDateInfo, String termKey){
 237  0
         String keyDateInfoKey = new String (KEY_DATE_INFO_KEY_PREFIX);
 238  
         
 239  
         String theKeyDateInfoType;
 240  
         
 241  0
         String theKeyDateInfoTypeKey = keyDateInfo.getTypeKey();      
 242  0
         if (theKeyDateInfoTypeKey.startsWith(MILESTONE_TYPE_KEY_PREFIX)){
 243  0
                 theKeyDateInfoType = theKeyDateInfoTypeKey.substring(MILESTONE_TYPE_KEY_PREFIX.length());
 244  
         }
 245  
         else {
 246  0
                 theKeyDateInfoType = theKeyDateInfoTypeKey;
 247  
         }        
 248  
 
 249  0
         keyDateInfoKey = keyDateInfoKey.concat("."+theKeyDateInfoType.toLowerCase()+"."+termKey.substring(6));
 250  0
         return keyDateInfoKey;       
 251  
         
 252  
     }
 253  
 
 254  
 }