01: /**********************************************************************************
02: *
03: * $Id: CourseGradeConverter.java 22547 2007-03-13 16:56:50Z 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:
25: import javax.faces.application.FacesMessage;
26: import javax.faces.component.UIComponent;
27: import javax.faces.context.FacesContext;
28: import javax.faces.convert.Converter;
29: import javax.faces.convert.ConverterException;
30:
31: import org.apache.commons.lang.StringUtils;
32: import org.sakaiproject.tool.gradebook.GradeMapping;
33: import org.sakaiproject.tool.gradebook.ui.GradebookBean;
34:
35: /**
36: * Validates and standardizes course grades.
37: */
38: public class CourseGradeConverter implements Converter, Serializable {
39:
40: public Object getAsObject(FacesContext context,
41: UIComponent component, String value)
42: throws ConverterException {
43: value = StringUtils.trimToNull(value);
44: if (value != null) {
45: // Get the current gradebook.
46: GradebookBean gbb = (GradebookBean) FacesUtil
47: .resolveVariable("gradebookBean");
48:
49: // Get the current grade mapping.
50: GradeMapping mapping = gbb.getGradebookManager()
51: .getGradebook(gbb.getGradebookId())
52: .getSelectedGradeMapping();
53:
54: // Find the corresponding standardized form, if any.
55: value = mapping.standardizeInputGrade(value);
56: if (value == null) {
57: throw new ConverterException(
58: new FacesMessage(
59: FacesUtil
60: .getLocalizedString(context,
61: "org.sakaiproject.gradebook.tool.jsf.CourseGradeConverter.INVALID")));
62: }
63: }
64: return value;
65: }
66:
67: public String getAsString(FacesContext context,
68: UIComponent component, Object value) {
69: if (value == null) {
70: return null;
71: }
72: return (String) value;
73: }
74:
75: }
|