001 package edu.nrao.sss.webapp.faces.converter; 002 003 import java.math.BigDecimal; 004 import javax.faces.convert.Converter; 005 import javax.faces.convert.ConverterException; 006 import javax.faces.context.FacesContext; 007 import javax.faces.component.UIComponent; 008 import javax.faces.application.FacesMessage; 009 010 import org.apache.log4j.Logger; 011 012 import edu.nrao.sss.util.StringUtil; 013 014 /** 015 * 016 */ 017 public class BigDecimalConverter implements Converter 018 { 019 private static Logger log = Logger.getLogger(BigDecimalConverter.class); 020 021 private static final StringUtil util = StringUtil.getInstance(); 022 023 public Object getAsObject(FacesContext context, UIComponent component, String value) 024 { 025 log.debug("Trying to convert " + value + " to an object"); 026 027 BigDecimal d = null; 028 if (value != null && value.length() > 0) 029 { 030 try 031 { 032 d = new BigDecimal(value); 033 } 034 035 catch (NumberFormatException nfe) 036 { 037 throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Value '" + value + "' is not a real number format we recognize."), nfe); 038 } 039 } 040 041 return d; 042 } 043 044 public String getAsString(FacesContext context, UIComponent component, Object value) 045 { 046 log.debug("Trying to convert " + value + " to a string"); 047 048 if (value == null) 049 return "0"; 050 051 else if (value instanceof BigDecimal) 052 { 053 return util.formatNoScientificNotation((BigDecimal)value); 054 } 055 056 else 057 { 058 log.warn("Cannot Convert objects of type: " + value.getClass()); 059 throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Cannot Convert objects of type: " + value.getClass())); 060 } 061 } 062 }