1 package org.apache.ojb.broker.metadata.torque; 2 3 17 18 import org.apache.ojb.broker.metadata.DescriptorRepository; 19 import org.apache.ojb.broker.metadata.FieldDescriptor; 20 21 import java.util.Iterator ; 22 import java.util.Vector ; 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 buffer, String indexTablespaceName) { 38 Iterator 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 buffer, String 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 buffer, TableDescriptor tableDescriptor, String 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 buffer) { 70 Vector foreignKeys = this.foreignKeyPreprocessor.getForeignKeysForTable(tableDescriptor.getName()); 71 if (foreignKeys != null) { 72 Iterator foreignKeyIterator = foreignKeys.iterator(); 73 while (foreignKeyIterator.hasNext()) { 74 buffer.append(foreignKeyIterator.next()); 75 } 76 } 77 } 78 } 79 | Popular Tags |