Add method to check if a property has changed
---------------------------------------------
Key: CAY-584
URL: http://issues.apache.org/cayenne/browse/CAY-584
Project: Cayenne
Type: New Feature
Components: Cayenne Core Library
Versions: AFTER 1.2
Reporter: Øyvind Harboe
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);
}
}
}
-- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/cayenne/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira
This archive was generated by hypermail 2.0.0 : Sat Jul 01 2006 - 06:57:32 EDT