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