package net.sourceforge.fenixedu.domain; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; import java.util.Date; import java.util.List; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import org.joda.time.DateTime; import pt.iscte.ci.metadata.ISCTE; import pt.ist.fenixframework.pstm.LoggingRelation; import dml.runtime.RelationAdapter; public class CandidacyPeriod extends CandidacyPeriod_Base { static { ((LoggingRelation) DegreeCurricularPlanCandidacyPeriod.getInverseRelation()) .addListener(new RelationAdapter() { @Override public void afterRemove(DegreeCurricularPlan dcp, CandidacyPeriod period) { if (dcp != null && period != null && !period.hasAnyDegreeCurricularPlan()) { period.delete(); } } }); } public static final Comparator COMPARATOR_BY_START_DATE = new Comparator() { public int compare(CandidacyPeriod left, CandidacyPeriod right) { final int result = left.getStartDateDateTime().compareTo(right.getStartDateDateTime()); return result == 0 ? COMPARATOR_BY_ID.compare(left, right) : result; } }; public CandidacyPeriod() { super(); setRootDomainObject(RootDomainObject.getInstance()); } protected void init(final Collection degreeCurricularPlans, final ExecutionPeriod executionPeriod, final Date startDate, final Date endDate) { init(degreeCurricularPlans, executionPeriod, new DateTime(startDate), new DateTime(endDate)); } protected void init(final Collection degreeCurricularPlans, final ExecutionPeriod executionPeriod, final DateTime startDate, final DateTime endDate) { if (!endDate.isAfter(startDate)) { throw new DomainException("EnrolmentPeriod.end.date.must.be.after.start.date"); } for (DegreeCurricularPlan degreeCurricularPlan : degreeCurricularPlans) { addDegreeCurricularPlan(degreeCurricularPlan); } setExecutionPeriod(executionPeriod); setStartDateDateTime(startDate); setEndDateDateTime(endDate); } public boolean isValid() { return containsDate(new DateTime()); } public boolean isValid(final Date date) { return containsDate(new DateTime(date)); } public boolean containsDate(DateTime date) { return !(getStartDateDateTime().isAfter(date) || getEndDateDateTime().isBefore(date)); } public void delete() { for (DegreeCurricularPlan dcp : getDegreeCurricularPlanSet()) { removeDegreeCurricularPlan(dcp); } removeExecutionPeriod(); removeRootDomainObject(); deleteDomainObject(); } @ISCTE(author = "Nadir Amin") public boolean isOpenFor(ExecutionPeriod executionPeriod) { return isFor(executionPeriod) && isValid(); } @ISCTE(author = "Nadir Amin") public boolean isFor(ExecutionPeriod executionPeriod) { return getExecutionPeriod() == executionPeriod; } @ISCTE(author = "Nadir Amin") public boolean isAfter(DateTime dateTime) { return getStartDateDateTime().isAfter(dateTime); } @ISCTE(author = "saidh") public static List readPeriods(final ExecutionPeriod executionPeriod, final DateTime startDateTime, final DateTime endDateTime) { final List result = new ArrayList(); for (final CandidacyPeriod candidacyPeriod : executionPeriod.getCandidacyPeriod()) { if (candidacyPeriod.getStartDateDateTime().equals(startDateTime) && candidacyPeriod.getEndDateDateTime().equals(endDateTime)) { result.add(candidacyPeriod); } } return result; } }