cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r596548 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/reflect/ test/java/org/apache/cayenne/access/ test/java/org/apache/cayenne/testdo/embeddable/auto/
Date Tue, 20 Nov 2007 06:26:13 GMT
Author: aadamchik
Date: Mon Nov 19 22:26:11 2007
New Revision: 596548

URL: http://svn.apache.org/viewvc?rev=596548&view=rev
Log:
CAY-735 Embeddable class support by Cayenne runtime
(supporting updates of embedded properties)

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/EmbeddableDescriptor.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/FieldEmbeddableDescriptor.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/EmbeddedFieldAccessor.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/EmbeddingTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_Embeddable1.java

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/EmbeddableDescriptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/EmbeddableDescriptor.java?rev=596548&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/EmbeddableDescriptor.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/EmbeddableDescriptor.java
Mon Nov 19 22:26:11 2007
@@ -0,0 +1,43 @@
+/*****************************************************************
+ *   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.reflect;
+
+import org.apache.cayenne.map.Embeddable;
+
+/**
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+public interface EmbeddableDescriptor {
+
+    /**
+     * Creates a new instance of an embeddable class described by this object.
+     */
+    Object createObject(Object owner, String embeddedProperty);
+
+    /**
+     * Returns an embeddable class mapped by this descriptor.
+     */
+    Class<?> getObjectClass();
+
+    /**
+     * Returns a metadata object for this descriptor.
+     */
+    Embeddable getEmbeddable();
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/EmbeddedFieldAccessor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/EmbeddedFieldAccessor.java?rev=596548&r1=596547&r2=596548&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/EmbeddedFieldAccessor.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/EmbeddedFieldAccessor.java
Mon Nov 19 22:26:11 2007
@@ -18,8 +18,6 @@
  ****************************************************************/
 package org.apache.cayenne.reflect;
 
-import org.apache.cayenne.CayenneRuntimeException;
-
 /**
  * An accessor for fields storing embedded objects. This accessor will initialize null
  * fields with appropriate embeddable objects when needed during get and set calls.
@@ -32,14 +30,14 @@
     protected String propertyPath;
     protected Accessor embeddedAccessor;
     protected Accessor embeddableAccessor;
-    protected Class embeddedClass;
+    protected EmbeddableDescriptor embeddableDescriptor;
 
-    public EmbeddedFieldAccessor(Class embeddableClass, Accessor embeddedAccessor,
-            Accessor embeddableAccessor) {
+    public EmbeddedFieldAccessor(EmbeddableDescriptor embeddableDescriptor,
+            Accessor embeddedAccessor, Accessor embeddableAccessor) {
         this.propertyPath = embeddedAccessor.getName()
                 + "."
                 + embeddableAccessor.getName();
-        this.embeddedClass = embeddableClass;
+        this.embeddableDescriptor = embeddableDescriptor;
         this.embeddableAccessor = embeddableAccessor;
         this.embeddedAccessor = embeddedAccessor;
     }
@@ -63,26 +61,11 @@
     protected Object getEmbeddable(Object owner) {
         Object embeddable = embeddedAccessor.getValue(owner);
         if (embeddable == null) {
-            embeddable = createEmbeddable();
+            embeddable = embeddableDescriptor.createObject(owner, embeddedAccessor
+                    .getName());
             embeddedAccessor.setValue(owner, embeddable);
         }
 
         return embeddable;
-    }
-
-    protected Object createEmbeddable() {
-        if (embeddedClass == null) {
-            throw new NullPointerException(
-                    "Null embedded objectClass. Accessor wasn't initialized properly.");
-        }
-
-        try {
-            return embeddedClass.newInstance();
-        }
-        catch (Throwable e) {
-            throw new CayenneRuntimeException("Error creating embedded object of class '"
-                    + embeddedClass.getName()
-                    + "'", e);
-        }
     }
 }

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/FieldEmbeddableDescriptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/FieldEmbeddableDescriptor.java?rev=596548&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/FieldEmbeddableDescriptor.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/FieldEmbeddableDescriptor.java
Mon Nov 19 22:26:11 2007
@@ -0,0 +1,80 @@
+/*****************************************************************
+ *   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.reflect;
+
+import org.apache.cayenne.Persistent;
+import org.apache.cayenne.map.Embeddable;
+
+/**
+ * A default POJO embeddable descriptor.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+public class FieldEmbeddableDescriptor implements EmbeddableDescriptor {
+
+    protected Class<?> embeddableClass;
+    protected Embeddable embeddable;
+    protected Accessor ownerAccessor;
+    protected Accessor embeddedPropertyAccessor;
+
+    public FieldEmbeddableDescriptor(Embeddable embeddable) {
+        this.embeddable = embeddable;
+        try {
+            this.embeddableClass = Class.forName(embeddable.getClassName(), true, Thread
+                    .currentThread()
+                    .getContextClassLoader());
+        }
+        catch (ClassNotFoundException e) {
+            throw new PropertyException("Class not found", e);
+        }
+
+        this.ownerAccessor = new FieldAccessor(embeddableClass, "owner", Persistent.class);
+        this.embeddedPropertyAccessor = new FieldAccessor(
+                embeddableClass,
+                "embeddedProperty",
+                String.class);
+    }
+
+    public Object createObject(Object owner, String embeddedProperty) {
+        Object embeddable;
+        try {
+            embeddable = embeddableClass.newInstance();
+        }
+        catch (Throwable e) {
+            throw new PropertyException("Error creating embeddable object of class '"
+                    + embeddableClass.getName()
+                    + "'", e);
+        }
+
+        ownerAccessor.setValue(embeddable, owner);
+        embeddedPropertyAccessor.setValue(embeddable, embeddedProperty);
+        return embeddable;
+
+    }
+
+    public Embeddable getEmbeddable() {
+        return embeddable;
+    }
+
+    public Class<?> getObjectClass() {
+        return embeddableClass;
+    }
+
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java?rev=596548&r1=596547&r2=596548&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java
Mon Nov 19 22:26:11 2007
@@ -163,8 +163,13 @@
                 embeddableName,
                 attribute.getJavaClass());
 
+        // TODO: andrus, 11/19/2007 = avoid creation of descriptor for every property of
+        // embeddable; look up reusable descriptor instead.
+        EmbeddableDescriptor embeddableDescriptor = new FieldEmbeddableDescriptor(
+                embeddedAttribute.getEmbeddable());
+
         Accessor accessor = new EmbeddedFieldAccessor(
-                embeddableClass,
+                embeddableDescriptor,
                 embeddedAccessor,
                 embeddedableAccessor);
         descriptor.addDeclaredProperty(new SimpleAttributeProperty(

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/EmbeddingTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/EmbeddingTest.java?rev=596548&r1=596547&r2=596548&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/EmbeddingTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/EmbeddingTest.java
Mon Nov 19 22:26:11 2007
@@ -23,6 +23,7 @@
 import org.apache.cayenne.DataObjectUtils;
 import org.apache.cayenne.DataRow;
 import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.PersistenceState;
 import org.apache.cayenne.query.SelectQuery;
 import org.apache.cayenne.testdo.embeddable.EmbedEntity1;
 import org.apache.cayenne.testdo.embeddable.Embeddable1;
@@ -80,7 +81,7 @@
         deleteTestData();
 
         ObjectContext context = createDataContext();
-        EmbedEntity1 o1 = (EmbedEntity1) context.newObject(EmbedEntity1.class);
+        EmbedEntity1 o1 = context.newObject(EmbedEntity1.class);
         o1.setName("NAME");
 
         Embeddable1 e1 = new Embeddable1();
@@ -93,12 +94,12 @@
         Embeddable1 e2 = new Embeddable1();
         o1.setEmbedded2(e2);
 
-        // init after it was set on the ownig object
+        // init after it was set on the owning object
         e2.setEmbedded10("E21");
         e2.setEmbedded20("E22");
 
         context.commitChanges();
-        
+
         SelectQuery query = new SelectQuery(EmbedEntity1.class);
         query.setFetchingDataRows(true);
         DataRow row = (DataRow) DataObjectUtils.objectForQuery(context, query);
@@ -109,27 +110,51 @@
         assertEquals("E22", row.get("EMBEDDED40"));
     }
 
-//    public void testUpdateEmbeddedProperties() throws Exception {
-//        createTestData("testUpdate");
-//
-//        SelectQuery query = new SelectQuery(EmbedEntity1.class);
-//        query.addOrdering(EmbedEntity1.NAME_PROPERTY, true);
-//
-//        ObjectContext context = createDataContext();
-//        List results = context.performQuery(query);
-//        EmbedEntity1 o1 = (EmbedEntity1) results.get(0);
-//
-//        assertEquals("n1", o1.getName());
-//        Embeddable1 e11 = o1.getEmbedded1();
-//        e11.setEmbedded10("x1");
-//        
-//        assertEquals(PersistenceState.MODIFIED, o1.getPersistenceState());
-//        
-//        context.commitChanges();
-//        SelectQuery query1 = new SelectQuery(EmbedEntity1.class);
-//        query1.setFetchingDataRows(true);
-//        DataRow row = (DataRow) DataObjectUtils.objectForQuery(context, query1);
-//        assertNotNull(row);
-//        assertEquals("x1", row.get("EMBEDDED10"));
-//    }
+    public void testUpdateEmbeddedProperties() throws Exception {
+        createTestData("testUpdate");
+
+        SelectQuery query = new SelectQuery(EmbedEntity1.class);
+        query.addOrdering(EmbedEntity1.NAME_PROPERTY, true);
+
+        ObjectContext context = createDataContext();
+        List results = context.performQuery(query);
+        EmbedEntity1 o1 = (EmbedEntity1) results.get(0);
+
+        Embeddable1 e11 = o1.getEmbedded1();
+        e11.setEmbedded10("x1");
+
+        assertEquals(PersistenceState.MODIFIED, o1.getPersistenceState());
+
+        context.commitChanges();
+        SelectQuery query1 = new SelectQuery(EmbedEntity1.class);
+        query1.setFetchingDataRows(true);
+        DataRow row = (DataRow) DataObjectUtils.objectForQuery(context, query1);
+        assertNotNull(row);
+        assertEquals("x1", row.get("EMBEDDED10"));
+    }
+
+    public void testUpdateEmbedded() throws Exception {
+        createTestData("testUpdate");
+
+        SelectQuery query = new SelectQuery(EmbedEntity1.class);
+        query.addOrdering(EmbedEntity1.NAME_PROPERTY, true);
+
+        ObjectContext context = createDataContext();
+        List results = context.performQuery(query);
+        EmbedEntity1 o1 = (EmbedEntity1) results.get(0);
+
+        Embeddable1 e11 = new Embeddable1();
+        e11.setEmbedded10("x1");
+        e11.setEmbedded20("x2");
+        o1.setEmbedded1(e11);
+
+        assertEquals(PersistenceState.MODIFIED, o1.getPersistenceState());
+
+        context.commitChanges();
+        SelectQuery query1 = new SelectQuery(EmbedEntity1.class);
+        query1.setFetchingDataRows(true);
+        DataRow row = (DataRow) DataObjectUtils.objectForQuery(context, query1);
+        assertNotNull(row);
+        assertEquals("x1", row.get("EMBEDDED10"));
+    }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_Embeddable1.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_Embeddable1.java?rev=596548&r1=596547&r2=596548&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_Embeddable1.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_Embeddable1.java
Mon Nov 19 22:26:11 2007
@@ -18,8 +18,13 @@
  ****************************************************************/
 package org.apache.cayenne.testdo.embeddable.auto;
 
+import org.apache.cayenne.Persistent;
+
 public class _Embeddable1 {
 
+    private Persistent owner;
+    private String embeddedProperty;
+
     protected String embedded10;
     protected String embedded20;
 
@@ -28,6 +33,7 @@
     }
 
     public void setEmbedded10(String embedded10) {
+        propertyWillChange("embdedded10", this.embedded10, embedded10);
         this.embedded10 = embedded10;
     }
 
@@ -36,6 +42,17 @@
     }
 
     public void setEmbedded20(String embedded20) {
+        propertyWillChange("embdedded20", this.embedded20, embedded20);
         this.embedded20 = embedded20;
+    }
+
+    protected void propertyWillChange(String property, Object oldValue, Object newValue)
{
+        if (owner != null && owner.getObjectContext() != null) {
+            owner.getObjectContext().propertyChanged(
+                    owner,
+                    embeddedProperty + "." + property,
+                    oldValue,
+                    newValue);
+        }
     }
 }



Mime
View raw message