Re: Identifying changed fields

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Thu Oct 14 2004 - 20:54:43 EDT

  • Next message: Mike Kienenberger: "Re: Identifying changed fields"

    Hi Steve,

    > I need to make an audit trail of changes to an object. Is there
    > support in the API for identifying, at commitChanges time, properties
    > which differ from the database? Also, would I do this comparison at
    > validation time?
    >
    > DataRow.createDiff is tantalizing but I can't quite see how to use it
    > for this purpose.

    You are on the right track. "validateForSave" is actually a good place
    to build an audit trail. Just be prepared to roll it back in case
    commit fails downstream (if I am not mistaken Eric and Matt implemented
    something similar on one of their projects?)

    You can do something like this (assuming you are within a
    CayenneDataObject subclass):

    DataRow freshSnapshot = getDataContext().currentSnapshot(this);
    DataRow oldSnapshot =
    getDataContext().getObjectStore().getRetainedSnapshot(this.getObjectId()
    );

    // from here it depends on what you want to log...
    // either the new values, the old values or both...
    // e.g. for the new values:
    DataRow diff = (oldSnapshot != null) ?
    oldSnapshot.createDiff(freshSnapshot) : freshSnapshot;

    // if you log to DB, here you can create an audit data object;
    // if you log to file, you should probably store the diff in memory
    // until the context is successfully committed,
    // e.g. by using a global TransactionDelegate set per DataDomain...

    > ps. let me take this opportunity to acknowledge Andrus' tireless,
    > patient and competant work in keeping this list an excellent
    > source of information -- I believe I would speak for everyone

    Thanks for the good word! ;-)

    Andrus



    This archive was generated by hypermail 2.0.0 : Thu Oct 14 2004 - 20:54:46 EDT