Generic Entity Locator

148 views
Skip to first unread message

Cem Ikta

unread,
Aug 10, 2011, 9:18:59 AM8/10/11
to Google Web Toolkit
Hello,

I am writing a request factory sample in gwt 2.3, for this sample i
want to write a generic entity locator. I've read David Chandler's
sample http://turbomanage.wordpress.com/2011/03/25/using-gwt-requestfactory-with-objectify/
but David is using ObjectifyLocator with google app engine.

// David's generic ObjectifyLocator.java
/**
* Generic @Locator for objects that extend DatastoreObject
*/
public class ObjectifyLocator extends Locator<DatastoreObject, Long>
{
@Override
public DatastoreObject create(Class<? extends DatastoreObject> clazz)
{
try
{
return clazz.newInstance();
} catch (InstantiationException e)
{
throw new RuntimeException(e);
} catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
}

@Override
public DatastoreObject find(Class<? extends DatastoreObject> clazz,
Long id)
{
DAOBase daoBase = new DAOBase();
return daoBase.ofy().find(clazz, id);
}

@Override
public Class<DatastoreObject> getDomainType()
{
// Never called
return null;
}

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

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

@Override
public Object getVersion(DatastoreObject domainObject)
{
return domainObject.getVersion();
}
}

My Classes :
Base entity : getId, getVersion etc. (base class for all entities)
Category : entity
CategoryLocator : locator class
CategoryProxy : DTO Proxy for Category entity.

// CategoryLocator
public class CategoryLocator extends Locator<Category, Long> {

@Override
public Category create(Class<? extends Category> clazz) {
return new Category();
}

@Override
public Category find(Class<? extends Category> clazz, Long id) {
return getCategoryDAO().findById(id);
}

private CategoryDao getCategoryDAO() {
return new CategoryDao();
}

@Override
public Class<Category> getDomainType() {
return Category.class;
}

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

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

@Override
public Object getVersion(Category domainObject) {
return domainObject.getVersion();
}
}

// CategoryProxy
@ProxyFor(value = Category.class, locator = CategoryLocator.class)
public interface CategoryProxy extends EntityProxy {

Long getId();

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

How can i write only a generic entity locator class for my all proxy
classes such as ObjectifyLocator.class without google app engine
structure?

Thanks,
Cem.

Jens

unread,
Aug 16, 2011, 5:43:58 AM8/16/11
to google-we...@googlegroups.com
Just create one Locator for your base entity, e.g. EntityLocator extends Locator<EntityBase, Long> and use it for all EntityProxies.

I have done that and it works but getDomainType() doesn't feel right to me, see: https://groups.google.com/forum/#!topic/google-web-toolkit/SGMsIBaJ4hI/discussion

-- J.

Cem Ikta

unread,
Aug 23, 2011, 9:16:12 AM8/23/11
to google-we...@googlegroups.com
Jens thanks for your answer, i have problems with generic dao and request factory (in gwt 2.3). The problem was in gwt 2.4 solved (according to  http://code.google.com/p/google-web-toolkit/wiki/RequestFactory_2_4 i have not yet tested!) . I am waiting for gwt 2.4 final.

Cem Ikta

unread,
Aug 26, 2011, 6:12:08 AM8/26/11
to google-we...@googlegroups.com
Hi Jens,

how do you write find Methode of your EntityLocator? Is my methode correct?

    @Override
    public EntityBase find(Class<? extends EntityBase> clazz, Long id) {
        return ?????
    }

Best regards.

Jens

unread,
Aug 26, 2011, 6:25:33 AM8/26/11
to google-we...@googlegroups.com
Should be fine. I just do 

return entitymanager.find(clazz, id).

-- J.

Mark Wengranowski

unread,
Sep 21, 2011, 1:49:35 PM9/21/11
to Google Web Toolkit
Hi Jens,

I'm currently implementing this solution and have a question about the
find method.

You said that you just return entityManager.find(clazz, id);
but where and how is the variable entityManager initialized?


Thanks,
-Mark
Reply all
Reply to author
Forward
0 new messages