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 2011/03/22 10:27:20 UTC

svn commit: r1084112 - in /cayenne/main/trunk: docs/doc/src/main/resources/ framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/ framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/update/...

Author: aadamchik
Date: Tue Mar 22 09:27:20 2011
New Revision: 1084112

URL: http://svn.apache.org/viewvc?rev=1084112&view=rev
Log:
 CAY-1547 cayenne-lifecycle: support for setting UuidRelationships

support for JIT UUID creation on commit

Added:
    cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/update/
    cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/update/UuidPropagatedValueFactory.java
Modified:
    cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
    cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipFilter.java
    cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipHandler.java
    cayenne/main/trunk/framework/cayenne-lifecycle/src/test/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipHandlerTest.java

Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt?rev=1084112&r1=1084111&r2=1084112&view=diff
==============================================================================
--- cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt (original)
+++ cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt Tue Mar 22 09:27:20 2011
@@ -15,6 +15,7 @@ Changes/New Features Since 3.1M2:
 
 CAY-1544 Remove jdk1.6 module from Cayenne sources
 CAY-1545 cayenne-lifecycle Referenceable handler refactoring
+CAY-1547 cayenne-lifecycle: support for setting UuidRelationships
 CAY-1549 Migrate BatchQueryBuilderFactory to DI
 
 Bug Fixes Since 3.1M2:

Modified: cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipFilter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipFilter.java?rev=1084112&r1=1084111&r2=1084112&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipFilter.java (original)
+++ cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipFilter.java Tue Mar 22 09:27:20 2011
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.lifecycle.relationship;
 
+import java.util.Collections;
+
 import org.apache.cayenne.DataChannel;
 import org.apache.cayenne.DataChannelFilter;
 import org.apache.cayenne.DataChannelFilterChain;
@@ -26,6 +28,7 @@ import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.QueryResponse;
 import org.apache.cayenne.annotation.PostLoad;
 import org.apache.cayenne.annotation.PostPersist;
+import org.apache.cayenne.annotation.PostUpdate;
 import org.apache.cayenne.graph.GraphDiff;
 import org.apache.cayenne.query.Query;
 
@@ -51,7 +54,7 @@ public class UuidRelationshipFilter impl
             GraphDiff diff,
             int syncType,
             DataChannelFilterChain chain) {
-        // noop for now
+        // noop ... all work is done via listeners...
         return chain.onSync(context, diff, syncType);
     }
 
@@ -68,12 +71,18 @@ public class UuidRelationshipFilter impl
         }
     }
 
+    @PostUpdate(entityAnnotations = UuidRelationship.class)
+    @PostPersist(entityAnnotations = UuidRelationship.class)
+    void postCommit(DataObject object) {
+        // invalidate after commit to ensure UUID property is re-read...
+        object.getObjectContext().invalidateObjects(Collections.singleton(object));
+    }
+
     /**
      * A lifecycle callback method that delegates object post load event processing to the
      * underlying faulting strategy.
      */
     @PostLoad(entityAnnotations = UuidRelationship.class)
-    @PostPersist(entityAnnotations = UuidRelationship.class)
     void postLoad(DataObject object) {
         faultingStrategy.afterObjectLoaded(object);
     }

Modified: cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipHandler.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipHandler.java?rev=1084112&r1=1084111&r2=1084112&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipHandler.java (original)
+++ cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipHandler.java Tue Mar 22 09:27:20 2011
@@ -20,6 +20,7 @@ package org.apache.cayenne.lifecycle.rel
 
 import org.apache.cayenne.DataObject;
 import org.apache.cayenne.lifecycle.ref.ReferenceableHandler;
+import org.apache.cayenne.lifecycle.relationship.update.UuidPropagatedValueFactory;
 
 /**
  * @since 3.1
@@ -77,15 +78,20 @@ public class UuidRelationshipHandler {
                         "'from' and 'to' objects are registered in different ObjectContexts");
             }
 
+            from.writePropertyDirectly(relationship, to);
+
             if (to.getObjectId().isTemporary()
                     && !to.getObjectId().isReplacementIdAttached()) {
-                throw new UnsupportedOperationException("TODO - eager ID generation");
-            }
-
-            String uuid = referenceableHandler.getUuid(to);
 
-            from.writeProperty(property, uuid);
-            from.writePropertyDirectly(relationship, to);
+                // defer UUID resolving till commit
+                from.writeProperty(property, new UuidPropagatedValueFactory(
+                        referenceableHandler,
+                        to));
+            }
+            else {
+                String uuid = referenceableHandler.getUuid(to);
+                from.writeProperty(property, uuid);
+            }
         }
         else {
             from.writeProperty(property, null);

Added: cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/update/UuidPropagatedValueFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/update/UuidPropagatedValueFactory.java?rev=1084112&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/update/UuidPropagatedValueFactory.java (added)
+++ cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/relationship/update/UuidPropagatedValueFactory.java Tue Mar 22 09:27:20 2011
@@ -0,0 +1,40 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.lifecycle.relationship.update;
+
+import org.apache.cayenne.lifecycle.ref.ReferenceableHandler;
+import org.apache.commons.collections.Factory;
+
+/**
+ * @since 3.1
+ */
+public class UuidPropagatedValueFactory implements Factory {
+
+    private ReferenceableHandler referenceableHandler;
+    private Object to;
+
+    public UuidPropagatedValueFactory(ReferenceableHandler referenceableHandler, Object to) {
+        this.referenceableHandler = referenceableHandler;
+        this.to = to;
+    }
+
+    public Object create() {
+        return referenceableHandler.getUuid(to);
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-lifecycle/src/test/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipHandlerTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-lifecycle/src/test/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipHandlerTest.java?rev=1084112&r1=1084111&r2=1084112&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-lifecycle/src/test/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipHandlerTest.java (original)
+++ cayenne/main/trunk/framework/cayenne-lifecycle/src/test/java/org/apache/cayenne/lifecycle/relationship/UuidRelationshipHandlerTest.java Tue Mar 22 09:27:20 2011
@@ -41,6 +41,12 @@ public class UuidRelationshipHandlerTest
     protected void setUp() throws Exception {
         runtime = new ServerRuntime("cayenne-lifecycle.xml");
 
+        // a filter is required to invalidate root objects after commit
+        UuidRelationshipFilter filter = new UuidRelationshipFilter();
+        runtime.getDataDomain().addFilter(filter);
+        runtime.getDataDomain().getEntityResolver().getCallbackRegistry().addListener(
+                filter);
+
         DBHelper dbHelper = new DBHelper(runtime.getDataSource(null));
 
         rootTable = new TableHelper(dbHelper, "UUID_ROOT1").setColumns("ID", "UUID");
@@ -94,7 +100,40 @@ public class UuidRelationshipHandlerTest
 
         context.commitChanges();
 
+        int id = Cayenne.intPKForObject(e1);
+
         Object[] r1x = rootTable.select();
-        assertEquals("E1:1", r1x[1]);
+        assertEquals("E1:" + id, r1x[1]);
+        assertEquals("E1:" + id, r1.getUuid());
+    }
+
+    public void testRelate_Change() throws Exception {
+
+        e1Table.insert(1);
+        rootTable.insert(1, "E1:1");
+
+        ObjectContext context = runtime.getContext();
+
+        UuidRoot1 r1 = Cayenne.objectForPK(context, UuidRoot1.class, 1);
+        assertEquals("E1:1", r1.getUuid());
+
+        E1 e1 = context.newObject(E1.class);
+
+        ReferenceableHandler refHandler = new ReferenceableHandler(context
+                .getEntityResolver());
+        UuidRelationshipHandler handler = new UuidRelationshipHandler(refHandler);
+        handler.relate(r1, e1);
+
+        assertSame(e1, r1.readPropertyDirectly("cay:related:uuid"));
+
+        context.commitChanges();
+
+        int id = Cayenne.intPKForObject(e1);
+        assertFalse(1 == id);
+
+        Object[] r1x = rootTable.select();
+        assertEquals("E1:" + id, r1x[1]);
+        assertEquals("E1:" + id, r1.getUuid());
+        assertSame(e1, r1.readProperty("cay:related:uuid"));
     }
 }