Cayenne
  1. Cayenne
  2. CAY-584

Add method to check if a property has changed

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: Undefined future
    • Fix Version/s: Undefined future
    • Component/s: Core Library
    • Labels:
      None

      Description

      Here is the code. I've tested isChanged() and it appears robust for both normal properties and foreign key relationships.

      package org.objectstyle.cayenne.access;

      import org.objectstyle.cayenne.DataObject;
      import org.objectstyle.cayenne.ObjectId;

      public class CayenneUtils
      {
      /** this version of isChanged can only be used for foreign key relationship properties */
      private static boolean isChangedArc(DataObject o, String propName)
      {
      ObjectDiff diff = (ObjectDiff) o.getDataContext().getObjectStore().getChangesByObjectId().get(o.getObjectId());
      Object oldValue = diff.getArcSnapshotValue(propName);
      ObjectId newId=((DataObject) o.readProperty(propName)).getObjectId();
      /* ick! old or new value can be null which .equals() does not support */
      if (newId==oldValue)

      { return false; } else if ((newId==null) || (oldValue==null))
      { return true; } else
      { return !oldValue.equals(newId); }
      }

      /**
      * this version of isChanged can only be used for normal properties and not
      * relationships.
      */
      private static boolean isChangedProperty(DataObject o, String propName)
      {
      ObjectDiff diff = (ObjectDiff) o.getDataContext().getObjectStore().getChangesByObjectId().get(o.getObjectId());
      Object oldValue = diff.getSnapshotValue(propName);
      Object newValue= o.readProperty(propName);
      if (newValue==oldValue)
      { return false; }

      else if ((newValue==null) || (oldValue==null))

      { return true; }

      else

      { return !oldValue.equals(newValue); }

      }

      /** checks if a property or foreign key relationship changed */
      public static boolean isChanged(DataObject o, String propName)
      {
      ObjectDiff diff = (ObjectDiff) o.getDataContext().getObjectStore().getChangesByObjectId().get(o.getObjectId());
      if (diff.containsArcSnapshot(propName))

      { return isChangedArc(o, propName); }

      else

      { return isChangedProperty(o, propName); }

      }
      }

        Activity

        There are no comments yet on this issue.

          People

          • Assignee:
            Unassigned
            Reporter:
            Øyvind Harboe
          • Votes:
            2 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:

              Development