0 Replies Latest reply on Aug 24, 2010 3:30 AM by Sofie Struyve

    Using custom String calendar converter

    Sofie Struyve Newbie

      Hi,

       

      In the (seam) project I'm currently working on,

      we represent dates in our model as a string, with a specific format (yyyyDDmm).

       

      However, we don't want to represent the user with this internal format, but with a different format (dd-MM-yyyy).

       

      I tried to solve this by writing a custom converter and add that to the rich:calendar control.

       

      This converter uses the default DateConverter to convert the GUI datestring (from dd-MM-yyyy) to a date, and then uses

      our internal converter to convert this date to the internal string format (yyyyMMdd), and vice-versa.

       

      dd-MM-yyyy ->(internalconverter)-> date ->(defaultDateConverter)-> yyyyMMdd

      yyyyMMdd ->(defaultDateConverter)-> date ->(internalConverter)-> dd-MM-yyyy

       

      @Name("filterDateConverter")

      @BypassInterceptors

      @Converter

      public class FilterDateConverter extends DateConverter {

          @Override

          public Object getAsObject(FacesContext context, UIComponent component, String value) {

              final Object date = super.getAsObject(context, component, value);

              return getConverter().toString(date);

          }


          @Override

          public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {

              final Object date = getConverter().fromString((String)arg2);

              return super.getAsString(arg0, arg1, date);

          }

         

          private DateDataTypeConverterImpl getConverter() {

              return (DateDataTypeConverterImpl)Component.getInstance(DateDataTypeConverterImpl.class, true);

          }

      }


      And in the gui:

       

               <rich:calendar
                      value="#{filterKolom.waarde}"
                      id="#{'waarde_calendar_'}#{filterKolom.number}"
                      required="true"
                      popup="true" datePattern="dd-MM-yyyy" showApplyButton="false"
                      enableManualInput="true" cellWidth="24px" cellHeight="22px" style="width:200px"
                      converter="filterDateConverter">
                  </rich:calendar>     
         

       

      Unfortunately, this doesn't work.

       

      the org.richfaces.component.UICalendar class assumes that if the value is a string,

      it knows how to convert the string to a date and tries to convert 20100824 to a date using dd-MM-yyyy,

      which obviously doesn't work. (see UICalendar.getAsDate(Object date))

       

      I solved this by wrapping our internal string in a wrapped class (which isn't a String), but I was wondering if there is a good reason

      why UICalendar doesn't use the custom converter for strings. or should I raise a bug for this?