6 Replies Latest reply on Oct 31, 2006 3:59 AM by christian.andersson

    Multiple select items in page scope with factories.

    christian.andersson

      Hi all,

      I have an issue with multiple @SelectItems and factories.

      Environment

      jboss-4.0.4.GA
      Seam 1.1.0.beta1
      selectitems 1.0.1rc1


      In my SFSB I have these @SelectItems

      @SelectItems(value = "selectProjectManagers", scope = ScopeType.PAGE, label = "name", strategy = SelectItems.Strategy.INDEX)
       private List<User> projectManagers;
      
      @SelectItems(value = "selectCustomers", scope = ScopeType.PAGE, label = "name", strategy = SelectItems.Strategy.INDEX)
       private List<Customer> customers;
      
      @SelectItems(value = "selectBusinessAreas", scope = ScopeType.PAGE, label = "name", strategy = SelectItems.Strategy.INDEX)
       private List<BusinessArea> businessAreas;


      I have three factories for these

       @Factory("selectProjectManagers")
       public void getProjectManagers()
       {
       projectManagers = userManager.getUsersInRoles(new String[] { SystemRole.PROJECT_ADMINISTRATOR });
       }
      
       @Factory("selectBusinessAreas")
       public void getBusinessAreas()
       {
       businessAreas = systemManagerBean.getBusinessAreas();
       }
      
       @Factory("selectCustomers")
       public void getCustomers()
       {
       customers = customerManager.getCustomers();
       }


      and finally a page that displays these select items on a page as drop downs.


       <h:outputLabel for="customer">Project Manager:</h:outputLabel>
       <h:selectOneMenu value="#{projectWebManager.projectManager}">
       <f:selectItems value="#{selectProjectManagers}"/>
       </h:selectOneMenu>
      
       <h:outputLabel for="customer">Customer:</h:outputLabel>
       <h:selectOneMenu value="#{projectWebManager.customer}">
       <f:selectItems value="#{selectCustomers}"/>
       </h:selectOneMenu>
      
       <h:outputLabel for="customer">Business Area:</h:outputLabel>
       <h:selectOneMenu value="#{projectWebManager.businessArea}">
       <f:selectItems value="#{selectBusinessAreas}"/>
       </h:selectOneMenu>


      When I debug this I can see that only the factory associated with the first of the three dropdowns (selectProjectManagers) is called. If I put the selectCustomers dropdown before the selectProjectManagers dropdown the selectCustomers factory is called.





        • 1. Re: Multiple select items in page scope with factories.
          christian.andersson

           

          "christian.andersson" wrote:
          Hi all,

          I have an issue with multiple @SelectItems and factories.

          Environment

          jboss-4.0.4.GA
          Seam 1.1.0.beta1
          selectitems 1.0.1rc1


          In my SFSB I have these @SelectItems

          @SelectItems(value = "selectProjectManagers", scope = ScopeType.PAGE, label = "name", strategy = SelectItems.Strategy.INDEX)
           private List<User> projectManagers;
          
          @SelectItems(value = "selectCustomers", scope = ScopeType.PAGE, label = "name", strategy = SelectItems.Strategy.INDEX)
           private List<Customer> customers;
          
          @SelectItems(value = "selectBusinessAreas", scope = ScopeType.PAGE, label = "name", strategy = SelectItems.Strategy.INDEX)
           private List<BusinessArea> businessAreas;


          I have three factories for these

           @Factory("selectProjectManagers")
           public void getProjectManagers()
           {
           projectManagers = userManager.getUsersInRoles(new String[] { SystemRole.PROJECT_ADMINISTRATOR });
           }
          
           @Factory("selectBusinessAreas")
           public void getBusinessAreas()
           {
           businessAreas = systemManagerBean.getBusinessAreas();
           }
          
           @Factory("selectCustomers")
           public void getCustomers()
           {
           customers = customerManager.getCustomers();
           }


          and finally a page that displays these select items on a page as drop downs.


           <h:outputLabel for="customer">Project Manager:</h:outputLabel>
           <h:selectOneMenu value="#{projectWebManager.projectManager}">
           <f:selectItems value="#{selectProjectManagers}"/>
           </h:selectOneMenu>
          
           <h:outputLabel for="customer">Customer:</h:outputLabel>
           <h:selectOneMenu value="#{projectWebManager.customer}">
           <f:selectItems value="#{selectCustomers}"/>
           </h:selectOneMenu>
          
           <h:outputLabel for="customer">Business Area:</h:outputLabel>
           <h:selectOneMenu value="#{projectWebManager.businessArea}">
           <f:selectItems value="#{selectBusinessAreas}"/>
           </h:selectOneMenu>


          When I debug this I can see that only the factory associated with the first of the three dropdowns (selectProjectManagers) is called. If I put the selectCustomers dropdown before the selectProjectManagers dropdown the selectCustomers factory is called.


          Note: The first call to the page loads all three, it is on the second call to the page that only the first factory gets called.


          • 2. Re: Multiple select items in page scope with factories.
            pmuir

            What's the scope of your SFSB? It's likely you are having issues with scope of the components. Are the java variables null (for the variables annotated @SelectItems) on the second page refresh?

            In general, if you want something to always reload when you refresh a page I would suggest outjecting an event scoped component from an an event scoped bean (i.e. use a SLSB). If you the values don't change often you could always cache them.

            • 3. Re: Multiple select items in page scope with factories.
              christian.andersson

              The scope of the SFSB is SESSION. I have PAGE scope on my @SelectItems because it is possible that other users actually adds Project managers, business areas and customers that I should be able to select in the drop downs and I want my select items to be updated on page referesh.

              I thought the PAGE scope was a good thing since then I can use the INDEX type on @SelectItems to pass the index and be sure it is pointing to the correct index of the "original list" before it is updated.
              And after the selects are called my Lists are updated because of the PAGE scope. Maybe I'm wrong and should consider something else?

              The variables are not null on the page refresh, they are just not reloaded.
              (Or to be correct only the first is reloaded)

              • 4. Re: Multiple select items in page scope with factories.
                christian.andersson

                And thanks for helping me :-)

                • 5. Re: Multiple select items in page scope with factories.
                  pmuir

                  I'm not entirely sure whats going on here but I suspect something along these lines:

                  Request 1
                  All of the context variables/bean variables are empty so the Factory method is called for each.

                  Request 2
                  All of the context variables are empty, all of the bean variables are initialised (to the values from Request 1) as the bean is in the session scope. As the first context variable is null the Factory method is called for it and the bean variable updated. Bean values which are not null are outjected from the bean - thus all three context variables are now not empty. With the first one updated and the second two not.

                  I think it would be fair to say its a 'non-standard' pattern to outject into a shorted-lived scope than the scope of the outjecting bean.

                  I don't use the index strategy so can't really discuss patterns for it. If you want to use with PAGE scope (which is the best way to use I suppose) you need to use a SLSB to control it. Otherwise I would suggest using the STRING strategy with an unchanging unique identifier on the outjected object or the OBJECT strategy and use either the provided converters (for EJB3 entities, enums) or write your own.

                  • 6. Re: Multiple select items in page scope with factories.
                    christian.andersson

                    Ok thanks, I see what you mean.

                    I'll try to use a SLSB and see how that goes.

                    Thanks for your help.