package net.sourceforge.fenixedu.dataTransferObject.student; import java.io.Serializable; import java.math.BigDecimal; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import net.sourceforge.fenixedu.domain.DegreeCurricularPlan; import net.sourceforge.fenixedu.domain.DomainReference; import net.sourceforge.fenixedu.domain.ExecutionYear; import net.sourceforge.fenixedu.domain.student.Registration; import net.sourceforge.fenixedu.domain.student.curriculum.AverageRule; import net.sourceforge.fenixedu.domain.student.curriculum.ICurriculum; import net.sourceforge.fenixedu.domain.studentCurriculum.CurriculumGroup; import net.sourceforge.fenixedu.domain.studentCurriculum.CycleCurriculumGroup; import org.joda.time.YearMonthDay; import pt.iscte.ci.metadata.ISCTE; public class RegistrationConclusionBean implements Serializable, IRegistrationBean { static private final long serialVersionUID = 5825221957160251388L; private DomainReference registration; private DomainReference cycleCurriculumGroup; private DomainReference conclusionYear; private YearMonthDay conclusionDate; transient private ConclusionCalculator calculator; public RegistrationConclusionBean(Registration registration) { this(registration, null); } public RegistrationConclusionBean(Registration registration, CycleCurriculumGroup cycleCurriculumGroup) { setRegistration(registration); setCycleCurriculumGroup(cycleCurriculumGroup); updateCalculator(); } private ConclusionCalculator updateCalculator() { if (calculator == null) { calculator = new ConclusionCalculator(); calculator.updateAverageRule(getConclusionYear()); } return calculator; } public ExecutionYear getConclusionYear() { if (this.conclusionYear != null) { return this.conclusionYear.getObject(); } if (isConclusionProcessed()) { return hasCycleCurriculumGroup() ? getCycleCurriculumGroup().getConclusionYear() : getRegistration() .getConclusionYear(); } return hasCycleCurriculumGroup() ? getCycleCurriculumGroup().calculateConclusionYear() : getRegistration() .calculateConclusionYear(); } @ISCTE(author = "Nadir Amin") public ExecutionYear getCalculatedConclusionYear() { return hasCycleCurriculumGroup() ? getCycleCurriculumGroup().calculateConclusionYear() : getRegistration() .calculateConclusionYear(); } public void setConclusionYear(final ExecutionYear conclusionYear) { this.conclusionYear = (conclusionYear != null) ? new DomainReference(conclusionYear) : null; } public CycleCurriculumGroup getCycleCurriculumGroup() { return (this.cycleCurriculumGroup != null) ? this.cycleCurriculumGroup.getObject() : null; } public void setCycleCurriculumGroup(CycleCurriculumGroup cycleCurriculumGroup) { this.cycleCurriculumGroup = (cycleCurriculumGroup != null) ? new DomainReference( cycleCurriculumGroup) : null; } public boolean hasCycleCurriculumGroup() { return getCycleCurriculumGroup() != null; } public Registration getRegistration() { return (this.registration != null) ? this.registration.getObject() : null; } public void setRegistration(Registration registration) { this.registration = (registration != null) ? new DomainReference(registration) : null; } public Integer getFinalAverage() { if (isConclusionProcessed()) { return hasCycleCurriculumGroup() ? getCycleCurriculumGroup().getFinalAverage() : getRegistration().getFinalAverage(); } return updateCalculator().getFinalAverage(); } public BigDecimal getAverage() { return updateCalculator().calculateAverage(); } public YearMonthDay getConclusionDate() { // ISCTE if (this.conclusionDate != null) { return this.conclusionDate; } if (isConclusionProcessed()) { return hasCycleCurriculumGroup() ? getCycleCurriculumGroup().getConclusionDate() : getRegistration() .getConclusionDate(); } return hasCycleCurriculumGroup() ? getCycleCurriculumGroup().calculateConclusionDate() : getRegistration() .calculateConclusionDate(); } @ISCTE(author = "Nadir Amin") public YearMonthDay getCalculatedConclusionDate() { return hasCycleCurriculumGroup() ? getCycleCurriculumGroup().calculateConclusionDate() : getRegistration() .calculateConclusionDate(); } @ISCTE(author = "Nadir Amin") public void setConclusionDate(YearMonthDay conclusionDate) { this.conclusionDate = conclusionDate; } public double getEctsCredits() { return hasCycleCurriculumGroup() ? getCycleCurriculumGroup().getCreditsConcluded() : getRegistration().getEctsCredits(); } public ICurriculum getCurriculumForConclusion() { return updateCalculator().getCurriculum(); } public int getCurriculumEntriesSize() { return getCurriculumForConclusion().getCurriculumEntries().size(); } public String getDegreeDescription() { return hasCycleCurriculumGroup() ? getRegistration().getDegreeDescription(getCycleCurriculumGroup().getCycleType()) : getRegistration().getDegreeDescription(); } public boolean isConcluded() { return hasCycleCurriculumGroup() ? getCycleCurriculumGroup().isConcluded() : getRegistration().hasConcluded(); } public Collection getCurriculumGroupsNotVerifyingStructure() { if (hasCycleCurriculumGroup()) { final Collection result = new HashSet(); getCycleCurriculumGroup().assertCorrectStructure(result); return result; } else { return Collections.emptyList(); } } public boolean isConclusionProcessed() { return hasCycleCurriculumGroup() ? getCycleCurriculumGroup().isConclusionProcessed() : getRegistration() .isRegistrationConclusionProcessed(); } public boolean getCanBeConclusionProcessed() { return !isConclusionProcessed() && isConcluded() && (getCurriculumGroupsNotVerifyingStructure().isEmpty() || getRegistration().getWasTransition()); } public boolean isByCycle() { return hasCycleCurriculumGroup() || getRegistration().getDegreeType().isComposite(); } public String getConclusionProcessNotes() { return hasCycleCurriculumGroup() ? getCycleCurriculumGroup().getConclusionProcessNotes() : getRegistration() .getConclusionProcessNotes(); } public String getConclusionProcessResponsibleIstUsername() { return hasCycleCurriculumGroup() ? getCycleCurriculumGroup().getConclusionProcessResponsibleIstUsername() : getRegistration().getConclusionProcessResponsibleIstUsername(); } public boolean getCanBeConclusionProcessedUpdated() { return isConclusionProcessed() && isConcluded() && (getCurriculumGroupsNotVerifyingStructure().isEmpty() || getRegistration().getWasTransition()); } private class ConclusionCalculator { private ICurriculum curriculum; private ConclusionCalculator() { curriculum = hasCycleCurriculumGroup() ? getCycleCurriculumGroup().getCurriculum() : getRegistration() .getCurriculum(); } private ICurriculum getCurriculum() { return curriculum; } private void updateAverageRule(final ExecutionYear executionYear) { curriculum.changeAverageRule(searchAverageRule(executionYear)); } private BigDecimal calculateAverage() { return curriculum.getAverage(); } /** * For registration use specific method from registration */ private Integer getFinalAverage() { return hasCycleCurriculumGroup() ? curriculum.getRoundedAverage() : getRegistration().calculateFinalAverage(); } } public void resetCalculator() { this.calculator = null; } private AverageRule searchAverageRule(final ExecutionYear executionYear) { return getDegreeCurricularPlan(executionYear).getAverageRule(executionYear); } private DegreeCurricularPlan getDegreeCurricularPlan(final ExecutionYear executionYear) { return getRegistration().getStudentCurricularPlan(executionYear).getDegreeCurricularPlan(); } public String getAverageRuleDescription() { return searchAverageRule(getConclusionYear()).getAbbreviatedDescription(); } }