gwtrequestfactoryrequestcontext

GWT RequestFactory: Send changes twice


I need your help with the gwt requestfactory

considering following scenario:
I get an existing entity (let's say a invoice) from the server:

InvoiceEntityProxy invoice = request1.getInvoice();

I want to make some changes, so I edit it with a new request:

InvoiceEntityProxy editableInvoice = request2.edit(invoice);
//make some changes to editableInvoice

Now I send the changes made with the second request to the server, to create a preview:

request2.createPreview(editableInvoice);

When the request is sent, the invoice proxy is frozen and I re-enable editing by assigning the proxy to a new request:

editableInvoice = request3.edit(editableInvoice);

If everything is okay, i want to update the proxy and send it to the server, using the latest request:

request3.update(editableInvoice);

But the changes never arrive on the server, because the latest request (request3) doesn't know anything about the changes made to the proxy assigned to the request2.

I thought about following solutions:

  1. I could redo the changes on the latest proxy. But for that, I've to iterate over all attributes and set them again (not very friendly solution, because I've to adjust the method each time I add some attributes to the proxy)
  2. Another approach would be to send the proyx without an id to the server and send the id as second parameter of the update-method. But this would be a shame, because not only the deltas would be sent to the server (which is one of the greate features of the requestFactory).

So what is the best and most common practice to let the request3 know about the changes already made to the proxy, when it was assigned to another request.


Solution

  • Nice! I found the solution for my problem.

    I still have an instance of the original proxy, because the edit() method of the context always return a new instance of the proxy. So I save the original proxy before sending any request.

    After each successful request, I re-enable editing the proxy by call the edit method again:

    editableInvoice = request3.edit(editableInvoice);
    

    Now the crux: I can set the original proxy of a proxy, which is used to consider if it changed and what changed. This is done by using AutoBean and set the PARENT_OBJECT Tag like this:

    AutoBean<InvoiceEntityProxy> editableInvoiceBean = AutoBeanUtils.getAutoBean(editableInvoice);
    AutoBean<InvoiceEntityProxy> originalInvoiceBean = AutoBeanUtils.getAutoBean(originalInvoice);
    
    editableInvoiceBean.setTag(Constants.PARENT_OBJECT, originalInvoiceBean);
    

    On the next request all changed properties are send to the server again.

    Thank you for your help and thank you for the hint with the AutoBean @Zied Hamdi