-1

I have an application server that sends an array of record data to the client in response to a search. Prior to sending the data to the client, my thought is to have the server store the data in a session variable.

When the user selects and then submits a record to edit, the client sends the entire record to the server. It updates the entire the record in the database (in case some other user has committed an update after they have searched).

However, I still need to capture the changes so that I can properly store audit information. I think it's best to capture old and new values based on what the user saw and changed. Therefore, I plan to (deep) compare field by field the record in the session data and the updated record sent by the client. I have two questions:

1) Are there pitfalls/problems with this approach?

2) If not, what is a good Java utility/library for comparing objects?

Thanks in advance for your help.

James
  • 2,876
  • 18
  • 72
  • 116

2 Answers2

1

You may be better off rejecting the change if another use has changed any field in the record. You can alert the user that the record has change and show them the new data to perform the update again (maybe keep their changed values available to them).

This way if another user changes a field that relates to a field they did not change, you will not be making changed to the records that no longer make sense with each other.

lilwupster
  • 873
  • 9
  • 11
  • That seems like a good conservative approach, but in this case, I think it will be OK for the last user to update to win with needing to see how the other user updated. I'm not sure there is a concern in relation to your last point (sentence). The reason is that the **entire** record is update (unchanged and changed fields). I'm marking this as the answer though since the question is closed and this is nice conservative that might be wanted by the users in the future. Thanks. – James Dec 17 '12 at 16:54
  • Replace "with" with "without" in the above comment. Sorry for the typo. – James Dec 17 '12 at 17:08
0

There are various approaches:
If for some reason you cannot mark the chhanged fields on server side and in the response, I would use reflection to compare all fields of a class, which hopefully are primitive objects or at least support equals(). However I would limit that to primitve types and String.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • Thanks for the response. Yes, I plan to compare the objects (probably with reflection) b/c I do not want the client telling the server what fields have changed. – James Dec 17 '12 at 16:57