5 Replies Latest reply on Apr 8, 2008 12:56 AM by jimk1723

    Seam's Character to Boolean converter

    braddm7

      Hello,


      I have a JPA entity property that is of a primitive char(1) datatype, i.e. it returns a 'Y' or an 'N'. This field in the UI is represented as a boolean checkbox.


      I created a Seam's converter which for some reason invoke only the 'getAsString' method of the javax.faces.convert.Converter interface. Here is a part of the Facelet:
                                         
      <h:selectBooleanCheckbox value="#{someEntity.somePrimitiveChar}">
      <f:converter converterId="charToBooleanConverter" />          </h:selectBooleanCheckbox>



      The initial UI rendering is fine and in debug mode the checkbox is managed by the converter's 'getAsString' method. When the form is submitted it does not enter the 'getAsObject' method. Here is the converter itself.



      package com.my.domain.converters;
      
      import javax.faces.component.UIComponent;
      import javax.faces.context.FacesContext;
      import javax.faces.convert.ConverterException;
      
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.faces.Converter;
      import org.jboss.seam.annotations.intercept.BypassInterceptors;
      
      @Name("charToBooleanConverter")
      @BypassInterceptors
      @Converter
      public class CharToBooleanConverter implements javax.faces.convert.Converter {
      
           /** Default construction */
           public CharToBooleanConverter() {
           }
      
           public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
                if (value == null || value.trim().length() == 0) return new Character('N');
      
                Character toReturn = (value.equalsIgnoreCase("true")) ? 'Y' : 'N';
                return toReturn;
           }
      
           public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
                String toReturn = "false";
      
                if (value == null) {
                     return toReturn;
                }
      
                if (value instanceof Boolean) {
                     toReturn = (((Boolean) value) == true) ? "true" : "false";
                }
                else if (value instanceof Character) {
                     char toChar = ((Character) value).charValue();
                     toReturn = (toChar == 'Y' || toChar == 'y') ? "true" : "false";
                }
      
                return toReturn;
           }
      }




      I appreciate your help. Thanks.

        • 1. Re: Seam's Character to Boolean converter
          nickarls

          I think this is a JSF issue, search the Sun JSF forums, it is popular there. I have solved a similar situation with a custom hibernate type that maps the Y/N to boolean.


          (Now that I think of it, the Y/N has built-in hibernate support, I have different characters, so you might get away without writing any custom classes yourself)

          • 2. Re: Seam's Character to Boolean converter
            pmuir

            Agreed, a Hibernate UserType is the best way to resolve this.

            • 3. Re: Seam's Character to Boolean converter
              nickarls

              it was the YesNoType i recalled. It's a true=>Y, false=>N mapping.


              for Hibernate, that is.

              • 4. Re: Seam's Character to Boolean converter
                braddm7

                Thanks, you are right that is it s JSF issue. Here is what I got from Sun's forum if anyone is curious:
                Converters are for String-to-Object conversion, where Object is the model type, and String is the display type. For h:selectBooleanCheckbox
                to support Converters, there'd have to something like Boolean-to-Object conversion supported, which would be weird. Anyway, this definitely isn't a bug in the RI. It's arguably a problem with the specification. Then h:selectBooleanCheckbox
                tag should not support the converter attribute at all.

                • 5. Re: Seam's Character to Boolean converter
                  jimk1723

                  Yup, this works for me:


                      @Column(name = "IS_CHART", table = "EVENT")
                      @Type(type = "org.hibernate.type.YesNoType")
                      public Boolean isChart() {
                          return chart;
                      }