001    package edu.nrao.sss.webapp.faces.converter;
002    
003    import javax.faces.convert.Converter;
004    import javax.faces.convert.ConverterException;
005    import javax.faces.context.FacesContext;
006    import javax.faces.component.UIComponent;
007    import javax.faces.application.FacesMessage;
008    
009    import edu.nrao.sss.measure.TimeOfDayInterval;
010    
011    import org.apache.log4j.Logger;
012    
013    public class TimeOfDayIntervalConverter implements Converter
014    {
015            private static Logger log = Logger.getLogger(TimeOfDayIntervalConverter.class);
016            
017            public Object getAsObject(FacesContext context, UIComponent component, String value)
018            {
019                    log.debug("Trying to convert " + value + " to an object");
020                    try 
021                    {
022                            return TimeOfDayInterval.parse(value, "-");
023                    }
024                    catch (IllegalArgumentException iae)
025                    {
026                            log.error("Could not convert '" + value + "'", iae);
027                            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Value '" + value + "' is not in a recognized Time of Day Interval format."), iae);
028                    }
029            }
030    
031            public String getAsString(FacesContext context, UIComponent component, Object value)
032            {
033                    log.debug("Trying to convert " + value + " to a string");
034                    
035                    if (value == null)
036                            return "";
037    
038                    else if (value instanceof TimeOfDayInterval)
039                            return ((TimeOfDayInterval)value).toString("-");
040    
041                    else
042                    {
043                            log.warn("Cannot Convert objects of type: " + value.getClass());
044                            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Cannot Convert objects of type: " + value.getClass()));
045                    }
046            }
047    }