001: package com.quantum.flatfiles.wizard;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.sql.SQLException;
006:
007: import com.quantum.model.Bookmark;
008: import com.quantum.model.Column;
009: import com.quantum.model.DatabaseObject;
010: import com.quantum.model.Entity;
011: import com.quantum.model.ForeignKey;
012: import com.quantum.model.Schema;
013: import com.quantum.util.connection.NotConnectedException;
014: import com.quantum.util.sql.TypesHelper;
015:
016: /**
017: * @author BC Holmes
018: */
019: public class TorqueConverter {
020:
021: private static final String LINE_SEPARATOR = System
022: .getProperty("line.separator");
023:
024: public void convert(Writer writer, Bookmark bookmark,
025: Schema schema, String databaseName, String defaultIdMethod)
026: throws IOException, NotConnectedException, SQLException {
027: DatabaseObject[] tables = bookmark.getObjectsForSchema(schema,
028: Entity.TABLE_TYPE);
029:
030: writer.write("<?xml version=\"1.0\" ?>");
031: writer.write(LINE_SEPARATOR);
032: writer.write("<!DOCTYPE database SYSTEM ");
033: writer
034: .write("\"http://db.apache.org/torque/dtd/database_3_1.dtd\">");
035: writer.write(LINE_SEPARATOR);
036:
037: writer.write("<database name=\"");
038: writer.write(databaseName);
039: writer.write("\" defaultIdMethod=\"");
040: writer.write(defaultIdMethod);
041: writer.write("\">");
042:
043: for (int i = 0, length = (tables == null) ? 0 : tables.length; i < length; i++) {
044: if (Entity.TABLE_TYPE.equals(tables[i].getType())) {
045: convert(writer, (Entity) tables[i]);
046: }
047: }
048:
049: writer.write(LINE_SEPARATOR);
050: writer.write("</database>");
051: }
052:
053: private void convert(Writer writer, Entity entity)
054: throws IOException, NotConnectedException, SQLException {
055: writer.write(LINE_SEPARATOR);
056: writer.write("\t<table name=\"");
057: writer.write(entity.getName());
058: writer.write("\" >");
059: Column[] columns = entity.getColumns();
060: for (int i = 0, length = (columns == null) ? 0 : columns.length; i < length; i++) {
061: convert(writer, columns[i]);
062: }
063: ForeignKey[] foreignKeys = entity.getImportedKeys();
064: for (int i = 0, length = (foreignKeys == null) ? 0
065: : foreignKeys.length; i < length; i++) {
066: convert(writer, foreignKeys[i]);
067: }
068: writer.write(LINE_SEPARATOR);
069: writer.write("\t</table>");
070: }
071:
072: public void convert(Writer writer, ForeignKey foreignKey)
073: throws IOException, NotConnectedException, SQLException {
074:
075: writer.write(LINE_SEPARATOR);
076: writer.write("\t\t<foreign-key foreignTable=\"");
077: writer.write(foreignKey.getLocalEntityName());
078: writer.write("\">");
079: for (int i = 0, length = foreignKey.getNumberOfColumns(); i < length; i++) {
080: writer.write(LINE_SEPARATOR);
081: writer.write("\t\t\t<reference local=\"");
082: writer.write(foreignKey.getForeignColumnName(i));
083: writer.write("\" foreign=\"");
084: writer.write(foreignKey.getLocalColumnName(i));
085: writer.write("\" />");
086: }
087: writer.write(LINE_SEPARATOR);
088: writer.write("\t\t</foreign-key>");
089: }
090:
091: public void convert(Writer writer, Column column)
092: throws IOException, NotConnectedException, SQLException {
093: writer.write(LINE_SEPARATOR);
094: writer.write("\t\t<column name=\"");
095: writer.write(column.getName());
096: if (column.isPrimaryKey()) {
097: writer.write("\" primaryKey=\"true");
098: }
099: writer.write("\" required=\"");
100: writer.write(column.isNullable() ? "false" : "true");
101: writer.write("\" type=\"");
102: writer.write(getTypeString(column.getType()));
103: if (hasSize(column.getType())) {
104: writer.write("\" size=\"");
105: writer.write(getSizeString(column));
106: }
107: writer.write("\" />");
108:
109: }
110:
111: /**
112: * @param column
113: * @return
114: */
115: private String getSizeString(Column column) {
116: if (hasSize(column.getType())) {
117: return ""
118: + column.getSize()
119: + (column.getNumberOfFractionalDigits() != 0 ? ("," + column
120: .getNumberOfFractionalDigits())
121: : "");
122: } else {
123: return "";
124: }
125: }
126:
127: /**
128: * @param type
129: * @return
130: */
131: private boolean hasSize(int type) {
132: switch (type) {
133: case TypesHelper.CHAR:
134: case TypesHelper.DECIMAL:
135: case TypesHelper.LONGVARCHAR:
136: case TypesHelper.VARCHAR:
137: return true;
138:
139: default:
140: return false;
141: }
142: }
143:
144: /**
145: * @param type
146: * @return
147: */
148: private String getTypeString(int type) {
149: String typeAsString = TypesHelper.getTypeName(type);
150: // TODO: should this have some kind of warning associated with it?
151: return typeAsString == null ? TypesHelper
152: .getTypeName(TypesHelper.INTEGER) : typeAsString;
153: }
154: }
|