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 2014/04/12 10:06:22 UTC

svn commit: r1586830 - /cayenne/main/trunk/cayenne-project/src/main/java/org/apache/cayenne/project/upgrade/v7/UpgradeHandler_V7.java

Author: aadamchik
Date: Sat Apr 12 08:06:22 2014
New Revision: 1586830

URL: http://svn.apache.org/r1586830
Log:
CAY-1795 "Invisible" ObjAttribute in subclass

patch by Oleg Artyukevich - v7upgrade_2.patch
(had to fix a few things manually as the patch no longer matches the upgarder)

Modified:
    cayenne/main/trunk/cayenne-project/src/main/java/org/apache/cayenne/project/upgrade/v7/UpgradeHandler_V7.java

Modified: cayenne/main/trunk/cayenne-project/src/main/java/org/apache/cayenne/project/upgrade/v7/UpgradeHandler_V7.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-project/src/main/java/org/apache/cayenne/project/upgrade/v7/UpgradeHandler_V7.java?rev=1586830&r1=1586829&r2=1586830&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-project/src/main/java/org/apache/cayenne/project/upgrade/v7/UpgradeHandler_V7.java (original)
+++ cayenne/main/trunk/cayenne-project/src/main/java/org/apache/cayenne/project/upgrade/v7/UpgradeHandler_V7.java Sat Apr 12 08:06:22 2014
@@ -18,12 +18,18 @@
  ****************************************************************/
 package org.apache.cayenne.project.upgrade.v7;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.cayenne.ConfigurationException;
 import org.apache.cayenne.configuration.ConfigurationTree;
 import org.apache.cayenne.configuration.DataChannelDescriptor;
 import org.apache.cayenne.configuration.XMLDataChannelDescriptorLoader;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.di.Injector;
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.project.Project;
 import org.apache.cayenne.project.ProjectSaver;
 import org.apache.cayenne.project.upgrade.BaseUpgradeHandler;
@@ -64,11 +70,65 @@ class UpgradeHandler_V7 extends BaseUpgr
         
         attachToNamespace((DataChannelDescriptor) project.getRootNode());
         
-        // load and safe cycle removes objects no longer supported, specifically listeners.
+        // remove "shadow" attributes per CAY-1795
+        checkObjEntities(project);
         
+        // load and safe cycle removes objects no longer supported, specifically listeners
         projectSaver.save(project); 
         return project.getConfigurationResource();
     }
+    
+    private void checkObjEntities(Project project) {
+        DataChannelDescriptor rootNode = (DataChannelDescriptor) project.getRootNode();
+
+        List<DataMap> dataList = new ArrayList<DataMap>(rootNode.getDataMaps());
+        List<ObjEntity> objEntityList = null;
+
+        // take ObjEntities from DataMap
+        if (!dataList.isEmpty()) {
+            for (DataMap dataMap : dataList) {
+                objEntityList = new ArrayList<ObjEntity>(dataMap.getObjEntities());
+                ObjEntity superEntity = null;
+
+                // if objEntity has super entity, then checks it
+                // for duplicated attributes
+                for (ObjEntity objEntity : objEntityList) {
+                    superEntity = objEntity.getSuperEntity();
+                    if (superEntity != null) {
+                        removeDuplicatedAttributes(objEntity, superEntity);
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * Remove attributes from objEntity, if superEntity has attributes with same
+     * names.
+     */
+    private void removeDuplicatedAttributes(ObjEntity objEntity, ObjEntity superEntity) {
+        List<ObjAttribute> entityAttr = new ArrayList<ObjAttribute>(objEntity.getDeclaredAttributes());
+        List<ObjAttribute> superEntityAttr = new ArrayList<ObjAttribute>(superEntity.getAttributes());
+        List<String> delList = new ArrayList<String>();
+        // entityAttr - attributes of objEntity, without inherited
+        // superEntityAttr - all attributes in the superEntity inheritance
+        // hierarchy
+        // delList - attributes, that will be removed from objEntity
+
+        // if subAttr and superAttr have same names, adds subAttr to delList
+        for (ObjAttribute subAttr : entityAttr) {
+            for (ObjAttribute superAttr : superEntityAttr) {
+                if (subAttr.getName().equals(superAttr.getName())) {
+                    delList.add(subAttr.getName());
+                }
+            }
+        }
+        if (!delList.isEmpty()) {
+            for (String i : delList) {
+                objEntity.removeAttribute(i);
+            }
+        }
+    }
 
     @Override
     protected UpgradeMetaData loadMetaData() {