3 Replies Latest reply on Apr 11, 2008 8:33 AM by Jens Weintraut

    Create a component after login

    Jens Weintraut Apprentice

      Hi,


      I want to create a stateful session component as soon as the user logs in.


      I tried this:


      @Stateful
      @Scope(ScopeType.SESSION)
      @Name("releaseSelector")
      public class ReleaseSelection implements ReleaseSelector, Serializable {
           private static final long serialVersionUID = -8533799770027760789L;
      
           @In(required=false) @Out(required=false, scope=ScopeType.SESSION)
           private Release selectedRelease;
      
           private Release getReleaseFromLUS() {
                return userService.getReleaseLastUsed(user);
           }
      
           @Create
           @Observer("org.jboss.seam.loginSuccessful")
           public void create() {
                if(selectedRelease == null) {
                     selectedRelease = getReleaseFromLUS();
                }
           }
      
           //skipped some methods and attributes
      }



      As you can see this releaseSelector is a manager for a component selectedRelease. But there is a problem with this approach: The @Observer annotation does what it is intended for (i. e. the selectedRelease is set to the last release the user chose), but the outjection doesn't work. The selectedRelease in another component is null.


      @Stateful
      @Scope(ScopeType.SESSION)
      @Name("testcaseSelector")
      @AutoCreate
      public class TestcaseSelection implements TestcaseSelector, Serializable {
           private static final long serialVersionUID = 8167228165757998662L;
      
           @In(required=false)
           private Release selectedRelease;
           
           @Create
           public void create() {
                getTestcases();
                if(testcases.size() > 0) {
                     selectedTestcase = testcases.get(0);
                }
           }
           
           public List<Testcase> getTestcases() {
                if(testcases == null || selectedReleaseChanged()) {
                     testcases = testcaseService.getTestcases(selectedRelease);
                     
                     lastSelectedReleaseID = (selectedRelease == null) ? null : selectedRelease.getID();
                }
                
                if(!selectedTestcaseIsInList() && testcases.size() > 0) {
                     selectedTestcase = testcases.get(0);
                }
                
                return testcases;
           }
           
           //skipped some methods and attributes
      }



      This testcaseSelector is created after the releaseSelector but the selectedRelease is null. I'm wondering why.


      Is there another elegant way to create a component after a user logged in? Adding @Startup to releaseSelector causes Seam to instantiate the releaseSelector as soon as a session is started, but at that time the user isn't logged in. The last idea I had was to create the component in the authenticate() method, but I don't know how to do this and I'm sure this is not the best way to do this.


      Thanks in advance
      Jens

        • 1. Re: Create a component after login
          Jens Weintraut Apprentice

          This problem can be explained with one question: Is it possible to create a manager component when its managed component is needed? If component C injects component B, which is managed by component A, I want Seam to create component A that outjects a value for component B. I hope you know what I mean?


          @Name("a")
          @Stateful
          @Scope(SESSION)
          public class A implements IA {
             @Out
             private B b;
          
             @Create
             public void create() {
                b = getBFromSomewhere();
                b.setWhatever();
             }
          }



          @Name("b")
          @Entity
          public class B {
             private int foo;
          
             //getters/setters
          }



          @Name("c")
          @Stateful
          @Scope(SESSION)
          public class C implements IC {
             @In
             private B b;
          
             public void create() {
                getInformationBasedOnB(b);
             }
          }



          Is it possible to tell Seam to create a as soon as b is needed in c?

          • 3. Re: Create a component after login
            Jens Weintraut Apprentice

            Yeah, OK. Sometimes it really is so easy ... I forgot that @Factory has attributes value and autoCreate. Nice annotation once you know where you can use it.