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 AverageWeightRule extends AverageWeightRule_Base { protected AverageWeightRule() { super(); } public AverageWeightRule(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 AverageWeightRuleResult(topCurriculumGroup, entries, scale, roundingMode); } static private class AverageWeightRuleResult extends AverageRuleResult { static private final long serialVersionUID = 1L; public AverageWeightRuleResult(final CurriculumGroup topCurriculumGroup, final Collection entries, final int scale, final RoundingMode roundingMode) { for (ICurriculumEntry entry : entries) { if (entry.getGrade().isNumeric()) { calculateCurriculumEntryAverage(entry, topCurriculumGroup); } } average = (sumOfWeight == BigDecimal.ZERO) ? sumOfWeight : gradeTimesFactor.divide(sumOfWeight, scale, roundingMode); } private void calculateCurriculumEntryAverage(final ICurriculumEntry entry, final CurriculumGroup topCurriculumGroup) { BigDecimal factor; if (!hasEnrolmentWrappers(entry, topCurriculumGroup)) { factor = entry.getWeigthForCurriculum(); sumOfEcts = sumOfEcts.add(entry.getEctsCreditsForCurriculum()); } else { factor = getEnrolmentWrappersWeight(entry, topCurriculumGroup); sumOfEcts = sumOfEcts.add(getEnrolmentWrappersEctsCredits(entry, topCurriculumGroup)); } sumOfWeight = sumOfWeight.add(factor); gradeTimesFactor = gradeTimesFactor.add(entry.getGrade().getNumericValue().multiply(factor)); } } @Override public boolean isWeightRule() { return true; } }