6 Replies Latest reply on May 26, 2006 3:20 PM by gavin.king

    Conceptual problem: Reusable lists?

    eekboom

      Basically I started by adopting the "clickable lists" (messages) example, just for me it was a list of Employee entities.

      Ok, now by user demand I added some filter options: internal/external, current/retired/future, specific branch or all branches.
      This was easy to do: I added some variables to the EmployeeManagerBean (the one that also holds the DataModel). The accessors of those variables are referenced by JSF selectOneBoolean/selectOneChoice components.
      The Factory method uses these variables to query for only a subset of all employees. Whenever a setter of any of this filter variables is called, I set the DataModel variable to null, so that seam calls the factory method again as needed.

      Now on to the trouble: I want this same kind of list on three or four pages in my application, but each list should have its own filter values.
      How do I reuse my list?

      I could define multiple instances of my EmployeeManagerBean, but that would still give me only one DataModel (because the datamodel introduces a seam variable of its own), right?

      Wouldn't it be more powerful and just as easy to use, if the DataModel annotation would define a property of the owning component, rather than a variable of its own?

      Or am I missing something and there's an easier way to do this?

        • 1. Re: Conceptual problem: Reusable lists?
          gavin.king

          You can always do it in the standard JSF way, and create a ListDataModel that you keep inside your component.

          • 2. Re: Conceptual problem: Reusable lists?
            pmuir

            Surely something like

            @Stateless
            @Name("employeeManager")
            public class EmployeeManagerBean implements EmployeeManager {
            
             @DataModel(scope=PAGE)
             private List<Employee> employees;
            
             @In(required=false)
             private Boolean internal;
            
             @In(required=false)
             private Branch status;
            
             ...
            
             @Factory("employees")
             public void employeeFactory() {
             employees = em.createQuery(...).getResultList();
             }
            
            }
            


            should work. The bean is stateless so the list will be rebuilt each time the employees variable is accessed per page but the datamodel is outjected to the PAGE scope so that clickable lists work. You shouldn't need to null the employees list manually here I think but I may be wrong as I haven't tried it yet myself.

            • 3. Re: Conceptual problem: Reusable lists?
              eekboom

              Thanks for the answers. I have to think a little more about wether page or maybe conversation scope would work in my case.

              Gavin, you're proposal makes sense. However it would save me some work if the code that wraps a collection in a ListDataModel would be made public.
              Why should I duplicate code that seam already contains?
              Ok to file a Jira issue for that?

              • 4. Re: Conceptual problem: Reusable lists?
                gavin.king

                huh? the code is: new ListDataModel(myList)

                • 5. Re: Conceptual problem: Reusable lists?
                  eekboom

                  *ashamed*
                  See, I only started with JSF for about a couple of hours when I realized that I am missing the funcionality that Seam offers.
                  Now you made the clickable list tutorial sound so nice and easy that I thought it would be much more complicated with pure JSF.
                  Should have read some more.
                  Now your original suggestion makes yet more sense - I see that the only code I need is a getter that lazily initializes a field with a single constructor call.
                  Sorry for my stupid question.

                  • 6. Re: Conceptual problem: Reusable lists?
                    gavin.king

                    No problem ;-)