6 Replies Latest reply on Jun 21, 2006 5:25 AM by liudan2005

    Help understanding Entity component lifecycle

    rpiaggio

      Is there a way to tell Seam that we don't want it to instantiate Seam-managed variables automatically?

      Basically, what I have is the following:

      @Entity
      @Name("currentUser")
      @Scope(ScopeType.SESSION)
      @Roles({
       @Role(name="loginUser", scope=ScopeType.EVENT),
       @Role(name="newUser", scope=ScopeType.CONVERSATION)
       })
      public class User implements Serializable {...


      In other words, I am using the same "User" entity for the current logged user, for the login process, and for the create user process.

      I would like to know if a user is logged in by or not by setting the "currentUser" variable to the current user or to null. However, Seam always instantiates "currentUser".

      This seems to happen when the "login.xhtml" part of the view is rendered, since there I check for the currentUser (in order to display the login box or not)...

      <c:when test="#{currentUser == null}">


      This condition never happens, even if I set currentUser = null in my code and outject the variable.

      Can I tell Seam not to instantiate "currentUser" automatically?


        • 1. Re: Help understanding Entity component lifecycle
          gavin.king

          Just don't make it a Seam component.

          You can outject any object to a Seam context variable, not just Seam components.

          • 2. Re: Help understanding Entity component lifecycle
            rpiaggio

            The reason why I am making it a Seam component is to specify the scope in one signle place in the code.

            If I don't make it a Seam component, then I have to specify the scope in every outjection. This means that if I want to change the behavior it in the future then I will have to change n places in the code instead of just one.

            Is there a way to specify the default scope of a variable that's not a Seam component? (I can do it the JSF way in faces-config.xml I guess, would that work with Seam?)

            • 3. Re: Help understanding Entity component lifecycle
              rpiaggio

              Let me rephrase:

              Is there a way to specify a default Seam scope for variables that are not Seam components?

              If not, can we turn it into a feature request?

              • 4. Re: Help understanding Entity component lifecycle
                gavin.king

                I suppose I could let you write @Scope on a class with no @Name. Put it in JIRA and I'll make a decision about that later. I don't love it, but maybe I can grow to like it.

                • 5. Re: Help understanding Entity component lifecycle
                  rpiaggio

                  Thanks for considering it, Gavin.

                  However, I don't like it either because it doesn't fully solve the problem: in the example above, the same class would be used as a Seam component in some roles but not in others.

                  My suggestion: a "create" property in the @Role annotation (and in @Scope?). Defaults to "true", but we could specify ("create=false").

                  This would allow null values in Seam components. (I'm JIRAing this suggestion).

                  Maybe this conflicts heavily with what a "Seam-managed component" is supposed to be. Then the solution would be to create a new annotation specifying a role and default scope (again, the same class could have different roles even if it's not a Seam component in any of them). But wouldn't that just look too much like the @Role annotation?

                  (All these forum messages with "feature XX doesn't work" and "feature YY" requests may sound a bit harsh, so let me use this opportunity to express, for what it's worth, how great a job I think you guys are doing with Seam).

                  • 6. Re: Help understanding Entity component lifecycle
                    liudan2005

                    I've had similar problem and this is how I solved my problem. Don't know if it helps to solve your problem.

                    Basically I created another wrapper object UserService for User. When currentUser is requested, I manually check the sesssion and decides what to return. Here is some my code.

                    UserService.java

                    @Name("currentUser")
                    @Scope(SESSION)
                    public class UserService
                    {
                    
                     @Unwrap
                     public User getUser()
                     {
                     return getMyCurrentUser(...);
                     }
                    ...
                    }
                    


                    User.java
                    @Entity
                    @Name("dummyUser")
                    @Scope(SESSION)
                    @Roles(
                    { @Role(name = "loginUser", scope = ScopeType.EVENT),
                     @Role(name = "newUser", scope = ScopeType.CONVERSATION) })
                    public class User implements Serializable
                    {
                    ...
                    }