[ardoJSON] How to get difference between two entity with same properties.

[ardoJSON] How to get difference between two entity with same properties.

  
Forge Component
(22)
Published on 2017-09-05 by Ricardo Silva
22 votes
Published on 2017-09-05 by Ricardo Silva

I have an entity called Application. I want to capture only changed attributes with old and new values. please advise.

Hello Raju,

Could you explain exactly what do you mean by "old and new values"?

If you are referring to values of the entity's attributes (the fields), unless you store the previous values somewhere, it is not possible to "compare" them.

(And why did you posted in the ardoJSON forge component?)

Cheers,
Eduardo Jauch

Thanks for your update. I have stored the old values of entity field values in the database. My objective is to capture only the changed field values and store them in the database.

For example my audit log table will be like 

Entity Name     -  Field Name      -  Old Value         - New Value

Application       - Date of Birth     -   10-Jan-1987   -  10-Jun 2017

Application       - Salary               -    5000               - 5050.


Please advise.

Hum...

I think it would be much easier to do this log at the moment of the change...
This way you can create logic to keep track of the fields that the user has changing, comparing the Form Record values with the Record you fetch from the database, and than create your audit at this moment.

Otherwise, you will have to "duplicate" your record on two different tables, and than, iterate through records an compare each field manually, or create a query (SQL), that will perform the comparison and return only the records that have changed fields. But in this case, you will have to create extra fields in the query result to be able to "check" which ones are different, and iterate through the list to be able to store the log. Probably it would be possible to create an SQL that do this for you, but would not be a simple query...

I would advise to use the first approach, as it is much simpler to keep...

Cheers,
Eduardo Jauch

Hi Raju,

Talking to Paulo (a co-worker), he suggested a different (and better approach).

When saving a record, you can serialize the original record and save it to a text field in your audit log, adding some extra information about the entity that is being stored and time, user, etc. 

The advantage is that it is a single operation and the log table will be independent, with the possibility to be used for any entity in the application.

When you want to see changes, you can filter by entity and deserialize the records to the correct entity structure, comparing the differences.

Cheers,
Eduardo Jauch

I also have this requirement, ArdoJSON will help a lot to serialize, deserialize, and do the comparing.

Thanks.