You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2014/04/09 22:35:11 UTC

svn commit: r1586133 - in /db/torque/torque4/trunk: torque-templates/src/main/java/org/apache/torque/templates/ torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ torque-templates/src/main/resources/org/apache/torque/templates/o...

Author: tfischer
Date: Wed Apr  9 20:35:11 2014
New Revision: 1586133

URL: http://svn.apache.org/r1586133
Log:
TORQUE-315: fix setAndSave so it also works if the cached collection from the object is passed in or the cached collection does not represent the state in the database.

Added:
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/resetterReferencingObject.vm
Modified:
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/TemplateOptionName.java
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FieldHelper.java
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/JavaFieldAttributeName.java
    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/conf/options.properties
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/referencingObjectMethods.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SetAndSaveTest.java

Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/TemplateOptionName.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/TemplateOptionName.java?rev=1586133&r1=1586132&r2=1586133&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/TemplateOptionName.java (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/TemplateOptionName.java Wed Apr  9 20:35:11 2014
@@ -90,6 +90,12 @@ public enum TemplateOptionName implement
     /** The name of the option for the suffix of the adder methods. */
     OM_ADDER_SUFFIX("torque.om.complexObjectModel.adderSuffix"),
 
+    /** The name of the option for the prefix of the resetter methods. */
+    OM_RESETTER_PREFIX("torque.om.complexObjectModel.resetterPrefix"),
+
+    /** The name of the option for the suffix of the resetter methods. */
+    OM_RESETTER_SUFFIX("torque.om.complexObjectModel.resetterSuffix"),
+
     /** The name of the option for the prefix of the initializer methods. */
     OM_INITIALIZER_PREFIX("torque.om.complexObjectModel.initializerPrefix"),
 

Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FieldHelper.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FieldHelper.java?rev=1586133&r1=1586132&r2=1586133&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FieldHelper.java (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FieldHelper.java Wed Apr  9 20:35:11 2014
@@ -58,9 +58,9 @@ final class FieldHelper
      * @return the getter method name, not null.
      */
     public static String getGetterName(
-            String fieldName,
-            String fieldType,
-            ControllerState controllerState)
+            final String fieldName,
+            final String fieldType,
+            final ControllerState controllerState)
     {
         String getterName;
         if (controllerState.getBooleanOption(
@@ -84,7 +84,7 @@ final class FieldHelper
      *
      * @return the setter method name, not null.
      */
-    public static String getSetterName(String fieldName)
+    public static String getSetterName(final String fieldName)
     {
         String setterName = SET + StringUtils.capitalize(fieldName);
         return setterName;
@@ -98,7 +98,7 @@ final class FieldHelper
      *
      * @return the field name, not null.
      */
-    public static String getFieldNameFromSetterName(String setterName)
+    public static String getFieldNameFromSetterName(final String setterName)
     {
         String fieldName = StringUtils.uncapitalize(
                 setterName.substring(SET.length()));
@@ -114,8 +114,8 @@ final class FieldHelper
      * @return the adder method name, not null.
      */
     public static String getAdderName(
-            String fieldName,
-            ControllerState controllerState)
+            final String fieldName,
+            final ControllerState controllerState)
     {
         String adderName
             = controllerState.getOption(
@@ -127,6 +127,27 @@ final class FieldHelper
     }
 
     /**
+     * Returns the name of the resetter method for a field.
+     *
+     * @param fieldName the name of the field, not null.
+     * @param controllerState the current controller state, not null.
+     *
+     * @return the resetter method name, not null.
+     */
+    public static String getResetterName(
+            final String fieldName,
+            final ControllerState controllerState)
+    {
+        String adderName
+            = controllerState.getOption(
+                    TemplateOptionName.OM_RESETTER_PREFIX)
+                + StringUtils.capitalize(fieldName)
+                + controllerState.getOption(
+                      TemplateOptionName.OM_RESETTER_SUFFIX);
+        return adderName;
+    }
+
+    /**
      * Returns the name of the initializer method for a field.
      *
      * @param fieldName the name of the field, not null.
@@ -135,8 +156,8 @@ final class FieldHelper
      * @return the initializer method name, not null.
      */
     public static String getInitializerName(
-            String fieldName,
-            ControllerState controllerState)
+            final String fieldName,
+            final ControllerState controllerState)
     {
         String initializerName
             = controllerState.getOption(
@@ -156,8 +177,8 @@ final class FieldHelper
      * @return the isInitialized method name, not null.
      */
     public static String getIsInitializedName(
-            String fieldName,
-            ControllerState controllerState)
+            final String fieldName,
+            final ControllerState controllerState)
     {
         String initializerName
             = controllerState.getOption(
@@ -179,9 +200,9 @@ final class FieldHelper
      * @return the filler method name, not null.
      */
     public static String getFillerName(
-            String fieldName,
-            String distinctionPart,
-            ControllerState controllerState)
+            final String fieldName,
+            final String distinctionPart,
+            final ControllerState controllerState)
     {
         String fillerName
             = controllerState.getOption(
@@ -204,9 +225,9 @@ final class FieldHelper
      * @return the setAndSave method name, not null.
      */
     public static String getSetAndSaveMethodName(
-            String fieldName,
-            String distinctionPart,
-            ControllerState controllerState)
+            final String fieldName,
+            final String distinctionPart,
+            final ControllerState controllerState)
     {
         String fillerName
             = controllerState.getOption(

Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/JavaFieldAttributeName.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/JavaFieldAttributeName.java?rev=1586133&r1=1586132&r2=1586133&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/JavaFieldAttributeName.java (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/JavaFieldAttributeName.java Wed Apr  9 20:35:11 2014
@@ -91,6 +91,12 @@ public enum JavaFieldAttributeName imple
     ADDER_NAME("adder"),
 
     /**
+     * In case the field is a collection:
+     * The name of the method used to reset (null) the collection.
+     */
+    RESETTER_NAME("resetter"),
+
+    /**
      * In case the field can be filled somehow:
      * The name of the method used to fill the field.
      */

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=1586133&r1=1586132&r2=1586133&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 Wed Apr  9 20:35:11 2014
@@ -230,6 +230,14 @@ public class OMForeignKeyTransformer
                         adderName);
             }
             {
+                String adderName = FieldHelper.getResetterName(
+                        getterSetterFieldName,
+                        controllerState);
+                foreignFieldElement.setAttribute(
+                        JavaFieldAttributeName.RESETTER_NAME,
+                        adderName);
+            }
+            {
                 String initializerName = FieldHelper.getInitializerName(
                         getterSetterFieldName,
                         controllerState);

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties?rev=1586133&r1=1586132&r2=1586133&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties Wed Apr  9 20:35:11 2014
@@ -144,6 +144,13 @@ torque.om.complexObjectModel.foreignFiel
 torque.om.complexObjectModel.adderPrefix = add
 torque.om.complexObjectModel.adderSuffix = 
 
+# Prefix and Suffix for the resetter method on the "local" side of the fk
+# Method name is ${prefix}${referencedObjectClass}${suffix}
+# If necessary ${torque.om.complexObjectModel.localFieldNameRelatedBy} 
+# followed by the column names is appended to this. 
+torque.om.complexObjectModel.resetterPrefix = reset
+torque.om.complexObjectModel.resetterSuffix =
+
 # Prefix and Suffix for the initializer method on the "local" side of the fk
 # Method name is ${prefix}${referencedObjectClass}${suffix}
 # If necessary ${torque.om.complexObjectModel.localFieldNameRelatedBy} 

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml?rev=1586133&r1=1586132&r2=1586133&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml Wed Apr  9 20:35:11 2014
@@ -352,6 +352,10 @@
       <action xsi:type="applyAction"
           outlet="torque.om.dbObject.base.adderReferencingObject"/>
     </mergepoint>
+    <mergepoint name="resetter">
+      <action xsi:type="applyAction"
+          outlet="torque.om.dbObject.base.resetterReferencingObject"/>
+    </mergepoint>
     <mergepoint name="initializer">
       <action xsi:type="applyAction"
           outlet="torque.om.dbObject.base.initializerReferencingObject"/>
@@ -381,6 +385,11 @@
       path="dbObject/base/adderReferencingObject.vm">
   </outlet>
 
+  <outlet name="torque.om.dbObject.base.resetterReferencingObject"
+      xsi:type="velocityOutlet"
+      path="dbObject/base/resetterReferencingObject.vm">
+  </outlet>
+
   <outlet name="torque.om.dbObject.base.initializerReferencingObject"
       xsi:type="velocityOutlet"
       path="dbObject/base/initializerReferencingObject.vm">

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/referencingObjectMethods.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/referencingObjectMethods.vm?rev=1586133&r1=1586132&r2=1586133&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/referencingObjectMethods.vm (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/referencingObjectMethods.vm Wed Apr  9 20:35:11 2014
@@ -31,6 +31,7 @@
 $torqueGen.mergepoint("initializer")
 $torqueGen.mergepoint("isInitialized")
 $torqueGen.mergepoint("adder")
+$torqueGen.mergepoint("resetter")
 #end
 $torqueGen.mergepoint("getter")
 #if ($objectIsCaching)

Added: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/resetterReferencingObject.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/resetterReferencingObject.vm?rev=1586133&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/resetterReferencingObject.vm (added)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/resetterReferencingObject.vm Wed Apr  9 20:35:11 2014
@@ -0,0 +1,41 @@
+## 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.
+##
+######
+##
+## version $Id: adderReferencingObject.vm 1331196 2012-04-27 02:56:12Z tfischer $
+##
+## This template creates the resetter method in the object for the "foreign" part
+## of a foreign key.  
+## The template expects as input a "foreign-field" element inside a 
+## "foreign-key" element from the torque schema, which was generated 
+## by the OMTransformer.  
+##
+#set ( $foreignKeyElement = $torqueGen.getParent() )
+#set ( $localTableElement = $foreignKeyElement.getChild("table") )
+#set ( $localClassName = $localTableElement.getAttribute("dbObjectClassName") )
+#set ( $foreignKeyLocalFieldElement = $foreignKeyElement.getChild("local-field") )
+#set ( $foreignKeyLocalColumnSetter = $foreignKeyLocalFieldElement.getAttribute("setter"))
+    /**
+     * Method called to reset the cache of $fieldContainedType objects
+     * which are related through the $field foreign key attribute.
+     */
+    public void ${resetter}()
+    {
+        $field = null;
+    }
+

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm?rev=1586133&r1=1586132&r2=1586133&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm Wed Apr  9 20:35:11 2014
@@ -128,6 +128,10 @@
             Connection connection) 
         throws TorqueException
     {
+        // make sure the collection cache in toLinkTo represents the current
+        // database state
+        toLinkTo.${resetter}();
+        toLinkTo.${getter}(connection);
 #if ($referencesPrimaryKey)
         ObjectKey localKey = toLinkTo.getPrimaryKey();
 #else
@@ -182,6 +186,9 @@
                 toSaveKeys.add(toSaveKey);
             }
         }
+        
+        // calculate intersection between objects in the database
+        // and objects in the list
         List<${foreignDbObjectClassName}> intersection;
         if (toSaveKeys.isEmpty())
         {

Modified: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SetAndSaveTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SetAndSaveTest.java?rev=1586133&r1=1586132&r2=1586133&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SetAndSaveTest.java (original)
+++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SetAndSaveTest.java Wed Apr  9 20:35:11 2014
@@ -24,6 +24,7 @@ import java.util.List;
 
 import org.apache.torque.BaseDatabaseTestCase;
 import org.apache.torque.ForeignKeySchemaData;
+import org.apache.torque.TorqueException;
 import org.apache.torque.test.dbobject.CompIntegerVarcharFk;
 import org.apache.torque.test.dbobject.CompIntegerVarcharPk;
 import org.apache.torque.test.dbobject.CompPkContainsFk;
@@ -151,6 +152,47 @@ public class SetAndSaveTest extends Base
                 oIntegerPk,
                 fkList);
 
+        assertSetAndSaveForOIntegerPk(testData, oIntegerPk, newNonPkOIntegerFk);
+    }
+
+    /**
+     * Tests whether the setAndSave method works
+     * where the collection is euql to the cached list of the object.
+     *
+     * @throws Exception if the test fails
+     */
+    public void testSetAndSaveCachedCollection() throws Exception
+    {
+        ForeignKeySchemaData.clearTablesInDatabase();
+        ForeignKeySchemaData testData
+                = ForeignKeySchemaData.getDefaultTestData();
+        testData.save();
+
+        OIntegerPk oIntegerPk = testData.getOIntegerPkList().get(0);
+        List<NonPkOIntegerFk> fkList = oIntegerPk.initNonPkOIntegerFks();
+        fkList.clear();
+        // object already associated to this object
+        fkList.add(testData.getNonPkOIntegerFkList().get(1));
+        // object already associated to another object
+        fkList.add(testData.getNonPkOIntegerFkList().get(2));
+        // object not associated yet
+        fkList.add(testData.getNonPkOIntegerFkList().get(3));
+        // new object
+        NonPkOIntegerFk newNonPkOIntegerFk = new NonPkOIntegerFk();
+        newNonPkOIntegerFk.setName("newNonPkOIntegerFk");
+        fkList.add(newNonPkOIntegerFk);
+
+        OIntegerPkPeer.setAndSaveNonPkOIntegerFks(
+                oIntegerPk,
+                fkList);
+
+        assertSetAndSaveForOIntegerPk(testData, oIntegerPk, newNonPkOIntegerFk);
+    }
+
+    private void assertSetAndSaveForOIntegerPk(final ForeignKeySchemaData testData,
+            final OIntegerPk oIntegerPk, final NonPkOIntegerFk newNonPkOIntegerFk)
+            throws TorqueException
+    {
         List<NonPkOIntegerFk> cachedFks
                 = oIntegerPk.getNonPkOIntegerFks();
         assertEquals(4, cachedFks.size());



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