1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
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 | |
|
35 | |
|
36 | |
|
37 | |
|
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 | |
|
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 | |
|
118 | 0 | dateCompareResult = compareDateToMilestone(comparisonDate, dateOverride.getEffectiveEndDate(), dateOverride.getEffectiveEndDate()); |
119 | |
|
120 | |
|
121 | |
|
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 | |
} |