Question

I have a CellTable<UserProxy>. So in other words it manages directly entity proxies of my database entities. With that I use an AsyncDataProvider<UserProxy> that fetches the data using a request factory. The cells of my columns are EditTextCell. And I added a FieldUpdater<UserProxy, String> to edit the values. Except here is my problem: if I update the value of the entity and save it immediately it works fine, but I don't know how I can differ the save to a click on a button later.

Basically, I want to implement the Apply-changes pattern (see: http://patterns.holehan.org/Review/ApplyChanges), so I want the user to be able to edit several values in the table and once he is done he can click the 'apply' button which will save all the changes.

So my idea for this was to change the value in the proxy entity without invoking save and then saving all modified entities in the clickhandler of the button.

But to make the change to a value in a proxy entity, I must call ctx.edit(user) first:

nameColumn.setFieldUpdater(new FieldUpdater<UserProxy, String>() {
    @Override
    public void update(int index, UserProxy object, String value) {
        if (!value.equals(object.getName())) {
            UserRequest ur = presenter.getClientFactory().getRequestFactory().getUserRequest();
            ur.edit(object);
            object.setName(value);
            saveButton.setEnabled(true);
        }
    }
});

And this makes it impossible to save them afterwards in the clickhandler of the apply button:

private void saveModifications() {
    List<UserProxy> items = cellTable.getVisibleItems();
    for (UserProxy item : items) {
        UserRequest ur = presenter.getClientFactory().getRequestFactory().getUserRequest();
        ur.save(item).fire();
    }
    cellTable.setVisibleRangeAndClearData(cellTable.getVisibleRange(), true);
}

Because calling save(item) throws this exception: java.lang.IllegalArgumentException: Attempting to edit an EntityProxy previously edited by another RequestContext

How to avoid this without having to make yet another class representing the same entity?

Was it helpful?

Solution

You must use a single RequestContext instance where you edit() all your proxies. You can edit() several times the same proxy with no error and no overhead.

So:

  1. store presenter.getClientFactory().getRequestFactory().getUserrequest() in a variable/field somewhere
  2. in the FieldUpdaters, ctx.edit(object).setName(value) will enqueue the changes in the RequestContext; possibly put the UserProxy in a Set too for later reference
  3. in saveModifications, loop over your proxies (possibly only those from the Set built on step 2) and ctx.save(item) and then at the end of the loop ctx.fire()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top