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.domain.candidacy.accounting.CandidacyCostDetail; import net.sourceforge.fenixedu.domain.candidacy.accounting.paymentCodes.SecondCycleCandidacyPaymentCode; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.util.Money; import org.joda.time.DateTime; import org.joda.time.Interval; import pt.iscte.ci.metadata.ISCTE; import pt.ist.fenixWebFramework.services.Service; public class DegreeCurricularPlanCandidacyPeriod extends DegreeCurricularPlanCandidacyPeriod_Base { public DegreeCurricularPlanCandidacyPeriod(final Collection degreeCurricularPlans, final ExecutionYear executionYear, final DateTime startDate, final DateTime endDate, CandidacyCostDetail amountDetail) { super(); init(degreeCurricularPlans, executionYear, startDate, endDate, null); setCandidacyCostDetail(amountDetail); } public DegreeCurricularPlanCandidacyPeriod(final Collection degreeCurricularPlans, final ExecutionYear executionYear, final DateTime startDate, final DateTime endDate, Money money) { super(); init(degreeCurricularPlans, executionYear, startDate, endDate, money); } protected void init(Collection degreeCurricularPlans, ExecutionYear executionYear, DateTime startDate, DateTime endDate, Money money) { // ISCTE modified checkRuleToCreate(degreeCurricularPlans, executionYear, startDate, endDate); setCost(money); super.init(degreeCurricularPlans, executionYear.getFirstExecutionPeriod(), startDate, endDate); } private void checkRuleToCreate(Collection degreeCurricularPlans, ExecutionYear executionYear, DateTime startDate, DateTime endDate) { Interval newInterval = new Interval(startDate, endDate); for (DegreeCurricularPlan degreeCurricularPlan : degreeCurricularPlans) { for (DegreeCurricularPlanCandidacyPeriod period : degreeCurricularPlan .getDegreeCurricularPlanCandidacyPeriods(executionYear)) { Interval otherInterval = new Interval(period.getStartDateDateTime(), period.getEndDateDateTime()); if (newInterval.overlaps(otherInterval)) { throw new DomainException("error.candidate.periodManagement.notPossibleToCreateOverlapping"); } } } } public ExecutionYear getExecutionYear() { return getExecutionPeriod().getExecutionYear(); } @Override public void delete() { for (SecondCycleCandidacyPaymentCode code : getCodes()) { if (code.hasCandidacyPaymentDetail()) { code.removePeriod(); } else { code.delete(); } } removeCandidacyCostDetail(); super.delete(); } @Override public Money getCost() { if (hasCandidacyCostDetail()) { return getCandidacyCostDetail().getCost(); } return super.getCost(); } @ISCTE(author = "saidh") public static List readPeriods(final ExecutionPeriod executionPeriod, final DateTime startDateTime, final DateTime endDateTime, final CandidacyCostDetail amountDetail, final DateTime finalizationStartDateTime, final DateTime finalizationEndDateTime) { final List result = new ArrayList(); for (final CandidacyPeriod candidacyPeriod : CandidacyPeriod.readPeriods(executionPeriod, startDateTime, endDateTime)) { if (candidacyPeriod instanceof DegreeCurricularPlanCandidacyPeriod) { final DegreeCurricularPlanCandidacyPeriod degreeCandidacyPeriod = (DegreeCurricularPlanCandidacyPeriod) candidacyPeriod; if (degreeCandidacyPeriod.getCandidacyCostDetail() == amountDetail && degreeCandidacyPeriod.candidacyFinalizationPeriodEqualsDates(finalizationStartDateTime, finalizationEndDateTime)) { result.add(degreeCandidacyPeriod); } } } return result; } @Service public void addDegreeCurricularPlans(final Collection degreeCurricularPlans) { super.getDegreeCurricularPlan().addAll(degreeCurricularPlans); } @ISCTE public static ExecutionYear readLastPeriodYear() { final List result = new ArrayList(); for (CandidacyPeriod candidacyPeriod : RootDomainObject.getInstance().getCandidacyPeriods()) { if (candidacyPeriod instanceof DegreeCurricularPlanCandidacyPeriod) { result.add(((DegreeCurricularPlanCandidacyPeriod) candidacyPeriod).getExecutionYear()); } } return Collections.max(result); } @ISCTE public int getPeriodOrderForDegreeCurricularPlan(final DegreeCurricularPlan degreeCurricularPlan) { final List candidacyPeriods = new ArrayList(degreeCurricularPlan.getCandidacyPeriods()); Collections.sort(candidacyPeriods, CandidacyPeriod.COMPARATOR_BY_START_DATE); int order = 0; for (final CandidacyPeriod candidacyPeriod : candidacyPeriods) { if (candidacyPeriod == this) { return order + 1; } if (candidacyPeriod instanceof DegreeCurricularPlanCandidacyPeriod) { order++; } } return order; } @ISCTE public boolean isCandidacyFinalizationPeriodOpen() { DateTime candidacyFinalizationPeriodStartDate = getCandidacyFinalizationPeriodStartDate(); DateTime candidacyFinalizationPeriodEndDate = getCandidacyFinalizationPeriodEndDate(); if (candidacyFinalizationPeriodStartDate == null || candidacyFinalizationPeriodEndDate == null) { return false; } return candidacyFinalizationPeriodStartDate.isBeforeNow() && candidacyFinalizationPeriodEndDate.isAfterNow(); } @ISCTE public static boolean isAnyCandidacyFinalizationPeriodOpen(final DegreeCurricularPlan degreeCurricularPlan) { for (final CandidacyPeriod candidacyPeriod : new ArrayList(degreeCurricularPlan.getCandidacyPeriods())) { if (candidacyPeriod instanceof DegreeCurricularPlanCandidacyPeriod) { final DegreeCurricularPlanCandidacyPeriod degreeCurricularPlanCandidacyPeriod = (DegreeCurricularPlanCandidacyPeriod) candidacyPeriod; DateTime startDate = degreeCurricularPlanCandidacyPeriod.getCandidacyFinalizationPeriodStartDate(); DateTime endDate = degreeCurricularPlanCandidacyPeriod.getCandidacyFinalizationPeriodEndDate(); if (startDate != null && endDate != null && startDate.isBeforeNow() && endDate.isAfterNow()) { return true; } } } return false; } @ISCTE private boolean candidacyFinalizationPeriodEqualsDates(final DateTime finalizationStart, final DateTime finalizationEnd) { return (getCandidacyFinalizationPeriodStartDate() == finalizationStart || finalizationStart != null && finalizationStart.equals(getCandidacyFinalizationPeriodStartDate())) && (getCandidacyFinalizationPeriodEndDate() == finalizationEnd || finalizationEnd != null && finalizationEnd.equals(getCandidacyFinalizationPeriodEndDate())); } }