db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1333881 - /db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
Date Fri, 04 May 2012 11:25:00 GMT
Author: tfischer
Date: Fri May  4 11:25:00 2012
New Revision: 1333881

URL: http://svn.apache.org/viewvc?rev=1333881&view=rev
Log:
- refactor a long method
- make all methods protected to ease inheritance

Modified:
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java

Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java?rev=1333881&r1=1333880&r2=1333881&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
(original)
+++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
Fri May  4 11:25:00 2012
@@ -268,7 +268,7 @@ public class OMColumnTransformer
      *
      * @param columnElement the column element, not null.
      */
-    private void setAccessModifierAttributes(SourceElement columnElement)
+    protected void setAccessModifierAttributes(SourceElement columnElement)
     {
         boolean isProtected = "true".equals(
                  columnElement.getAttribute(
@@ -597,138 +597,174 @@ public class OMColumnTransformer
         {
             return;
         }
-        String defaultValue = (String) columnElement.getAttribute(
+        String defaultAttributeValue = (String) columnElement.getAttribute(
                 TorqueSchemaAttributeName.DEFAULT);
-        boolean primitiveFieldType = javaType.isPrimitive();
 
         String fieldDefaultValue;
-        if (defaultValue != null)
+        if (defaultAttributeValue != null)
         {
-            if (JavaType.BOOLEAN_PRIMITIVE == javaType)
+            fieldDefaultValue = getDefaultValueWithDefaultSet(
+                    javaType, defaultAttributeValue);
+        }
+        else
+        {
+            fieldDefaultValue = getDefaultValueWithoutDefaultSet(javaType);
+        }
+        columnElement.setAttribute(
+                JavaFieldAttributeName.DEFAULT_VALUE,
+                fieldDefaultValue);
+    }
+
+    /**
+     * Calculates the java default value of a column in case a default value
+     * is set.
+     *
+     * @param javaType The java type of the column.
+     * @param defaultValue The default value from the schema.
+     *
+     * @return The java default value.
+     */
+    protected String getDefaultValueWithDefaultSet(
+            JavaType javaType,
+            String defaultValue)
+    {
+        boolean primitiveFieldType = javaType.isPrimitive();
+        String fieldDefaultValue;
+        if (JavaType.BOOLEAN_PRIMITIVE == javaType)
+        {
+            if ("Y".equals(defaultValue)
+                || "1".equals(defaultValue)
+                || "true".equalsIgnoreCase(defaultValue))
             {
-                if ("Y".equals(defaultValue)
-                    || "1".equals(defaultValue)
-                    || "true".equalsIgnoreCase(defaultValue))
-                {
-                    fieldDefaultValue = "true";
-                }
-                else
-                {
-                    fieldDefaultValue = "false";
-                }
-            }
-            else if (JavaType.BOOLEAN_OBJECT == javaType)
-            {
-                if ("Y".equals(defaultValue)
-                    || "1".equals(defaultValue)
-                    || "true".equalsIgnoreCase(defaultValue))
-                {
-                    fieldDefaultValue = "Boolean.TRUE";
-                }
-                else
-                {
-                    fieldDefaultValue = "Boolean.FALSE";
-                }
-            }
-            else if (JavaType.STRING == javaType)
-            {
-                fieldDefaultValue = "\"" + defaultValue + "\"";
-            }
-            else if (JavaType.SHORT_OBJECT == javaType)
-            {
-                // The following is better than casting with (short)
-                // because a range check is performed,
-                fieldDefaultValue = "Short.valueOf(\"" + defaultValue + "\")";
-            }
-            else if (JavaType.BYTE_OBJECT == javaType)
-            {
-                // The following is better than casting with (byte)
-                // because a range check is performed,
-                fieldDefaultValue = "Byte.valueOf(\"" + defaultValue + "\")";
-            }
-            else if (JavaType.INTEGER_OBJECT == javaType)
-            {
-                fieldDefaultValue = "Integer.valueOf(" + defaultValue + ")";
-            }
-            else if (JavaType.LONG_OBJECT == javaType)
-            {
-                fieldDefaultValue = "Long.valueOf(" + defaultValue + "L)";
-            }
-            else if (JavaType.DATE == javaType)
-            {
-                if ("CURRENT_DATE".equalsIgnoreCase(defaultValue)
-                        || "CURRENT_TIME".equalsIgnoreCase(defaultValue)
-                        || "CURRENT_TIMESTAMP".equalsIgnoreCase(defaultValue))
-                {
-                    fieldDefaultValue = "null";
-                }
-                else
-                {
-                    fieldDefaultValue = "new " + javaType.getFullClassName()
-                            + "(" + defaultValue + ")";
-                }
+                fieldDefaultValue = "true";
             }
-            else if (primitiveFieldType)
+            else
+            {
+                fieldDefaultValue = "false";
+            }
+        }
+        else if (JavaType.BOOLEAN_OBJECT == javaType)
+        {
+            if ("Y".equals(defaultValue)
+                || "1".equals(defaultValue)
+                || "true".equalsIgnoreCase(defaultValue))
             {
-                fieldDefaultValue = defaultValue;
+                fieldDefaultValue = "Boolean.TRUE";
             }
             else
             {
-                fieldDefaultValue
-                        = "new " + javaType.getFullClassName()
-                            + "(" + defaultValue + ")";
+                fieldDefaultValue = "Boolean.FALSE";
             }
         }
+        else if (JavaType.STRING == javaType)
+        {
+            fieldDefaultValue = "\"" + defaultValue + "\"";
+        }
+        else if (JavaType.SHORT_OBJECT == javaType)
+        {
+            // The following is better than casting with (short)
+            // because a range check is performed,
+            fieldDefaultValue = "Short.valueOf(\"" + defaultValue + "\")";
+        }
+        else if (JavaType.BYTE_OBJECT == javaType)
+        {
+            // The following is better than casting with (byte)
+            // because a range check is performed,
+            fieldDefaultValue = "Byte.valueOf(\"" + defaultValue + "\")";
+        }
+        else if (JavaType.INTEGER_OBJECT == javaType)
+        {
+            fieldDefaultValue = "Integer.valueOf(" + defaultValue + ")";
+        }
+        else if (JavaType.LONG_OBJECT == javaType)
+        {
+            fieldDefaultValue = "Long.valueOf(" + defaultValue + "L)";
+        }
+        else if (JavaType.DATE == javaType)
+        {
+            if ("CURRENT_DATE".equalsIgnoreCase(defaultValue)
+                    || "CURRENT_TIME".equalsIgnoreCase(defaultValue)
+                    || "CURRENT_TIMESTAMP".equalsIgnoreCase(defaultValue))
+            {
+                fieldDefaultValue = "null";
+            }
+            else
+            {
+                fieldDefaultValue = "new " + javaType.getFullClassName()
+                        + "(" + defaultValue + ")";
+            }
+        }
+        else if (primitiveFieldType)
+        {
+            fieldDefaultValue = defaultValue;
+        }
         else
         {
-            if (primitiveFieldType)
+            fieldDefaultValue
+                    = "new " + javaType.getFullClassName()
+                        + "(" + defaultValue + ")";
+        }
+        return fieldDefaultValue;
+    }
+
+    /**
+     * Calculates the java default value of a column in case a default value
+     * is not set.
+     *
+     * @param javaType The java type of the column.
+     *
+     * @return The java default value.
+     */
+    protected String getDefaultValueWithoutDefaultSet(JavaType javaType)
+            throws SourceTransformerException
+    {
+        String fieldDefaultValue;
+        boolean primitiveFieldType = javaType.isPrimitive();
+        if (primitiveFieldType)
+        {
+            if (JavaType.BOOLEAN_PRIMITIVE == javaType)
+            {
+                fieldDefaultValue = "false";
+            }
+            else if (JavaType.BYTE_PRIMITIVE == javaType)
             {
-                if (JavaType.BOOLEAN_PRIMITIVE == javaType)
-                {
-                    fieldDefaultValue = "false";
-                }
-                else if (JavaType.BYTE_PRIMITIVE == javaType)
-                {
-                    fieldDefaultValue = "(byte) 0";
-                }
-                else if (JavaType.SHORT_PRIMITIVE == javaType)
-                {
-                    fieldDefaultValue = "(short) 0";
-                }
-                else if (JavaType.INTEGER_PRIMITIVE == javaType)
-                {
-                    fieldDefaultValue = "0";
-                }
-                else if (JavaType.LONG_PRIMITIVE == javaType)
-                {
-                    fieldDefaultValue = "0L";
-                }
-                else if (JavaType.FLOAT_PRIMITIVE == javaType)
-                {
-                    fieldDefaultValue = "0";
-                }
-                else if (JavaType.DOUBLE_PRIMITIVE == javaType)
-                {
-                    fieldDefaultValue = "0";
-                }
-                else if (JavaType.CHAR_PRIMITIVE == javaType)
-                {
-                    fieldDefaultValue = "'\0'";
-                }
-                else
-                {
-                    throw new SourceTransformerException(
-                            "unknown primitive type" + javaType);
-                }
+                fieldDefaultValue = "(byte) 0";
+            }
+            else if (JavaType.SHORT_PRIMITIVE == javaType)
+            {
+                fieldDefaultValue = "(short) 0";
+            }
+            else if (JavaType.INTEGER_PRIMITIVE == javaType)
+            {
+                fieldDefaultValue = "0";
+            }
+            else if (JavaType.LONG_PRIMITIVE == javaType)
+            {
+                fieldDefaultValue = "0L";
+            }
+            else if (JavaType.FLOAT_PRIMITIVE == javaType)
+            {
+                fieldDefaultValue = "0";
+            }
+            else if (JavaType.DOUBLE_PRIMITIVE == javaType)
+            {
+                fieldDefaultValue = "0";
+            }
+            else if (JavaType.CHAR_PRIMITIVE == javaType)
+            {
+                fieldDefaultValue = "'\0'";
             }
             else
             {
-                fieldDefaultValue = "null";
+                throw new SourceTransformerException(
+                        "unknown primitive type" + javaType);
             }
         }
-        columnElement.setAttribute(
-                JavaFieldAttributeName.DEFAULT_VALUE,
-                fieldDefaultValue);
+        else
+        {
+            fieldDefaultValue = "null";
+        }
+        return fieldDefaultValue;
     }
 
     /**



---------------------------------------------------------------------
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