5 Replies Latest reply on Oct 3, 2009 9:29 PM by jk jkl

    Call a managed bean function from converter

    jk jkl Newbie

      Hello,
      I am trying to call a managed bean function with one argument in a converter. What I am trying to do is to get the selected value from a combobox and then search the entity that has that value (it is an identifier).
      I have unsuccessfully tried:
      1-Get the managed bean from the facesContext and then call the function
      This is not possible because SEAM changes the class type of the managed bean. If I cast the class into an interface it does not work
      2-Use the @In annotation. It is always null, even using create parameter


      The problem with converters is that the UI component value is not stored in any managed bean so I must use an argument in the function.


      I works without using converters and having the combobox value stored in a managed bean. The setter of the managed bean injects programatically all the dependencies. I do not want to use this solution because it does not seem elegant.

        • 1. Re: Call a managed bean function from converter
          jk jkl Newbie

          Now I use the @Converter annotation and when I modify one of the fields of the form where the value has to be converted appears, then the conversation crashes. It does not call any converter function, it seems to crash when trying to load the SEAM component
          I have the following annotations:
          @Name(tendaConverter)
          @Converter
          @BypassInterceptors


          I do not understand why there is no documentation (even in comercial books) to describe how to convert an entity identification field in an entity.
          Netbeans web generator creates converters for each entity getting the component from the facesContext. BUt, as I said in the previous post, it is not possible in SEAM because the framework changes the class name so it can not be casted.


          • 2. Re: Call a managed bean function from converter
            jk jkl Newbie

            Finally I have solved it after 3 hours of trial and error :S
            Here you have the code:



            package org.ajpollenca.compres.converter;
            
            import java.io.Serializable;
            ....
            import org.jboss.seam.annotations.intercept.BypassInterceptors;
            
            @Name("tendaConverter")
            @Scope(ScopeType.CONVERSATION)
            @Converter
            @BypassInterceptors
            public class TendaConverter implements javax.faces.convert.Converter,
                      Serializable {
            
                 @Transactional
                 public Object getAsObject(FacesContext facesContext, UIComponent arg1,
                           String value) {
                      TendaSearcherBean tendaSearcherBean = (TendaSearcherBean) Component
                                .getInstance("tendaSearcherBean");
                      return tendaSearcherBean.findByNom(value);
                 }
            
                 @Transactional
                 public String getAsString(FacesContext arg0, UIComponent arg1, Object object) {
                      if (object == null) {
                           return "";
                      } else {
                           return object.toString();
                      }
                 }
            
            }
            
            


            ... and ...




            package org.ajpollenca.compres.session;
            
            import javax.persistence.EntityManager;
            import javax.persistence.Query;
            import org.ajpollenca.compres.entity.Tenda;
            import org.jboss.seam.Component;
            import org.jboss.seam.annotations.Name;
            
            @Name("tendaSearcherBean")
            public class TendaSearcherBean {
                 
                 public Tenda findByNom(String nom) {
                      EntityManager entityManager;
                      entityManager = (EntityManager) Component.getInstance("entityManager");
                      Query query = entityManager.createQuery(
                                          "select tenda from org.ajpollenca.compres.entity.Tenda tenda where tenda.nom = :nom");
                      query.setParameter("nom", nom);
                      Tenda tenda = (Tenda) query.getSingleResult();
                      return tenda;
                 }
            
            }
            



            At least I have learnt many SEAM concepts


            Hope this helps

            • 3. Re: Call a managed bean function from converter
              jk jkl Newbie

              And even better:



              package org.ajpollenca.compres.session;
              
              import java.io.Serializable;
              
              import javax.persistence.EntityManager;
              import javax.persistence.Query;
              import org.ajpollenca.compres.entity.Tenda;
              import org.jboss.seam.annotations.In;
              import org.jboss.seam.annotations.Name;
              
              @Name("tendaSearcherBean")
              public class TendaSearcherBean implements TendaSearcher, Serializable {
                   
                   @In
                   EntityManager entityManager;
                   
                   public Tenda findByNom(String nom) {
                        Query query = entityManager.createQuery(
                                            "select tenda from org.ajpollenca.compres.entity.Tenda tenda where tenda.nom = :nom");
                        query.setParameter("nom", nom);
                        Tenda tenda = (Tenda) query.getSingleResult();
                        return tenda;
                   }
              
              }



              • 4. Re: Call a managed bean function from converter
                B Tomic Newbie
                I didn't entirely understand your usecase, but it seems to me you should check out <s:convertEntity />.
                • 5. Re: Call a managed bean function from converter
                  jk jkl Newbie

                  Hi,


                  convertEntity just transforms an entity instance into an identity object string. This is not useful for most cases because the entity id is usually an integer.


                  If I use the nom attribute as an identifier then I have no way in JPA to automatically generate the id attribute value. I can not use @GeneratedValue annotation because it must come along with @Id annotation.


                  It is even worse if I drop the id attribute from the table and make nom attribute the identifier because then I can not change the nom value due to its references.


                  Am I right??