001: /**
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */package com.bostechcorp.cbesb.common.util.etl.util;
024:
025: import java.io.File;
026: import java.io.IOException;
027: import java.util.ArrayList;
028: import java.util.List;
029:
030: import org.apache.ddlutils.io.DatabaseIO;
031: import org.apache.ddlutils.model.Column;
032: import org.apache.ddlutils.model.Database;
033: import org.apache.ddlutils.model.Reference;
034: import org.apache.ddlutils.model.Table;
035:
036: import com.bostechcorp.cbesb.common.constant.DatabaseConstants;
037: import com.bostechcorp.cbesb.common.util.etl.model.ColumnElement;
038: import com.bostechcorp.cbesb.common.util.etl.model.CompareOperation;
039: import com.bostechcorp.cbesb.common.util.etl.model.CompareOperatorEnum;
040: import com.bostechcorp.cbesb.common.util.etl.model.LogicalOperatorEnum;
041:
042: /**
043: * @author LPS
044: *
045: */
046: public class DatabaseUtil {
047:
048: private static String XML_HEADER = "<?xml version=\"1.0\"?>\n"
049: + "<!DOCTYPE database SYSTEM \"http://db.apache.org/torque/dtd/database.dtd\">";
050:
051: public static Database readDatabaseFromXML(File file) {
052: return new DatabaseIO().read(file);
053: }
054:
055: public static void writeDatabaseToXML(Database db, String fileName)
056: throws IOException {
057: // FileWriter fstream=new FileWriter(fileName);;
058: // BufferedWriter out = new BufferedWriter(fstream);
059: // out.write(XML_HEADER);
060: // out.flush();
061: new DatabaseIO().write(db, fileName);
062: // fstream.flush();
063: // fstream.close();
064: }
065:
066: public static List<CompareOperation> referenceOperationGenerator(
067: Table draggedTable, Table containerTable) {
068: //data source and data map should share the same
069: //instances for Table/Column objects
070: List<CompareOperation> operationList = new ArrayList<CompareOperation>();
071:
072: for (int i = 0; i < draggedTable.getForeignKeyCount(); i++) {
073: if (draggedTable.getForeignKey(i).getForeignTable() == containerTable) //TODO == | equal(conatinerTable)
074: {
075: for (int j = 0; j < draggedTable.getForeignKey(i)
076: .getReferenceCount(); j++) {
077: Reference reference = draggedTable.getForeignKey(i)
078: .getReference(j);
079: Column foreignColumn = containerTable
080: .findColumn(reference
081: .getForeignColumnName());
082: if (reference.getForeignColumn() == foreignColumn) {
083: CompareOperation operation = new CompareOperation();
084: operation
085: .setLogicalOperator(LogicalOperatorEnum.AND);
086: operation.setLeftOperand(new ColumnElement(
087: reference.getLocalColumn(), reference
088: .getLocalColumn().getName(),
089: draggedTable.getName()));
090: operation.setRightOperand(new ColumnElement(
091: foreignColumn, foreignColumn.getName(),
092: containerTable.getName()));
093: operation
094: .setCompareOperator(CompareOperatorEnum.EQUAL);
095: }
096: }
097: }
098: }
099: return operationList;
100: }
101:
102: public static String getDabaseNameFromURL(String url, String dbType) {
103: String dbName = "Database";
104:
105: try {
106:
107: // url template---jdbc:oracle:thin:@[host][:port]:[database]
108: if (dbType.equalsIgnoreCase(DatabaseConstants.DB_ORACLE)) {
109: int pos = url.lastIndexOf(":");
110: if (pos > -1)
111: dbName = url.substring(pos + 1);
112: }
113: // url
114: // template---jdbc:microsoft:sqlserver://[host][:port];database=[database]
115: else if (dbType
116: .equalsIgnoreCase(DatabaseConstants.DB_MSSQLSERVER)) {
117: int pos = url.toLowerCase().indexOf("database=");
118: if (pos > 0) {
119: String temp = url.substring(pos + 9);
120: pos = temp.indexOf(";");
121: if (pos > -1) {
122: dbName = temp.substring(0, pos);
123: } else
124: dbName = temp.trim();
125: }
126:
127: }
128: // jdbc:mysql://[host][:port]/[database]
129: else if (dbType
130: .equalsIgnoreCase(DatabaseConstants.DB_MYSQL)) {
131: int pos = url.lastIndexOf("/");
132: if (pos > -1)
133: dbName = url.substring(pos + 1);
134:
135: }
136: // url template ---jdbc:derby://[host][:port]/[database]
137: else if (dbType
138: .equalsIgnoreCase(DatabaseConstants.DB_DERBY)) {
139: int pos = url.lastIndexOf("/");
140: if (pos > -1)
141: dbName = url.substring(pos + 1);
142:
143: }
144:
145: } catch (Exception e) {
146: //don't need do anything
147: dbName = "Database";
148: }
149: return dbName;
150: }
151:
152: // public static void main(String[] args)
153: // {
154: // JdbcConnectionFactory fac = new JdbcConnectionFactory("com.mysql.jdbc.Driver","jdbc:mysql://localhost/joomla","root","");
155: // try
156: // {
157: // Database db= DatabaseUtil.getDatabase(fac);
158: // System.out.println(db);
159: // System.out.println(db.getTables()[1].getName());
160: // DatabaseIO dbio = new DatabaseIO();
161: // //dbio.write(db, new OutputStreamWriter(System.out) );
162: // dbio.write(db, "c:\\asd\\db.db" );
163: // } catch (SQLException e)
164: // {
165: // e.printStackTrace();
166: // } catch (JDBCConnectionException e)
167: // {
168: // // TODO Auto-generated catch block
169: // e.printStackTrace();
170: // }
171: // }
172: }
|