package net.sourceforge.fenixedu.domain.student.curriculum; import static net.sourceforge.fenixedu.domain.studentCurriculum.ISCTEEnrolmentWrapperUtils.getEnrolmentWrappersEctsCredits; import static net.sourceforge.fenixedu.domain.studentCurriculum.ISCTEEnrolmentWrapperUtils.getEnrolmentWrappersWeight; import static net.sourceforge.fenixedu.domain.studentCurriculum.ISCTEEnrolmentWrapperUtils.hasEnrolmentWrappers; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Collection; import net.sourceforge.fenixedu.domain.DegreeCurricularPlan; import net.sourceforge.fenixedu.domain.ExecutionYear; import net.sourceforge.fenixedu.domain.studentCurriculum.CurriculumGroup; public class AverageEctsRule extends AverageEctsRule_Base { protected AverageEctsRule() { super(); } public AverageEctsRule(final DegreeCurricularPlan degreeCurricularPlan, final ExecutionYear executionYear) { this(); init(degreeCurricularPlan, executionYear); } @Override protected AverageRuleResult createResult(final CurriculumGroup topCurriculumGroup, final Collection entries, final int scale, final RoundingMode roundingMode) { return new AverateEctsRuleResult(topCurriculumGroup, entries, scale, roundingMode); } static private class AverateEctsRuleResult extends AverageRuleResult { static private final long serialVersionUID = 1L; public AverateEctsRuleResult(final CurriculumGroup topCurriculumGroup, final Collection entries, final int scale, final RoundingMode roundingMode) { for (final ICurriculumEntry entry : entries) { if (entry.getGrade().isNumeric()) { calculateCurriculumEntryAverage(entry, topCurriculumGroup); } } average = (sumOfEcts.compareTo(BigDecimal.ZERO)==0) ? sumOfEcts : gradeTimesFactor.divide(sumOfEcts, scale, roundingMode); } protected void calculateCurriculumEntryAverage(final ICurriculumEntry entry, final CurriculumGroup topCurriculumGroup) { BigDecimal factor; if (!hasEnrolmentWrappers(entry, topCurriculumGroup)) { factor = entry.getEctsCreditsForCurriculum(); sumOfWeight = sumOfWeight.add(entry.getWeigthForCurriculum()); } else { factor = getEnrolmentWrappersEctsCredits(entry, topCurriculumGroup); sumOfWeight = sumOfWeight.add(getEnrolmentWrappersWeight(entry, topCurriculumGroup)); } sumOfEcts = sumOfEcts.add(factor); gradeTimesFactor = gradeTimesFactor.add(entry.getGrade().getNumericValue().multiply(factor)); } } }