2 Replies Latest reply on Jun 16, 2008 12:08 PM by Reinhard Graf

    Problem with @DataModel

    Reinhard Graf Newbie

      I'm writing my first Seam-Application for managing customers. After login all customers are displayed in a list. Therefore I use rich:dataTable and the @DataModel annotation in a Stateless SessionBean.
      With the right mouse button I can open a rich:modalPanel for editing customer data. If I save the changes of one customer and want to open the next customer, the DataModel contains only null values.


      I'm using Seam 2.0.0GA, JBoss 4.2.2GA, Hibernate 3.2.6GA
      JDK1.5.09 and richfaces.


      Here is the code of my Bean:


      @Stateful
      
      @Name("adminClientBean")
      
      //@Scope(ScopeType.CONVERSATION)
      
      @TransactionManagement(TransactionManagementType.CONTAINER)
      
      public class AdminClientBean implements IAdminClient, Serializable {
      
              
      
              @In(create=true)
      
              private Session dokupoolDatabase;
      
              
      
              @DataModel("adminClientCustomers")
      
              private List<Customer> lstCustomers = null;
      
              
      
              @DataModelSelection("adminClientCustomers")
      
              @In(required=false)
      
              @Out(required=false, scope=ScopeType.CONVERSATION)
      
              private Customer currentCustomer;
      
              
      
              @Out (required=false)
      
              private String currentDialog = null;
      
              
      
              private Long noteIndex = null;
      
              
      
              @In (required=false)
      
              @Out (required=false)
      
              private Note currentNote = new Note();
      
              
      
              @In (required=false)
      
              @Out (required=false)
      
              private Member projectAdmin = null;
      
              
      
              
      
              @In (required=false)
      
              @Out (required=false)
      
              private Project newProject = null;
      
              
      
              @In
      
              private Events events;
      
              
      
              @Create
      
              public void init() {
      
                      System.err.println("AdminClientBean created!");
      
              }
      
              
      
              @Factory("adminClientCustomers")
      
              @Begin(flushMode=FlushModeType.MANUAL)
      
              public void prepareCustomers() {
      
                      System.err.println("prepareCustomers");
      
                      lstCustomers = CustomerDAO.getAllCustomers(dokupoolDatabase);
      
              }
      
      
              public String deleteCustomerAction() {
      
                      // TODO Auto-generated method stub
      
                      return null;
      
              }
      
              
      
              @Begin(nested=true,flushMode=FlushModeType.MANUAL)
      
              public String newCustomerAction() {
      
                      // Initialisieren der Objekte für die Anzeige
      
                      this.currentCustomer = new Customer();
      
                      this.currentCustomer.setNotes(new ArrayList<Note>());
      
                      this.currentNote = new Note();
      
                      this.newProject = new Project();
      
                      this.projectAdmin = new Member();
      
                      this.projectAdmin.setPerson(new Person());
      
                      this.projectAdmin.setAddress(new Address());
      
                      this.currentDialog = "/admin/edit_customer.xhtml";
      
                      return "success";
      
              }
      
      
              @Begin(nested=true, flushMode=FlushModeType.MANUAL)
      
              public String editCustomerAction(Long index) {
      
                      // Initialisieren der Objekte für die Anzeige
      
                      this.currentCustomer = lstCustomers.get(index.intValue());
      
                      this.currentDialog = "/admin/edit_customer.xhtml";
      
                      this.currentNote = new Note();
      
                      this.projectAdmin = MemberDAO.getProjectAdminForCustomer(dokupoolDatabase, this.currentCustomer);
      
                      return "success";
      
              }
      
      
              public String freeCustomerAction() {
      
                      // TODO Auto-generated method stub
      
                      return null;
      
      
              }
      
      
              public String lockCustomerAction() {
      
                      // TODO Auto-generated method stub
      
                      return null;
      
              }
      
              
      
              @End
      
              @Transactional(TransactionPropagationType.REQUIRED)
      
              public String saveCustomerAction() {
      
                      // Bei einer Neuanlage muss ein neues Projekt mit entsprechendem 
      
                      // Projekt-Administrator erstellt werden.
      
                      if (this.currentCustomer.getCustId() == null) {
      
                              saveNewCustomerData();
      
                              prepareCustomers();
      
                      } else {
      
                              dokupoolDatabase.persist(this.projectAdmin);
      
                              dokupoolDatabase.persist(this.currentCustomer);
      
                      }
      
                      dokupoolDatabase.flush();
      
                      cancelCustomerAction();
      
                      return "success";
      
              }       
      
              
      
              @End
      
              public String cancelCustomerAction() {
      
                      FacesMessages.instance().clear();
      
                      this.currentCustomer = null;
      
                      this.currentNote = null;
      
                      this.currentDialog = null;
      
                      return "success";
      
              }
      
              
      
              public String editNoteAction() {        
      
                      this.currentNote = this.currentCustomer.getNotes().get(this.noteIndex.intValue());
      
                      return "success";
      
              }
      
              
      
              public String deleteNoteAction() {
      
                      Note note = this.currentCustomer.getNotes().get(this.noteIndex.intValue());
      
                      this.currentCustomer.getNotes().remove(this.noteIndex.intValue());
      
                      dokupoolDatabase.delete(note);
      
                      return "success";
      
              }
      
              
      
              public String saveNoteAction() {
      
                      System.err.println("Save Note: " + this.currentNote.getNoteText());
      
                      // Bei einer neuen Notiz wird das Objekt der Collection hinzugefügt
      
                      if (this.currentNote.getNoteId() == null) {
      
                              this.currentNote.setNoteDate(new Date());
      
                              this.currentNote.setcustomer(this.currentCustomer);
      
                              this.currentCustomer.getNotes().add(this.currentNote);
      
                      }
      
                      cancelNoteAction();
      
                      return "success";
      
              }
      
              
      
              public String cancelNoteAction() {
      
                      this.currentNote = new Note();
      
                      return "success";
      
              }
      
              
      
              public void setNoteIndex(Long index) {
      
                      this.noteIndex = index;
      
              }
      
              
      
              public String customerSelectedAction() {
      
                      System.err.println("Customer: " + this.currentCustomer.getperson().getPersFirm() +" selected! ");
      
                      return "success";
      
              }
      
              
      
              @End
      
              public String logoutAction() {
      
                      System.err.println("logoutAction");
      
                      return "success";
      
              }
      
              
      
      
              @Destroy
      
              @Remove
      
              public void destroy() {
      
                      System.err.println("AdminClientBean destroyed!");
      
              }
      
              
      
              
      
              private void saveNewCustomerData() {
      
                      Date now = new Date();
      
                      
      
                      Address address = projectAdmin.getAddress();
      
                      dokupoolDatabase.persist(address);
      
                      Person person = projectAdmin.getPerson();
      
                      person.setPersEMail(projectAdmin.getMmbrUserName());
      
                      dokupoolDatabase.persist(person);
      
                      
      
                      this.currentCustomer.setCustDateCreate(now);
      
                      this.currentCustomer.setAddress(address);
      
                      this.currentCustomer.setperson(person);
      
                      this.currentCustomer.setCustNr("K"+now.getTime());
      
                      dokupoolDatabase.persist(this.currentCustomer);
      
                      
      
                      currentNote.setcustomer(this.currentCustomer);
      
                      currentNote.setNoteDate(now);
      
                      dokupoolDatabase.persist(currentNote);
      
                      
      
                      projectAdmin.setMmbrDateCreate(now);
      
                      projectAdmin.setAddress(address);
      
                      projectAdmin.setMmbrIsLoggedin(Boolean.FALSE);
      
                      
      
                      dokupoolDatabase.persist(projectAdmin);
      
                      
      
                      newProject.setProjDateCreate(now);
      
                      newProject.setcustomer(this.currentCustomer);
      
                      newProject.setProjMaxMembers(10);
      
                      newProject.setProjMaxStorage(new Long(10));
      
                      dokupoolDatabase.persist(newProject);
      
                      
      
                      MemberPropertyId memberPropertyId = new MemberPropertyId();
      
                      memberPropertyId.setMmbrId(projectAdmin.getMmbrId());
      
                      memberPropertyId.setProjId(newProject.getProjId());
      
                      
      
                      MemberProperty memberProperty = new MemberProperty();
      
                      memberProperty.setId(memberPropertyId);
      
                      memberProperty.setProject(this.newProject);
      
                      memberProperty.setMbprStatus(MemberProperty.STATE_ACTIVE);
      
                      memberProperty.setMbprLevel(MemberProperty.LEVEL_PROJECTADMIN);
      
                      memberProperty.setMbprEmailNotification(MemberProperty.MAIL_NOTIFICATION_IMMEDIATELY);
      
                      memberProperty.setMember(projectAdmin);
      
                      memberProperty.setProject(newProject);
      
                      
      
                      dokupoolDatabase.persist(memberProperty);
      
                      
      
              } 
      
      
      }