2 Replies Latest reply on May 6, 2008 12:17 PM by r_nicolazzo

    rich:calendar and XMLGregorianCalendar

    r_nicolazzo

      Hi,
      i use a "rich:calendar" component in a jsf page, the component value is a XMLGregorianCalendar object from a web-service!

      rich:calendar don't accept XMLGregorianCalendar

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

      have you a solution for use XMLGregorianCalendar in RichFaces component??

      Thanks



        • 1. Re: rich:calendar and XMLGregorianCalendar
          fabmars

          From my own experience:

          - rich:calendar doesnt handle Calendar types and even less XMLCalendar types. Only java.util.Date.


          - you don't wanna use XMLGregorianCalendar's elsewhere than in your WS layer. This is just an architectural advice, one could argue on, but personally I dont see the point in keeping "convenience/transport" (or how you wanna call them) classes in places you can't even manipulate them. Seriously, XMLGregorianCalendars are a pain in the ass, don't you think ? Better use adapters on your client-side like explained here: http://weblogs.java.net/blog/kohsuke/archive/2005/09/using_jaxb_20s.html


          - Ayhow, you have to make and register a JSF converter anyway that will process date <> (XML)GregorianCalendar conversions. Check the JSF tutorial for how-to's.
          Btw I had already posted about a simple Calendar converter a while ago: http://jboss.com/index.html?module=bb&op=viewtopic&t=131758
          but my code has changed a bit since:


          package org.mars.toolkit.jsf.converter;
          
          import java.text.DateFormat;
          import java.text.ParseException;
          import java.text.SimpleDateFormat;
          import java.util.Calendar;
          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 org.richfaces.component.UICalendar;
          
          
          public class CalendarConverter implements Converter {
          
           public Object getAsObject(FacesContext context, UIComponent component, String value) {
          
           Calendar cal = 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);
           }
           catch (ParseException e) {
           throw new ConverterException(e);
           }
           }
           return cal;
           }
          
           public String getAsString(FacesContext context, UIComponent component, Object value) {
          
           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 = ((Calendar) value).getTime();
           result = df.format(date);
           }
           }
           return result;
           }
          
          }
          


          Hope this helps.

          • 2. Re: rich:calendar and XMLGregorianCalendar
            r_nicolazzo

            Hi fabmars,
            10q for you reply and architectural advice,
            I agree about your assessment of XMLGregorianCalendars :o), there is no convenience is only about a justification graphics.

            Try one of two directions