Coverage Report - org.kuali.student.process.poc.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.process.poc.krms.proposition;
 17  
 
 18  
 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
 19  
 import org.kuali.rice.krms.api.engine.ResultEvent;
 20  
 import org.kuali.rice.krms.api.engine.Term;
 21  
 import org.kuali.rice.krms.framework.engine.PropositionResult;
 22  
 import org.kuali.rice.krms.framework.engine.result.BasicResult;
 23  
 import org.kuali.student.common.util.krms.RulesExecutionConstants;
 24  
 import org.kuali.student.common.util.krms.proposition.AbstractLeafProposition;
 25  
 import org.kuali.student.r2.core.atp.dto.MilestoneInfo;
 26  
 import org.kuali.student.r2.core.exemption.dto.DateOverrideInfo;
 27  
 import org.kuali.student.r2.core.exemption.infc.DateOverride;
 28  
 
 29  
 import java.util.Collections;
 30  
 import java.util.Date;
 31  
 import java.util.HashMap;
 32  
 import java.util.List;
 33  
 import java.util.Map;
 34  
 
 35  
 /**
 36  
  * This proposition evaluates a check of a particular date against the dates of a given Milestone.
 37  
  * Caveat: the comparisons are all exclusive by default.  If the inclusive field is set to true, then an inclusive comparison is performed
 38  
  *
 39  
  * @author alubbers
 40  
  */
 41  
 public class MilestoneDateComparisonProposition extends AbstractLeafProposition implements ExemptionAwareProposition {
 42  
 
 43  0
     private boolean exemptionUsed = false;
 44  
     private final Term milestonesTerm;
 45  
 
 46  0
     public enum DateComparisonType {
 47  0
         BETWEEN, BEFORE, AFTER
 48  
     }
 49  
 
 50  
     private final Term comparisonDateTerm;
 51  
 
 52  
     private final DateOverride dateOverride;
 53  
 
 54  
     private final DateComparisonType comparisonType;
 55  
 
 56  
     private final String milestoneType;
 57  
 
 58  
     private final String atpKey;
 59  
 
 60  
     private final Boolean inclusive;
 61  
 
 62  0
     public MilestoneDateComparisonProposition(String comparisonDateTermKey, DateComparisonType comparisonType, String milestoneType, String atpKey, Boolean inclusive, DateOverride dateOverrideInfo) {
 63  0
         this.comparisonDateTerm = new Term(comparisonDateTermKey);
 64  0
         this.comparisonType = comparisonType;
 65  0
         this.milestoneType = milestoneType;
 66  0
         this.inclusive = inclusive;
 67  0
         this.dateOverride = dateOverrideInfo;
 68  0
         this.atpKey = atpKey;
 69  
 
 70  0
         Map<String, String> termParametersMap = new HashMap<String, String>(2);
 71  0
         termParametersMap.put(RulesExecutionConstants.MILESTONE_TYPE_TERM_PROPERTY, milestoneType);
 72  0
         termParametersMap.put(RulesExecutionConstants.MILESTONE_ATP_KEY_TERM_PROPERTY, atpKey);
 73  0
         milestonesTerm = new Term(RulesExecutionConstants.MILESTONES_BY_TYPE_TERM_NAME, termParametersMap);
 74  0
     }
 75  
 
 76  
     public Term getComparisonDateTerm() {
 77  0
         return comparisonDateTerm;
 78  
     }
 79  
 
 80  
     public DateComparisonType getComparisonType() {
 81  0
         return comparisonType;
 82  
     }
 83  
 
 84  
     public String getMilestoneType() {
 85  0
         return milestoneType;
 86  
     }
 87  
 
 88  
     public Boolean getInclusive() {
 89  0
         return inclusive;
 90  
     }
 91  
 
 92  
     public DateOverride getDateOverride() {
 93  0
         return dateOverride;
 94  
     }
 95  
 
 96  
     @Override
 97  
     public PropositionResult evaluate(ExecutionEnvironment environment) {
 98  
 
 99  
         // resolve the list of Milestones from the given Milestone type key
 100  0
         List<MilestoneInfo> milestones = environment.resolveTerm(milestonesTerm, this);
 101  
 
 102  0
         Date comparisonDate = environment.resolveTerm(comparisonDateTerm, this);
 103  
 
 104  0
         boolean dateCompareResult = true;
 105  0
         for(MilestoneInfo milestone : milestones) {
 106  0
             dateCompareResult = compareDateToMilestone(comparisonDate, milestone.getStartDate(), milestone.getEndDate());
 107  
 
 108  0
             if(dateCompareResult) {
 109  0
                 break;
 110  
             }
 111  
         }
 112  
 
 113  0
         if(dateOverride != null) {
 114  0
            if(dateCompareResult) {
 115  0
                exemptionUsed = false;
 116  
            }
 117  
            else {
 118  
 
 119  
                // set the proposition result equal to the comparison of the date and the dates from the override
 120  0
                dateCompareResult = compareDateToMilestone(comparisonDate, dateOverride.getEffectiveEndDate(), dateOverride.getEffectiveEndDate());
 121  
 
 122  
                // it should be up to the caller what happens when the comparison fails even when the exemption information was checked
 123  
                // so we set the exemptionUsed to true no matter what the actual comparison result was
 124  0
                exemptionUsed = true;
 125  
            }
 126  
         }
 127  
 
 128  0
         PropositionResult result = new PropositionResult(dateCompareResult);
 129  
 
 130  0
         environment.getEngineResults().addResult(new BasicResult(ResultEvent.PropositionEvaluated, this, environment, result.getResult()));
 131  
 
 132  0
         return result;
 133  
     }
 134  
 
 135  
     private boolean compareDateToMilestone(Date comparisonDate, Date startDate, Date endDate) {
 136  
         boolean dateCompareResult;
 137  0
         switch (comparisonType) {
 138  
             case BEFORE: {
 139  0
                 dateCompareResult = comparisonDate.before(endDate);
 140  0
                 if(inclusive && !dateCompareResult) {
 141  0
                     dateCompareResult = comparisonDate.equals(endDate);
 142  
                 }
 143  
                 break;
 144  
             }
 145  
             case AFTER: {
 146  0
                 dateCompareResult = comparisonDate.after(startDate);
 147  0
                 if(inclusive && !dateCompareResult) {
 148  0
                     dateCompareResult = comparisonDate.equals(startDate);
 149  
                 }
 150  
                 break;
 151  
             }
 152  
             case BETWEEN: {
 153  0
                 dateCompareResult = comparisonDate.after(startDate) && comparisonDate.before(endDate);
 154  0
                 if(inclusive && !dateCompareResult) {
 155  0
                     dateCompareResult = comparisonDate.equals(startDate) || comparisonDate.equals(endDate);
 156  
                 }
 157  
                 break;
 158  
             }
 159  
             default: {
 160  0
                 throw new RuntimeException("Invalid comparisonType when evaluating a MilestoneDateComparisonProposition: " + comparisonType);
 161  
             }
 162  
         }
 163  0
         return dateCompareResult;
 164  
     }
 165  
 
 166  
     public boolean isExemptionUsed() {
 167  0
         return exemptionUsed;
 168  
     }
 169  
 }