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 2007/01/29 15:02:04 UTC

svn commit: r501054 - in /cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src: main/java/org/apache/cayenne/access/jdbc/ main/java/org/apache/cayenne/access/trans/ main/java/org/apache/cayenne/reflect/ test/java/org/apache/cayenne/access/ test/...

Author: aadamchik
Date: Mon Jan 29 06:02:03 2007
New Revision: 501054

URL: http://svn.apache.org/viewvc?view=rev&rev=501054
Log:
CAY-736: Embeddable class support by Cayenne runtime
(refactoring to handle embeddable attributes in select queries)

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/DataRowPostProcessor.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/ClassDescriptor.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/LazyClassDescriptorDecorator.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptor.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/EmbeddingTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/reflect/MockClassDescriptor.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/DataRowPostProcessor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/DataRowPostProcessor.java?view=diff&rev=501054&r1=501053&r2=501054
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/DataRowPostProcessor.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/DataRowPostProcessor.java Mon Jan 29 06:02:03 2007
@@ -92,14 +92,7 @@
                     .getExtendedTypes()
                     .getRegisteredType(attribute.getType());
 
-            Collection overrides = null;
-
-            if (columnOverrides == null) {
-                columnOverrides = new HashMap(2);
-            }
-            else {
-                overrides = (Collection) columnOverrides.get(entity.getName());
-            }
+            Collection overrides = (Collection) columnOverrides.get(entity.getName());
 
             if (overrides == null) {
                 overrides = new ArrayList(3);
@@ -110,8 +103,8 @@
         }
 
         // inject null post-processor
-        return columnOverrides != null ? new DataRowPostProcessor(translator
-                .getRootInheritanceTree(), columnOverrides) : null;
+        return columnOverrides.isEmpty() ? null : new DataRowPostProcessor(translator
+                .getRootInheritanceTree(), columnOverrides);
     }
 
     private DataRowPostProcessor(EntityInheritanceTree inheritanceTree,

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java?view=diff&rev=501054&r1=501053&r2=501054
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java Mon Jan 29 06:02:03 2007
@@ -38,13 +38,18 @@
 import org.apache.cayenne.map.DbJoin;
 import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.map.DerivedDbEntity;
-import org.apache.cayenne.map.EntityInheritanceTree;
 import org.apache.cayenne.map.ObjAttribute;
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.query.PrefetchSelectQuery;
 import org.apache.cayenne.query.PrefetchTreeNode;
 import org.apache.cayenne.query.SelectQuery;
+import org.apache.cayenne.reflect.ArcProperty;
+import org.apache.cayenne.reflect.AttributeProperty;
+import org.apache.cayenne.reflect.ClassDescriptor;
+import org.apache.cayenne.reflect.PropertyVisitor;
+import org.apache.cayenne.reflect.ToManyProperty;
+import org.apache.cayenne.reflect.ToOneProperty;
 
 /**
  * A builder of JDBC PreparedStatements based on Cayenne SelectQueries. Translates
@@ -334,9 +339,9 @@
      * Appends columns needed for object SelectQuery to the provided columns list.
      */
     // TODO: this whole method screams REFACTORING!!!
-    List appendQueryColumns(List columns, SelectQuery query) {
+    List appendQueryColumns(final List columns, SelectQuery query) {
 
-        Set attributes = new HashSet();
+        final Set attributes = new HashSet();
 
         // fetched attributes include attributes that are either:
         // 
@@ -346,59 +351,67 @@
         // * GROUP BY
         // * joined prefetch PK
 
-        ObjEntity oe = getRootEntity();
-
-        // null tree will indicate that we don't take inheritance into account
-        EntityInheritanceTree tree = null;
-
-        if (query.isResolvingInherited()) {
-            tree = getRootInheritanceTree();
-        }
-
-        // ObjEntity attrs
-        Iterator attrs = (tree != null) ? tree.allAttributes().iterator() : oe
-                .getAttributes()
-                .iterator();
-        while (attrs.hasNext()) {
-            ObjAttribute oa = (ObjAttribute) attrs.next();
-            Iterator dbPathIterator = oa.getDbPathIterator();
-            while (dbPathIterator.hasNext()) {
-                Object pathPart = dbPathIterator.next();
-                if (pathPart instanceof DbRelationship) {
-                    DbRelationship rel = (DbRelationship) pathPart;
-                    dbRelationshipAdded(rel);
-                }
-                else if (pathPart instanceof DbAttribute) {
-                    DbAttribute dbAttr = (DbAttribute) pathPart;
-                    if (dbAttr == null) {
-                        throw new CayenneRuntimeException(
-                                "ObjAttribute has no DbAttribute: " + oa.getName());
+        ClassDescriptor descriptor = query
+                .getMetaData(getEntityResolver())
+                .getClassDescriptor();
+        ObjEntity oe = descriptor.getEntity();
+
+        PropertyVisitor visitor = new PropertyVisitor() {
+
+            public boolean visitAttribute(AttributeProperty property) {
+                ObjAttribute oa = property.getAttribute();
+                Iterator dbPathIterator = oa.getDbPathIterator();
+                while (dbPathIterator.hasNext()) {
+                    Object pathPart = dbPathIterator.next();
+                    if (pathPart instanceof DbRelationship) {
+                        DbRelationship rel = (DbRelationship) pathPart;
+                        dbRelationshipAdded(rel);
                     }
+                    else if (pathPart instanceof DbAttribute) {
+                        DbAttribute dbAttr = (DbAttribute) pathPart;
+                        if (dbAttr == null) {
+                            throw new CayenneRuntimeException(
+                                    "ObjAttribute has no DbAttribute: " + oa.getName());
+                        }
 
-                    appendColumn(columns, oa, dbAttr, attributes, null);
+                        appendColumn(columns, oa, dbAttr, attributes, null);
+                    }
                 }
+                return true;
             }
-        }
 
-        // relationship keys
-        Iterator rels = (tree != null) ? tree.allRelationships().iterator() : oe
-                .getRelationships()
-                .iterator();
-        while (rels.hasNext()) {
-            ObjRelationship rel = (ObjRelationship) rels.next();
-            DbRelationship dbRel = (DbRelationship) rel.getDbRelationships().get(0);
+            public boolean visitToMany(ToManyProperty property) {
+                visitRelationship(property);
+                return true;
+            }
 
-            List joins = dbRel.getJoins();
-            int len = joins.size();
-            for (int i = 0; i < len; i++) {
-                DbJoin join = (DbJoin) joins.get(i);
-                DbAttribute src = join.getSource();
-                appendColumn(columns, null, src, attributes, null);
+            public boolean visitToOne(ToOneProperty property) {
+                visitRelationship(property);
+                return true;
             }
+
+            private void visitRelationship(ArcProperty property) {
+                ObjRelationship rel = property.getRelationship();
+                DbRelationship dbRel = (DbRelationship) rel.getDbRelationships().get(0);
+
+                List joins = dbRel.getJoins();
+                int len = joins.size();
+                for (int i = 0; i < len; i++) {
+                    DbJoin join = (DbJoin) joins.get(i);
+                    DbAttribute src = join.getSource();
+                    appendColumn(columns, null, src, attributes, null);
+                }
+            }
+        };
+
+        if (query.isResolvingInherited()) {
+            descriptor.visitAllProperties(visitor);
+        }
+        else {
+            descriptor.visitProperties(visitor);
         }
 
         // add remaining needed attrs from DbEntity
-
         DbEntity table = getRootDbEntity();
         Iterator pk = table.getPrimaryKey().iterator();
         while (pk.hasNext()) {
@@ -408,7 +421,7 @@
 
         // special handling of a disjoint query...
 
-        // TODO, Andrus 11/17/2005 - resultPath mechansim is generic and should probably
+        // TODO, Andrus 11/17/2005 - resultPath mechanism is generic and should probably
         // be moved in the superclass (SelectQuery), replacing customDbAttributes.
 
         if (query instanceof PrefetchSelectQuery) {
@@ -631,18 +644,16 @@
             for (int i = 0; i < columns.size(); i++) {
                 ColumnDescriptor column = (ColumnDescriptor) columns.get(i);
                 if (attribute.getName().equals(column.getName())) {
-
-                    if (attributeOverrides == null) {
-                        attributeOverrides = new HashMap();
-                    }
-
-                    attributeOverrides.put(objAttribute, column);
-
+                  
                     // kick out the original attribute
                     ObjAttribute original = (ObjAttribute) defaultAttributesByColumn
                             .remove(column);
 
                     if (original != null) {
+                        if (attributeOverrides == null) {
+                            attributeOverrides = new HashMap();
+                        }
+                        
                         attributeOverrides.put(original, column);
                         column.setJavaClass(Void.TYPE.getName());
                     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/ClassDescriptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/ClassDescriptor.java?view=diff&rev=501054&r1=501053&r2=501054
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/ClassDescriptor.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/ClassDescriptor.java Mon Jan 29 06:02:03 2007
@@ -79,21 +79,24 @@
     /**
      * Returns a property descriptor matching property name, or null if no such property
      * is found. Lookup includes properties from this descriptor and all its superclass
-     * decsriptors. Returned property maybe any one of simple, value holder or collection
-     * properties.
+     * decsriptors. Returned property can be any one of {@link AttributeProperty},
+     * {@link ToManyProperty}, {@link ToOneProperty}.
      */
     Property getProperty(String propertyName);
 
     /**
      * Returns a Java Bean property descriptor matching property name or null if no such
      * property is found. Lookup DOES NOT including properties from the superclass
-     * decsriptors. Returned property maybe any one of simple, value holder or collection
-     * properties.
+     * descriptors. Returned property can be any one of {@link AttributeProperty},
+     * {@link ToManyProperty}, {@link ToOneProperty}.
      */
     Property getDeclaredProperty(String propertyName);
 
     /**
      * Returns an Iterator over descriptor properties.
+     * 
+     * @deprecated since 3.0. Use {@link #visitProperties(PropertyVisitor)} method
+     *             instead.
      */
     Iterator getProperties();
 
@@ -105,11 +108,31 @@
     Iterator getIdProperties();
 
     /**
-     * Passes the visitor to all properties "visit" method, terminating properties walk
-     * through in case one of the properties returns false. Returns true if all visited
-     * properties returned true, false - if one property returned false.
+     * Passes the visitor to all properties "visit" method, terminating properties
+     * walkthrough in case one of the properties returns false. Returns true if all
+     * visited properties returned true, false - if one property returned false.
      */
     boolean visitProperties(PropertyVisitor visitor);
+
+    /**
+     * Passes the visitor to the properties "visit" method for all properties declared in
+     * this descriptor, terminating properties walkthrough in case one of the properties
+     * returns false. Returns true if all visited properties returned true, false - if one
+     * property returned false.
+     * 
+     * @since 3.0
+     */
+    boolean visitDeclaredProperties(PropertyVisitor visitor);
+
+    /**
+     * Passes the visitor to the properties "visit" method for all properties declared in
+     * this descriptor, its super and subdescriptors, terminating properties walkthrough
+     * in case one of the properties returns false. Returns true if all visited properties
+     * returned true, false - if one property returned false.
+     * 
+     * @since 3.0
+     */
+    boolean visitAllProperties(PropertyVisitor visitor);
 
     /**
      * Returns true if an object is not fully resolved.

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/LazyClassDescriptorDecorator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/LazyClassDescriptorDecorator.java?view=diff&rev=501054&r1=501053&r2=501054
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/LazyClassDescriptorDecorator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/LazyClassDescriptorDecorator.java Mon Jan 29 06:02:03 2007
@@ -79,11 +79,15 @@
         return descriptor.getObjectClass();
     }
 
+    /**
+     * @deprecated since 3.0. Use {@link #visitProperties(PropertyVisitor)} method
+     *             instead.
+     */
     public Iterator getProperties() {
         checkDescriptorInitialized();
         return descriptor.getProperties();
     }
-    
+
     public Iterator getIdProperties() {
         checkDescriptorInitialized();
         return descriptor.getIdProperties();
@@ -119,8 +123,18 @@
         descriptor.shallowMerge(from, to);
     }
 
+    public boolean visitDeclaredProperties(PropertyVisitor visitor) {
+        checkDescriptorInitialized();
+        return descriptor.visitDeclaredProperties(visitor);
+    }
+
     public boolean visitProperties(PropertyVisitor visitor) {
         checkDescriptorInitialized();
         return descriptor.visitProperties(visitor);
+    }
+
+    public boolean visitAllProperties(PropertyVisitor visitor) {
+        checkDescriptorInitialized();
+        return descriptor.visitAllProperties(visitor);
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptor.java?view=diff&rev=501054&r1=501053&r2=501054
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptor.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptor.java Mon Jan 29 06:02:03 2007
@@ -89,7 +89,7 @@
      */
     public void removeDeclaredProperty(String propertyName) {
         Object removed = declaredProperties.remove(propertyName);
-        
+
         if (declaredIdProperties != null && removed != null) {
             declaredIdProperties.remove(removed);
         }
@@ -150,6 +150,10 @@
         return subclassDescriptor != null ? subclassDescriptor : this;
     }
 
+    /**
+     * @deprecated since 3.0. Use {@link #visitProperties(PropertyVisitor)} method
+     *             instead.
+     */
     public Iterator getProperties() {
         Iterator declaredIt = IteratorUtils.unmodifiableIterator(declaredProperties
                 .values()
@@ -271,12 +275,10 @@
         });
     }
 
-    public boolean visitProperties(PropertyVisitor visitor) {
-        if (superclassDescriptor != null
-                && !superclassDescriptor.visitProperties(visitor)) {
-            return false;
-        }
-
+    /**
+     * @since 3.0
+     */
+    public boolean visitDeclaredProperties(PropertyVisitor visitor) {
         Iterator it = declaredProperties.values().iterator();
         while (it.hasNext()) {
             Property next = (Property) it.next();
@@ -286,6 +288,36 @@
         }
 
         return true;
+    }
+
+    /**
+     * @since 3.0
+     */
+    public boolean visitAllProperties(PropertyVisitor visitor) {
+        if (!visitProperties(visitor)) {
+            return false;
+        }
+
+        if (!subclassDescriptors.isEmpty()) {
+            Iterator it = subclassDescriptors.values().iterator();
+            while (it.hasNext()) {
+                ClassDescriptor next = (ClassDescriptor) it.next();
+                if (!next.visitDeclaredProperties(visitor)) {
+                    return false;
+                }
+            }
+        }
+
+        return true;
+    }
+
+    public boolean visitProperties(PropertyVisitor visitor) {
+        if (superclassDescriptor != null
+                && !superclassDescriptor.visitProperties(visitor)) {
+            return false;
+        }
+
+        return visitDeclaredProperties(visitor);
     }
 
     public void setPersistenceStateAccessor(Accessor persistenceStateAccessor) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java?view=diff&rev=501054&r1=501053&r2=501054
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java Mon Jan 29 06:02:03 2007
@@ -21,6 +21,7 @@
 import java.util.Iterator;
 
 import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.map.EmbeddedAttribute;
 import org.apache.cayenne.map.EntityInheritanceTree;
 import org.apache.cayenne.map.ObjAttribute;
 import org.apache.cayenne.map.ObjEntity;
@@ -74,8 +75,13 @@
             if (attribute instanceof ObjAttribute) {
                 createAttributeProperty(descriptor, (ObjAttribute) attribute);
             }
-            else {
-                // TODO: andrus, 1/25/2007 - EmbeddedAttribute
+            else if (attribute instanceof EmbeddedAttribute) {
+                EmbeddedAttribute embedded = (EmbeddedAttribute) attribute;
+                Iterator embeddedAttributes = embedded.getAttributes().iterator();
+                while (embeddedAttributes.hasNext()) {
+                    createAttributeProperty(descriptor, (ObjAttribute) embeddedAttributes
+                            .next());
+                }
             }
         }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/EmbeddingTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/EmbeddingTest.java?view=diff&rev=501054&r1=501053&r2=501054
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/EmbeddingTest.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/EmbeddingTest.java Mon Jan 29 06:02:03 2007
@@ -18,6 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.access;
 
+import java.util.List;
+
+import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.query.SelectQuery;
 import org.apache.cayenne.testdo.embeddable.EmbedEntity1;
 import org.apache.cayenne.unit.AccessStack;
@@ -38,11 +41,9 @@
         SelectQuery query = new SelectQuery(EmbedEntity1.class);
         query.addOrdering(EmbedEntity1.NAME_PROPERTY, true);
 
-        // TODO: andrus 1/25/2007 - this fails
-
-        // ObjectContext context = createDataContext();
+        ObjectContext context = createDataContext();
 
-        // List results = context.performQuery(query);
-        // assertEquals(2, results.size());
+        List results = context.performQuery(query);
+        assertEquals(2, results.size());
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/reflect/MockClassDescriptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/reflect/MockClassDescriptor.java?view=diff&rev=501054&r1=501053&r2=501054
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/reflect/MockClassDescriptor.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/reflect/MockClassDescriptor.java Mon Jan 29 06:02:03 2007
@@ -42,10 +42,14 @@
         return false;
     }
 
+    /**
+     * @deprecated since 3.0. Use {@link #visitProperties(PropertyVisitor)} method
+     *             instead.
+     */
     public Iterator getProperties() {
         return null;
     }
-    
+
     public Iterator getIdProperties() {
         return null;
     }
@@ -89,6 +93,14 @@
     }
 
     public boolean visitProperties(PropertyVisitor visitor) {
+        return true;
+    }
+
+    public boolean visitAllProperties(PropertyVisitor visitor) {
+        return true;
+    }
+
+    public boolean visitDeclaredProperties(PropertyVisitor visitor) {
         return true;
     }
 }