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 2006/10/14 22:46:02 UTC

svn commit: r464017 - in /incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map: EnhancedPojoDescriptor.java EnhancedPojoSingleObjectArcProperty.java EntityDescriptorFactory.java

Author: aadamchik
Date: Sat Oct 14 13:45:59 2006
New Revision: 464017

URL: http://svn.apache.org/viewvc?view=rev&rev=464017
Log:
CAY-682: Generic Cayenne POJO enhancer
(starting on faults support in POJOs)

Added:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EnhancedPojoDescriptor.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EnhancedPojoSingleObjectArcProperty.java
Modified:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EntityDescriptorFactory.java

Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EnhancedPojoDescriptor.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EnhancedPojoDescriptor.java?view=auto&rev=464017
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EnhancedPojoDescriptor.java (added)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EnhancedPojoDescriptor.java Sat Oct 14 13:45:59 2006
@@ -0,0 +1,75 @@
+/*****************************************************************
+ *   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.map;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.cayenne.property.ClassDescriptor;
+import org.apache.cayenne.property.ListProperty;
+import org.apache.cayenne.property.Property;
+import org.apache.cayenne.property.PropertyAccessor;
+
+/**
+ * A descriptor for enhanced POJO's. Used in conjunction with Cayenne class enhancer.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+class EnhancedPojoDescriptor extends EntityDescriptor {
+
+    EnhancedPojoDescriptor(ObjEntity entity, ClassDescriptor superclassDescriptor) {
+        super(entity, superclassDescriptor);
+    }
+
+    protected void compileRelationships(EntityResolver resolver, Map allDescriptors) {
+
+        // only include this entity relationships and skip superclasses...
+        Iterator it = entity.getDeclaredRelationships().iterator();
+        while (it.hasNext()) {
+
+            ObjRelationship relationship = (ObjRelationship) it.next();
+            ClassDescriptor targetDescriptor = resolver.getClassDescriptor(relationship
+                    .getTargetEntityName());
+            String reverseName = relationship.getReverseRelationshipName();
+
+            Property property;
+            if (relationship.isToMany()) {
+                // TODO: andrus, 10/13/2006 - unfinished...
+                PropertyAccessor accessor = makeAccessor(
+                        relationship.getName(),
+                        List.class);
+                property = new ListProperty(this, targetDescriptor, accessor, reverseName);
+            }
+            else {
+                PropertyAccessor accessor = makeAccessor(
+                        relationship.getName(),
+                        targetDescriptor.getObjectClass());
+                property = new EnhancedPojoSingleObjectArcProperty(
+                        this,
+                        targetDescriptor,
+                        accessor,
+                        reverseName);
+            }
+
+            allDescriptors.put(relationship.getName(), property);
+        }
+    }
+}

Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EnhancedPojoSingleObjectArcProperty.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EnhancedPojoSingleObjectArcProperty.java?view=auto&rev=464017
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EnhancedPojoSingleObjectArcProperty.java (added)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EnhancedPojoSingleObjectArcProperty.java Sat Oct 14 13:45:59 2006
@@ -0,0 +1,75 @@
+/*****************************************************************
+ *   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.map;
+
+import org.apache.cayenne.Fault;
+import org.apache.cayenne.Persistent;
+import org.apache.cayenne.property.AbstractSingleObjectArcProperty;
+import org.apache.cayenne.property.ClassDescriptor;
+import org.apache.cayenne.property.FieldAccessor;
+import org.apache.cayenne.property.PropertyAccessException;
+import org.apache.cayenne.property.PropertyAccessor;
+
+/**
+ * A property descriptor for the to-one relationship property of an enhanced pojo. Assumes
+ * a class in question has a field called "$cay_fault_propertyName" that stores a boolean
+ * flag indicating whether the property in question is not yet resolved.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+class EnhancedPojoSingleObjectArcProperty extends AbstractSingleObjectArcProperty {
+
+    public static final String FAULT_FIELD_PREFIX = "$cay_fault_";
+
+    protected PropertyAccessor faultFlagAccessor;
+
+    public EnhancedPojoSingleObjectArcProperty(ClassDescriptor owner,
+            ClassDescriptor targetDescriptor, PropertyAccessor accessor,
+            String reverseName) {
+        super(owner, targetDescriptor, accessor, reverseName);
+        this.faultFlagAccessor = makeFaultFlagAccessor();
+    }
+
+    PropertyAccessor makeFaultFlagAccessor() {
+        return new FieldAccessor(
+                owner.getObjectClass(),
+                FAULT_FIELD_PREFIX + getName(),
+                Boolean.TYPE);
+    }
+
+    public boolean isFault(Object source) {
+        return ((Boolean) faultFlagAccessor.readPropertyDirectly(source)).booleanValue();
+    }
+
+    protected void resolveFault(Object object) {
+        if (isFault(object)) {
+            Object target = Fault.getToOneFault().resolveFault(
+                    (Persistent) object,
+                    getName());
+            writePropertyDirectly(object, null, target);
+            faultFlagAccessor.writePropertyDirectly(object, Boolean.TRUE, Boolean.FALSE);
+        }
+    }
+
+    public Object readProperty(Object object) throws PropertyAccessException {
+        resolveFault(object);
+        return super.readProperty(object);
+    }
+}

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EntityDescriptorFactory.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EntityDescriptorFactory.java?view=diff&rev=464017&r1=464016&r2=464017
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EntityDescriptorFactory.java (original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/EntityDescriptorFactory.java Sat Oct 14 13:45:59 2006
@@ -100,15 +100,33 @@
     protected EntityDescriptor createDescriptor(
             ObjEntity entity,
             ClassDescriptor superclassDescriptor) {
-        
-        boolean dataObject = DataObject.class.isAssignableFrom(entity.getJavaClass());
 
-        // return uncompiled
+        Class entityClass = entity.getJavaClass();
+        boolean dataObject = DataObject.class.isAssignableFrom(entityClass);
+        boolean enhanced = !dataObject && sniffEnhanced(entityClass);
+
+        // TODO: andrus, 10/23/2006 - refactor as a chain of ClassDescriptorFactories.
         if (dataObject) {
             return new DataObjectDescriptor(entity, superclassDescriptor);
         }
+        else if (enhanced) {
+            return new EnhancedPojoDescriptor(entity, superclassDescriptor);
+        }
         else {
             return new EntityDescriptor(entity, superclassDescriptor);
+        }
+    }
+
+    /**
+     * Checks for the signs of Cayenne enhancer in the class.
+     */
+    boolean sniffEnhanced(Class entityClass) {
+        try {
+            return Integer.TYPE.equals(entityClass.getDeclaredField(
+                    "$cay_persistenceState").getType());
+        }
+        catch (Throwable th) {
+            return false;
         }
     }
 }