KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > torque > TorqueTableGenerator


1 package org.apache.ojb.broker.metadata.torque;
2
3 /* Copyright 2002-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 org.apache.ojb.broker.metadata.DescriptorRepository;
19 import org.apache.ojb.broker.metadata.FieldDescriptor;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 public class TorqueTableGenerator {
25
26     private TorqueFieldGenerator fieldGenerator;
27     private TorqueForeignKeyGenerator foreignKeyPreprocessor;
28     private TorqueIndexGenerator indexGenerator;
29
30     public TorqueTableGenerator(DescriptorRepository repository, boolean ignoreAutoIncrement) {
31         this.fieldGenerator = new TorqueFieldGenerator(ignoreAutoIncrement);
32         this.foreignKeyPreprocessor = new TorqueForeignKeyGenerator(repository);
33         this.foreignKeyPreprocessor.buildConstraintsMap();
34         this.indexGenerator = new TorqueIndexGenerator();
35     }
36
37     public void generateMappingTables(StringBuffer JavaDoc buffer, String JavaDoc indexTablespaceName) {
38         Iterator JavaDoc mappingTables = this.foreignKeyPreprocessor.getMappingTables().values().iterator();
39         while (mappingTables.hasNext()) {
40             TableDescriptor mappingTable = (TableDescriptor) mappingTables.next();
41             FieldDescriptor descriptors[] = (FieldDescriptor[]) (mappingTable.getColumns().toArray(new FieldDescriptor[0]));
42             if (descriptors != null && descriptors.length > 0) {
43                 generateTableHeader(buffer, mappingTable, indexTablespaceName);
44                 this.fieldGenerator.generateMappingFieldDescriptors(descriptors, buffer);
45                 generateForeignKeys(mappingTable, buffer);
46                 buffer.append(" </table>\n");
47             }
48         }
49     }
50
51     public void generateStandardTable(TableDescriptor tableDescriptor, StringBuffer JavaDoc buffer, String JavaDoc indexTablespaceName) {
52         generateTableHeader(buffer, tableDescriptor, indexTablespaceName);
53
54         this.fieldGenerator.generateFieldDescriptors((FieldDescriptor[]) tableDescriptor.getColumns().toArray(new FieldDescriptor[0]), buffer);
55         this.indexGenerator.generateIndices(tableDescriptor.getIndices(), buffer);
56         generateForeignKeys(tableDescriptor, buffer);
57
58         buffer.append(" </table>\n\n");
59     }
60
61     private void generateTableHeader(StringBuffer JavaDoc buffer, TableDescriptor tableDescriptor, String JavaDoc indexTablespaceName) {
62         buffer.append(" <table name=\"");
63         buffer.append(tableDescriptor.getName());
64         buffer.append("\" indexTablespace=\"");
65         buffer.append(indexTablespaceName);
66         buffer.append("\">\n");
67     }
68
69     private void generateForeignKeys(TableDescriptor tableDescriptor, StringBuffer JavaDoc buffer) {
70         Vector JavaDoc foreignKeys = this.foreignKeyPreprocessor.getForeignKeysForTable(tableDescriptor.getName());
71         if (foreignKeys != null) {
72             Iterator JavaDoc foreignKeyIterator = foreignKeys.iterator();
73             while (foreignKeyIterator.hasNext()) {
74                 buffer.append(foreignKeyIterator.next());
75             }
76         }
77     }
78 }
79
Popular Tags