JIRA RF-1471 - Incomplete fix? Custom Date types are not ful
kragoth Apr 1, 2008 2:21 AMOne of the more important updates for me was the support for custom Date types in the Calendar. I have written a converter for our custom date type.
Code for converter as follows:
package gekko.web.jsf.converter; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.ConverterException; import org.jboss.seam.annotations.Name; import org.richfaces.component.html.HtmlCalendar; import org.springframework.beans.factory.annotation.Autowired; import com.sun.faces.util.MessageFactory; import gekko.services.reference.DateService; import gekko.type.GekkoDate; import gekko.util.FormatUtils; import gekko.util.StringUtils; import gekko.web.services.SpringAutowirable; @Name("DateConverter") @org.jboss.seam.annotations.faces.Converter(id = "GekkoDateConverter", forClass = GekkoDate.class) @SpringAutowirable public class DateConverter implements Converter { @Autowired DateService dateSvc; /** * <p> * The default message identifier of the * {@link javax.faces.application.FacesMessage} to be created if the * conversion to <code>GekkoDate</code> fails. * </p> */ public static final String CONVERTER_ID = "gekko.web.converter.DateConverter"; /** * <p> * The message identifier of the * {@link javax.faces.application.FacesMessage} to be created if the * conversion to <code>GekkoDate</code> fails. The message format string * for this message may optionally include the following placeholders: * <ul> * <li><code>{0}</code> replaced by the unconverted value.</li> * <li><code>{1}</code> replaced by the accepted date format.</li> * <li><code>{2}</code> replaced by an example value.</li> * <li><code>{3}</code> replaced by a <code>String</code> whose value * is the label of the input component that produced this message.</li> * </ul> * </p> */ public static final String DATE_ID = DateConverter.class.getName() + ".DATE"; /** * <p> * The message identifier of the * {@link javax.faces.application.FacesMessage} to be created if the * conversion of the <code>GekkoDate</code> value to <code>String</code> * fails. The message format string for this message may optionally include * the following placeholders: * <ul> * <li><code>{0}</code> replaced by the unconverted value.</li> * <li><code>{1}</code> replaced by a <code>String</code> whose value * is the label of the input component that produced this message.</li> * </ul> * </p> */ public static final String STRING_ID = DateConverter.class.getName() + ".STRING"; @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { Object returnValue = null; if (StringUtils.isBlank(value)) { return null; } try { returnValue = FormatUtils.parseShortDate(value); } catch (IllegalArgumentException e) { throw new ConverterException(MessageFactory.getMessage(context, DATE_ID, value, getDateFormat(component), dateSvc .getCurrentDate(), MessageFactory.getLabel(context, component)), e); } return returnValue; } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { if (value == null) { return ""; } try { return FormatUtils.formatDate((GekkoDate) value, getDateFormat(component)); } catch (ConverterException e) { throw new ConverterException(MessageFactory.getMessage(context, STRING_ID, value, MessageFactory.getLabel(context, component)), e); } } private String getDateFormat(UIComponent component) { String format = FormatUtils.SHORT_DATE_FORMAT.toLowerCase(); if (component instanceof HtmlCalendar) { format = ((HtmlCalendar) component).getDatePattern(); } return format; } }
So as you can see I have registered this converter for my GekkoDate.class.
The problem is that in the converter on the getAsString is called. getAsObject is never called.
However! If i specifically specify the converter on the actual richfaces component like this:
<rich:calendar id="commencementDateInput" enableManualInput="true" value="#{AccountController.currentAccount.commencementDate}" converter="GekkoDateConverter" datePattern="#{FormatUtils.shortDateFormat}" />
Then it will work.
Why does the forClass converter not work for this component properly?