cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r578160 - in /cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect: JpaClassDescriptorFactory.java JpaListProperty.java JpaMapProperty.java JpaSetProperty.java JpaToManyProperty.java
Date Fri, 21 Sep 2007 15:07:53 GMT
Author: aadamchik
Date: Fri Sep 21 08:07:52 2007
New Revision: 578160

URL: http://svn.apache.org/viewvc?rev=578160&view=rev
Log:
(CAY-848) Support for mapping to-many as Maps and Sets and Collections
(adding set and map property descriptors)

Added:
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaListProperty.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaMapProperty.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaSetProperty.java
Modified:
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaClassDescriptorFactory.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaToManyProperty.java

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaClassDescriptorFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaClassDescriptorFactory.java?rev=578160&r1=578159&r2=578160&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaClassDescriptorFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaClassDescriptorFactory.java
Fri Sep 21 08:07:52 2007
@@ -19,12 +19,18 @@
 
 package org.apache.cayenne.jpa.reflect;
 
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
 import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.reflect.Accessor;
 import org.apache.cayenne.reflect.ClassDescriptor;
 import org.apache.cayenne.reflect.ClassDescriptorMap;
 import org.apache.cayenne.reflect.FaultFactory;
 import org.apache.cayenne.reflect.PersistentDescriptor;
+import org.apache.cayenne.reflect.Property;
 import org.apache.cayenne.reflect.pojo.EnhancedPojoDescriptorFactory;
 
 public class JpaClassDescriptorFactory extends EnhancedPojoDescriptorFactory {
@@ -42,14 +48,53 @@
                 .getTargetEntityName());
         String reverseName = relationship.getReverseRelationshipName();
 
-        Accessor accessor = new JpaCollectionFieldAccessor(
-                descriptor.getObjectClass(),
-                relationship.getName(),
-                null);
-        descriptor.addDeclaredProperty(new JpaToManyProperty(
-                descriptor,
-                targetDescriptor,
-                accessor,
-                reverseName));
+        String collectionType = relationship.getCollectionType();
+        Property property;
+
+        if (collectionType == null
+                || ObjRelationship.DEFAULT_COLLECTION_TYPE.equals(collectionType)) {
+
+            Accessor accessor = new JpaCollectionFieldAccessor(descriptor
+                    .getObjectClass(), relationship.getName(), List.class);
+
+            property = new JpaListProperty(
+                    descriptor,
+                    targetDescriptor,
+                    accessor,
+                    reverseName);
+        }
+        else if (collectionType.equals("java.util.Map")) {
+            Accessor accessor = new JpaCollectionFieldAccessor(descriptor
+                    .getObjectClass(), relationship.getName(), Map.class);
+            property = new JpaMapProperty(
+                    descriptor,
+                    targetDescriptor,
+                    accessor,
+                    reverseName);
+        }
+        else if (collectionType.equals("java.util.Set")) {
+            Accessor accessor = new JpaCollectionFieldAccessor(descriptor
+                    .getObjectClass(), relationship.getName(), Set.class);
+            property = new JpaSetProperty(
+                    descriptor,
+                    targetDescriptor,
+                    accessor,
+                    reverseName);
+        }
+        else if (collectionType.equals("java.util.Collection")) {
+            Accessor accessor = new JpaCollectionFieldAccessor(descriptor
+                    .getObjectClass(), relationship.getName(), Collection.class);
+            property = new JpaListProperty(
+                    descriptor,
+                    targetDescriptor,
+                    accessor,
+                    reverseName);
+        }
+        else {
+            throw new IllegalArgumentException("Unsupported to many collection type: "
+                    + collectionType);
+        }
+
+        descriptor.addDeclaredProperty(property);
     }
 }

Added: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaListProperty.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaListProperty.java?rev=578160&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaListProperty.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaListProperty.java
Fri Sep 21 08:07:52 2007
@@ -0,0 +1,38 @@
+/*****************************************************************
+ *   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.jpa.reflect;
+
+import org.apache.cayenne.Persistent;
+import org.apache.cayenne.ValueHolder;
+import org.apache.cayenne.reflect.Accessor;
+import org.apache.cayenne.reflect.ClassDescriptor;
+import org.apache.cayenne.util.PersistentObjectList;
+
+class JpaListProperty extends JpaToManyProperty {
+
+    JpaListProperty(ClassDescriptor owner, ClassDescriptor targetDescriptor,
+            Accessor accessor, String reverseName) {
+        super(owner, targetDescriptor, accessor, reverseName);
+    }
+
+    @Override
+    protected ValueHolder createValueHolder(Persistent relationshipOwner) {
+        return new PersistentObjectList(relationshipOwner, getName());
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaMapProperty.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaMapProperty.java?rev=578160&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaMapProperty.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaMapProperty.java
Fri Sep 21 08:07:52 2007
@@ -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.jpa.reflect;
+
+import org.apache.cayenne.Persistent;
+import org.apache.cayenne.ValueHolder;
+import org.apache.cayenne.reflect.Accessor;
+import org.apache.cayenne.reflect.ClassDescriptor;
+import org.apache.cayenne.util.PersistentObjectMap;
+
+class JpaMapProperty extends JpaToManyProperty {
+
+    JpaMapProperty(ClassDescriptor owner, ClassDescriptor targetDescriptor,
+            Accessor accessor, String reverseName) {
+        super(owner, targetDescriptor, accessor, reverseName);
+    }
+
+    @Override
+    protected ValueHolder createValueHolder(Persistent relationshipOwner) {
+        return new PersistentObjectMap(relationshipOwner, getName(), getRelationship()
+                .getMapKeyExpression());
+    }
+
+}

Added: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaSetProperty.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaSetProperty.java?rev=578160&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaSetProperty.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaSetProperty.java
Fri Sep 21 08:07:52 2007
@@ -0,0 +1,38 @@
+/*****************************************************************
+ *   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.jpa.reflect;
+
+import org.apache.cayenne.Persistent;
+import org.apache.cayenne.ValueHolder;
+import org.apache.cayenne.reflect.Accessor;
+import org.apache.cayenne.reflect.ClassDescriptor;
+import org.apache.cayenne.util.PersistentObjectSet;
+
+class JpaSetProperty extends JpaToManyProperty {
+
+    JpaSetProperty(ClassDescriptor owner, ClassDescriptor targetDescriptor,
+            Accessor accessor, String reverseName) {
+        super(owner, targetDescriptor, accessor, reverseName);
+    }
+
+    @Override
+    protected ValueHolder createValueHolder(Persistent relationshipOwner) {
+        return new PersistentObjectSet(relationshipOwner, getName());
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaToManyProperty.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaToManyProperty.java?rev=578160&r1=578159&r2=578160&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaToManyProperty.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaToManyProperty.java
Fri Sep 21 08:07:52 2007
@@ -23,17 +23,16 @@
 import java.util.Map;
 
 import org.apache.cayenne.Fault;
+import org.apache.cayenne.Persistent;
 import org.apache.cayenne.ValueHolder;
 import org.apache.cayenne.reflect.Accessor;
+import org.apache.cayenne.reflect.BaseToManyProperty;
 import org.apache.cayenne.reflect.ClassDescriptor;
-import org.apache.cayenne.reflect.ListProperty;
 import org.apache.cayenne.reflect.PropertyException;
 
-// TODO: andrus 11/25/2006 - this should be modeled after EnhancedPojo instead of
-// DataObject to-many property.
-class JpaToManyProperty extends ListProperty {
+abstract class JpaToManyProperty extends BaseToManyProperty {
 
-    public JpaToManyProperty(ClassDescriptor owner, ClassDescriptor targetDescriptor,
+    JpaToManyProperty(ClassDescriptor owner, ClassDescriptor targetDescriptor,
             Accessor accessor, String reverseName) {
         super(owner, targetDescriptor, accessor, reverseName);
     }
@@ -70,5 +69,36 @@
         }
 
         return (ValueHolder) value;
+    }
+
+    /**
+     * Creates a List for an object. Expects an object to be an instance of Persistent.
+     */
+    protected ValueHolder createCollectionValueHolder(Object object)
+            throws PropertyException {
+        if (!(object instanceof Persistent)) {
+            throw new PropertyException(
+                    "ValueHolders for non-persistent objects are not supported.",
+                    this,
+                    object);
+        }
+
+        return createValueHolder((Persistent) object);
+    }
+
+    protected abstract ValueHolder createValueHolder(Persistent relationshipOwner);
+
+    public boolean isFault(Object object) {
+        Object target = accessor.getValue(object);
+        return target == null
+                || target instanceof Fault
+                || ((ValueHolder) target).isFault();
+    }
+
+    public void invalidate(Object object) {
+        ValueHolder list = (ValueHolder) readPropertyDirectly(object);
+        if (list != null) {
+            list.invalidate();
+        }
     }
 }



Mime
View raw message