package net.sourceforge.fenixedu.domain.student.curriculum; import java.io.Serializable; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Collection; import java.util.ResourceBundle; import net.sourceforge.fenixedu.domain.DegreeCurricularPlan; import net.sourceforge.fenixedu.domain.DomainObject; import net.sourceforge.fenixedu.domain.ExecutionYear; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.studentCurriculum.CurriculumGroup; import net.sourceforge.fenixedu.util.LanguageUtils; abstract public class AverageRule extends AverageRule_Base implements Comparable { protected AverageRule() { super(); setRootDomainObject(RootDomainObject.getInstance()); } protected void init(final DegreeCurricularPlan degreeCurricularPlan, final ExecutionYear executionYear) { check(degreeCurricularPlan, "error.AverageRule.invalid.degreeCurricularPlan"); check(executionYear, "error.AverageRule.invalid.executionYear"); checkForExistingRule(degreeCurricularPlan, executionYear); setExecutionYear(executionYear); setDegreeCurricularPlan(degreeCurricularPlan); } protected void checkForExistingRule(final DegreeCurricularPlan degreeCurricularPlan, final ExecutionYear executionYear) { for (final AverageRule averageRule : degreeCurricularPlan.getAverageRules()) { if (averageRule != this && averageRule.isFor(executionYear)) { throw new DomainException("error.AverageRule.already.exists.rule.for.executionYear", executionYear.getName()); } } } public boolean isFor(final ExecutionYear executionYear) { return getExecutionYear().equals(executionYear); } public void delete() { checkRulesToDelete(); disconnect(); super.deleteDomainObject(); } protected void checkRulesToDelete() { // TODO Auto-generated method stub } protected void disconnect() { removeDegreeCurricularPlan(); removeExecutionYear(); removeRootDomainObject(); } public int compareTo(final AverageRule other) { int result = getExecutionYear().compareTo(other.getExecutionYear()); return result != 0 ? result : DomainObject.COMPARATOR_BY_ID.compare(this, other); } public AverageRuleResult calculate(final CurriculumGroup topCurriculumGroup, final Collection entries, final int scale, final RoundingMode roundingMode) { return createResult(topCurriculumGroup, entries, scale, roundingMode); } abstract protected AverageRuleResult createResult(CurriculumGroup topCurriculumGroup, Collection entries, int scale, RoundingMode roundingMode); public String getDescription() { return getMessage("description"); } public String getAbbreviatedDescription() { return getMessage("abbreviated.description"); } private String getMessage(final String suffix) { return ResourceBundle.getBundle("resources.ApplicationResources", LanguageUtils.getLocale()).getString( getClass().getSimpleName() + "." + suffix); } static abstract public class AverageRuleResult implements Serializable { static private final long serialVersionUID = 1L; protected BigDecimal average = BigDecimal.ZERO; protected BigDecimal sumOfEcts = BigDecimal.ZERO; protected BigDecimal sumOfWeight = BigDecimal.ZERO; protected BigDecimal gradeTimesFactor = BigDecimal.ZERO; public String getAverageResult() { final StringBuilder result = new StringBuilder(); result.append(gradeTimesFactor).append(" / "); result.append(sumOfEcts).append(" = ").append(average.setScale(2, RoundingMode.HALF_UP)); return result.toString(); } public BigDecimal getAverage() { return average; } public BigDecimal getSumOfEcts() { return sumOfEcts; } public BigDecimal getSumOfWeight() { return sumOfWeight; } public BigDecimal getGradeTimesFactor() { return gradeTimesFactor; } } public boolean isWeightRule() { return false; } }