1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.exolab.castor.builder.descriptors;
17
18 import java.util.List;
19
20 import org.castor.core.constants.cpa.JDOConstants;
21 import org.exolab.castor.builder.BuilderConfiguration;
22 import org.exolab.castor.builder.info.ClassInfo;
23 import org.exolab.castor.builder.info.FieldInfo;
24 import org.exolab.castor.builder.info.nature.JDOClassInfoNature;
25 import org.exolab.castor.builder.info.nature.JDOFieldInfoNature;
26 import org.exolab.castor.builder.info.nature.XMLInfoNature;
27 import org.exolab.castor.builder.info.nature.relation.JDOOneToManyNature;
28 import org.exolab.castor.builder.info.nature.relation.JDOOneToOneNature;
29 import org.exolab.castor.builder.types.XSList;
30 import org.exolab.castor.builder.types.XSType;
31 import org.exolab.javasource.JClass;
32 import org.exolab.javasource.JConstructor;
33 import org.exolab.javasource.JNaming;
34 import org.exolab.javasource.JPrimitiveType;
35 import org.exolab.javasource.JSourceCode;
36 import org.exolab.javasource.JType;
37
38
39
40
41
42
43
44
45
46
47
48
49 public final class JDOClassDescriptorFactory {
50
51
52
53
54 private final BuilderConfiguration _config;
55
56
57
58
59 private String _fields = null;
60
61
62
63
64 private String _identities = null;
65
66
67
68
69
70
71
72
73 public JDOClassDescriptorFactory(final BuilderConfiguration config) {
74 if (config == null) {
75 String err = "The argument 'config' must not be null.";
76 throw new IllegalArgumentException(err);
77 }
78 _config = config;
79 }
80
81
82
83
84
85
86
87
88
89 public JClass createSource(final ClassInfo classInfo) {
90 if (!checkClassInfoNature(classInfo)) {
91 return null;
92 }
93
94 JClass jClass = classInfo.getJClass();
95 String descriptorClassName =
96 getQualifiedJDODescriptorClassName(jClass.getName());
97 JDODescriptorJClass classDesc =
98 new JDODescriptorJClass(_config, descriptorClassName, jClass);
99 JDOClassInfoNature cNature = new JDOClassInfoNature(classInfo);
100
101
102 JConstructor ctor = classDesc.getConstructor(0);
103 JSourceCode jsc = ctor.getSourceCode();
104
105 jsc = createClassInfoPart(classInfo, jsc);
106
107
108
109
110
111 for (int i = 0; i < classInfo.getElementFields().length; i++) {
112 FieldInfo fInfo = classInfo.getElementFields()[i];
113 if (checkFieldInfoNatures(fInfo)) {
114
115 if (fInfo.hasNature(JDOOneToOneNature.class.getName())) {
116 jsc = createOneToOneFieldInfoPart(fInfo, jsc);
117
118 } else if (fInfo.hasNature(JDOOneToManyNature.class.getName())) {
119 jsc = createOneToManyFieldInfoPart(fInfo, jsc);
120 } else {
121 jsc = createEntityFieldInfoPart(fInfo, jsc);
122 }
123 }
124 }
125
126
127 _fields = setFields(classInfo.getElementFields());
128 _identities = setIdentities(cNature.getPrimaryKeys());
129
130 jsc.add("");
131
132 jsc.add("setFields(new FieldDescriptor[] {" + _fields + "});");
133 jsc.add("setIdentities(new FieldDescriptor[] {" + _identities + "});");
134
135 return classDesc;
136 }
137
138
139
140
141
142
143
144
145
146
147
148 private String getQualifiedJDODescriptorClassName(final String name) {
149 String descPackage = JNaming.getPackageFromClassName(name);
150 String descClassName = JNaming.getLocalNameFromClassName(name);
151
152 if (descPackage != null && descPackage.length() > 0) {
153 descPackage = descPackage + "."
154 + JDOConstants.JDO_DESCRIPTOR_PACKAGE + ".";
155 } else {
156 descPackage = "";
157 }
158
159 return descPackage + descClassName + "JDODescriptor";
160 }
161
162
163
164
165
166
167 private static String toUpperCaseFirstLetter(final String string) {
168 return string.substring(0, 1).toUpperCase() + string.substring(1);
169 }
170
171
172
173
174
175
176
177 private String setFields(final FieldInfo[] fInfos) {
178 String str = "";
179
180 FieldInfo fInfo;
181 ClassInfo cInfo;
182 for (int i = 0; i < fInfos.length; i++) {
183 fInfo = fInfos[i];
184 cInfo = fInfo.getDeclaringClassInfo();
185 JDOClassInfoNature cNature = new JDOClassInfoNature(cInfo);
186 if (cNature.getPrimaryKeys() != null) {
187 if (cNature.getPrimaryKeys().contains(new XMLInfoNature(fInfo).getNodeName())) {
188 continue;
189 }
190 }
191 if (str.equals("")) {
192 str = str + new XMLInfoNature(fInfo).getNodeName() + "FieldDescr";
193 } else {
194 str = str + "," + new XMLInfoNature(fInfo).getNodeName() + "FieldDescr";
195 }
196 }
197 return str;
198 }
199
200
201
202
203
204
205
206 private String setIdentities(final List<?> primaryKeys) {
207 String identities = "";
208 Object[] pkArray = null;
209 if (primaryKeys != null) {
210 pkArray = primaryKeys.toArray();
211 for (int i = 0; i < pkArray.length; i++) {
212 if (identities.equals("")) {
213 identities = identities + pkArray[i] + "FieldDescr";
214 } else {
215 identities = identities + "," + pkArray[i] + "FieldDescr";
216 }
217 }
218 }
219 return identities;
220 }
221
222
223
224
225
226
227
228
229 private boolean checkClassInfoNature(final ClassInfo cInfo) {
230 if (cInfo.hasNature(JDOClassInfoNature.class.getName())) {
231 JDOClassInfoNature cNature = new JDOClassInfoNature(cInfo);
232 if (cNature.getAccessMode() == null
233 || cNature.getPrimaryKeys() == null
234 || cNature.getPrimaryKeys().isEmpty()
235 || cNature.getTableName() == null
236 || cNature.getTableName().length() == 0) {
237 return false;
238 }
239 return true;
240 }
241 return false;
242 }
243
244
245
246
247
248
249
250
251 private boolean checkFieldInfoNatures(final FieldInfo fInfo) {
252 if (!fInfo.hasNature(XMLInfoNature.class.getName())) {
253 return false;
254 }
255
256 if (fInfo.hasNature(JDOFieldInfoNature.class.getName())) {
257 JDOFieldInfoNature fNature = new JDOFieldInfoNature(fInfo);
258 if (fNature.getColumnName() == null
259 || fNature.getColumnName().length() == 0
260 || fNature.getColumnType() == null) {
261 return false;
262 }
263 return true;
264 }
265
266 if (fInfo.hasNature(JDOOneToOneNature.class.getName())) {
267 JDOOneToOneNature oneNature = new JDOOneToOneNature(fInfo);
268 if (oneNature.getForeignKeys().size() != 1) {
269 return false;
270 }
271 return true;
272 }
273
274 if (fInfo.hasNature(JDOOneToManyNature.class.getName())) {
275 JDOOneToManyNature manyNature = new JDOOneToManyNature(fInfo);
276 if (manyNature.getForeignKeys().size() != 1) {
277 return false;
278 }
279 return true;
280 }
281 return false;
282 }
283
284
285
286
287
288
289
290 private JSourceCode createClassInfoPart(final ClassInfo classInfo, final JSourceCode jsc) {
291
292 JDOClassInfoNature cNature = new JDOClassInfoNature(classInfo);
293
294 jsc.add("");
295
296 jsc.add("addNature(ClassDescriptorJDONature.class.getName());");
297 jsc.add("ClassDescriptorJDONature jdoNature = new ClassDescriptorJDONature(this);");
298
299
300 String tableName = cNature.getTableName();
301 jsc.add("jdoNature.setTableName(\"" + tableName + "\");");
302
303
304
305 String className = classInfo.getJClass().getLocalName();
306 if ((className != null) && (className.length() > 0)) {
307 jsc.add("setJavaClass(");
308 jsc.append(className);
309 jsc.append(".class);");
310 }
311
312
313 String accessMode = cNature.getAccessMode().getName();
314 jsc.add("jdoNature.setAccessMode(AccessMode.valueOf(\"" + accessMode + "\"));");
315
316
317
318 String fullName = classInfo.getJClass().getName();
319 if ((fullName != null) && (fullName.length() > 0)) {
320 jsc.add("jdoNature.addCacheParam(\"name\", \"");
321 jsc.append(fullName);
322 jsc.append("\");");
323 }
324
325 jsc.add("");
326
327
328 jsc.add("mapping.setAccess(ClassMappingAccessType.fromValue(\"");
329 jsc.append(accessMode);
330 jsc.append("\"));");
331
332 jsc.add("mapping.setAutoComplete(true);");
333
334
335 if ((fullName != null) && (fullName.length() > 0)) {
336 jsc.add("mapping.setName(\"");
337 jsc.append(fullName);
338 jsc.append("\");");
339 }
340
341
342 jsc.add("mapping.setClassChoice(choice);");
343
344
345 String table = cNature.getTableName();
346 jsc.add("mapTo.setTable(\"" + table + "\");");
347
348
349 jsc.add("mapping.setMapTo(mapTo);");
350
351
352 jsc.add("setMapping(mapping);");
353
354 return jsc;
355 }
356
357
358
359
360
361
362
363
364 private JSourceCode createEntityFieldInfoPart(final FieldInfo fInfo, final JSourceCode jsc) {
365 JDOFieldInfoNature fNature = new JDOFieldInfoNature(fInfo);
366 JDOClassInfoNature cNature = new JDOClassInfoNature(fInfo.getDeclaringClassInfo());
367 XMLInfoNature xmlNature = new XMLInfoNature(fInfo);
368
369
370 String name = xmlNature.getNodeName();
371 jsc.add("");
372 jsc.add("//" + name + " field");
373 jsc.add("String " + name + "FieldName = \"" + name + "\";");
374
375
376 jsc.add("FieldDescriptorImpl " + name + "FieldDescr;");
377
378 jsc.add("FieldMapping " + name + "FM = new FieldMapping();");
379
380
381 String type = null;
382 XSType schemaType;
383
384 schemaType = xmlNature.getSchemaType();
385 JType javaType = schemaType.getJType();
386 type = javaType.toString();
387 String wrapperType = null;
388 if (javaType instanceof JPrimitiveType) {
389 wrapperType = ((JPrimitiveType) javaType).getWrapperName();
390 } else {
391 wrapperType = type;
392 }
393
394
395 if ((type != null) && (type.length() > 0)) {
396 jsc.add("TypeInfo " + name
397 + "Type = new TypeInfo(" + wrapperType + ".class);");
398 }
399
400 jsc.add("// Set columns required (= not null)");
401 jsc.add(name + "Type.setRequired("
402 + Boolean.toString(xmlNature.isRequired()) + ");");
403
404 jsc.add("");
405
406 jsc.add("FieldHandler " + name + "Handler;");
407 jsc.add("try {");
408
409
410 jsc.indent();
411 String className = fInfo.getDeclaringClassInfo().getJClass().getLocalName();
412
413 if ((className != null) && (className.length() > 0)) {
414 jsc.add("Method " + name + "GetMethod = "
415 + className + ".class.getMethod(\"get"
416 + toUpperCaseFirstLetter(name) + "\", null);");
417 jsc.add("Method " + name + "SetMethod = "
418 + className + ".class.getMethod(\"set"
419 + toUpperCaseFirstLetter(name) + "\", new Class[]{");
420 }
421
422
423 if ((type != null) && (type.length() > 0)) {
424 jsc.addIndented(type + ".class});");
425 }
426
427 jsc.add("");
428 jsc.add(name + "Handler = new FieldHandlerImpl(" + name + "FieldName, ");
429 jsc.append("null, null,");
430 jsc.addIndented(name + "GetMethod, " + name + "SetMethod, " + name + "Type);");
431 jsc.unindent();
432 jsc.add("");
433
434
435 jsc.add("} catch (SecurityException e1) {");
436 jsc.indent();
437 jsc.add("throw new RuntimeException(e1.getMessage());");
438 jsc.unindent();
439 jsc.add("} catch (MappingException e1) {");
440 jsc.indent();
441 jsc.add("throw new RuntimeException(e1.getMessage());");
442 jsc.unindent();
443 jsc.add("} catch (NoSuchMethodException e1) {");
444 jsc.indent();
445 jsc.add("throw new RuntimeException(e1.getMessage());");
446 jsc.unindent();
447 jsc.add("}");
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474 jsc.add("// Instantiate " + name + " field descriptor");
475 jsc.add(name + "FieldDescr = new FieldDescriptorImpl(");
476 jsc.append(name + "FieldName, " + name + "Type,");
477 jsc.append(name + "Handler, ");
478 jsc.append(Boolean.toString(fInfo.isTransient()) + ");");
479
480 jsc.add(name + "FieldDescr.addNature(FieldDescriptorJDONature.class.getName());");
481
482 jsc.add("FieldDescriptorJDONature " + name + "FieldJdoNature = new FieldDescriptorJDONature("
483 + name + "FieldDescr);");
484
485 String sqlName = new JDOFieldInfoNature(fInfo).getColumnName();
486 jsc.add(name + "FieldJdoNature.setSQLName(new String[] { \"" + sqlName + "\" });");
487 jsc.add(name + "FieldJdoNature.setSQLType(new int[] {SQLTypeInfos");
488 jsc.append(".javaType2sqlTypeNum(");
489
490
491 if ((type != null) && (type.length() > 0)) {
492 jsc.append(wrapperType + ".class) });");
493 }
494
495
496 jsc.add(name + "FieldJdoNature.setManyTable(null);");
497 jsc.add(name + "FieldJdoNature.setManyKey(new String[] {});");
498 jsc.add(name + "FieldJdoNature.setDirtyCheck(" + Boolean.toString(fNature.isDirty()) + ");");
499 jsc.add(name + "FieldJdoNature.setReadOnly(" + Boolean.toString(fNature.isReadOnly()) + ");");
500
501 jsc.add("");
502
503 jsc.add(name + "FieldDescr.setDirect(false);");
504 jsc.add(name + "FieldDescr.setRequired(" + xmlNature.isRequired() + ");");
505 jsc.add(name + "FieldDescr.setSetMethod(\"set"
506 + toUpperCaseFirstLetter(name) + "\");");
507 jsc.add(name + "FieldDescr.setGetMethod(\"get"
508 + toUpperCaseFirstLetter(name) + "\");");
509
510 jsc.add("");
511
512
513 jsc.add(name + "FieldDescr.setContainingClassDescriptor(this);");
514
515 boolean isPrimaryKey = false;
516 if (cNature.getPrimaryKeys() != null) {
517 isPrimaryKey = (cNature.getPrimaryKeys().contains(xmlNature.getNodeName()));
518 }
519
520 jsc.add(name + "FieldDescr.setIdentity("
521 + Boolean.toString(isPrimaryKey) + ");");
522
523
524 jsc.add(name + "FM.setIdentity(" + Boolean.toString(isPrimaryKey) + ");");
525 jsc.add(name + "FM.setDirect(false);");
526 jsc.add(name + "FM.setName(\"" + name + "\");");
527 jsc.add(name + "FM.setRequired(" + xmlNature.isRequired() + ");");
528 jsc.add(name + "FM.setSetMethod(\"set"
529 + toUpperCaseFirstLetter(name) + "\");");
530 jsc.add(name + "FM.setGetMethod(\"get"
531 + toUpperCaseFirstLetter(name) + "\");");
532
533
534 jsc.add("Sql " + name + "Sql = new Sql();");
535 jsc.add(name + "Sql.addName(\"" + name + "\");");
536
537 String sqlType = fNature.getColumnType();
538 if ((sqlType != null) && (sqlType.length() > 0)) {
539 jsc.add(name + "Sql.setType(\"" + sqlType + "\");");
540 }
541
542 jsc.add(name + "FM.setSql(" + name + "Sql);");
543
544 if ((type != null) && (type.length() > 0)) {
545 jsc.add(name + "FM.setType(\"" + type + "\");");
546 }
547
548 jsc.add("choice.addFieldMapping(" + name + "FM);");
549
550 return jsc;
551 }
552
553
554
555
556
557
558
559
560
561 private JSourceCode createOneToOneFieldInfoPart(final FieldInfo fInfo, final JSourceCode jsc) {
562
563 JDOClassInfoNature cNature = new JDOClassInfoNature(fInfo.getDeclaringClassInfo());
564 JDOOneToOneNature oneNature = new JDOOneToOneNature(fInfo);
565 XMLInfoNature xmlNature = new XMLInfoNature(fInfo);
566
567
568 String name = xmlNature.getNodeName();
569 jsc.add("");
570 jsc.add("//" + name + " field");
571 jsc.add("String " + name + "FieldName = \"" + name + "\";");
572
573 String sqlName = oneNature.getForeignKeys().get(0).toString();
574 jsc.add("String " + name + "SqlName = \"" + sqlName + "\";");
575
576
577 jsc.add("FieldDescriptorImpl " + name + "FieldDescr;");
578 jsc.add("FieldMapping " + name + "FM = new FieldMapping();");
579
580
581 String type = null;
582 XSType schemaType;
583
584 schemaType = xmlNature.getSchemaType();
585 JType javaType = schemaType.getJType();
586 type = javaType.toString();
587
588 String wrapperType = null;
589 if (javaType instanceof JPrimitiveType) {
590 wrapperType = ((JPrimitiveType) javaType).getWrapperName();
591 } else {
592 wrapperType = type;
593 }
594
595
596
597 if ((type != null) && (type.length() > 0)) {
598 jsc.add("TypeInfo " + name
599 + "Type = new TypeInfo(" + type + ".class);");
600 }
601
602 jsc.add("// Set columns required (= not null)");
603 jsc.add(name + "Type.setRequired("
604 + Boolean.toString(xmlNature.isRequired()) + ");");
605
606 jsc.add("");
607
608 jsc.add("FieldHandler " + name + "Handler;");
609 jsc.add("try {");
610
611
612 jsc.indent();
613
614 String className = fInfo.getDeclaringClassInfo().getJClass().getLocalName();
615
616 if ((className != null) && (className.length() > 0)) {
617 jsc.add("Method " + name + "GetMethod = "
618 + className + ".class.getMethod(\"get"
619 + toUpperCaseFirstLetter(name) + "\", null);");
620 jsc.add("Method " + name + "SetMethod = "
621 + className + ".class.getMethod(\"set"
622 + toUpperCaseFirstLetter(name) + "\", new Class[]{");
623 }
624
625
626 if ((type != null) && (type.length() > 0)) {
627 jsc.addIndented(type + ".class});");
628 }
629
630 jsc.add("");
631 jsc.add(name + "Handler = new FieldHandlerImpl(" + name + "FieldName, ");
632 jsc.append("null, null,");
633 jsc.addIndented(name + "GetMethod, " + name + "SetMethod, " + name + "Type);");
634 jsc.unindent();
635 jsc.add("");
636
637
638 jsc.add("} catch (SecurityException e1) {");
639 jsc.indent();
640 jsc.add("throw new RuntimeException(e1.getMessage());");
641 jsc.unindent();
642 jsc.add("} catch (MappingException e1) {");
643 jsc.indent();
644 jsc.add("throw new RuntimeException(e1.getMessage());");
645 jsc.unindent();
646 jsc.add("} catch (NoSuchMethodException e1) {");
647 jsc.indent();
648 jsc.add("throw new RuntimeException(e1.getMessage());");
649 jsc.unindent();
650 jsc.add("}");
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677 jsc.add("// Instantiate " + name + " field descriptor");
678 jsc.add(name + "FieldDescr = new FieldDescriptorImpl(");
679 jsc.append(name + "FieldName, " + name + "Type,");
680 jsc.append(name + "Handler, ");
681 jsc.append(Boolean.toString(fInfo.isTransient()) + ");");
682
683 jsc.add(name + "FieldDescr.addNature(FieldDescriptorJDONature.class.getName());");
684
685 jsc.add("FieldDescriptorJDONature " + name + "FieldJdoNature = new FieldDescriptorJDONature("
686 + name + "FieldDescr);");
687
688 jsc.add(name + "FieldJdoNature.setSQLName(new String[] { " + name + "SqlName });");
689 jsc.add(name + "FieldJdoNature.setSQLType(new int[] {SQLTypeInfos");
690 jsc.append(".javaType2sqlTypeNum(");
691
692
693 if ((type != null) && (type.length() > 0)) {
694 jsc.append(wrapperType + ".class) });");
695 }
696
697
698 jsc.add(name + "FieldJdoNature.setManyKey(new String[] { " + name + "SqlName });");
699 jsc.add(name + "FieldJdoNature.setDirtyCheck(" + Boolean.toString(oneNature.isDirty()) + ");");
700 jsc.add(name + "FieldJdoNature.setReadOnly(" + Boolean.toString(oneNature.isReadOnly()) + ");");
701
702 jsc.add("");
703
704
705 jsc.add(name + "FieldDescr.setContainingClassDescriptor(this);");
706 jsc.add(name + "FieldDescr.setClassDescriptor(new " + getLocalName(type)
707 + "JDODescriptor());");
708
709 boolean isPrimaryKey = false;
710 if (cNature.getPrimaryKeys() != null) {
711 isPrimaryKey = (cNature.getPrimaryKeys().contains(xmlNature.getNodeName()));
712 }
713
714
715 jsc.add(name + "FM.setIdentity(" + Boolean.toString(isPrimaryKey) + ");");
716 jsc.add(name + "FM.setDirect(false);");
717 jsc.add(name + "FM.setName(\"" + name + "\");");
718 jsc.add(name + "FM.setRequired(" + xmlNature.isRequired() + ");");
719 jsc.add(name + "FM.setSetMethod(\"set"
720 + toUpperCaseFirstLetter(name) + "\");");
721 jsc.add(name + "FM.setGetMethod(\"get"
722 + toUpperCaseFirstLetter(name) + "\");");
723
724
725 jsc.add("Sql " + name + "Sql = new Sql();");
726 jsc.add(name + "Sql.addName(\"" + sqlName + "\");");
727 jsc.add(name + "Sql.setManyKey(new String[] {\"" + sqlName + "\"});");
728 jsc.add(name + "FM.setSql(" + name + "Sql);");
729
730 if ((type != null) && (type.length() > 0)) {
731 jsc.add(name + "FM.setType(\"" + type + "\");");
732 }
733
734 jsc.add("choice.addFieldMapping(" + name + "FM);");
735 return jsc;
736 }
737
738
739
740
741
742
743
744
745
746 private JSourceCode createOneToManyFieldInfoPart(final FieldInfo fInfo, final JSourceCode jsc) {
747
748
749 JDOClassInfoNature cNature = new JDOClassInfoNature(fInfo.getDeclaringClassInfo());
750 JDOOneToManyNature manyNature = new JDOOneToManyNature(fInfo);
751 XMLInfoNature xmlNature = new XMLInfoNature(fInfo);
752
753
754 String name = xmlNature.getNodeName();
755 jsc.add("");
756 jsc.add("//" + name + " field");
757 jsc.add("String " + name + "FieldName = \"" + name + "\";");
758
759 String sqlName = manyNature.getForeignKeys().get(0).toString();
760 jsc.add("String " + name + "SqlName = \"" + sqlName + "\";");
761
762
763 jsc.add("FieldDescriptorImpl " + name + "FieldDescr;");
764 jsc.add("FieldMapping " + name + "FM = new FieldMapping();");
765
766
767 String type = null;
768 XSList schemaType;
769
770 schemaType = (XSList) xmlNature.getSchemaType();
771 JType javaType = schemaType.getContentType().getJType();
772 type = javaType.toString();
773
774 String wrapperType = null;
775 if (javaType instanceof JPrimitiveType) {
776 wrapperType = ((JPrimitiveType) javaType).getWrapperName();
777 } else {
778 wrapperType = type;
779 }
780
781
782 if ((type != null) && (type.length() > 0)) {
783 jsc.add("TypeInfo " + name
784 + "Type = new TypeInfo(" + type + ".class);");
785 }
786
787 jsc.add("// Set columns required (= not null)");
788 jsc.add(name + "Type.setRequired("
789 + Boolean.toString(xmlNature.isRequired()) + ");");
790
791 jsc.add("");
792
793 jsc.add("FieldHandler " + name + "Handler;");
794 jsc.add("try {");
795
796
797 jsc.indent();
798
799 String className = fInfo.getDeclaringClassInfo().getJClass().getLocalName();
800
801 if ((className != null) && (className.length() > 0)) {
802 jsc.add("Method " + name + "GetMethod = "
803 + className + ".class.getMethod(\"get"
804 + toUpperCaseFirstLetter(name) + "\", null);");
805 jsc.add("Method " + name + "SetMethod = "
806 + className + ".class.getMethod(\"set"
807 + toUpperCaseFirstLetter(name) + "\", new Class[]{");
808 }
809
810
811 if ((type != null) && (type.length() > 0)) {
812 jsc.addIndented(type + "[].class});");
813 }
814
815 jsc.add("");
816 jsc.add(name + "Handler = new FieldHandlerImpl(" + name + "FieldName, ");
817 jsc.append("null, null,");
818 jsc.addIndented(name + "GetMethod, " + name + "SetMethod, " + name + "Type);");
819 jsc.unindent();
820 jsc.add("");
821
822
823 jsc.add("} catch (SecurityException e1) {");
824 jsc.indent();
825 jsc.add("throw new RuntimeException(e1.getMessage());");
826 jsc.unindent();
827 jsc.add("} catch (MappingException e1) {");
828 jsc.indent();
829 jsc.add("throw new RuntimeException(e1.getMessage());");
830 jsc.unindent();
831 jsc.add("} catch (NoSuchMethodException e1) {");
832 jsc.indent();
833 jsc.add("throw new RuntimeException(e1.getMessage());");
834 jsc.unindent();
835 jsc.add("}");
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862 jsc.add("// Instantiate " + name + " field descriptor");
863 jsc.add(name + "FieldDescr = new FieldDescriptorImpl(");
864 jsc.append(name + "FieldName, " + name + "Type,");
865 jsc.append(name + "Handler, ");
866 jsc.append(Boolean.toString(fInfo.isTransient()) + ");");
867
868 jsc.add(name + "FieldDescr.addNature(FieldDescriptorJDONature.class.getName());");
869
870 jsc.add("FieldDescriptorJDONature " + name + "FieldJdoNature = new FieldDescriptorJDONature("
871 + name + "FieldDescr);");
872
873 jsc.add(name + "FieldJdoNature.setSQLName(null);");
874 jsc.add(name + "FieldJdoNature.setSQLType(new int[] {SQLTypeInfos");
875 jsc.append(".javaType2sqlTypeNum(");
876
877
878 if ((type != null) && (type.length() > 0)) {
879 jsc.append(wrapperType + ".class) });");
880 }
881
882 jsc.add(name + "FieldJdoNature.setManyKey(new String[] { " + name + "SqlName });");
883 jsc.add(name + "FieldJdoNature.setDirtyCheck(" + Boolean.toString(manyNature.isDirty()) + ");");
884 jsc.add(name + "FieldJdoNature.setReadOnly(" + Boolean.toString(manyNature.isReadOnly()) + ");");
885
886 jsc.add("");
887
888 jsc.add(name + "FieldDescr.setDirect(false);");
889 jsc.add(name + "FieldDescr.setName(\"" + name + "\");");
890 jsc.add(name + "FieldDescr.setRequired(" + xmlNature.isRequired() + ");");
891
892 jsc.add(name + "FieldDescr.setCollection(FieldMappingCollectionType.ARRAY);");
893
894 jsc.add("");
895
896
897 jsc.add(name + "FieldDescr.setContainingClassDescriptor(this);");
898 jsc.add(name + "FieldDescr.setClassDescriptor(new " + getLocalName(type)
899 + "JDODescriptor());");
900 jsc.add(name + "FieldDescr.setMultivalued(true);");
901
902 boolean isPrimaryKey = false;
903 if (cNature.getPrimaryKeys() != null) {
904 isPrimaryKey = (cNature.getPrimaryKeys().contains(xmlNature.getNodeName()));
905 }
906
907
908 jsc.add(name + "FM.setIdentity(" + Boolean.toString(isPrimaryKey) + ");");
909 jsc.add(name + "FM.setDirect(false);");
910 jsc.add(name + "FM.setName(\"" + name + "\");");
911 jsc.add(name + "FM.setRequired(" + xmlNature.isRequired() + ");");
912
913 jsc.add(name + "FM.setCollection(FieldMappingCollectionType.ARRAY);");
914
915
916 jsc.add("Sql " + name + "Sql = new Sql();");
917 jsc.add(name + "Sql.addName(\"" + sqlName + "\");");
918 jsc.add(name + "Sql.setManyKey(new String[] {\"" + sqlName + "\"});");
919 jsc.add(name + "FM.setSql(" + name + "Sql);");
920
921 if ((type != null) && (type.length() > 0)) {
922 jsc.add(name + "FM.setType(\"" + type + "\");");
923 }
924
925 jsc.add("choice.addFieldMapping(" + name + "FM);");
926
927 return jsc;
928 }
929
930
931
932
933
934
935
936 private String getLocalName(final String name) {
937
938 return JNaming.getLocalNameFromClassName(name);
939 }
940 }