View Javadoc

1   package org.apache.torque.templates.transformer.om;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.text.ParseException;
23  import java.text.SimpleDateFormat;
24  import java.util.Collections;
25  import java.util.Date;
26  import java.util.HashSet;
27  import java.util.Set;
28  import java.util.TimeZone;
29  
30  import org.apache.commons.lang.StringUtils;
31  import org.apache.torque.generator.control.ControllerState;
32  import org.apache.torque.generator.processor.string.Camelbacker;
33  import org.apache.torque.generator.processor.string.WrapReservedJavaWords;
34  import org.apache.torque.generator.source.SourceElement;
35  import org.apache.torque.generator.source.transform.SourceTransformerException;
36  import org.apache.torque.templates.TorqueSchemaAttributeName;
37  import org.apache.torque.templates.TorqueSchemaElementName;
38  import org.apache.torque.templates.TorqueSchemaInheritance;
39  import org.apache.torque.templates.TorqueSchemaJavaType;
40  import org.apache.torque.templates.transformer.SchemaTypeHelper;
41  import org.apache.torque.templates.typemapping.JavaType;
42  import org.apache.torque.templates.typemapping.ResultSetGetter;
43  import org.apache.torque.templates.typemapping.SchemaType;
44  import org.apache.torque.templates.typemapping.SqlType;
45  import org.apache.torque.templates.typemapping.TypeMap;
46  
47  /**
48   * Sets the class names and packages for the OM model.
49   * The id method attribute must already be set on the parent table element
50   * when this transformer is called.
51   */
52  public class OMColumnTransformer
53  {
54      /** The camelbacker to create the java name from the column name. */
55      private static Camelbacker javaNameCamelbacker = new Camelbacker();
56  
57      /** The transformer for inheritance elements. */
58      private static OMInheritanceTransformer inheritanceTransformer
59              = new OMInheritanceTransformer();
60  
61      /** Names which cannot be used as constants for column names. */
62      private static final Set<String> RESERVED_CONSTANT_NAMES;
63  
64      /** The Date format for Dates in Default values. */
65      private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.S";
66  
67      /** Constant for the CURRENT_DATE default value for Dates. */
68      static final String CURRENT_DATE = "CURRENT_DATE";
69  
70      /** Constant for the CURRENT_TIME default value for Dates. */
71      static final String CURRENT_TIME = "CURRENT_TIME";
72  
73      /** Constant for the CURRENT_TIMESTAMP default value for Dates. */
74      static final String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
75  
76      /** Constant for the getDefaultDate method name. */
77      static final String GET_DEFAULT_DATE_METHOD_NAME = "getCurrentDate";
78  
79      /** Constant for the getDefaultTime method name. */
80      static final String GET_DEFAULT_TIME_METHOD_NAME = "getCurrentTime";
81  
82      /** Constant for the getDefaultTimestamp method name. */
83      static final String GET_DEFAULT_TIMESTAMP_METHOD_NAME
84              = "getCurrentTimestamp";
85  
86      /** Prevents reserved java words. */
87      private static WrapReservedJavaWords reservedJavaWordsWrapper
88              = new WrapReservedJavaWords();
89  
90      static
91      {
92          Set<String> reservedConstantNames = new HashSet<String>();
93          reservedConstantNames.add("DATABASE");
94          reservedConstantNames.add("TABLE_NAME");
95          reservedConstantNames.add("TABLE");
96          RESERVED_CONSTANT_NAMES
97              = Collections.unmodifiableSet(reservedConstantNames);
98      }
99  
100     public void transform(
101             SourceElement columnElement,
102             ControllerState controllerState,
103             int columnPosition)
104         throws SourceTransformerException
105     {
106         checkElementName(columnElement);
107         checkColumnNameExists(columnElement);
108         setJavaTypeAttribute(columnElement);
109 
110         columnElement.setAttribute(
111                 ColumnAttributeName.POSITION,
112                 columnPosition);
113         setAttributeDefaultValues(columnElement);
114 
115         SchemaType schemaType = SchemaTypeHelper.getSchemaType(
116                 columnElement,
117                 controllerState);
118         columnElement.setAttribute("schemaType", schemaType);
119         setDomainAttributes(columnElement, controllerState);
120 
121         JavaType fieldJavaType = getFieldJavaType(columnElement, schemaType);
122         columnElement.setAttribute(
123                 JavaFieldAttributeName.FIELD_TYPE,
124                 fieldJavaType.getFullClassName());
125 
126 
127         setPrimitiveTypeAttribute(columnElement, fieldJavaType);
128         setNumberTypeAttribute(columnElement, fieldJavaType);
129         setJavaNameAttribute(columnElement);
130         setFieldNameAttribute(columnElement);
131         setPeerColumnNameAttribute(columnElement);
132         setQualifiedColumnNameAttribute(columnElement);
133         setGetterNameAttribute(columnElement, fieldJavaType, controllerState);
134         setSetterNameAttribute(columnElement);
135         setAccessModifierAttributes(columnElement);
136         setDefaultValueAttribute(columnElement, fieldJavaType);
137         setUseDatabaseDefaultValueAttribute(columnElement);
138         setResultSetGetterAttribute(columnElement, schemaType);
139         setSampleObjectAttribute(columnElement, schemaType);
140 
141         for (SourceElement inheritanceElement : columnElement.getChildren(
142                 TorqueSchemaElementName.INHERITANCE.getName()))
143         {
144             inheritanceTransformer.transform(
145                     inheritanceElement,
146                     controllerState);
147         }
148     }
149 
150     /**
151      * Sets the javaType attribute of the column element
152      * if it is not already set and a default value is set.
153      *
154      * @param columnElement the column element, not null.
155      */
156     protected void setJavaTypeAttribute(SourceElement columnElement)
157     {
158         if (columnElement.getAttribute(TorqueSchemaAttributeName.JAVA_TYPE)
159                 != null)
160         {
161             return;
162         }
163         SourceElement databaseElement = columnElement.getParent().getParent();
164         String defaultJavaType = (String) databaseElement.getAttribute(
165                 TorqueSchemaAttributeName.DEFAULT_JAVA_TYPE);
166         if (defaultJavaType != null)
167         {
168             columnElement.setAttribute(
169                     TorqueSchemaAttributeName.JAVA_TYPE,
170                     defaultJavaType);
171         }
172     }
173 
174     /**
175      * Sets default values for attributes of the column element
176      * if the attribute is not set.
177      * The following attributes are checked:
178      * autoIncrement, protected, inheritance, required, primaryKey
179      *
180      * @param columnElement the column element, not null.
181      */
182     protected void setAttributeDefaultValues(SourceElement columnElement)
183     {
184         // set autoincrement attribute
185         if (columnElement.getAttribute(TorqueSchemaAttributeName.AUTO_INCREMENT)
186                 == null)
187         {
188             String idMethod
189                 = (String) columnElement.getParent().getAttribute(
190                         TorqueSchemaAttributeName.ID_METHOD);
191             // idMethod can not be null because it is already set by the
192             // table transformer
193             if (!"none".equals(idMethod))
194             {
195                 columnElement.setAttribute(
196                         TorqueSchemaAttributeName.AUTO_INCREMENT,
197                         Boolean.TRUE.toString());
198             }
199             else
200             {
201                 columnElement.setAttribute(
202                     TorqueSchemaAttributeName.AUTO_INCREMENT,
203                     Boolean.FALSE.toString());
204             }
205         }
206 
207         // set protected attribute
208         if (columnElement.getAttribute(
209                     TorqueSchemaAttributeName.PROTECTED)
210                 == null)
211         {
212             columnElement.setAttribute(
213                 TorqueSchemaAttributeName.PROTECTED,
214                 Boolean.FALSE.toString());
215         }
216 
217         // set inheritance attribute
218         if (columnElement.getAttribute(
219                     TorqueSchemaAttributeName.INHERITANCE)
220                 == null)
221         {
222             columnElement.setAttribute(
223                 TorqueSchemaAttributeName.INHERITANCE,
224                 Boolean.FALSE.toString());
225         }
226 
227         // set required attribute
228         Object required = columnElement.getAttribute(
229                 TorqueSchemaAttributeName.REQUIRED);
230         if (required == null)
231         {
232             columnElement.setAttribute(
233                     TorqueSchemaAttributeName.REQUIRED,
234                     Boolean.FALSE.toString());
235         }
236 
237         // set primaryKey attribute
238         Object primaryKey = columnElement.getAttribute(
239                 TorqueSchemaAttributeName.PRIMARY_KEY);
240         if (primaryKey == null)
241         {
242             columnElement.setAttribute(
243                     TorqueSchemaAttributeName.PRIMARY_KEY,
244                     Boolean.FALSE.toString());
245         }
246     }
247 
248     protected void setDomainAttributes(
249                 SourceElement columnElement,
250                 ControllerState controllerState)
251             throws SourceTransformerException
252     {
253         SqlType domain = SchemaTypeHelper.getDomain(
254                 columnElement,
255                 controllerState);
256         if (domain == null)
257         {
258             return;
259         }
260         if (columnElement.getAttribute(TorqueSchemaAttributeName.TYPE)
261                     == null
262                 && domain.getSqlTypeName() != null)
263         {
264             columnElement.setAttribute(
265                     TorqueSchemaAttributeName.TYPE,
266                     domain.getSqlTypeName());
267         }
268         if (columnElement.getAttribute(TorqueSchemaAttributeName.DEFAULT)
269                     == null
270                 && domain.getDefaultValue() != null)
271         {
272             columnElement.setAttribute(
273                     TorqueSchemaAttributeName.DEFAULT,
274                     domain.getDefaultValue());
275         }
276         if (columnElement.getAttribute(TorqueSchemaAttributeName.SIZE) == null
277               && domain.getSize() != null)
278         {
279             columnElement.setAttribute(
280                     TorqueSchemaAttributeName.SIZE,
281                     domain.getSize());
282         }
283         if (columnElement.getAttribute(TorqueSchemaAttributeName.SCALE) == null
284               && domain.getScale() != null)
285         {
286             columnElement.setAttribute(
287                     TorqueSchemaAttributeName.SCALE,
288                     domain.getScale());
289         }
290     }
291 
292     /**
293      * Sets the attributes getterAccessModifer and setterAccessModifer
294      * on the column element.
295      *
296      * @param columnElement the column element, not null.
297      */
298     protected void setAccessModifierAttributes(SourceElement columnElement)
299     {
300         boolean isProtected = "true".equals(
301                  columnElement.getAttribute(
302                          TorqueSchemaAttributeName.PROTECTED));
303 
304         String accessModifier;
305         if (isProtected)
306         {
307             accessModifier = "protected";
308         }
309         else
310         {
311             accessModifier = "public";
312         }
313         columnElement.setAttribute(
314                 JavaFieldAttributeName.GETTER_ACCESS_MODIFIER,
315                 accessModifier);
316         columnElement.setAttribute(
317                 JavaFieldAttributeName.SETTER_ACCESS_MODIFIER,
318                 accessModifier);
319     }
320 
321 
322     /**
323      * Returns the java type of the field representing a database column.
324      *
325      * @param columnElement the column element, not null.
326      * @param schemaType the schema type, not null.
327      *
328      * @return the java type of the column
329      */
330     protected JavaType getFieldJavaType(
331                 SourceElement columnElement,
332                 SchemaType schemaType)
333             throws SourceTransformerException
334     {
335         JavaType result;
336         String javaType = (String) columnElement.getAttribute(
337                 TorqueSchemaAttributeName.JAVA_TYPE);
338         if (TorqueSchemaJavaType.OBJECT.getValue().equals(javaType))
339         {
340             result = TypeMap.getJavaObjectType(schemaType);
341         }
342         else if (TorqueSchemaJavaType.PRIMITIVE.getValue().equals(javaType)
343                  || javaType == null)
344         {
345             result = TypeMap.getJavaPrimitiveType(schemaType);
346         }
347         else
348         {
349             String columnName = (String) columnElement.getAttribute(
350                     TorqueSchemaAttributeName.NAME);
351             throw new SourceTransformerException("Unknown javaType "
352                     + javaType
353                     + " in column "
354                     + columnName);
355         }
356         return result;
357     }
358 
359     /**
360      * Checks that the name of the column element is correct.
361      *
362      * @param columnElement the column element, not null.
363      *
364      * @throws IllegalArgumentException if the element name is wrong.
365      */
366     protected void checkElementName(SourceElement columnElement)
367     {
368         if (!TorqueSchemaElementName.COLUMN.getName().equals(
369                 columnElement.getName()))
370         {
371             throw new IllegalArgumentException("Illegal element Name "
372                     + columnElement.getName());
373         }
374     }
375 
376     /**
377      * Checks that the name attribute exists on the column element.
378      *
379      * @param columnElement the column element, not null.
380      *
381      * @throws SourceTransformerException if the name attribute does not exist.
382      */
383     protected void checkColumnNameExists(SourceElement columnElement)
384             throws SourceTransformerException
385     {
386         String columnName
387                 = (String) columnElement.getAttribute(
388                         TorqueSchemaAttributeName.NAME);
389         if (columnName == null)
390         {
391             throw new SourceTransformerException("The attribute "
392                     + TorqueSchemaAttributeName.NAME.getName()
393                     + " on element "
394                     + columnElement.getName()
395                     + " is null");
396         }
397     }
398 
399     /**
400      * Sets the javaName attribute of the column element if it is not
401      * already set.
402      *
403      * @param columnElement the column element, not null.
404      */
405     protected void setJavaNameAttribute(SourceElement columnElement)
406     {
407         if (columnElement.getAttribute(TorqueSchemaAttributeName.JAVA_NAME)
408                 != null)
409         {
410             return;
411         }
412         String columnName = (String) columnElement.getAttribute(
413                 TorqueSchemaAttributeName.NAME);
414         String javaName = javaNameCamelbacker.process(columnName);
415         columnElement.setAttribute(
416                 TorqueSchemaAttributeName.JAVA_NAME,
417                 javaName);
418     }
419 
420     /**
421      * Sets the fieldName attribute of the column element if it is not
422      * already set.
423      * The javaName attribute of the column must be set.
424      *
425      * @param columnElement the column element, not null.
426      */
427     protected void setFieldNameAttribute(SourceElement columnElement)
428     {
429         if (columnElement.getAttribute(JavaFieldAttributeName.FIELD_NAME)
430                 != null)
431         {
432             return;
433         }
434         String javaName = (String) columnElement.getAttribute(
435                 TorqueSchemaAttributeName.JAVA_NAME);
436         String fieldName = StringUtils.uncapitalize(javaName);
437         fieldName = reservedJavaWordsWrapper.process(fieldName);
438         columnElement.setAttribute(
439                 JavaFieldAttributeName.FIELD_NAME,
440                 fieldName);
441     }
442 
443     /**
444      * Sets the peerColumnName attribute of the column element if it is not
445      * already set.
446      *
447      * @param columnElement the column element, not null.
448      */
449     protected void setPeerColumnNameAttribute(SourceElement columnElement)
450     {
451         if (columnElement.getAttribute(ColumnAttributeName.PEER_COLUMN_NAME)
452                 != null)
453         {
454             return;
455         }
456         String columnName = (String) columnElement.getAttribute(
457                 TorqueSchemaAttributeName.NAME);
458         String peerColumnName = columnName.toUpperCase();
459         if (RESERVED_CONSTANT_NAMES.contains(peerColumnName))
460         {
461             peerColumnName = "_" +  peerColumnName;
462         }
463         columnElement.setAttribute(
464                 ColumnAttributeName.PEER_COLUMN_NAME,
465                 peerColumnName);
466     }
467 
468     /**
469      * Sets the qualifiedColumnName attribute of the column element
470      * if it is not already set.
471      *
472      * @param columnElement the column element, not null.
473      */
474     protected void setQualifiedColumnNameAttribute(SourceElement columnElement)
475     {
476         if (columnElement.getAttribute(ColumnAttributeName.QUALIFIED_COLUMN_NAME)
477                 != null)
478         {
479             return;
480         }
481         String tableName = (String) columnElement.getParent().getAttribute(
482                 TorqueSchemaAttributeName.NAME);
483         String columnName = (String) columnElement.getAttribute(
484                 TorqueSchemaAttributeName.NAME);
485         String qualifiedColumnName
486                 = tableName + "." + columnName;
487         columnElement.setAttribute(
488                 ColumnAttributeName.QUALIFIED_COLUMN_NAME,
489                 qualifiedColumnName);
490     }
491 
492     /**
493      * Sets the getterName attribute of the column element
494      * if it is not already set.
495      * The fieldName attribute of the column element must already be set.
496      *
497      * @param columnElement the column element, not null.
498      * @param javaType the java type of the column, not null.
499      * @param controllerState the controller state, not null.
500      */
501     protected void setGetterNameAttribute(
502             SourceElement columnElement,
503             JavaType javaType,
504             ControllerState controllerState)
505     {
506         if (columnElement.getAttribute(JavaFieldAttributeName.GETTER_NAME)
507                 != null)
508         {
509             return;
510         }
511         String fieldName = (String) columnElement.getAttribute(
512                 JavaFieldAttributeName.FIELD_NAME);
513         String getterName = FieldHelper.getGetterName(
514                 fieldName,
515                 javaType.getFullClassName(),
516                 controllerState);
517         columnElement.setAttribute(
518                 JavaFieldAttributeName.GETTER_NAME,
519                 getterName);
520     }
521 
522     /**
523      * Sets the setterName attribute of the column element
524      * if it is not already set.
525      * The fieldName attribute of the column element must already be set.
526      *
527      * @param columnElement the column element, not null.
528      */
529     protected void setSetterNameAttribute(SourceElement columnElement)
530     {
531         if (columnElement.getAttribute(JavaFieldAttributeName.SETTER_NAME)
532                 != null)
533         {
534             return;
535         }
536         String fieldName = (String) columnElement.getAttribute(
537                 JavaFieldAttributeName.FIELD_NAME);
538         String setterName = FieldHelper.getSetterName(fieldName);
539         columnElement.setAttribute(
540                 JavaFieldAttributeName.SETTER_NAME,
541                 setterName);
542     }
543 
544     /**
545      * Sets the primitiveType attribute of the column element
546      * if it is not already set.
547      *
548      * @param columnElement the column element, not null.
549      * @param javaType the type of the java field corresponding to the
550      *        column, not null.
551      */
552     protected void setPrimitiveTypeAttribute(
553             SourceElement columnElement,
554             JavaType javaType)
555     {
556         if (columnElement.getAttribute(ColumnAttributeName.PRIMITIVE_TYPE)
557                 != null)
558         {
559             return;
560         }
561         boolean primitiveFieldType = javaType.isPrimitive();
562         columnElement.setAttribute(
563                 ColumnAttributeName.PRIMITIVE_TYPE,
564                 Boolean.toString(primitiveFieldType));
565     }
566 
567     /**
568      * Sets the numberType attribute of the column element
569      * if it is not already set.
570      *
571      * @param columnElement the column element, not null.
572      * @param javaType the type of the java field corresponding to the
573      *        column, not null.
574      */
575     protected void setNumberTypeAttribute(
576             SourceElement columnElement,
577             JavaType javaType)
578     {
579         if (columnElement.getAttribute(ColumnAttributeName.NUMBER_TYPE)
580                 != null)
581         {
582             return;
583         }
584         boolean numberFieldType = javaType.isNumber();
585         columnElement.setAttribute(
586                 ColumnAttributeName.NUMBER_TYPE,
587                 Boolean.toString(numberFieldType));
588     }
589 
590 
591     /**
592      * Sets the defaultValue attribute of the column element
593      * if it is not already set.
594      *
595      * @param columnElement the column element, not null.
596      * @param javaType the type of the java field corresponding to the
597      *        column, not null.
598      *
599      * @throws SourceTransformerException if an unknown primitive type
600      *         is encountered
601      */
602     protected void setDefaultValueAttribute(
603                 SourceElement columnElement,
604                 JavaType javaType)
605             throws SourceTransformerException
606     {
607         // special case inheritance by class name
608         // In this case, the class name of the object must be written into the
609         // column as default, overriding any SQL default values.
610         if (TorqueSchemaInheritance.SINGLE.getValue().equals(
611                     columnElement.getAttribute(
612                         TorqueSchemaAttributeName.INHERITANCE))
613             && columnElement.getChildren(TorqueSchemaElementName.INHERITANCE)
614                     .isEmpty())
615         {
616             columnElement.setAttribute(
617                     JavaFieldAttributeName.DEFAULT_VALUE,
618                     "getClass().getName()");
619             return;
620         }
621 
622         if (columnElement.getAttribute(JavaFieldAttributeName.DEFAULT_VALUE)
623                 != null)
624         {
625             return;
626         }
627         String defaultAttributeValue = (String) columnElement.getAttribute(
628                 TorqueSchemaAttributeName.DEFAULT);
629 
630         String fieldDefaultValue;
631         if (defaultAttributeValue != null)
632         {
633             boolean useDatabaseDefaultValue = "true".equals(
634                     columnElement.getAttribute(
635                         TorqueSchemaAttributeName.USE_DATABASE_DEFAULT_VALUE));
636             fieldDefaultValue = getDefaultValueWithDefaultSet(
637                     javaType,
638                     defaultAttributeValue,
639                     useDatabaseDefaultValue,
640                     columnElement);
641         }
642         else
643         {
644             fieldDefaultValue = getDefaultValueWithoutDefaultSet(javaType);
645         }
646         columnElement.setAttribute(
647                 JavaFieldAttributeName.DEFAULT_VALUE,
648                 fieldDefaultValue);
649     }
650 
651     /**
652      * Calculates the java default value of a column in case a default value
653      * is set.
654      *
655      * @param javaType The java type of the column.
656      * @param defaultValue The default value from the schema.
657      * @param useDatabaseDefaultValue whether the database default value should
658      *        be used.
659      * @param columnElement the current column element for which
660      *        the default value should be calculated.
661      *
662      * @return The java default value.
663      *
664      * @throws SourceTransformerException if an illegal default value is used.
665      */
666     protected String getDefaultValueWithDefaultSet(
667                 JavaType javaType,
668                 String defaultValue,
669                 boolean useDatabaseDefaultValue,
670                 SourceElement columnElement)
671             throws SourceTransformerException
672     {
673         boolean primitiveFieldType = javaType.isPrimitive();
674         String fieldDefaultValue;
675         if (JavaType.BOOLEAN_PRIMITIVE == javaType)
676         {
677             if ("Y".equals(defaultValue)
678                 || "1".equals(defaultValue)
679                 || "true".equalsIgnoreCase(defaultValue))
680             {
681                 fieldDefaultValue = "true";
682             }
683             else
684             {
685                 fieldDefaultValue = "false";
686             }
687         }
688         else if (JavaType.BOOLEAN_OBJECT == javaType)
689         {
690             if ("Y".equals(defaultValue)
691                 || "1".equals(defaultValue)
692                 || "true".equalsIgnoreCase(defaultValue))
693             {
694                 fieldDefaultValue = "Boolean.TRUE";
695             }
696             else
697             {
698                 fieldDefaultValue = "Boolean.FALSE";
699             }
700         }
701         else if (JavaType.STRING == javaType)
702         {
703             fieldDefaultValue = "\"" + defaultValue + "\"";
704         }
705         else if (JavaType.SHORT_OBJECT == javaType)
706         {
707             // The following is better than casting with (short)
708             // because a range check is performed,
709             fieldDefaultValue = "Short.valueOf(\"" + defaultValue + "\")";
710         }
711         else if (JavaType.BYTE_OBJECT == javaType)
712         {
713             // The following is better than casting with (byte)
714             // because a range check is performed,
715             fieldDefaultValue = "Byte.valueOf(\"" + defaultValue + "\")";
716         }
717         else if (JavaType.INTEGER_OBJECT == javaType)
718         {
719             fieldDefaultValue = "Integer.valueOf(" + defaultValue + ")";
720         }
721         else if (JavaType.LONG_OBJECT == javaType)
722         {
723             fieldDefaultValue = "Long.valueOf(" + defaultValue + "L)";
724         }
725         else if (JavaType.DATE == javaType)
726         {
727             if (CURRENT_DATE.equalsIgnoreCase(defaultValue)
728                     || CURRENT_TIME.equalsIgnoreCase(defaultValue)
729                     || CURRENT_TIMESTAMP.equalsIgnoreCase(defaultValue))
730             {
731                 if (useDatabaseDefaultValue)
732                 {
733                     // if the database default value is used do not use
734                     // current time in java as it might be different
735                     fieldDefaultValue = "null";
736                 }
737                 else
738                 {
739                     // the database does not provide a default so use
740                     // java current time.
741                     if (CURRENT_DATE.equalsIgnoreCase(defaultValue))
742                     {
743                         String methodName;
744                         if (columnElement.getParent().getAttribute(
745                                 TableAttributeName.GET_CURRENT_DATE_METHOD_NAME)
746                             != null)
747                         {
748                             methodName = columnElement.getParent().getAttribute(
749                                     TableAttributeName.GET_CURRENT_DATE_METHOD_NAME)
750                                 .toString();
751                         }
752                         else
753                         {
754                             methodName = GET_DEFAULT_DATE_METHOD_NAME;
755                         }
756                         fieldDefaultValue = methodName + "()";
757                     }
758                     else if (CURRENT_TIME.equalsIgnoreCase(defaultValue))
759                     {
760                         String methodName;
761                         if (columnElement.getParent().getAttribute(
762                                 TableAttributeName.GET_CURRENT_TIME_METHOD_NAME)
763                             != null)
764                         {
765                             methodName = columnElement.getParent().getAttribute(
766                                     TableAttributeName.GET_CURRENT_TIME_METHOD_NAME)
767                                 .toString();
768                         }
769                         else
770                         {
771                             methodName = GET_DEFAULT_TIME_METHOD_NAME;
772                         }
773                         fieldDefaultValue = methodName + "()";
774                     }
775                     else
776                     {
777                         String methodName;
778                         if (columnElement.getParent().getAttribute(
779                                 TableAttributeName.GET_CURRENT_TIMESTAMP_METHOD_NAME)
780                             != null)
781                         {
782                             methodName = columnElement.getParent().getAttribute(
783                                     TableAttributeName.GET_CURRENT_TIMESTAMP_METHOD_NAME)
784                                 .toString();
785                         }
786                         else
787                         {
788                             methodName = GET_DEFAULT_TIMESTAMP_METHOD_NAME;
789                         }
790                         fieldDefaultValue = methodName + "()";
791                     }
792                 }
793             }
794             else
795             {
796                 if (useDatabaseDefaultValue)
797                 {
798                     // if the database default value is used, do not use
799                     // current time in java as it might be different
800                     // and have a custom format.
801                     fieldDefaultValue = "null";
802                 }
803                 else
804                 {
805                     fieldDefaultValue = "new Date("
806                             + getDefaultValueAsDate(defaultValue).getTime()
807                             + "L)";;
808                 }
809             }
810         }
811         else if (primitiveFieldType)
812         {
813             fieldDefaultValue = defaultValue;
814         }
815         else
816         {
817             fieldDefaultValue
818                     = "new " + javaType.getFullClassName()
819                         + "(" + defaultValue + ")";
820         }
821         return fieldDefaultValue;
822     }
823 
824     /**
825      * Parses the default value String as Date.
826      *
827      * @param defaultValue the String to parse.
828      * @return the parsed date.
829      *
830      * @throws SourceTransformerException if the date cannot be parsed.
831      */
832     public static Date getDefaultValueAsDate(String defaultValue)
833             throws SourceTransformerException
834     {
835         try
836         {
837             SimpleDateFormat dateFormat
838                     = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
839             dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
840             return dateFormat.parse(defaultValue);
841         }
842         catch (ParseException e)
843         {
844             throw new SourceTransformerException(
845                 "The default value "
846                     + defaultValue
847                     + " does not match the format String "
848                     + DEFAULT_DATE_FORMAT
849                     + " for date values");
850         }
851     }
852 
853     /**
854      * Calculates the java default value of a column in case a default value
855      * is not set.
856      *
857      * @param javaType The java type of the column.
858      *
859      * @return The java default value.
860      */
861     protected String getDefaultValueWithoutDefaultSet(JavaType javaType)
862             throws SourceTransformerException
863     {
864         String fieldDefaultValue;
865         boolean primitiveFieldType = javaType.isPrimitive();
866         if (primitiveFieldType)
867         {
868             if (JavaType.BOOLEAN_PRIMITIVE == javaType)
869             {
870                 fieldDefaultValue = "false";
871             }
872             else if (JavaType.BYTE_PRIMITIVE == javaType)
873             {
874                 fieldDefaultValue = "(byte) 0";
875             }
876             else if (JavaType.SHORT_PRIMITIVE == javaType)
877             {
878                 fieldDefaultValue = "(short) 0";
879             }
880             else if (JavaType.INTEGER_PRIMITIVE == javaType)
881             {
882                 fieldDefaultValue = "0";
883             }
884             else if (JavaType.LONG_PRIMITIVE == javaType)
885             {
886                 fieldDefaultValue = "0L";
887             }
888             else if (JavaType.FLOAT_PRIMITIVE == javaType)
889             {
890                 fieldDefaultValue = "0";
891             }
892             else if (JavaType.DOUBLE_PRIMITIVE == javaType)
893             {
894                 fieldDefaultValue = "0";
895             }
896             else if (JavaType.CHAR_PRIMITIVE == javaType)
897             {
898                 fieldDefaultValue = "'\0'";
899             }
900             else
901             {
902                 throw new SourceTransformerException(
903                         "unknown primitive type" + javaType);
904             }
905         }
906         else
907         {
908             fieldDefaultValue = "null";
909         }
910         return fieldDefaultValue;
911     }
912 
913     /**
914      * Sets the useDatabaseDefaultValue attribute of the column element to its
915      * default "false" if it is not already set.
916      *
917      * @param columnElement the column element, not null.
918      */
919     protected void setUseDatabaseDefaultValueAttribute(
920             SourceElement columnElement)
921     {
922         if (columnElement.getAttribute(
923                     TorqueSchemaAttributeName.USE_DATABASE_DEFAULT_VALUE)
924                 != null)
925         {
926             return;
927         }
928         columnElement.setAttribute(
929                 TorqueSchemaAttributeName.USE_DATABASE_DEFAULT_VALUE,
930                 Boolean.toString(false));
931     }
932 
933     /**
934      * Sets the resultSetGetter attribute of the column element
935      * if it is not already set.
936      * If the resultSetGetter is a string value, it is converted to
937      * a ResultSetGetter value.
938      *
939      * @param columnElement the column element, not null.
940      * @param schemaType the schema type of the column, not null.
941      */
942     protected void setResultSetGetterAttribute(
943             SourceElement columnElement,
944             SchemaType schemaType)
945     {
946         ResultSetGetter resultSetGetter = null;
947         Object originalValue = columnElement.getAttribute(
948                 ColumnAttributeName.RESULT_SET_GETTER);
949 
950         if (originalValue != null)
951         {
952             if (originalValue instanceof String)
953             {
954                 resultSetGetter = ResultSetGetter.getByMethodName(
955                         (String) originalValue);
956             }
957         }
958         else
959         {
960             resultSetGetter = TypeMap.getResultSetGetter(schemaType);
961         }
962 
963         if (resultSetGetter != null)
964         {
965             columnElement.setAttribute(
966                     ColumnAttributeName.RESULT_SET_GETTER,
967                     resultSetGetter);
968         }
969     }
970 
971     /**
972      * Sets the sampleObject attribute of the column element
973      * if it is not already set.
974      *
975      * @param columnElement the column element, not null.
976      * @param schemaType the schema type of the column, not null.
977      */
978     protected void setSampleObjectAttribute(
979             SourceElement columnElement,
980             SchemaType schemaType)
981     {
982         if (columnElement.getAttribute(ColumnAttributeName.SAMPLE_OBJECT)
983                 != null)
984         {
985             return;
986         }
987 
988         String sampleObject = TypeMap.getJavaObject(schemaType);
989         columnElement.setAttribute(
990                 ColumnAttributeName.SAMPLE_OBJECT,
991                 sampleObject);
992     }
993 
994 }