db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r997809 - in /db/torque/torque4/trunk/torque-templates/src/main: java/org/apache/torque/templates/transformer/om/OMForeignKeyTransformer.java resources/org/apache/torque/templates/om/templates/dbObject/base/bean/objectBeanMethods.vm
Date Thu, 16 Sep 2010 16:00:35 GMT
Author: tfischer
Date: Thu Sep 16 16:00:35 2010
New Revision: 997809

URL: http://svn.apache.org/viewvc?rev=997809&view=rev
Log:
split the transform method in OMForeignKeyTransformer into smaller methods and cleanup the attributes for the created foreign-filed and local-field elements

Modified:
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMForeignKeyTransformer.java
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/bean/objectBeanMethods.vm

Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMForeignKeyTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMForeignKeyTransformer.java?rev=997809&r1=997808&r2=997809&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMForeignKeyTransformer.java (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMForeignKeyTransformer.java Thu Sep 16 16:00:35 2010
@@ -142,8 +142,6 @@ public class OMForeignKeyTransformer
         }
 
         SourceElement localTable = foreignKey.getParent();
-        String localTableName = (String) localTable.getAttribute(
-                TorqueSchemaAttributeName.NAME);
         SourceElement database = localTable.getParent();
 
         String foreignTableName = (String) foreignKey.getAttribute(
@@ -155,34 +153,8 @@ public class OMForeignKeyTransformer
         for (SourceElement reference : foreignKey.getChildren(
                 TorqueSchemaElementName.REFERENCE))
         {
-            // create child element "local" for reference
-            // and add local column to it
-            {
-                String localColumnName = (String)
-                        reference.getAttribute(
-                                TorqueSchemaAttributeName.LOCAL);
-                SourceElement localColumnElement
-                        = new SourceElement(
-                                ReferenceChildElementName.LOCAL_COLUMN);
-                SourceElement column
-                        = FindHelper.findColumn(localTable, localColumnName);
-                localColumnElement.getChildren().add(column);
-                reference.getChildren().add(localColumnElement);
-            }
-            // create child element "foreign" for reference
-            // and add foreign column
-            {
-                String foreignColumnName = (String)
-                        reference.getAttribute(
-                                TorqueSchemaAttributeName.FOREIGN);
-                SourceElement foreignColumnElement
-                        = new SourceElement(
-                                ReferenceChildElementName.FOREIGN_COLUMN);
-                SourceElement column
-                        = FindHelper.findColumn(foreignTable, foreignColumnName);
-                foreignColumnElement.getChildren().add(column);
-                reference.getChildren().add(foreignColumnElement);
-            }
+            createLocalElementForReference(localTable, reference);
+            createForeignElementForReference(foreignTable, reference);
         }
 
         // create reference only if this table was not loaded from
@@ -190,156 +162,8 @@ public class OMForeignKeyTransformer
         if (localTable.getAttribute(TableAttributeName.FROM_EXTERNAL_SCHEMA)
                 == null)
         {
-            // create attributes for a field in the local table referencing
-            // the foreign table. This field will store one instance of the
-            // foreign table's java object.
-            StringBuilder referencedBySuffix = new StringBuilder();
-            List<SourceElement> referencingSameTable
-                    = FindHelper.findForeignKeyByReferencedTable(
-                            localTable,
-                            foreignTableName);
-            if (referencingSameTable.size() > 1
-                    || foreignKey.getAttribute(
-                            TorqueSchemaAttributeName.FOREIGN_TABLE)
-                        .equals(localTableName))
-            {
-                referencedBySuffix.append((String) controllerState.getOption(
-                        LOCAL_FIELD_NAME_RELATED_BY_OPTION));
-                for (SourceElement reference : foreignKey.getChildren(
-                        TorqueSchemaElementName.REFERENCE))
-                {
-                    SourceElement localColumnElement
-                            = reference.getChildren(
-                                    ReferenceChildElementName.LOCAL_COLUMN)
-                                .get(0);
-                    SourceElement localColumn
-                            = localColumnElement.getChildren(
-                                    TorqueSchemaElementName.COLUMN)
-                                .get(0);
-                    String fieldName = (String) localColumn.getAttribute(
-                            JavaFieldAttributeName.FIELD_NAME);
-                    referencedBySuffix.append(
-                            StringUtils.capitalize(fieldName));
-                }
-            }
-            // the field name to create the bean getter and setter names
-            String beanGetterSetterFieldName
-                    = (String) foreignTable.getAttribute(
-                            TableAttributeName.BEAN_CLASS_NAME)
-                        + referencedBySuffix;
-            {
-                // the field name for the variable used
-                String localFieldName = (String) controllerState.getOption(
-                            LOCAL_FIELD_NAME_PREFIX_OPTION)
-                        + foreignTable.getAttribute(
-                                TableAttributeName.DB_OBJECT_CLASS_NAME)
-                        + controllerState.getOption(
-                                    LOCAL_FIELD_NAME_SUFFIX_OPTION)
-                        + referencedBySuffix;
-                // the field name to create the getter and setter names
-                String getterSetterFieldName
-                        = (String) foreignTable.getAttribute(
-                                TableAttributeName.DB_OBJECT_CLASS_NAME)
-                            + referencedBySuffix;
-
-                SourceElement localFieldElement
-                        = new SourceElement(
-                                ForeignKeyChildElementName.LOCAL_FIELD);
-
-                localFieldElement.setAttribute(
-                        JavaFieldAttributeName.FIELD_NAME,
-                        localFieldName.toString());
-
-                String fieldType = (String) foreignTable.getAttribute(
-                        TableAttributeName.DB_OBJECT_CLASS_NAME);
-                localFieldElement.setAttribute(
-                        JavaFieldAttributeName.FIELD_TYPE,
-                        fieldType);
-                localFieldElement.setAttribute(
-                        JavaFieldAttributeName.DEFAULT_VALUE,
-                        "null");
-                {
-                    String getterName = FieldHelper.getGetterName(
-                            getterSetterFieldName,
-                            fieldType,
-                            controllerState);
-                    localFieldElement.setAttribute(
-                            JavaFieldAttributeName.GETTER_NAME,
-                            getterName);
-                }
-                {
-                    String setterName = FieldHelper.getSetterName(
-                            getterSetterFieldName);
-                    localFieldElement.setAttribute(
-                            JavaFieldAttributeName.SETTER_NAME,
-                            setterName);
-                    localFieldElement.setAttribute(
-                            JavaFieldAttributeName.SETTER_THROWS,
-                            "TorqueException");
-                }
-                {
-                    String beanGetterName = FieldHelper.getGetterName(
-                            beanGetterSetterFieldName,
-                            fieldType,
-                            controllerState);
-                    localFieldElement.setAttribute(
-                            ForeignKeyChildAttributeName.BEAN_GETTER,
-                            beanGetterName);
-                }
-                {
-                    String beanSetterName = FieldHelper.getSetterName(
-                            beanGetterSetterFieldName);
-                    localFieldElement.setAttribute(
-                            ForeignKeyChildAttributeName.BEAN_SETTER,
-                            beanSetterName);
-                }
-
-                foreignKey.getChildren().add(localFieldElement);
-            }
-            {
-                // the field name for the variable used
-                String localBeanFieldName = (String) controllerState.getOption(
-                            LOCAL_FIELD_NAME_PREFIX_OPTION)
-                        + foreignTable.getAttribute(
-                                TableAttributeName.BEAN_CLASS_NAME)
-                        + controllerState.getOption(
-                                    LOCAL_FIELD_NAME_SUFFIX_OPTION)
-                        + referencedBySuffix;
-                SourceElement localFieldInBeanElement
-                        = new SourceElement(
-                                ForeignKeyChildElementName.LOCAL_FIELD_IN_BEAN);
-
-                localFieldInBeanElement.setAttribute(
-                        JavaFieldAttributeName.FIELD_NAME,
-                        localBeanFieldName.toString());
-
-                String fieldType = (String) foreignTable.getAttribute(
-                        TableAttributeName.BEAN_CLASS_NAME);
-                localFieldInBeanElement.setAttribute(
-                        JavaFieldAttributeName.FIELD_TYPE,
-                        fieldType);
-                localFieldInBeanElement.setAttribute(
-                        JavaFieldAttributeName.DEFAULT_VALUE,
-                        "null");
-                {
-                    String getterName = FieldHelper.getGetterName(
-                            beanGetterSetterFieldName,
-                            fieldType,
-                            controllerState);
-                    localFieldInBeanElement.setAttribute(
-                            JavaFieldAttributeName.GETTER_NAME,
-                            getterName);
-                }
-                {
-                    String setterName = FieldHelper.getSetterName(
-                            beanGetterSetterFieldName);
-                    localFieldInBeanElement.setAttribute(
-                            JavaFieldAttributeName.SETTER_NAME,
-                            setterName);
-                }
-                foreignKey.getChildren().add(localFieldInBeanElement);
-
-            }
+            addLocalField(foreignKey, controllerState);
+            addLocalFieldInBean(foreignKey, controllerState);
         }
 
         // create backreference only if the foreign table was not loaded from
@@ -347,294 +171,574 @@ public class OMForeignKeyTransformer
         if (foreignTable.getAttribute(TableAttributeName.FROM_EXTERNAL_SCHEMA)
                 == null)
         {
-            // create attributes for a field in the foreign table referencing
-            // the local table. As more than one foreign object can point
-            // to this object, it needs to be a collection.
-            StringBuilder referencedBySuffix = new StringBuilder();
-            List<SourceElement> referencingSameTable
-                    = FindHelper.findForeignKeyByReferencedTable(
-                            localTable,
-                            foreignTableName);
-            if (referencingSameTable.size() > 1)
-            {
-                referencedBySuffix.append((String) controllerState.getOption(
-                        FOREIGN_FIELD_NAME_RELATED_BY_OPTION));
-                for (SourceElement reference : foreignKey.getChildren(
-                        TorqueSchemaElementName.REFERENCE))
-                {
-                    SourceElement localColumnElement
-                            = reference.getChildren(
-                                    ReferenceChildElementName.LOCAL_COLUMN)
-                                .get(0);
-                    SourceElement localColumn
-                            = localColumnElement.getChildren(
-                                    TorqueSchemaElementName.COLUMN)
-                                .get(0);
-                    String fieldName = (String) localColumn.getAttribute(
-                            JavaFieldAttributeName.FIELD_NAME);
-                    referencedBySuffix.append(
-                            StringUtils.capitalize(fieldName));
-                }
-            }
-            // the field name to create the name of the bean getter and setter
-            String beanGetterSetterFieldName
+            addForeignField(foreignKey, controllerState);
+            addForeignFieldInBean(foreignKey, controllerState);
+        }
+    }
+
+   /**
+    * Create a foreign-field element which describes a field
+    * in the foreign table referencing the local table.
+    * As more than one foreign object can point
+    * to this object, the filed needs to be a collection.
+    *
+     * @param foreignKey the foreign-key element to which the foreign-field
+     *        element should be added.
+     * @param controllerState the current controller state.
+    */
+    private void addForeignField(
+            SourceElement foreignKey,
+            ControllerState controllerState)
+    {
+        SourceElement localTable = foreignKey.getParent();
+        SourceElement foreignTable = foreignKey.getChild(
+                TorqueSchemaElementName.TABLE);
+        String referencedBySuffix = getForeignReferencedBySuffix(
+                foreignKey, controllerState);
+        {
+            // the field name for the variable used
+            String foreignFieldName = (String) controllerState.getOption(
+                        FOREIGN_FIELD_NAME_PREFIX_OPTION)
+                    + localTable.getAttribute(
+                            TableAttributeName.DB_OBJECT_CLASS_NAME)
+                    + controllerState.getOption(
+                        FOREIGN_FIELD_NAME_SUFFIX_OPTION)
+                    + referencedBySuffix;
+            // the field name to create the name of the getter and setter
+            String getterSetterFieldName
                     = (String) localTable.getAttribute(
-                            TableAttributeName.BEAN_CLASS_NAME)
+                            TableAttributeName.DB_OBJECT_CLASS_NAME)
                         + referencedBySuffix;
+            SourceElement foreignFieldElement
+                    = new SourceElement(
+                            ForeignKeyChildElementName.FOREIGN_FIELD);
+
+            foreignFieldElement.setAttribute(
+                    JavaFieldAttributeName.FIELD_NAME,
+                    foreignFieldName);
+            foreignFieldElement.setAttribute(
+                    JavaFieldAttributeName.FIELD_ACCESS_MODIFIER,
+                    "protected");
             {
-                // the field name for the variable used
-                String foreignFieldName = (String) controllerState.getOption(
-                            FOREIGN_FIELD_NAME_PREFIX_OPTION)
-                        + localTable.getAttribute(
-                                TableAttributeName.DB_OBJECT_CLASS_NAME)
-                        + controllerState.getOption(
-                            FOREIGN_FIELD_NAME_SUFFIX_OPTION)
-                        + referencedBySuffix;
-                // the field name to create the name of the getter and setter
-                String getterSetterFieldName
-                        = (String) localTable.getAttribute(
-                                TableAttributeName.DB_OBJECT_CLASS_NAME)
-                            + referencedBySuffix;
-                SourceElement foreignFieldElement
-                        = new SourceElement(
-                                ForeignKeyChildElementName.FOREIGN_FIELD);
+                // the field name to cache the used Criteria
+                String criteriaFieldName = "last"
+                        + StringUtils.capitalize(getterSetterFieldName)
+                        + "Criteria";
+                foreignFieldElement.setAttribute(
+                        ForeignKeyChildAttributeName
+                                .FOREIGN_COLUMN_CRITERIA_CACHE_FIELD,
+                        criteriaFieldName);
+            }
 
+            String fieldContainedType = (String) localTable.getAttribute(
+                    TorqueSchemaAttributeName.JAVA_NAME);
+            foreignFieldElement.setAttribute(
+                    JavaFieldAttributeName.FIELD_CONTAINED_TYPE,
+                    fieldContainedType);
+
+            String fieldType = (String) controllerState.getOption(
+                    FOREIGN_FIELD_TYPE_OPTION);
+            if ("true".equals(controllerState.getOption(JAVA5_OPTION)))
+            {
+                fieldType = fieldType + "<" + fieldContainedType + ">";
+            }
+            foreignFieldElement.setAttribute(
+                    JavaFieldAttributeName.FIELD_TYPE,
+                    fieldType);
+
+            foreignFieldElement.setAttribute(
+                    JavaFieldAttributeName.DEFAULT_VALUE,
+                    "null");
+            {
+                String getterName = FieldHelper.getGetterName(
+                            getterSetterFieldName,
+                            fieldType,
+                            controllerState)
+                        + "s";
                 foreignFieldElement.setAttribute(
-                        JavaFieldAttributeName.FIELD_NAME,
-                        foreignFieldName);
+                        JavaFieldAttributeName.GETTER_NAME,
+                        getterName);
+            }
+            {
+                String setterName = FieldHelper.getSetterName(
+                            getterSetterFieldName)
+                        + "s";
                 foreignFieldElement.setAttribute(
-                        JavaFieldAttributeName.FIELD_ACCESS_MODIFIER,
-                        "protected");
-                {
-                    // the field name to cache the used Criteria
-                    String criteriaFieldName = "last"
-                            + StringUtils.capitalize(getterSetterFieldName)
-                            + "Criteria";
-                    foreignFieldElement.setAttribute(
-                            ForeignKeyChildAttributeName
-                                    .FOREIGN_COLUMN_CRITERIA_CACHE_FIELD,
-                            criteriaFieldName);
-                }
-
-
-                String fieldContainedType = (String) localTable.getAttribute(
-                        TorqueSchemaAttributeName.JAVA_NAME);
+                        JavaFieldAttributeName.SETTER_NAME,
+                            setterName);
+            }
+            {
+                String adderName = FieldHelper.getAdderName(
+                        getterSetterFieldName);
                 foreignFieldElement.setAttribute(
-                        JavaFieldAttributeName.FIELD_CONTAINED_TYPE,
-                        fieldContainedType);
-
-                String fieldType = (String) controllerState.getOption(
-                        FOREIGN_FIELD_TYPE_OPTION);
+                        JavaFieldAttributeName.ADDER_NAME,
+                        adderName);
+            }
+            {
+                String initializerName = FieldHelper.getInitializerName(
+                        getterSetterFieldName);
+                foreignFieldElement.setAttribute(
+                        JavaFieldAttributeName.INITIALIZER_NAME,
+                        initializerName);
+            }
+            {
+                String initType = (String) controllerState.getOption(
+                        FOREIGN_FIELD_INIT_TYPE_OPTION);
                 if ("true".equals(controllerState.getOption(JAVA5_OPTION)))
                 {
-                    fieldType = fieldType + "<" + fieldContainedType + ">";
+                    initType = initType + "<" + fieldContainedType + ">";
                 }
                 foreignFieldElement.setAttribute(
-                        JavaFieldAttributeName.FIELD_TYPE,
-                        fieldType);
+                        JavaFieldAttributeName.INITIALIZER_TYPE,
+                        initType);
 
+            }
+            {
+                // Name for a getter in the foreign table to
+                // retrieve entries in the foreign table plus the joined
+                // entries in the local table.
+                String joinGetterFieldName
+                        = StringUtils.capitalize(getterSetterFieldName)
+                        + "sJoin"
+                        + foreignTable.getAttribute(
+                            TorqueSchemaAttributeName.JAVA_NAME);
+                String joinGetterName = FieldHelper.getGetterName(
+                        joinGetterFieldName,
+                        fieldType,
+                        controllerState);
                 foreignFieldElement.setAttribute(
-                        JavaFieldAttributeName.DEFAULT_VALUE,
-                        "null");
-                {
-                    String getterName = FieldHelper.getGetterName(
-                                getterSetterFieldName,
-                                fieldType,
-                                controllerState)
-                            + "s";
-                    foreignFieldElement.setAttribute(
-                            JavaFieldAttributeName.GETTER_NAME,
-                            getterName);
-                }
-                {
-                    String setterName = FieldHelper.getSetterName(
-                                getterSetterFieldName)
-                            + "s";
-                    foreignFieldElement.setAttribute(
-                            JavaFieldAttributeName.SETTER_NAME,
-                                setterName);
-                }
-                {
-                    String adderName = FieldHelper.getAdderName(
-                            getterSetterFieldName);
-                    foreignFieldElement.setAttribute(
-                            JavaFieldAttributeName.ADDER_NAME,
-                            adderName);
-                }
-                {
-                    String initializerName = FieldHelper.getInitializerName(
-                            getterSetterFieldName);
-                    foreignFieldElement.setAttribute(
-                            JavaFieldAttributeName.INITIALIZER_NAME,
-                            initializerName);
-                }
-                {
-                    String beanGetterName = FieldHelper.getGetterName(
-                                beanGetterSetterFieldName,
-                                fieldType,
-                                controllerState)
-                            + "s";
-                    foreignFieldElement.setAttribute(
-                            ForeignKeyChildAttributeName.BEAN_GETTER,
-                            beanGetterName);
-                }
-                {
-                    String beanSetterName = FieldHelper.getSetterName(
-                                beanGetterSetterFieldName)
-                            + "s";
-                    foreignFieldElement.setAttribute(
-                            ForeignKeyChildAttributeName.BEAN_SETTER,
-                            beanSetterName);
-                }
-                {
-                    String initType = (String) controllerState.getOption(
-                            FOREIGN_FIELD_INIT_TYPE_OPTION);
-                    if ("true".equals(controllerState.getOption(JAVA5_OPTION)))
-                    {
-                        initType = initType + "<" + fieldContainedType + ">";
-                    }
-                    foreignFieldElement.setAttribute(
-                            JavaFieldAttributeName.INITIALIZER_TYPE,
-                            initType);
-
-                }
-                {
-                    // Name for a getter in the foreign table to
-                    // retrieve entries in the foreign table plus the joined
-                    // entries in the local table.
-                    String joinGetterFieldName
-                            = StringUtils.capitalize(getterSetterFieldName)
-                            + "sJoin"
-                            + foreignTable.getAttribute(
+                        ForeignKeyChildAttributeName
+                            .FOREIGN_FIELD_JOIN_GETTER,
+                        joinGetterName);
+            }
+            {
+                // Name for a getter in the foreign table to
+                // retrieve entries in the foreign table plus all joined
+                // entries, except the entries in the local table.
+                String joinAllExceptGetterFieldName
+                        = StringUtils.capitalize(getterSetterFieldName)
+                        + "sJoinAllExcept"
+                        + foreignTable.getAttribute(
                                 TorqueSchemaAttributeName.JAVA_NAME);
-                    String joinGetterName = FieldHelper.getGetterName(
-                            joinGetterFieldName,
-                            fieldType,
-                            controllerState);
-                    foreignFieldElement.setAttribute(
-                            ForeignKeyChildAttributeName
-                                .FOREIGN_FIELD_JOIN_GETTER,
-                            joinGetterName);
-                }
-                {
-                    // Name for a getter in the foreign table to
-                    // retrieve entries in the foreign table plus all joined
-                    // entries, except the entries in the local table.
-                    String joinAllExceptGetterFieldName
-                            = StringUtils.capitalize(getterSetterFieldName)
-                            + "sJoinAllExcept"
+                String joinAllExceptGetterName = FieldHelper.getGetterName(
+                        joinAllExceptGetterFieldName,
+                        fieldType,
+                        controllerState);
+                foreignFieldElement.setAttribute(
+                        ForeignKeyChildAttributeName
+                            .FOREIGN_FIELD_JOIN_GETTER,
+                        joinAllExceptGetterName);
+            }
+            {
+                // Name for the doSelectJoinXXX method in the Peer Class
+                // of the foreign table.
+                String peerJoinSelectMethodName
+                        = "doSelectJoin"
                             + foreignTable.getAttribute(
-                                    TorqueSchemaAttributeName.JAVA_NAME);
-                    String joinAllExceptGetterName = FieldHelper.getGetterName(
-                            joinAllExceptGetterFieldName,
-                            fieldType,
-                            controllerState);
-                    foreignFieldElement.setAttribute(
-                            ForeignKeyChildAttributeName
-                                .FOREIGN_FIELD_JOIN_GETTER,
-                            joinAllExceptGetterName);
-                }
-                {
-                    // Name for the doSelectJoinXXX method in the Peer Class
-                    // of the foreign table.
-                    String peerJoinSelectMethodName
-                            = "doSelectJoin"
-                                + foreignTable.getAttribute(
-                                        TorqueSchemaAttributeName.JAVA_NAME)
-                                + referencedBySuffix;
-                    foreignFieldElement.setAttribute(
-                            ForeignKeyChildAttributeName
-                                .PEER_JOIN_SELECT_METHOD,
-                            peerJoinSelectMethodName);
-                }
-                {
-                    // Name for the doSelectJoinAllExceptXXX method
-                    // in the Peer Class of the foreign table.
-                    String peerJoinAllExceptSelectMethodName
-                            = "doSelectJoinAllExcept"
-                                + foreignTable.getAttribute(
-                                        TorqueSchemaAttributeName.JAVA_NAME)
-                                + referencedBySuffix;
-                    foreignFieldElement.setAttribute(
-                            ForeignKeyChildAttributeName
-                                .PEER_JOIN_ALL_EXCEPT_SELECT_METHOD,
-                            peerJoinAllExceptSelectMethodName);
-                }
-                foreignKey.getChildren().add(foreignFieldElement);
+                                    TorqueSchemaAttributeName.JAVA_NAME)
+                            + referencedBySuffix;
+                foreignFieldElement.setAttribute(
+                        ForeignKeyChildAttributeName
+                            .PEER_JOIN_SELECT_METHOD,
+                        peerJoinSelectMethodName);
             }
             {
-                // the field name for the variable used
-                String foreignFieldInBeanName
-                        = (String) controllerState.getOption(
-                                FOREIGN_FIELD_NAME_PREFIX_OPTION)
-                            + localTable.getAttribute(
-                                    TableAttributeName.BEAN_CLASS_NAME)
-                            + controllerState.getOption(
-                                FOREIGN_FIELD_NAME_SUFFIX_OPTION)
+                // Name for the doSelectJoinAllExceptXXX method
+                // in the Peer Class of the foreign table.
+                String peerJoinAllExceptSelectMethodName
+                        = "doSelectJoinAllExcept"
+                            + foreignTable.getAttribute(
+                                    TorqueSchemaAttributeName.JAVA_NAME)
                             + referencedBySuffix;
-                SourceElement foreignFieldInBeanElement
-                        = new SourceElement(
-                            ForeignKeyChildElementName.FOREIGN_FIELD_IN_BEAN);
-
-                foreignFieldInBeanElement.setAttribute(
-                        JavaFieldAttributeName.FIELD_NAME,
-                        foreignFieldInBeanName);
-                foreignFieldInBeanElement.setAttribute(
-                        JavaFieldAttributeName.FIELD_ACCESS_MODIFIER,
-                        "protected");
-
-                String fieldContainedType = (String) localTable.getAttribute(
-                        TableAttributeName.BEAN_CLASS_NAME);
-                foreignFieldInBeanElement.setAttribute(
-                        JavaFieldAttributeName.FIELD_CONTAINED_TYPE,
-                        fieldContainedType);
+                foreignFieldElement.setAttribute(
+                        ForeignKeyChildAttributeName
+                            .PEER_JOIN_ALL_EXCEPT_SELECT_METHOD,
+                        peerJoinAllExceptSelectMethodName);
+            }
+            foreignKey.getChildren().add(foreignFieldElement);
+        }
+    }
 
-                String fieldType = (String) controllerState.getOption(
-                        FOREIGN_FIELD_TYPE_OPTION);
-                if ("true".equals(controllerState.getOption(JAVA5_OPTION)))
-                {
-                    fieldType = fieldType + "<" + fieldContainedType + ">";
-                }
-                foreignFieldInBeanElement.setAttribute(
-                        JavaFieldAttributeName.FIELD_TYPE,
-                        fieldType);
+    /**
+     * Create a foreign-field-in-bean element which describes the referenced
+     * instances of the local table bean object in the foreign table bean class.
+     * As more than one foreign bean can point to this bean,
+     * the field needs to be a collection.
+     *
+     * @param foreignKey the foreign-key element
+     *        to which the local-field-in-bean element should be added.
+     * @param controllerState the current controller state.
+     */
+    private void addForeignFieldInBean(
+            SourceElement foreignKey,
+            ControllerState controllerState)
+    {
+        SourceElement localTable = foreignKey.getParent();
+        String referencedBySuffix = getForeignReferencedBySuffix(
+                foreignKey, controllerState);
+        // the field name to create the name of the bean getter and setter
+        String beanGetterSetterFieldName
+                = (String) localTable.getAttribute(
+                        TableAttributeName.BEAN_CLASS_NAME)
+                    + referencedBySuffix;
+
+        // the field name for the variable used
+        String foreignFieldInBeanName
+                = (String) controllerState.getOption(
+                        FOREIGN_FIELD_NAME_PREFIX_OPTION)
+                    + localTable.getAttribute(
+                            TableAttributeName.BEAN_CLASS_NAME)
+                    + controllerState.getOption(
+                        FOREIGN_FIELD_NAME_SUFFIX_OPTION)
+                    + referencedBySuffix;
+        SourceElement foreignFieldInBeanElement
+                = new SourceElement(
+                    ForeignKeyChildElementName.FOREIGN_FIELD_IN_BEAN);
+
+        foreignFieldInBeanElement.setAttribute(
+                JavaFieldAttributeName.FIELD_NAME,
+                foreignFieldInBeanName);
+        foreignFieldInBeanElement.setAttribute(
+                JavaFieldAttributeName.FIELD_ACCESS_MODIFIER,
+                "protected");
+
+        String fieldContainedType = (String) localTable.getAttribute(
+                TableAttributeName.BEAN_CLASS_NAME);
+        foreignFieldInBeanElement.setAttribute(
+                JavaFieldAttributeName.FIELD_CONTAINED_TYPE,
+                fieldContainedType);
+
+        String fieldType = (String) controllerState.getOption(
+                FOREIGN_FIELD_TYPE_OPTION);
+        if ("true".equals(controllerState.getOption(JAVA5_OPTION)))
+        {
+            fieldType = fieldType + "<" + fieldContainedType + ">";
+        }
+        foreignFieldInBeanElement.setAttribute(
+                JavaFieldAttributeName.FIELD_TYPE,
+                fieldType);
 
-                {
-                    String initType = (String) controllerState.getOption(
-                            FOREIGN_FIELD_INIT_TYPE_OPTION);
-                    if ("true".equals(controllerState.getOption(JAVA5_OPTION)))
-                    {
-                        initType = initType + "<" + fieldContainedType + ">";
-                    }
-                    foreignFieldInBeanElement.setAttribute(
-                            JavaFieldAttributeName.INITIALIZER_TYPE,
-                            initType);
+        {
+            String initType = (String) controllerState.getOption(
+                    FOREIGN_FIELD_INIT_TYPE_OPTION);
+            if ("true".equals(controllerState.getOption(JAVA5_OPTION)))
+            {
+                initType = initType + "<" + fieldContainedType + ">";
+            }
+            foreignFieldInBeanElement.setAttribute(
+                    JavaFieldAttributeName.INITIALIZER_TYPE,
+                    initType);
+        }
 
-                }
+        foreignFieldInBeanElement.setAttribute(
+                JavaFieldAttributeName.DEFAULT_VALUE,
+                "null");
+        {
+            String getterName = FieldHelper.getGetterName(
+                        beanGetterSetterFieldName,
+                        fieldType,
+                        controllerState)
+                    + "s";
+            foreignFieldInBeanElement.setAttribute(
+                    JavaFieldAttributeName.GETTER_NAME,
+                    getterName);
+        }
+        {
+            String setterName = FieldHelper.getSetterName(
+                        beanGetterSetterFieldName)
+                    + "s";
+            foreignFieldInBeanElement.setAttribute(
+                    JavaFieldAttributeName.SETTER_NAME,
+                        setterName);
+        }
+        foreignKey.getChildren().add(foreignFieldInBeanElement);
+    }
 
-                foreignFieldInBeanElement.setAttribute(
-                        JavaFieldAttributeName.DEFAULT_VALUE,
-                        "null");
-                {
-                    String getterName = FieldHelper.getGetterName(
-                                beanGetterSetterFieldName,
-                                fieldType,
-                                controllerState)
-                            + "s";
-                    foreignFieldInBeanElement.setAttribute(
-                            JavaFieldAttributeName.GETTER_NAME,
-                            getterName);
-                }
-                {
-                    String setterName = FieldHelper.getSetterName(
-                                beanGetterSetterFieldName)
-                            + "s";
-                    foreignFieldInBeanElement.setAttribute(
-                            JavaFieldAttributeName.SETTER_NAME,
-                                setterName);
-                }
-                foreignKey.getChildren().add(foreignFieldInBeanElement);
+    /**
+     * Create a local-field-in-bean element which describes the referenced
+     * instance of the foreign table bean object in the local table bean class.
+     * This field will store one instance of the foreign table's bean object.
+     *
+     * @param foreignKey the foreign-key element
+     *        to which the local-field-in-bean element should be added.
+     * @param controllerState the current controller state.
+     */
+    private void addLocalFieldInBean(
+            SourceElement foreignKey,
+            ControllerState controllerState)
+    {
+        SourceElement foreignTable = foreignKey.getChild(
+                TorqueSchemaElementName.TABLE);
+        String referencedBySuffix = getLocalReferencedBySuffix(
+                foreignKey, controllerState);
+        String beanGetterSetterFieldName
+                = (String) foreignTable.getAttribute(
+                        TableAttributeName.BEAN_CLASS_NAME)
+                    + referencedBySuffix;
+        // the field name for the variable used
+        String localBeanFieldName = (String) controllerState.getOption(
+                    LOCAL_FIELD_NAME_PREFIX_OPTION)
+                + foreignTable.getAttribute(
+                        TableAttributeName.BEAN_CLASS_NAME)
+                + controllerState.getOption(
+                            LOCAL_FIELD_NAME_SUFFIX_OPTION)
+                + referencedBySuffix;
+        SourceElement localFieldInBeanElement
+                = new SourceElement(
+                        ForeignKeyChildElementName.LOCAL_FIELD_IN_BEAN);
+
+        localFieldInBeanElement.setAttribute(
+                JavaFieldAttributeName.FIELD_NAME,
+                localBeanFieldName.toString());
+
+        String fieldType = (String) foreignTable.getAttribute(
+                TableAttributeName.BEAN_CLASS_NAME);
+        localFieldInBeanElement.setAttribute(
+                JavaFieldAttributeName.FIELD_TYPE,
+                fieldType);
+        localFieldInBeanElement.setAttribute(
+                JavaFieldAttributeName.DEFAULT_VALUE,
+                "null");
+        {
+            String getterName = FieldHelper.getGetterName(
+                    beanGetterSetterFieldName,
+                    fieldType,
+                    controllerState);
+            localFieldInBeanElement.setAttribute(
+                    JavaFieldAttributeName.GETTER_NAME,
+                    getterName);
+        }
+        {
+            String setterName = FieldHelper.getSetterName(
+                    beanGetterSetterFieldName);
+            localFieldInBeanElement.setAttribute(
+                    JavaFieldAttributeName.SETTER_NAME,
+                    setterName);
+        }
+        foreignKey.getChildren().add(localFieldInBeanElement);
+    }
+
+    /**
+     * Create a local-field element which describes a field in the local table
+     * referencing the foreign table.
+     * This field will store one instance of the foreign table's java object.
+     *
+     * @param foreignKey the foreign-key element to which the local-field
+     *        element should be added.
+     * @param controllerState the current controller state.
+     */
+    private void addLocalField(
+            SourceElement foreignKey,
+            ControllerState controllerState)
+    {
+        SourceElement foreignTable = foreignKey.getChild(
+                TorqueSchemaElementName.TABLE);
+        String referencedBySuffix = getLocalReferencedBySuffix(
+                foreignKey, controllerState);
+        // the field name for the variable used
+        String localFieldName = (String) controllerState.getOption(
+                    LOCAL_FIELD_NAME_PREFIX_OPTION)
+                + foreignTable.getAttribute(
+                        TableAttributeName.DB_OBJECT_CLASS_NAME)
+                + controllerState.getOption(
+                            LOCAL_FIELD_NAME_SUFFIX_OPTION)
+                + referencedBySuffix;
+        // the field name to create the getter and setter names
+        String getterSetterFieldName
+                = (String) foreignTable.getAttribute(
+                        TableAttributeName.DB_OBJECT_CLASS_NAME)
+                    + referencedBySuffix;
+
+        SourceElement localFieldElement
+                = new SourceElement(
+                        ForeignKeyChildElementName.LOCAL_FIELD);
+
+        localFieldElement.setAttribute(
+                JavaFieldAttributeName.FIELD_NAME,
+                localFieldName.toString());
+
+        String fieldType = (String) foreignTable.getAttribute(
+                TableAttributeName.DB_OBJECT_CLASS_NAME);
+        localFieldElement.setAttribute(
+                JavaFieldAttributeName.FIELD_TYPE,
+                fieldType);
+        localFieldElement.setAttribute(
+                JavaFieldAttributeName.DEFAULT_VALUE,
+                "null");
+        {
+            String getterName = FieldHelper.getGetterName(
+                    getterSetterFieldName,
+                    fieldType,
+                    controllerState);
+            localFieldElement.setAttribute(
+                    JavaFieldAttributeName.GETTER_NAME,
+                    getterName);
+        }
+        {
+            String setterName = FieldHelper.getSetterName(
+                    getterSetterFieldName);
+            localFieldElement.setAttribute(
+                    JavaFieldAttributeName.SETTER_NAME,
+                    setterName);
+            localFieldElement.setAttribute(
+                    JavaFieldAttributeName.SETTER_THROWS,
+                    "TorqueException");
+        }
+        foreignKey.getChildren().add(localFieldElement);
+    }
+
+    /**
+     * If a foreign table is referenced more than once from this table,
+     * the local field for the foreign key must be qualified by which
+     * field(s) the foreign table is referenced. This method calculates
+     * this qualifying suffix and returns it.
+     *
+     * @param foreignKey the foreign key element for which the suffix
+     *        is calculated, not null.
+     * @param controllerState the current controller state, not null.
+     *
+     * @return the qualifying suffix, or the empty String if none is needed.
+     *         Not null.
+     */
+    private String getLocalReferencedBySuffix(
+            SourceElement foreignKey,
+            ControllerState controllerState)
+    {
+        SourceElement localTable = foreignKey.getParent();
+        String localTableName = (String) localTable.getAttribute(
+                TorqueSchemaAttributeName.NAME);
+        String foreignTableName = (String) foreignKey.getAttribute(
+                TorqueSchemaAttributeName.FOREIGN_TABLE);
+
+        StringBuilder result = new StringBuilder();
+        List<SourceElement> referencesToSameTable
+                = FindHelper.findForeignKeyByReferencedTable(
+                        localTable,
+                        foreignTableName);
+        if (referencesToSameTable.size() > 1
+                || foreignKey.getAttribute(
+                        TorqueSchemaAttributeName.FOREIGN_TABLE)
+                    .equals(localTableName))
+        {
+            result.append((String) controllerState.getOption(
+                    LOCAL_FIELD_NAME_RELATED_BY_OPTION));
+            for (SourceElement reference : foreignKey.getChildren(
+                    TorqueSchemaElementName.REFERENCE))
+            {
+                SourceElement localColumnElement
+                        = reference.getChildren(
+                                ReferenceChildElementName.LOCAL_COLUMN)
+                            .get(0);
+                SourceElement localColumn
+                        = localColumnElement.getChildren(
+                                TorqueSchemaElementName.COLUMN)
+                            .get(0);
+                String fieldName = (String) localColumn.getAttribute(
+                        JavaFieldAttributeName.FIELD_NAME);
+                result.append(
+                        StringUtils.capitalize(fieldName));
+            }
+        }
+        return result.toString();
+    }
+
+    /**
+     * If a foreign table is referenced more than once from this table,
+     * the foreign field for the foreign key must be qualified by which
+     * field(s) the foreign table is referenced. This method calculates
+     * this qualifying suffix and returns it.
+     *
+     * @param foreignKey the foreign key element for which the suffix
+     *        is calculated, not null.
+     * @param controllerState the current controller state, not null.
+     *
+     * @return the qualifying suffix, or the empty String if none is needed.
+     *         Not null.
+     */
+    private String getForeignReferencedBySuffix(
+            SourceElement foreignKey,
+            ControllerState controllerState)
+    {
+        SourceElement localTable = foreignKey.getParent();
+        String foreignTableName = (String) foreignKey.getAttribute(
+                TorqueSchemaAttributeName.FOREIGN_TABLE);
+
+        StringBuilder result = new StringBuilder();
+        List<SourceElement> referencingSameTable
+                = FindHelper.findForeignKeyByReferencedTable(
+                        localTable,
+                        foreignTableName);
+        if (referencingSameTable.size() > 1)
+        {
+            result.append((String) controllerState.getOption(
+                    FOREIGN_FIELD_NAME_RELATED_BY_OPTION));
+            for (SourceElement reference : foreignKey.getChildren(
+                    TorqueSchemaElementName.REFERENCE))
+            {
+                SourceElement localColumnElement
+                        = reference.getChildren(
+                                ReferenceChildElementName.LOCAL_COLUMN)
+                            .get(0);
+                SourceElement localColumn
+                        = localColumnElement.getChildren(
+                                TorqueSchemaElementName.COLUMN)
+                            .get(0);
+                String fieldName = (String) localColumn.getAttribute(
+                        JavaFieldAttributeName.FIELD_NAME);
+                result.append(
+                        StringUtils.capitalize(fieldName));
             }
         }
+        return result.toString();
+    }
+
+    /**
+     * Creates the child element "foreign-colum" for the reference element
+     * and adds the foreign column as a child of it.
+     *
+     * @param foreignTable the foreign table element.
+     * @param reference the refenced element to enrich.
+     */
+    private void createForeignElementForReference(
+            SourceElement foreignTable,
+            SourceElement reference)
+    {
+        {
+            String foreignColumnName = (String)
+                    reference.getAttribute(
+                            TorqueSchemaAttributeName.FOREIGN);
+            SourceElement foreignColumnElement
+                    = new SourceElement(
+                            ReferenceChildElementName.FOREIGN_COLUMN);
+            SourceElement column
+                    = FindHelper.findColumn(foreignTable, foreignColumnName);
+            foreignColumnElement.getChildren().add(column);
+            reference.getChildren().add(foreignColumnElement);
+        }
+    }
+
+    /**
+     * Creates the child element "local-column" for the reference element
+     * and adds the local column as a child of it.
+     *
+     * @param localTable the local table element.
+     * @param reference the refenced element to enrich.
+     */
+    protected void createLocalElementForReference(
+            SourceElement localTable,
+            SourceElement reference)
+    {
+        {
+            String localColumnName = (String)
+                    reference.getAttribute(
+                            TorqueSchemaAttributeName.LOCAL);
+            SourceElement localColumnElement
+                    = new SourceElement(
+                            ReferenceChildElementName.LOCAL_COLUMN);
+            SourceElement column
+                    = FindHelper.findColumn(localTable, localColumnName);
+            localColumnElement.getChildren().add(column);
+            reference.getChildren().add(localColumnElement);
+        }
     }
 }

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/bean/objectBeanMethods.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/bean/objectBeanMethods.vm?rev=997809&r1=997808&r2=997809&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/bean/objectBeanMethods.vm (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/bean/objectBeanMethods.vm Thu Sep 16 16:00:35 2010
@@ -182,7 +182,8 @@
           #set ( $fieldType = $fieldElement.getAttribute("fieldType") )
           #set ( $fieldContainedType = $fieldElement.getAttribute("fieldContainedType") )
           #set ( $initializerType = $fieldElement.getAttribute("initializerType") )
-          #set ( $beanGetter = $fieldElement.getAttribute("beanGetter") )
+          #set ( $beanFieldElement = $foreignKeyElement.getChild("foreign-field-in-bean") )
+          #set ( $beanGetter = $beanFieldElement.getAttribute("getter") )
           #set ( $adder = $fieldElement.getAttribute("adder") )
         {
             List#if($java5 == "true")<$foreignBeanClassName>#end relatedBeans = bean.${beanGetter}();
@@ -208,7 +209,8 @@
     #set ( $fieldElement = $foreignKeyElement.getChild("local-field") )
     #set ( $fieldType = $fieldElement.getAttribute("fieldType") )
     #set ( $setter = $fieldElement.getAttribute("setter") )
-    #set ( $beanGetter = $fieldElement.getAttribute("beanGetter") )
+    #set ( $beanFieldElement = $foreignKeyElement.getChild("local-field-in-bean") )
+    #set ( $beanGetter = $beanFieldElement.getAttribute("getter") )
         {
             ${foreignBeanClassName} relatedBean = bean.${beanGetter}();
             if (relatedBean != null)



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


Mime
View raw message