6 Replies Latest reply on Jun 20, 2008 8:57 AM by nimo22

    One @Factory ?

    nimo22

      Hi,


      I am writing a xhtml-Page having multiple selectOneMenus.
      my bean:



      How can I develop only ONE @Factory-Method in which all lists are generated and referenced by their list-Name?


      I have tried to do this..


      but this does NOT work:



      ...
      List<Person> aList;
      List<Product> bList;
      
      @Factory("init")
      public void initLists() {
      
      aList =  entityManager.createQuery("from Person a").getResultList();
      
      bList =  entityManager.createQuery("from Product b").getResultList();
      
       }
      ...
      



      my view:


      <!-- Select Person -->
      <s:label styleClass="label">Person: </s:label>
      <h:selectOneMenu value ="#{person.name}" id="name">
      <s:selectItems value="#{aList}" var="a" label="#{a.name}"/>
      </h:selectOneMenu>
      
      <!-- Select Product -->
      <s:label styleClass="label">Product: </s:label>
      <h:selectOneMenu value="#{product.desc}" id="desc">
      <f:selectItems value="#{bList}" var="b" label="#{b.desc}"/>
      </h:selectOneMenu>
      



      But this will work:



      ...
      
      public List<Person> initPersonList() {
      
      return entityManager.createQuery("from Person a").getResultList();
      }
      
      public List<Product> initProductList() {
      return entityManager.createQuery("from Product b").getResultList();
      }
      ...
      



      my view:


      <!-- Select Person -->
      <s:label styleClass="label">Person: </s:label>
      <h:selectOneMenu value ="#{person.name}" id="name">
      <s:selectItems value="#{initPersonList()}" var="a" label="#{a.name}"/>
      </h:selectOneMenu>
      
      <!-- Select Product -->
      <s:label styleClass="label">Product: </s:label>
      <h:selectOneMenu value="#{product.desc}" id="desc">
      <f:selectItems value="#{initProductList()}" var="b" label="#{b.desc}"/>
      </h:selectOneMenu>
      




      How can I develop only ONE @Factory-Method in which all lists are generated and referenced by their list-Name?


        • 1. Re: One @Factory ?
          wrzep

          Heya,


          This seems to be a bit weird requirement. Why not 2 factories? :)


          Anyway, here:



          nimo mayr wrote on Jun 18, 2008 13:20:


          ...
          List<Person> aList;
          List<Product> bList;
          
          @Factory("init")
          public void initLists() {
          
          aList =  entityManager.createQuery("from Person a").getResultList();
          
          bList =  entityManager.createQuery("from Product b").getResultList();
          
           }
          ...
          



          you have to provide a a name of the returned Seam Component in the @Factory attribute. So, it should be @Factory("aList"). You can't provide 2 names... The thing you can do is mark bList with @Out and have it outjected when the factory method for aList is triggered.


          Cheers


          -Pawel

          • 2. Re: One @Factory ?
            nimo22

            yes, indeed. It s really better to write 2 Factories!..(or one with an @Create-Annotation?) I guess, I take 2 Factories.


            thanks:-)

            • 3. Re: One @Factory ?
              endyamon

              what about using one @Create instead of many @Factories to initialize your component?

              • 4. Re: One @Factory ?
                jnusaira

                FWIW the @Factory is only going to do a lookup when you actually use the item.


                In your example you never call init inside the xhtml hence why it doesnt work.


                The @Create annotation according to the doco is really only for non Session Beans.


                I had kindof the same problem as you.


                What i did i had the SLSB with a @PostConstruct on it.


                Then the factories were tied to the retrieve methods of each item. Of course in my example the retrieves were necessary anyway since a non Seam EJB needed access to them.

                • 5. Re: One @Factory ?
                  nimo22

                  Okay, indeed, I use two @Factory-Methods and declared both with names:


                  ..
                  @Factory("initListA")
                  public void initListA(){..}
                  ..
                  @Factory("initListB")
                  public void initListB(){..}
                  ..
                  
                  



                  Now I can take an xhtml and referenced the factories in it. It works without problems:-)


                  But now, I will reach the same with an @Create-Annotation in a Stateful SB:



                  ...
                  List<SelectItem> selectListA = new ArrayList<SelectItem>();
                  List<SelectItem> selectListB = new ArrayList<SelectItem>();
                  
                  @Create @Begin
                  public void initSession() {
                  
                  selectListA = entityManager.createQuery("from TableA a").getResultList();
                  selectListB = entityManager.createQuery("from TableB b").getResultList();
                  
                  }
                  ...
                  



                  Normally, the reference in xhtml should be like:



                  <!-- Select List A -->
                  ..
                  <h:selectOneMenu value ="#{listA.row}" id="row">
                  <s:selectItems value="#{selectListA}" var="a" label="#{a.row}" id="selectTeam"/>
                  </h:selectOneMenu>
                  




                  But the reference 'selectListA' does not work!




                  Why? Have I use the @PostConstruct?







                  • 6. Re: One @Factory ?
                    nimo22

                    Oh, I forgot the GETTERS for the two Lists..now it works with the @Create-Annotation.


                    bye:-)