package net.sourceforge.fenixedu.domain; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import net.sourceforge.fenixedu.dataTransferObject.administrativeOffice.equivalencyPlan.DCPTransitionPlanBean; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import org.apache.axis.utils.StringUtils; import pt.iscte.ci.metadata.ISCTE; @ISCTE(author = "Paulo Zenida") public class DCPTransitionPlan extends DCPTransitionPlan_Base { public DCPTransitionPlan() { super(); setRootDomainObject(RootDomainObject.getInstance()); } public DCPTransitionPlan(DCPTransitionPlanBean transitionPlanBean) { this(); check(transitionPlanBean); setName(transitionPlanBean.getName()); setOriginDcp(transitionPlanBean.getOriginDcp()); setDestinyDcp(transitionPlanBean.getDestinyDcp()); setExecutionYear(transitionPlanBean.getExecutionYear()); } public void check(final DCPTransitionPlanBean transitionPlanBean) { if (transitionPlanBean == null) { throw new DomainException("dcp.transition.plan.bean.not.null"); } if (StringUtils.isEmpty(transitionPlanBean.getName())) { throw new DomainException("dcp.transition.plan.name.not.null"); } if (transitionPlanBean.getOriginDcp() == null) { throw new DomainException("dcp.transition.plan.originDcp.not.null"); } if (transitionPlanBean.getDestinyDcp() == null) { throw new DomainException("dcp.transition.plan.destinyDcp.not.null"); } checkForExistingPlan(transitionPlanBean); } private void checkForExistingPlan(final DCPTransitionPlanBean transitionPlanBean) { for (final DCPTransitionPlan plan : transitionPlanBean.getOriginDcp().getOriginDcpTransitionPlans()) { if (plan.isFor(transitionPlanBean.getDestinyDcp(), transitionPlanBean.getExecutionYear())) { throw new DomainException("dcp.transition.plan.already.exists.in.origin.and.destiny.dcp.in.same.execution.year"); } } } private boolean isFor(DegreeCurricularPlan destinyDcp, ExecutionYear executionYear) { return getDestinyDcp().equals(destinyDcp) && getExecutionYear().equals(executionYear); } public boolean canDelete() { if (hasAnyDcpTransitionPlanRules()) { return false; } return true; } public void delete() { if (canDelete()) { removeOriginDcp(); removeDestinyDcp(); removeExecutionYear(); removeRootDomainObject(); deleteDomainObject(); } else { throw new DomainException("error.cannot.delete.transitionPlan"); } } public void edit(DCPTransitionPlanBean transitionPlanBean) { setName(transitionPlanBean.getName()); setOriginDcp(transitionPlanBean.getOriginDcp()); setDestinyDcp(transitionPlanBean.getDestinyDcp()); } public ExecutionPeriod getExecutionPeriod() { return getExecutionYear().getFirstExecutionPeriod(); } public Collection getSortedDcpTransitionPlanRules() { final List result = new ArrayList(); result.addAll(getDcpTransitionPlanRules()); Collections.sort(result, DCPTransitionPlanRule.COMPARATOR_BY_RULE_ORDER); return result; } static public List readByOriginAndDestinyDcp(final DegreeCurricularPlan originDcp, final DegreeCurricularPlan destinyDcp) { final List result = new ArrayList(); for (final DCPTransitionPlan plan : RootDomainObject.getInstance().getDcpTransitionPlans()) { if (plan.getOriginDcp() == originDcp && plan.getDestinyDcp() == destinyDcp) { result.add(plan); } } return result; } }