Coverage Report - org.kuali.student.enrollment.class2.acal.service.TermInfoMaintainableImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
TermInfoMaintainableImpl
0%
0/50
0%
0/10
4.167
 
 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.TermInfo;
 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.common.util.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  0
 public class TermInfoMaintainableImpl extends MaintainableImpl {
 21  
         private static final long serialVersionUID = 1L;        
 22  
         
 23  
     public final static String TERM_KEY_PREFIX = "kuali.term.";
 24  
     public final static String TYPE_KEY_PREFIX = "kuali.atp.type.";
 25  
     public final static String DEFAULT_VALUE_OF_ATP_STATE ="kuali.atp.state.Official";
 26  
     
 27  
     private transient AcademicCalendarService academicCalendarService;
 28  
 
 29  
     @Override
 30  
     public void saveDataObject() {
 31  0
             System.out.println(">>In TermInfoMaintainableImpl.saveDataObject()");
 32  0
         TermInfo termInfo = (TermInfo)getDataObject();
 33  0
         String termKey = getTermInfoKey (termInfo);
 34  0
         System.out.println(">>>termKey = "+termKey);
 35  0
         termInfo.setStateKey(AtpServiceConstants.ATP_OFFICIAL_STATE_KEY);
 36  
 
 37  
         try{
 38  0
                 if(getMaintenanceAction().equals(KRADConstants.MAINTENANCE_NEW_ACTION) ||
 39  
                 getMaintenanceAction().equals(KRADConstants.MAINTENANCE_COPY_ACTION)) {   
 40  0
                         getAcademicCalendarService().createTerm(termKey, termInfo, new ContextInfo());
 41  
                 }
 42  
                 else {
 43  0
                         getAcademicCalendarService().updateTerm(termKey, termInfo, new ContextInfo());
 44  
                 }
 45  0
         }catch (DataValidationErrorException dvee){
 46  
             
 47  0
         }catch (InvalidParameterException ipe){
 48  
             
 49  0
         }catch (MissingParameterException mpe){
 50  
             
 51  0
         }catch (OperationFailedException ofe){
 52  
            
 53  0
         }catch (PermissionDeniedException pde){
 54  
 
 55  0
         }catch (ReadOnlyException roe){
 56  
             
 57  0
         }catch (DoesNotExistException dee){
 58  
             
 59  0
         }catch (VersionMismatchException vme){
 60  
             
 61  0
         }       
 62  
         
 63  0
     }
 64  
 
 65  
     @Override
 66  
     public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
 67  0
             ContextInfo context = new ContextInfo();
 68  
             try{
 69  0
                     return getAcademicCalendarService().getTerm(dataObjectKeys.get("key"), context);
 70  
             
 71  0
         }catch (InvalidParameterException ipe){
 72  
             
 73  0
         }catch (MissingParameterException mpe){
 74  
             
 75  0
         }catch (OperationFailedException ofe){
 76  
            
 77  0
         }catch (PermissionDeniedException pde){
 78  
             
 79  0
         }catch (DoesNotExistException dee){
 80  
             
 81  0
         }
 82  0
         return null;
 83  
   
 84  
     }
 85  
     
 86  
     protected AcademicCalendarService getAcademicCalendarService() {
 87  0
         if(academicCalendarService == null) {
 88  0
                 academicCalendarService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/acal","AcademicCalendarService"));
 89  
        }
 90  
 
 91  0
        return academicCalendarService;
 92  
    }
 93  
     
 94  
     /**
 95  
      * @see org.kuali.rice.krad.maintenance.MaintainableImpl#prepareForSave()
 96  
      */
 97  
     @Override
 98  
     public void prepareForSave() {
 99  0
             System.out.println (">>> in prepareForSave ");
 100  0
         if (getMaintenanceAction().equalsIgnoreCase(KRADConstants.MAINTENANCE_NEW_ACTION)) {
 101  0
                 TermInfo newTerm = (TermInfo)getDataObject();           
 102  0
                 newTerm.setStateKey(AtpServiceConstants.ATP_OFFICIAL_STATE_KEY);
 103  
         }
 104  0
         super.prepareForSave();
 105  0
     }
 106  
     
 107  
     /*
 108  
      *  Based on Norm's suggestion at 
 109  
      *  https://wiki.kuali.org/display/STUDENT/How+to+Calculate+Keys+for+Academic+Calendar+Entities
 110  
      *  Term Keys should be 
 111  
      *  kuali.term.<yearOfStartDate>-<yearOfEndDate>.
 112  
      *  <The last part of the type key of the term selected (when split using ".") converted to lower case>
 113  
      */
 114  
    private String getTermInfoKey(TermInfo termInfo){
 115  0
        String termKey = new String (TERM_KEY_PREFIX);
 116  
        String theType;
 117  
        
 118  0
        String theTypeKey = termInfo.getTypeKey();      
 119  0
        if (theTypeKey.startsWith(TYPE_KEY_PREFIX)){
 120  0
                theType = theTypeKey.substring(15);
 121  
        }
 122  
        else {
 123  0
                theType = theTypeKey;
 124  
        }        
 125  0
        String yearOfStartDate = getYearFromDate(termInfo.getStartDate());
 126  0
        String yearOfEndDate = getYearFromDate(termInfo.getEndDate());
 127  0
        termKey = termKey.concat("."+yearOfStartDate+"-"+yearOfEndDate+"."+theType.toLowerCase());
 128  0
        return termKey;       
 129  
        
 130  
    }
 131  
    
 132  
    private String getYearFromDate(Date date){
 133  0
            Calendar cal = Calendar.getInstance();
 134  0
            cal.setTime(date);
 135  0
            int year = cal.get(Calendar.YEAR);
 136  0
            return new Integer(year).toString();
 137  
    }
 138  
 
 139  
 }