You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cayenne.apache.org by "Øyvind Harboe (JIRA)" <ji...@apache.org> on 2006/07/01 12:55:49 UTC

[JIRA] Created: (CAY-584) Add method to check if a property has changed

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