Search code examples
gwtrequestfactory

How to edit value of ValueProxy of gwt requestfactory at client side?


I have 2 models: ContactGroup and Contact. ContactGroup contains many Contacts.

In the page, I have to display a list of groups and number of contacts in the correspondence group like this:

  • Group Foo (12 contacts)
  • Group Bar (20 contacts)

So I at server side I used a DTO ContactGroupInfo:

public class ContactGroupInfo {
    private Integer contactCount;
    private Long id;
    private String name;

    public Integer getContactCount() { return this.contactCount; }
    public Long getId() { return this.id; }
    public String getName() { return this.name; }
    public void setContactCount(Integer count) { this.contactCount = count; }
    public void setId(Long id) { this.id = id; }
    public void setName(String name) { this.name = name; }
}

In this ContactGroupInfo, I added contactCount field which is not a field in ContactGroup entity.

And at client side, I used a ValueProxy:

@ProxyFor(value = ContactGroupInfo.class, locator = ContactGroupService.class)
public interface LightContactGroupProxy extends ValueProxy {
    Integer getContactCount();
    Long getId();
    String getName();
    void setContactCount(Integer count);
    void setId(Long id);
    void setName(String name);
}

So when server side returns to client side a list of LightContactGroupProxy, I stored that list a in ArrayList to render to a CellTable.

And here is the problem comes to me: when I need to edit the name of the group at client side, I can't edit the LightContactGroupProxy object directly.

  • So I have to send the new name to server to return a new LightContactGroupProxy with the new name. This is not effective because I have to count contacts again (althought I know the number of contacts does not change).
  • Or I have to send both the number of contacts and new name to server to create a new LightContactGroupProxy with the new name. This is not I want, because if LightContactGroupProxy has many other fields I have to send many fields.

I don't know why GWT teams designs the immutable proxy. So please, someone has experience on requestfactory please show me the correct way to handle ValueProxy returned from server so that we can use them to render and edit?

Thank you


Solution

  • Maybe you should try something like this :

    ContactGroupContext ctx = requestFactory.newContactGroupContext();
    LightContactGroupProxy editableProxy = ctx.edit(lightContactGroupProxy);
    editableProxy.setName(newName);
    ctx.saveInfoAndReturn(editableProxy).fire(receiver); // or just ctx.fire();
    

    Anyway, I wouldn't use ValueProxy in this case, I would directly get the ContactGroup entities with a transiant property contactCount. The property could be a primitive, or a ValueProxy if you don't want it to be calculated every time a ContactGroup is requested.