Wednesday, July 16, 2008

How to create an Editable data Table Using Seam2.0

Hi,
here i would like to introduce you the implementation of the Updatable DataTable(An HTML table in Client Side) on change in any of this table field will automatically persist in the Database.
For more details on Seam2.0 Look at
  1. Seam Tutorial
  2. Gavin King Blog
The following is the table we are going to Implement through Seam2.0 Framework.
The
database Table is

The Table person POJO.
===================================================================

package org.domain.person.model;

// default package
// Generated Jun 26, 2008 4:04:45 PM by Hibernate Tools 3.2.0.CR1

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.jboss.seam.annotations.Name;

/**
* Person generated by hbm2java
*/
@Entity
@Table(name = "Person", schema = "dbo", catalog = "TEST")
@Name("person")
public class Person implements java.io.Serializable {

    private int id;
    private String firstname;
    private String lastname;
    private String sname;

    public Person() {
    }

    public Person(int id) {
       this.id = id;
    }

    public Person(int id, String firstname, String lastname, String sname) {
       this.id = id;
       this.firstname = firstname;
       this.lastname = lastname;
       this.sname = sname;
    }

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
    public int getId() {
       return this.id;
    }

    public void setId(int id) {
       this.id = id;
    } 

    @Column(name = "firstname", length = 50)
    public String getFirstname() {
       return this.firstname;
    }

    public void setFirstname(String firstname) {
       this.firstname = firstname;
    }

    @Column(name = "lastname", length = 50)
    public String getLastname() {
       return this.lastname;
    }

    public void setLastname(String lastname) {
       this.lastname = lastname;
    }

    @Column(name = "sname", length = 50)
    public String getSname() {
       return this.sname;
    }

    public void setSname(String sname) {
       this.sname = sname;
    }

}


The Session Bean Handling the Client Table Actions.

package org.domain.person.delegate;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.domain.person.model.Person;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Synchronized;
import org.jboss.seam.log.Log;

@Stateful
@Scope(ScopeType.SESSION)
@Synchronized(timeout=Long.MAX_VALUE)
@Name("testTable")
public class TestTable implements TestTableInt{
     @Logger Log log;
     @PersistenceContext
     private EntityManager em;

     private static List personList = null;

     @In(create=true) @Out
     private Person person;
     private String myName;

     @Remove
     @Destroy
     public void destroy() {
     }

     public void newPerson(){
         log.info("New person Loading");
     }

     public void save(){
         if(person.getId()==0){
             em.persist(person);
         }else{
             em.merge(person);
         }
     }

     public List getPersonList() {
         String sql="SELECT T FROM Person T ";
         personList=em.createQuery(sql).setMaxResults(50).getResultList();
     return personList;
     }
}

The InterFace

import java.util.List;
import javax.faces.model.SelectItem;

import org.domain.EMSeam2.model.Person;

public interface TestTableInt {
    public void destroy() ;
    public void save();
    public List getPersonList();
    public void newPerson();
}

Test Table XHTML File :




The XHTML File:




I didnt found any other option to give the file here, you can get it by mailing to askeralim@gmail.com


2 comments:

Unknown said...

Hi, I`m from china, I`m sorry, I am no speak english, but I learning jsf richfaces freamwork. I do`t know use of example h:inplaceinput lable!, you website up painted, i do`t understand. can you give me a little about richfaces example? thank you!
my mailbox : arke56@163.com

ikaaki said...

Thanks for your email, this article is really helpful.