2 Replies Latest reply on Oct 11, 2010 12:38 PM by Nicola Jones

    rich calendar backed by a String object

    Nicola Jones Newbie

      Hi, I'm having trouble getting my rich:calendar to work. It's backed by a String property and it has a custom converter the combination of which I believe to be causing my problem.  I've read lots of previous posts on converters but they all seem to have been fixed. I think this might be an unusual case though.

       

      The string in my backing bean is always of a fixed format required by the underlying service.

      The string I want to display to the user is based on a format in a properties file so it can be changed per locale. This is the dateFormat I pass to the calendar.

      My converter does nothing more than convert between the two formats. I've debugged and this all works fine.

       

      When I submit the date it works ok but when I come back in to the page the calendar attempts to get the current date.

       

      HtmlCalendar getCurrentDate returns the unconverted form property value i.e. not the user's format.

      UICalendar then calls getAsDate(currentDate) which detects that the value is a String and not a Date and attempts to convert it to a Date:

       

      if (date instanceof String) {
           DateTimeConverter converter = new DateTimeConverter();
           converter.setPattern(this.getDatePattern());

      ...

      this results in an exception because it's using the date pattern for the calendar which is different to the unconverted format of the current date.

       

      I can't immediately see how I can work around this, any suggestions gratefully received.

       

       

      I'll include my code in case I've got something wrongly configured. I'm using rich faces 3.3.3 Final.

       

      In my facelet I have the following:

       

      <rich:calendar value="#{control.value}"
                      popup="true"
                      enableManualInput="true"
                      datePattern="#{resourceBundle.dateFormat}"
                      converter="dateStringConverter"
                      cellWidth="24px" cellHeight="22px" style="width:200px">
                     <f:attribute name="userFormat" value="#{resourceBundle.dateFormat}" />
      </rich:calendar>

       

      My proof of concept (will be improved) converter has:

      public class DateStringConverter extends DateTimeConverter
      {
      public Object getAsObject(FacesContext context, UIComponent component, String value)
      {
        // Need to convert the user's format for their locale into format required by service layer
        String userDateFormat = (String) component.getAttributes().get("userFormat");
       
        if(value != null && value.length() > 0)
        {
         super.setPattern(userDateFormat);
         Date date = (Date)super.getAsObject(context, component, value);
         super.setPattern(SERVICE_LAYER_DATE_FORMAT);
         return super.getAsString(context, component, date);
        }
        else
        {
         return "";
        }
      }

      public String getAsString(FacesContext context, UIComponent component, Object value)
      {
        // Need to convert the service layer format into the user's format for their locale
        String valueStr = (String)value;
       
        String userDateFormat = (String) component.getAttributes().get("userFormat");
       
        if(value != null && valueStr.length() > 0)
        {
         super.setPattern(SERVICE_LAYER_DATE_FORMAT);
         Date date = (Date)super.getAsObject(context, component, valueStr);
         super.setPattern(userDateFormat);
         valueStr = super.getAsString(context, component, date);
        }
        else
        {
         valueStr = "";
        }
      }

       

      thanks in advance

      Nicola