3 Replies Latest reply on Nov 22, 2007 7:17 PM by Gerardo Segura

    question about programmatically accessing seam component

    Gerardo Segura Newbie

      Hello,
      I'm in the need of programmatically accessing (I mean not using @In annotation) a component which is created with @Factory . But neither this:

      Course course = (Course)Contexts.lookupInStatefulContexts("course") ;
      

      nor this, is working:
      Course course=(Course)Contexts.getSessionContext().get("course") ;

      Both methods return null, as if the factory method is not been called. Is this correct behavior or could it be something in my code, I reproduce here the related parts.
      By the way I'm not able to inject the component because its factory method is declared inside a component with @Restrict("#{identity.loggedIn}") declared, but I'm trying to use it when no login has happened yet. But just before I plan to use the login ocurrs, so at that time the @Restrict should not be a problem. If I try to use @In the injection tries to occur to early.

      I think I better wrote the code:

      firstly the component with the declared factory
      @Name("courseManager")
      @Scope(ScopeType.SESSION)
      @Restrict("#{identity.loggedIn}")
      public class CourseManagerAction implements CourseManager {
      
       @Factory
       public Course getCourse() {
       // gets default single course
       Course course = (Course) entityManager
       .createQuery("select c from Course c").getSingleResult();
       return course;
       }
      }
      


      then the case where I want to use the "course" component:
      @Scope(ScopeType.EVENT)
      @Name("register")
      public class RegisterAction implements Register {
      
       public void register() {
       //here the user register itself ....
       //after that do auto-login
       identity.authenticate() ;
      
       //now I want to access the course
       Course course = (Course)Contexts.getSessionContext().get("course") ;
      
       //but course is null
       //why? shouldn't this call be similar to: @In Course course ??
       }
      }
      


      any comments?