2 Replies Latest reply on May 7, 2008 10:59 AM by Rosario Nicolazzo

    rich:calendar and XMLGregorianCalendar converter

    Rosario Nicolazzo Newbie

      Hi,
      I'm trying to use a Converter to convert a XMLGregorianCalendar,
      when open page the conversion is ok and method getAsString is invoked
      but when post the request method getAsObject is not invoked and in jsf page view this message and not find errors in the logs and system out:

      insert date: An error occurred when processing your submitted information.


      this is my jsf code:

      <rich:calendar value="#{HomeBB.home.insertDate}" popup="true" label="insert date" datePattern="dd/MM/yyyy" showApplyButton="false"/>
      


      this is my faces-config:
      <converter>
       <converter-for-class>javax.xml.datatype.XMLGregorianCalendar</converter-for-class>
       <converter-class>com.homesite.converter.XMLGregorianCalendarConverter</converter-class>
       </converter>
      


      this is converter:
      import java.text.DateFormat;
      import java.text.SimpleDateFormat;
      import java.util.Date;
      import java.util.GregorianCalendar;
      import java.util.Locale;
      import javax.faces.component.UIComponent;
      import javax.faces.context.FacesContext;
      import javax.faces.convert.Converter;
      import javax.faces.convert.ConverterException;
      import javax.xml.datatype.DatatypeFactory;
      import javax.xml.datatype.XMLGregorianCalendar;
      import org.richfaces.component.UICalendar;
      
      public class XMLGregorianCalendarConverter implements Converter {
      
       @Override
       public Object getAsObject(FacesContext context, UIComponent component, String value) {
       System.out.println("invoke getAsObject");
       GregorianCalendar cal = null;
       XMLGregorianCalendar calendar = null;
      
       if (value != null && value.length() > 0) {
       Locale locale = (Locale) ((UICalendar) component).getLocale();
       String pattern = ((UICalendar) component).getDatePattern();
       DateFormat df = new SimpleDateFormat(pattern, locale);
      
       try {
       Date date = df.parse(value);
       cal = new GregorianCalendar();
       cal.setTime(date);
      
       calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
       } catch (Exception e) {
       e.printStackTrace();
       throw new ConverterException(e);
       }
       }
       return calendar;
       }
      
       @Override
       public String getAsString(FacesContext context, UIComponent component, Object value) {
       System.out.println("invoke getASString");
       String result = null;
      
       if (value != null) {
       if (value instanceof String) {
       result = (String) value;
       } else {
       DateFormat df;
       if (component instanceof UICalendar) {
       Locale locale = (Locale) ((UICalendar) component).getLocale();
       String pattern = ((UICalendar) component).getDatePattern();
       df = new SimpleDateFormat(pattern, locale);
       } else {
       Locale locale = context.getViewRoot().getLocale();
       df = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
       }
      
       Date date = ((XMLGregorianCalendar) value).toGregorianCalendar().getTime();
       result = df.format(date);
       }
       }
       return result;
       }
      }