| 1 | |
package org.kuali.student.process.poc.evaluator; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.Arrays; |
| 5 | |
import java.util.Calendar; |
| 6 | |
import java.util.Date; |
| 7 | |
import java.util.List; |
| 8 | |
|
| 9 | |
import org.kuali.student.process.poc.context.CourseRegistrationProcessContextInfo; |
| 10 | |
import org.kuali.student.process.poc.context.DirectRuleCheckContext; |
| 11 | |
import org.kuali.student.process.poc.context.HoldCheckContext; |
| 12 | |
import org.kuali.student.process.poc.context.MilestoneCheckContext; |
| 13 | |
import org.kuali.student.process.poc.context.SubProcessCheckContext; |
| 14 | |
import org.kuali.student.r2.common.dto.ContextInfo; |
| 15 | |
import org.kuali.student.r2.common.dto.ValidationResultInfo; |
| 16 | |
import org.kuali.student.r2.common.exceptions.OperationFailedException; |
| 17 | |
import org.kuali.student.r2.common.util.constants.ExemptionServiceConstants; |
| 18 | |
import org.kuali.student.r2.common.util.constants.ProcessServiceConstants; |
| 19 | |
|
| 20 | |
import org.kuali.student.r2.core.atp.service.AtpService; |
| 21 | |
import org.kuali.student.r2.core.exemption.dto.ExemptionInfo; |
| 22 | |
import org.kuali.student.r2.core.exemption.service.ExemptionService; |
| 23 | |
import org.kuali.student.r2.core.population.service.PopulationService; |
| 24 | |
import org.kuali.student.r2.core.process.dto.CheckInfo; |
| 25 | |
import org.kuali.student.r2.core.process.dto.InstructionInfo; |
| 26 | |
import org.kuali.student.r2.core.process.service.ProcessService; |
| 27 | |
|
| 28 | 0 | public class RegistrationProcessEvaluator implements ProcessEvaluator<CourseRegistrationProcessContextInfo> { |
| 29 | |
|
| 30 | |
private AtpService atpService; |
| 31 | |
private ProcessService processService; |
| 32 | |
private PopulationService populationService; |
| 33 | |
private ExemptionService exemptionService; |
| 34 | |
private MilestoneCheckEvaluator milestoneCheckEvaluator; |
| 35 | |
private HoldCheckEvaluator holdCheckEvaluator; |
| 36 | |
private DirectRuleCheckEvaluator directRuleCheckEvaluator; |
| 37 | |
private SubProcessCheckEvaluator subProcessCheckEvaluator; |
| 38 | |
|
| 39 | |
public AtpService getAtpService() { |
| 40 | 0 | return atpService; |
| 41 | |
} |
| 42 | |
|
| 43 | |
public void setAtpService(AtpService atpService) { |
| 44 | 0 | this.atpService = atpService; |
| 45 | 0 | } |
| 46 | |
|
| 47 | |
public ExemptionService getExemptionService() { |
| 48 | 0 | return exemptionService; |
| 49 | |
} |
| 50 | |
|
| 51 | |
public void setExemptionService(ExemptionService exemptionService) { |
| 52 | 0 | this.exemptionService = exemptionService; |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
public PopulationService getPopulationService() { |
| 56 | 0 | return populationService; |
| 57 | |
} |
| 58 | |
|
| 59 | |
public void setPopulationService(PopulationService populationService) { |
| 60 | 0 | this.populationService = populationService; |
| 61 | 0 | } |
| 62 | |
|
| 63 | |
public ProcessService getProcessService() { |
| 64 | 0 | return processService; |
| 65 | |
} |
| 66 | |
|
| 67 | |
public void setProcessService(ProcessService processService) { |
| 68 | 0 | this.processService = processService; |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
public HoldCheckEvaluator getHoldCheckEvaluator() { |
| 72 | 0 | return holdCheckEvaluator; |
| 73 | |
} |
| 74 | |
|
| 75 | |
public void setHoldCheckEvaluator(HoldCheckEvaluator holdCheckEvaluator) { |
| 76 | 0 | this.holdCheckEvaluator = holdCheckEvaluator; |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
public MilestoneCheckEvaluator getMilestoneCheckEvaluator() { |
| 80 | 0 | return milestoneCheckEvaluator; |
| 81 | |
} |
| 82 | |
|
| 83 | |
public void setMilestoneCheckEvaluator(MilestoneCheckEvaluator milestoneCheckEvaluator) { |
| 84 | 0 | this.milestoneCheckEvaluator = milestoneCheckEvaluator; |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
public DirectRuleCheckEvaluator getDirectRuleCheckEvaluator() { |
| 88 | 0 | return directRuleCheckEvaluator; |
| 89 | |
} |
| 90 | |
|
| 91 | |
public void setDirectRuleCheckEvaluator(DirectRuleCheckEvaluator directRuleCheckEvaluator) { |
| 92 | 0 | this.directRuleCheckEvaluator = directRuleCheckEvaluator; |
| 93 | 0 | } |
| 94 | |
|
| 95 | |
public SubProcessCheckEvaluator getSubProcessCheckEvaluator() { |
| 96 | 0 | return subProcessCheckEvaluator; |
| 97 | |
} |
| 98 | |
|
| 99 | |
public void setSubProcessCheckEvaluator(SubProcessCheckEvaluator subProcessCheckEvaluator) { |
| 100 | 0 | this.subProcessCheckEvaluator = subProcessCheckEvaluator; |
| 101 | 0 | } |
| 102 | |
|
| 103 | |
@Override |
| 104 | |
public List<ValidationResultInfo> evaluate(CourseRegistrationProcessContextInfo processContext, ContextInfo context) |
| 105 | |
throws OperationFailedException { |
| 106 | 0 | List<ValidationResultInfo> allResults = new ArrayList<ValidationResultInfo>(); |
| 107 | |
|
| 108 | |
|
| 109 | |
List<InstructionInfo> instructions; |
| 110 | |
try { |
| 111 | 0 | instructions = processService.getInstructionsForEvaluation(processContext.getProcessKey(), context); |
| 112 | 0 | } catch (OperationFailedException ex) { |
| 113 | 0 | throw ex; |
| 114 | 0 | } catch (Exception ex) { |
| 115 | 0 | throw new OperationFailedException("unexpected", ex); |
| 116 | 0 | } |
| 117 | 0 | for (InstructionInfo instruction : instructions) { |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | 0 | boolean matchesPopulation = false; |
| 122 | 0 | for (String appliedPopulationKey : instruction.getAppliedPopulationKeys()) { |
| 123 | |
boolean isMember; |
| 124 | |
try { |
| 125 | 0 | isMember = populationService.isMember(processContext.getStudentId(), appliedPopulationKey, context); |
| 126 | 0 | } catch (OperationFailedException ex) { |
| 127 | 0 | throw ex; |
| 128 | 0 | } catch (Exception ex) { |
| 129 | 0 | throw new OperationFailedException("unexpected", ex); |
| 130 | 0 | } |
| 131 | 0 | if (isMember) { |
| 132 | 0 | matchesPopulation = true; |
| 133 | 0 | break; |
| 134 | |
} |
| 135 | 0 | } |
| 136 | 0 | if (!matchesPopulation) { |
| 137 | 0 | continue; |
| 138 | |
} |
| 139 | 0 | List<ValidationResultInfo> vrs = this.evaluateAnInstruction(instruction, processContext, context); |
| 140 | 0 | allResults.addAll(vrs); |
| 141 | 0 | if (hasErrors (vrs)) { |
| 142 | 0 | if (instruction.getContinueOnFail() != null) { |
| 143 | 0 | if (instruction.getContinueOnFail()) { |
| 144 | 0 | continue; |
| 145 | |
} |
| 146 | |
break; |
| 147 | |
} |
| 148 | |
} |
| 149 | 0 | } |
| 150 | 0 | return allResults; |
| 151 | |
} |
| 152 | |
|
| 153 | |
private static boolean hasErrors(List<ValidationResultInfo> vrs) { |
| 154 | 0 | return SubProcessCheckEvaluator.hasErrors(vrs); |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
private List<ValidationResultInfo> evaluateAnInstruction(InstructionInfo instruction, CourseRegistrationProcessContextInfo processContext, ContextInfo context) |
| 159 | |
throws OperationFailedException { |
| 160 | |
|
| 161 | |
List<ExemptionInfo> exemptions; |
| 162 | |
try { |
| 163 | 0 | exemptions = exemptionService.getActiveExemptionsByTypeProcessAndCheckForPerson(ExemptionServiceConstants.CHECK_EXEMPTION_TYPE_KEY, |
| 164 | |
processContext.getProcessKey(), |
| 165 | |
instruction.getCheckKey(), |
| 166 | |
processContext.getStudentId(), |
| 167 | |
context); |
| 168 | 0 | } catch (OperationFailedException ex) { |
| 169 | 0 | throw ex; |
| 170 | 0 | } catch (Exception ex) { |
| 171 | 0 | throw new OperationFailedException("unexpected", ex); |
| 172 | 0 | } |
| 173 | |
|
| 174 | 0 | if (!exemptions.isEmpty()) { |
| 175 | 0 | ValidationResultInfo vr = new ValidationResultInfo(); |
| 176 | 0 | vr.setElement(processContext.getProcessKey() + "-" + instruction.getCheckKey()); |
| 177 | 0 | vr.setLevel(ValidationResultInfo.ErrorLevel.OK); |
| 178 | 0 | vr.setMessage("exempted from check"); |
| 179 | 0 | return Arrays.asList(vr); |
| 180 | |
} |
| 181 | |
|
| 182 | |
CheckInfo check; |
| 183 | |
try { |
| 184 | 0 | check = processService.getCheck(instruction.getCheckKey(), context); |
| 185 | 0 | } catch (OperationFailedException ex) { |
| 186 | 0 | throw ex; |
| 187 | 0 | } catch (Exception ex) { |
| 188 | 0 | throw new OperationFailedException("unexpected", ex); |
| 189 | 0 | } |
| 190 | |
|
| 191 | |
|
| 192 | 0 | Calendar registerDate = Calendar.getInstance(); |
| 193 | 0 | registerDate.set(2011, 11, 30); |
| 194 | |
|
| 195 | |
|
| 196 | 0 | if (check.getTypeKey().equals(ProcessServiceConstants.START_DATE_CHECK_TYPE_KEY)) { |
| 197 | 0 | MilestoneCheckContext checkContext = new MilestoneCheckContext(); |
| 198 | 0 | checkContext.setInstruction(instruction); |
| 199 | 0 | checkContext.setCheck(check); |
| 200 | 0 | checkContext.setAtpKey(processContext.getTermKey()); |
| 201 | 0 | checkContext.setStudentId(processContext.getStudentId()); |
| 202 | |
|
| 203 | |
|
| 204 | 0 | checkContext.setDateToTest(registerDate.getTime()); |
| 205 | 0 | checkContext.setComparison(MilestoneCheckContext.START_DATE); |
| 206 | 0 | List<ValidationResultInfo> vrs = milestoneCheckEvaluator.evaluate(checkContext, context); |
| 207 | 0 | return vrs; |
| 208 | |
} |
| 209 | |
|
| 210 | 0 | if (check.getTypeKey().equals(ProcessServiceConstants.DEADLINE_CHECK_TYPE_KEY)) { |
| 211 | 0 | MilestoneCheckContext checkContext = new MilestoneCheckContext(); |
| 212 | 0 | checkContext.setInstruction(instruction); |
| 213 | 0 | checkContext.setCheck(check); |
| 214 | 0 | checkContext.setAtpKey(processContext.getTermKey()); |
| 215 | 0 | checkContext.setStudentId(processContext.getStudentId()); |
| 216 | |
|
| 217 | 0 | checkContext.setDateToTest(registerDate.getTime()); |
| 218 | 0 | checkContext.setComparison(MilestoneCheckContext.END_DATE); |
| 219 | 0 | List<ValidationResultInfo> vrs = milestoneCheckEvaluator.evaluate(checkContext, context); |
| 220 | 0 | return vrs; |
| 221 | |
} |
| 222 | |
|
| 223 | 0 | if (check.getTypeKey().equals(ProcessServiceConstants.TIME_PERIOD_CHECK_TYPE_KEY)) { |
| 224 | 0 | MilestoneCheckContext checkContext = new MilestoneCheckContext(); |
| 225 | 0 | checkContext.setInstruction(instruction); |
| 226 | 0 | checkContext.setCheck(check); |
| 227 | 0 | checkContext.setAtpKey(processContext.getTermKey()); |
| 228 | 0 | checkContext.setStudentId(processContext.getStudentId()); |
| 229 | |
|
| 230 | 0 | checkContext.setDateToTest(registerDate.getTime()); |
| 231 | 0 | checkContext.setComparison(MilestoneCheckContext.PERIOD); |
| 232 | 0 | List<ValidationResultInfo> vrs = milestoneCheckEvaluator.evaluate(checkContext, context); |
| 233 | 0 | return vrs; |
| 234 | |
} |
| 235 | |
|
| 236 | 0 | if (check.getTypeKey().equals(ProcessServiceConstants.HOLD_CHECK_TYPE_KEY)) { |
| 237 | 0 | HoldCheckContext checkContext = new HoldCheckContext(); |
| 238 | 0 | checkContext.setInstruction(instruction); |
| 239 | 0 | checkContext.setCheck(check); |
| 240 | 0 | checkContext.setAtpKey(processContext.getTermKey()); |
| 241 | 0 | checkContext.setStudentId(processContext.getStudentId()); |
| 242 | 0 | List<ValidationResultInfo> vrs = holdCheckEvaluator.evaluate(checkContext, context); |
| 243 | 0 | return vrs; |
| 244 | |
} |
| 245 | |
|
| 246 | 0 | if (check.getTypeKey().equals(ProcessServiceConstants.DIRECT_RULE_CHECK_TYPE_KEY)) { |
| 247 | 0 | DirectRuleCheckContext checkContext = new DirectRuleCheckContext(); |
| 248 | 0 | checkContext.setInstruction(instruction); |
| 249 | 0 | checkContext.setCheck(check); |
| 250 | 0 | checkContext.setAtpKey(processContext.getTermKey()); |
| 251 | 0 | checkContext.setStudentId(processContext.getStudentId()); |
| 252 | 0 | List<ValidationResultInfo> vrs = directRuleCheckEvaluator.evaluate(checkContext, context); |
| 253 | 0 | return vrs; |
| 254 | |
} |
| 255 | |
|
| 256 | 0 | if (check.getTypeKey().equals(ProcessServiceConstants.PROCESS_CHECK_TYPE_KEY)) { |
| 257 | 0 | SubProcessCheckContext checkContext = new SubProcessCheckContext(); |
| 258 | 0 | checkContext.setInstruction(instruction); |
| 259 | 0 | checkContext.setCheck(check); |
| 260 | 0 | checkContext.setAtpKey(processContext.getTermKey()); |
| 261 | 0 | checkContext.setStudentId(processContext.getStudentId()); |
| 262 | 0 | checkContext.setProcessEvaluator(this); |
| 263 | 0 | checkContext.setProcessContext(processContext); |
| 264 | 0 | List<ValidationResultInfo> vrs = subProcessCheckEvaluator.evaluate(checkContext, context); |
| 265 | 0 | return vrs; |
| 266 | |
} |
| 267 | 0 | throw new OperationFailedException(check.getTypeKey() + " is not yet supported"); |
| 268 | |
} |
| 269 | |
} |