KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > JdbcOIDGenerator


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc;
13
14 import com.versant.core.metadata.generator.OIDSrcGenerator;
15 import com.versant.core.metadata.ModelMetaData;
16 import com.versant.core.metadata.ClassMetaData;
17 import com.versant.core.metadata.MDStatics;
18 import com.versant.core.metadata.FieldMetaData;
19 import com.versant.core.common.BindingSupportImpl;
20 import com.versant.core.compiler.ClassSpec;
21 import com.versant.core.jdbc.metadata.JdbcColumn;
22 import com.versant.core.jdbc.metadata.JdbcField;
23 import com.versant.core.jdbc.metadata.JdbcClass;
24
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 /**
30  * Adds JDBC specific stuff to OID.
31  */

32 public class JdbcOIDGenerator extends OIDSrcGenerator {
33
34     public JdbcOIDGenerator(ModelMetaData jmd) {
35         super(jmd);
36     }
37
38     public ClassSpec generateOID(ClassMetaData cmd) {
39         ClassSpec spec = super.generateOID(cmd);
40
41         spec.addImport(JdbcOID.class.getName());
42         spec.addImport(ResultSet JavaDoc.class.getName());
43         spec.addImport(PreparedStatement JavaDoc.class.getName());
44         spec.addImport(SQLException JavaDoc.class.getName());
45         spec.addImport(JdbcColumn.class.getName());
46         spec.addImport(JdbcUtils.class.getName());
47         spec.addImport(JdbcField.class.getName());
48         spec.addImport(JdbcClass.class.getName());
49
50         spec.addInterface("JdbcOID");
51
52         addCopyKeyFields();
53         addSetParams();
54         addSetParams2();
55         addValidateKeyFields();
56         addCopyKeyFields2();
57
58         return spec;
59     }
60
61     protected void addInitStaticsBody(StringBuffer JavaDoc buf) {
62         super.addInitStaticsBody(buf);
63         // early exit from method if storeClass is null (i.e. remote PMF)
64
if (cmd.isInHeirachy()) {
65             buf.append("\t\tif (top.storeClass == null) return true;\n");
66         } else {
67             buf.append("\t\tif (cmd.storeClass == null) return true;\n");
68         }
69         JdbcColumn[] pkc = ((JdbcClass)cmd.top.storeClass).table.pk;
70         boolean first = true;
71         for (int j = 0; j < pkc.length; j++) {
72             if (pkc[j].converter != null) {
73                 if (first) {
74                     buf.append("\t\tClassMetaData t = jmd.classes[" + currentCMD.top.index + "];\n");
75                     buf.append("\t\tJdbcColumn[] pkc = ((JdbcClass)t.storeClass).table.pk;\n");
76                     first = false;
77                 }
78                 buf.append("\t\t" + JDBC_CONVERTER_FIELD_PREFIX + j +
79                         " = (" + pkc[j].converter.getClass().getName() +
80                         ")pkc[" + j + "].converter;\n");
81                 buf.append("\t\tjdbcCol_" + j + " = pkc[" + j + "];\n");
82             }
83         }
84     }
85
86     protected void addPopulateObjectIdClassInstance() {
87         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
88         buf.append("\n\tpublic void populateObjectIdClassInstance(Object o) {\n");
89         if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
90             if (cmd.top.objectIdClass != null){
91                 String JavaDoc oidClassName = cmd.top.objectIdClass.getName().replace('$','.');
92                 buf.append("\t\t"+oidClassName+" pk = ("+ oidClassName +")o;\n");
93                 FieldMetaData[] fields = cmd.pkFields;
94                 for (int i = 0; i < fields.length; i++) {
95                     FieldMetaData field = fields[i];
96                     buf.append("\t\tpk.");
97                     buf.append(field.getPkFieldName());
98                     buf.append(" = ");
99                     buf.append(getFieldName(i));
100                     buf.append(";\n");
101                 }
102             }
103         }
104         buf.append("\t}\n");
105         spec.addMethod(buf.toString());
106     }
107
108     private void addValidateKeyFields() {
109         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
110         /*
111         public boolean validateKeyFields(ResultSet rs, int firstCol) throws SQLException {
112             rs.getInt(firstCol++);
113             return !rs.wasNull();
114         }
115         */

116         buf.append("\n\tpublic boolean validateKeyFields(ResultSet rs, int firstCol) throws SQLException {\n");
117         JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
118         for (int i = 0; i < pkc.length; i++) {
119             Class JavaDoc fieldType = pkc[i].javaType;
120             if (pkc[i].converter != null) { // converter
121
/*
122                 jdbcConverter_2.get(rs, firstCol++, jdbcCol_2);
123                 if (rs.wasNull())return false;
124                 */

125                 buf.append("\t\t");
126                 buf.append("jdbcConverter_" + i);
127                 buf.append(".get(rs,firstCol++, jdbcCol_" + i);
128                 buf.append(");\n\t\t");
129                 buf.append("if (rs.wasNull())return false;\n");
130
131             } else { // no converter
132
boolean isWrapper = wrapperTypesToPrimitive.keySet().contains(fieldType);
133                 if (isWrapper) {
134                     /*
135                     rs.getShort(firstCol++);
136                     if (rs.wasNull()) return false;
137                     */

138
139                     buf.append("\t\trs.");
140                     buf.append((String JavaDoc) typeToResultSetGetField.get(wrapperTypesToPrimitive.get(fieldType)));
141                     buf.append("(firstCol++);\n");
142                 } else {
143                     /*
144                     rs.getString(firstCol++);
145                     if (rs.wasNull()) return false;
146                     */

147                     buf.append("\t\trs.");
148                     buf.append((String JavaDoc) typeToResultSetGetField.get(fieldType));
149                     buf.append("(firstCol++);\n");
150
151                 }
152                 buf.append("\t\tif (rs.wasNull())return false;\n");
153             }
154         }
155         buf.append("\t\treturn true;\n\t}\n");
156         spec.addMethod(buf.toString());
157     }
158
159
160     private void addCopyKeyFields() {
161         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
162         /*
163     public boolean copyKeyFields(ResultSet rs, int firstCol) throws SQLException {
164         _0 = rs.getInt(firstCol++);
165         if (rs.wasNull()) {
166             return false;
167         }
168         return true;
169     }
170
171
172         public boolean copyKeyFields(ResultSet rs, int firstCol) throws SQLException {
173
174
175         Boolean prim_0 = (Boolean)jdbcConverter_0.get(rs, firstCol, jdbcCol_0);
176         firstCol++;
177         if (rs.wasNull()) {
178         return false;
179         }
180         _0 = prim_0.booleanValue();
181         Boolean prim_1 = (Boolean)jdbcConverter_1.get(rs, firstCol, jdbcCol_1);
182         firstCol++;
183         if (rs.wasNull()) {
184         return false;
185         }
186         _1 = prim_1.booleanValue();
187         _2 = (Locale)jdbcConverter_2.get(rs, firstCol, jdbcCol_2);
188         firstCol++;
189         if (rs.wasNull()) {
190         return false;
191         }
192         _3 = rs.getString(firstCol++);
193         return !rs.wasNull();
194         }
195
196         */

197
198         buf.append("\n\tpublic boolean copyKeyFields(ResultSet rs,int firstCol) throws SQLException{\n");
199
200         JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
201         for (int i = 0; i < pkc.length; i++) {
202             Class JavaDoc fieldType = pkc[i].javaType;
203             boolean isPrim = pkc[i].javaType.isPrimitive();
204             String JavaDoc fieldName = "_" + i;
205             if (pkc[i].converter != null) { // converter
206
if (isPrim) {
207                     /*
208                     Boolean prim_1 = (Boolean)jdbcConverter_1.get(rs, firstCol++, jdbcCol_1);
209                     if (rs.wasNull()) {
210                     return false;
211                     }
212                     _0 = prim_0.booleanValue();
213                     */

214                     buf.append("\t\t");
215                     buf.append((String JavaDoc) primitiveToWrapperTypes.get(fieldType));
216                     buf.append(" prim_"+i);
217                     buf.append(" = (");
218                     buf.append((String JavaDoc) primitiveToWrapperTypes.get(fieldType));
219                     buf.append(")jdbcConverter_"+i);
220                     buf.append(".get(rs,firstCol++, jdbcCol_"+i);
221                     buf.append(");\n");
222                     buf.append("\t\tif (rs.wasNull()) return false;\n");
223                     buf.append("\t\t");
224                     buf.append(fieldName);
225                     buf.append(" = ");
226                     buf.append(" prim_" + i);
227                     buf.append(".");
228                     buf.append((String JavaDoc) wrapperTypesToValue.get(fieldType));
229                     buf.append("();\n");
230                 } else {
231                     /*
232                     _2 = (Locale)jdbcConverter_2.get(rs, firstCol++, jdbcCol_2);
233                     if (rs.wasNull())return false;
234                     */

235                     buf.append("\t\t");
236                     buf.append(fieldName);
237                     buf.append(" = (");
238                     buf.append(fieldType.getName());
239                     buf.append(")jdbcConverter_" + i);
240                     buf.append(".get(rs,firstCol++, jdbcCol_" + i);
241                     buf.append(");\n");
242                     buf.append("\t\tif (rs.wasNull()) return false;\n");
243                 }
244             } else { // no converter
245
boolean isWrapper = wrapperTypesToPrimitive.keySet().contains(fieldType);
246                 if (isWrapper) {
247                     /*
248                     _3 = new Short(rs.getShort(firstCol++));
249                     if (rs.wasNull()) return false;
250                     */

251                     buf.append("\t\t");
252                     buf.append(fieldName);
253                     buf.append(" = new ");
254                     buf.append(fieldType.getName());
255                     buf.append("(rs.");
256                     buf.append((String JavaDoc) typeToResultSetGetField.get(
257                             wrapperTypesToPrimitive.get(fieldType)));
258                     buf.append("(firstCol++));\n");
259                 } else {
260                     /*
261                     _3 = rs.getString(firstCol++);
262                     if (rs.wasNull()) return false;
263                     */

264                     buf.append("\t\t");
265                     buf.append(fieldName);
266                     buf.append(" = rs.");
267                     buf.append((String JavaDoc) typeToResultSetGetField.get(fieldType));
268                     buf.append("(firstCol++);\n");
269
270                 }
271                 buf.append("\t\tif (rs.wasNull()) return false;\n");
272             }
273         }
274         buf.append("\t\treturn true;\n\t}\n");
275         spec.addMethod(buf.toString());
276     }
277
278     private void addSetParams() {
279         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
280         /*
281
282         public int setParams(PreparedStatement ps, int firstParam) throws SQLException {
283             ps.setInt(firstParam++, _0);
284             jdbcConverter_1.set(ps, firstParam++, jdbcCol_1, new Boolean(_1));
285             jdbcConverter_2.set(ps, firstParam++, jdbcCol_2, _2);
286             if (_3 == null) {
287                 ps.setNull(firstParam++, 12);
288             } else {
289                 ps.setString(firstParam++, _3);
290             }
291             return firstParam;
292         }
293         */

294
295         buf.append("\n\tpublic int setParams(PreparedStatement ps, int firstParam) throws SQLException {\n");
296         JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
297         for (int i = 0; i < pkc.length; i++) {
298             Class JavaDoc fieldType = pkc[i].javaType;
299             String JavaDoc fieldName = "_" + i;
300             boolean isPrimitive = pkc[i].javaType.isPrimitive();
301             if (pkc[i].converter != null) { // converter
302
String JavaDoc converterName = JDBC_CONVERTER_FIELD_PREFIX + i;
303                 String JavaDoc colName = "jdbcCol_" + i;
304
305                 if (isPrimitive) {
306                     /*
307                     jdbcConverter_1.set(ps, firstParam++, jdbcCol_1, new Boolean(_1));
308                     */

309                     buf.append("\t\t");
310                     buf.append(converterName);
311                     buf.append(".set(ps, firstParam++, ");
312                     buf.append(colName);
313                     buf.append(", new ");
314                     buf.append((String JavaDoc) primitiveToWrapperTypes.get(fieldType));
315                     buf.append("(");
316                     buf.append(fieldName);
317                     buf.append("));\n");
318                 } else {
319                     /*
320                     jdbcConverter_2.set(ps, firstParam++, jdbcCol_2, _2);
321                     */

322                     buf.append("\t\t");
323                     buf.append(converterName);
324                     buf.append(".set(ps, firstParam++, ");
325                     buf.append(colName);
326                     buf.append(", ");
327                     buf.append(fieldName);
328                     buf.append(");\n");
329                 }
330             } else if (isPrimitive) { //primitive no converter
331
/*
332                 ps.setInt(firstParam++, _0);
333                 */

334                 buf.append("\t\t");
335                 buf.append("ps.");
336                 buf.append((String JavaDoc) typeToPreparedStatementSetField.get(fieldType));
337                 buf.append("(firstParam++, ");
338                 buf.append(fieldName);
339                 buf.append(");\n");
340             } else { // Object no converter
341
/*
342                 if (_3 == null) ps.setNull(firstParam++, 12);
343                 else ps.setString(firstParam++, _3);
344                 */

345                 buf.append("\t\t");
346                 buf.append("if (");
347                 buf.append(fieldName);
348                 buf.append(" == null) ps.setNull(firstParam++,");
349                 buf.append(" "+ (pkc[i]).jdbcType);
350                 buf.append(");\n");
351                 if (wrapperTypesToPrimitive.containsKey(fieldType)) {// we have a wrapper type
352

353                     /*
354                     else ps.setShort(firstParam++, _3.shortValue());
355                     */

356
357                     buf.append("\t\t");
358                     buf.append("else ps.");
359                     buf.append((String JavaDoc) typeToPreparedStatementSetField.get(
360                             wrapperTypesToPrimitive.get(fieldType)));
361                     buf.append("(firstParam++,");
362                     buf.append(fieldName);
363                     buf.append(".");
364                     buf.append((String JavaDoc) wrapperTypesToValue.get(
365                             wrapperTypesToPrimitive.get(fieldType)));
366                     buf.append("());\n");
367                 } else {
368                     /*
369                     else ps.setString(firstParam++, _3);
370                     */

371                     buf.append("\t\t");
372                     buf.append("else ps.");
373                     buf.append((String JavaDoc) typeToPreparedStatementSetField.get(fieldType));
374                     buf.append("(firstParam++,");
375                     buf.append(fieldName);
376                     buf.append(");\n");
377                 }
378             }
379         }
380         buf.append("\t\treturn firstParam;\n\t}\n");
381         spec.addMethod(buf.toString());
382     }
383
384
385     private void addSetParams2() {
386         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
387         /*
388     public int setParams(PreparedStatement ps, int firstParam, JdbcColumn pkc[]) throws SQLException {
389         JdbcColumn c = null;
390         c = pkc[0];
391         if (c.isForUpdate()) {
392             if (c.converter != null) {
393                 c.converter.set(ps, firstParam++, c, new Boolean(_0));
394             } else {
395                 JdbcUtils.set(ps, firstParam++, new Boolean(_0), c.javaTypeCode, c.jdbcType);
396             }
397         }
398         c = pkc[2];
399         if (c.isForUpdate()) {
400             if (c.converter != null) {
401                 c.converter.set(ps, firstParam++, c, _2);
402             } else {
403                 JdbcUtils.set(ps, firstParam++, _2, c.javaTypeCode, c.jdbcType);
404             }
405         }
406         return firstParam;
407     }
408         */

409         buf.append("\n\tpublic int setParams(PreparedStatement ps, int firstParam, JdbcColumn[] pkc) throws SQLException {\n");
410
411         JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
412         /*
413         JdbcColumn c = null;
414         */

415         buf.append("\t\tJdbcColumn c = null;\n");
416         for (int i = 0; i < pkc.length; i++) {
417             Class JavaDoc fieldType = pkc[i].javaType;
418             String JavaDoc fieldName = "_" + i;
419             boolean isPrimitive = pkc[i].javaType.isPrimitive();
420             /*
421             c = pkc[0];
422             if (c.isForUpdate()) {
423                 if (c.converter != null) c.converter.set(ps, firstParam++, c, new Boolean(_0));
424                 else JdbcUtils.set(ps, firstParam++, new Boolean(_0), c.javaTypeCode, c.jdbcType);
425             }
426             */

427             buf.append("\t\tc = pkc["+i);
428             buf.append("];\n");
429
430             buf.append("\t\tif (c.isForUpdate()) {\n");
431             if (isPrimitive) {
432                 /*
433                 if (c.converter != null) c.converter.set(ps, firstParam++, c, new Boolean(_0));
434                 else JdbcUtils.set(ps, firstParam++, new Boolean(_0), c.javaTypeCode, c.jdbcType);
435                 */

436
437                 buf.append("\t\t\tif (c.converter != null) c.converter.set(ps, firstParam++, c, new ");
438                 buf.append((String JavaDoc) primitiveToWrapperTypes.get(fieldType));
439                 buf.append("(");
440                 buf.append(fieldName);
441                 buf.append("));\n");
442
443                 buf.append("\t\t\telse JdbcUtils.set(ps, firstParam++, new ");
444                 buf.append((String JavaDoc) primitiveToWrapperTypes.get(fieldType));
445                 buf.append("(");
446                 buf.append(fieldName);
447                 buf.append("), c.javaTypeCode, c.jdbcType);\n");
448
449             } else {
450                 /*
451                 if (c.converter != null) c.converter.set(ps, firstParam++, c, _2);
452                 else JdbcUtils.set(ps, firstParam++, _2, c.javaTypeCode, c.jdbcType);
453                 */

454                 buf.append("\t\t\tif (c.converter != null) c.converter.set(ps, firstParam++, c, ");
455                 buf.append(fieldName);
456                 buf.append(");\n");
457
458                 buf.append("\t\t\telse JdbcUtils.set(ps, firstParam++, ");
459                 buf.append(fieldName);
460                 buf.append(", c.javaTypeCode, c.jdbcType);\n");
461
462             }
463             buf.append("\t\t}\n");
464
465         }
466         buf.append("\t\treturn firstParam;\n\t}\n");
467         spec.addMethod(buf.toString());
468     }
469
470     protected void addCopyKeyFields2(){
471         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
472         buf.append("\n\tpublic boolean copyKeyFields(ResultSet rs, JdbcField[] pks, int[] pkFieldIndexs) throws SQLException {\n");
473         if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
474             buf.append("\t\tfor (int j = 0; j < pkFieldIndexs.length; j++) {\n");
475             JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
476             buf.append("\t\t\tswitch(j){\n");
477             for (int i = 0; i < pkc.length; i++) {
478                 Class JavaDoc fieldType = pkc[i].javaType;
479                 boolean isPrim = pkc[i].javaType.isPrimitive();
480                 String JavaDoc fieldName = "_" + i;
481                 buf.append("\t\t\t\tcase " + i + " :\n");
482                 if (pkc[i].converter != null) { // converter
483
if (isPrim) {
484                         /*
485                         Boolean prim_1 = (Boolean)jdbcConverter_1.get(rs, pkFieldIndexs[j] + 1, jdbcCol_1);
486                         if (rs.wasNull()) {
487                         return false;
488                         }
489                         _0 = prim_0.booleanValue();
490                         */

491
492                         buf.append("\t\t\t\t\t"+(String JavaDoc) primitiveToWrapperTypes.get(fieldType));
493                         buf.append(" prim_" + i);
494                         buf.append(" = (");
495                         buf.append((String JavaDoc) primitiveToWrapperTypes.get(fieldType));
496                         buf.append(")jdbcConverter_" + i);
497                         buf.append(".get(rs, pkFieldIndexs[j] + 1, jdbcCol_" + i);
498                         buf.append(");\n");
499                         buf.append("\t\t\t\t\t"+fieldName);
500                         buf.append(" = ");
501                         buf.append(" prim_" + i);
502                         buf.append(".");
503                         buf.append((String JavaDoc) wrapperTypesToValue.get(fieldType));
504                         buf.append("();\n");
505                     } else {
506                         /*
507                         _2 = (Locale)jdbcConverter_2.get(rs, firstCol++, jdbcCol_2);
508                         if (rs.wasNull())return false;
509                         */

510                         buf.append("\t\t\t\t\t" + fieldName);
511                         buf.append(" = (");
512                         buf.append(fieldType.getName());
513                         buf.append(")jdbcConverter_" + i);
514                         buf.append(".get(rs,pkFieldIndexs[j] + 1, jdbcCol_" + i);
515                         buf.append(");\n");
516                     }
517                 } else { // no converter
518
boolean isWrapper = wrapperTypesToPrimitive.keySet().contains(fieldType);
519                     if (isWrapper) {
520                         /*
521                         _3 = new Short(rs.getShort(firstCol++));
522                         if (rs.wasNull()) return false;
523                         */

524                         buf.append("\t\t\t\t\t" + fieldName);
525                         buf.append(" = new ");
526                         buf.append(fieldType.getName());
527                         buf.append("(rs.");
528                         buf.append((String JavaDoc) typeToResultSetGetField.get(wrapperTypesToPrimitive.get(fieldType)));
529                         buf.append("(pkFieldIndexs[j] + 1));\n");
530                     } else {
531                         /*
532                         _3 = rs.getString(firstCol++);
533                         if (rs.wasNull()) return false;
534                         */

535                         buf.append("\t\t\t\t\t" + fieldName);
536                         buf.append(" = rs.");
537                         buf.append((String JavaDoc) typeToResultSetGetField.get(fieldType));
538                         buf.append("(pkFieldIndexs[j] + 1);\n");
539
540                     }
541                 }
542                 buf.append("\t\t\t\t\tbreak;\n");
543             }
544             buf.append("\t\t\t}\n");
545             buf.append("\t\t\tif (rs.wasNull()) return false;\n");
546             buf.append("\t\t}\n");
547         }
548         buf.append("\t\treturn true;\n");
549         buf.append("\t}\n");
550         spec.addMethod(buf.toString());
551     }
552
553     protected void addGetLongPrimaryKey() {
554         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
555         buf.append("\n\tpublic long getLongPrimaryKey() {\n");
556         if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) {
557             JdbcColumn c = ((JdbcClass)currentCMD.storeClass).table.pk[0];
558             switch (c.javaTypeCode) {
559                 case MDStatics.BYTE:
560                 case MDStatics.SHORT:
561                 case MDStatics.INT:
562                     buf.append("\t\treturn (long)_0;\n");
563                     break;
564
565                 case MDStatics.LONG:
566                     buf.append("\t\treturn _0;\n");
567                     break;
568             }
569         } else {
570             buf.append("\t\treturn (long) -1;\n");
571         }
572         buf.append("\t}\n");
573         spec.addMethod(buf.toString());
574     }
575
576     protected void addSetLongPrimaryKey() {
577         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
578         buf.append("\n\tpublic void setLongPrimaryKey(long newPK) {\n");
579         if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) {
580             JdbcColumn c = ((JdbcClass)currentCMD.storeClass).table.pk[0];
581             buf.append("\t\t_0 = ");
582             switch (c.javaTypeCode) {
583                 case MDStatics.BYTE:
584                     buf.append("(byte)");
585                     break;
586                 case MDStatics.SHORT:
587                     buf.append("(short)");
588                     break;
589                 case MDStatics.INT:
590                     buf.append("(int)");
591                     break;
592             }
593             buf.append("newPK;\n");
594         }
595         buf.append("\t}\n");
596         spec.addMethod(buf.toString());
597     }
598
599     protected void addfillFromPK() {
600         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
601         buf.append("\n\tpublic void fillFromPK(Object pk) {\n");
602         if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
603             if (cmd.top.objectIdClass != null){
604                 String JavaDoc oidClassName = cmd.top.objectIdClass.getName().replace('$','.');
605                 /*
606                 AppIdConcrete1Key appPK = (AppIdConcrete1Key)pk;
607                 */

608                 buf.append("\t\t"+oidClassName);
609                 buf.append(" appPK = (");
610                 buf.append(oidClassName);
611                 buf.append(")pk;\n");
612
613                 FieldMetaData[] fields = cmd.pkFields;
614                 for (int i = 0; i < fields.length; i++) {
615                     FieldMetaData field = fields[i];
616                     /*
617                     _0 = appPK.appIdConcKey;
618                     */

619                     buf.append("\t\t" + getFieldName(i));
620                     buf.append(" = appPK.");
621                     buf.append(field.getPkFieldName());
622                     buf.append(";\n");
623                 }
624             }
625         }
626         buf.append("\t}\n");
627         spec.addMethod(buf.toString());
628     }
629
630     protected void addCompareTo() {
631         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
632         buf.append("\n\tpublic int compareTo(Object o) {\n");
633         buf.append("\t\tOID other = (OID)o;\n");
634         buf.append("\t\tint diff = "+ cmd.top.index +" - other.getBaseClassMetaData().index;\n");
635         buf.append("\t\tif (diff != 0) return diff;\n");
636         buf.append("\t\tif (other.isNew()) return 1;\n");
637         buf.append("\t\telse {\n");
638         buf.append("\t\t\t"+ className +" original = ("+ className +")other;\n");
639
640         ClassMetaData currentCMD = getCurrentCMD();
641
642         JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
643         int count = pkc.length - 1;
644         boolean isLast;
645         for (int i = 0; i < pkc.length; i++) {
646             JdbcColumn c = pkc[i];
647             String JavaDoc fieldName = "_" + i;
648             isLast = i == count;
649
650             switch (c.javaTypeCode) {
651
652                 case MDStatics.CHAR:
653                 case MDStatics.BYTE:
654                 case MDStatics.SHORT:
655                 case MDStatics.INT:
656
657                     if (isLast) {
658                         /*
659                         return _0 - original._0;
660                         */

661                         buf.append("\t\t\treturn "+ fieldName +" - original."+ fieldName +";\n");
662                     } else {
663                         /*
664                         if (_0 != original._0) return _0 - original._0;
665                         */

666                         buf.append("\t\t\tif (" + fieldName + " != original." + fieldName + ") return " + fieldName + " - original." + fieldName + ";\n");
667                     }
668                     break;
669                 case MDStatics.LOCALE:
670                     /*
671                     diff = _2.toString().compareTo(original._2.toString());
672                     if (diff != 0) return diff;
673                     */

674                     buf.append("\t\t\tdiff = "+ fieldName +".toString().compareTo(original."+ fieldName +".toString());\n");
675
676                     if (isLast) {
677                         buf.append("\t\t\treturn diff;\n");
678                     } else {
679                         buf.append("\t\t\tif (diff != 0) return diff;\n");
680                     }
681                     break;
682
683                 case MDStatics.CHARW:
684                 case MDStatics.BYTEW:
685                 case MDStatics.SHORTW:
686                 case MDStatics.INTW:
687                 case MDStatics.LONGW:
688                 case MDStatics.FLOATW:
689                 case MDStatics.DOUBLEW:
690                 case MDStatics.STRING:
691                 case MDStatics.BIGDECIMAL:
692                 case MDStatics.BIGINTEGER:
693                 case MDStatics.DATE:
694
695                     if (isLast) {
696                         buf.append("\t\t\treturn "+ fieldName +".compareTo(original."+ fieldName +");\n");
697                     } else {
698                         buf.append("\t\t\tdiff = " + fieldName + ".compareTo(original." + fieldName + ");\n");
699                         buf.append("\t\t\tif (diff != 0) return diff;\n");
700                     }
701                     break;
702
703                 case MDStatics.DOUBLE:
704                 case MDStatics.FLOAT:
705                 case MDStatics.LONG:
706
707                     if (isLast) {
708                         /*
709                         return _0 - original._0;
710                         */

711                         buf.append("\t\t\treturn (int)(" + fieldName + " - original." + fieldName + ");\n");
712                     } else {
713                         /*
714                         if (_0 != original._0) return _0 - original._0;
715                         */

716                         buf.append("\t\t\tif (" + fieldName + " != original." + fieldName + ") return (int)(" + fieldName + " - original." + fieldName + ");\n");
717                     }
718                     break;
719
720
721                 case MDStatics.BOOLEANW:
722
723                     if (isLast) {
724                         buf.append("\t\t\treturn !" + fieldName + ".booleanValue() ? -1 : 1;\n");
725                     } else {
726                         /*
727                         if (_0 != original._0) return !_0 ? -1 : 1;
728                         */

729                         buf.append("\t\t\tif (!" + fieldName + ".equals(original." + fieldName + ")) return !" + fieldName + ".booleanValue() ? -1 : 1;\n");
730                     }
731
732                     break;
733                 case MDStatics.BOOLEAN:
734                     if (isLast) {
735                         buf.append("\t\t\treturn !" + fieldName + " ? -1 : 1;\n");
736                     } else {
737                         /*
738                         if (_0 != original._0) return !_0 ? -1 : 1;
739                         */

740                         buf.append("\t\t\tif ("+ fieldName +" != original."+ fieldName +") return !"+ fieldName +" ? -1 : 1;\n");
741                     }
742                     break;
743
744
745                 default:
746                     throw BindingSupportImpl.getInstance().runtime("Unable to create a ObjectId for a Type '"
747                             + c.javaType.getName() + "'");
748             }
749
750         }
751         buf.append("\t\t}\n\t}\n");
752         spec.addMethod(buf.toString());
753     }
754
755     protected void addToPKString() {
756         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
757         buf.append("\n\tpublic String toPkString() {\n");
758         buf.append("\t\tStringBuffer s = new StringBuffer();\n");
759         JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk;
760         for (int i = 0; i < pkc.length; i++) {
761             String JavaDoc fieldName = "_" + i;
762             buf.append("\t\ts.append(");
763             buf.append(fieldName);
764             buf.append(");\n");
765             if (i != (pkc.length - 1)) {
766                 buf.append("\t\ts.append(\", \");\n");
767             }
768         }
769         buf.append("\t\treturn s.toString();\n\t}\n");
770         spec.addMethod(buf.toString());
771     }
772
773     protected void addToSString() {
774         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
775         buf.append("\n\tpublic String toSString() {\n");
776         JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk;
777         /*
778         StringBuffer s = new StringBuffer("OID:za.AppIDAbstract1 (Table:app_i_d_abstract1");
779         */

780         buf.append("\t\tStringBuffer s = new StringBuffer(\"");
781         buf.append("OID:" + currentCMD.qname + " (Table:" +
782                 ((JdbcClass)currentCMD.storeClass).tableName);
783         buf.append("\");\n");
784
785         for (int i = 0; i < pkc.length; i++) {
786             JdbcColumn c = pkc[i];
787             String JavaDoc fieldName = "_" + i;
788
789             buf.append("\t\ts.append(\"");
790             buf.append(" Column:" + c.name + " = \"");
791             buf.append(");\n");
792
793             buf.append("\t\ts.append(");
794             buf.append(fieldName);
795             buf.append(");\n");
796         }
797         buf.append("\t\ts.append(\")\");\n");
798         buf.append("\t\treturn s.toString();\n");
799         buf.append("\t}\n");
800         spec.addMethod(buf.toString());
801     }
802
803     protected void addGetCopy() {
804         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
805         buf.append("\n\tpublic OID copy() {\n");
806         buf.append("\t\t"+className);
807         buf.append(" copy = new ");
808         buf.append(className);
809         buf.append("();\n");
810         if (cmd.isInHeirachy()) {
811             buf.append("\t\tcopy.cmd = cmd;\n");
812             buf.append("\t\tcopy.resolved = resolved;\n");
813         }
814         JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
815         int num = pkc.length;
816         for (int i = 0; i < num; i++) {
817             Class JavaDoc fieldType = pkc[i].javaType;
818             String JavaDoc fieldName = getFieldName(i);
819             boolean isPrimitive = pkc[i].javaType.isPrimitive();
820             boolean isArray = pkc[i].javaType.isArray();
821             boolean isCloneable = false;
822
823             if (!isPrimitive) {
824                 Class JavaDoc[] interfaces = pkc[i].javaType.getInterfaces();
825                 for (int j = 0; j < interfaces.length; j++) {
826                     Class JavaDoc aClass = interfaces[j];
827                     if (aClass.getName().equals("Cloneable")) {
828                         isCloneable = true;
829                     }
830                 }
831             }
832             if (isPrimitive) {
833                 /*
834                 copy._0 = _0;
835                 */

836                 buf.append("\t\tcopy.");
837                 buf.append(fieldName);
838                 buf.append(" = ");
839                 buf.append(fieldName);
840                 buf.append(";\n");
841             } else if (isArray) {
842                 /*
843                 System.arraycopy((Object)_0,0, (Object) copy._0, 0, _0.length);
844                 */

845                 buf.append("\t\tSystem.arraycopy((Object)");
846                 buf.append(fieldName);
847                 buf.append(", 0, (Object)copy.");
848                 buf.append(fieldName);
849                 buf.append(", 0, ");
850                 buf.append(fieldName);
851                 buf.append(".length);\n");
852             } else if (isCloneable) {
853                 /*
854                 copy._1 = (Locale)_1.clone();
855                 */

856                 buf.append("\t\tcopy.");
857                 buf.append(fieldName);
858                 buf.append(" = (");
859                 buf.append(fieldType.getName());
860                 buf.append(")");
861                 buf.append(fieldName);
862                 buf.append(".clone();\n");
863             } else {// normal object ????
864
/*
865                 copy._0 = _0;
866                 */

867                 buf.append("\t\tcopy.");
868                 buf.append(fieldName);
869                 buf.append(" = ");
870                 buf.append(fieldName);
871                 buf.append(";\n");
872             }
873         }
874         buf.append("\t\treturn copy;\n\t}\n");
875         spec.addMethod(buf.toString());
876     }
877
878     protected void addCopyKeyFieldsUpdate() {
879         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
880         buf.append("\n\tpublic void copyKeyFieldsUpdate(State state) {\n");
881         if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
882             ClassMetaData currentCMD = getTopPCSuperClassMetaData();
883             buf.append("\t\t");
884             buf.append(stateClassName);
885             buf.append(" other = (");
886             buf.append(stateClassName);
887             buf.append(")state;\n");
888             FieldMetaData[] pkFields = currentCMD.pkFields;
889             JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
890             for (int i = 0; i < pkc.length; i++) {
891                 int stateFieldNum = pkFields[i].fieldNo;
892                 String JavaDoc stateFieldName = "_" + stateFieldNum;
893                 String JavaDoc fieldName = "_" + i;
894                 /*
895                 if (other.containsField(1)) _0 = other._1;
896                 */

897                 buf.append("\t\tif (other.containsField("+ stateFieldNum);
898                 buf.append(")) ");
899                 buf.append(fieldName);
900                 buf.append(" = other.");
901                 buf.append(stateFieldName);
902                 buf.append(";\n");
903             }
904         }
905         buf.append("\t}\n");
906         spec.addMethod(buf.toString());
907     }
908
909     protected void addCopyKeyFieldsFromState() {
910         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
911         buf.append("\n\tpublic void copyKeyFields(State state) {\n");
912         if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
913             ClassMetaData currentCMD = getTopPCSuperClassMetaData();
914             buf.append("\t\t");
915             buf.append(stateClassName);
916             buf.append(" other = (");
917             buf.append(stateClassName);
918             buf.append(")state;\n");
919             FieldMetaData[] pkFields = currentCMD.pkFields;
920             JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
921             for (int i = 0; i < pkc.length; i++) {
922                 String JavaDoc stateFieldName = "_" + pkFields[i].fieldNo;
923                 String JavaDoc fieldName = "_" + i;
924                 buf.append("\t\t");
925                 buf.append(fieldName);
926                 buf.append(" = other.");
927                 buf.append(stateFieldName);
928                 buf.append(";\n");
929             }
930         }
931         buf.append("\t}\n");
932         spec.addMethod(buf.toString());
933     }
934
935     protected void addEqualsObject() {
936         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
937         buf.append("\n\tpublic boolean equals(Object object) {\n");
938         buf.append("\t\t");
939         buf.append("if (object instanceof ");
940         buf.append(className);
941         buf.append(") {\n");
942         buf.append("\t\t\t");
943         buf.append(className);
944         buf.append(" other = (");
945         buf.append(className);
946         buf.append(")object;\n");
947         JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
948         for (int i = 0; i < pkc.length; i++) {
949             String JavaDoc fieldName = "_" + i;
950             boolean isPrimitive = pkc[i].javaType.isPrimitive();
951
952             if (isPrimitive) {
953                 /*
954                 if (_0 != other._0) return false;
955                 */

956                 buf.append("\t\t\t");
957                 buf.append("if (");
958                 buf.append(fieldName);
959                 buf.append(" != other.");
960                 buf.append(fieldName);
961                 buf.append(") return false;\n");
962
963             } else { // this is a object and it can be null
964
/*
965                 if (_3 != null) {
966                     if (!_3.equals(other._3)) return false;
967                 } else if (other._3 != null) return false;
968
969                 */

970                 buf.append("\t\t\t");
971                 buf.append("if (");
972                 buf.append(fieldName);
973                 buf.append(" != null){\n");
974                 buf.append("\t\t\t\t");
975                 buf.append("if (!");
976                 buf.append(fieldName);
977                 buf.append(".equals(other.");
978                 buf.append(fieldName);
979                 buf.append(")) return false;\n");
980                 buf.append("\t\t\t");
981                 buf.append("} else if (other.");
982                 buf.append(fieldName);
983                 buf.append(" != null) return false;\n");
984             }
985         }
986         buf.append("\t\t\treturn true;\n");
987         buf.append("\t\t} else {\n\t\t\treturn false;\n\t\t}\n\t}\n");
988         spec.addMethod(buf.toString());
989     }
990
991     protected void addHashCode() {
992         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
993         buf.append("\n\tpublic int hashCode() {\n");
994         int noOfVars = 0;
995         JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
996         noOfVars = pkc.length;
997         for (int i = 0; i < noOfVars; i++) {
998             Class JavaDoc fieldType = pkc[i].javaType;
999             String JavaDoc fieldName = "_" + i;
1000            boolean isPrimitive = pkc[i].javaType.isPrimitive();
1001
1002            /* If there is only one pk field we speed things up */
1003            if (noOfVars == 1) {
1004                if (isPrimitive) {
1005                    if (fieldType.equals(long.class)
1006                            || fieldType.equals(float.class)
1007                            || fieldType.equals(double.class)) {
1008                        /*
1009                        return (int)(877146213 * _0);
1010                        */

1011                        buf.append("\t\treturn (int)("+ cmd.classId);
1012                        buf.append(" * ");
1013                        buf.append(fieldName);
1014                        buf.append(");\n");
1015                    } else if (fieldType.equals(boolean.class)){
1016                        /*
1017                        return _0 ? 1 : -1;
1018                        */

1019                        buf.append("\t\treturn ");
1020                        buf.append(fieldName);
1021                        buf.append(" ? 1 : -1;\n");
1022
1023                    } else {
1024                        /*
1025                        return 877146213 * _0;
1026                        */

1027                        buf.append("\t\treturn " + cmd.classId);
1028                        buf.append(" * ");
1029                        buf.append(fieldName);
1030                        buf.append(";\n");
1031                    }
1032                } else { // this is a object and it can be null
1033
/*
1034                    return 877146213 * _0.hashCode(); ???????
1035                    */

1036                    buf.append("\t\treturn ");
1037                    buf.append(fieldName);
1038                    buf.append(".hashCode();\n");
1039                }
1040            } else { /** We have multiple fields */
1041                if (i == 0) {
1042                    buf.append("\t\tint hashCode = 0;\n");
1043                }
1044                if (isPrimitive) {
1045                    if (fieldType.equals(long.class)
1046                            || fieldType.equals(float.class)
1047                            || fieldType.equals(double.class)) {
1048                        /*
1049                        hashCode += _4;
1050                        */

1051                        buf.append("\t\thashCode += ");
1052                        buf.append(fieldName);
1053                        buf.append(";\n");
1054                    } else if (fieldType.equals(boolean.class)) {
1055                        /*
1056                        hashCode += _1 ? 1 : -1;
1057                        */

1058                        buf.append("\t\thashCode += ");
1059                        buf.append(fieldName);
1060                        buf.append(" ? 1 : -1;\n");
1061
1062                    } else {
1063                        /*
1064                        hashCode += _4;
1065                        */

1066                        buf.append("\t\thashCode += ");
1067                        buf.append(fieldName);
1068                        buf.append(";\n");
1069                    }
1070                } else { // this is a object and it can be null
1071
/*
1072                    hashCode += _4;
1073                    */

1074                    buf.append("\t\thashCode += ");
1075                    buf.append(fieldName);
1076                    buf.append(".hashCode();\n");
1077                }
1078            }
1079        }
1080        if (noOfVars > 1) {
1081            buf.append("\t\treturn hashCode;\n\t}\n");
1082        } else {
1083            buf.append("\t}\n");
1084        }
1085        spec.addMethod(buf.toString());
1086    }
1087
1088    /**
1089     * Add all PK field(s).
1090     */

1091    protected void addFields() {
1092        super.addFields();
1093        JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
1094        for (int i = 0; i < pkc.length; i++) {
1095            spec.addField("public " + pkc[i].javaType.getName() + " _" + i);
1096            if (pkc[i].converter != null) {
1097                spec.addField("public static " +
1098                        pkc[i].converter.getClass().getName() + " " +
1099                        JDBC_CONVERTER_FIELD_PREFIX + i);
1100                spec.addField("public static " +
1101                        pkc[i].getClass().getName() + " jdbcCol_" + i);
1102            }
1103        }
1104    }
1105
1106    protected void addCopyKeyFieldsObjects() {
1107        StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
1108        buf.append("\n\tpublic void copyKeyFields(Object[] data) {\n");
1109        JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk;
1110        for (int i = 0; i < pkc.length; i++) {
1111            Class JavaDoc fieldType = pkc[i].javaType;
1112            String JavaDoc fieldName = "_" + i;
1113            boolean isPrimitive = pkc[i].javaType.isPrimitive();
1114
1115            if (isPrimitive) {
1116                /*
1117                _0 = ((Integer)data[0]).intValue();
1118                */

1119                buf.append("\t\t");
1120                buf.append(fieldName);
1121                buf.append(" = ((");
1122                buf.append((String JavaDoc)primitiveToWrapperTypes.get(fieldType));
1123                buf.append(")data["+i);
1124                buf.append("]).");
1125                buf.append((String JavaDoc) primitiveTypesToValue.get(fieldType));
1126                buf.append("();\n");
1127            } else {
1128                /*
1129                _3 = (String)data[3];
1130                */

1131                buf.append("\t\t");
1132                buf.append(fieldName);
1133                buf.append(" = (");
1134                buf.append(fieldType.getName());
1135                buf.append(")data[" + i);
1136                buf.append("];\n");
1137            }
1138        }
1139        buf.append("\t}\n");
1140        spec.addMethod(buf.toString());
1141    }
1142
1143    protected void addToString() {
1144        StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
1145        buf.append("\n\tpublic String toString() {\n");
1146        if (cmd.isInHeirachy()) {
1147            buf.append(
1148                    "\t\tif (!resolved) {\n" +
1149                    "\t\t\tthrow BindingSupportImpl.getInstance().internal(\n" +
1150                    "\t\t\t\t\"Called 'toString()' on unresolved oid\");\n" +
1151                    "\t\t}\n");
1152        }
1153        buf.append("\t\tStringBuffer s = new StringBuffer();\n");
1154        if (currentCMD.isInHeirachy()) {
1155            buf.append("\t\ts.append(cmd.classId);\n");
1156        } else {
1157            buf.append("\t\ts.append(\""+ cmd.classId);
1158            buf.append("\");\n");
1159        }
1160        JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk;
1161        for (int i = 0; i < pkc.length; i++) {
1162            String JavaDoc fieldName = "_" + i;
1163            buf.append("\t\ts.append(\"" + MDStatics.OID_STRING_SEPERATOR + "\");\n");
1164            buf.append("\t\ts.append("+ fieldName +");\n");
1165        }
1166        buf.append("\t\treturn s.toString();\n\t}\n");
1167        spec.addMethod(buf.toString());
1168    }
1169
1170}
1171
1172
Popular Tags