package net.sourceforge.fenixedu.domain.studentCurriculum; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import net.sourceforge.fenixedu.dataTransferObject.administrativeOffice.dismissal.ISCTEDismissalBean.SelectedCurriculumEntry; import net.sourceforge.fenixedu.dataTransferObject.administrativeOffice.dismissal.ISCTEDismissalBean.SelectedDismissalEntry; import net.sourceforge.fenixedu.domain.CurricularCourse; import net.sourceforge.fenixedu.domain.Enrolment; import net.sourceforge.fenixedu.domain.ExecutionPeriod; import net.sourceforge.fenixedu.domain.StudentCurricularPlan; import net.sourceforge.fenixedu.domain.degreeStructure.OptionalCurricularCourse; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.student.curriculum.ICurriculumEntry; /** * * @author rjmvo * */ final public class ISCTEDismissalsUtils { public static Double getEnrolmentCredits(final Collection enrolments) { BigDecimal credits = BigDecimal.ZERO; for (SelectedCurriculumEntry selectedEnrolment : enrolments) { credits = credits.add(new BigDecimal(selectedEnrolment.getCreditsEcts().doubleValue())); } return Double.valueOf(credits.doubleValue()); } public static Double getDismissalCredits(final ExecutionPeriod executionPeriod, final Collection dismissals) { BigDecimal credits = BigDecimal.ZERO; for (final SelectedDismissalEntry selectedCurricularCourse : dismissals) { final Double curricularCourseCredits = selectedCurricularCourse.getCreditsEcts(executionPeriod); if (curricularCourseCredits == null) { throw new DomainException("error.credits.in.curricular.course.are.required", selectedCurricularCourse.getName()); } credits = credits.add(new BigDecimal(curricularCourseCredits.doubleValue())); } return Double.valueOf(credits.doubleValue()); } public static void checkGivenCredits(Credits credits, ExecutionPeriod executionPeriod, Collection entries, Collection dismissals) { checkGivenCredits(credits, executionPeriod, entries, getDismissalCredits(executionPeriod, dismissals)); } /** * This method is used on this class since all specific features related to * ISCTE dismissals extends this class and use ISelectedEnrolment interface */ public static void checkGivenCredits(Credits credits, ExecutionPeriod executionPeriod, Collection entries, Double givenCredits) { if (!credits.getStudentCurricularPlan().isBolonhaDegree()) { return; } // SUM(given credits) = SUM(dismissal credits) if (!entries.isEmpty() || !canHaveEmptyOriginEntries(credits)) { // if (!credits.isCurricularIntegration() || !entries.isEmpty() || // !dismissals.isEmpty()) { final Double sourceCredits = getEnrolmentCredits(entries); if (sourceCredits.doubleValue() != givenCredits.doubleValue()) { throw new DomainException("error.credits.given.in.dismissal"); } } // UC original credits >= SUM(UC given credits) final StudentCurricularPlan studentCurricularPlan = credits.getStudentCurricularPlan(); for (final SelectedCurriculumEntry selectedEnrolment : entries) { BigDecimal ects = new BigDecimal(selectedEnrolment.getCreditsEcts().doubleValue()); final ICurriculumEntry entry = selectedEnrolment.getCurriculumEntry(); if (isEnrolment(entry) && entry.getCurricularCourse().getDegree().isBolonhaDegree()) { for (final EnrolmentWrapper enrolmentWrapper : ((Enrolment) entry).getEnrolmentWrappers()) { if (enrolmentWrapper instanceof ISCTECurriculumEntryWrapper && isPartOfCurriculum(studentCurricularPlan, enrolmentWrapper)) { ects = ects.add(((ISCTECurriculumEntryWrapper) enrolmentWrapper).getEctsCredits()); } } if (ects.doubleValue() > entry.getEctsCreditsForCurriculum().doubleValue()) { throw new DomainException("error.credits.in.enrollment.are.being.exceeded"); } } } } static private boolean canHaveEmptyOriginEntries(final Credits credits) { return credits.isCurricularIntegration() || credits.isEquivalence(); } private static boolean isPartOfCurriculum(final StudentCurricularPlan scp, final EnrolmentWrapper enrolmentWrapper) { return enrolmentWrapper.getCredits().hasAnyDismissalFor(scp.getRoot()); } public static void addCurriculumEntries(Credits credits, Collection entries) { for (final SelectedCurriculumEntry entry : entries) { if (!entry.getCurriculumEntry().isValidSourceForCredits(credits)) { throw new DomainException("error.Credits.source.must.not.be.part.of.curriculum.structure", entry .getCurriculumEntry().getName().getContent()); } if (isDismissal(entry.getCurriculumEntry())) { new ISCTEDismissalWrapper(credits, (Dismissal) entry.getCurriculumEntry(), entry.getWeigth(), entry.getCreditsEcts()); } else if (isEnrolment(entry.getCurriculumEntry())) { new ISCTEInternalEnrolmentWrapper(credits, (Enrolment) entry.getCurriculumEntry(), entry.getWeigth(), entry.getCreditsEcts()); } else { new ISCTEExternalEnrolmentWrapper(credits, (ExternalEnrolment) entry.getCurriculumEntry(), entry.getWeigth(), entry.getCreditsEcts()); } } } static boolean isEnrolment(ICurriculumEntry entry) { return entry instanceof Enrolment; } static boolean isDismissal(ICurriculumEntry entry) { return entry instanceof Dismissal; } public static void addDismissalsEntries(Credits credits, Collection dismissals) { for (final SelectedDismissalEntry dismissal : dismissals) { Dismissal d = null; if (!dismissal.getCurricularCourse().isOptionalCurricularCourse()) { d = Dismissal.createNewDismissal(credits, credits.getStudentCurricularPlan(), dismissal.getCurriculumGroup(), dismissal.getCurricularCourse()); } else { d = Dismissal.createNewOptionalDismissal(credits, credits.getStudentCurricularPlan(), dismissal.getCurriculumGroup(), (OptionalCurricularCourse) dismissal.getCurricularCourse(), dismissal.getCreditsEcts(), dismissal.getWeight()); } d.setExecutionPeriod(credits.getExecutionPeriod()); } } public static void addCreditsDismissals(Credits credits, CurriculumGroup curriculumGroup) { CreditsDismissal.createNewDismissal(credits, credits.getStudentCurricularPlan(), curriculumGroup, getEmptyCurricularCourses()); } static Collection getCurricularCourses(Collection dismissals) { List result = new ArrayList(dismissals.size()); for (final SelectedDismissalEntry dismissal : dismissals) { result.add(dismissal.getCurricularCourse()); } return result; } static Collection getEmptyCurricularCourses() { return Collections. emptyList(); } }