Coverage Report - org.kuali.student.r2.core.class1.process.krms.proposition.MilestoneDateComparisonProposition
 
Classes in this File Line Coverage Branch Coverage Complexity
MilestoneDateComparisonProposition
0%
0/46
0%
0/32
2.778
MilestoneDateComparisonProposition$1
0%
0/1
N/A
2.778
MilestoneDateComparisonProposition$DateComparisonType
0%
0/2
N/A
2.778
 
 1  
 /**
 2  
  * Copyright 2011 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.r2.core.class1.process.krms.proposition;
 17  
 
 18  
 import java.util.Date;
 19  
 import java.util.HashMap;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
 24  
 import org.kuali.rice.krms.api.engine.ResultEvent;
 25  
 import org.kuali.rice.krms.api.engine.Term;
 26  
 import org.kuali.rice.krms.framework.engine.PropositionResult;
 27  
 import org.kuali.rice.krms.framework.engine.result.BasicResult;
 28  
 import org.kuali.student.common.util.krms.RulesExecutionConstants;
 29  
 import org.kuali.student.common.util.krms.proposition.AbstractLeafProposition;
 30  
 import org.kuali.student.r2.core.atp.dto.MilestoneInfo;
 31  
 import org.kuali.student.r2.core.exemption.infc.DateOverride;
 32  
 
 33  
 /**
 34  
  * This proposition evaluates a check of a particular date against the dates of a given Milestone.
 35  
  * Caveat: the comparisons are all exclusive by default.  If the inclusive field is set to true, then an inclusive comparison is performed
 36  
  *
 37  
  * @author alubbers
 38  
  */
 39  
 public class MilestoneDateComparisonProposition extends AbstractLeafProposition implements ExemptionAwareProposition {
 40  
 
 41  0
     private boolean exemptionUsed = false;
 42  
     private final Term milestonesTerm;
 43  
 
 44  0
     public enum DateComparisonType {
 45  0
         BETWEEN, BEFORE, AFTER
 46  
     }
 47  
 
 48  
     private final Term comparisonDateTerm;
 49  
 
 50  
     private final DateOverride dateOverride;
 51  
 
 52  
     private final DateComparisonType comparisonType;
 53  
 
 54  
     private final String milestoneType;
 55  
 
 56  
     private final String atpKey;
 57  
 
 58  
     private final Boolean inclusive;
 59  
 
 60  0
     public MilestoneDateComparisonProposition(String comparisonDateTermKey, DateComparisonType comparisonType, String milestoneType, String atpKey, Boolean inclusive, DateOverride dateOverrideInfo) {
 61  0
         this.comparisonDateTerm = new Term(comparisonDateTermKey);
 62  0
         this.comparisonType = comparisonType;
 63  0
         this.milestoneType = milestoneType;
 64  0
         this.inclusive = inclusive;
 65  0
         this.dateOverride = dateOverrideInfo;
 66  0
         this.atpKey = atpKey;
 67  
 
 68  0
         Map<String, String> termParametersMap = new HashMap<String, String>(2);
 69  0
         termParametersMap.put(RulesExecutionConstants.MILESTONE_TYPE_TERM_PROPERTY, milestoneType);
 70  0
         termParametersMap.put(RulesExecutionConstants.MILESTONE_ATP_KEY_TERM_PROPERTY, atpKey);
 71  0
         milestonesTerm = new Term(RulesExecutionConstants.MILESTONES_BY_TYPE_TERM_NAME, termParametersMap);
 72  0
     }
 73  
 
 74  
     public Term getComparisonDateTerm() {
 75  0
         return comparisonDateTerm;
 76  
     }
 77  
 
 78  
     public DateComparisonType getComparisonType() {
 79  0
         return comparisonType;
 80  
     }
 81  
 
 82  
     public String getMilestoneType() {
 83  0
         return milestoneType;
 84  
     }
 85  
 
 86  
     public Boolean getInclusive() {
 87  0
         return inclusive;
 88  
     }
 89  
 
 90  
     public DateOverride getDateOverride() {
 91  0
         return dateOverride;
 92  
     }
 93  
 
 94  
     @Override
 95  
     public PropositionResult evaluate(ExecutionEnvironment environment) {
 96  
 
 97  
         // resolve the list of Milestones from the given Milestone type key
 98  0
         List<MilestoneInfo> milestones = environment.resolveTerm(milestonesTerm, this);
 99  
 
 100  0
         Date comparisonDate = environment.resolveTerm(comparisonDateTerm, this);
 101  
 
 102  0
         boolean dateCompareResult = true;
 103  0
         for(MilestoneInfo milestone : milestones) {
 104  0
             dateCompareResult = compareDateToMilestone(comparisonDate, milestone.getStartDate(), milestone.getEndDate());
 105  
 
 106  0
             if(dateCompareResult) {
 107  0
                 break;
 108  
             }
 109  
         }
 110  
 
 111  0
         if(dateOverride != null) {
 112  0
            if(dateCompareResult) {
 113  0
                exemptionUsed = false;
 114  
            }
 115  
            else {
 116  
 
 117  
                // set the proposition result equal to the comparison of the date and the dates from the override
 118  0
                dateCompareResult = compareDateToMilestone(comparisonDate, dateOverride.getEffectiveEndDate(), dateOverride.getEffectiveEndDate());
 119  
 
 120  
                // it should be up to the caller what happens when the comparison fails even when the exemption information was checked
 121  
                // so we set the exemptionUsed to true no matter what the actual comparison result was
 122  0
                exemptionUsed = true;
 123  
            }
 124  
         }
 125  
 
 126  0
         PropositionResult result = new PropositionResult(dateCompareResult);
 127  
 
 128  0
         environment.getEngineResults().addResult(new BasicResult(ResultEvent.PROPOSITION_EVALUATED, this, environment, result.getResult()));
 129  
 
 130  0
         return result;
 131  
     }
 132  
 
 133  
     private boolean compareDateToMilestone(Date comparisonDate, Date startDate, Date endDate) {
 134  
         boolean dateCompareResult;
 135  0
         switch (comparisonType) {
 136  
             case BEFORE: {
 137  0
                 dateCompareResult = comparisonDate.before(endDate);
 138  0
                 if(inclusive && !dateCompareResult) {
 139  0
                     dateCompareResult = comparisonDate.equals(endDate);
 140  
                 }
 141  
                 break;
 142  
             }
 143  
             case AFTER: {
 144  0
                 dateCompareResult = comparisonDate.after(startDate);
 145  0
                 if(inclusive && !dateCompareResult) {
 146  0
                     dateCompareResult = comparisonDate.equals(startDate);
 147  
                 }
 148  
                 break;
 149  
             }
 150  
             case BETWEEN: {
 151  0
                 dateCompareResult = comparisonDate.after(startDate) && comparisonDate.before(endDate);
 152  0
                 if(inclusive && !dateCompareResult) {
 153  0
                     dateCompareResult = comparisonDate.equals(startDate) || comparisonDate.equals(endDate);
 154  
                 }
 155  
                 break;
 156  
             }
 157  
             default: {
 158  0
                 throw new RuntimeException("Invalid comparisonType when evaluating a MilestoneDateComparisonProposition: " + comparisonType);
 159  
             }
 160  
         }
 161  0
         return dateCompareResult;
 162  
     }
 163  
 
 164  
     public boolean isExemptionUsed() {
 165  0
         return exemptionUsed;
 166  
     }
 167  
 }