package net.sourceforge.fenixedu.domain.curricularRules; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import net.sourceforge.fenixedu.dataTransferObject.GenericPair; import net.sourceforge.fenixedu.domain.ExecutionSemester; import net.sourceforge.fenixedu.domain.curricularRules.executors.RuleResult; import net.sourceforge.fenixedu.domain.curricularRules.executors.verifyExecutors.VerifyRuleExecutor; import net.sourceforge.fenixedu.domain.curricularRules.executors.verifyExecutors.VerifyRuleLevel; import net.sourceforge.fenixedu.domain.degreeStructure.Context; import net.sourceforge.fenixedu.domain.degreeStructure.CourseGroup; import net.sourceforge.fenixedu.domain.degreeStructure.DegreeModule; import net.sourceforge.fenixedu.domain.enrolment.EnrolmentContext; import net.sourceforge.fenixedu.domain.enrolment.IDegreeModuleToEvaluate; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import dml.runtime.RelationAdapter; public abstract class CompositeRule extends CompositeRule_Base { static { CurricularRuleCompositeRule.addListener(new RelationAdapter() { @Override public void beforeAdd(CompositeRule compositeRule, CurricularRule curricularRule) { if (curricularRule.getNotRule() != null) { throw new DomainException("error.curricular.rule.invalid.state"); } } }); } protected CompositeRule() { } protected void initCompositeRule(CurricularRule... curricularRules) { if (curricularRules.length < 2) { throw new DomainException("curricular.rule.invalid.parameters"); } if (!haveAllSameDegreeModule(curricularRules)) { throw new DomainException("curricular.rule.invalid.parameters"); } this.setDegreeModuleToApplyRule(curricularRules[0].getDegreeModuleToApplyRule()); this.setBegin(getBeginExecutionPeriod(curricularRules)); this.setEnd(getEndExecutionPeriod(curricularRules)); for (CurricularRule rule : curricularRules) { rule.removeDegreeModuleToApplyRule(); rule.setParentCompositeRule(this); } } private ExecutionSemester getEndExecutionPeriod(CurricularRule[] curricularRules) { ExecutionSemester executionSemester = null; for (CurricularRule rule : curricularRules) { if (rule.getEnd() == null) { return null; } if (executionSemester == null || rule.getEnd().isAfter(executionSemester)) { executionSemester = rule.getEnd(); } } return executionSemester; } private ExecutionSemester getBeginExecutionPeriod(CurricularRule... curricularRules) { ExecutionSemester executionSemester = null; for (CurricularRule rule : curricularRules) { if (executionSemester == null || rule.getBegin().isBefore(executionSemester)) { executionSemester = rule.getBegin(); } } return executionSemester; } private boolean haveAllSameDegreeModule(CurricularRule... curricularRules) { DegreeModule degreeModule = curricularRules[0].getDegreeModuleToApplyRule(); for (CurricularRule rule : curricularRules) { if (!rule.getDegreeModuleToApplyRule().equals(degreeModule)) { return false; } } return true; } @Override public abstract List> getLabel(); public List> getLabel(final String operator) { final List> labelList = new ArrayList>(); labelList.add(new GenericPair("( ", false)); final Iterator curricularRulesIterator = getCurricularRules().listIterator(); while (curricularRulesIterator.hasNext()) { labelList.addAll(curricularRulesIterator.next().getLabel()); if (curricularRulesIterator.hasNext()) { labelList.add(new GenericPair(" ", false)); labelList.add(new GenericPair(operator, true)); labelList.add(new GenericPair(" ", false)); } } labelList.add(new GenericPair(" )", false)); return labelList; } @Override public RuleResult evaluate(final IDegreeModuleToEvaluate sourceDegreeModuleToEvaluate, final EnrolmentContext enrolmentContext) { throw new DomainException("unsupported.composite.rule"); } @Override public RuleResult verify(VerifyRuleLevel verifyRuleLevel, EnrolmentContext enrolmentContext, DegreeModule degreeModuleToVerify, CourseGroup parentCourseGroup) { throw new DomainException("unsupported.composite.rule"); } public VerifyRuleExecutor createVerifyRuleExecutor() { throw new DomainException( "error.net.sourceforge.fenixedu.domain.curricularRules.CompositeRule.does.not.support.createVerifyRuleExecutor"); } @Override public boolean appliesToContext(Context context) { for (CurricularRule curricularRule : this.getCurricularRules()) { if (!curricularRule.appliesToContext(context)) { return false; } } return true; } @Override protected void removeOwnParameters() { for (; !getCurricularRules().isEmpty(); getCurricularRules().get(0).delete()) ; } @Override public boolean isLeaf() { return false; } }