3 Replies Latest reply on Jul 10, 2007 6:34 PM by pmuir

    Session-Variable in Conversation Context (or so...)

    dschaedl

      Hi

      I'm new to seam.
      I'd like to have a List of object on one page and a second page to edit/create these object.

      For now I have my List of objects - using @Datamodel and dataTable for display. Additionaly I have a List (@out, @in) to locally store the Objects in my Session.
      To edit an Object I pass it to the editClass, edit it and save it - as a new conversation (@Begin, @End). I use the same list of Objects as before here.
      I also use the same class to create new object and then add them to my list of object.
      All work fine so far (most of it...)

      My problem is the mixture of Session and Conversation-Context. The List of objects in the editClass is not the same as in the display-class. If I add a new Object to to object list in the conversation context it will not be displayed in my List-view because this uses the list from the Session context. (somehow clear what I'd like to say?)

      hm.... I'd still like to use the conversation for the edit. Can they be merged into the session again? can I access a Session-variable in the Conversation?
      How do I solve this the clean-Seam-way?

      greatful for any hint
      Daniel

        • 1. Re: Session-Variable in Conversation Context (or so...)
          pmuir

          Please, post code to show what you are doing/what. It sounds like a classic CRUD situation. Take a look at what seam-gen creates - it's a pretty nice way to do this.

          • 2. Re: Session-Variable in Conversation Context (or so...)
            dschaedl

            It's exactly CRUD what I'd like to do.
            I used Seam-gen to setup the project and to create my forms (originaly).
            here us my current code (slightly simplified)
            *** person.java ***
            @Entity
            @Name("person")
            public class person implements Serializable {
            private Long id;
            private String name;
            private String firstName;

            @Id
            @GeneratedValue
            public Long getId() {..}
            public void setId(Long id) {..}

            @Length(min=3, max=120)
            public String getName() {..}
            public void setName(String name) {..}

            public String getFirstName() {..}
            public void setFirstName(String firstName) {..}

            *** PersonManagerBean.java ***
            @Stateful
            @Scope(ScopeType.SESSION)
            @Name("personManager")
            public class PersonViewerBean implements PersonManager {

            @Logger
            private Log log;

            @In(required=false)
            @Out
            private List personList;

            @DataModel
            private List personsDataModel;

            @DataModelSelection
            @Out(required=false)
            private Person person;

            @PersistenceContext(type=PersistenceContextType.EXTENDED)
            private EntityManager em;

            @Factory("personsDataModel")
            public void findPersons() {
            personsDataModel = em.createQuery("from Person r order by r.name desc").getResultList();
            personList = personssDataModel;
            }

            @Remove @Destroy
            public void destroy(){}
            }

            *** EditPersonBean ***
            @Stateful
            @Name("editperson")
            public class EditpersonBean implements Editperson {

            @Logger
            private Log log;

            @PersistenceContext(type=PersistenceContextType.EXTENDED)
            private EntityManager em;

            @In
            FacesMessages facesMessages;

            @In(required=false)
            @Out
            private Person person;

            @In
            @Out
            private List personList;

            @Begin
            public void newPerson() {
            person = new person();
            }

            @End
            public void savePerson()
            {
            List existing = new ArrayList();
            existing = em.createQuery("select name from Person where name=#{person.name}").getResultList();
            if (existing == null || existing.size() == 0) {
            em.persist(person);
            personList.add(person);
            }
            else {
            facesMessages.add("a person with this name already exists! (name=#{person.name})");
            }
            }

            @End
            public void updateperson()
            {
            em.persist(person);
            }

            @Begin
            public void editperson(person r) {
            this.person = em.merge(r);
            }


            public person getperson() { ..}
            public void setperson(person person) {...}

            @Remove @Destroy
            public void destroy(){}
            }

            ****** view.xhtml ******
            ...
            <rich:panel>
            <f:facet name="header">Welcome!</f:facet>
            MY PERSON:
            <h:outputText value="nothing to display" rendered="#{personsDataModel.rowCount==0}" />
            <h:dataTable var="person" value="${personsDataModel}" rendered="#{personsDataModel.rowCount>0}">
            <h:column>
            <f:facet name="header">
            <h:outputText value="Name" />
            </f:facet>
            <h:commandLink value="#{person.name}" action="#{personManager.select}" /> <!-- action does nothing -->
            </h:column>
            <h:column>
            <f:facet name="header">
            <h:outputText value="Vorname" />
            </f:facet>
            <h:commandLink value="#{person.firstName}" action="#{personManager.select}" />
            </h:column>
            <h:column>
            <s:link id="editperson" value="edit" action="#{editperson.editPerson(person)}" />
            </h:column>
            </h:dataTable>
            <h:commandButton value="refresh" action="#{personManager.findPersons}" />
            <h:commandButton value="new entry" action="#{editperson.newPerson}" />
            <h3><h:outputText value="#{rower.email}" /></h3>
            </rich:Panel>
            ...


            ***** edit.xhtml ****
            ....
            <rich:panel>
            <f:facet name="header">editPerson</f:facet>
            <s:decorate id="firstnameDecoration" template="layout/edit.xhtml">
            <ui:define name="label">Vorname</ui:define>
            <h:inputText id="firstname" required="false" value="#{person.firstName}" />
            </s:decorate>
            <s:decorate id="nameDecoration" template="layout/edit.xhtml">
            <ui:define name="label">Name</ui:define>
            <h:inputText id="name" required="true" value="#{person.name}"/>
            </s:decorate>
            ...

            ***************************
            Logging, Comments, some other stuff was left out.

            when pressing the 'new' or 'edit' Button a new conversation starts and my List (personList) is copied into the conversation-context. At the end of the conversation I'd like to copy it back to the Session.personList.
            How? possible? did I missunderstand something?

            thanks
            Daniel

            • 3. Re: Session-Variable in Conversation Context (or so...)
              pmuir

               

              @Out(scope=SESSION) List personList;


              or, if you want to do it *just* at the end, you can do it programmatically

              Contexts.getSessionContext().set("personList", personList);


              N.B. I'm not sure why you are copying this to the session context, but you probably don't want to.