01: /**********************************************************************************
02: *
03: * $Id: AssignmentGradeValidator.java 9271 2006-05-10 21:52:49Z ray@media.berkeley.edu $
04: *
05: ***********************************************************************************
06: *
07: * Copyright (c) 2005 The Regents of the University of California, The MIT Corporation
08: *
09: * Licensed under the Educational Community License, Version 1.0 (the "License");
10: * you may not use this file except in compliance with the License.
11: * You may obtain a copy of the License at
12: *
13: * http://www.opensource.org/licenses/ecl1.php
14: *
15: * Unless required by applicable law or agreed to in writing, software
16: * distributed under the License is distributed on an "AS IS" BASIS,
17: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18: * See the License for the specific language governing permissions and
19: * limitations under the License.
20: *
21: **********************************************************************************/package org.sakaiproject.tool.gradebook.jsf;
22:
23: import java.io.Serializable;
24: import java.math.BigDecimal;
25:
26: import javax.faces.application.FacesMessage;
27: import javax.faces.component.UIComponent;
28: import javax.faces.context.FacesContext;
29: import javax.faces.validator.Validator;
30: import javax.faces.validator.ValidatorException;
31:
32: import org.apache.commons.logging.Log;
33: import org.apache.commons.logging.LogFactory;
34:
35: /**
36: * Validates assignment grades entered into the gradebook. Since we display a
37: * maximum of two decimal places in the UI, we use this validator to ensure that
38: * the maximum precision entered into the gradebook is also two decimal places.
39: * This should reduce rounding errors between actual scores and what is displayed
40: * in the UI.
41: *
42: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman </a>
43: */
44: public class AssignmentGradeValidator implements Validator,
45: Serializable {
46: private static Log logger = LogFactory
47: .getLog(AssignmentGradeValidator.class);
48:
49: /**
50: * @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext,
51: * javax.faces.component.UIComponent, java.lang.Object)
52: */
53: public void validate(FacesContext context, UIComponent component,
54: Object value) throws ValidatorException {
55: if (value != null) {
56: if (!(value instanceof Number)) {
57: throw new IllegalArgumentException(
58: "The assignment grade must be a number");
59: }
60: double grade = ((Number) value).doubleValue();
61: BigDecimal bd = new BigDecimal(grade);
62: bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); // Two decimal places
63: double roundedVal = bd.doubleValue();
64: double diff = grade - roundedVal;
65: if (diff != 0) {
66: throw new ValidatorException(
67: new FacesMessage(
68: FacesUtil
69: .getLocalizedString(context,
70: "org.sakaiproject.gradebook.tool.jsf.AssignmentGradeValidator.PRECISION")));
71: }
72: }
73: }
74: }
|