KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > metadata > generator > OIDSrcGenerator


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.metadata.generator;
13
14 import com.versant.core.common.*;
15 import com.versant.core.util.FastParser;
16 import com.versant.core.util.OIDObjectOutput;
17 import com.versant.core.util.OIDObjectInput;
18 import com.versant.core.metadata.*;
19 import com.versant.core.metadata.*;
20 import com.versant.core.compiler.ClassSpec;
21 //import com.versant.core.jdbc.metadata.JdbcField;
22

23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.io.*;
26
27 /**
28  * Generates java source for an OID class for a PC class.
29  */

30 public abstract class OIDSrcGenerator {
31
32     protected final ModelMetaData jmd;
33     protected ClassMetaData cmd;
34     protected String JavaDoc className;
35     protected ClassSpec spec;
36     protected ClassMetaData currentCMD = null;
37
38     protected final String JavaDoc CLASS_INDEX = "classIndex";
39     protected final String JavaDoc RESOLVED = "resolved";
40
41     protected HashMap JavaDoc typeToResultSetGetField;
42     protected HashMap JavaDoc typeToPreparedStatementSetField;
43
44     protected HashMap JavaDoc primitiveToWrapperTypes;
45     protected HashMap JavaDoc primitiveTypesToValue;
46     protected HashMap JavaDoc wrapperTypesToValue;
47     protected HashMap JavaDoc wrapperTypesToPrimitive;
48
49     protected String JavaDoc stateClassName;
50
51     public static final String JavaDoc JDBC_CONVERTER_FIELD_PREFIX = "jdbcConverter_";
52
53     protected HashMap JavaDoc primClassToSerReadMethod;
54     protected HashMap JavaDoc primClassToSerWriteMethod;
55
56     public OIDSrcGenerator(ModelMetaData jmd) {
57         this.jmd = jmd;
58
59         wrapperTypesToValue = new HashMap JavaDoc(8);
60         wrapperTypesToValue.put(int.class, "intValue");
61         wrapperTypesToValue.put(byte.class, "byteValue");
62         wrapperTypesToValue.put(char.class, "charValue");
63         wrapperTypesToValue.put(short.class, "shortValue");
64         wrapperTypesToValue.put(float.class, "floatValue");
65         wrapperTypesToValue.put(double.class, "doubleValue");
66         wrapperTypesToValue.put(long.class, "longValue");
67         wrapperTypesToValue.put(boolean.class, "booleanValue");
68
69
70         primitiveToWrapperTypes = new HashMap JavaDoc(8);
71         primitiveToWrapperTypes.put(int.class, "Integer");
72         primitiveToWrapperTypes.put(byte.class, "Byte");
73         primitiveToWrapperTypes.put(char.class, "Character");
74         primitiveToWrapperTypes.put(short.class, "Short");
75         primitiveToWrapperTypes.put(float.class, "Float");
76         primitiveToWrapperTypes.put(double.class, "Double");
77         primitiveToWrapperTypes.put(long.class, "Long");
78         primitiveToWrapperTypes.put(boolean.class, "Boolean");
79
80
81
82         wrapperTypesToPrimitive = new HashMap JavaDoc(8);
83         wrapperTypesToPrimitive.put(Integer JavaDoc.class, int.class);
84         wrapperTypesToPrimitive.put(Byte JavaDoc.class, byte.class);
85         wrapperTypesToPrimitive.put(Character JavaDoc.class, char.class);
86         wrapperTypesToPrimitive.put(Short JavaDoc.class, short.class);
87         wrapperTypesToPrimitive.put(Float JavaDoc.class, float.class);
88         wrapperTypesToPrimitive.put(Double JavaDoc.class, double.class);
89         wrapperTypesToPrimitive.put(Long JavaDoc.class, long.class);
90         wrapperTypesToPrimitive.put(Boolean JavaDoc.class, boolean.class);
91
92         primitiveTypesToValue = new HashMap JavaDoc(8);
93         primitiveTypesToValue.put(int.class, "intValue");
94         primitiveTypesToValue.put(byte.class, "byteValue");
95         primitiveTypesToValue.put(char.class, "charValue");
96         primitiveTypesToValue.put(short.class, "shortValue");
97         primitiveTypesToValue.put(float.class, "floatValue");
98         primitiveTypesToValue.put(double.class, "doubleValue");
99         primitiveTypesToValue.put(long.class, "longValue");
100         primitiveTypesToValue.put(boolean.class, "booleanValue");
101
102
103         typeToResultSetGetField = new HashMap JavaDoc();
104         typeToResultSetGetField.put(int.class, "getInt");
105         typeToResultSetGetField.put(byte.class, "getByte");
106         typeToResultSetGetField.put(short.class, "getShort");
107         typeToResultSetGetField.put(float.class, "getFloat");
108         typeToResultSetGetField.put(double.class, "getDouble");
109         typeToResultSetGetField.put(long.class, "getLong");
110         typeToResultSetGetField.put(boolean.class, "getBoolean");
111         typeToResultSetGetField.put(String JavaDoc.class, "getString");
112         typeToResultSetGetField.put(byte[].class, "getBytes");
113         typeToResultSetGetField.put(java.math.BigDecimal JavaDoc.class, "getBigDecimal"); // has a scale
114
typeToResultSetGetField.put(java.util.Date JavaDoc.class, "getDate");
115         typeToResultSetGetField.put(java.sql.Time JavaDoc.class, "getTime");
116         typeToResultSetGetField.put(java.sql.Timestamp JavaDoc.class, "getTimestamp");
117         typeToResultSetGetField.put(java.io.InputStream JavaDoc.class, "getAsciiStream");
118         typeToResultSetGetField.put(java.io.InputStream JavaDoc.class, "getBinaryStream");
119
120
121         typeToPreparedStatementSetField = new HashMap JavaDoc();
122         typeToPreparedStatementSetField.put(int.class, "setInt");
123         typeToPreparedStatementSetField.put(byte.class, "setByte");
124         typeToPreparedStatementSetField.put(short.class, "setShort");
125         typeToPreparedStatementSetField.put(float.class, "setFloat");
126         typeToPreparedStatementSetField.put(double.class, "setDouble");
127         typeToPreparedStatementSetField.put(long.class, "setLong");
128         typeToPreparedStatementSetField.put(boolean.class, "setBoolean");
129         typeToPreparedStatementSetField.put(String JavaDoc.class, "setString");
130         typeToPreparedStatementSetField.put(Object JavaDoc.class, "setObject");
131         typeToPreparedStatementSetField.put(byte[].class, "setBytes");
132         typeToPreparedStatementSetField.put(java.math.BigDecimal JavaDoc.class, "setBigDecimal");
133         typeToPreparedStatementSetField.put(java.util.Date JavaDoc.class, "setDate");
134         typeToPreparedStatementSetField.put(java.sql.Time JavaDoc.class, "setTime");
135         typeToPreparedStatementSetField.put(java.sql.Timestamp JavaDoc.class, "setTimestamp");
136         typeToPreparedStatementSetField.put(java.io.InputStream JavaDoc.class, "setAsciiStream");
137         typeToPreparedStatementSetField.put(java.io.InputStream JavaDoc.class, "setBinaryStream");
138
139         primClassToSerReadMethod = new HashMap JavaDoc(8);
140         primClassToSerReadMethod.put(int.class, "readInt");
141         primClassToSerReadMethod.put(byte.class, "readByte");
142         primClassToSerReadMethod.put(char.class, "readChar");
143         primClassToSerReadMethod.put(short.class, "readShort");
144         primClassToSerReadMethod.put(float.class, "readFloat");
145         primClassToSerReadMethod.put(double.class, "readDouble");
146         primClassToSerReadMethod.put(long.class, "readLong");
147         primClassToSerReadMethod.put(boolean.class, "readBoolean");
148
149         primClassToSerWriteMethod = new HashMap JavaDoc(8);
150         primClassToSerWriteMethod.put(int.class, "writeInt");
151         primClassToSerWriteMethod.put(byte.class, "writeByte");
152         primClassToSerWriteMethod.put(char.class, "writeChar");
153         primClassToSerWriteMethod.put(short.class, "writeShort");
154         primClassToSerWriteMethod.put(float.class, "writeFloat");
155         primClassToSerWriteMethod.put(double.class, "writeDouble");
156         primClassToSerWriteMethod.put(long.class, "writeLong");
157         primClassToSerWriteMethod.put(boolean.class, "writeBoolean");
158     }
159
160     public ClassSpec generateOID(ClassMetaData cmd) {
161         this.cmd = cmd;
162         this.className = cmd.oidClassName;
163         this.stateClassName = cmd.stateClassName;
164         spec = new ClassSpec(null, className, null);
165
166         spec.addImportsForJavaLang();
167         spec.addImport(OID.class.getName());
168         spec.addImport(State.class.getName());
169         spec.addImport(ClassMetaData.class.getName());
170         spec.addImport(ModelMetaData.class.getName());
171         spec.addImport(ObjectOutput.class.getName());
172         spec.addImport(ObjectInput.class.getName());
173         spec.addImport(DataInputStream.class.getName());
174         spec.addImport(DataOutputStream.class.getName());
175         spec.addImport(IOException.class.getName());
176         spec.addImport(BindingSupportImpl.class.getName());
177         spec.addImport(FastParser.class.getName());
178         spec.addImport(SerUtils.class.getName());
179         spec.addImport(OIDObjectOutput.class.getName());
180         spec.addImport(OIDObjectInput.class.getName());
181         spec.addImport(Externalizable.class.getName());
182 // spec.addImport(JdbcField.class.getName());
183

184         spec.addInterface("OID");
185         spec.addInterface("Externalizable");
186
187         currentCMD = cmd.top;
188
189         addFields();
190         addInitStatics();
191         addDefaultConstructor(); //cool
192
addConstructor(); //cool
193
addCopyKeyFieldsObjects();
194         addEqualsObject();
195         addHashCode();
196         addReadExternal(); //cool
197
addWriteExternal(); //cool
198
addReadExternalFast(); //cool
199
addWriteExternalFast(); //cool
200
addCopyKeyFieldsFromState();
201         addCopyKeyFieldsUpdate();
202         addIsResolved(); //cool
203
addResolve(); //cool
204
addGetClassIndex(); //cool
205
addGetAvailableClassMetaData(); //cool
206
addGetBaseClassMetaData(); //cool
207
addGetClassMetaData(); //cool
208
addGetIdentityType(); //cool
209
addGetCopy(); //cool
210
addfillFromPK();
211         addToPKString();
212         addToSString();
213         addToString();
214         addToStringImp();
215         addFillFromIDString2();
216         addGetCompareClassIndex();
217         addCompareTo();
218         addVersion();
219         addGetLongPrimaryKey();
220         addSetLongPrimaryKey();
221         addPopulateObjectIdClassInstance();
222         addIsNewEtc();
223         addGetAvailableClassId();
224         addFillFromIDObject();
225
226         return spec;
227     }
228
229     protected void addIsNewEtc() {
230         spec.addMethod(
231                 "public boolean isNew() {\n" +
232                 " return false;\n" +
233                 "}");
234         spec.addMethod(
235                 "public OID getMappedOID() {\n" +
236                 " return this;\n" +
237                 "}");
238         spec.addMethod(
239                 "public OID getRealOID() {\n" +
240                 " return this;\n" +
241                 "}");
242         spec.addMethod(
243                 "public OID getAvailableOID() {\n" +
244                 " return this;\n" +
245                 "}");
246     }
247
248     protected void addPopulateObjectIdClassInstance() {
249         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
250         buf.append("\n\tpublic void populateObjectIdClassInstance(Object o) {\n");
251         buf.append("\t}\n");
252         spec.addMethod(buf.toString());
253     }
254
255     protected abstract void addGetLongPrimaryKey();
256
257     protected abstract void addSetLongPrimaryKey();
258
259     protected void addVersion() {
260         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
261         buf.append("\n\tpublic String getVersion() {\n");
262         buf.append("\t\treturn \""+ Debug.VERSION +"\";\n\t}\n");
263         spec.addMethod(buf.toString());
264     }
265
266     protected abstract void addfillFromPK();
267
268     protected void addGetBaseClassMetaData() {
269         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
270         buf.append("\n\tpublic ClassMetaData getBaseClassMetaData() {\n");
271         if (cmd.isInHeirachy()) {
272             buf.append("\t\treturn top;\n");
273         } else {
274             buf.append("\t\treturn cmd;\n");
275         }
276         buf.append("\t}\n");
277         spec.addMethod(buf.toString());
278     }
279
280     protected void addGetAvailableClassMetaData() {
281         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
282         buf.append("\n\tpublic ClassMetaData getAvailableClassMetaData() {\n");
283         buf.append("\t\treturn cmd;\n");
284         buf.append("\t}\n");
285         spec.addMethod(buf.toString());
286     }
287
288     protected void addGetAvailableClassId() {
289         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
290         buf.append("\n\tpublic int getAvailableClassId() {\n");
291         if (cmd.isInHeirachy()) {
292             buf.append("\t\treturn cmd.classId;\n");
293         } else {
294             buf.append("\t\treturn " + cmd.classId + ";\n");
295         }
296         buf.append("\t}\n");
297         spec.addMethod(buf.toString());
298     }
299
300     protected void addGetClassMetaData() {
301         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
302         buf.append("\n\tpublic ClassMetaData getClassMetaData() {\n");
303         if (cmd.isInHeirachy()) {
304             buf.append(
305                     "\t\tif (!resolved) {\n" +
306                     "\t\t\tthrow BindingSupportImpl.getInstance().internal(\n" +
307                     "\t\t\t\t\"Called 'getClassMetaData()' on unresolved oid\");\n" +
308                     "\t\t}\n");
309         }
310         buf.append("\t\treturn cmd;\n");
311         buf.append("\t}\n");
312         spec.addMethod(buf.toString());
313     }
314
315     protected void addGetClassIndex() {
316         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
317         buf.append("\n\tpublic int getClassIndex() {\n");
318         if (cmd.isInHeirachy()) {
319             buf.append("\t\treturn cmd.index;\n");
320         } else {
321             buf.append("\t\treturn "+ cmd.index);
322             buf.append(";\n");
323         }
324         buf.append("\t}\n");
325         spec.addMethod(buf.toString());
326     }
327
328     protected void addGetCompareClassIndex() {
329         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
330         /*
331         public int getCompareClassIndex() {
332            return 93;
333         }
334         */

335         buf.append("\n\tpublic int getCompareClassIndex() {\n");
336         buf.append("\t\treturn "+ getIndex() +";\n\t}\n");
337         spec.addMethod(buf.toString());
338     }
339
340     protected abstract void addCompareTo();
341
342     protected int getIndex() {
343         if (cmd.isInHeirachy()) {
344             return cmd.pcHeirachy[0].index;
345         } else {
346             return cmd.index;
347         }
348     }
349
350
351     protected void addResolve() {
352         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
353         buf.append("\n\tpublic void resolve(State state) {\n");
354         if (cmd.isInHeirachy()) {
355             buf.append("\t\tif (!resolved) {\n");
356             buf.append("\t\t\tcmd = state.getClassMetaData();\n");
357             buf.append("\t\t\tresolved = true;\n");
358             buf.append("\t\t}\n");
359         }
360         buf.append("\t}\n");
361         spec.addMethod(buf.toString());
362     }
363
364
365     protected void addIsResolved() {
366         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
367         /*
368         public boolean isResolved() {
369         return resolved;
370         }
371         */

372         buf.append("\n\tpublic boolean isResolved() {\n");
373         if (cmd.isInHeirachy()) {
374             buf.append("\t\treturn resolved;\n");
375         } else {
376             buf.append("\t\treturn true;\n");
377         }
378         buf.append("\t}\n");
379         spec.addMethod(buf.toString());
380     }
381
382     protected abstract void addToPKString();
383
384     protected abstract void addToSString();
385
386     protected ClassMetaData[] getAllSubClasses(ClassMetaData topCMD) {
387         ArrayList JavaDoc list = new ArrayList JavaDoc();
388         ClassMetaData[] cmds = jmd.classes;
389         for (int i = 0; i < cmds.length; i++) {
390             ClassMetaData cmd = cmds[i];
391             if (cmd.top.index == topCMD.index) {
392                 list.add(cmd);
393             }
394         }
395         ClassMetaData[] allCMDs = new ClassMetaData[list.size()];
396         list.toArray(allCMDs);
397         return allCMDs;
398     }
399
400     protected void addFillFromIDString2() {
401         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
402         /*
403         public void fillFromIDString(String idString, int index) {
404             _0 = FastParser.parseInt(idString, index);
405         }
406         */

407         buf.append("\n\tpublic void fillFromIDString(String idString, int index) {\n");
408
409         if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
410             buf.append("\t\tthrow new javax.jdo.JDOFatalInternalException(\"fillFromIDString() called on applicationId class.\");");
411         } else if (cmd.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) {
412             switch (cmd.datastoreIdentityTypeCode) {
413                 case MDStatics.INT:
414                     buf.append("\t\t_0 = FastParser.parseInt(idString, index);");
415                     break;
416                 case MDStatics.LONG:
417                     buf.append("\t\t_0 = FastParser.parseLong(idString, index);");
418                     break;
419                 default:
420                     throw BindingSupportImpl.getInstance().runtime(
421                             "Unhandled java type code " + cmd.datastoreIdentityTypeCode);
422             }
423         }
424         buf.append("\t}\n");
425         spec.addMethod(buf.toString());
426     }
427
428     protected void addToStringImp() {
429         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
430         /*
431         public String toStringImp() {
432             if (!resolved) return "null pk";
433             else return toString();
434         }
435         */

436         buf.append("\n\tpublic String toStringImp() {\n");
437         if (!currentCMD.isInHeirachy()) {
438            buf.append("\t\treturn toString();\n");
439         } else {
440             /*
441             if (!resolved) return "null pk";
442             else return toString();
443             */

444             buf.append("\t\tif (!resolved) return \"null pk\";\n");
445             buf.append("\t\telse return this.toString();\n");
446         }
447         buf.append("\t}\n");
448         spec.addMethod(buf.toString());
449     }
450
451     protected abstract void addToString();
452
453     protected void addGetIdentityType() {
454         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
455         buf.append("\n\tpublic int getIdentityType() {\n");
456         buf.append("\t\treturn "+ cmd.identityType + ";\n");
457         buf.append("\t}\n");
458         spec.addMethod(buf.toString());
459     }
460
461     protected abstract void addGetCopy();
462
463     protected abstract void addCopyKeyFieldsUpdate();
464
465     protected abstract void addCopyKeyFieldsFromState();
466
467     protected void addReadExternal() {
468         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
469         buf.append("\n\tpublic void readExternal(ObjectInput is) throws ClassNotFoundException, IOException {\n");
470         if (cmd.isInHeirachy()) {
471             buf.append("\t\tcmd = top.jmd.classes[is.readShort()];\n");
472             buf.append("\t\tresolved = is.readBoolean();\n");
473         }
474         addReadExternalImp(buf);
475         buf.append("\t}\n");
476         spec.addMethod(buf.toString());
477     }
478
479     protected void addWriteExternal() {
480         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
481         buf.append("\n\tpublic void writeExternal(ObjectOutput os) throws IOException {\n");
482         if (cmd.isInHeirachy()) {
483             buf.append("\t\tos.writeShort(cmd.index);\n");
484             buf.append("\t\tos.writeBoolean(resolved);\n");
485         }
486         addWriteExternalImp(buf);
487         buf.append("\t}\n");
488         spec.addMethod(buf.toString());
489     }
490
491     protected void addReadExternalFast() {
492         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
493         buf.append("\n\tpublic void readExternal(OIDObjectInput is) throws ClassNotFoundException, IOException {\n");
494         addReadExternalImp(buf);
495         buf.append("\t}\n");
496         spec.addMethod(buf.toString());
497     }
498
499     protected void addWriteExternalFast() {
500         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
501         buf.append("\n\tpublic void writeExternal(OIDObjectOutput os) throws IOException {\n");
502         addWriteExternalImp(buf);
503         buf.append("\t}\n");
504         spec.addMethod(buf.toString());
505     }
506
507     protected void addReadExternalImp(StringBuffer JavaDoc buf) {
508         Class JavaDoc bigIntegerType = java.math.BigInteger JavaDoc.class;
509         boolean isBigInteger = false; // 2
510
FieldMetaData[] pkc = currentCMD.pkFields;
511         if (pkc != null) {
512             for (int i = 0; i < pkc.length; i++) {
513                 if (pkc[i].type.equals(bigIntegerType)) {
514                     isBigInteger = true;
515                 }
516             }
517         }
518         if (isBigInteger) {
519             buf.append("\t\tbyte bytes[] = null;\n");
520         }
521         int n = pkc == null ? 1 : pkc.length;
522         for (int i = 0; i < n; i++) {
523             Class JavaDoc type = pkc == null ? currentCMD.datastoreIdentityType : pkc[i].type;
524             if (type.isPrimitive()) {// do primative types
525
buf.append("\t\t_" + i + " = is." + primClassToSerReadMethod.get(type) + "();\n");
526             } else if (type.equals(String JavaDoc.class)) {
527                 buf.append("\t\t_" + i + " = com.versant.core.common.Utils.readLongUTF8(is);\n");
528             } else if (type.equals(java.util.Locale JavaDoc.class)) {
529                 buf.append("\t\t_" + i + " = new java.util.Locale(is.readUTF(), is.readUTF(), is.readUTF());\n");
530             } else if (type.equals(java.math.BigDecimal JavaDoc.class)) {
531                 buf.append("\t\t_" + i + " = new java.math.BigDecimal(is.readUTF());\n");
532             } else if (type.equals(java.math.BigInteger JavaDoc.class)) {
533                 buf.append("\t\tbytes = new byte[is.readInt()];\n");
534                 buf.append("\t\tis.readFully(bytes);\n");
535                 buf.append("\t\t_" + i + " = new java.math.BigInteger(bytes);\n");
536             } else if (type.equals(java.util.Date JavaDoc.class)) {
537                 buf.append("\t\t_" + i + " = new java.util.Date(is.readLong());\n");
538             } else if (type.equals(java.net.URL JavaDoc.class)) {
539                 buf.append("\t\t_" + i + " = new java.net.URL(is.readUTF());\n");
540             } else if (type.equals(File.class)) {
541                 buf.append("\t\t_" + i + " = new java.io.File(is.readUTF());\n");
542             } else if (type.equals(java.sql.Timestamp JavaDoc.class)) {
543                 buf.append("\t\t_" + i + " = new java.sql.Timestamp(is.readLong());\n");
544             } else if (wrapperTypesToPrimitive.containsKey(type)) { // wrapper
545
buf.append("\t\t_" + i + " = new " + type.getName() + "(is." + primClassToSerReadMethod.get(wrapperTypesToPrimitive.get(type)) + "());\n");
546             } else {
547                 buf.append("\t\t_" + i + " = (" + type.getName() + ")SerUtils.readObject(is);\n");
548             }
549         }
550     }
551
552     protected void addWriteExternalImp(StringBuffer JavaDoc buf) {
553         Class JavaDoc bigIntegerType = java.math.BigInteger JavaDoc.class;
554         boolean isBigInteger = false; // 2
555
FieldMetaData[] pkc = currentCMD.pkFields;
556         if (pkc != null) {
557             for (int i = 0; i < pkc.length; i++) {
558                 if (pkc[i].type.equals(bigIntegerType)) {
559                     isBigInteger = true;
560                 }
561             }
562         }
563         if (isBigInteger) {
564             buf.append("\t\tbyte bytes[] = null;\n");
565         }
566         int n = pkc == null ? 1 : pkc.length;
567         for (int i = 0; i < n; i++) {
568             Class JavaDoc type = pkc == null ? currentCMD.datastoreIdentityType : pkc[i].type;
569             if (type.isPrimitive()) {// do primative types
570
buf.append("\t\tos." + primClassToSerWriteMethod.get(type) + "(_" + i + ");\n");
571             } else if (type.equals(String JavaDoc.class)) {
572                 buf.append("\t\tcom.versant.core.common.Utils.writeLongUTF8(_" + i + ", os);\n");
573             } else if (type.equals(java.util.Locale JavaDoc.class)) {
574                 buf.append("\t\tos.writeUTF(_" + i + ".getLanguage());\n");
575                 buf.append("\t\tos.writeUTF(_" + i + ".getCountry());\n");
576                 buf.append("\t\tos.writeUTF(_" + i + ".getVariant());\n");
577             } else if (type.equals(java.math.BigDecimal JavaDoc.class)) {
578                 buf.append("\t\tos.writeUTF(_" + i + ".toString());\n");
579             } else if (type.equals(java.math.BigInteger JavaDoc.class)) {
580                 buf.append("\t\tbytes = _" + i + ".toByteArray();\n");
581                 buf.append("\t\tos.writeInt(bytes.length);\n");
582                 buf.append("\t\tos.write(bytes);\n");
583             } else if (type.equals(java.util.Date JavaDoc.class)) {
584                 buf.append("\t\tos.writeLong(_" + i + ".getTime());\n");
585             } else if (type.equals(java.net.URL JavaDoc.class)) {
586                 buf.append("\t\tos.writeUTF(_" + i + ".toString());\n");
587             } else if (type.equals(File.class)) {
588                 buf.append("\t\tos.writeUTF(_" + i + ".toString());\n");
589             } else if (type.equals(java.sql.Timestamp JavaDoc.class)) {
590                 buf.append("\t\tos.writeLong(_" + i + ".getTime());\n");
591             } else if (wrapperTypesToPrimitive.containsKey(type)) { // wrapper
592
buf.append("\t\tos." +
593                         primClassToSerWriteMethod.get(wrapperTypesToPrimitive.get(type)) +
594                         "(_" + i + "." + wrapperTypesToValue.get(wrapperTypesToPrimitive.get(type)) + "());\n");
595             } else {
596                 buf.append("\t\tSerUtils.writeObject(_" + i + ", os);\n");
597             }
598         }
599     }
600
601     protected abstract void addEqualsObject();
602
603     protected abstract void addHashCode();
604
605     /**
606      * Add's a default constructor that calls the OID super class
607      * this constructor is needed so that we can get an instance from the class with class.newInstance()
608      */

609     protected void addDefaultConstructor() {
610         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
611         buf.append("\n\tpublic ");
612         buf.append(className);
613         buf.append("(){}\n");
614         spec.addMethod(buf.toString());
615     }
616
617     /**
618      * Add's a constructor that calls the OID super class
619      * this constructor is needed only when there is a class hier involved and we keep a
620      * classIndex var in the class.
621      */

622     protected void addConstructor() {
623         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
624         if (cmd.isInHeirachy()) {
625             buf.append("\n\tpublic ");
626             buf.append(className);
627             buf.append("(ClassMetaData cmd, boolean resolved){\n");
628             buf.append("\t\tthis.cmd = cmd;\n");
629             buf.append("\t\tthis.resolved = resolved;\n\t}\n");
630         }
631         spec.addMethod(buf.toString());
632     }
633
634     protected ClassMetaData tempClassMetaData;
635
636     protected ClassMetaData getTopPCSuperClassMetaData() {
637         if (cmd.pcSuperMetaData == null) {
638             return cmd;
639         } else {
640             setTopPCSuperClassMetaData(cmd);
641         }
642         return tempClassMetaData;
643     }
644
645     protected void setTopPCSuperClassMetaData(ClassMetaData currentCMD) {
646         if (currentCMD.pcSuperMetaData == null) {
647             tempClassMetaData = currentCMD;
648             return;
649         } else {
650             setTopPCSuperClassMetaData(currentCMD.pcSuperMetaData);
651         }
652     }
653
654     protected ClassMetaData getCurrentCMD() {
655         if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
656             return getTopPCSuperClassMetaData();
657         } else if (cmd.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) {
658             return cmd;
659         } else {
660             throw BindingSupportImpl.getInstance().unsupported("");
661         }
662     }
663
664     /**
665      * Add all PK field(s).
666      */

667     protected void addFields() {
668         if (cmd.isInHeirachy()) {
669             spec.addField("public static ClassMetaData top");
670             spec.addField("public ClassMetaData cmd");
671             spec.addField("public boolean " + RESOLVED);
672         } else {
673             spec.addField("public static ClassMetaData cmd");
674         }
675     }
676
677     /**
678      * Add method to fill in values of static fields from ModelMetaData.
679      * This is invoked once the class has been loaded.
680      */

681     protected void addInitStatics() {
682         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
683         buf.append("\tpublic static boolean initStatics(ModelMetaData jmd) {\n");
684         addInitStaticsBody(buf);
685         buf.append("\t\treturn true;\n");
686         buf.append("\t}");
687         spec.addMethod(buf.toString());
688     }
689
690     protected void addInitStaticsBody(StringBuffer JavaDoc buf) {
691         if (cmd.isInHeirachy()) {
692             buf.append("\t\tif (top != null) return false;\n");
693             buf.append("\t\ttop = jmd.classes[" + cmd.top.index + "];\n");
694         } else {
695             buf.append("\t\tif (cmd != null) return false;\n");
696             buf.append("\t\tcmd = jmd.classes[" + cmd.index + "];\n");
697         }
698     }
699
700     protected abstract void addCopyKeyFieldsObjects();
701
702     protected String JavaDoc getFieldName(int num) {
703         return "_" + num;
704     }
705
706     protected void addFillFromIDObject() {
707 // public OID fillFromIDObject(Object id) {
708
// pk = new Object[cmd.pkFields.length];
709
// if (cmd.objectIdClass == null) {
710
// if (cmd.pkFields.length != 1) {
711
// throw BindingSupportImpl.getInstance().invalidOperation("Classes with application can only have a single pk " +
712
// "field if a ApplicationId class is not specified");
713
// }
714
// pk[0] = id;
715
// } else {
716
// FieldMetaData[] pkFields = cmd.pkFields;
717
// Object[] pkValues = pk;
718
// for (int i = 0; i < pkFields.length; i++) {
719
// try {
720
// pkValues[i] = pk.getClass().getField(pkFields[i].getPkFieldName()).get(pk);
721
// } catch (Exception e) {
722
// throw BindingSupportImpl.getInstance().internal(e.getMessage(), e);
723
// }
724
// }
725
// }
726
// return this;
727
// }
728
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
729         buf.append("\n\tpublic OID fillFromIDObject(Object id) {\n");
730         if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
731             if (currentCMD.objectIdClass == null) {
732                 if (currentCMD.pkFields.length != 1) {
733                     throw BindingSupportImpl.getInstance().invalidOperation("Class "+
734                             currentCMD.cls.getName()
735                             +" with application can only have a single pk " +
736                             "field if a ApplicationId class is not specified");
737                 }
738                 Class JavaDoc type = currentCMD.pkFields[0].type;
739                 if (type.isPrimitive() && !type.isArray()) {// do primative types
740
buf.append("\t\t_0 = (("+ primitiveToWrapperTypes.get(type) +
741                             ")id)."+ wrapperTypesToValue.get(type) +"();\n");
742                 } else { // wrapper
743
buf.append("\t\t_0 = (" + type.getName() + ")id;\n");
744                 }
745             } else {
746                 // do other stuff here
747
}
748         }
749         buf.append("\t\treturn this;\n");
750         buf.append("\t}\n");
751         spec.addMethod(buf.toString());
752
753     }
754
755
756
757
758 }
759
760
761
Popular Tags