db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject cvs commit: db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb CommaListIterator.java
Date Wed, 16 Mar 2005 23:54:34 GMT
tomdz       2005/03/16 15:54:34

  Modified:    lib      Tag: OJB_1_0_RELEASE xdoclet-ojb-module-1.2.2.jar
               src/xdoclet/java/src/xdoclet/modules/ojb/model Tag:
                        OJB_1_0_RELEASE TorqueModelDef.java
                        CollectionDescriptorDef.java
               src/xdoclet/test/xdoclet/modules/ojb/tests Tag:
                        OJB_1_0_RELEASE
                        ReferenceTagForeignkeyAttributeTests.java
                        CollectionTagForeignkeyAttributeTests.java
               src/xdoclet/java/src/xdoclet/modules/ojb/constraints Tag:
                        OJB_1_0_RELEASE ModelConstraints.java
                        CollectionDescriptorConstraints.java
               src/xdoclet/java/src/xdoclet/modules/ojb Tag:
                        OJB_1_0_RELEASE CommaListIterator.java
  Log:
  More sophisticated handling of scenarios with types not mapped to a table that are referenced
by collections/references, and database foreignkey generation
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.1.2.6   +135 -127  db-ojb/lib/Attic/xdoclet-ojb-module-1.2.2.jar
  
  	<<Binary file>>
  
  
  No                   revision
  No                   revision
  1.6.2.6   +56 -5     db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/model/TorqueModelDef.java
  
  Index: TorqueModelDef.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/model/TorqueModelDef.java,v
  retrieving revision 1.6.2.5
  retrieving revision 1.6.2.6
  diff -u -r1.6.2.5 -r1.6.2.6
  --- TorqueModelDef.java	15 Mar 2005 23:58:30 -0000	1.6.2.5
  +++ TorqueModelDef.java	16 Mar 2005 23:54:33 -0000	1.6.2.6
  @@ -352,6 +352,54 @@
       }
   
       /**
  +     * Tries to return the single target table to which the given foreign key columns map
in
  +     * all m:n collections that target this indirection table. 
  +     * 
  +     * @param indirectionTable The indirection table
  +     * @param foreignKeys      The foreign keys columns in the indirection table pointing
back to the
  +     *                         class' table
  +     * @return The table name or <code>null</code> if there is not exactly
one table
  +     */
  +    private String getTargetTable(ModelDef modelDef, String indirectionTable, String foreignKeys)
  +    {
  +        String tableName = null;
  +
  +        for (Iterator classIt = modelDef.getClasses(); classIt.hasNext();)
  +        {
  +            ClassDescriptorDef curClassDef = (ClassDescriptorDef)classIt.next();
  +
  +            if (!curClassDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO,
true))
  +            {
  +                continue;
  +            }
  +            for (Iterator collIt = curClassDef.getCollections(); collIt.hasNext();)
  +            {
  +                CollectionDescriptorDef curCollDef = (CollectionDescriptorDef)collIt.next();
  +
  +                if (!indirectionTable.equals(curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE))
||
  +                    !CommaListIterator.sameLists(foreignKeys, curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY)))
  +                {
  +                    continue;
  +                }
  +                // ok, collection fits
  +                if (tableName != null)
  +                {
  +                    if (!tableName.equals(curClassDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE)))
  +                    {
  +                        // maps to a different table
  +                        return null;
  +                    }
  +                }
  +                else
  +                {
  +                    tableName = curClassDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE);
  +                }
  +            }
  +        }
  +        return tableName;
  +    }
  +
  +    /**
        * Tries to return the single table to which all classes in the hierarchy with the
given
        * class as the root map.
        * 
  @@ -451,14 +499,17 @@
           //   local columns  = columns in indirection table 
           //   remote columns = columns of corresponding primarykeys in ownerclass/elementclass
           ClassDescriptorDef ownerClassDef    = (ClassDescriptorDef)collDef.getOwner();
  -        ClassDescriptorDef elementClassDef  = ((ModelDef)ownerClassDef.getOwner()).getClass(collDef.getProperty(PropertyHelper.OJB_PROPERTY_ELEMENT_CLASS_REF));
  +        ModelDef           modelDef         = (ModelDef)ownerClassDef.getOwner();
  +        ClassDescriptorDef elementClassDef  = modelDef.getClass(collDef.getProperty(PropertyHelper.OJB_PROPERTY_ELEMENT_CLASS_REF));
           ArrayList          localPrimFields  = ownerClassDef.getPrimaryKeys();
           ArrayList          remotePrimFields = elementClassDef.getPrimaryKeys();
  -        String             ownerTable       = getHierarchyTable(ownerClassDef);
  -        String             elementTable     = getHierarchyTable(elementClassDef);
  -        CommaListIterator  localKeys        = new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY));
  +        String             localKeyList     = collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY);
  +        String             remoteKeyList    = collDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY);
  +        String             ownerTable       = getTargetTable(modelDef, tableName, localKeyList);
  +        String             elementTable     = getTargetTable(modelDef, tableName, remoteKeyList);
  +        CommaListIterator  localKeys        = new CommaListIterator(localKeyList);
           CommaListIterator  localKeyDocs     = new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY_DOCUMENTATION));
  -        CommaListIterator  remoteKeys       = new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY));
  +        CommaListIterator  remoteKeys       = new CommaListIterator(remoteKeyList);
           CommaListIterator  remoteKeyDocs    = new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY_DOCUMENTATION));
           ArrayList          localColumns     = new ArrayList();
           ArrayList          remoteColumns    = new ArrayList();
  
  
  
  1.5.2.2   +3 -1      db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/model/CollectionDescriptorDef.java
  
  Index: CollectionDescriptorDef.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/model/CollectionDescriptorDef.java,v
  retrieving revision 1.5.2.1
  retrieving revision 1.5.2.2
  diff -u -r1.5.2.1 -r1.5.2.2
  --- CollectionDescriptorDef.java	1 Mar 2005 21:54:08 -0000	1.5.2.1
  +++ CollectionDescriptorDef.java	16 Mar 2005 23:54:33 -0000	1.5.2.2
  @@ -2,6 +2,8 @@
   
   import java.util.Iterator;
   
  +import xdoclet.modules.ojb.CommaListIterator;
  +
   /* Copyright 2004-2004 The Apache Software Foundation
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
  @@ -69,7 +71,7 @@
               remoteCollDef = (CollectionDescriptorDef)it.next();
               if (indirTable.equals(remoteCollDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE))
&&
                   (this != remoteCollDef) &&
  -                (!hasRemoteKey || remoteKey.equals(remoteCollDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY))))
  +                (!hasRemoteKey || CommaListIterator.sameLists(remoteKey, remoteCollDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY))))
               {
                   return remoteCollDef;
               }
  
  
  
  No                   revision
  No                   revision
  1.6.2.2   +15 -0     db-ojb/src/xdoclet/test/xdoclet/modules/ojb/tests/ReferenceTagForeignkeyAttributeTests.java
  
  Index: ReferenceTagForeignkeyAttributeTests.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/xdoclet/test/xdoclet/modules/ojb/tests/ReferenceTagForeignkeyAttributeTests.java,v
  retrieving revision 1.6.2.1
  retrieving revision 1.6.2.2
  diff -u -r1.6.2.1 -r1.6.2.2
  --- ReferenceTagForeignkeyAttributeTests.java	15 Mar 2005 23:58:30 -0000	1.6.2.1
  +++ ReferenceTagForeignkeyAttributeTests.java	16 Mar 2005 23:54:33 -0000	1.6.2.2
  @@ -500,6 +500,21 @@
               "    class=\"test.B\"\n"+
               ">\n"+
               "    <extent-class class-ref=\"test.C\"/>\n"+
  +            "    <field-descriptor\n"+
  +            "        name=\"idA\"\n"+
  +            "        column=\"idA\"\n"+
  +            "        jdbc-type=\"INTEGER\"\n"+
  +            "        primarykey=\"true\"\n"+
  +            "    >\n"+
  +            "    </field-descriptor>\n"+
  +            "    <field-descriptor\n"+
  +            "        name=\"idB\"\n"+
  +            "        column=\"idB\"\n"+
  +            "        jdbc-type=\"VARCHAR\"\n"+
  +            "        primarykey=\"true\"\n"+
  +            "        length=\"254\"\n"+
  +            "    >\n"+
  +            "    </field-descriptor>\n"+
               "</class-descriptor>\n"+
               "<class-descriptor\n"+
               "    class=\"test.C\"\n"+
  
  
  
  1.5.2.3   +17 -33    db-ojb/src/xdoclet/test/xdoclet/modules/ojb/tests/CollectionTagForeignkeyAttributeTests.java
  
  Index: CollectionTagForeignkeyAttributeTests.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/xdoclet/test/xdoclet/modules/ojb/tests/CollectionTagForeignkeyAttributeTests.java,v
  retrieving revision 1.5.2.2
  retrieving revision 1.5.2.3
  diff -u -r1.5.2.2 -r1.5.2.3
  --- CollectionTagForeignkeyAttributeTests.java	15 Mar 2005 23:58:30 -0000	1.5.2.2
  +++ CollectionTagForeignkeyAttributeTests.java	16 Mar 2005 23:54:33 -0000	1.5.2.3
  @@ -136,39 +136,6 @@
           assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
       }
   
  -    // Test: foreignkey specifies field that exists only in a sub-type
  -    public void testForeignkey5()
  -    {
  -        addClass(
  -            "test.A",
  -            "package test;\n"+
  -            "/** @ojb.class */\n" +
  -            "public class A {\n"+
  -            "  /** @ojb.field primarykey=\"true\" */\n"+
  -            "  private int id;\n"+
  -            "  /** @ojb.collection element-class-ref=\"test.B\"\n"+
  -            "    *                 foreignkey=\"aid\"\n"+
  -            "    */\n"+
  -            "  private java.util.List attr;\n"+
  -            "}");
  -        addClass(
  -            "test.B",
  -            "package test;\n"+
  -            "/** @ojb.class */\n"+
  -            "public class B {}\n");
  -        addClass(
  -            "test.C",
  -            "package test;\n"+
  -            "/** @ojb.class */\n"+
  -            "public class C extends B {\n"+
  -            "  /** @ojb.field */\n"+
  -            "  private int aid;\n"+
  -            "}\n");
  -
  -        assertNull(runOjbXDoclet(OJB_DEST_FILE));
  -        assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
  -    }
  -
       // Test: normal use, foreignkey and primary key have same type
       public void testForeignkey6()
       {
  @@ -894,6 +861,19 @@
               "    class=\"test.B\"\n"+
               ">\n"+
               "    <extent-class class-ref=\"test.C\"/>\n"+
  +            "    <field-descriptor\n"+
  +            "        name=\"aid1\"\n"+
  +            "        column=\"aid1\"\n"+
  +            "        jdbc-type=\"INTEGER\"\n"+
  +            "    >\n"+
  +            "    </field-descriptor>\n"+
  +            "    <field-descriptor\n"+
  +            "        name=\"aid2\"\n"+
  +            "        column=\"aid2\"\n"+
  +            "        jdbc-type=\"VARCHAR\"\n"+
  +            "        length=\"254\"\n"+
  +            "    >\n"+
  +            "    </field-descriptor>\n"+
               "</class-descriptor>\n"+
               "<class-descriptor\n"+
               "    class=\"test.C\"\n"+
  @@ -941,6 +921,10 @@
               "                type=\"VARCHAR\"\n"+
               "                size=\"254\"\n"+
               "        />\n"+
  +            "        <foreign-key foreignTable=\"A\">\n"+
  +            "            <reference local=\"aid1\" foreign=\"id1\"/>\n"+
  +            "            <reference local=\"aid2\" foreign=\"id2\"/>\n"+
  +            "        </foreign-key>\n"+
               "    </table>\n"+
               "</database>",
               runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
  
  
  
  No                   revision
  No                   revision
  1.6.2.5   +13 -10    db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/ModelConstraints.java
  
  Index: ModelConstraints.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/ModelConstraints.java,v
  retrieving revision 1.6.2.4
  retrieving revision 1.6.2.5
  diff -u -r1.6.2.4 -r1.6.2.5
  --- ModelConstraints.java	15 Mar 2005 23:58:29 -0000	1.6.2.4
  +++ ModelConstraints.java	16 Mar 2005 23:54:34 -0000	1.6.2.5
  @@ -17,7 +17,6 @@
   
   import java.util.ArrayList;
   import java.util.Collection;
  -import java.util.HashMap;
   import java.util.Iterator;
   
   import org.apache.commons.collections.SequencedHashMap;
  @@ -137,15 +136,15 @@
               ClassDescriptorDef subTypeDef = (ClassDescriptorDef)it.next();
   
               // find the collection in the element class that has the same indirection table
  -            for (Iterator collIt = elementClassDef.getCollections(); collIt.hasNext();)
  +            for (Iterator collIt = subTypeDef.getCollections(); collIt.hasNext();)
               {
                   CollectionDescriptorDef curCollDef = (CollectionDescriptorDef)collIt.next();
   
                   if (indirTable.equals(curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE))
&&
                       (collDef != curCollDef) &&
  -                    (!hasRemoteKey || remoteKey.equals(curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY)))
&&
  +                    (!hasRemoteKey || CommaListIterator.sameLists(remoteKey, curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY)))
&&
                       (!curCollDef.hasProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY)
||
  -                         localKey.equals(curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY))))
  +                         CommaListIterator.sameLists(localKey, curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY))))
                   {
                       fittingCollections.add(curCollDef);
                   }
  @@ -163,7 +162,7 @@
                   {
                       CollectionDescriptorDef curCollDef = (CollectionDescriptorDef)fittingCollections.get(idx);
   
  -                    if (!foreignKey.equals(curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY)))
  +                    if (!CommaListIterator.sameLists(foreignKey, curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY)))
                       {
                           throw new ConstraintException("Cannot determine the element-side
collection that corresponds to the collection "+
                                                         collDef.getName()+" in type "+collDef.getOwner().getName()+
  @@ -204,10 +203,14 @@
           // first we gather all field names
           for (CommaListIterator it = new CommaListIterator(fkFieldNames); it.hasNext();)
           {
  -            String fieldName = (String)it.next();
  +            String             fieldName = (String)it.next();
  +            FieldDescriptorDef fieldDef  = elementClassDef.getField(fieldName);
   
  -            missingFields.add(fieldName);
  -            fkFields.put(fieldName, null);
  +            if (fieldDef == null)
  +            {
  +                missingFields.add(fieldName);
  +            }
  +            fkFields.put(fieldName, fieldDef);
           }
   
           // next we traverse all sub types and gather fields as we go
  @@ -249,7 +252,7 @@
        */
       private void ensurePKsFromHierarchy(ClassDescriptorDef classDef) throws ConstraintException
       {
  -        HashMap pks = new HashMap();
  +        SequencedHashMap pks = new SequencedHashMap();
   
           for (Iterator it = classDef.getAllExtentClasses(); it.hasNext();)
           {
  @@ -429,7 +432,7 @@
                   // check that the specified remote-foreignkey equals the remote foreignkey
setting
                   String remoteKeys1 = collDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY);
   
  -                if (!remoteKeys1.equals(remoteKeys2))
  +                if (!CommaListIterator.sameLists(remoteKeys1, remoteKeys2))
                   {
                       throw new ConstraintException("The remote-foreignkey property specified
for collection "+collDef.getName()+" in class "+collDef.getOwner().getName()+" doesn't match
the foreignkey property of the corresponding collection "+remoteCollDef.getName()+" in class
"+elementClass.getName());
                   }
  
  
  
  1.4.2.1   +5 -1      db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/CollectionDescriptorConstraints.java
  
  Index: CollectionDescriptorConstraints.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/CollectionDescriptorConstraints.java,v
  retrieving revision 1.4
  retrieving revision 1.4.2.1
  diff -u -r1.4 -r1.4.2.1
  --- CollectionDescriptorConstraints.java	5 Apr 2004 00:32:30 -0000	1.4
  +++ CollectionDescriptorConstraints.java	16 Mar 2005 23:54:34 -0000	1.4.2.1
  @@ -135,7 +135,11 @@
           {
               return;
           }
  -        if (!collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY).equals(collDef.getOriginal().getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY)))
  +
  +        String localFk     = collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY);
  +        String inheritedFk = collDef.getOriginal().getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY);
  +
  +        if (!CommaListIterator.sameLists(localFk, inheritedFk))
           {
               throw new ConstraintException("The foreignkey property has been changed for
the m:n collection "+collDef.getName()+" in class "+collDef.getOwner().getName());
           }
  
  
  
  No                   revision
  No                   revision
  1.4.2.2   +44 -0     db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/CommaListIterator.java
  
  Index: CommaListIterator.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/CommaListIterator.java,v
  retrieving revision 1.4.2.1
  retrieving revision 1.4.2.2
  diff -u -r1.4.2.1 -r1.4.2.2
  --- CommaListIterator.java	1 Feb 2005 21:18:10 -0000	1.4.2.1
  +++ CommaListIterator.java	16 Mar 2005 23:54:34 -0000	1.4.2.2
  @@ -100,4 +100,48 @@
           }
           return false;
       }
  +
  +    /**
  +     * Compares this iterator with the other given one. Note that this consumes
  +     * the iterators, so you should not use them afterwards.
  +     * 
  +     * @param obj The other object
  +     * @return If the other object is a comma list iterator and it delivers the same values
  +     *         as this iterator 
  +     * @see java.lang.Object#equals(java.lang.Object)
  +     */
  +    public boolean equals(Object obj)
  +    {
  +        if (!(obj instanceof CommaListIterator))
  +        {
  +            return false;
  +        }
  +
  +        CommaListIterator otherIt = (CommaListIterator)obj;
  +
  +        while (hasNext() || otherIt.hasNext())
  +        {
  +            if (!hasNext() || !otherIt.hasNext())
  +            {
  +                return false;
  +            }
  +            if (!getNext().equals(otherIt.getNext()))
  +            {
  +                return false;
  +            }
  +        }
  +        return true;
  +    }
  +
  +    /**
  +     * Compares the two comma-separated lists.
  +     * 
  +     * @param list1 The first list
  +     * @param list2 The second list
  +     * @return <code>true</code> if the lists are equal
  +     */
  +    public static boolean sameLists(String list1, String list2)
  +    {
  +        return new CommaListIterator(list1).equals(new CommaListIterator(list2));
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message