root/trunk/src/java/org/jcoderz/phoenix/cmpgen2/GenerateHelper.vtl

Revision 1, 9.3 kB (checked in by amandel, 6 years ago)

Tags dir

Line 
1##
2## Special treatment for compound columns
3##
4#macro(compoundColumn $compoundColumn)
5#set($compoundJavaField = $cmpgen.sqlNameToJavaName($compoundColumn))
6#if($column.getJavaType().endsWith("Amount"))
7#set($compoundType = "Currency")
8#set($fqCompoundType = "java.util.Currency")
9#elseif($column.getJavaType().endsWith("Period"))
10#set($compoundType = "Timestamp")
11#set($fqCompoundType = "java.sql.Timestamp")
12#end
13#if(!$column.isPeriodDefined())
14#if($column.isNotNull())
15#set($javaType = "long")
16#set($fqJavaType = "long")
17#else
18#set($javaType = "Long")
19#set($fqJavaType = "java.lang.Long")
20#end
21#else
22#set($javaType = "Timestamp")
23#set($fqJavaType = "java.sql.Timestamp")
24#end
25#set($javaFieldName = $cmpgen.sqlNameToJavaName($column.getColumnName()))
26#set($fqComplexType = $column.getJavaType())
27#set($complexType = $cmpgen.unqualifyType($column.getJavaType()))
28#set($storeMethodSig = $column.getStoreMethod())
29#set($loadMethodSig = $column.getLoadMethod())
30#set($loadMethod = $loadMethodSig.substring(0, $loadMethodSig.indexOf("(")).trim())
31#set($storeMethod = $storeMethodSig.substring(0, $storeMethodSig.indexOf("(")).trim())
32#set($simpleGetterName = "get${javaFieldName}As$cmpgen.capitalize($javaType)")
33#set($simpleSetterName = "set${javaFieldName}As$cmpgen.capitalize($javaType)")
34#set($compoundGetterWithType = "get${compoundJavaField}As$cmpgen.capitalize($javaType)")
35#set($compoundSetterWithType = "set${compoundJavaField}As$cmpgen.capitalize($javaType)")
36#set($compoundGetter = "get${compoundJavaField}")
37#set($compoundSetter = "set${compoundJavaField}")
38   /**
39    * Returns the value of the $javaFieldName field as ${complexType}.
40    * @return the value of the $javaFieldName field as ${complexType}.
41    * @throws RuntimeException if the DB value can't
42    *         be converted to the complex type
43    * @ejb.interface-method
44    */
45#if(!$column.isPeriodDefined())
46   public $complexType get$javaFieldName ()
47#else
48   public Period get$column.getPeriodFieldName() ()
49#end
50         throws InconsistentDatabaseException
51   {
52      final $fqJavaType value = $simpleGetterName();
53#if(!$column.isPeriodDefined())
54      final String currencyStr = ${compoundGetter}();
55      final $fqCompoundType comp;
56      if (currencyStr != null)
57      {
58         comp = java.util.Currency.getInstance(currencyStr);
59      }
60      else
61      {
62         comp = null;
63      }
64#else
65## CHECKME: do we need to handle nullable periods?
66      final $fqJavaType comp
67            = ${compoundGetterWithType}();
68#end
69      final $complexType result;
70#if($column.isNotNull())
71      if (comp == null)
72      {
73         // hey! $compoundColumn must not be null if base column is not nullable
74        throw new InconsistentDatabaseException (
75              "null",
76              "$compoundColumn",
77              "$stmt.getTableName()",
78              "$fqJavaType");
79      }
80      else
81      {
82#if(!$column.isPeriodDefined())
83         result = ${complexType}.fromLong(value, comp);
84#else
85         result = ${complexType}.createPeriod(
86               Date.fromSqlTimestamp($simpleGetterName()),
87               Date.fromSqlTimestamp(${compoundGetterWithType}()));
88#end
89      }
90#else
91      if (value != null && comp != null)
92      {
93         try
94         {
95#if(!$column.isPeriodDefined())
96            result = ${complexType}.fromLong(value.longValue(), comp);
97#else
98            result = ${complexType}.createPeriod(
99                  Date.fromSqlTimestamp($simpleGetterName()),
100                  Date.fromSqlTimestamp(${compoundGetterWithType}()));
101#end
102         }
103         catch (Exception x)
104         {
105            throw new InconsistentDatabaseException (
106               "base = " + value
107               + ", compound = " + comp,
108               "$column.getColumnName(), $compoundColumn",
109               "$stmt.getTableName()",
110               "$fqComplexType",
111               x);
112         }
113      }
114      else if (value == null && comp == null)
115      {
116         result = null;
117      }
118      else
119      {
120         throw new InconsistentDatabaseException (
121            "base = " + value
122            + ", compound = " + comp,
123            "$column.getColumnName(), $compoundColumn",
124            "$stmt.getTableName()",
125            "$fqComplexType");
126      }
127#end
128      return result;
129   }
130
131   /**
132    * Returns the value of the $javaFieldName field as ${javaType}.
133    * @return the value of the $javaFieldName field as ${javaType}.
134    */
135   public abstract $fqJavaType $simpleGetterName ();
136
137   /**
138    * Sets the value of the $javaFieldName field as ${complexType}.
139    * @param value the value of the $javaFieldName field as ${complexType}.
140    * @ejb.interface-method
141    */
142#if(!$column.isPeriodDefined())
143   public void set$javaFieldName ($complexType value)
144#else
145   public void set$column.getPeriodFieldName() ($complexType value)
146#end
147   {
148#if($column.isNotNull())
149      Assert.notNull(value, "value");
150#if(!$column.isPeriodDefined())
151      ${simpleSetterName}(value.getValue());
152      ${compoundSetter}(value.getCurrency().toString());
153#else
154      ${simpleSetterName}(value.getStartTime().toSqlTimestamp());
155      ${compoundSetterWithType}(value.getEndTime().toSqlTimestamp());
156#end
157#else
158      if (value == null)
159      {
160         ${simpleSetterName}(null);
161         ${compoundSetter}(null);
162      }
163      else
164      {
165#if(!$column.isPeriodDefined())
166         ${simpleSetterName}(new Long(value.getValue()));
167         ${compoundSetter}(value.getCurrency().toString());
168#else
169         ${simpleSetterName}(value.getStartTime().toSqlTimestamp());
170         ${compoundSetterWithType}(value.getEndTime().toSqlTimestamp());
171#end
172      }
173#end
174   }
175
176   /**
177    * Sets the value of the $javaFieldName field as ${javaType}.
178    * @param value the value of the $javaFieldName field as ${javaType}.
179    */
180   public abstract void $simpleSetterName ($fqJavaType value);
181
182#end
183#copyrightHeader()
184
185package ${package};
186
187#set($imports = $cmpgen.buildHelperImportList($stmt))
188#foreach($import in $imports)
189import ${import};
190#end
191
192/**
193 * $baseName Entity Bean Helper.
194 * Generated by Phoenix CMP Generator II $cmpgen.getVersion()
195 *
196 * @author fawkeZ (jCoderZ.org)
197 */
198public abstract class ${baseName}TypeConverter
199{
200#foreach($column in $stmt.getColumns())
201#if($column.isSkipInInterface())
202## only the primitive-type getters and setters required here,
203## without being interface methods
204#computeFields()
205#simpleHelperGetter()
206
207#simpleSetter(false)
208
209#elseif($column.getJavaType().endsWith("Amount"))
210#compoundColumn($column.getCurrencyColumn())
211#elseif($column.getJavaType().endsWith("Period"))
212#if($column.isPeriodDefined())
213#compoundColumn($column.getPeriodEndDateColumn())
214#end
215#else
216#computeFields()
217#if($column.getLoadMethod())
218   /**
219#if($column.getAnnotation())
220#set($annotationWithPeriod = $column.getAnnotation() + ".")
221#set($indexOfFirstDot = $annotationWithPeriod.indexOf(". "))
222#set($annotationFirstSentence = $annotationWithPeriod.substring(0, $indexOfFirstDot))
223    * Returns $column.getAnnotation()
224    * @return $annotationFirstSentence
225#else
226    * Returns the value of the $javaFieldName field as ${complexType}.
227    * @return the value of the $javaFieldName field as ${complexType}.
228#end
229    * @throws RuntimeException if the DB value can't
230    *         be converted to the complex type
231    * @ejb.interface-method
232    */
233   public $complexType get$javaFieldName ()
234         throws InconsistentDatabaseException
235   {
236      final $fqJavaType value = $simpleGetterName();
237      final $complexType result;
238#if($cmpgen.isPrimitiveType($fqJavaType))
239      result = ${complexType}.${loadMethod}(value);
240#else
241      if (value == null)
242      {
243         result = null;
244      }
245      else
246      {
247         try
248         {
249            result = ${complexType}.${loadMethod}(value);
250         }
251         catch (Exception x)
252         {
253            throw new InconsistentDatabaseException (
254                  String.valueOf(value),
255                  "$column.getColumnName()",
256                  "$stmt.getTableName()",
257                  "$fqComplexType",
258                  x);
259         }
260      }
261#end
262      return result;
263   }
264
265   /**
266    * Sets the value of the $javaFieldName field as ${complexType}.
267    * @param value the value of the $javaFieldName field as ${complexType}.
268    * @ejb.interface-method
269    */
270   public void set$javaFieldName ($complexType value)
271   {
272#if($cmpgen.isPrimitiveType($fqJavaType))
273      ${simpleSetterName}(value.${storeMethod}());
274#else
275      if (value != null)
276      {
277         ${simpleSetterName}(value.${storeMethod}());
278      }
279      else
280      {
281         ${simpleSetterName}(null);
282      }
283#end
284   }
285
286#simpleSetter(false)
287
288#end##if colum.getLoadMethod
289#simpleHelperGetter()
290#end##if
291
292#end##foreach
293
294   /**
295    * Returns an immutable value object representing this entity.
296    * @return an immutable value object representing this entity.
297    * @throws InconsistentDatabaseException if the value in the database
298    *       cannot be converted into its corresponding java type.
299    * @ejb.interface-method
300    */
301   public ${baseName}Value toValue ()
302         throws InconsistentDatabaseException
303   {
304      return new ${baseName}ValueImpl(
305#set($count = 0)
306#foreach($column in $stmt.getColumns())
307#if(!$column.isSkipInInterface())
308#computeFields()
309#if(!$column.isPeriodDefined())
310         #if($count != 0),#end
311         get${javaFieldName}()
312#else
313         #if($count != 0),#end
314         get$column.getPeriodFieldName()()
315#end
316#set($count = $count + 1)
317#end
318#end
319      );
320   }
321}
Note: See TracBrowser for help on using the browser.