Coverage Report - org.kuali.student.r2.core.class1.atp.service.decorators.AtpServiceCalculationDecorator
 
Classes in this File Line Coverage Branch Coverage Complexity
AtpServiceCalculationDecorator
0%
0/46
0%
0/18
5
 
 1  
 package org.kuali.student.r2.core.class1.atp.service.decorators;
 2  
 
 3  
 import org.kuali.student.core.atp.entity.Milestone;
 4  
 import org.kuali.student.r2.common.dto.ContextInfo;
 5  
 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
 6  
 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
 7  
 import org.kuali.student.r2.common.exceptions.MissingParameterException;
 8  
 import org.kuali.student.r2.common.exceptions.OperationFailedException;
 9  
 import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
 10  
 import org.kuali.student.r2.common.util.constants.AtpServiceConstants;
 11  
 import org.kuali.student.r2.core.atp.dto.MilestoneInfo;
 12  
 import org.kuali.student.r2.core.class1.atp.dao.MilestoneDao;
 13  
 import org.kuali.student.r2.core.class1.atp.model.MilestoneEntity;
 14  
 
 15  
 import java.util.Calendar;
 16  
 import java.util.Date;
 17  
 import java.util.GregorianCalendar;
 18  
 
 19  0
 public class AtpServiceCalculationDecorator extends AtpServiceDecorator {
 20  
     private MilestoneDao milestoneDao;
 21  
 
 22  
     public MilestoneDao getMilestoneDao() {
 23  0
         return milestoneDao;
 24  
     }
 25  
 
 26  
     public void setMilestoneDao(MilestoneDao milestoneDao) {
 27  0
         this.milestoneDao = milestoneDao;
 28  0
     }
 29  
 
 30  
     @Override
 31  
     public MilestoneInfo calculateMilestone(String milestoneId, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 32  
 
 33  0
         MilestoneInfo milestone = getMilestone(milestoneId, contextInfo);
 34  0
         if (milestone == null) {
 35  0
             throw new DoesNotExistException("milestone: " + milestoneId);
 36  
         }
 37  
 
 38  0
         String typeKey = milestone.getTypeKey();
 39  0
         if (AtpServiceConstants.MILESTONE_FINANCIAL_AID_CENSUS_TYPE_KEY.equals(typeKey)) {
 40  0
             calculateCensus(milestone, contextInfo);
 41  
         }
 42  
 
 43  0
         return milestone;
 44  
     }
 45  
 
 46  
     private void calculateCensus(MilestoneInfo census, ContextInfo contextInfo) throws OperationFailedException {
 47  0
         MilestoneInfo instructionPeriod = null;
 48  
         try {
 49  0
             instructionPeriod = getMilestone(census.getRelativeAnchorMilestoneId(), contextInfo);
 50  0
             if (!AtpServiceConstants.MILESTONE_INSTRUCTIONAL_PERIOD_TYPE_KEY.equals(instructionPeriod.getTypeKey())) {
 51  0
                 throw new OperationFailedException("Census milestone's relative anchor mile is of incorrect type: " + instructionPeriod.getTypeKey());
 52  
             }
 53  0
         } catch (DoesNotExistException e) {
 54  0
             throw new OperationFailedException("Could not retrieve instruction period for census '" + census.getId() + "'");
 55  0
         } catch (InvalidParameterException e) {
 56  0
             throw new OperationFailedException("Could not retrieve instruction period for census '" + census.getId() + "'");
 57  0
         } catch (MissingParameterException e) {
 58  0
             throw new OperationFailedException("Could not retrieve instruction period for census '" + census.getId() + "'");
 59  0
         } catch (OperationFailedException e) {
 60  0
             throw new OperationFailedException("Could not retrieve instruction period for census '" + census.getId() + "'");
 61  0
         } catch (PermissionDeniedException e) {
 62  0
             throw new OperationFailedException("Could not retrieve instruction period for census '" + census.getId() + "'");
 63  0
         }
 64  0
         Date censusStartTime = getCensus(instructionPeriod);
 65  0
         Calendar cal = Calendar.getInstance();
 66  0
         cal.setTime(censusStartTime);
 67  0
         cal.add(Calendar.DATE, 1);
 68  0
         Date censusEndTime = cal.getTime();
 69  
 
 70  0
         census.setStartDate(censusStartTime);
 71  0
         census.setEndDate(censusEndTime);
 72  0
     }
 73  
 
 74  
     private Date getCensus(MilestoneInfo instructionPeriod) {
 75  0
         Calendar cal = new GregorianCalendar();
 76  0
         cal.setTime(instructionPeriod.getStartDate());
 77  0
         cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
 78  0
         cal.add(Calendar.DATE, 7);
 79  0
         return cal.getTime();
 80  
     }
 81  
 
 82  
     private boolean timespanOccursDuringTimespan(Date startTime1, Date endTime1, Date startTime2, Date endTime2) {
 83  0
         if (!startTime1.before(startTime2) && startTime1.before(endTime2)) {
 84  
             // milestone starts as/after ATP starts (fully contained or extends past ATP end)
 85  0
             return true;
 86  
         }
 87  0
         if (!endTime1.before(startTime2) && !endTime1.after(endTime2)) {
 88  
             // milestone ends while ATP is in progress
 89  0
             return true;
 90  
         }
 91  0
         if (!startTime1.after(startTime2) && !endTime1.before(endTime2)) {
 92  
             // milestone encompasses ATP
 93  0
             return true;
 94  
         }
 95  0
         return false;
 96  
     }
 97  
 
 98  
 }