You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2013/08/04 20:47:17 UTC
svn commit: r1510305 - in
/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne:
access/ reflect/ reflect/generic/
Author: aadamchik
Date: Sun Aug 4 18:47:16 2013
New Revision: 1510305
URL: http://svn.apache.org/r1510305
Log:
CAY-1861 Remove runtime relationships
tracking complimentary FK change for one-way to-many
Modified:
cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectDiff.java
cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java
cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ArcProperty.java
cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/BaseArcProperty.java
cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectToManyProperty.java
cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectToOneProperty.java
Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectDiff.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectDiff.java?rev=1510305&r1=1510304&r2=1510305&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectDiff.java (original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectDiff.java Sun Aug 4 18:47:16 2013
@@ -29,9 +29,12 @@ import org.apache.cayenne.Fault;
import org.apache.cayenne.ObjectId;
import org.apache.cayenne.PersistenceState;
import org.apache.cayenne.Persistent;
+import org.apache.cayenne.exp.parser.ASTDbPath;
import org.apache.cayenne.graph.GraphChangeHandler;
import org.apache.cayenne.graph.GraphDiff;
import org.apache.cayenne.graph.NodeDiff;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbRelationship;
import org.apache.cayenne.map.EntityResolver;
import org.apache.cayenne.map.ObjEntity;
import org.apache.cayenne.map.ObjRelationship;
@@ -59,6 +62,7 @@ class ObjectDiff extends NodeDiff {
private Map<String, Object> arcSnapshot;
private Map<String, Object> currentArcSnapshot;
private Map<ArcOperation, ArcOperation> flatIds;
+ private Map<ArcOperation, ArcOperation> phantomFks;
private Persistent object;
@@ -180,7 +184,7 @@ class ObjectDiff extends NodeDiff {
});
}
- void addDiff(NodeDiff diff) {
+ void addDiff(NodeDiff diff, ObjectStore parent) {
boolean addDiff = true;
@@ -197,7 +201,9 @@ class ObjectDiff extends NodeDiff {
// so we cant't do 'instanceof SingleObjectArcProperty'
// TODO: andrus, 3.22.2006 - should we consider this a bug?
- if (property instanceof ToManyProperty) {
+ if (property == null && arcId.startsWith(ASTDbPath.DB_PREFIX)) {
+ addDiff = addPhantomFkDiff(arcDiff);
+ } else if (property instanceof ToManyProperty) {
// record flattened op changes
@@ -219,7 +225,15 @@ class ObjectDiff extends NodeDiff {
otherDiffs.remove(oldOp);
}
}
+ } else if (property.getComplimentaryReverseArc() == null) {
+
+ // register complimentary arc diff
+ String arc = ASTDbPath.DB_PREFIX + property.getComplimentaryReverseDbRelationshipPath();
+ ArcOperation complimentartyOp = new ArcOperation(targetId, arcDiff.getNodeId(), arc,
+ arcDiff.isDelete());
+ parent.registerDiff(targetId, complimentartyOp);
}
+
} else if (property instanceof ToOneProperty) {
if (currentArcSnapshot == null) {
@@ -243,6 +257,44 @@ class ObjectDiff extends NodeDiff {
}
}
+ private boolean addPhantomFkDiff(ArcOperation arcDiff) {
+ String arcId = arcDiff.getArcId().toString();
+
+ DbEntity dbEntity = classDescriptor.getEntity().getDbEntity();
+ DbRelationship dbRelationship = (DbRelationship) dbEntity.getRelationship(arcId.substring(ASTDbPath.DB_PREFIX
+ .length()));
+
+ if (dbRelationship.isToMany()) {
+ return false;
+ }
+
+ boolean addDiff = true;
+
+ if (currentArcSnapshot == null) {
+ currentArcSnapshot = new HashMap<String, Object>();
+ }
+
+ currentArcSnapshot.put(arcId, arcDiff.getTargetNodeId());
+
+ if (phantomFks == null) {
+ phantomFks = new HashMap<ArcOperation, ArcOperation>();
+ }
+
+ ArcOperation oldOp = phantomFks.put(arcDiff, arcDiff);
+
+ // "delete" cancels "create" and vice versa...
+ if (oldOp != null && oldOp.isDelete() != arcDiff.isDelete()) {
+ addDiff = false;
+ phantomFks.remove(arcDiff);
+
+ if (otherDiffs != null) {
+ otherDiffs.remove(oldOp);
+ }
+ }
+
+ return addDiff;
+ }
+
/**
* Checks whether at least a single property is modified.
*/
@@ -258,6 +310,10 @@ class ObjectDiff extends NodeDiff {
return false;
}
+ if (phantomFks != null && !phantomFks.isEmpty()) {
+ return false;
+ }
+
int state = object.getPersistenceState();
if (state == PersistenceState.NEW || state == PersistenceState.DELETED) {
return false;
Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java?rev=1510305&r1=1510304&r2=1510305&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java (original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java Sun Aug 4 18:47:16 2013
@@ -211,7 +211,7 @@ public class ObjectStore implements Seri
}
if (diff != null) {
- objectDiff.addDiff(diff);
+ objectDiff.addDiff(diff, this);
}
return objectDiff;
Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ArcProperty.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ArcProperty.java?rev=1510305&r1=1510304&r2=1510305&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ArcProperty.java (original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ArcProperty.java Sun Aug 4 18:47:16 2013
@@ -19,11 +19,12 @@
package org.apache.cayenne.reflect;
+import org.apache.cayenne.map.DbRelationship;
import org.apache.cayenne.map.ObjRelationship;
/**
- * A Property that represents an "arc" connecting source node to the target node in the
- * graph.
+ * A Property that represents an "arc" connecting source node to the target node
+ * in the graph.
*
* @since 1.2
*/
@@ -37,23 +38,34 @@ public interface ArcProperty extends Pro
ObjRelationship getRelationship();
/**
- * Returns a complimentary reverse ArcProperty or null if no reverse arc exists.
+ * Returns a path over reverse DbRelationships for this arc's
+ * ObjRelationship.
+ *
+ * @since 3.2
+ */
+ String getComplimentaryReverseDbRelationshipPath();
+
+ /**
+ * Returns a complimentary reverse ArcProperty or null if no reverse arc
+ * exists.
*/
ArcProperty getComplimentaryReverseArc();
/**
- * Returns a ClassDescriptor for the type of graph nodes pointed to by this arc
- * property. Note that considering that a target object may be a subclass of the class
- * handled by the descriptor, users of this method may need to call
- * {@link ClassDescriptor#getSubclassDescriptor(Class)} before using the descriptor to
- * access objects.
+ * Returns a ClassDescriptor for the type of graph nodes pointed to by this
+ * arc property. Note that considering that a target object may be a
+ * subclass of the class handled by the descriptor, users of this method may
+ * need to call {@link ClassDescriptor#getSubclassDescriptor(Class)} before
+ * using the descriptor to access objects.
*/
ClassDescriptor getTargetDescriptor();
/**
- * Returns whether a target node connected to a given object is an unresolved fault.
+ * Returns whether a target node connected to a given object is an
+ * unresolved fault.
*
- * @param source an object that is a source object of the relationship.
+ * @param source
+ * an object that is a source object of the relationship.
*/
boolean isFault(Object source);
Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/BaseArcProperty.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/BaseArcProperty.java?rev=1510305&r1=1510304&r2=1510305&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/BaseArcProperty.java (original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/BaseArcProperty.java Sun Aug 4 18:47:16 2013
@@ -30,6 +30,7 @@ public abstract class BaseArcProperty ex
protected String complimentaryReverseArcName;
protected ClassDescriptor targetDescriptor;
protected ObjRelationship relationship;
+ protected String reverseDbPath;
public BaseArcProperty(ClassDescriptor owner, ClassDescriptor targetDescriptor, Accessor accessor,
String reverseName) {
@@ -45,6 +46,15 @@ public abstract class BaseArcProperty ex
public abstract boolean visit(PropertyVisitor visitor);
public abstract boolean isFault(Object source);
+
+ @Override
+ public String getComplimentaryReverseDbRelationshipPath() {
+ if (reverseDbPath == null) {
+ reverseDbPath = relationship.getReverseDbRelationshipPath();
+ }
+
+ return reverseDbPath;
+ }
public ObjRelationship getRelationship() {
return relationship;
Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectToManyProperty.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectToManyProperty.java?rev=1510305&r1=1510304&r2=1510305&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectToManyProperty.java (original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectToManyProperty.java Sun Aug 4 18:47:16 2013
@@ -36,11 +36,11 @@ class DataObjectToManyProperty extends D
protected ObjRelationship relationship;
protected String reverseName;
+ protected String reverseDbPath;
protected ClassDescriptor targetDescriptor;
protected Fault fault;
- DataObjectToManyProperty(ObjRelationship relationship,
- ClassDescriptor targetDescriptor, Fault fault) {
+ DataObjectToManyProperty(ObjRelationship relationship, ClassDescriptor targetDescriptor, Fault fault) {
this.relationship = relationship;
this.targetDescriptor = targetDescriptor;
this.reverseName = relationship.getReverseRelationshipName();
@@ -48,8 +48,7 @@ class DataObjectToManyProperty extends D
}
public ArcProperty getComplimentaryReverseArc() {
- return reverseName != null ? (ArcProperty) targetDescriptor
- .getProperty(reverseName) : null;
+ return reverseName != null ? (ArcProperty) targetDescriptor.getProperty(reverseName) : null;
}
public ClassDescriptor getTargetDescriptor() {
@@ -57,6 +56,15 @@ class DataObjectToManyProperty extends D
}
@Override
+ public String getComplimentaryReverseDbRelationshipPath() {
+ if (reverseDbPath == null) {
+ reverseDbPath = relationship.getReverseDbRelationshipPath();
+ }
+
+ return reverseDbPath;
+ }
+
+ @Override
public String getName() {
return relationship.getName();
}
@@ -65,40 +73,26 @@ class DataObjectToManyProperty extends D
return relationship;
}
- public void addTarget(Object source, Object target, boolean setReverse)
- throws PropertyException {
+ public void addTarget(Object source, Object target, boolean setReverse) throws PropertyException {
try {
- toDataObject(source).addToManyTarget(
- getName(),
- toDataObject(target),
- setReverse);
- }
- catch (Throwable th) {
- throw new PropertyException("Error setting to-many DataObject property: "
- + getName(), this, source, th);
+ toDataObject(source).addToManyTarget(getName(), toDataObject(target), setReverse);
+ } catch (Throwable th) {
+ throw new PropertyException("Error setting to-many DataObject property: " + getName(), this, source, th);
}
}
- public void removeTarget(Object source, Object target, boolean setReverse)
- throws PropertyException {
+ public void removeTarget(Object source, Object target, boolean setReverse) throws PropertyException {
try {
- toDataObject(source).removeToManyTarget(
- getName(),
- toDataObject(target),
- setReverse);
- }
- catch (Throwable th) {
- throw new PropertyException("Error unsetting to-many DataObject property: "
- + getName(), this, source, th);
+ toDataObject(source).removeToManyTarget(getName(), toDataObject(target), setReverse);
+ } catch (Throwable th) {
+ throw new PropertyException("Error unsetting to-many DataObject property: " + getName(), this, source, th);
}
}
@Override
public void injectValueHolder(Object object) throws PropertyException {
if (readPropertyDirectly(object) == null) {
- writePropertyDirectly(object, null, fault.resolveFault(
- (Persistent) object,
- getName()));
+ writePropertyDirectly(object, null, fault.resolveFault((Persistent) object, getName()));
}
}
@@ -115,22 +109,18 @@ class DataObjectToManyProperty extends D
Object value = readPropertyDirectly(object);
if (value instanceof Fault) {
// nothing to do
- }
- else if (value instanceof ValueHolder) {
+ } else if (value instanceof ValueHolder) {
((ValueHolder) value).invalidate();
- }
- else {
+ } else {
writePropertyDirectly(object, null, fault);
}
}
- public void addTargetDirectly(Object source, Object target)
- throws PropertyException {
+ public void addTargetDirectly(Object source, Object target) throws PropertyException {
addTarget(source, target, false);
}
- public void removeTargetDirectly(Object source, Object target)
- throws PropertyException {
+ public void removeTargetDirectly(Object source, Object target) throws PropertyException {
removeTarget(source, target, false);
}
}
Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectToOneProperty.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectToOneProperty.java?rev=1510305&r1=1510304&r2=1510305&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectToOneProperty.java (original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/generic/DataObjectToOneProperty.java Sun Aug 4 18:47:16 2013
@@ -36,6 +36,7 @@ class DataObjectToOneProperty extends Da
protected ObjRelationship relationship;
protected String reverseName;
+ protected String reverseDbPath;
protected ClassDescriptor targetDescriptor;
protected Fault fault;
@@ -51,6 +52,15 @@ class DataObjectToOneProperty extends Da
return reverseName != null ? (ArcProperty) targetDescriptor
.getProperty(reverseName) : null;
}
+
+ @Override
+ public String getComplimentaryReverseDbRelationshipPath() {
+ if (reverseDbPath == null) {
+ reverseDbPath = relationship.getReverseDbRelationshipPath();
+ }
+
+ return reverseDbPath;
+ }
public ClassDescriptor getTargetDescriptor() {
return targetDescriptor;