0

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.

3 Answers3

1

You simply forget to call fire(). Example

request2.createPreview(editableInvoice).fire();

Bear in mind that if the following request depend on the result of the previous one, you should put your code in the OnSuccess methode because the request is asynchronous

It's also possible to append multiple requests

EDIT

It important to use the same request for the edit and fire operations. So replace this line

request.update(editableInvoice);

with

request3.update(editableInvoice);
Momo
  • 2,471
  • 5
  • 31
  • 52
  • sorry, my code examples aren't complete. Of course I'm calling the fire method in my code. The second request, doesn't depend on result of the first one. And I'm not sure, if the append method helps me in this scenario, because with it, I just send multiple requests at once, what is not in my mind. – user1215985 Feb 13 '14 at 16:06
  • you're right. again an error in my code examples. Of course I use the same request, but my problem remains. I can't send the same deltas to the server twice. – user1215985 Feb 13 '14 at 16:23
  • @user1215985 If the method in the DAO still not being called, Check if the DAO or the Service class has been correctly mapped in the DAOLocator (if you have one) – Momo Feb 14 '14 at 01:55
1

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

  • You're welcome :), you should also flag (+1) helping answers so that people who help can have a "good rank" in stackOverflow, this is both good for them "as a reputation" and for people who read them "as a reliability indicator". It's somehow addictive as collecting points in a game to collect points here :p Best regards – Zied Hamdi Feb 17 '14 at 08:01
  • 1
    Hehe, I'd like to vote your post up, but I don't have enough reputation yet. I'll as soon as I can :-) – user1215985 Feb 17 '14 at 16:24
0

You also can use AutoBeans to duplicate the object before you start changing it. You can keep the original one untouched then request.edit() it and apply changes (introspection like changes) from the "dirty" object.

You'll maybe have to do some research on how to handle EntityProxies since they are "special AutoBeans" : I had to use special utility objects to serialize them to json (available in GWT). So there might be some special handling in doing a deep copy too.

There is an issue maybe with GWT keeping only one version of each EntityProxy (I never checked if it is global or only in the context of a request)

Zied Hamdi
  • 2,400
  • 1
  • 25
  • 40