Memo‎ > ‎

Run a GWT RequestFactory Sample without Setting up Persistence

posted Dec 14, 2013, 8:47 PM by Teng-Yok Lee   [ updated Dec 15, 2014, 12:25 AM ]
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.
  • Install Google Web Toolkit
  • Install Google App Engine
  • checkout this sample code on DemoRequestFactory  contributed by Brandon Donnelson. Note the at the destination folder is DemoRequestFactory/DemoRequestFactory.
svn co http://gwt-examples.googlecode.com/svn/trunk/DemoRequestFactory DemoRequestFactory/DemoRequestFactory

  • Open Eclipse and switch the workspace to DemoRequestFactory
  • Import the project from DemoRequestFactory/DemoRequestFactory
  • Right click at the project DemoRequestFactory and choose property:
    • Google/App Engine: Uncheck Use Google App Engine
    • Java Build Path, and open the tab Libraries
      • Remove all .jar under \Application
      • Add Google Web Toolkit as a library
      • Add Google App Engine as a library
  • Copy the following to war/WEB-INF/lib. I downloaded them from mvp-test, another sample for RequestFactory (contributed by Pandurang Patil)
    • hibernate-validator-4.0.2.GA.jar (required)
    • slf4j-api-1.6.1.jar (required)
    • slf4j-log4j12-1.6.1.jar (optional. Can reduce the warning for slf4j/log4j)
    • log4j-1.2.16.jar (optional. Can reduce the warning for slf4j/log4j)
Now, replace the content of src/org/gonevertical/server/domain/NameData.java by the following quoted code. The original code is commented by the label // TEST-...). The idea is simple: Other than accessing the database or so called persistence layer, just use a HashMap to maintain the entry. Of course this is not a correct way to design a website, but it will be sufficient for a starter to know how RequestFactory works.
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();
  }

}

Disclaimer

I 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.

Reference:

  1. http://stackoverflow.com/questions/4460144/gwt-requestfactory-examples

       
Comments