KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ojb > model > TorqueModelDef


1 package xdoclet.modules.ojb.model;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.*;
19
20 import xdoclet.modules.ojb.CommaListIterator;
21
22 /**
23  * Represents the model used for generating the torque database schema.
24  */

25 public class TorqueModelDef extends DefBase
26 {
27     /** The tables keyed by their names*/
28     private SortedMap _tableDefs = new TreeMap();
29
30     /**
31      * Generates a new torque database model from the given ojb model.
32      *
33      * @param dbName The name of the database
34      * @param ojbModel The ojb model
35      * @return The torque model
36      */

37     public TorqueModelDef(String JavaDoc dbName, ModelDef ojbModel)
38     {
39         super(dbName);
40
41         ClassDescriptorDef classDef;
42         TableDef tableDef;
43         FieldDescriptorDef fieldDef;
44         ColumnDef columnDef;
45         String JavaDoc name;
46
47         for (Iterator classIt = ojbModel.getClasses(); classIt.hasNext();)
48         {
49             classDef = (ClassDescriptorDef)classIt.next();
50             if (classDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_OJB_PERSISTENT, false) &&
51                 classDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO, true))
52             {
53                 addTableFor(classDef);
54             }
55         }
56     }
57
58     // The conversion algorithm
59
// Note that the complete algorithm is here rather than splitting in onto the
60
// various torque model elements because maintaining it is easier this way
61

62     /**
63      * Adds a table for the given class descriptor (if necessary).
64      *
65      * @param classDef The class descriptor
66      */

67     private void addTableFor(ClassDescriptorDef classDef)
68     {
69         String JavaDoc name = classDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE);
70         TableDef tableDef = getTable(name);
71         FieldDescriptorDef fieldDef;
72         ReferenceDescriptorDef refDef;
73         CollectionDescriptorDef collDef;
74         ColumnDef columnDef;
75         IndexDef indexDef;
76
77         if (tableDef == null)
78         {
79             tableDef = new TableDef(name);
80             addTable(tableDef);
81         }
82         if (classDef.hasProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION))
83         {
84             tableDef.setProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION,
85                                  classDef.getProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION));
86         }
87         if (classDef.hasProperty(PropertyHelper.OJB_PROPERTY_TABLE_DOCUMENTATION))
88         {
89             tableDef.setProperty(PropertyHelper.OJB_PROPERTY_TABLE_DOCUMENTATION,
90                                  classDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE_DOCUMENTATION));
91         }
92         for (Iterator fieldIt = classDef.getFields(); fieldIt.hasNext();)
93         {
94             fieldDef = (FieldDescriptorDef)fieldIt.next();
95             if (fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false) ||
96                 fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_VIRTUAL_FIELD, false))
97             {
98                 continue;
99             }
100             columnDef = addColumnFor(fieldDef, tableDef);
101             if (fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_INDEXED, false))
102             {
103                 // add the field to the default index
104
indexDef = tableDef.getIndex(null);
105                 if (indexDef == null)
106                 {
107                     indexDef = new IndexDef(null, false);
108                     tableDef.addIndex(indexDef);
109                 }
110                 indexDef.addColumn(columnDef.getName());
111             }
112         }
113         for (Iterator refIt = classDef.getReferences(); refIt.hasNext();)
114         {
115             refDef = (ReferenceDescriptorDef)refIt.next();
116             if (!refDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false))
117             {
118                 addForeignkeys(refDef, tableDef);
119             }
120         }
121         for (Iterator collIt = classDef.getCollections(); collIt.hasNext();)
122         {
123             collDef = (CollectionDescriptorDef)collIt.next();
124             if (!collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false))
125             {
126                 if (collDef.hasProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE))
127                 {
128                     addIndirectionTable(collDef);
129                 }
130                 else
131                 {
132                     addForeignkeys(collDef, tableDef);
133                 }
134             }
135         }
136         for (Iterator indexIt = classDef.getIndexDescriptors(); indexIt.hasNext();)
137         {
138             addIndex((IndexDescriptorDef)indexIt.next(), tableDef);
139         }
140     }
141
142     /**
143      * Generates a column for the given field and adds it to the table.
144      *
145      * @param fieldDef The field
146      * @param tableDef The table
147      * @return The column def
148      */

149     private ColumnDef addColumnFor(FieldDescriptorDef fieldDef, TableDef tableDef)
150     {
151         String JavaDoc name = fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN);
152         ColumnDef columnDef = tableDef.getColumn(name);
153
154         if (columnDef == null)
155         {
156             columnDef = new ColumnDef(name);
157             tableDef.addColumn(columnDef);
158         }
159         if (!fieldDef.isNested())
160         {
161             columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_JAVANAME, fieldDef.getName());
162         }
163         columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_TYPE, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_JDBC_TYPE));
164         columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_ID, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_ID));
165         if (fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_PRIMARYKEY, false))
166         {
167             columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_PRIMARYKEY, "true");
168             columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_REQUIRED, "true");
169         }
170         else if (!fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_NULLABLE, true))
171         {
172             columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_REQUIRED, "true");
173         }
174         if ("database".equals(fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_AUTOINCREMENT)))
175         {
176             columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_AUTOINCREMENT, "true");
177         }
178         columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_SIZE, fieldDef.getSizeConstraint());
179         if (fieldDef.hasProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION))
180         {
181             columnDef.setProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION,
182                                   fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION));
183         }
184         if (fieldDef.hasProperty(PropertyHelper.OJB_PROPERTY_COLUMN_DOCUMENTATION))
185         {
186             columnDef.setProperty(PropertyHelper.OJB_PROPERTY_COLUMN_DOCUMENTATION,
187                                   fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN_DOCUMENTATION));
188         }
189         return columnDef;
190     }
191
192     /**
193      * Adds foreignkey(s) for the reference to the corresponding table(s).
194      *
195      * @param refDef The reference
196      * @param tableDef The table of the class owning the reference
197      */

198     private void addForeignkeys(ReferenceDescriptorDef refDef, TableDef tableDef)
199     {
200         if (!refDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_DATABASE_FOREIGNKEY, true))
201         {
202             // we shall not generate a database foreignkey
203
return;
204         }
205
206         // a foreignkey is added to the table schema if
207
// the referenced table exists (i.e. the referenced type has an associated table)
208
// then the foreignkey consists of:
209
// remote table = table of referenced type
210
// local fields = foreignkey fields of the reference
211
// remote fields = primarykeys of the referenced type
212
ClassDescriptorDef ownerClassDef = (ClassDescriptorDef)refDef.getOwner();
213         String JavaDoc targetClassName = refDef.getProperty(PropertyHelper.OJB_PROPERTY_CLASS_REF);
214         ClassDescriptorDef referencedClassDef = ((ModelDef)ownerClassDef.getOwner()).getClass(targetClassName);
215
216         // we can add a foreignkey only if the target type and all its subtypes either
217
// map to the same table or do not map to a table at all
218
String JavaDoc tableName = getHierarchyTable(referencedClassDef);
219
220         if (tableName == null)
221         {
222             return;
223         }
224
225         try
226         {
227             String JavaDoc name = refDef.getName();
228             ArrayList localFields = ownerClassDef.getFields(refDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY));
229             ArrayList remoteFields = referencedClassDef.getPrimaryKeys();
230
231             tableDef.addForeignkey(name, tableName, getColumns(localFields), getColumns(remoteFields));
232         }
233         catch (NoSuchFieldException JavaDoc ex)
234         {
235             // won't happen if we already checked the constraints
236
}
237     }
238
239     /**
240      * Adds foreignkey(s) for the collection to the corresponding table(s).
241      *
242      * @param collDef The collection
243      * @param tableDef The table
244      */

245     private void addForeignkeys(CollectionDescriptorDef collDef, TableDef tableDef)
246     {
247         if (!collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_DATABASE_FOREIGNKEY, true))
248         {
249             // we shall not generate a database foreignkey
250
return;
251         }
252
253         // a foreignkey is added to the table schema if for both ends of the collection
254
// a table exists
255
// then the foreignkey consists of:
256
// remote table = table of collection owner
257
// local fields = foreignkey fields in the element type
258
// remote fields = primarykeys of the collection owner type
259
ClassDescriptorDef ownerClassDef = (ClassDescriptorDef)collDef.getOwner();
260         String JavaDoc elementClassName = collDef.getProperty(PropertyHelper.OJB_PROPERTY_ELEMENT_CLASS_REF);
261         ClassDescriptorDef elementClassDef = ((ModelDef)ownerClassDef.getOwner()).getClass(elementClassName);
262
263         // we can only generate foreignkeys if the collection itself is not shared by
264
// several classes in the hierarchy
265
for (Iterator it = ownerClassDef.getAllBaseTypes(); it.hasNext();)
266         {
267             if (containsCollectionAndMapsToDifferentTable(collDef, tableDef, (ClassDescriptorDef)it.next()))
268             {
269                 return;
270             }
271         }
272         for (Iterator it = ownerClassDef.getAllExtentClasses(); it.hasNext();)
273         {
274             if (containsCollectionAndMapsToDifferentTable(collDef, tableDef, (ClassDescriptorDef)it.next()))
275             {
276                 return;
277             }
278         }
279
280         // We add a foreignkey to all classes in the subtype hierarchy of the element type
281
// that map to a table (we're silently assuming that they contain the fk fields)
282
ArrayList candidates = new ArrayList();
283
284         if (elementClassDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO, true))
285         {
286             candidates.add(elementClassDef);
287         }
288         for (Iterator it = elementClassDef.getAllExtentClasses(); it.hasNext();)
289         {
290             ClassDescriptorDef curSubTypeDef = (ClassDescriptorDef)it.next();
291
292             if (curSubTypeDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO, true))
293             {
294                 candidates.add(curSubTypeDef);
295             }
296         }
297
298         String JavaDoc name = collDef.getName();
299         ArrayList remoteFields = ownerClassDef.getPrimaryKeys();
300         HashMap processedTables = new HashMap();
301
302         for (Iterator it = candidates.iterator(); it.hasNext();)
303         {
304             elementClassDef = (ClassDescriptorDef)it.next();
305             try
306             {
307                 // for the element class and its subclasses
308
String JavaDoc elementTableName = elementClassDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE);
309
310                 // ensure that tables are only processed once
311
if (!processedTables.containsKey(elementTableName))
312                 {
313                     ArrayList localFields = elementClassDef.getFields(collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY));
314                     TableDef elementTableDef = getTable(elementTableName);
315     
316                     if (elementTableDef == null)
317                     {
318                         elementTableDef = new TableDef(elementTableName);
319                         addTable(elementTableDef);
320                     }
321                     elementTableDef.addForeignkey(name, tableDef.getName(), getColumns(localFields), getColumns(remoteFields));
322                     processedTables.put(elementTableName, null);
323                 }
324             }
325             catch (NoSuchFieldException JavaDoc ex)
326             {
327                 // Shouldn't happen, but even if, then we're ignoring it and simply don't add the fk
328
}
329         }
330     }
331
332     /**
333      * Extracts the list of columns from the given field list.
334      *
335      * @param fields The fields
336      * @return The corresponding columns
337      */

338     private List getColumns(List fields)
339     {
340         ArrayList columns = new ArrayList();
341
342         for (Iterator it = fields.iterator(); it.hasNext();)
343         {
344             FieldDescriptorDef fieldDef = (FieldDescriptorDef)it.next();
345
346             columns.add(fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN));
347         }
348         return columns;
349     }
350     
351     /**
352      * Checks whether the given class maps to a different table but also has the given collection.
353      *
354      * @param origCollDef The original collection to search for
355      * @param origTableDef The original table
356      * @param classDef The class descriptor to test
357      * @return <code>true</code> if the class maps to a different table and has the collection
358      */

359     private boolean containsCollectionAndMapsToDifferentTable(CollectionDescriptorDef origCollDef, TableDef origTableDef, ClassDescriptorDef classDef)
360     {
361         if (classDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO, true) &&
362             !origTableDef.getName().equals(classDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE)))
363         {
364             CollectionDescriptorDef curCollDef = classDef.getCollection(origCollDef.getName());
365
366             if ((curCollDef != null) &&
367                 !curCollDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false))
368             {
369                 return true;
370             }
371         }
372         return false;
373     }
374
375     /**
376      * Tries to return the single target table to which the given foreign key columns map in
377      * all m:n collections that target this indirection table.
378      *
379      * @param targetClassDef The original target class
380      * @param indirectionTable The indirection table
381      * @param foreignKeys The foreign keys columns in the indirection table pointing back to the
382      * class' table
383      * @return The table name or <code>null</code> if there is not exactly one table
384      */

385     private String JavaDoc getTargetTable(ClassDescriptorDef targetClassDef, String JavaDoc indirectionTable, String JavaDoc foreignKeys)
386     {
387         ModelDef modelDef = (ModelDef)targetClassDef.getOwner();
388         String JavaDoc tableName = null;
389
390         for (Iterator classIt = modelDef.getClasses(); classIt.hasNext();)
391         {
392             ClassDescriptorDef curClassDef = (ClassDescriptorDef)classIt.next();
393
394             if (!curClassDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO, true))
395             {
396                 continue;
397             }
398             for (Iterator collIt = curClassDef.getCollections(); collIt.hasNext();)
399             {
400                 CollectionDescriptorDef curCollDef = (CollectionDescriptorDef)collIt.next();
401
402                 if (!indirectionTable.equals(curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE)) ||
403                     !CommaListIterator.sameLists(foreignKeys, curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY)))
404                 {
405                     continue;
406                 }
407                 // ok, collection fits
408
if (tableName != null)
409                 {
410                     if (!tableName.equals(curClassDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE)))
411                     {
412                         // maps to a different table
413
return null;
414                     }
415                 }
416                 else
417                 {
418                     tableName = curClassDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE);
419                 }
420             }
421         }
422         if (tableName == null)
423         {
424             // no fitting collection found -> indirection table with only one collection
425
// we have to check whether the hierarchy of the target class maps to one table only
426
return getHierarchyTable(targetClassDef);
427         }
428         else
429         {
430             return tableName;
431         }
432     }
433
434     /**
435      * Tries to return the single table to which all classes in the hierarchy with the given
436      * class as the root map.
437      *
438      * @param classDef The root class of the hierarchy
439      * @return The table name or <code>null</code> if the classes map to more than one table
440      * or no class in the hierarchy maps to a table
441      */

442     private String JavaDoc getHierarchyTable(ClassDescriptorDef classDef)
443     {
444         ArrayList queue = new ArrayList();
445         String JavaDoc tableName = null;
446
447         queue.add(classDef);
448
449         while (!queue.isEmpty())
450         {
451             ClassDescriptorDef curClassDef = (ClassDescriptorDef)queue.get(0);
452
453             queue.remove(0);
454
455             if (curClassDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO, true))
456             {
457                 if (tableName != null)
458                 {
459                     if (!tableName.equals(curClassDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE)))
460                     {
461                         return null;
462                     }
463                 }
464                 else
465                 {
466                     tableName = curClassDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE);
467                 }
468             }
469             for (Iterator it = curClassDef.getExtentClasses(); it.hasNext();)
470             {
471                 curClassDef = (ClassDescriptorDef)it.next();
472
473                 if (curClassDef.getReference("super") == null)
474                 {
475                     queue.add(curClassDef);
476                 }
477             }
478         }
479         return tableName;
480     }
481
482     /**
483      * Adds an index to the table for the given index descriptor.
484      *
485      * @param indexDescDef The index descriptor
486      * @param tableDef The table
487      */

488     private void addIndex(IndexDescriptorDef indexDescDef, TableDef tableDef)
489     {
490         IndexDef indexDef = tableDef.getIndex(indexDescDef.getName());
491
492         if (indexDef == null)
493         {
494             indexDef = new IndexDef(indexDescDef.getName(),
495                                     indexDescDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_UNIQUE, false));
496             tableDef.addIndex(indexDef);
497         }
498
499         try
500         {
501             String JavaDoc fieldNames = indexDescDef.getProperty(PropertyHelper.OJB_PROPERTY_FIELDS);
502             ArrayList fields = ((ClassDescriptorDef)indexDescDef.getOwner()).getFields(fieldNames);
503             FieldDescriptorDef fieldDef;
504
505             for (Iterator it = fields.iterator(); it.hasNext();)
506             {
507                 fieldDef = (FieldDescriptorDef)it.next();
508                 indexDef.addColumn(fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN));
509             }
510         }
511         catch (NoSuchFieldException JavaDoc ex)
512         {
513             // won't happen if we already checked the constraints
514
}
515     }
516
517     /**
518      * Adds the indirection table for the given collection descriptor.
519      *
520      * @param collDef The collection descriptor
521      */

522     private void addIndirectionTable(CollectionDescriptorDef collDef)
523     {
524         String JavaDoc tableName = collDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE);
525         TableDef tableDef = getTable(tableName);
526
527         if (tableDef == null)
528         {
529             tableDef = new TableDef(tableName);
530             addTable(tableDef);
531         }
532         if (collDef.hasProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE_DOCUMENTATION))
533         {
534             tableDef.setProperty(PropertyHelper.OJB_PROPERTY_TABLE_DOCUMENTATION,
535                                  collDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE_DOCUMENTATION));
536         }
537
538         // we add columns for every primarykey in this and the element type
539
// collection.foreignkeys <-> ownerclass.primarykeys
540
// collection.remote-foreignkeys <-> elementclass.primarykeys
541
// we also add foreignkeys to the table
542
// name is empty (default foreignkey)
543
// remote table = table of ownerclass/elementclass
544
// local columns = columns in indirection table
545
// remote columns = columns of corresponding primarykeys in ownerclass/elementclass
546
ClassDescriptorDef ownerClassDef = (ClassDescriptorDef)collDef.getOwner();
547         ModelDef modelDef = (ModelDef)ownerClassDef.getOwner();
548         String JavaDoc elementClassName = collDef.getProperty(PropertyHelper.OJB_PROPERTY_ELEMENT_CLASS_REF);
549         ClassDescriptorDef elementClassDef = modelDef.getClass(elementClassName);
550         ArrayList localPrimFields = ownerClassDef.getPrimaryKeys();
551         ArrayList remotePrimFields = elementClassDef.getPrimaryKeys();
552         String JavaDoc localKeyList = collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY);
553         String JavaDoc remoteKeyList = collDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY);
554         String JavaDoc ownerTable = getTargetTable(ownerClassDef, tableName, localKeyList);
555         String JavaDoc elementTable = getTargetTable(elementClassDef, tableName, remoteKeyList);
556         CommaListIterator localKeys = new CommaListIterator(localKeyList);
557         CommaListIterator localKeyDocs = new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY_DOCUMENTATION));
558         CommaListIterator remoteKeys = new CommaListIterator(remoteKeyList);
559         CommaListIterator remoteKeyDocs = new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY_DOCUMENTATION));
560         ArrayList localColumns = new ArrayList();
561         ArrayList remoteColumns = new ArrayList();
562         boolean asPrimarykeys = collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE_PRIMARYKEYS, false);
563         FieldDescriptorDef fieldDef;
564         ColumnDef columnDef;
565         String JavaDoc relationName;
566         String JavaDoc name;
567         int idx;
568
569         for (idx = 0; localKeys.hasNext(); idx++)
570         {
571             fieldDef = (FieldDescriptorDef)localPrimFields.get(idx);
572             name = localKeys.getNext();
573             columnDef = tableDef.getColumn(name);
574             if (columnDef == null)
575             {
576                 columnDef = new ColumnDef(name);
577                 tableDef.addColumn(columnDef);
578             }
579             columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_TYPE, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_JDBC_TYPE));
580             columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_SIZE, fieldDef.getSizeConstraint());
581             if (asPrimarykeys)
582             {
583                 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_PRIMARYKEY, "true");
584             }
585             if (localKeyDocs.hasNext())
586             {
587                 columnDef.setProperty(PropertyHelper.OJB_PROPERTY_COLUMN_DOCUMENTATION, localKeyDocs.getNext());
588             }
589             localColumns.add(name);
590             remoteColumns.add(fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN));
591         }
592         if (collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_DATABASE_FOREIGNKEY, true))
593         {
594             relationName = collDef.getProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME);
595             if ((relationName != null) && (ownerTable != null))
596             {
597                 tableDef.addForeignkey(relationName, ownerTable, localColumns, remoteColumns);
598             }
599         }
600         localColumns.clear();
601         remoteColumns.clear();
602
603         for (idx = 0; remoteKeys.hasNext(); idx++)
604         {
605             fieldDef = (FieldDescriptorDef)remotePrimFields.get(idx);
606             name = remoteKeys.getNext();
607
608             columnDef = tableDef.getColumn(name);
609             if (columnDef == null)
610             {
611                 columnDef = new ColumnDef(name);
612                 tableDef.addColumn(columnDef);
613             }
614             columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_TYPE, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_JDBC_TYPE));
615             columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_SIZE, fieldDef.getSizeConstraint());
616             if (asPrimarykeys)
617             {
618                 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_PRIMARYKEY, "true");
619             }
620             if (remoteKeyDocs.hasNext())
621             {
622                 columnDef.setProperty(PropertyHelper.OJB_PROPERTY_COLUMN_DOCUMENTATION, remoteKeyDocs.getNext());
623             }
624             localColumns.add(name);
625             remoteColumns.add(fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN));
626         }
627
628         CollectionDescriptorDef elementCollDef = collDef.getRemoteCollection();
629
630         if (((elementCollDef != null) && elementCollDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_DATABASE_FOREIGNKEY, true)) ||
631             ((elementCollDef == null) && collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_DATABASE_FOREIGNKEY, true)))
632         {
633             relationName = collDef.getProperty(PropertyHelper.TORQUE_PROPERTY_INV_RELATION_NAME);
634             if ((relationName != null) && (elementTable != null))
635             {
636                 tableDef.addForeignkey(relationName, elementTable, localColumns, remoteColumns);
637             }
638         }
639     }
640
641     // Access methods
642

643     /**
644      * Returns an iterator of the tables.
645      *
646      * @return The tables
647      */

648     public Iterator getTables()
649     {
650         return _tableDefs.values().iterator();
651     }
652     
653     /**
654      * Returns the table of the given name if it exists.
655      *
656      * @param name The table name
657      * @return The table def or <code>null</code> if there is no such table
658      */

659     public TableDef getTable(String JavaDoc name)
660     {
661         return (TableDef)_tableDefs.get(name);
662     }
663
664     /**
665      * Adds a table to this model.
666      *
667      * @param table The table
668      */

669     private void addTable(TableDef table)
670     {
671         table.setOwner(this);
672         _tableDefs.put(table.getName(), table);
673     }
674 }
675
Popular Tags