3 Replies Latest reply on Oct 28, 2005 6:09 AM by Gavin King

    Contexts question

    drapierwimATgmailDOTcom Newbie

      I'm having some problems with the different contexts that are not clear to me.

      First my EntityBean is scoped with 'session' an SFSB scoped also 'session' this SFSB has a method that returns a collection of my entitybeans.

      When I write my test I can't find my object in the session context, but instead it's found in the conversation context. This I do not understand.

      Below is the code in case...




      @Entity
      @Name("journal")
      @Scope(ScopeType.SESSION)
      @Inheritance(discriminatorValue="J")
      public class JournalAccount extends DetailAccount {
      
       private Collection<Transaction> transactions;
      
       public JournalAccount() { }
      
       /**
       * @return Returns the posts.
       */
       @OneToMany
       public Collection<Transaction> getTransactions() {
       return transactions;
       }
      
       /**
       * @param posts The posts to set.
       */
       public void setTransactions(Collection<Transaction> posts) {
       this.transactions = posts;
       }
      }
      


      @Stateful
      @Name("accountingService")
      @Scope(ScopeType.SESSION)
      @Interceptor(SeamInterceptor.class)
      @LoggedIn
      public class AccountingServiceBean implements AccountingService {
      
       private static final Logger logger = Logger.getLogger(AccountingServiceBean.class);
      
       @PersistenceContext(type=PersistenceContextType.EXTENDED)
       private EntityManager em;
      
       @DataModel
       private List<JournalAccount> journals;
       @DataModelSelectionIndex
       private int journalIndex;
       @In(required=false)
       @Out(required=false)
       private JournalAccount journal;
      
      
       public String getJournals() {
       journal = null;
       journals = em.createQuery("from JournalAccount")
       .getResultList();
       logger.info(journals.size() + " Journals found");
       return "journals";
       }
      
       public String selectJournal() {
       if(journals == null) return "main";
       setJournal();
       return "selected";
       }
      
       @Destroy @Remove
       public void destroy() {
       logger.info("destroyed");
       }
      
       private void setJournal() {
       journal = journals.get(journalIndex);
       logger.info("Selected journal has tableindex " + journalIndex +
       " -> " + journal.getNumber() +
       "," + journal.getDescription());
       }
      
      }


      String id = new Script() {
      
       AccountingService accountingService;
      
       @Override
       protected void applyRequestValues() throws Exception {
       Contexts.getSessionContext().set("loggedIn", true);
       Contexts.getSessionContext().set("user", new User("Gavin King", "foobar", "gavin"));
       }
      
       @Override
       protected void invokeApplication() throws Exception {
       accountingService = (AccountingService) Component.getInstance("accountingService", true);
       String outcome = accountingService.getJournals();
       assert "journals".equals(outcome);
       }
      
       @Override
       protected void renderResponse() throws Exception {
       DataModel journals = (DataModel) Contexts.getSessionContext().get("journals");
       assert journals.getRowCount() == 4;
       assert ((JournalAccount) journals.getRowData()).getNumber() == 700000;
       assert Manager.instance().isLongRunningConversation();
       }
      
       }.run();