package net.sourceforge.fenixedu.domain.studentCurriculum; import java.math.BigDecimal; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Set; import net.sourceforge.fenixedu.dataTransferObject.administrativeOffice.dismissal.DismissalBean.SelectedCurricularCourse; import net.sourceforge.fenixedu.dataTransferObject.administrativeOffice.dismissal.DismissalBean.SelectedOptionalCurricularCourse; import net.sourceforge.fenixedu.domain.CurricularCourse; import net.sourceforge.fenixedu.domain.ExecutionSemester; import net.sourceforge.fenixedu.domain.ExecutionYear; import net.sourceforge.fenixedu.domain.Grade; import net.sourceforge.fenixedu.domain.IEnrolment; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.StudentCurricularPlan; import net.sourceforge.fenixedu.domain.degreeStructure.Context; import net.sourceforge.fenixedu.domain.degreeStructure.CourseGroup; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.student.curriculum.ICurriculumEntry; import pt.ist.fenixWebFramework.security.accessControl.Checked; public class Credits extends Credits_Base { public Credits() { super(); setRootDomainObject(RootDomainObject.getInstance()); } public Credits(StudentCurricularPlan studentCurricularPlan, Collection dismissals, Collection enrolments, ExecutionSemester executionSemester) { this(); init(studentCurricularPlan, dismissals, enrolments, executionSemester); } public Credits(StudentCurricularPlan studentCurricularPlan, CourseGroup courseGroup, Collection enrolments, Collection noEnrolCurricularCourses, Double credits, ExecutionSemester executionSemester) { this(); init(studentCurricularPlan, courseGroup, enrolments, noEnrolCurricularCourses, credits, executionSemester); } public Credits(StudentCurricularPlan studentCurricularPlan, CurriculumGroup curriculumGroup, Collection enrolments, Double credits, ExecutionSemester executionSemester) { this(); init(studentCurricularPlan, curriculumGroup, enrolments, new HashSet(0), credits, executionSemester); } final protected void initExecutionPeriod(ExecutionSemester executionSemester) { if (executionSemester == null) { throw new DomainException("error.credits.wrong.arguments"); } setExecutionPeriod(executionSemester); } protected void init(StudentCurricularPlan studentCurricularPlan, CourseGroup courseGroup, Collection enrolments, Collection noEnrolCurricularCourses, Double credits, ExecutionSemester executionSemester) { if (studentCurricularPlan == null || courseGroup == null || credits == null) { throw new DomainException("error.credits.wrong.arguments"); } checkGivenCredits(studentCurricularPlan, courseGroup, credits, executionSemester); initExecutionPeriod(executionSemester); setStudentCurricularPlan(studentCurricularPlan); setGivenCredits(credits); addEnrolments(enrolments); Dismissal.createNewDismissal(this, studentCurricularPlan, courseGroup, noEnrolCurricularCourses); } protected void init(StudentCurricularPlan studentCurricularPlan, CurriculumGroup curriculumGroup, Collection enrolments, Collection noEnrolCurricularCourses, Double credits, ExecutionSemester executionSemester) { if (studentCurricularPlan == null || curriculumGroup == null || credits == null) { throw new DomainException("error.credits.wrong.arguments"); } initExecutionPeriod(executionSemester); setStudentCurricularPlan(studentCurricularPlan); setGivenCredits(credits); addEnrolments(enrolments); Dismissal.createNewDismissal(this, studentCurricularPlan, curriculumGroup, noEnrolCurricularCourses); } private void checkGivenCredits(final StudentCurricularPlan studentCurricularPlan, final CourseGroup courseGroup, final Double credits, final ExecutionSemester executionSemester) { if (courseGroup.isBolonhaDegree() && !allowsEctsCredits(studentCurricularPlan, courseGroup, executionSemester, credits.doubleValue())) { throw new DomainException("error.credits.invalid.credits", credits.toString()); } } private boolean allowsEctsCredits(final StudentCurricularPlan studentCurricularPlan, final CourseGroup courseGroup, final ExecutionSemester executionSemester, final double ectsCredits) { final double ectsCreditsForCourseGroup = studentCurricularPlan.getCreditsConcludedForCourseGroup(courseGroup) .doubleValue(); if (ectsCredits + ectsCreditsForCourseGroup > courseGroup.getMaxEctsCredits(executionSemester).doubleValue()) { return false; } if (courseGroup.isCycleCourseGroup() || courseGroup.isRoot()) { return true; } for (final Context context : courseGroup.getParentContexts()) { if (context.isOpen(executionSemester)) { if (allowsEctsCredits(studentCurricularPlan, context.getParentCourseGroup(), executionSemester, ectsCredits)) { return true; } } } return false; } protected void init(StudentCurricularPlan studentCurricularPlan, Collection dismissals, Collection enrolments, ExecutionSemester executionSemester) { if (studentCurricularPlan == null || dismissals == null || dismissals.isEmpty()) { throw new DomainException("error.credits.wrong.arguments"); } initExecutionPeriod(executionSemester); setStudentCurricularPlan(studentCurricularPlan); addEnrolments(enrolments); for (final SelectedCurricularCourse selectedCurricularCourse : dismissals) { if (selectedCurricularCourse.isOptional()) { final SelectedOptionalCurricularCourse selectedOptionalCurricularCourse = (SelectedOptionalCurricularCourse) selectedCurricularCourse; Dismissal.createNewOptionalDismissal(this, studentCurricularPlan, selectedOptionalCurricularCourse .getCurriculumGroup(), selectedOptionalCurricularCourse.getCurricularCourse(), selectedOptionalCurricularCourse.getCredits()); } else { Dismissal.createNewDismissal(this, studentCurricularPlan, selectedCurricularCourse.getCurriculumGroup(), selectedCurricularCourse.getCurricularCourse()); } } } private void addEnrolments(final Collection enrolments) { if (enrolments != null) { for (final IEnrolment enrolment : enrolments) { EnrolmentWrapper.create(this, enrolment); } } } final public Collection getIEnrolments() { final Set result = new HashSet(); for (final EnrolmentWrapper enrolmentWrapper : this.getEnrolmentsSet()) { IEnrolment enrolment = enrolmentWrapper.getIEnrolment(); if (enrolment != null) { result.add(enrolmentWrapper.getIEnrolment()); } } return result; } final public boolean hasIEnrolments(final IEnrolment iEnrolment) { for (final EnrolmentWrapper enrolmentWrapper : this.getEnrolmentsSet()) { if (enrolmentWrapper.getIEnrolment() == iEnrolment) { return true; } } return false; } final public boolean hasAnyIEnrolments() { return hasAnyEnrolments(); } @Override final public Double getGivenCredits() { if (super.getGivenCredits() == null) { BigDecimal bigDecimal = BigDecimal.ZERO; for (Dismissal dismissal : getDismissalsSet()) { bigDecimal = bigDecimal.add(new BigDecimal(dismissal.getEctsCredits())); } return Double.valueOf(bigDecimal.doubleValue()); } return super.getGivenCredits(); } public String getGivenGrade() { return null; } public Grade getGrade() { return null; } @Checked("CreditsPredicates.DELETE") final public void delete() { for (; hasAnyDismissals(); getDismissals().get(0).delete()) ; for (; hasAnyEnrolments(); getEnrolments().get(0).delete()) ; removeStudentCurricularPlan(); removeRootDomainObject(); removeExecutionPeriod(); super.deleteDomainObject(); } final public Double getEnrolmentsEcts() { Double result = 0d; for (final IEnrolment enrolment : getIEnrolments()) { result = result + enrolment.getEctsCredits(); } return result; } final public boolean hasGivenCredits() { return getGivenCredits() != null; } final public boolean hasGivenCredits(final Double ectsCredits) { return hasGivenCredits() && getGivenCredits().equals(ectsCredits); } public boolean isTemporary() { return false; } public boolean isCredits() { return true; } public boolean isSubstitution() { return false; } public boolean isEquivalence() { return false; } public Collection getAverageEntries(final ExecutionYear executionYear) { return Collections.emptyList(); } public boolean hasAnyDismissalInCurriculum() { for (final Dismissal dismissal : getDismissalsSet()) { if (!dismissal.parentCurriculumGroupIsNoCourseGroupCurriculumGroup()) { return true; } } return false; } }