Coverage Report - org.kuali.student.process.poc.evaluator.MilestoneCheckEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
MilestoneCheckEvaluator
0%
0/69
0%
0/26
5.125
 
 1  
 package org.kuali.student.process.poc.evaluator;
 2  
 
 3  
 import java.util.Arrays;
 4  
 import java.util.Date;
 5  
 import java.util.List;
 6  
 
 7  
 import java.util.logging.Level;
 8  
 import java.util.logging.Logger;
 9  
 import org.kuali.student.process.poc.context.MilestoneCheckContext;
 10  
 import org.kuali.student.r2.common.dto.ContextInfo;
 11  
 import org.kuali.student.r2.common.dto.ValidationResultInfo;
 12  
 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
 13  
 import org.kuali.student.r2.common.exceptions.MissingParameterException;
 14  
 import org.kuali.student.r2.common.exceptions.OperationFailedException;
 15  
 import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
 16  
 import org.kuali.student.r2.common.util.constants.ExemptionServiceConstants;
 17  
 import org.kuali.student.r2.core.atp.dto.MilestoneInfo;
 18  
 import org.kuali.student.r2.core.atp.service.AtpService;
 19  
 import org.kuali.student.r2.core.exemption.dto.ExemptionInfo;
 20  
 import org.kuali.student.r2.core.exemption.service.ExemptionService;
 21  
 import org.kuali.student.r2.core.process.dto.InstructionInfo;
 22  
 
 23  0
 public class MilestoneCheckEvaluator implements CheckEvaluator<MilestoneCheckContext> {
 24  
 
 25  
     private AtpService atpService;
 26  
     private ExemptionService exemptionService;
 27  
 
 28  
     public AtpService getAtpService() {
 29  0
         return atpService;
 30  
     }
 31  
 
 32  
     public void setAtpService(AtpService atpService) {
 33  0
         this.atpService = atpService;
 34  0
     }
 35  
 
 36  
     public ExemptionService getExemptionService() {
 37  0
         return exemptionService;
 38  
     }
 39  
 
 40  
     public void setExemptionService(ExemptionService exemptionService) {
 41  0
         this.exemptionService = exemptionService;
 42  0
     }
 43  
 
 44  0
     public MilestoneCheckEvaluator() {
 45  0
     }
 46  
 
 47  
     @Override
 48  
     public List<ValidationResultInfo> evaluate(MilestoneCheckContext checkContext, ContextInfo context) throws OperationFailedException {
 49  
 
 50  
         // TODO: look for a DateOverride Exemption for the student that might extend the milestone deadline
 51  
         List<MilestoneInfo> milestones;
 52  
         try {
 53  0
             milestones = atpService.getMilestonesByTypeForAtp(checkContext.getAtpKey(), checkContext.getCheck().getMilestoneTypeKey(), context);
 54  0
         } catch (OperationFailedException ex) {
 55  0
             throw ex;
 56  0
         } catch (Exception ex) {
 57  0
             throw new OperationFailedException("unexpected", ex);
 58  0
         }
 59  0
         if (milestones.isEmpty()) {
 60  0
             throw new OperationFailedException("no miilestone found for atp " + checkContext.getAtpKey() + " " + checkContext.getCheck().getMilestoneTypeKey());
 61  
         }
 62  
         // TODO: consider if it should be OK if it matches ANY if more than one found
 63  0
         if (milestones.size() > 1) {
 64  0
             throw new OperationFailedException("More than one miilestone found for atp & type " + checkContext.getAtpKey() + " " + checkContext.getCheck().getMilestoneTypeKey());
 65  
         }
 66  
 
 67  0
         MilestoneInfo milestone = milestones.get(0);
 68  0
         ValidationResultInfo vr = null;
 69  0
         switch (checkContext.getComparison()) {
 70  
             case MilestoneCheckContext.PERIOD:
 71  0
                 if (checkContext.getDateToTest().before(milestone.getStartDate())) {
 72  0
                     vr = initError(checkContext.getInstruction());
 73  0
                     return Arrays.asList(vr);
 74  
                 }
 75  0
                 if (checkContext.getDateToTest().after(milestone.getEndDate())) {
 76  0
                     vr = initError(checkContext.getInstruction());
 77  0
                     return Arrays.asList(vr);
 78  
                 }
 79  0
                 vr = initInfo(checkContext.getInstruction());
 80  0
                 return Arrays.asList(vr);
 81  
             case MilestoneCheckContext.START_DATE:
 82  0
                 if (checkContext.getDateToTest().before(milestone.getStartDate())) {
 83  0
                     vr = initError(checkContext.getInstruction());
 84  0
                     return Arrays.asList(vr);
 85  
                 }
 86  0
                 vr = initInfo(checkContext.getInstruction());
 87  0
                 return Arrays.asList(vr);
 88  
             case MilestoneCheckContext.END_DATE:
 89  0
                 Date endDate = milestone.getEndDate();
 90  
                 // if not a date range then compare to the start date
 91  0
                 if (endDate == null) {
 92  0
                     endDate = milestone.getStartDate();
 93  
                 }
 94  0
                 if (checkContext.getDateToTest().after(endDate)) {
 95  0
                     List<ExemptionInfo> extensions = null;
 96  
                     try {
 97  0
                         extensions = exemptionService.getActiveExemptionsByTypeForPerson(ExemptionServiceConstants.MILESTONE_DATE_EXEMPTION_TYPE_KEY,
 98  
                                 checkContext.getStudentId(), context);
 99  0
                     } catch (OperationFailedException ex) {
 100  0
                         throw ex;
 101  0
                     } catch (Exception ex) {
 102  0
                         throw new OperationFailedException("unexpected", ex);
 103  0
                     }
 104  0
                     if (extensions.isEmpty()) {
 105  0
                         vr = initError(checkContext.getInstruction());
 106  0
                         return Arrays.asList(vr);
 107  
                     }
 108  
                     // TODO: worry about having more than one extension 
 109  0
                     endDate = extensions.get(0).getDateOverride().getEffectiveEndDate();
 110  0
                     if (endDate != null) {
 111  0
                          if (checkContext.getDateToTest().after(endDate)) {
 112  
                            // TODO: insert the info object that the exemption was used
 113  0
                            vr = initError(checkContext.getInstruction());
 114  0
                            return Arrays.asList(vr);         
 115  
                          }
 116  
                     }
 117  
                     // TODO: insert the info object that the excemption failed despite the exemption
 118  
                 }
 119  0
                 vr = initInfo(checkContext.getInstruction());
 120  0
                 return Arrays.asList(vr);
 121  
         }
 122  0
         throw new OperationFailedException("bad logic in java code");
 123  
     }
 124  
 
 125  
     public static ValidationResultInfo initError(InstructionInfo instruction) {
 126  0
         ValidationResultInfo vr = new ValidationResultInfo();
 127  0
         vr.setElement(instruction.getProcessKey() + " - " + instruction.getCheckKey());
 128  0
         if (instruction.getIsWarning()) {
 129  0
             vr.setLevel(ValidationResultInfo.ErrorLevel.WARN);
 130  
         } else {
 131  0
             vr.setLevel(ValidationResultInfo.ErrorLevel.ERROR);
 132  
         }
 133  0
         vr.setMessage(instruction.getMessage().getPlain());
 134  0
         return vr;
 135  
     }
 136  
 
 137  
     public static ValidationResultInfo initInfo(InstructionInfo instruction) {
 138  0
         ValidationResultInfo vr = new ValidationResultInfo();
 139  0
         vr.setElement(instruction.getProcessKey() + " - " + instruction.getCheckKey());
 140  0
         vr.setLevel(ValidationResultInfo.ErrorLevel.OK);
 141  0
         vr.setMessage(instruction.getMessage().getPlain());
 142  0
         return vr;
 143  
     }
 144  
 }