Tuesday, March 1, 2011

Re: RequestFactory and GData glue



On Tuesday, March 1, 2011 5:01:19 PM UTC+1, koma wrote:
Hi,

I am working on a GWT 2.2 application and the server side connects to the GData Contacts Feed.

First implementation  was sending wrapped GData contacts as ValueProxy over to the client.

Coming to the think about it, a GData ContactEntry is actually an entity, but not the typical database entity.
So it would be great to work on a contact list with RequestFactory and eventbus dispatchen updates to the editors and managing a cache.

So I implemented a Locator for a ContactEntry :

public class ContactLocator extends Locator<ContactEntry, String> {
@Override
public ContactEntry create(Class<? extends ContactEntry> clazz) {
ContactEntry contactEntry = null;
try {
contactEntry = new ContactEntry();
Name name = new Name();
contactEntry.setName(name);
// Ask the service to insert the new entry
URL postUrl = new URL("https://www.google.com/m8/feeds/contacts/default/id/" + contactEntry.getId());
contactEntry = ContactsServiceImpl.getContactsService().insert(postUrl, contactEntry);

I believe you should do it in some kind of "persist" method in a service instead. create() is called when sending to the server a proxy that was created on the client-side (for EntityProxy-s; for ValueProxy-s, it's called for any object sent from the client to the server, whether it was created on the client or just edited there).
 
} catch (Exception e) {
// TODO: deal with this;
}
return new ContactEntry(contactEntry);
}

@Override
public ContactEntry find(Class<? extends ContactEntry> clazz, String id) {
ContactEntry contactEntry = null;
try {
// Build the GData URL for the contact entry and retrieve;
contactEntry = ContactsServiceImpl.getContactsService().getEntry(
new URL(id), ContactEntry.class);
} catch (Exception e) {
// TODO: deal with this;
}
return contactEntry;
}

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

@Override
public String getId(ContactEntry contactEntry) {
String ret = null;
if (contactEntry != null) {
ret = contactEntry.getEditLink().getHref();
}
return ret;
}

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

@Override
public Long getVersion(ContactEntry contactEntry) {
if (contactEntry != null) {
return contactEntry.getUpdated().getValue();
} else {
return null;
}
}
}


I setup ContactProxy as EntityProxy for GDATA ContactEntry

@ProxyForName(value = "com.google.gdata.data.contacts.ContactEntry", locator = "com.example.domain.ContactLocator")
public interface ContactProxy extends EntityProxy {
NameProxy getName();
List<EmailProxy> getEmailAddresses();
}


NameProxy and EmailProxy are ValueProxies mapping the properties from the respective Name and Email extension in GData.

One question, one problem :

PROBLEM :
- I make a service call to query for all contacts. The list with contact proxies is correctly returned to the client. But all value proxies (getName, getEmailAddresses) are NULL, so I can't access the information.

You have to add .with("name", "emailAddresses") to your Request<List<ContactProxy>> (or Request<ContactProxy>) before you fire() it: context.getAllContacts().with("name", "emailAddresses").to(new Receiver<List<ContactProxy>>() { ... })
 
QUESTION 
- After the query for all contacts, the locator find by Id methods are called for each ContactEntry. This seems like a lot of overkill - why does it do so ?

It checks if they're still "live" (this is the default implementation of isLive in the Locator class). If they're not, an EntityProxyChange(DELETED) event is dispatched on the client side.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment