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.TimeInterval;
010    import edu.nrao.sss.util.FormatString;
011    
012    import org.apache.log4j.Logger;
013    
014    public class TimeIntervalConverter implements Converter
015    {
016            private static Logger log = Logger.getLogger(TimeIntervalConverter.class);
017            
018            public Object getAsObject(FacesContext context, UIComponent component, String value)
019            {
020                    log.debug("Trying to convert " + value + " to an object");
021                    try 
022                    {
023                            return TimeInterval.parse(value, FormatString.ISO8601_DATE_TIME, FormatString.ISO8601_TIME_INTERVAL_SEPARATOR);
024                    }
025                    catch (IllegalArgumentException iae)
026                    {
027                            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Value '" + value + "' is not in a recognized time 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 TimeInterval)
039                            return 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    }