9 Replies Latest reply on Jan 30, 2008 7:13 AM by archana_mannepalli

    getAsObject not being called - why?

      here is the converter:

      @Name("tst")
      @Scope(ScopeType.APPLICATION)
      @org.jboss.seam.annotations.faces.Converter(id="tst")
      public class Tst implements Converter {
      
       public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
       int i = 0;
       return (arg2 == null || arg2.equals("") || arg2.equals("0") || arg2.equals("false") ? new BigDecimal(0) : new BigDecimal(1));
       }
      
       public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
       if (arg2 instanceof Boolean)
       {
       return (arg2 == null || ((Boolean)arg2).equals(false) ? "N" : "Y");
       }
       else
       return (arg2 == null || ((BigDecimal)arg2).intValue() == 0 ? "false" : "true");
       }
      
      }


      Here is the xhtml:
      <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:s="http://jboss.com/products/seam/taglib"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:a4j="http://richfaces.org/a4j"
       xmlns:rich="http://richfaces.ajax4jsf.org/rich">
      
      <link
       href="stylesheet/fraud.css"
       rel="stylesheet"
       type="text/css" />
      <h:messages
       globalOnly="true"
       styleClass="message"
       id="globalMessages" />
      <h:form id="caseDetailForm">
       <br />
      .
      .
      <td class="data">
       <h:selectBooleanCheckbox
       id="hotTask"
       value="#{selectedCase.basedonhottask}"
       converter="tst">
       </h:selectBooleanCheckbox>
       </td>
      

      In my backing bean, the field basedonhottask is a BigDecimal and my converter should convert it to and from Boolean to BigDecimal.

      The proble is that upon render, it calls getAsString to convert from BigDecmal to the strings "false" or "true" depending on whether BigDecimal is 0 or 1 - and that seems to work fine - the checkbox is populated correctly. BUT, after changing the checkbox, on submit, it NEVER calls getAsObject - it calls getAsString again - dont know why??? Shouldnt it calls getAsObject to convert the BooleanCheckbos back into a BigDecimal?
      If this is a bug, is there a workaround?

        • 1. Re: getAsObject not being called - why?
          pmuir

          Normally this sequence of events means you have some other problem prior to it converting it back to an Object.

          • 2. Re: getAsObject not being called - why?

            Thanks you for the reply Pete - How would I debug this? The scenario is that on render of my xhtml, it renders correctly - the basedonhottask is checked or not checked depending on whether basedonhottask is 0 or 1. On submit of the form after changing the checkbos for basedonhottask, it goes right back into getAsString - it does not call the backing bean getter or setter first, as I have set breakpoints on those as well as the converter functions. After exiting getAsString, I get

            sourceId=caseDetailForm:hotTask[severity=(ERROR 2), summary=(/caseDetail.xhtml @95,41 value="#{selectedCase.basedonhottask}": java.lang.IllegalArgumentException: argument type mismatch), detail=(/caseDetail.xhtml @95,41 value="#{selectedCase.basedonhottask}": java.lang.IllegalArgumentException: argument type mismatch)]
            

            How can I determine the issue? How can I hook into the apply request values phase to determine why the getAsObject is not being called?

            • 4. Re: getAsObject not being called - why?
              archana_mannepalli

              Hi to all,
              i also needed the help regarding how to convert boolean to integer using custom converters,
              i tried to convert but am facing some problems,
              for anyone is it working fine?
              if it working plese send me the code.

              • 5. Re: getAsObject not being called - why?
                pmuir

                Ask on a JSF forum, this is not Seam.

                • 6. Re: getAsObject not being called - why?
                  archana_mannepalli

                  Hi

                  Am working on seam only...

                  for me also getAsObject() method is not invoking.....


                  • 7. Re: getAsObject not being called - why?
                    nickarls

                    As already pointed out in the sun forums, I don't think the converter is supposed to work in that setting.

                    I had a similar issue when working with a legacy database and ended up writing a custom type for the database field so that it would be converted there...

                    • 8. Re: getAsObject not being called - why?
                      archana_mannepalli

                      hi this is my xtml code:

                      <ui:define name="label">Active</ui:define>
                      <h:selectBooleanCheckbox id="active" value="#{roleHome.instance.active}"
                      converter="tst">
                      </h:selectBooleanCheckbox>

                      in the back end "active" field is an integer.

                      for this am writing custom converter.

                      • 9. Re: getAsObject not being called - why?
                        archana_mannepalli

                        but am getting this exception:

                        javax.faces.convert.ConverterException: java.lang.NullPointerException

                        for anyone if its working plese send me the code:
                        and my class is:
                        package org.domain.Portal.entity;

                        import javax.faces.component.UIComponent;
                        import javax.faces.context.FacesContext;
                        import javax.faces.convert.ConverterException;
                        import org.jboss.seam.ScopeType;
                        import org.jboss.seam.annotations.Logger;
                        import org.jboss.seam.annotations.Name;
                        import org.jboss.seam.annotations.Scope;
                        import org.jboss.seam.annotations.faces.Converter;
                        import org.jboss.seam.annotations.intercept.BypassInterceptors;
                        import org.jboss.seam.log.Log;


                        @Name("tst")
                        @Scope(ScopeType.APPLICATION)
                        @BypassInterceptors
                        @Converter
                        public class Tst implements javax.faces.convert.Converter {

                        @Logger private Log log;

                        public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException {
                        try {
                        arg2 = "true";
                        log.warn("MyConverter.getAsObject action called");
                        return (arg2 == null || arg2.equals("") || arg2.equals("0") || arg2.equals("false") ? new Integer(0) : new Integer(1));
                        } catch (Exception e) {
                        throw new ConverterException(e);
                        }
                        }

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

                        log.warn("MyConverter.getAsString action called"+arg2);
                        if (arg2 instanceof Boolean)
                        {
                        return (arg2 == null || ((Boolean)arg2).equals(false) ? "N" : "Y");
                        }
                        else
                        return (arg2 == null || ((Integer)arg2).intValue() == 0 ? "false" : "true");
                        } catch (Exception e) {
                        throw new ConverterException(e);
                        }


                        }



                        }