Generic Entity Locator for Request Factory in GWT

gwt logoIn my old Gwt projects i have previously used RPC with Dozer for database communications. Now i am using RequestFactory in my new Gwt project.

I’ve written a generic entity locator for all my entities. (thanks to Gwt community)

For generic entity locator we need a entity base class. All entities can extend EntityBase, that provides getId() and getVersion(). And you write in proxy @ProxyFor(value = Category.class, locator = EntityLocator.class)

/**
 * EntityBase, base entity for all entities.
 */
@MappedSuperclass
public abstract class EntityBase implements Serializable {

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "id", nullable = false, columnDefinition = "BIGINT UNSIGNED")
  protected Long id;

  @Version
  @Column(name = "version", nullable = false, columnDefinition = "BIGINT UNSIGNED DEFAULT 0")
  private Long version;

  public Long getId() {
    return id;
  }

  public Long getVersion() {
    return version;
  }
}
/**
 * Category entity
 */
@Entity
@Table(name = "category")
@AttributeOverride(name = "id", column = @Column(name = "category_id",
nullable = false, columnDefinition = "BIGINT UNSIGNED"))
public class Category extends EntityBase {

  private static final long serialVersionUID = 1L;

  @Basic(optional = false)
  @Column(name = "name")
  private String name;

  public Category() {
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
  ...
}
/**
 * Entity Locator for all entities
 */
public class EntityLocator extends Locator<EntityBase, Long> {

  @Override
  public EntityBase create(Class<? extends EntityBase> clazz) {
    try {
      return clazz.newInstance();
    } catch (InstantiationException e) {
      throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  public EntityBase find(Class<? extends EntityBase> clazz, Long id) {
    // TODO : create your entity manager !
    return entityManager.find(clazz, id);
  }

  /**
   * it's never called
   */
  @Override
  public Class<EntityBase> getDomainType() {
    throw new UnsupportedOperationException();
    // or return null;
  }

  @Override
  public Long getId(EntityBase domainObject) {
    return domainObject.getId();
  }

  @Override
  public Class<Long> getIdType() {
    return Long.class;
  }

  @Override
  public Object getVersion(EntityBase domainObject) {
    return domainObject.getVersion();
  }
}
/**
 * DTO Proxy for Category entity.
 */
@ProxyFor(value = Category.class, locator = EntityLocator.class)
public interface CategoryProxy extends EntityProxy {

  Long getId();

  String getName();
  void setName(String name);

}

This generic entity locator you can use with JPA. But David Chandler has written a sample project “Using Gwt RequestFactory with Objectify“. Perhaps this is also interesting for you.

Happy coding.

Debug Pyramid projects through eclipse (aptana studio)

aptana studio 3 pyramid framework debug

In my previous post i wrote pyramid framework settings in eclipse (aptana studio).

How can i debug pyramid projects through eclipse (aptana studio)?

When you give  “–reload” parameter in your project run configuration in arguments tab, eclipse will not debug your pyramid application.

development.ini –reload  => start without debugging

1 – delete “–reload” parameter in your project run configuration in arguments tab

development.ini  => start with debugging.

2 – set your debug points in your classes.

3 – click your debug configuration menu in eclipse toolbar -> debug button.

* debug icon credit

Pyramid framework settings in eclipse (aptana studio)

aptana studio 3 pyramid framework

How can i set up Pyramid Framework 1.3 in Eclipse or in Aptana Studio 3?

Sure, IDE is very important in software development(debugging, code assist, organize imports etc.). You can code python in editors, it is practical but diffucult for beginners.

Python and Django have eclipse plugin, there is no eclipse plugin for Pyramid Framework?

How i can run a Pyramid Project from Eclipse or Aptana Studio 3?

Steps :

1 – in Console : Create new pyramid 1.3.xx project :
On Linux:

   $ bin/pcreate -s alchemy MyProject

On Windows :

   > Scripts\pcreate -s alchemy MyProject

2 – Install your newly created project for development

   $ cd MyProject
   $ ../bin/python setup.py develop

On Windows :

   > cd MyProject
   > ..\Scripts\python.exe setup.py develop

3 – In Aptana Studio 3 : Create new PyDev project and give project content path as your Pyramid Project path

pydev

4 – For run configuration add a  pyramid_run variable in menu : Window > Preferences > in list PyDev – Interpreter – Python in tab String Substitution Variables – click Add variable and give key and value as in the screenshot below. On MS Windows select Scripts\pserve-script.py instead pserve.

pydev

5 – Add a run configuration in menu : Run -> Run Configuration on the left side on Python Run node Right Click : New Click and give run configuration settings and run.

pydev

pydev

6 – Your pyramid application runs on browser.

pydev