cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r700062 - in /cayenne/main/trunk/framework: cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/
Date Mon, 29 Sep 2008 11:17:42 GMT
Author: aadamchik
Date: Mon Sep 29 04:17:40 2008
New Revision: 700062

URL: http://svn.apache.org/viewvc?rev=700062&view=rev
Log:
CAY-795 Horizontal inheritance (patch by Dzmitry Rusak)

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/MapLoader.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjEntity.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityInheritanceTest.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityTab.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/MapLoader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/MapLoader.java?rev=700062&r1=700061&r2=700062&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/MapLoader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/MapLoader.java
Mon Sep 29 04:17:40 2008
@@ -46,6 +46,7 @@
  * @author Misha Shengaout
  * @author Andrus Adamchik
  * @author Andriy Shapochka
+ * @author Dzmitry Rusak
  */
 public class MapLoader extends DefaultHandler {
 
@@ -82,6 +83,7 @@
     public static final String OBJ_ENTITY_TAG = "obj-entity";
     public static final String DB_ATTRIBUTE_TAG = "db-attribute";
     public static final String OBJ_ATTRIBUTE_TAG = "obj-attribute";
+    public static final String OBJ_ATTRIBUTE_OVERRIDE_TAG = "attribute-override";
     public static final String OBJ_RELATIONSHIP_TAG = "obj-relationship";
     public static final String DB_RELATIONSHIP_TAG = "db-relationship";
     public static final String DB_RELATIONSHIP_REF_TAG = "db-relationship-ref";
@@ -199,6 +201,14 @@
             }
         });
 
+        startTagOpMap.put(OBJ_ATTRIBUTE_OVERRIDE_TAG, new StartClosure() {
+
+            @Override
+            void execute(Attributes attributes) throws SAXException {
+                processStartAttributeOverride(attributes);
+            }
+        });
+
         startTagOpMap.put(EMBEDDABLE_TAG, new StartClosure() {
 
             @Override
@@ -898,11 +908,12 @@
             objEntity.setSuperEntityName(superEntityName);
         }
         else {
-            objEntity.setDbEntityName(atts.getValue("", "dbEntityName"));
             objEntity.setSuperClassName(atts.getValue("", "superClassName"));
             objEntity.setClientSuperClassName(atts.getValue("", "clientSuperClassName"));
         }
 
+        objEntity.setDbEntityName(atts.getValue("", "dbEntityName"));
+
         dataMap.addObjEntity(objEntity);
     }
 
@@ -923,6 +934,13 @@
         oa.setDbAttributePath(dbPath);
     }
 
+    private void processStartAttributeOverride(Attributes atts) {
+        String name = atts.getValue("", "name");
+        String dbPath = atts.getValue("", "db-attribute-path");
+        
+        objEntity.addAttributeOverride(name, dbPath);
+    }
+
     private void processStartDbRelationship(Attributes atts) throws SAXException {
         String name = atts.getValue("", "name");
         if (name == null) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java?rev=700062&r1=700061&r2=700062&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java
Mon Sep 29 04:17:40 2008
@@ -32,6 +32,7 @@
  * 
  * @author Misha Shengaout
  * @author Andrus Adamchik
+ * @author Dzmitry Rusak
  */
 public class ObjAttribute extends Attribute {
 
@@ -52,6 +53,15 @@
         setEntity(entity);
     }
 
+    /** @since 3.0 */
+    public ObjAttribute(ObjAttribute attribute) {
+        setName(attribute.getName());
+        setType(attribute.getType());
+        setEntity(attribute.getEntity());
+        setDbAttributePath(attribute.getDbAttributePath());
+        setUsedForLocking(attribute.isUsedForLocking());
+    }
+
     /**
      * Returns Java class of an object property described by this attribute. Wraps any
      * thrown exceptions into CayenneRuntimeException.
@@ -151,25 +161,69 @@
      * Returns a DbAttribute mapped by this ObjAttribute.
      */
     public DbAttribute getDbAttribute() {
-        Iterator<CayenneMapEntry> pathIterator = getDbPathIterator();
+        Iterator<CayenneMapEntry> pathIterator = getDbPathIterator((ObjEntity) getEntity());
         CayenneMapEntry o = null;
         while (pathIterator.hasNext()) {
             o = pathIterator.next();
         }
+        if (o == null) {
+            return getParentDbAttribute((ObjEntity) getEntity());
+        }
         return (DbAttribute) o;
     }
 
+    private DbAttribute getParentDbAttribute(ObjEntity entity) {
+        if (entity != null) {
+            ObjEntity parent = entity.getSuperEntity();
+            if (parent != null) {
+                Iterator<CayenneMapEntry> pathIterator = getDbPathIterator(parent);
+                CayenneMapEntry o = null;
+                while (pathIterator.hasNext()) {
+                    o = pathIterator.next();
+                }
+                if (o == null) {
+                    return getParentDbAttribute(parent);
+                }
+                return (DbAttribute) o;
+            }
+        }
+
+        return null;
+    }
+    
+    /**
+     * Return <code>true</code> if attribute inhertit from parent {@link ObjEntity}.
+     *  
+     * @since 3.0 
+     */
+    public boolean isInherited() {
+        if (getEntity() == null) {
+            return false;
+        }
+        
+        ObjEntity parent = ((ObjEntity) getEntity()).getSuperEntity();
+
+        if (parent == null) {
+            return false;
+        }
+
+        return parent.getAttribute(getName()) != null;
+    }
+
     public Iterator<CayenneMapEntry> getDbPathIterator() {
+        return getDbPathIterator((ObjEntity) getEntity());
+    }
+
+    public Iterator<CayenneMapEntry> getDbPathIterator(ObjEntity entity) {
         if (dbAttributePath == null) {
             return IteratorUtils.EMPTY_ITERATOR;
         }
-
-        ObjEntity ent = (ObjEntity) getEntity();
-        if (ent == null) {
+       
+        if (entity == null) {
             return IteratorUtils.EMPTY_ITERATOR;
         }
 
-        DbEntity dbEnt = ent.getDbEntity();
+        DbEntity dbEnt = entity.getDbEntity();
         if (dbEnt == null) {
             return IteratorUtils.EMPTY_ITERATOR;
         }
@@ -230,6 +284,10 @@
 
     public void setDbAttributePath(String dbAttributePath) {
         this.dbAttributePath = dbAttributePath;
+        
+        if (isInherited()) {
+            ((ObjEntity)entity).addAttributeOverride(getName(), dbAttributePath);
+        }
     }
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjEntity.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjEntity.java?rev=700062&r1=700061&r2=700062&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjEntity.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjEntity.java
Mon Sep 29 04:17:40 2008
@@ -50,6 +50,7 @@
  * 
  * @author Misha Shengaout
  * @author Andrus Adamchik
+ * @author Dzmitry Rusak
  */
 public class ObjEntity extends Entity implements ObjEntityListener {
 
@@ -84,6 +85,8 @@
     protected boolean excludingDefaultListeners;
     protected boolean excludingSuperclassListeners;
 
+    protected Map<String, String> overridedAttributes;
+
     public ObjEntity() {
         this(null);
     }
@@ -93,6 +96,7 @@
         this.lockType = LOCK_TYPE_NONE;
         this.callbacks = new CallbackMap();
         this.entityListeners = new ArrayList<EntityListener>(2);
+        this.overridedAttributes = new TreeMap<String, String>();
     }
 
     /**
@@ -136,7 +140,7 @@
             encoder.print("\" lock-type=\"optimistic");
         }
 
-        if (getSuperEntityName() == null && getDbEntity() != null) {
+        if (getDbEntity() != null) {
             encoder.print("\" dbEntityName=\"");
             encoder.print(Util.encodeXmlAttribute(getDbEntityName()));
         }
@@ -171,6 +175,14 @@
         // store attributes
         encoder.print(getDeclaredAttributes());
 
+        for (Map.Entry<String, String> override : overridedAttributes.entrySet()) {
+            encoder.print("<attribute-override name=\"" + override.getKey() + '\"');
+            encoder.print(" db-attribute-path=\"");
+            encoder.print(Util.encodeXmlAttribute(override.getValue()));
+            encoder.print('\"');
+            encoder.println("/>");
+        }
+
         // write entity listeners
         for (EntityListener entityListener : entityListeners) {
             entityListener.encodeAsXML(encoder);
@@ -669,7 +681,24 @@
         }
 
         ObjEntity superEntity = getSuperEntity();
-        return (superEntity != null) ? superEntity.getAttribute(name) : null;
+        if (superEntity != null) {
+            Attribute attr = superEntity.getAttribute(name);
+            ObjAttribute result = null;
+            if (attr instanceof ObjAttribute) {
+                String overridedDbPath = overridedAttributes.get(name);
+                if (overridedDbPath != null) {
+                    result = new ObjAttribute((ObjAttribute) attr);
+                    result.setEntity(this);
+                    result.setDbAttributePath(overridedDbPath);
+                    return result;
+                }
+            }
+
+            return attr;
+
+        }
+
+        return null;
     }
 
     /**
@@ -694,10 +723,27 @@
         map.putAll((Map<String, ObjAttribute>) super.getAttributeMap());
 
         ObjEntity superEntity = getSuperEntity();
-        if (superEntity != null) {
-            superEntity.appendAttributes(map);
+        if (superEntity != null) 
+        {
+            SortedMap<String, ObjAttribute> attributeMap = new TreeMap<String, ObjAttribute>();
+            superEntity.appendAttributes(attributeMap);
+            for (String attributeName : attributeMap.keySet()) {
+
+                String overridedDbPath = overridedAttributes.get(attributeName);
+
+                ObjAttribute attribute = new ObjAttribute(attributeMap.get(attributeName));
+                attribute.setEntity(this);
+                if (overridedDbPath != null) {
+                    attribute.setDbAttributePath(overridedDbPath);
+                }
+                map.put(attributeName, attribute);
+            }
         }
     }
+    
+    public void addAttributeOverride(String attributeName, String dbPath) {
+        overridedAttributes.put(attributeName, dbPath);
+    }
 
     /**
      * Returns a Collection of all attributes that either belong to this ObjEntity or

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityInheritanceTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityInheritanceTest.java?rev=700062&r1=700061&r2=700062&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityInheritanceTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityInheritanceTest.java
Mon Sep 29 04:17:40 2008
@@ -137,7 +137,6 @@
 
         assertSame(attribute1, entity1.getAttributeForDbAttribute(dbAttribute1));
         assertNotNull(entity1.getAttributeForDbAttribute(dbAttribute2));
-        assertSame(attribute2, entity1.getAttributeForDbAttribute(dbAttribute2));
     }
 
     public void testRelationshipForDbRelationship() throws Exception {

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java?rev=700062&r1=700061&r2=700062&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java
Mon Sep 29 04:17:40 2008
@@ -41,6 +41,7 @@
  * 
  * @author Michael Misha Shengaout.
  * @author Andrus Adamchik
+ * @author Dzmitry Rusak
  */
 public class ObjAttributeTableModel extends CayenneTableModel {
 
@@ -208,20 +209,19 @@
         mediator.fireObjAttributeEvent(event);
     }
 
-    private boolean isInherited(int row) {
-        ObjAttribute attribute = getAttribute(row);
-        return (attribute != null) ? attribute.getEntity() != entity : false;
-    }
-
     public boolean isCellEditable(int row, int col) {
-        if (isInherited(row)) {
-            return false;
-        }
 
         if (dbEntity == null) {
             return col != DB_ATTRIBUTE_TYPE && col != DB_ATTRIBUTE;
         }
 
+        if (getAttribute(row).isInherited()) {
+            if (col == DB_ATTRIBUTE) {
+                return true;
+            }
+            return false;
+        }
+
         return col != DB_ATTRIBUTE_TYPE;
     }
 

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityTab.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityTab.java?rev=700062&r1=700061&r2=700062&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityTab.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityTab.java
Mon Sep 29 04:17:40 2008
@@ -242,6 +242,7 @@
                 // Change DbEntity for current ObjEntity
                 ObjEntity entity = mediator.getCurrentObjEntity();
                 DbEntity dbEntity = (DbEntity) dbEntityCombo.getSelectedItem();
+                syncWithDbEntityButton.setEnabled(dbEntity != null);
 
                 if (dbEntity != entity.getDbEntity()) {
                     entity.setDbEntity(dbEntity);
@@ -594,9 +595,7 @@
         superClassName.getComponent().setEnabled(directTableMapping);
         superclassLabel.setEnabled(directTableMapping);
 
-        dbEntityCombo.setEnabled(directTableMapping);
-        syncWithDbEntityButton.setEnabled(directTableMapping);
-        tableLabel.setEnabled(directTableMapping);
+        syncWithDbEntityButton.setEnabled(dbEntityCombo.getSelectedItem() != null);
 
         clientSuperClassName.getComponent().setEnabled(
                 directTableMapping && clientFieldsEnabled);



Mime
View raw message