0001: /*
0002: * Workbench2Designer.java
0003: *
0004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
0005: *
0006: * Copyright 2002-2008, Thomas Kellerer
0007: * No part of this code maybe reused without the permission of the author
0008: *
0009: * To contact the author please send an email to: support@sql-workbench.net
0010: *
0011: */
0012: package workbench.db.report;
0013:
0014: /**
0015: * @author Tobias Still
0016: */
0017: import java.io.BufferedInputStream;
0018: import java.io.File;
0019: import java.io.FileInputStream;
0020: import java.io.IOException;
0021: import java.io.InputStream;
0022: import java.io.Reader;
0023: import java.util.Iterator;
0024: import java.util.LinkedList;
0025: import java.util.Map;
0026: import java.util.Set;
0027: import java.util.TreeMap;
0028: import java.util.TreeSet;
0029:
0030: import javax.xml.parsers.DocumentBuilder;
0031: import javax.xml.parsers.DocumentBuilderFactory;
0032: import javax.xml.parsers.ParserConfigurationException;
0033: import javax.xml.transform.Transformer;
0034: import javax.xml.transform.TransformerConfigurationException;
0035: import javax.xml.transform.TransformerException;
0036: import javax.xml.transform.TransformerFactory;
0037: import javax.xml.transform.dom.DOMSource;
0038: import javax.xml.transform.stream.StreamResult;
0039:
0040: import org.w3c.dom.DOMException;
0041: import org.w3c.dom.Document;
0042: import org.w3c.dom.Element;
0043: import org.w3c.dom.NamedNodeMap;
0044: import org.w3c.dom.Node;
0045: import org.w3c.dom.NodeList;
0046: import org.xml.sax.InputSource;
0047: import org.xml.sax.SAXException;
0048: import workbench.log.LogMgr;
0049:
0050: public class Workbench2Designer {
0051: // Global value so it can be ref'd by the tree-adapter
0052: private static Document source;
0053: private static Document destination;
0054:
0055: //object model
0056:
0057: private static TreeMap tables = new TreeMap();
0058: private static TreeMap relations = new TreeMap();
0059: private static TreeMap dataTypes = new TreeMap();
0060: private static GlobalSettings globalSettings;
0061: private static final TreeMap globalSQLDataTypes = Workbench2Designer
0062: .initGlobalSQLDataTypes();
0063:
0064: private static TreeMap initGlobalSQLDataTypes() {
0065: TreeMap ret = new TreeMap();
0066: ret.put("INTEGER", new SQLDataType("INTEGER", 0, "", "INTEGER",
0067: new String[] { "length" }));
0068: ret.put("FLOAT", new SQLDataType("FLOAT", 1, "", "FLOAT",
0069: new String[] { "precision" }));
0070: ret.put("#FLOAT", new SQLDataType("#FLOAT", 0, "", "FLOAT",
0071: new String[] { "length", "decimals" }));
0072: ret.put("DOUBLE", new SQLDataType("DOUBLE", 0, "", "DOUBLE",
0073: new String[] { "length", "decimals" }));
0074: ret.put("REAL", new SQLDataType("REAL", 0, "", "REAL",
0075: new String[] { "length", "decimals" }));
0076: ret.put("DECIMAL", new SQLDataType("DECIMAL", 0, "", "DECIMAL",
0077: new String[] { "length", "decimals" }));
0078: ret.put("NUMBER", new SQLDataType("NUMBER", 1, "NUMBER",
0079: "NUMERIC", new String[] { "length", "decimals" }));
0080: ret.put("DATE", new SQLDataType("DATE", 0, "DATE", "DATE"));
0081: ret.put("DATETIME", new SQLDataType("DATETIME", 0, "DATE",
0082: "DATETIME"));
0083: ret.put("TIMESTAMP", new SQLDataType("TIMESTAMP", 0, "DATE",
0084: "TIMESTAMP"));
0085: ret.put("VARCHAR2", new SQLDataType("VARCHAR2", 1, "VARCHAR2",
0086: "VARCHAR", new String[] { "length" }));
0087: ret.put("VARCHAR", new SQLDataType("VARCHAR", 0, "", "VARCHAR",
0088: new String[] { "length" }));
0089: ret
0090: .put("BOOL", new SQLDataType("BOOL", 0, "NUMBER(1)",
0091: "BOOL"));
0092: ret.put("LONG", new SQLDataType("LONG", 0, "LONG", "CLOB"));
0093: ret
0094: .put("OTHER", new SQLDataType("OTHER", 0, "OBJECT",
0095: "OTHER"));
0096: return ret;
0097: }
0098:
0099: private static Workbench2Designer.IDCounter idCounter = new Workbench2Designer.IDCounter();
0100:
0101: //DBD transformation ralated fields
0102: private static DBDIDReference dbdIDReference;
0103:
0104: //GUI
0105: private static final int dbdCanvasWidth = 1000;
0106: private static final int dbdCanvasHeight = 4000;
0107: private static final boolean cascadeIfNecc = true;
0108: private final static int dbdXOffset = 100;
0109: private final static int dbdYOffset = 20;
0110: private final static int dbdTableWidth = 300;
0111: private final static int dbdTableHeight = 100;
0112: private static final Workbench2Designer.GUIPositioner dbdTablePositioner = new Workbench2Designer.GUIPositioner(
0113: dbdCanvasHeight, dbdCanvasWidth, dbdTableHeight,
0114: dbdTableWidth, dbdXOffset, dbdYOffset, cascadeIfNecc);
0115: //so called metadata
0116: private static final LinkedList dbdRelationKinds = Workbench2Designer
0117: .initDBDRelationKinds();
0118:
0119: private static LinkedList initDBDRelationKinds() {
0120: LinkedList ret = new LinkedList();
0121: ret.add(0, "1:1");
0122: ret.add(1, "1:n");
0123: ret.add(2, "n:n");
0124: return ret;
0125: }
0126:
0127: private static final LinkedList dbdOnUpdate = Workbench2Designer
0128: .initDBDOnUpdate();
0129:
0130: private static LinkedList initDBDOnUpdate() {
0131: LinkedList ret = new LinkedList();
0132: ret.add(0, "RESTRICT");
0133: ret.add(1, "CASCADE");
0134: ret.add(2, "SET NULL");
0135: ret.add(3, "NO ACTION");
0136: ret.add(4, "SET DEFAULT");
0137:
0138: return ret;
0139: }
0140:
0141: private static final LinkedList dbdOnDelete = Workbench2Designer
0142: .initDBDOnDelete();
0143:
0144: private static LinkedList initDBDOnDelete() {
0145: LinkedList ret = new LinkedList();
0146: ret.add(0, "RESTRICT");
0147: ret.add(1, "CASCADE");
0148: ret.add(2, "SET NULL");
0149: ret.add(3, "NO ACTION");
0150: ret.add(4, "SET DEFAULT");
0151:
0152: return ret;
0153: }
0154:
0155: private static final TreeMap dbdDataTypeGroupRef = Workbench2Designer
0156: .initDBDDataTypeGroupRef();
0157:
0158: private static TreeMap initDBDDataTypeGroupRef() {
0159: TreeMap ret = new TreeMap();
0160: ret.put("INTEGER", "Numeric Types");
0161: ret.put("FLOAT", "Numeric Types");
0162: ret.put("#FLOAT", "Numeric Types");
0163: ret.put("DOUBLE", "Numeric Types");
0164: ret.put("REAL", "Numeric Types");
0165: ret.put("DECIMAL", "Numeric Types");
0166: ret.put("NUMBER", "Numeric Types");
0167: ret.put("DATE", "Date and Time Types");
0168: ret.put("DATETIME", "Date and Time Types");
0169: ret.put("TIMESTAMP", "Date and Time Types");
0170: ret.put("VARCHAR2", "String Types");
0171: ret.put("VARCHAR", "String Types");
0172: ret.put("BOOL", "Numeric Types");
0173: ret.put("LONG", "Numeric Types");
0174: return ret;
0175: }
0176:
0177: private static final LinkedList dbdDataTypeGroups = Workbench2Designer
0178: .initDBDDataTypeGroups();
0179:
0180: private static LinkedList initDBDDataTypeGroups() {
0181:
0182: LinkedList ret = new LinkedList();
0183: ret.add(0, "Numeric Types");
0184: ret.add(1, "Date and Time Types");
0185: ret.add(2, "String Types");
0186: ret.add(3, "Blob and Text Types");
0187: ret.add(4, "User defined Types");
0188: ret.add(5, "Geographic Types");
0189: return ret;
0190: }
0191:
0192: private static final String[] dbdRegionColors = Workbench2Designer
0193: .initDBDRegionColors();
0194:
0195: private static String[] initDBDRegionColors() {
0196: String[] ret = { "Yellow=#FEFDED", "Green=#EAFFE5",
0197: "Cyan=#ECFDFF", "Blue=#F0F1FE", "Magenta=#FFEBFA" };
0198: return ret;
0199: }
0200:
0201: public Workbench2Designer(File f) throws SAXException,
0202: ParserConfigurationException, IOException {
0203: InputSource in = new InputSource(new BufferedInputStream(
0204: new FileInputStream(f)));
0205: this .init(in);
0206: }
0207:
0208: public Workbench2Designer(Reader in) throws SAXException,
0209: ParserConfigurationException, IOException {
0210: this .init(new InputSource(in));
0211: }
0212:
0213: public Workbench2Designer(InputStream in) throws SAXException,
0214: ParserConfigurationException, IOException {
0215: this .init(new InputSource(in));
0216: }
0217:
0218: private void init(InputSource in) throws SAXException,
0219: ParserConfigurationException, IOException {
0220: DocumentBuilderFactory factory = DocumentBuilderFactory
0221: .newInstance();
0222: //factory.setNamespaceAware(true);
0223: //factory.setValidating(true);
0224: DocumentBuilder builder = factory.newDocumentBuilder();
0225: Workbench2Designer.source = builder.parse(in);
0226: Workbench2Designer.destination = builder.newDocument();
0227: }
0228:
0229: public void transformWorkbench2Designer() throws DOMException,
0230: Workbench2Designer.MalformedSourceException {
0231: this .wbReadGlobalSettings();
0232: this .wbReadTables();
0233: Workbench2Designer.dbdIDReference = new DBDIDReference();
0234: Workbench2Designer.dbdIDReference.createDBDIDRef();
0235:
0236: Element dbdSettings = this .dbdCreateSettings();
0237: Element dbdTables = this .dbdCreateTables();
0238: Element dbdRelations = this .dbdCreateRelations();
0239: Element dbdNotes = destination.createElement("NOTES");
0240: Element dbdImages = destination.createElement("IMAGES");
0241: Element dbdPluginData = destination.createElement("PLUGINDATA");
0242: Element dbdPluginDataRec = destination
0243: .createElement("PLUGINDATARECORDS");
0244: Element dbdQueryData = destination.createElement("QUERYDATA");
0245: Element dbdQueryDataRec = destination
0246: .createElement("QUERYRECORDS");
0247: Element dbdLinkedMod = destination
0248: .createElement("LINKEDMODELS");
0249: Element dbdMetaData = destination.createElement("METADATA");
0250: Element dbdRoot = destination.createElement("DBMODEL");
0251:
0252: destination.appendChild(dbdRoot);
0253: dbdRoot.setAttribute("version", "4.0");
0254: dbdRoot.appendChild(dbdSettings);
0255: dbdRoot.appendChild(dbdMetaData);
0256: dbdRoot.appendChild(dbdPluginData);
0257: dbdRoot.appendChild(dbdQueryData);
0258: dbdPluginData.appendChild(dbdPluginDataRec);
0259: dbdQueryData.appendChild(dbdQueryDataRec);
0260: dbdMetaData.appendChild(dbdTables);
0261: dbdMetaData.appendChild(dbdRelations);
0262: dbdMetaData.appendChild(dbdImages);
0263: dbdMetaData.appendChild(dbdNotes);
0264:
0265: //actually not needed..
0266: this .dbdAppendRelations();
0267: //needed...
0268: this .dbdAppendIndices();
0269: }
0270:
0271: public void writeOutputFile(File outputFile) throws IOException,
0272: TransformerException, TransformerConfigurationException {
0273: // Use a Transformer for output
0274: TransformerFactory tFactory = TransformerFactory.newInstance();
0275: Transformer transformer = tFactory.newTransformer();
0276:
0277: DOMSource domsource = new DOMSource(
0278: Workbench2Designer.destination);
0279:
0280: StreamResult result = new StreamResult(outputFile);
0281: transformer.transform(domsource, result);
0282: }
0283:
0284: public String stripNewLines(String s) {
0285: String t = s.trim();
0286: int x = t.indexOf("\n");
0287: if (x >= 0)
0288: t = t.substring(0, x);
0289: return t;
0290: }
0291:
0292: public String getNodeTypeName(int index) {
0293: // An array of names for DOM node-types
0294: // (Array indexes = nodeType() values.)
0295: final String[] typeName = { "none", "Element", "Attr", "Text",
0296: "CDATA", "EntityRef", "Entity", "ProcInstr", "Comment",
0297: "Document", "DocType", "DocFragment", "Notation", };
0298: return typeName[index];
0299: }
0300:
0301: public String getTextFromElement(Node node) {
0302: String type = getNodeTypeName(node.getNodeType());
0303: if (!type.equals("Element"))
0304: return "";
0305: Node firstChild = node.getFirstChild();
0306: String text;
0307: if (firstChild == null) {
0308: text = "";
0309: } else {
0310: text = (getNodeTypeName(firstChild.getNodeType())
0311: .equals("Text")) ? this .stripNewLines(firstChild
0312: .getNodeValue()) : "";
0313: }
0314: return text;
0315: }
0316:
0317: public String getTextByTagName(String tagname, Document document) {
0318: NodeList nodelist;
0319: Node node;
0320: String ret;
0321: try {
0322: nodelist = document.getElementsByTagName(tagname);
0323: node = nodelist.item(0);
0324: ret = this .getTextFromElement(node);
0325: } catch (java.lang.NullPointerException e) {
0326: ret = "";
0327: }
0328: return ret;
0329: }
0330:
0331: public void setAttribute(String name, String value, Element element) {
0332: if (value == null)
0333: value = "";
0334: if (name != null)
0335: element.setAttribute(name, value);
0336: }
0337:
0338: public void setAttributes(TreeMap attributes, Element element) {
0339: Iterator it = attributes.entrySet().iterator();
0340: while (it.hasNext()) {
0341: Map.Entry entry = (Map.Entry) it.next();
0342: String name = (String) entry.getKey();
0343: if (name == null)
0344: continue;
0345: Object value = entry.getValue();
0346: if (value == null) {
0347: element.setAttribute(name, "");
0348: } else if (value instanceof String) {
0349: if (name != null)
0350: element.setAttribute(name, (String) value);
0351: } else {
0352: LogMgr.logError("Workbench2Designer.setAttributes()",
0353: "Not a String for key=" + name + ", got "
0354: + value.getClass().getName()
0355: + " instead!", null);
0356: }
0357: }
0358:
0359: }
0360:
0361: public void forceEndTag(Element node) {
0362: if (!node.hasChildNodes()) {
0363: node.setAttribute("xml:space", "preserve");
0364: Node forceEndTag = destination.createTextNode(" ");
0365: node.appendChild(forceEndTag);
0366: }
0367: }
0368:
0369: public Element dbdCreateTables() throws DOMException,
0370: Workbench2Designer.MalformedSourceException {
0371: Set tblNames = Workbench2Designer.tables.keySet();
0372: Iterator it = tblNames.iterator();
0373: Element tablesElm = destination.createElement("TABLES");
0374: int orderPos = 0;
0375: while (it.hasNext()) {
0376: orderPos++;
0377:
0378: Table table = (Table) Workbench2Designer.tables.get(it
0379: .next());
0380: Element tableElm = destination.createElement("TABLE");
0381: String id = dbdIDReference.getTableDBDID(table.getName());
0382:
0383: Workbench2Designer.dbdTablePositioner.setNewPosition();
0384: int xPos = Workbench2Designer.dbdTablePositioner
0385: .getCurrentXPos();
0386: int yPos = Workbench2Designer.dbdTablePositioner
0387: .getCurrentYPos();
0388:
0389: TreeMap attributes = new TreeMap();
0390: attributes.put("ID", id);
0391: attributes.put("Comments", table.getComment());
0392: attributes.put("Tablename", table.getName());
0393: attributes.put("Collapsed", "0");
0394: attributes.put("IDLinkedModel", "-1");
0395: attributes.put("IsLinkedObject", "0");
0396: attributes.put("Obj_id_Linked", "-1");
0397: attributes.put("OrderPos", orderPos + "");
0398: attributes.put("PrevTableName", "");
0399: attributes.put("StandardInserts", "\n");
0400: attributes.put("TableOptions", "");
0401: attributes.put("TablePrefix", "0");
0402: attributes.put("TableType", "0");
0403: attributes.put("UseStandardInserts", "0");
0404: attributes.put("XPos", xPos + "");
0405: attributes.put("YPos", yPos + "");
0406: attributes.put("nmTable", "0");
0407:
0408: this .setAttributes(attributes, tableElm);
0409:
0410: tablesElm.appendChild((Node) tableElm);
0411: Element columnsElm = this .dbdCreateColumns(table);
0412: tableElm.appendChild((Node) columnsElm);
0413: }
0414: return tablesElm;
0415: }
0416:
0417: public Element dbdCreateColumns(Table table) throws DOMException,
0418: Workbench2Designer.MalformedSourceException {
0419: Element columnsElm = destination.createElement("COLUMNS");
0420:
0421: TreeMap columns = table.getColumns();
0422: Set colNames = columns.keySet();
0423: Iterator iit = colNames.iterator();
0424:
0425: while (iit.hasNext()) {
0426: try {
0427: Column column = (Column) columns.get(iit.next());
0428: Element columnElm = destination.createElement("COLUMN");
0429: SQLDataTypeStatement dataTypeStatement = column
0430: .getDataTypeStatement();
0431: String idDataType = Workbench2Designer.dbdIDReference
0432: .getDataTypeDBDID(dataTypeStatement.datatype.index);
0433: int notNull = (column.isNullable()) ? 0 : 1;
0434: int foreignKey = (column.isForeignKey()) ? 1 : 0;
0435: int primaryKey = (column.isPrimaryKey()) ? 1 : 0;
0436:
0437: TreeMap attributes = new TreeMap();
0438: String name = column.getName();
0439: String id = Workbench2Designer.dbdIDReference
0440: .getColumnDBDID(table.name, name);
0441:
0442: attributes.put("ID", id);
0443: attributes.put("Comments", column.getComment());
0444: attributes.put("ColName", name);
0445: attributes.put("DatatypeParams",
0446: dataTypeStatement.paramString);
0447: attributes
0448: .put("DefaultValue", column.getDefaultValue());
0449: attributes.put("IsForeignKey", "" + foreignKey);
0450: attributes.put("NotNull", "" + notNull);
0451: attributes.put("Pos", column.position + "");
0452: attributes.put("Prec", "-1");
0453: attributes.put("PrevColName", "");
0454: attributes.put("PrimaryKey", "" + primaryKey);
0455: attributes.put("Width", "-1");
0456: attributes.put("idDatatype", idDataType);
0457: attributes.put("AutoInc", "0");
0458:
0459: this .setAttributes(attributes, columnElm);
0460:
0461: columnsElm.appendChild((Node) columnElm);
0462: Element optionsElm = destination
0463: .createElement("OPTIONSELECTED");
0464: this .forceEndTag(optionsElm);
0465: columnElm.appendChild((Node) optionsElm);
0466: } catch (java.lang.NullPointerException ne) {
0467: throw (new Workbench2Designer.MalformedSourceException(
0468: "Invalid table declaration ", ne));
0469: }
0470: }
0471: return columnsElm;
0472: }
0473:
0474: public Element dbdCreateRelations() throws DOMException,
0475: Workbench2Designer.MalformedSourceException {
0476: Iterator it = Workbench2Designer.relations.entrySet()
0477: .iterator();
0478: Element relationsElm = destination.createElement("RELATIONS");
0479: while (it.hasNext()) {
0480: Map.Entry entry = (Map.Entry) it.next();
0481: Relation relation = (Relation) entry.getValue();
0482: if (relation == null)
0483: continue;
0484:
0485: try {
0486: Table src = (Table) Workbench2Designer.tables
0487: .get(relation.getSrcTable());
0488:
0489: // ignore missing tables
0490: if (src == null) {
0491: LogMgr
0492: .logWarning(
0493: "Workbench2DbDesigner.dbdCreateRelations()",
0494: "Source table "
0495: + relation.getSrcTable()
0496: + "for relation "
0497: + relation.getRelName()
0498: + " not found in WB table list!");
0499: it.remove();
0500: continue;
0501: }
0502:
0503: //String relSource = Workbench2Designer.dbdIDReference.getTableDBDID(src.getName());
0504: Table dest = (Table) Workbench2Designer.tables
0505: .get(relation.getDestTable());
0506:
0507: if (dest == null) {
0508: LogMgr
0509: .logWarning(
0510: "Workbench2DbDesigner.dbdCreateRelations()",
0511: "Destination table "
0512: + relation.getDestTable()
0513: + "for relation "
0514: + relation.getRelName()
0515: + " not found in WB table list!");
0516: it.remove();
0517: continue;
0518: }
0519:
0520: String relDestination = Workbench2Designer.dbdIDReference
0521: .getTableDBDID(dest.getName());
0522: if (relDestination == null) {
0523: LogMgr
0524: .logWarning(
0525: "Workbench2DbDesigner.dbdCreateRelations()",
0526: "Destination string for relation "
0527: + relation.getRelName()
0528: + " not found in WB table list!");
0529: it.remove();
0530: continue;
0531: }
0532: String kind;
0533:
0534: String[] cols = relation.getConstraintStatement()
0535: .split("\\\\n");
0536: cols = cols[0].split(relation.getOperator());
0537: Column sc = (Column) src.getColumns().get(cols[0]);
0538: Column dc = (Column) dest.getColumns().get(cols[1]);
0539: String t1 = (sc.isPrimaryKey() && !(src
0540: .getPrimaryKeys().size() > 1)) ? "1" : "n";
0541: String t2 = (dc.isPrimaryKey() && !(dest
0542: .getPrimaryKeys().size() > 1)) ? "1" : "n";
0543: kind = Workbench2Designer.dbdRelationKinds.indexOf(t1
0544: + ":" + t2)
0545: + "";
0546:
0547: String onDelete = (!(relation.getOnDelete().equals("") && relation
0548: .getOnDelete() == null)) ? "OnDelete="
0549: + Workbench2Designer.dbdOnDelete
0550: .indexOf(relation.getOnDelete())
0551: + "\\n" : "";
0552: String onUpdate = (!(relation.getOnUpdate().equals("") && relation
0553: .getOnUpdate() == null)) ? "OnUpdate="
0554: + Workbench2Designer.dbdOnUpdate
0555: .indexOf(relation.getOnUpdate())
0556: + "\\n" : "";
0557:
0558: Element relationElm = Workbench2Designer.destination
0559: .createElement("RELATION");
0560:
0561: TreeMap attributes = new TreeMap();
0562: String id = Workbench2Designer.dbdIDReference
0563: .getRelationDBDID(relation.getID());
0564:
0565: String srcId = Workbench2Designer.dbdIDReference
0566: .getTableDBDID(src.getName());
0567: String destId = Workbench2Designer.dbdIDReference
0568: .getTableDBDID(dest.getName());
0569: attributes.put("ID", id);
0570: attributes.put("DestTable", destId);
0571: attributes.put("SrcTable", srcId);
0572: attributes.put("RelationName", relation.getRelName());
0573: attributes.put("RefDef", "Matching=0\\n" + onDelete
0574: + onUpdate);
0575: attributes.put("CaptionOffsetX", "0");
0576: attributes.put("CaptionOffsetY", "0");
0577: attributes.put("Comments", "");
0578: attributes.put("CreateRefDef", "1");
0579: attributes.put("EndIntervalOffsetX", "0");
0580: attributes.put("EndIntervalOffsetY", "0");
0581: attributes.put("FKFields", relation
0582: .getConstraintStatement());
0583: attributes.put("FKFieldsComments", "\\n");
0584: attributes.put("FKRefDefIndex_Obj_id", "-1");
0585: attributes.put("IDLinkedModel", "-1");
0586: attributes.put("Invisible", "0");
0587: attributes.put("IsLinkedObject", "0");
0588: attributes.put("Kind", kind);
0589: attributes.put("MidOffset", "35");
0590: attributes.put("Obj_id_Linked", "-1");
0591: attributes.put("OptionalEnd", "0");
0592: attributes.put("OptionalStart", "0");
0593: attributes.put("OrderPos", "0");
0594: attributes.put("Splitted", "0");
0595: attributes.put("StartIntervalOffsetX", "0");
0596: attributes.put("StartIntervalOffsetY", "");
0597: attributes.put("relDirection", "4");
0598:
0599: this .setAttributes(attributes, relationElm);
0600:
0601: relationsElm.appendChild((Node) relationElm);
0602: } catch (java.lang.NullPointerException ne) {
0603: //throw(new Workbench2Designer.MalformedSourceException("Invalid relation - destination/source missing or not existing",ne));
0604: LogMgr.logError(
0605: "Workbench2Designer.dbdCreateRelations",
0606: "Error when adding relation for " + relation,
0607: ne);
0608: }
0609: }//while
0610: return relationsElm;
0611:
0612: }
0613:
0614: public Element dbdCreateDatatypeGroups() throws DOMException {
0615:
0616: Iterator it = Workbench2Designer.dbdDataTypeGroups
0617: .listIterator();
0618: Element dataTypeGroupsElm = destination
0619: .createElement("DATATYPEGROUPS");
0620: int i = 0;
0621: while (it.hasNext()) {
0622: i++;
0623: String dataTypeGroup = (String) it.next();
0624:
0625: Element dataTypeGroupElm = destination
0626: .createElement("DATATYPEGROUP");
0627:
0628: dataTypeGroupElm.setAttribute("Name", dataTypeGroup);
0629: dataTypeGroupElm.setAttribute("Icon", i + "");
0630:
0631: dataTypeGroupsElm.appendChild((Node) dataTypeGroupElm);
0632:
0633: }
0634: return dataTypeGroupsElm;
0635:
0636: }
0637:
0638: public Element dbdCreateDatatypes() throws DOMException,
0639: Workbench2Designer.MalformedSourceException {
0640: Set dtpKeys = Workbench2Designer.globalSQLDataTypes.keySet();
0641: Iterator it = dtpKeys.iterator();
0642: Element datatypesElm = destination.createElement("DATATYPES");
0643:
0644: while (it.hasNext()) {
0645: try {
0646: String text = (String) it.next();
0647: SQLDataType datatype = (SQLDataType) Workbench2Designer.globalSQLDataTypes
0648: .get(text);
0649: String group = (String) Workbench2Designer.dbdDataTypeGroupRef
0650: .get(datatype.index);
0651: String idGroup = Workbench2Designer.dbdDataTypeGroups
0652: .indexOf(group)
0653: + "";
0654: Element datatypeElm = destination
0655: .createElement("DATATYPE");
0656: String id = Workbench2Designer.dbdIDReference
0657: .getDataTypeDBDID(datatype.index);
0658: TreeMap attributes = new TreeMap();
0659:
0660: attributes.put("ID", id);
0661: attributes.put("Description", "");
0662: attributes.put("IDGroup", idGroup);
0663: attributes.put("OptionCount", "0");
0664: attributes.put("ParamCount", datatype.paramCount + "");
0665: attributes.put("ParamRequired", datatype.paramRequired
0666: + "");
0667: attributes.put("PhysicalMapping", "0");
0668: attributes.put("PhysicalTypeName", datatype.alias);
0669: attributes.put("SynonymGroup", "0");
0670: attributes.put("TypeName", datatype.name);
0671: attributes.put("EditParamsAsString", "0");
0672:
0673: this .setAttributes(attributes, datatypeElm);
0674:
0675: datatypesElm.appendChild((Node) datatypeElm);
0676: if (datatype.paramCount != 0) {
0677: Element optionsElm = destination
0678: .createElement("OPTIONS");
0679: Element optionElm = destination
0680: .createElement("OPTION");
0681: attributes = new TreeMap();
0682: attributes.put("Default", "0");
0683: attributes.put("Name", "ZEROFILL");
0684: this .setAttributes(attributes, optionElm);
0685: Element paramsElm = destination
0686: .createElement("PARAMS");
0687: if (datatype.params != null) {
0688: for (int i = 0; i < datatype.params.length; i++) {
0689: Element paramElm = destination
0690: .createElement("PARAM");
0691: this .setAttribute("Name",
0692: datatype.params[i], paramElm);
0693: paramsElm.appendChild(paramElm);
0694: }
0695: }
0696: datatypeElm.appendChild(paramsElm);
0697: optionsElm.appendChild(optionElm);
0698: datatypeElm.appendChild(optionsElm);
0699: } else {
0700: forceEndTag(datatypeElm);
0701: }
0702: } catch (java.lang.NullPointerException ne) {
0703: throw (new Workbench2Designer.MalformedSourceException(
0704: "datatype not found in dbd datatype-groups", ne));
0705: }
0706: }
0707: return datatypesElm;
0708:
0709: }
0710:
0711: public Element dbdCreateCommonDatatypes() throws DOMException {
0712:
0713: Element dbdCommonDataTypes = destination
0714: .createElement("COMMON_DATATYPES");
0715: Iterator it = Workbench2Designer.dataTypes.keySet().iterator();
0716: Element dbdCommonDataType;
0717: while (it.hasNext()) {
0718: String dataTypeIndexName = (String) it.next();
0719: String id = dbdIDReference
0720: .getDataTypeDBDID(dataTypeIndexName);
0721: dbdCommonDataType = destination
0722: .createElement("COMMON_DATATYPE");
0723: dbdCommonDataType.setAttribute("ID", id);
0724: dbdCommonDataTypes.appendChild(dbdCommonDataType);
0725: }
0726: return dbdCommonDataTypes;
0727: }
0728:
0729: public Element dbdCreateGlobalSettings() throws DOMException {
0730:
0731: Element globalsettingsElm = destination
0732: .createElement("GLOBALSETTINGS");
0733: TreeMap attributes = new TreeMap();
0734: attributes.put("ActivateRefDefForNewRelations", "1");
0735: attributes.put("AutoIncVersion", "1");
0736: attributes.put("CanvasHeight",
0737: Workbench2Designer.dbdCanvasHeight + "");
0738: attributes.put("CanvasWidth", Workbench2Designer.dbdCanvasWidth
0739: + "");
0740: attributes.put("Comments",
0741: Workbench2Designer.globalSettings.comments);
0742: attributes.put("CreateFKRefDefIndex", "0");
0743: attributes.put("CreateSQLforLinkedObjects", "0");
0744: attributes.put("DBQuoteCharacter", "\"");
0745: attributes.put("DatabaseType", "MySql");
0746: attributes.put("DefModelFont", "Tahoma");
0747: attributes.put("DefQueryDBConn", "");
0748: attributes.put("DefSaveDBConn", "");
0749: attributes.put("DefSyncDBConn", "");
0750: attributes.put("DefaultDataType", "5");
0751: attributes.put("DefaultTablePrefix", "0");
0752: attributes.put("DefaultTableType", "0");
0753: attributes.put("Description", "");
0754: attributes.put("FKPostfix", "");
0755: attributes.put("FKPrefix", "");
0756: attributes.put("HPageCount", "3.005135730007337");
0757: attributes.put("IDModel", "0");
0758: attributes.put("IDVersion", "0");
0759: attributes.put("ModelName",
0760: Workbench2Designer.globalSettings.modelName);
0761: attributes.put("PageAspectRatio", "1.418318380972251");
0762: attributes.put("PageFormat",
0763: "A4 (210x297 mm, 8.26x11.7 inches)");
0764: attributes.put("PageOrientation", "1");
0765: attributes.put("PositionGridX", "20");
0766: attributes.put("PositionGridY", "20");
0767: attributes.put("Printer", "");
0768: attributes.put("SelectedPages", "0");
0769: attributes.put("TableNameInRefs", "1");
0770: attributes.put("UsePositionGrid", "0");
0771: attributes.put("UseVersionHistroy", "1");
0772: attributes.put("VersionStr", "1.0.0.0");
0773: attributes.put("XPos", "0");
0774: attributes.put("YPos", "0");
0775: attributes.put("ZoomFac", "100.00");
0776:
0777: this .setAttributes(attributes, globalsettingsElm);
0778:
0779: return globalsettingsElm;
0780:
0781: }
0782:
0783: public Element dbdCreateSettings() throws DOMException,
0784: Workbench2Designer.MalformedSourceException {
0785:
0786: Element dbdSettings = destination.createElement("SETTINGS");
0787:
0788: Element dbdGlobalSettings = this .dbdCreateGlobalSettings();
0789: Element dbdDataTypeGroups = this .dbdCreateDatatypeGroups();
0790: Element dbdDataTypes = this .dbdCreateDatatypes();
0791: Element dbdCommonDataTypes = this .dbdCreateCommonDatatypes();
0792:
0793: Element dbdTablePrefixes = destination
0794: .createElement("TABLEPREFIXES");
0795: Element dbdTablePrefix = destination
0796: .createElement("TABLEPREFIX");
0797: dbdTablePrefix.setAttribute("Name", "Default (no prefix)");
0798: dbdTablePrefixes.appendChild(dbdTablePrefix);
0799:
0800: Element dbdRegionColors = destination
0801: .createElement("REGIONCOLORS");
0802: Element dbdRegionColor;
0803: for (int i = 0; i < Workbench2Designer.dbdRegionColors.length; i++) {
0804: dbdRegionColor = destination.createElement("REGIONCOLOR");
0805: dbdRegionColor.setAttribute("Color",
0806: Workbench2Designer.dbdRegionColors[i]);
0807: dbdRegionColors.appendChild(dbdRegionColor);
0808: }
0809:
0810: Element dbdPositionMarkers = destination
0811: .createElement("POSITIONMARKERS");
0812: Element dbdPositionMarker = destination
0813: .createElement("POSITIONMARKER");
0814: dbdPositionMarker.setAttribute("X", "0");
0815: dbdPositionMarker.setAttribute("Y", "0");
0816: dbdPositionMarker.setAttribute("ZoomFac", "-1.0");
0817: dbdPositionMarkers.appendChild(dbdPositionMarker);
0818:
0819: dbdSettings.appendChild(dbdGlobalSettings);
0820: dbdSettings.appendChild(dbdDataTypeGroups);
0821: dbdSettings.appendChild(dbdDataTypes);
0822: dbdSettings.appendChild(dbdCommonDataTypes);
0823: dbdSettings.appendChild(dbdTablePrefixes);
0824: dbdSettings.appendChild(dbdRegionColors);
0825: dbdSettings.appendChild(dbdPositionMarkers);
0826:
0827: return dbdSettings;
0828: }
0829:
0830: public void dbdAppendRelations() throws DOMException {
0831:
0832: NodeList dbdTables = destination.getElementsByTagName("TABLE");
0833:
0834: for (int i = 0; i < dbdTables.getLength(); i++) {
0835: NamedNodeMap attributes = dbdTables.item(i).getAttributes();
0836: String tablename = attributes.getNamedItem("Tablename")
0837: .getNodeValue();
0838: Table table = (Table) Workbench2Designer.tables
0839: .get(tablename);
0840:
0841: Element dbdRelationsStart = destination
0842: .createElement("RELATIONS_START");
0843: Element dbdRelationsEnd = destination
0844: .createElement("RELATIONS_END");
0845: Element dbdRelationEnd;
0846: Element dbdRelationStart;
0847:
0848: Iterator iit = table.getRelations().keySet().iterator();
0849: while (iit.hasNext()) {
0850: Relation relation = (Relation) Workbench2Designer.relations
0851: .get(iit.next());
0852: String id = Workbench2Designer.dbdIDReference
0853: .getRelationDBDID(relation.getID());
0854: if (table.getName().equals(relation.getDestTable())) {
0855: dbdRelationEnd = destination
0856: .createElement("RELATION_END");
0857: dbdRelationEnd.setAttribute("ID", id);
0858: dbdRelationsEnd.appendChild(dbdRelationEnd);
0859: }
0860: if (table.getName().equals(relation.getSrcTable())) {
0861: dbdRelationStart = destination
0862: .createElement("RELATION_START");
0863: dbdRelationStart.setAttribute("ID", id);
0864: dbdRelationsStart.appendChild(dbdRelationStart);
0865: }
0866: }
0867: this .forceEndTag(dbdRelationsStart);
0868: dbdTables.item(i).appendChild(dbdRelationsStart);
0869: this .forceEndTag(dbdRelationsEnd);
0870: dbdTables.item(i).appendChild(dbdRelationsEnd);
0871: }
0872: }
0873:
0874: public void dbdAppendIndices() throws DOMException {
0875: NodeList dbdTables = destination.getElementsByTagName("TABLE");
0876:
0877: for (int i = 0; i < dbdTables.getLength(); i++) {
0878: NamedNodeMap attr = dbdTables.item(i).getAttributes();
0879: String tablename = attr.getNamedItem("Tablename")
0880: .getNodeValue();
0881: Table table = (Table) Workbench2Designer.tables
0882: .get(tablename);
0883: Element dbdIndices = destination.createElement("INDICES");
0884: TreeMap indices = table.getIndices();
0885: Iterator it = indices.keySet().iterator();
0886: while (it.hasNext()) {
0887: Index index = (Index) indices.get(it.next());
0888: if (index.isPrimaryKey())
0889: continue;
0890: Element dbdIndex = destination.createElement("INDEX");
0891:
0892: TreeMap attributes = new TreeMap();
0893: attributes.put("IndexName", index.getName());
0894: attributes.put("IndexKind", "0");
0895: attributes.put("FKRefDef_Obj_id", "-1");
0896: attributes.put("ID", dbdIDReference.getIndexDBDID(index
0897: .getID()));
0898: this .setAttributes(attributes, dbdIndex);
0899:
0900: Element dbdIndexCols = destination
0901: .createElement("INDEXCOLUMNS");
0902:
0903: Iterator iit = index.getColumns().iterator();
0904: while (iit.hasNext()) {
0905: Element dbdIndexCol = destination
0906: .createElement("INDEXCOLUMN");
0907: dbdIndexCols.appendChild(dbdIndexCol);
0908:
0909: String colName = (String) iit.next();
0910: String idColumn = Workbench2Designer.dbdIDReference
0911: .getColumnDBDID(table.getName(), colName);
0912: attributes = new TreeMap();
0913: attributes.put("LengthParam", "0");
0914: attributes.put("idColumn", idColumn);
0915: this .setAttributes(attributes, dbdIndexCol);
0916: }
0917: dbdIndices.appendChild(dbdIndex);
0918: dbdIndex.appendChild(dbdIndexCols);
0919: this .forceEndTag(dbdIndexCols);
0920: }
0921: this .forceEndTag(dbdIndices);
0922: dbdTables.item(i).appendChild(dbdIndices);
0923: }
0924:
0925: }
0926:
0927: public void wbReadGlobalSettings() {
0928: String databaseType = this .getTextByTagName(
0929: "database-product-name", source);
0930: String comments = "created: "
0931: + this .getTextByTagName("created", source);
0932: String modelName = "";
0933: Workbench2Designer.globalSettings = new GlobalSettings(
0934: comments, databaseType, modelName);
0935: }
0936:
0937: public void wbReadTables() {
0938: NodeList tbls = source.getElementsByTagName("table-def");
0939: for (int i = 0; i < tbls.getLength(); i++) {
0940: Node tbl = tbls.item(i);
0941: Table table = new Table();
0942: NodeList childs = tbl.getChildNodes();
0943:
0944: for (int ii = 0; ii < childs.getLength(); ii++) {
0945: Node node = childs.item(ii);
0946: String name = node.getNodeName();
0947: String text = this .getTextFromElement(node);
0948:
0949: if (name.equals("table-name")) {
0950: table.setName(text);
0951: continue;
0952: }
0953: if (name.equals("table-comment")) {
0954: table.setComment(text);
0955: continue;
0956: }
0957: if (name.equals("table-schema")) {
0958: table.setScheme(text);
0959: continue;
0960: }
0961: if (name.equals("column-def")) {
0962: wbReadColumn(table, node.getChildNodes());
0963: continue;
0964: }
0965: if (name.equals("index-def")) {
0966: wbReadIndex(table, node.getChildNodes());
0967: continue;
0968: }
0969:
0970: }
0971: if (!(table.getName() == null && table.getName().equals(""))) {
0972: Workbench2Designer.tables.put(table.getName(), table);
0973: }
0974: }
0975: }
0976:
0977: public void wbReadIndex(Table table, NodeList indexdata) {
0978: Index newIndex = new Index();
0979: for (int i = 0; i < indexdata.getLength(); i++) {
0980: Node node = indexdata.item(i);
0981: // String type = getNodeTypeName(node.getNodeType());
0982: String name = node.getNodeName();
0983: String text = this .getTextFromElement(node);
0984:
0985: if (name.equals("name")) {
0986: newIndex.setName(text);
0987: continue;
0988: }
0989: if (name.equals("unique")) {
0990: if (text.equals("true"))
0991: newIndex.setUnique(true);
0992: continue;
0993: }
0994: if (name.equals("primary-key")) {
0995: if (text.equals("true"))
0996: newIndex.setPrimaryKey(true);
0997: continue;
0998: }
0999: if (name.equals("index-expression")) {
1000: String[] cols = text.split(", ");
1001: for (int ii = 0; ii < cols.length; ii++) {
1002: newIndex.addColumn(cols[ii]);
1003: }
1004: continue;
1005: }
1006: }
1007: newIndex.setID(Workbench2Designer.idCounter.getNewID() + "");
1008: table.setIndex(newIndex);
1009: }
1010:
1011: public void wbReadColumn(Table table, NodeList coldata) {
1012: Column newCol = new Column();
1013: for (int i = 0; i < coldata.getLength(); i++) {
1014:
1015: Node node = coldata.item(i);
1016: // String type = getNodeTypeName(node.getNodeType());
1017: String name = node.getNodeName();
1018: String text = this .getTextFromElement(node);
1019:
1020: if (name.equals("column-name")) {
1021: newCol.setName(text);
1022: continue;
1023: }
1024: if (name.equals("dbms-data-type")) {
1025: SQLDataTypeStatement dataTypeStatement = new SQLDataTypeStatement(
1026: text);
1027: newCol.setDataTypeStatement(dataTypeStatement);
1028: continue;
1029: }
1030: if (name.equals("primary-key")) {
1031: if (text.equals("true"))
1032: newCol.setPrimaryKey();
1033: table.addPrimaryKey(newCol);
1034: continue;
1035: }
1036: if (name.equals("nullable")) {
1037: if (text.equals("true"))
1038: newCol.setNullable();
1039: continue;
1040: }
1041: if (name.equals("default-value")) {
1042: newCol.setDefaultValue(text);
1043: continue;
1044: }
1045: if (name.equals("dbms-position")) {
1046: Integer pos = (text != null && !text.equals("")) ? new Integer(
1047: text)
1048: : new Integer(0);
1049: newCol.setPosition(pos.intValue());
1050: continue;
1051: }
1052:
1053: }
1054: for (int i = 0; i < coldata.getLength(); i++) {
1055: String colName = coldata.item(i).getNodeName();
1056: if (colName.equals("references")) {
1057: newCol.setForeignKey();
1058: wbReadRelations(newCol.getName(), table.getName(),
1059: coldata.item(i).getChildNodes());
1060: }
1061: }
1062: if (!(newCol.getName() == null || newCol.getName().equals(""))) {
1063: SQLDataType datatype = newCol.getDataTypeStatement().datatype;
1064: Workbench2Designer.dataTypes.put(datatype.index, "");
1065: table.addColumn(newCol);
1066: }
1067: }
1068:
1069: public void wbReadRelations(String colname, String tablename,
1070: NodeList relcontent) {
1071: Relation newRel = new Relation();
1072: for (int i = 0; i < relcontent.getLength(); i++) {
1073:
1074: Node node = relcontent.item(i);
1075: String name = node.getNodeName();
1076: String text = this .getTextFromElement(node);
1077:
1078: if (name.equals("table-name")) {
1079: newRel = new Relation();
1080: newRel.setOperator("=");
1081: newRel.setID(Workbench2Designer.idCounter.getNewID());
1082: newRel.setDestTable(tablename);
1083: newRel.setSrcTable(text);
1084: String id = newRel.getID();
1085: Workbench2Designer.relations.put(id, newRel);
1086: }
1087: if (name.equals("column-name")) {
1088: newRel.addConstraint(text, colname);
1089: }
1090: if (name.equals("constraint-name")) {
1091: newRel.setRelName(text);
1092: }
1093: if (name.equals("update-rule")) {
1094: newRel.setOnUpdate(text);
1095: }
1096: if (name.equals("delete-rule")) {
1097: newRel.setOnDelete(text);
1098: }
1099: }
1100: }
1101:
1102: private static class GUIPositioner {
1103: private int xPos = 0;
1104: private int yPos = 0;
1105: private int canvasHeight;
1106: private int canvasWidth;
1107: private int elementHeight;
1108: private int elementWidth;
1109: private int elementXOffset;
1110: private int elementYOffset;
1111: private boolean cascadeIfNecc;
1112:
1113: private GUIPositioner(int canvasHeight, int canvasWidth,
1114: int elementHeight, int elementWidth,
1115: int elementXOffset, int elementYOffset,
1116: boolean cascadeIfNecc) {
1117: this .canvasHeight = canvasHeight;
1118: this .canvasWidth = canvasWidth;
1119: this .elementHeight = elementHeight;
1120: this .elementWidth = elementWidth;
1121: this .elementYOffset = elementYOffset;
1122: this .elementXOffset = elementXOffset;
1123: this .cascadeIfNecc = cascadeIfNecc;
1124: }
1125:
1126: public int getCurrentXPos() {
1127: return this .xPos;
1128: }
1129:
1130: public int getCurrentYPos() {
1131: return this .yPos;
1132: }
1133:
1134: public void setNewPosition() {
1135: boolean endReachedX = (this .xPos + this .elementWidth
1136: - this .elementXOffset >= this .canvasWidth) ? true
1137: : false;
1138: boolean endReachedY = (this .yPos + this .elementHeight
1139: - this .elementYOffset >= this .canvasHeight) ? true
1140: : false;
1141: if (endReachedX && !endReachedY) {
1142: this .xPos = 0;
1143: this .yPos = this .yPos + this .elementYOffset;
1144: }
1145: if (!endReachedX) {
1146: this .xPos = this .xPos + this .elementXOffset;
1147: this .yPos = this .yPos + 20;
1148: }
1149: if (endReachedX && endReachedY && cascadeIfNecc) {
1150: this .elementXOffset = this .elementXOffset / 2;
1151: this .elementYOffset = this .elementYOffset / 2;
1152: this .xPos = this .elementXOffset;
1153: this .yPos = this .elementYOffset;
1154: }
1155: if (endReachedX && endReachedY && !cascadeIfNecc) {
1156: this .xPos = 0;
1157: this .yPos = 0;
1158: }
1159: }
1160: }
1161:
1162: private static class IDCounter {
1163: private int idCounter = 0;
1164:
1165: public String getNewID() {
1166: this .idCounter = this .idCounter + 1;
1167: return this .idCounter + "";
1168: }
1169:
1170: }
1171:
1172: private static class DBDIDReference {
1173:
1174: private int idCounter = 0;
1175: private TreeMap dataTypeReference = new TreeMap();
1176: private TreeMap tableReference = new TreeMap();
1177: private TreeMap relationsReference = new TreeMap();
1178: private TreeMap columnsReference = new TreeMap();
1179: private TreeMap indicesReference = new TreeMap();
1180: private TreeMap extraReference = new TreeMap();
1181:
1182: private String getNewID() {
1183: this .idCounter = this .idCounter + 1;
1184: return this .idCounter + "";
1185: }
1186:
1187: public void createDBDIDRef() {
1188: Iterator it;
1189: it = Workbench2Designer.globalSQLDataTypes.keySet()
1190: .iterator();
1191: while (it.hasNext()) {
1192: this .dataTypeReference.put(it.next(), this .getNewID());
1193: }
1194: it = Workbench2Designer.tables.keySet().iterator();
1195: //this.idCounter=999;
1196: while (it.hasNext()) {
1197: String tableName = (String) it.next();
1198: this .tableReference.put(tableName, this .getNewID());
1199: Table table = (Table) Workbench2Designer.tables
1200: .get(tableName);
1201: Iterator iit = table.getColumns().keySet().iterator();
1202: while (iit.hasNext()) {
1203: String columnName = (String) iit.next();
1204: this .columnsReference.put(tableName + "/"
1205: + columnName, this .getNewID());
1206: }
1207: iit = table.getIndices().keySet().iterator();
1208: while (iit.hasNext()) {
1209: String id = (String) iit.next();
1210: this .indicesReference.put(id, this .getNewID());
1211: }
1212:
1213: }
1214: it = Workbench2Designer.relations.keySet().iterator();
1215: while (it.hasNext()) {
1216: this .relationsReference.put(it.next(), this .getNewID());
1217: }
1218: }
1219:
1220: public String getTableDBDID(String tableName) {
1221: return (String) this .tableReference.get(tableName);
1222: }
1223:
1224: public String getColumnDBDID(String tableName, String columnName) {
1225: return (String) this .columnsReference.get(tableName + "/"
1226: + columnName);
1227: }
1228:
1229: public String getIndexDBDID(String id) {
1230: return (String) this .indicesReference.get(id);
1231: }
1232:
1233: public String getRelationDBDID(String relationID) {
1234: return (String) this .relationsReference.get(relationID);
1235: }
1236:
1237: public String getDataTypeDBDID(String dataTypeIndexName) {
1238: return (String) this .dataTypeReference
1239: .get(dataTypeIndexName);
1240: }
1241:
1242: public String getExtraDBDID(String name) {
1243: return (String) this .extraReference.get(name);
1244: }
1245:
1246: }
1247:
1248: private static class GlobalSettings {
1249: private String comments;
1250: private String databaseType;
1251: private String modelName;
1252:
1253: GlobalSettings(String comments, String databaseType,
1254: String modelName) {
1255: this .comments = comments;
1256: this .databaseType = databaseType;
1257: this .modelName = modelName;
1258: }
1259: }
1260:
1261: public static class SQLDataTypeStatement {
1262:
1263: private String[] params;
1264: private String paramString = "";
1265: // private Object[] ret;
1266: private String name;
1267: private SQLDataType datatype;
1268: private int paramCount;
1269:
1270: private SQLDataTypeStatement(String typeStatement) {
1271:
1272: if (typeStatement.indexOf("(") != -1) {
1273: int start = typeStatement.indexOf("(");
1274: int end = typeStatement.indexOf(")");
1275: this .paramString = typeStatement.substring(start + 1,
1276: end);
1277: this .params = this .paramString.split(",");
1278: this .paramString = "(" + this .paramString + ")";
1279: this .name = typeStatement.substring(0, start);
1280: } else {
1281: this .name = typeStatement;
1282: }
1283: this .paramCount = (params == null) ? 0 : params.length;
1284: if (this .name.equals("FLOAT")) {
1285: if (this .paramCount == 1) {
1286: this .datatype = (SQLDataType) Workbench2Designer.globalSQLDataTypes
1287: .get("FLOAT");
1288: } else {
1289: this .datatype = (SQLDataType) Workbench2Designer.globalSQLDataTypes
1290: .get("#FLOAT");
1291: }
1292: } else {
1293: this .datatype = (SQLDataType) Workbench2Designer.globalSQLDataTypes
1294: .get(this .name);
1295: }
1296:
1297: if (this .datatype == null) {
1298: this .datatype = (SQLDataType) Workbench2Designer.globalSQLDataTypes
1299: .get("OTHER");
1300: }
1301: }
1302:
1303: public SQLDataType getSQLDataType() {
1304: return this .datatype;
1305: }
1306: }
1307:
1308: private static class SQLDataType {
1309: private String[] params;
1310: private int paramCount;
1311: private int paramRequired;
1312: private String index;
1313: private String name;
1314: private String alias;
1315:
1316: SQLDataType(String index, int paramRequired, String alias,
1317: String name, String[] params) {
1318: this .params = params;
1319: this .paramCount = this .params.length;
1320: this .index = index;
1321: this .name = name;
1322: this .alias = alias;
1323: this .paramRequired = paramRequired;
1324: }
1325:
1326: SQLDataType(String index, int paramRequired, String alias,
1327: String name) {
1328: this .params = null;
1329: this .paramCount = 0;
1330: this .index = index;
1331: this .name = name;
1332: this .alias = alias;
1333: this .paramRequired = 0;
1334: }
1335: }
1336:
1337: public static class Table {
1338:
1339: private String name;
1340: private String comment;
1341: private String scheme;
1342: private TreeMap relations = new TreeMap();
1343: private TreeMap columns = new TreeMap();
1344: private TreeMap indices = new TreeMap();
1345: private TreeMap primaryKeys = new TreeMap();
1346:
1347: public TreeMap getPrimaryKeys() {
1348: return this .primaryKeys;
1349: }
1350:
1351: public void addPrimaryKey(Column pk) {
1352: this .primaryKeys.put(pk.getName(), pk);
1353: }
1354:
1355: public String getName() {
1356: return this .name;
1357: }
1358:
1359: public void setName(String n) {
1360: this .name = n;
1361: }
1362:
1363: public String getComment() {
1364: return this .comment;
1365: }
1366:
1367: public void setComment(String c) {
1368: this .comment = c;
1369: }
1370:
1371: public String getScheme() {
1372: return this .scheme;
1373: }
1374:
1375: public void setScheme(String s) {
1376: this .scheme = s;
1377: }
1378:
1379: public void setRelation(Relation r) {
1380: String id = r.getID();
1381: Workbench2Designer.relations.put(id, r);
1382: }
1383:
1384: public TreeMap getRelations() {
1385: return Workbench2Designer.relations;
1386: }
1387:
1388: public TreeMap getIndices() {
1389: return this .indices;
1390: }
1391:
1392: public void setIndex(Index index) {
1393: String id = index.getID();
1394: this .indices.put(id, index);
1395: }
1396:
1397: public void addColumn(Column c) {
1398: this .columns.put(c.getName(), c);
1399: }
1400:
1401: public TreeMap getColumns() {
1402: return this .columns;
1403: }
1404: }
1405:
1406: public static class Column {
1407: private String name;
1408: private String comment = "";
1409: private SQLDataTypeStatement dataTypeStatement;
1410: private String table;
1411: private boolean primaryKey = false;
1412: private String defaultValue = "";
1413: private int position;
1414: private boolean nullable = false;
1415: private boolean foreignKey = false;
1416:
1417: public String getTable() {
1418: return this .table;
1419: }
1420:
1421: public void setTable(String tbl) {
1422: this .table = tbl;
1423: }
1424:
1425: public String getName() {
1426: return this .name;
1427: }
1428:
1429: public void setName(String n) {
1430: this .name = n;
1431: }
1432:
1433: public String getComment() {
1434: return this .comment;
1435: }
1436:
1437: public void setComment(String c) {
1438: this .comment = c;
1439: }
1440:
1441: public String getDefaultValue() {
1442: return this .comment;
1443: }
1444:
1445: public void setDefaultValue(String dV) {
1446: this .defaultValue = dV;
1447: }
1448:
1449: public int getPosition() {
1450: return this .position;
1451: }
1452:
1453: public void setPosition(int p) {
1454: this .position = p;
1455: }
1456:
1457: public void setDataTypeStatement(SQLDataTypeStatement statement) {
1458: this .dataTypeStatement = statement;
1459: }
1460:
1461: public SQLDataTypeStatement getDataTypeStatement() {
1462: return this .dataTypeStatement;
1463: }
1464:
1465: public boolean isForeignKey() {
1466: return this .foreignKey;
1467: }
1468:
1469: public void setForeignKey() {
1470: this .foreignKey = true;
1471: }
1472:
1473: public boolean isPrimaryKey() {
1474: return this .primaryKey;
1475: }
1476:
1477: public void setPrimaryKey() {
1478: this .primaryKey = true;
1479: }
1480:
1481: public boolean isNullable() {
1482: return this .nullable;
1483: }
1484:
1485: public void setNullable() {
1486: this .nullable = true;
1487: }
1488: }
1489:
1490: public static class Relation {
1491: private String destTable;
1492: private String srcTable;
1493: private String operator;
1494: private TreeSet constraints = new TreeSet();
1495: private String onUpdate;
1496: private String onDelete;
1497: private String id;
1498: private int relDirection;
1499: private String relName;
1500:
1501: public void setOperator(String operator) {
1502: this .operator = operator;
1503: }
1504:
1505: public String getOperator() {
1506: return this .operator;
1507: }
1508:
1509: public String getDestTable() {
1510: return this .destTable;
1511: }
1512:
1513: public void setDestTable(String dT) {
1514: this .destTable = dT;
1515: }
1516:
1517: public String getSrcTable() {
1518: return this .srcTable;
1519: }
1520:
1521: public void setSrcTable(String sT) {
1522: this .srcTable = sT;
1523: }
1524:
1525: public TreeSet getConstraints() {
1526: return this .constraints;
1527: }
1528:
1529: public String getConstraintStatement() {
1530: String ret = "";
1531: Iterator it = this .constraints.iterator();
1532: while (it.hasNext()) {
1533: ret = ret + it.next() + "\\n";
1534: }
1535: return ret;
1536: }
1537:
1538: void addConstraint(String f1, String f2) {
1539: this .constraints.add(f1 + this .operator + f2);
1540: }
1541:
1542: public String getOnUpdate() {
1543: return this .onUpdate;
1544: }
1545:
1546: public void setOnUpdate(String upd) {
1547: this .onUpdate = upd;
1548: }
1549:
1550: public String getOnDelete() {
1551: return this .onDelete;
1552: }
1553:
1554: public void setOnDelete(String del) {
1555: this .onDelete = del;
1556: }
1557:
1558: public String getID() {
1559: return this .id;
1560: }
1561:
1562: public void setID(String iD) {
1563: this .id = iD;
1564: }
1565:
1566: public int getRelDirection() {
1567: return this .relDirection;
1568: }
1569:
1570: public void setRelDirection(int rD) {
1571: this .relDirection = rD;
1572: }
1573:
1574: public String getRelName() {
1575: return this .relName;
1576: }
1577:
1578: public void setRelName(String rN) {
1579: this .relName = rN;
1580: }
1581: }
1582:
1583: public static class Index {
1584: private String id;
1585: private String name;
1586: private LinkedList columns = new LinkedList();
1587: private boolean unique = false;
1588: private boolean primaryKey = false;
1589:
1590: String getID() {
1591: return this .id;
1592: }
1593:
1594: void setID(String iD) {
1595: this .id = iD;
1596: }
1597:
1598: public void setName(String name) {
1599: this .name = name;
1600: }
1601:
1602: public String getName() {
1603: return this .name;
1604: }
1605:
1606: public void addColumn(String colName) {
1607: this .columns.add(colName);
1608: }
1609:
1610: public LinkedList getColumns() {
1611: return this .columns;
1612: }
1613:
1614: public void setUnique(boolean unique) {
1615: this .unique = unique;
1616: }
1617:
1618: public boolean isUnique() {
1619: return this .unique;
1620: }
1621:
1622: public void setPrimaryKey(boolean flag) {
1623: this .primaryKey = flag;
1624: }
1625:
1626: public boolean isPrimaryKey() {
1627: return this .primaryKey;
1628: }
1629: }
1630:
1631: public static class MalformedSourceException extends Exception {
1632: MalformedSourceException(String msg, Throwable thrw) {
1633: super(msg, thrw);
1634: }
1635: }
1636:
1637: }
|