package net.sourceforge.fenixedu.domain; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.ResourceBundle; import java.util.Set; import net.sourceforge.fenixedu.dataTransferObject.administrativeOffice.equivalencyPlan.DCPTransitionPlanRuleBean; import net.sourceforge.fenixedu.dataTransferObject.administrativeOffice.equivalencyPlan.DCPTransitionPlanRuleBean.SelectedContext; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.studentCurriculum.Credits; import net.sourceforge.fenixedu.domain.studentCurriculum.CurricularIntegration; import net.sourceforge.fenixedu.domain.studentCurriculum.Equivalence; import net.sourceforge.fenixedu.domain.studentCurriculum.ISCTEEquivalence; import net.sourceforge.fenixedu.domain.studentCurriculum.ISCTEExemptionCredits; import net.sourceforge.fenixedu.domain.studentCurriculum.ISCTESubstitution; import net.sourceforge.fenixedu.domain.studentCurriculum.ISCTETransitionCredits; import net.sourceforge.fenixedu.util.LanguageUtils; import pt.iscte.ci.metadata.ISCTE; @ISCTE(author = "Paulo Zenida") public class DCPTransitionPlanRule extends DCPTransitionPlanRule_Base { static public Comparator COMPARATOR_BY_RULE_ORDER = new Comparator() { public int compare(DCPTransitionPlanRule o1, DCPTransitionPlanRule o2) { if (o1.getRuleOrder() == null) { return 1; } if (o2.getRuleOrder() == null) { return -1; } int result = o1.getRuleOrder().compareTo(o2.getRuleOrder()); return result != 0 ? result : COMPARATOR_BY_ID.compare(o1, o2); } }; public enum RuleType { TRANSITION(ISCTETransitionCredits.class), SUBSTITUTION(ISCTESubstitution.class), INTEGRATION(CurricularIntegration.class), EQUIVALENCE(Equivalence.class, ISCTEEquivalence.class), CREDITS(Credits.class), EXEMPTION_CREDITS(ISCTEExemptionCredits.class); private List> creditsClasses; private RuleType(final Class... creditsClasses) { this.creditsClasses = new ArrayList>(creditsClasses.length); for (final Class creditsClass : creditsClasses) { this.creditsClasses.add(creditsClass); } } static public RuleType ruleTypeBy(Class clazz) { for (final RuleType type : values()) { if (type.creditsClasses.contains(clazz)) { return type; } } return null; } public String getLocalizedName() { return ResourceBundle.getBundle("resources.EnumerationResources", LanguageUtils.getLocale()).getString(getLabel()); } private String getLabel() { return this.getClass().getSimpleName() + "." + name(); } } public DCPTransitionPlanRule() { super(); setRootDomainObject(RootDomainObject.getInstance()); setNormalizeOriginEcts(false); } public DCPTransitionPlanRule(final DCPTransitionPlan transitionPlan, final Set origins, final Set destinies) { this(); checkGivenCredits(transitionPlan, origins, destinies); init(transitionPlan, origins, destinies); } private void init(DCPTransitionPlan transitionPlan, Set origins, Set destinies) { setDcpTransitionPlan(transitionPlan); for (final SelectedContext selectedContext : origins) { addOriginDcpTransitionPlanRuleWrappers(new OriginDCPTransitionPlanRuleWrapper(selectedContext)); } for (final SelectedContext selectedContext : destinies) { addDestinyDcpTransitionPlanRuleWrappers(new DestinyDCPTransitionPlanRuleWrapper(selectedContext)); } } public static void createTransitionPlanRule(DCPTransitionPlanRuleBean bean) { final DCPTransitionPlan transitionPlan = bean.getDcpTransitionPlan(); DCPTransitionPlanRule rule = new DCPTransitionPlanRule(transitionPlan, bean.getSelectedOriginEntries(), bean .getSelectedDestinyEntries()); rule.setRuleType(bean.getRuleType().name()); } private static BigDecimal getOriginCredits(final Set origins) { BigDecimal credits = BigDecimal.ZERO; for (DCPTransitionPlanRuleBean.SelectedContext selectedContext : origins) { credits = credits.add(new BigDecimal(selectedContext.getCreditsEcts().doubleValue())); } return credits; } private static BigDecimal getDestinyCredits(final Set destinies, final ExecutionPeriod executionPeriod) { BigDecimal credits = BigDecimal.ZERO; for (final DCPTransitionPlanRuleBean.SelectedContext selectedContext : destinies) { credits = credits.add(new BigDecimal(selectedContext.getCurricularCourse().getEctsCredits(executionPeriod) .doubleValue())); } return credits; } static void checkGivenCredits(final DCPTransitionPlan transitionPlan, final Set origins, final Set destinies) { final BigDecimal originCredits = getOriginCredits(origins); final BigDecimal destinyCredits = getDestinyCredits(destinies, transitionPlan.getExecutionPeriod()); if (!originCredits.equals(destinyCredits)) { throw new DomainException("error.transition.rule.origin.credits.different.from.destiny.credits"); } } public void delete() { while (!getOriginDcpTransitionPlanRuleWrappers().isEmpty()) { getOriginDcpTransitionPlanRuleWrappers().get(0).delete(); } while (!getDestinyDcpTransitionPlanRuleWrappers().isEmpty()) { getDestinyDcpTransitionPlanRuleWrappers().get(0).delete(); } removeDcpTransitionPlan(); removeRootDomainObject(); deleteDomainObject(); } public RuleType getRuleTypeEnum() { return RuleType.valueOf(getRuleType()); } public boolean hasDestinyOperator() { return getDestinyOperator() != null; } public Collection getSortedOriginDcpTransitionPlanRuleWrappers() { if (getOriginDcpTransitionPlanRuleWrappersCount() == 1) { return getOriginDcpTransitionPlanRuleWrappers(); } final List result = new ArrayList( getOriginDcpTransitionPlanRuleWrappers()); Collections.sort(result, DCPTransitionPlanRuleWrapper.COMPARE_BY_FULL_PATH_AND_NAME); return result; } public DegreeCurricularPlan getOriginDcp() { return getDcpTransitionPlan().getOriginDcp(); } public DegreeCurricularPlan getDestinyDcp() { return getDcpTransitionPlan().getDestinyDcp(); } public String getDcpTransitionPlanName() { return getDcpTransitionPlan().getName(); } }