One more way of doing that, based on the internal Cayenne mechanism for tracking all object changes as instances of GraphDiff:

  
http://cayenne.apache.org/doc20/api/cayenne/org/apache/cayenne/graph/GraphDiff.html

There is no public API to access the diffs directly, but you can intercept and process diffs on commit. Normally a DataContext.getChannel() would return a DataDomain. A user can replace it with a wrapper (via 'context.setChannel(..)') that delegates all DataChannel operations to the DataDomain:

  
http://cayenne.apache.org/doc20/api/cayenne/org/apache/cayenne/DataChannel.html

In the wrapper you can override 'onSync' to analyze/capture all the diffs being sent to the DataDomain for commit.

Andrus




On Feb 22, 2008, at 6:09 PM, Robert Zeigler wrote:
Hi,

I had a requirement very similar to this in a recent project.
Every change made to a set of objects had to be recorded to be able to provide a history of changes to an object. What I did was to have those objects descend from a common super class, which, in turn, descends from CayenneDataObject. In my superclass, I overrode writeProperty and setToOneTarget. You get the new values passed into you, and you have a chance to examine the old values, as well, by calling readProperty. It has worked out quite nicely.

Here's a sample snippet from writeProperty:

   @Override
   public void writeProperty(String name, Object value){
       Object old = readProperty(name);
       if (value == null) {
           if (old != null) {
recordChange(name,name + " changed from " + labelFor(readProperty(name)) + " to " + labelFor(value));
           }
       } else if (!value.equals(old)) {
recordChange(name,name + " changed from " + labelFor(readProperty(name)) + " to " + labelFor(value));
       }
       super.writeProperty(name, value);
   }

recordChange handles recording the changes for me; labelFor takes an object and converts it into a string suitable for user consumption.

HTH,

Robert

On Feb 22, 2008, at 2/228:59 AM , Ilya Lazarev wrote:

Apologies if this has been asked before, I couldn't find anything in the archives. The problem boils down to this: there is a cayenne object which is updated via a form. I want to capture changes made to every single modified field in a DB, the value before modification and the value after. The object has a number of to-many relationships, which would also have to be checked one by one. The simplest way I can envision this is by manually creating an object clone before any modifications are made, and then comparing the two objects and noting the differences. Is there an easier way to see a diff of the fields, perhaps by accessing properties of the cayenne object itself? I
am using cayenne 2.

Many thanks,
Ilya



Reply via email to