Index: impl/src/java/org/sakaiproject/component/gradebook/GradebookServiceHibernateImpl.java =================================================================== --- impl/src/java/org/sakaiproject/component/gradebook/GradebookServiceHibernateImpl.java (revision 97135) +++ impl/src/java/org/sakaiproject/component/gradebook/GradebookServiceHibernateImpl.java (working copy) @@ -2838,4 +2838,42 @@ if(log.isDebugEnabled()) log.debug("GradebookManager.applyDropScores took " + (System.currentTimeMillis() - start) + " millis to execute"); } + + public PointsPossibleValidation isPointsPossibleValid(String gradebookUid, org.sakaiproject.service.gradebook.shared.Assignment gradebookItem, + Double pointsPossible) { + if (gradebookUid == null) { + throw new IllegalArgumentException("Null gradebookUid passed to isPointsPossibleValid"); + } + if (gradebookItem == null) { + throw new IllegalArgumentException("Null gradebookItem passed to isPointsPossibleValid"); + } + + // At this time, all gradebook items follow the same business rules for + // points possible (aka relative weight in % gradebooks) so special logic + // using the properties of the gradebook item is unnecessary. + // In the future, we will have the flexibility to change + // that behavior without changing the method signature + + // the points possible must be a non-null value greater than 0 with + // no more than 2 decimal places + + if (pointsPossible == null) { + return PointsPossibleValidation.INVALID_NULL_VALUE; + } + + if (pointsPossible.doubleValue() <= 0) { + return PointsPossibleValidation.INVALID_NUMERIC_VALUE; + } + // ensure there are no more than 2 decimal places + BigDecimal bd = new BigDecimal(pointsPossible.doubleValue()); + bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); // Two decimal places + double roundedVal = bd.doubleValue(); + double diff = pointsPossible - roundedVal; + if (diff != 0) { + return PointsPossibleValidation.INVALID_DECIMAL; + } + + return PointsPossibleValidation.VALID; + } + } \ No newline at end of file Index: api/src/java/org/sakaiproject/service/gradebook/shared/GradebookService.java =================================================================== --- api/src/java/org/sakaiproject/service/gradebook/shared/GradebookService.java (revision 97135) +++ api/src/java/org/sakaiproject/service/gradebook/shared/GradebookService.java (working copy) @@ -62,6 +62,33 @@ public static final MathContext MATH_CONTEXT = new MathContext(10, RoundingMode.HALF_DOWN); + /** + * An enum for defining valid/invalid information for a points possible/relative weight + * value for a gradebook item. See {@link GradebookService#isPointsPossibleValid(String, Assignment, Double)} + * for usage + */ + public enum PointsPossibleValidation { + /** + * The points possible/relative weight is valid + */ + VALID, + /** + * The points possible/relative weight is invalid because it is null + * and a value is required. + */ + INVALID_NULL_VALUE, + /** + * The points possible/relative weight is invalid because it + * is a value <= 0 + */ + INVALID_NUMERIC_VALUE, + /** + * The points possible/relative weight is invalid because it contains + * more than 2 decimal places + */ + INVALID_DECIMAL + } + public static Comparator lettergradeComparator = new Comparator() { public int compare(Object o1, Object o2) @@ -777,4 +804,19 @@ */ public String getLowestPossibleGradeForGbItem(final String gradebookUid, final Long gradebookItemId); + /** + * + * @param gradebookUid (non-null) + * @param gradebookItem (non-null) the Assignment object representing the gradebook item for which you are + * setting the points possible (aka relative weight). May be a new gradebook item without + * an id yet. + * @param pointsPossible the points possible/relative weight you would like to validate + * for the gradebookItem above. + * @return {@link PointsPossibleValidation} value indicating the validity of the given + * points possible/relative weight or a problem code defining why it is invalid + */ + public PointsPossibleValidation isPointsPossibleValid(String gradebookUid, org.sakaiproject.service.gradebook.shared.Assignment gradebookItem, + Double pointsPossible); + + }