Re: Identifying changed fields

From: Steve Steinitz (steinit..atatactics.com.au)
Date: Mon Oct 25 2004 - 20:31:09 EDT

  • Next message: Derek Rendall: "Nested editing contexts"

    Hello,

    Per a helpful suggestion from Andrus earlier in this thread I have
    used the following technique (in MyObject.validateForUpdate) to
    identify changed fields in myObject:

        DataRow freshSnapshot = getDataContext().currentSnapshot(this);
        DataRow oldSnapshot = getDataContext().getObjectStore().getRetainedSnapshot(this.getObjectId());
        DataRow diff = (oldSnapshot != null) ?
        oldSnapshot.createDiff(freshSnapshot) : freshSnapshot;
        
        if (diff.size() > 0)
        {
            Set changedKeys = diff.keySet();
            Iterator i;
            for (i= changedKeys.iterator(); i.hasNext(); )
            {
                String key = (String)i.next();
                Object value = diff.get(key);
            
                AuditTrailEntry ate = (AuditTrailEntry)CustomDataObject.
                                                       createPersistantObject ( getDataContext(),
                                                                                "AuditTrailEntry");
                ate.setChangedAttribute(key);
                ate.setChangeDate(new Date());
                ate.setPreviousValue(oldSnapshot == null ? "-" :
                                    (oldSnapshot.get(key) != null ?
                                     oldSnapshot.get(key).toString() : "-"));
                ate.setNewValue(value == null ? "-" : value.toString());
                addToAuditTrailEntries(lec);
                ...

    This works well. However, I was surprised to discover that if I add
    an object to one of MyObject's relationships to using the following code

            SubObject aSubObject = (SubObject)createObject("SubObject");
            myObject.addToSubObjects(aSubObject);

    then the old snapshot, above:
    DataRow oldSnapshot = getDataContext().getObjectStore().getRetainedSnapshot(this.getObjectId ())
    is null so that I can't get a meaningful diff.

    Can anyone think of a reason this might occur? If I am unable to
    solve this I may take up Mike's suggestion which he joked was "the
    hard way" but which appears to have some advantages in specific field
    handling.

    Thanks,

    Steve



    This archive was generated by hypermail 2.0.0 : Mon Oct 25 2004 - 20:31:16 EDT