package net.sourceforge.fenixedu.domain.organizationalStructure; import java.util.Comparator; import net.sourceforge.fenixedu.domain.CompetenceCourse; import net.sourceforge.fenixedu.domain.ExecutionYear; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import pt.iscte.ci.metadata.ISCTE; public class CCGUContext extends CCGUContext_Base { static final public Comparator COMPARATOR_BY_YEAR = new Comparator() { public int compare(CCGUContext o1, CCGUContext o2) { return o1.getEndExecutionYear().getYear().compareTo(o2.getEndExecutionYear().getYear()); } }; static final public Comparator COMPARATOR_BY_BEGIN_YEAR = new Comparator() { public int compare(CCGUContext o1, CCGUContext o2) { if (!o1.hasBeginExecutionYear()) { return 1; } if (!o2.hasBeginExecutionYear()) { return -1; } return o1.getBeginExecutionYear().getYear().compareTo(o2.getBeginExecutionYear().getYear()); } }; public CCGUContext() { super(); setRootDomainObject(RootDomainObject.getInstance()); } public CCGUContext(CompetenceCourseGroupUnit competenceCourseGroupUnit, CompetenceCourse CompetenceCourse) { this(); super.setCompetenceCourseGroupUnit(competenceCourseGroupUnit); super.setCompetenceCourse(CompetenceCourse); } public CCGUContext(CompetenceCourseGroupUnit competenceCourseGroupUnit, CompetenceCourse competenceCourse, ExecutionYear beginExecutionYear, ExecutionYear endExecutionYear) { this(); super.setCompetenceCourseGroupUnit(competenceCourseGroupUnit); super.setCompetenceCourse(competenceCourse); editPeriods(beginExecutionYear, endExecutionYear); } public CCGUContext(CCGUContext ggcuContext) { this(); super.setCompetenceCourseGroupUnit(ggcuContext.getCompetenceCourseGroupUnit()); super.setCompetenceCourse(ggcuContext.getCompetenceCourse()); editPeriods(ggcuContext.getBeginExecutionYear(), ggcuContext.getEndExecutionYear()); } public void delete() { removeCompetenceCourseGroupUnit(); removeCompetenceCourse(); removeBeginExecutionYear(); removeEndExecutionYear(); removeRootDomainObject(); super.deleteDomainObject(); } public void editPeriods(final ExecutionYear beginExecutionYear, final ExecutionYear endExecutionYear) { editPeriods(beginExecutionYear, endExecutionYear, true); } public void editPeriods(final ExecutionYear beginExecutionYear, final ExecutionYear endExecutionYear, boolean checkFirstVersion) { if (endExecutionYear != null && endExecutionYear.isBefore(beginExecutionYear)) { throw new DomainException("error.CCGUContext.endPeriodBeforeBeginPeriod"); } if (hasCompetenceCourse()) { if (checkFirstVersion && getCompetenceCourse().getFirstCCGUContext() == this && beginExecutionYear != getCompetenceCourse().getOldestCompetenceCourseInformation().getExecutionYear()) { throw new DomainException("error.CCGUContext.periodBeginsDifferentFromCompetenceFirstVersion"); } for (final CCGUContext ccguContext : getCompetenceCourse().getCCGUContexts()) { if (ccguContext != this && ccguContext.overlaps(beginExecutionYear, endExecutionYear)) { if (ccguContext.isOpen() && beginExecutionYear.isAfter(ccguContext.getBeginExecutionYear())) { ccguContext.setEndExecutionYear(beginExecutionYear.getPreviousExecutionYear()); } else { throw new DomainException("error.CCGUContext.periodOverlapsWithExistingContext", getCompetenceCourse() .getCodeAndName()); } } } } super.setBeginExecutionYear(beginExecutionYear); super.setEndExecutionYear(endExecutionYear); } private boolean isOpen() { return getEndExecutionYear() == null; } private boolean overlaps(ExecutionYear beginExecutionYear, ExecutionYear endExecutionYear) { if (isOpen() && endExecutionYear == null) { return true; } if (endExecutionYear == null) { return getEndExecutionYear().isAfterOrEquals(beginExecutionYear); } if (isOpen()) { return endExecutionYear.isAfterOrEquals(getBeginExecutionYear()); } return !getEndExecutionYear().isBefore(beginExecutionYear) && !getBeginExecutionYear().isAfter(endExecutionYear); } public boolean contains(final ExecutionYear executionYear) { return getBeginExecutionYear().isBeforeOrEquals(executionYear) && (isOpen() || getEndExecutionYear().isAfterOrEquals(executionYear)); } @ISCTE(author = "Nadir Amin") public boolean isValidOn(ExecutionYear executionYear) { return contains(executionYear); } }