0

I have a Spring 4 and Hibernate 5 back-end RESTful web-service. This works great and is all unit tested. The front-end is a SmartGWT 5.0p application which uses DataSources, not RestDataSources to communicate with the back-end.

The front-end SmartGWT 5.0p uses a listgrid to edit data, and then the ListGrid is attched to a datasource. Only the edited data in the ListGrid is sent back, not the entire row. If I could, I'd like to be able send backthe entire listgrid row with edited data, and the unedited data. If I could get an answer to that, that would be great.

Or, the alternate is we let SmartGWT only send back part of the data which is edited. This comes to the back-end as JSON and is changed into an Object/Entity. The controller/end-point is not in a session yet, but then we call a method in the service layer which is transactional.

So, then question becomes we have a detached object in a session in the method in the service layer. We have a detached object with a database primary key ... but it also has 1 or 2 fields of updated data, and now we want to merge that data back to the database. We can't call an update with this entity because with the partial data, some of the fields are being set to null. In reality, we want to pull back the item from the databae, update the edited fields, and then write the data back to the database.

I could do this all manually ... but do I have to? I expect there is a more graceful way to handle this.

Thanks!

tjholmes66
  • 1,850
  • 4
  • 37
  • 73

1 Answers1

0

This is only a partial answer:
I can make SmartGWT combine old values and new values with a link I found here:

SMARTGWT DataSource (GWT-RPC-DATASource) LISTGRID

And the code is as follows:

private ListGridRecord getEditedRecord(DSRequest request)
{
    // Retrieving values before edit
    JavaScriptObject oldValues = request
            .getAttributeAsJavaScriptObject("oldValues");

    // Creating new record for combining old values with changes
    ListGridRecord newRecord = new ListGridRecord();

    // Copying properties from old record
    JSOHelper.apply(oldValues, newRecord.getJsObj());

    // Retrieving changed values
    JavaScriptObject data = request.getData();

    // Apply changes
    JSOHelper.apply(data, newRecord.getJsObj());

    return newRecord;
}

This JSON string contains the new fields updated, and the old fields. When this json string is sent back to the RESTful back end, the Jackson Mapper creates an entity and puts all the fields in, this is essentially a detached entity. It's an object outside of the session, but the id resides in the database.

Because I have a complete entity, I can all an update, and that works. Problem solved.

BUT, I'd still like to find out an elegant solution for taking a detached entity, get the original record from the database, and then merge these two objects, and then finally update the record.

In the Service layer in Spring which has a transaction, and creates the session, a manual process, which I don't want to do might look something like this:

1) get the id from the updatedEntity
2) get that attachedEntity from the database using that id
3) compare the fields
   if( updatedEntity.updateField1 != null ) 
   {   attachedEntity.setField1(updatedEntity.updateField1)   }
   If I had to do step 3 for multiple fields,  
   that's not very elegant.
4) Update attachedEntity to the database, because it now has updated fields.

So, again, an elegant solution to fix this might be helpful. Thanks!

Community
  • 1
  • 1
tjholmes66
  • 1,850
  • 4
  • 37
  • 73