For those who want to quickly run a sample code to learn how GWT RequestFactory works, but do not want to be bothered by the configuration of Google App Engine and persistence layer, you can try the following procedure.
svn co http://gwt-examples.googlecode.com/svn/trunk/DemoRequestFactory DemoRequestFactory/DemoRequestFactory
package org.gonevertical.server.domain; import java.util.LinkedList; import java.util.List; import java.util.Map; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EntityManager; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Version; import org.gonevertical.server.EMF; import com.google.gwt.dev.util.collect.HashMap; @Entity public class NameData { // TEST-DEL-BEGIN /* public static final EntityManager entityManager() { return EMF.get().createEntityManager(); } */ // TEST-DEL-END // TEST-ADD-BEGIN private static Map<Long, NameData> store = new HashMap<Long, NameData>(); // TEST-ADD-END public static long count() { // TEST-MOD-FROM: /* EntityManager em = entityManager(); try { return ((Number) em.createQuery("select count(o) from " + NameData.class.getSimpleName() + " o") .getSingleResult()).longValue(); } finally { em.close(); } */ // TO: return (long)store.size(); // TEST-MOD-END } /** * It is mandatory to have a find[EntityClass] here!!! * * @param id * @return */ public static NameData findNameData(Long id) { if (id == null) { return null; } // TEST-MOD-FROM: /* EntityManager em = entityManager(); try { NameData employee = em.find(NameData.class, id); return employee; } finally { em.close(); } */ // TO: if( store.containsKey(id) ) { return store.get(id); } else { return null; } // TEST-MOD-END } public static List<NameData> query() { // TEST-MOD-FROM: /* EntityManager em = entityManager(); try { List<NameData> list = em.createQuery("select o from " + NameData.class.getSimpleName() + " o") .getResultList(); // force to get all the Names list.size(); return list; } finally { em.close(); } */ // TO: List<NameData> list = new LinkedList<NameData>(); for(NameData value:store.values()) { list.add(value); } return list; // TEST-MOD-END } public static List<NameData> query(int firstResult, int maxResults) { // TEST-MOD-FROM: /* EntityManager em = entityManager(); try { List<NameData> resultList = em.createQuery("select o from " + NameData.class.getSimpleName() + " o") .setFirstResult(firstResult).setMaxResults(maxResults).getResultList(); // force it to materialize resultList.size(); return resultList; } finally { em.close(); } */ // TEST-MOD-TO: return new LinkedList<NameData>(); // TEST-MOD-END } @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; /** * Used by RequestFactory to infer if an entity has changed */ @Version @Column(name = "version") private Integer version; private String name; public void setId(Long id) { this.id = id; } public Long getId() { return id; } public void setVersion(Integer version) { this.version = version; } public Integer getVersion() { return version; } public void setName(String name) { this.name = name; } public String getName() { return name; } public NameData persist() { // TEST-DEL-BEGIN /* EntityManager em = entityManager(); try { em.persist(this); } finally { em.close(); } */ // TEST-DEL-END return this; } public void remove() { // TEST-DEL /* EntityManager em = entityManager(); try { NameData attached = em.find(NameData.class, this.id); em.remove(attached); } finally { em.close(); } */ if( store.values().contains(this) ) { store.remove(this); } // TEST-DEL-END } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Id: ").append(getId()).append(", "); sb.append("name: ").append(getName()).append(", "); return sb.toString(); } } DisclaimerI spent one Saturday afternoon to troubleshoot different issues for
Google App Engine, validation, and persistence layers, but still cannot
make it work. Later I realized: They are totally not related to my goal - Learn GWT RequestFactory. I cannot understand why GWT team cannot provides a cleaner example for starters, but I can list what I learn and hopefully it can save the time of others. |
Memo >