Author: aadamchik
Date: Sat Jan 21 17:09:02 2012
New Revision: 1234364
URL: http://svn.apache.org/viewvc?rev=1234364&view=rev
Log:
CAY-1616 Remove internal dependencies on deprecated ObjectContext.localObject
merging implementation of the deprecated method between client and server
the differences are now addressed by specialized PersistentDescriptors
(shallowMerge)
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/CayenneContext.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectDescriptor.java
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java?rev=1234364&r1=1234363&r2=1234364&view=diff
==============================================================================
---
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java
(original)
+++
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java
Sat Jan 21 17:09:02 2012
@@ -96,7 +96,7 @@ public abstract class BaseContext implem
protected transient DataChannel channel;
protected transient QueryCache queryCache;
protected transient EntityResolver entityResolver;
-
+
protected boolean validatingObjectsOnCommit = true;
/**
@@ -153,7 +153,7 @@ public abstract class BaseContext implem
* @since 3.1
*/
protected void attachToRuntime(Injector injector) {
-
+
// TODO: nested contexts handling??
attachToChannel(injector.getInstance(DataChannel.class));
setQueryCache(new
NestedQueryCache(injector.getInstance(QueryCache.class)));
@@ -224,7 +224,7 @@ public abstract class BaseContext implem
public void setValidatingObjectsOnCommit(boolean flag) {
this.validatingObjectsOnCommit = flag;
}
-
+
/**
* @since 3.1
*/
@@ -298,7 +298,80 @@ public abstract class BaseContext implem
* internal code has been refactored to avoid using this
method all
* together.
*/
- public abstract Persistent localObject(ObjectId id, Object prototype);
+ public Persistent localObject(ObjectId id, Object prototype) {
+
+ if (id == null) {
+ throw new IllegalArgumentException("Null ObjectId");
+ }
+
+ ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(
+ id.getEntityName());
+
+ // have to synchronize almost the entire method to prevent multiple
threads from
+ // messing up dataobjects per CAY-845. Originally only parts of "else"
were
+ // synchronized, but we had to expand the lock scope to ensure
consistent
+ // behavior.
+ synchronized (getGraphManager()) {
+ Persistent cachedObject = (Persistent)
getGraphManager().getNode(id);
+
+ // merge into an existing object
+ if (cachedObject != null) {
+
+ // TODO: Andrus, 1/24/2006 implement smart merge for modified
objects...
+ if (cachedObject != prototype
+ && cachedObject.getPersistenceState() !=
PersistenceState.MODIFIED
+ && cachedObject.getPersistenceState() !=
PersistenceState.DELETED) {
+
+ if (prototype != null
+ && ((Persistent) prototype).getPersistenceState()
!= PersistenceState.HOLLOW) {
+
+ descriptor.shallowMerge(prototype, cachedObject);
+
+ if (cachedObject.getPersistenceState() ==
PersistenceState.HOLLOW) {
+
cachedObject.setPersistenceState(PersistenceState.COMMITTED);
+ }
+ }
+ }
+
+ return cachedObject;
+ }
+ // create and merge into a new object
+ else {
+
+ // Andrus, 1/26/2006 - note that there is a tricky case of a
temporary
+ // object
+ // passed from peer DataContext... In the past we used to
throw an
+ // exception
+ // or return null. Now that we can have a valid (but generally
+ // indistinguishible) case of such object passed from parent,
we let it
+ // slip... Not sure what's the best way of handling it that
does not
+ // involve
+ // breaking encapsulation of the DataChannel to detect where
in the
+ // hierarchy
+ // this context is.
+
+ Persistent localObject;
+
+ localObject = (Persistent) descriptor.createObject();
+
+ localObject.setObjectContext(this);
+ localObject.setObjectId(id);
+
+ getGraphManager().registerNode(id, localObject);
+
+ if (prototype != null
+ && ((Persistent) prototype).getPersistenceState() !=
PersistenceState.HOLLOW) {
+
localObject.setPersistenceState(PersistenceState.COMMITTED);
+ descriptor.shallowMerge(prototype, localObject);
+ }
+ else {
+ localObject.setPersistenceState(PersistenceState.HOLLOW);
+ }
+
+ return localObject;
+ }
+ }
+ }
public abstract Collection<?> modifiedObjects();
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/CayenneContext.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/CayenneContext.java?rev=1234364&r1=1234363&r2=1234364&view=diff
==============================================================================
---
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/CayenneContext.java
(original)
+++
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/CayenneContext.java
Sat Jan 21 17:09:02 2012
@@ -132,7 +132,7 @@ public class CayenneContext extends Base
CayenneContextGraphManager internalGraphManager() {
return graphManager;
}
-
+
/**
* Commits changes to uncommitted objects. First checks if there are
changes in this
* context and if any changes are detected, sends a commit message to
remote Cayenne
@@ -155,7 +155,7 @@ public class CayenneContext extends Base
if (graphManager.hasChanges()) {
- if (isValidatingObjectsOnCommit()) {
+ if (isValidatingObjectsOnCommit()) {
ValidationResult result = new ValidationResult();
Iterator<?> it = graphManager.dirtyNodes().iterator();
while (it.hasNext()) {
@@ -174,11 +174,11 @@ public class CayenneContext extends Base
}
}
}
-
+
if (result.hasFailures()) {
throw new ValidationException(result);
}
- }
+ }
graphManager.graphCommitStarted();
@@ -296,97 +296,6 @@ public class CayenneContext extends Base
return new CayenneContextQueryAction(this, context, query).execute();
}
- /**
- * Converts a list of Persistent objects registered in some other
ObjectContext to a
- * list of objects local to this ObjectContext.
- * <p>
- * <i>Current limitation: all objects in the source list must be either in
COMMITTED
- * or in HOLLOW state.</i>
- * </p>
- *
- * @deprecated since 3.1 Cayenne users should use {@link
#localObject(Object)}; the
- * internal code has been refactored to avoid using this
method all
- * together.
- */
- @Override
- public Persistent localObject(ObjectId id, Object prototype) {
-
- // TODO: Andrus, 1/26/2006 - this implementation is copied verbatim
from
- // DataContext. Somehow need to pull out the common code or implement
inheritance
-
- // ****** Copied from DataContext - start *******
-
- if (id == null) {
- throw new IllegalArgumentException("Null ObjectId");
- }
-
- ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(
- id.getEntityName());
-
- synchronized (getGraphManager()) {
- Persistent cachedObject = (Persistent)
getGraphManager().getNode(id);
-
- // merge into an existing object
- if (cachedObject != null) {
-
- // TODO: Andrus, 1/24/2006 implement smart merge for modified
objects...
- if (cachedObject != prototype
- && cachedObject.getPersistenceState() !=
PersistenceState.MODIFIED
- && cachedObject.getPersistenceState() !=
PersistenceState.DELETED) {
-
- if (prototype != null
- && ((Persistent) prototype).getPersistenceState()
!= PersistenceState.HOLLOW) {
-
- descriptor.shallowMerge(prototype, cachedObject);
-
- if (cachedObject.getPersistenceState() ==
PersistenceState.HOLLOW) {
-
cachedObject.setPersistenceState(PersistenceState.COMMITTED);
- }
- }
- }
-
- return cachedObject;
- }
- // create and merge into a new object
- else {
-
- // Andrus, 1/26/2006 - note that there is a tricky case of a
temporary
- // object
- // passed from peer DataContext... In the past we used to
throw an
- // exception
- // or return null. Now that we can have a valid (but generally
- // indistinguishible) case of such object passed from parent,
we let it
- // slip... Not sure what's the best way of handling it that
does not
- // involve
- // breaking encapsulation of the DataChannel to detect where
in the
- // hierarchy
- // this context is.
-
- Persistent localObject;
-
- localObject = (Persistent) descriptor.createObject();
-
- localObject.setObjectContext(this);
- localObject.setObjectId(id);
-
- getGraphManager().registerNode(id, localObject);
-
- if (prototype != null
- && ((Persistent) prototype).getPersistenceState() !=
PersistenceState.HOLLOW) {
-
localObject.setPersistenceState(PersistenceState.COMMITTED);
- descriptor.shallowMerge(prototype, localObject);
- }
- else {
- localObject.setPersistenceState(PersistenceState.HOLLOW);
- }
-
- return localObject;
- }
- }
-
- // ****** Copied from DataContext - end *******
- }
-
@Override
public Collection<?> uncommittedObjects() {
synchronized (graphManager) {
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java?rev=1234364&r1=1234363&r2=1234364&view=diff
==============================================================================
---
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java
(original)
+++
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java
Sat Jan 21 17:09:02 2012
@@ -1188,98 +1188,6 @@ public class DataContext extends BaseCon
}
- /**
- * Returns an object local to this DataContext and matching the ObjectId.
If
- * <code>prototype</code> is not null, local object is refreshed with the
prototype
- * values.
- * <p>
- * In case you pass a non-null second parameter, you are responsible for
setting
- * correct persistence state of the returned local object, as generally
there is no
- * way for Cayenne to determine the resulting local object state.
- *
- * @since 1.2
- * @deprecated since 3.1 Cayenne users should use {@link
#localObject(Object)}; the
- * internal code has been refactored to avoid using this
method all
- * together.
- */
- @Override
- public Persistent localObject(ObjectId id, Object prototype) {
-
- // ****** Warning: when changing the code below, don't forget to change
- // CayenneContext's implementation which right now relies on
copy/paste "reuse"
-
- if (id == null) {
- throw new IllegalArgumentException("Null ObjectId");
- }
-
- // note that per-object ClassDescriptor lookup is needed as even if all
- // objects where fetched as a part of the same query, as they may
belong to
- // different subclasses
- ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(
- id.getEntityName());
-
- // have to synchronize almost the entire method to prevent multiple
threads from
- // messing up dataobjects per CAY-845. Originally only parts of "else"
were
- // synchronized, but we had to expand the lock scope to ensure
consistent
- // behavior.
- synchronized (getGraphManager()) {
- Persistent cachedObject = (Persistent)
getGraphManager().getNode(id);
-
- // merge into an existing object
- if (cachedObject != null) {
-
- int state = cachedObject.getPersistenceState();
-
- // TODO: Andrus, 1/24/2006 implement smart merge for modified
objects...
- if (cachedObject != prototype
- && state != PersistenceState.MODIFIED
- && state != PersistenceState.DELETED) {
-
- descriptor.injectValueHolders(cachedObject);
-
- if (prototype != null
- && ((Persistent) prototype).getPersistenceState()
!= PersistenceState.HOLLOW) {
-
- descriptor.shallowMerge(prototype, cachedObject);
-
- if (state == PersistenceState.HOLLOW) {
-
cachedObject.setPersistenceState(PersistenceState.COMMITTED);
- }
- }
- }
-
- return cachedObject;
- }
- // create and merge into a new object
- else {
-
- Persistent localObject;
-
- localObject = (Persistent) descriptor.createObject();
-
- localObject.setObjectContext(this);
- localObject.setObjectId(id);
-
- getGraphManager().registerNode(id, localObject);
-
- if (prototype != null
- && ((Persistent) prototype).getPersistenceState() !=
PersistenceState.HOLLOW) {
-
localObject.setPersistenceState(PersistenceState.COMMITTED);
-
- // *** the only line different from CayenneContext
implementation. Not
- // clear why it is different?
- descriptor.injectValueHolders(localObject);
- descriptor.shallowMerge(prototype, localObject);
- }
- else {
- localObject.setPersistenceState(PersistenceState.HOLLOW);
- }
-
- return localObject;
- }
- }
- }
-
@Override
protected void fireDataChannelChanged(Object postedBy, GraphDiff changes) {
super.fireDataChannelChanged(postedBy, changes);
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectDescriptor.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectDescriptor.java?rev=1234364&r1=1234363&r2=1234364&view=diff
==============================================================================
---
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectDescriptor.java
(original)
+++
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectDescriptor.java
Sat Jan 21 17:09:02 2012
@@ -35,6 +35,9 @@ class DataObjectDescriptor extends Persi
@Override
public void shallowMerge(Object from, Object to) throws PropertyException {
+
+ injectValueHolders(to);
+
super.shallowMerge(from, to);
if (from instanceof DataObject && to instanceof DataObject) {