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