Create a component after login
newlukai Apr 8, 2008 2:39 PMHi,
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