isis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ke...@apache.org
Subject svn commit: r1170255 - in /incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql: sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/ sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/ sql-impl/src/ma...
Date Tue, 13 Sep 2011 17:24:13 GMT
Author: kevin
Date: Tue Sep 13 17:24:13 2011
New Revision: 1170255

URL: http://svn.apache.org/viewvc?rev=1170255&view=rev
Log:
ISIS-118: Alternate polymorphic collection mapper implementation: PolymorphicForeignKeyInChildCollectionBaseMapper

Added:
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/PolymorphicForeignKeyInChildCollectionBaseMapper.java
  (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/JdbcPolymorphicObjectReferenceMapping.java
  (with props)
Modified:
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/VersionMapping.java
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/AbstractAutoMapper.java
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/AutoMapper.java
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/ForeignKeyCollectionMapper.java
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/PolymorphicForeignKeyInChildCollectionMapper.java
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-tests-common/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/testsystem/dataclasses/polymorphism/PolyTestClass.java
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-tests-common/src/test/java/org/apache/isis/runtimes/dflt/objectstores/sql/PolymorphismTest.java

Modified: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/VersionMapping.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/VersionMapping.java?rev=1170255&r1=1170254&r2=1170255&view=diff
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/VersionMapping.java
(original)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/VersionMapping.java
Tue Sep 13 17:24:13 2011
@@ -62,7 +62,7 @@ public class VersionMapping {
         return versionColumn + " = ?";
     }
 
-    public String appendSelectColumns() {
+    public String appendColumnNames() {
         final StringBuffer sql = new StringBuffer();
         sql.append(versionColumn);
         sql.append(",");

Modified: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/AbstractAutoMapper.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/AbstractAutoMapper.java?rev=1170255&r1=1170254&r2=1170255&view=diff
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/AbstractAutoMapper.java
(original)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/AbstractAutoMapper.java
Tue Sep 13 17:24:13 2011
@@ -95,7 +95,6 @@ public abstract class AbstractAutoMapper
         final IsisConfiguration configParameters = IsisContext.getConfiguration();
         table = configParameters.getString(parameterBase + ".table." + className);
         if (table == null) {
-            // final String name = "isis_" + className.substring(className.lastIndexOf('.')
+ 1);
             final String name = getTableNameFromSpecification(specification);
             table = name;
         } else {
@@ -153,6 +152,9 @@ public abstract class AbstractAutoMapper
 
                 // TODO: Replace "new ForeignKeyCollectionMapper" with a factory method(?)
to allow a different
                 // default CollectionMapper
+
+                // TODO: I think the default order should be changed - and I think I (KAM)
have dropped support for the
+                // original "association-table" implementation. This means the current checks
are misleading.
                 final String type = subset.getString(field.getId());
                 if (type == null || type.equals("association-table")) {
                     // collectionMappers[collectionFieldNo] = new AutoCollectionMapper(specification,
@@ -176,9 +178,12 @@ public abstract class AbstractAutoMapper
 
                     if (collectionMapper == null) {
                         // TODO: Polymorphism - is it sufficient for the collectionMapper
to handle the subclasses?
-                        if (field.getSpecification().hasSubclasses()) {
+                        final ObjectSpecification fieldSpecification = field.getSpecification();
+                        if (fieldSpecification.hasSubclasses() || fieldSpecification.isAbstract())
{
+                            // PolymorphicForeignKeyInChildCollectionBaseMapper
+                            // Or PolymorphicForeignKeyInChildCollectionMapper
                             collectionMapper =
-                                new PolymorphicForeignKeyInChildCollectionMapper(
+                                new PolymorphicForeignKeyInChildCollectionBaseMapper(
                                     oneToManyProperties[collectionFieldNo], parameterBase,
lookup, objectMapperLookup,
                                     this, field);
                         } else {

Modified: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/AutoMapper.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/AutoMapper.java?rev=1170255&r1=1170254&r2=1170255&view=diff
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/AutoMapper.java
(original)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/AutoMapper.java
Tue Sep 13 17:24:13 2011
@@ -344,7 +344,7 @@ public class AutoMapper extends Abstract
         sql.append("select ");
         sql.append(columnList(fieldMappings));
         sql.append(",");
-        sql.append(versionMapping.appendSelectColumns());
+        sql.append(versionMapping.appendColumnNames());
         sql.append(" from " + table + " WHERE ");
         idMapping.appendWhereClause(connector, sql, object.getOid());
 

Modified: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/ForeignKeyCollectionMapper.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/ForeignKeyCollectionMapper.java?rev=1170255&r1=1170254&r2=1170255&view=diff
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/ForeignKeyCollectionMapper.java
(original)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/ForeignKeyCollectionMapper.java
Tue Sep 13 17:24:13 2011
@@ -36,6 +36,7 @@ import org.apache.isis.runtimes.dflt.obj
 import org.apache.isis.runtimes.dflt.objectstores.sql.DatabaseConnector;
 import org.apache.isis.runtimes.dflt.objectstores.sql.FieldMappingLookup;
 import org.apache.isis.runtimes.dflt.objectstores.sql.IdMapping;
+import org.apache.isis.runtimes.dflt.objectstores.sql.IdMappingAbstract;
 import org.apache.isis.runtimes.dflt.objectstores.sql.ObjectMapping;
 import org.apache.isis.runtimes.dflt.objectstores.sql.ObjectMappingLookup;
 import org.apache.isis.runtimes.dflt.objectstores.sql.Results;
@@ -110,6 +111,14 @@ public class ForeignKeyCollectionMapper 
         this.columnName = columnName;
     }
 
+    protected VersionMapping getVersionMapping() {
+        return versionMapping;
+    }
+
+    protected ObjectReferenceMapping getForeignKeyMapping() {
+        return foreignKeyMapping;
+    }
+
     protected String getForeignKeyName() {
         return foreignKeyName;
     }
@@ -130,12 +139,35 @@ public class ForeignKeyCollectionMapper 
 
     @Override
     public void createTables(final DatabaseConnector connection) {
-        final StringBuffer sql = new StringBuffer();
-        sql.append("alter table ");
-        sql.append(table);
-        sql.append(" add ");
-        appendColumnDefinitions(sql);
-        connection.update(sql.toString());
+        if (connection.hasTable(table)) {
+            final StringBuffer sql = new StringBuffer();
+            sql.append("alter table ");
+            sql.append(table);
+            sql.append(" add ");
+            appendColumnDefinitions(sql);
+            connection.update(sql.toString());
+        } else {
+            final StringBuffer sql = new StringBuffer();
+            sql.append("create table ");
+            sql.append(table);
+            sql.append(" (");
+            idMapping.appendCreateColumnDefinitions(sql);
+            sql.append(", ");
+
+            appendColumnDefinitions(sql);
+
+            // for (final FieldMapping mapping : fieldMappings) {
+            // mapping.appendColumnDefinitions(sql);
+            // sql.append(",");
+            // }
+            // sql.append(versionMapping.appendColumnDefinitions());
+            sql.append(")");
+            connection.update(sql.toString());
+        }
+    }
+
+    public IdMappingAbstract getIdMapping() {
+        return idMapping;
     }
 
     protected void appendCollectionUpdateColumnsToNull(StringBuffer sql) {
@@ -167,8 +199,8 @@ public class ForeignKeyCollectionMapper 
 
             final List<ObjectAdapter> list = new ArrayList<ObjectAdapter>();
 
-            loadCollectionIntoList(connector, parent, makeResolved, table, specification,
idMapping, fieldMappings,
-                versionMapping, list);
+            loadCollectionIntoList(connector, parent, makeResolved, table, specification,
getIdMapping(),
+                fieldMappings, versionMapping, list);
 
             final CollectionFacet collectionFacet = collection.getSpecification().getFacet(CollectionFacet.class);
             collectionFacet.init(collection, list.toArray(new ObjectAdapter[list.size()]));
@@ -188,11 +220,12 @@ public class ForeignKeyCollectionMapper 
     }
 
     protected void loadCollectionIntoList(final DatabaseConnector connector, final ObjectAdapter
parent,
-        final boolean makeResolved, final String table, ObjectSpecification specification,
final IdMapping idMapping,
+        final boolean makeResolved, final String table, ObjectSpecification specification,
final IdMappingAbstract idMappingAbstract,
         final List<FieldMapping> fieldMappings, final VersionMapping versionMapping,
final List<ObjectAdapter> list) {
+
         final StringBuffer sql = new StringBuffer();
         sql.append("select ");
-        idMapping.appendColumnNames(sql);
+        idMappingAbstract.appendColumnNames(sql);
 
         sql.append(", ");
         final String columnList = columnList(fieldMappings);
@@ -200,7 +233,7 @@ public class ForeignKeyCollectionMapper 
             sql.append(columnList);
             sql.append(", ");
         }
-        sql.append(versionMapping.appendSelectColumns());
+        sql.append(versionMapping.appendColumnNames());
         sql.append(" from ");
         sql.append(table);
         sql.append(" where ");
@@ -208,7 +241,7 @@ public class ForeignKeyCollectionMapper 
 
         final Results rs = connector.select(sql.toString());
         while (rs.next()) {
-            final Oid oid = idMapping.recreateOid(rs, specification);
+            final Oid oid = idMappingAbstract.recreateOid(rs, specification);
             final ObjectAdapter element = getAdapter(specification, oid);
             loadFields(element, rs, makeResolved, fieldMappings);
             LOG.debug("  element  " + element.getOid());
@@ -258,8 +291,13 @@ public class ForeignKeyCollectionMapper 
             return;
         }
 
+        clearCollectionParent(connector, parent);
+
+        resetCollectionParent(connector, parent, elements);
+    }
+
+    protected void clearCollectionParent(final DatabaseConnector connector, final ObjectAdapter
parent) {
         // Delete collection parent
-        // TODO: for polymorphism, must delete from all? appropriate child tables.
         final StringBuffer sql = new StringBuffer();
         sql.append("update ");
         sql.append(table);
@@ -268,9 +306,11 @@ public class ForeignKeyCollectionMapper 
         sql.append(" where ");
         appendCollectionWhereValues(connector, parent, sql);
         connector.update(sql.toString());
+    }
 
+    protected void resetCollectionParent(final DatabaseConnector connector, final ObjectAdapter
parent,
+        final Iterator<ObjectAdapter> elements) {
         // Reinstall collection parent
-        // TODO: : for polymorphism, must load from all appropriate child tables.
         final StringBuffer update = new StringBuffer();
         update.append("update ");
         update.append(table);

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/PolymorphicForeignKeyInChildCollectionBaseMapper.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/PolymorphicForeignKeyInChildCollectionBaseMapper.java?rev=1170255&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/PolymorphicForeignKeyInChildCollectionBaseMapper.java
(added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/PolymorphicForeignKeyInChildCollectionBaseMapper.java
Tue Sep 13 17:24:13 2011
@@ -0,0 +1,222 @@
+/*
+ *  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.isis.runtimes.dflt.objectstores.sql.auto;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.adapter.oid.Oid;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.apache.isis.core.metamodel.spec.SpecificationLoader;
+import org.apache.isis.core.metamodel.spec.feature.ObjectAssociation;
+import org.apache.isis.runtimes.dflt.objectstores.sql.DatabaseConnector;
+import org.apache.isis.runtimes.dflt.objectstores.sql.FieldMappingLookup;
+import org.apache.isis.runtimes.dflt.objectstores.sql.IdMappingAbstract;
+import org.apache.isis.runtimes.dflt.objectstores.sql.ObjectMapping;
+import org.apache.isis.runtimes.dflt.objectstores.sql.ObjectMappingLookup;
+import org.apache.isis.runtimes.dflt.objectstores.sql.Results;
+import org.apache.isis.runtimes.dflt.objectstores.sql.Sql;
+import org.apache.isis.runtimes.dflt.objectstores.sql.VersionMapping;
+import org.apache.isis.runtimes.dflt.objectstores.sql.jdbc.JdbcConnector;
+import org.apache.isis.runtimes.dflt.objectstores.sql.jdbc.JdbcPolymorphicObjectReferenceMapping;
+import org.apache.isis.runtimes.dflt.objectstores.sql.mapping.FieldMapping;
+import org.apache.isis.runtimes.dflt.runtime.system.context.IsisContext;
+
+/**
+ * Used to map 1-to-many collections by creating, in the collection child table (which may
be an interface or abstract
+ * class), 2 columns per parent collection. The first column is the class type, the second
is the entity ID. The columns
+ * are named by combining the final part of the parent class name and the collection variable
name.
+ * 
+ * You have a choice between this class and {@link PolymorphicForeignKeyInChildCollectionMapper}
+ * 
+ * @author Kevin
+ */
+public class PolymorphicForeignKeyInChildCollectionBaseMapper extends ForeignKeyInChildCollectionMapper
{
+
+    private static final Logger LOG = Logger.getLogger(PolymorphicForeignKeyInChildCollectionBaseMapper.class);
+
+    private final String classColumnName;
+    private final String itemIdColumnName;
+
+    public PolymorphicForeignKeyInChildCollectionBaseMapper(final ObjectAssociation objectAssociation,
+        final String parameterBase, final FieldMappingLookup lookup, final ObjectMappingLookup
objectMapperLookup,
+        final AbstractAutoMapper abstractAutoMapper, final ObjectAssociation field) {
+
+        super(objectAssociation, parameterBase, lookup, objectMapperLookup, abstractAutoMapper,
field);
+
+        classColumnName = Sql.identifier(Sql.sqlName(getForeignKeyName() + "_cls"));
+        itemIdColumnName = Sql.identifier("item_id");
+    }
+
+    @Override
+    public boolean needsTables(final DatabaseConnector connection) {
+        return super.needsTables(connection) || !connection.hasColumn(table, classColumnName);
+    }
+
+    @Override
+    public void createTables(final DatabaseConnector connection) {
+        if (super.needsTables(connection)) {
+            super.createTables(connection);
+        }
+
+        if (!connection.hasColumn(table, classColumnName)) {
+            addColumn(connection, classColumnName, JdbcConnector.TYPE_LONG_STRING());
+            addColumn(connection, itemIdColumnName, JdbcConnector.TYPE_PK());
+        }
+    }
+
+    protected void addColumn(final DatabaseConnector connection, String columnName, String
columnType) {
+        final StringBuffer sql = new StringBuffer();
+        sql.append("alter table ");
+        sql.append(table);
+        sql.append(" add ");
+        sql.append(columnName);
+        sql.append(" ");
+        sql.append(columnType);
+        connection.update(sql.toString());
+    }
+
+    @Override
+    protected void appendCollectionUpdateColumnsToNull(StringBuffer sql) {
+        super.appendCollectionUpdateColumnsToNull(sql);
+        sql.append(", " + classColumnName + "=NULL ");
+    }
+
+    @Override
+    protected void appendCollectionUpdateValues(final DatabaseConnector connector, final
ObjectAdapter parent,
+        final StringBuffer sql) {
+        super.appendCollectionUpdateValues(connector, parent, sql);
+    }
+
+    @Override
+    protected void appendColumnDefinitions(final StringBuffer sql) {
+        super.appendColumnDefinitions(sql);
+    }
+
+    @Override
+    protected void clearCollectionParent(final DatabaseConnector connector, final ObjectAdapter
parent) {
+        // Delete collection parent
+        final StringBuffer sql = new StringBuffer();
+        sql.append("DELETE FROM ");
+        sql.append(table);
+        sql.append(" WHERE ");
+        appendCollectionWhereValues(connector, parent, sql);
+        connector.update(sql.toString());
+    }
+
+    @Override
+    protected void resetCollectionParent(final DatabaseConnector connector, final ObjectAdapter
parent,
+        final Iterator<ObjectAdapter> elements) {
+        LOG.debug("Saving polymorphic list");
+
+        // TODO: Continue appending "insert" IDs while the element specification is the same.
When it changes, commit
+        // current list and start again.
+
+        ObjectSpecification elementSpecification;
+        while (elements.hasNext()) {
+            ObjectAdapter thisAdapter = elements.next();
+            elementSpecification = thisAdapter.getSpecification();
+
+            // Reinstall collection parent
+            final StringBuffer update = new StringBuffer();
+            update.append("INSERT INTO ");
+            update.append(table);
+            update.append(" (");
+            // list of column names
+            super.getIdMapping().appendColumnNames(update);
+            update.append("," + getForeignKeyName());
+            update.append(", " + itemIdColumnName);
+            update.append("," + classColumnName);
+            update.append(") VALUES (");
+
+            // TODO: I'm not sure about reusing the item's own Id as ID for this table..
+            // PK_ID column
+            getIdMapping().appendObjectId(connector, update, thisAdapter.getOid());
+            update.append(",");
+
+            // Foreign key ID column
+            getForeignKeyMapping().appendInsertValues(connector, update, parent);
+            update.append(",");
+
+            // item Id column
+            getIdMapping().appendObjectId(connector, update, thisAdapter.getOid());
+
+            // Class name column
+            update.append(",?)");
+            connector.addToQueryValues(elementSpecification.getFullIdentifier());
+
+            connector.insert(update.toString());
+        }
+    }
+
+    @Override
+    public IdMappingAbstract getIdMapping() {
+        return new JdbcPolymorphicObjectReferenceMapping(itemIdColumnName);
+    }
+
+    @Override
+    protected void loadCollectionIntoList(final DatabaseConnector connector, final ObjectAdapter
parent,
+        final boolean makeResolved, final String table, ObjectSpecification specification,
+        final IdMappingAbstract idMappingAbstract, final List<FieldMapping> fieldMappings,
+        final VersionMapping versionMapping, final List<ObjectAdapter> list) {
+        LOG.debug("Loading polymorphic list");
+
+        final StringBuffer sql = new StringBuffer();
+        sql.append("select ");
+        super.getIdMapping().appendColumnNames(sql);
+
+        sql.append("," + getForeignKeyName());
+        sql.append("," + classColumnName);
+        sql.append("," + itemIdColumnName);
+
+        sql.append(" from ");
+        sql.append(table);
+        sql.append(" where ");
+        appendCollectionWhereValues(connector, parent, sql);
+
+        final Results rs = connector.select(sql.toString());
+
+        SpecificationLoader reflector = IsisContext.getSpecificationLoader();
+        final JdbcPolymorphicObjectReferenceMapping idMapping =
+            (JdbcPolymorphicObjectReferenceMapping) idMappingAbstract;
+
+        while (rs.next()) {
+            ObjectSpecification itemSpecification = reflector.loadSpecification(rs.getString(classColumnName));
+            idMapping.setObjectSpecification(itemSpecification);
+
+            // Load new recordSet for the actual class
+            ObjectMapping itemMapper = objectMapperLookup.getMapping(itemSpecification, connector);
+            final Oid oid = idMapping.recreateOid(rs, itemSpecification);
+            final ObjectAdapter loadedObject = itemMapper.getObject(connector, oid, itemSpecification);
+
+            LOG.debug("  element  " + loadedObject.getOid());
+
+            list.add(loadedObject);
+        }
+        rs.close();
+
+    }
+}

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/PolymorphicForeignKeyInChildCollectionBaseMapper.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/PolymorphicForeignKeyInChildCollectionMapper.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/PolymorphicForeignKeyInChildCollectionMapper.java?rev=1170255&r1=1170254&r2=1170255&view=diff
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/PolymorphicForeignKeyInChildCollectionMapper.java
(original)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/auto/PolymorphicForeignKeyInChildCollectionMapper.java
Tue Sep 13 17:24:13 2011
@@ -33,7 +33,7 @@ import org.apache.isis.core.metamodel.sp
 import org.apache.isis.core.metamodel.spec.feature.ObjectAssociation;
 import org.apache.isis.runtimes.dflt.objectstores.sql.DatabaseConnector;
 import org.apache.isis.runtimes.dflt.objectstores.sql.FieldMappingLookup;
-import org.apache.isis.runtimes.dflt.objectstores.sql.IdMapping;
+import org.apache.isis.runtimes.dflt.objectstores.sql.IdMappingAbstract;
 import org.apache.isis.runtimes.dflt.objectstores.sql.ObjectMapping;
 import org.apache.isis.runtimes.dflt.objectstores.sql.ObjectMappingLookup;
 import org.apache.isis.runtimes.dflt.objectstores.sql.VersionMapping;
@@ -43,6 +43,8 @@ import org.apache.isis.runtimes.dflt.obj
  * Used to map 1-to-many collections by creating, in the child table, 1 column per parent
collection. The column is
  * named by combining the final part of the parent class name and the collection variable
name.
  * 
+ * You have a choice between this class and {@link PolymorphicForeignKeyInChildCollectionBaseMapper}
+ * 
  * @author Kevin
  */
 public class PolymorphicForeignKeyInChildCollectionMapper extends ForeignKeyInChildCollectionMapper
{
@@ -181,8 +183,8 @@ public class PolymorphicForeignKeyInChil
     @Override
     protected void loadCollectionIntoList(final DatabaseConnector connector, final ObjectAdapter
parent,
         final boolean makeResolved, final String table, final ObjectSpecification specification,
-        final IdMapping idMapping, final List<FieldMapping> fieldMappings, final VersionMapping
versionMapping,
-        final List<ObjectAdapter> superList) {
+        final IdMappingAbstract idMappingAbstract, final List<FieldMapping> fieldMappings,
+        final VersionMapping versionMapping, final List<ObjectAdapter> superList) {
         final List<ObjectAdapter> list = new ArrayList<ObjectAdapter>();
         for (int i = 0; i < tables.size(); i++) {
 

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/JdbcPolymorphicObjectReferenceMapping.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/JdbcPolymorphicObjectReferenceMapping.java?rev=1170255&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/JdbcPolymorphicObjectReferenceMapping.java
(added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/JdbcPolymorphicObjectReferenceMapping.java
Tue Sep 13 17:24:13 2011
@@ -0,0 +1,69 @@
+/*
+ *  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.isis.runtimes.dflt.objectstores.sql.jdbc;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.adapter.oid.Oid;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.apache.isis.runtimes.dflt.objectstores.sql.DatabaseConnector;
+import org.apache.isis.runtimes.dflt.objectstores.sql.IdMappingAbstract;
+import org.apache.isis.runtimes.dflt.objectstores.sql.Results;
+import org.apache.isis.runtimes.dflt.objectstores.sql.Sql;
+import org.apache.isis.runtimes.dflt.objectstores.sql.SqlObjectStoreException;
+import org.apache.isis.runtimes.dflt.objectstores.sql.mapping.ObjectReferenceMapping;
+
+public class JdbcPolymorphicObjectReferenceMapping extends IdMappingAbstract implements ObjectReferenceMapping
{
+    private ObjectSpecification specification;
+
+    public JdbcPolymorphicObjectReferenceMapping(final String columnName) {
+        final String idColumn = Sql.sqlName(columnName);
+        setColumn(idColumn);
+    }
+
+    public void setObjectSpecification(ObjectSpecification specification) {
+        this.specification = specification;
+    }
+
+    @Override
+    public void appendUpdateValues(final DatabaseConnector connector, final StringBuffer
sql, final ObjectAdapter object) {
+        sql.append(getColumn());
+        if (object == null) {
+            sql.append("= NULL ");
+        } else {
+            sql.append("= ?");
+            // sql.append(primaryKey(object.getOid()));
+            connector.addToQueryValues(primaryKeyAsObject(object.getOid()));
+        }
+    }
+
+    public ObjectAdapter initializeField(final Results rs) {
+        final Oid oid = recreateOid(rs, specification);
+        if (oid != null) {
+            if (specification.isAbstract()) {
+                throw new SqlObjectStoreException("NOT DEALING WITH POLYMORPHIC ASSOCIATIONS");
+            } else {
+                return getAdapter(specification, oid);
+            }
+        } else {
+            return null;
+        }
+    }
+
+}

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/JdbcPolymorphicObjectReferenceMapping.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-tests-common/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/testsystem/dataclasses/polymorphism/PolyTestClass.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-tests-common/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/testsystem/dataclasses/polymorphism/PolyTestClass.java?rev=1170255&r1=1170254&r2=1170255&view=diff
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-tests-common/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/testsystem/dataclasses/polymorphism/PolyTestClass.java
(original)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-tests-common/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/testsystem/dataclasses/polymorphism/PolyTestClass.java
Tue Sep 13 17:24:13 2011
@@ -52,7 +52,7 @@ public class PolyTestClass extends Abstr
 
     // }}
 
-    // {{ PolyBaseClass
+    // {{ PolyBaseClass collection
     private List<PolyBaseClass> polyBaseClasses = new ArrayList<PolyBaseClass>();
 
     public List<PolyBaseClass> getPolyBaseClasses() {
@@ -78,6 +78,19 @@ public class PolyTestClass extends Abstr
 
     // }}
 
+    // {{ PolyInterface collection
+    private List<PolyInterface> polyInterfaces = new ArrayList<PolyInterface>();
+
+    public List<PolyInterface> getPolyInterfaces() {
+        return polyInterfaces;
+    }
+
+    public void setPolyInterfaces(final List<PolyInterface> polyInterfaces) {
+        this.polyInterfaces = polyInterfaces;
+    }
+
+    // }}
+
     // {{ PolySelfRefClass: Can we store / restore classes that contain self-referencing
collections?
     private PolySelfRefClass polySelfRefClass;
 

Modified: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-tests-common/src/test/java/org/apache/isis/runtimes/dflt/objectstores/sql/PolymorphismTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-tests-common/src/test/java/org/apache/isis/runtimes/dflt/objectstores/sql/PolymorphismTest.java?rev=1170255&r1=1170254&r2=1170255&view=diff
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-tests-common/src/test/java/org/apache/isis/runtimes/dflt/objectstores/sql/PolymorphismTest.java
(original)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-tests-common/src/test/java/org/apache/isis/runtimes/dflt/objectstores/sql/PolymorphismTest.java
Tue Sep 13 17:24:13 2011
@@ -80,6 +80,9 @@ public class PolymorphismTest extends Sq
 
     public void testCreate() throws Exception {
         SqlIntegrationTestSingleton.drop("ISIS_POLYTESTCLASS");
+        SqlIntegrationTestSingleton.drop("ISIS_POLYBASECLASS");
+        SqlIntegrationTestSingleton.drop("ISIS_POLYINTERFACE");
+        SqlIntegrationTestSingleton.drop("ISIS_POLYSUBCLASS");
         SqlIntegrationTestSingleton.drop("ISIS_POLYSUBCLASSONE");
         SqlIntegrationTestSingleton.drop("ISIS_POLYSUBCLASSTWO");
         SqlIntegrationTestSingleton.drop("ISIS_POLYSUBCLASSTHREE");
@@ -123,6 +126,7 @@ public class PolymorphismTest extends Sq
         factory.save(polyIntImpA);
 
         polyTestClass.setPolyInterfaceType(polyIntImpA);
+        polyTestClass.getPolyInterfaces().add(polyIntImpA);
 
         // setup the polyTestClass
         PolySubClassOne polySubClassOne = factory.newPolySubClassOne();
@@ -225,12 +229,22 @@ public class PolymorphismTest extends Sq
         assertEquals(1, list2.size());
     }
 
-    public void testInterfaceLoadCollection() {
+    public void testInterfaceLoadProperty() {
         final PolyTestClass polyTestClass = SqlIntegrationTestSingleton.getStaticPolyTestClass();
         PolyInterface loaded = polyTestClass.getPolyInterfaceType();
         assertEquals(polyIntImpA.getString(), loaded.getString());
     }
 
+    public void testInterfaceLoadCollection() {
+        final PolyTestClass polyTestClass = SqlIntegrationTestSingleton.getStaticPolyTestClass();
+        List<PolyInterface> list = polyTestClass.getPolyInterfaces();
+
+        assertEquals(1, list.size());
+        PolyInterface loaded = list.get(0);
+
+        assertEquals(polyIntImpA.getString(), loaded.getString());
+    }
+
     public void testInterfaceEditSave() {
         final SqlDataClassFactory factory = SqlIntegrationTestSingleton.getSqlDataClassFactory();
         polyIntImpB = factory.newPolyInterfaceImplB();



Mime
View raw message