001: package net.sourceforge.squirrel_sql.plugins.dbcopy.commands;
002:
003: /*
004: * Copyright (C) 2005 Rob Manning
005: * manningr@users.sourceforge.net
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or 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
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: import java.sql.SQLException;
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import net.sourceforge.squirrel_sql.client.gui.ProgessCallBackDialog;
027: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
028: import net.sourceforge.squirrel_sql.client.session.ISession;
029: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
030: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
031: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
032: import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
033: import net.sourceforge.squirrel_sql.fw.sql.SQLUtilities;
034: import net.sourceforge.squirrel_sql.fw.util.ICommand;
035: import net.sourceforge.squirrel_sql.fw.util.StringManager;
036: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
037: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
038: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
039: import net.sourceforge.squirrel_sql.plugins.dbcopy.DBCopyPlugin;
040: import net.sourceforge.squirrel_sql.plugins.dbcopy.util.DBUtil;
041:
042: public class CopyTableCommand implements ICommand {
043: /**
044: * Current session.
045: */
046: private ISession _session;
047:
048: /**
049: * Current plugin.
050: */
051: private final DBCopyPlugin _plugin;
052:
053: /** Logger for this class. */
054: private final static ILogger log = LoggerController
055: .createLogger(CopyTableCommand.class);
056:
057: /** Internationalized strings for this class */
058: private static final StringManager s_stringMgr = StringManagerFactory
059: .getStringManager(CopyTableCommand.class);
060:
061: static interface i18n {
062:
063: //i18n[CopyTablesCommand.progressDialogTitle=Analyzing FKs in Tables to Copy]
064: String PROGRESS_DIALOG_TITLE = s_stringMgr
065: .getString("CopyTablesCommand.progressDialogTitle");
066:
067: //i18n[CopyTablesCommand.loadingPrefix=Analyzing table:]
068: String LOADING_PREFIX = s_stringMgr
069: .getString("CopyTablesCommand.loadingPrefix");
070:
071: }
072:
073: /**
074: * Ctor specifying the current session.
075: */
076: public CopyTableCommand(ISession session, DBCopyPlugin plugin) {
077: super ();
078: _session = session;
079: _plugin = plugin;
080: }
081:
082: /**
083: * Execute this command. Save the session and selected objects in the plugin
084: * for use in paste command.
085: */
086: public void execute() {
087: IObjectTreeAPI api = _session
088: .getObjectTreeAPIOfActiveSessionWindow();
089: if (api != null) {
090: IDatabaseObjectInfo[] dbObjs = api
091: .getSelectedDatabaseObjects();
092: if (DatabaseObjectType.TABLE_TYPE_DBO.equals(dbObjs[0]
093: .getDatabaseObjectType())) {
094: String catalog = dbObjs[0].getCatalogName();
095: String schema = dbObjs[0].getSchemaName();
096: if (log.isDebugEnabled()) {
097: log.debug("CopyTableCommand.execute: catalog="
098: + catalog);
099: log.debug("CopyTableCommand.execute: schema="
100: + schema);
101: }
102: dbObjs = DBUtil.getTables(_session, catalog, schema,
103: null);
104: for (int i = 0; i < dbObjs.length; i++) {
105: ITableInfo info = (ITableInfo) dbObjs[i];
106: if (log.isDebugEnabled()) {
107: log.debug("dbObj[" + i + "] = "
108: + info.getSimpleName());
109: }
110: }
111: }
112:
113: _plugin.setCopySourceSession(_session);
114: final IDatabaseObjectInfo[] fdbObjs = dbObjs;
115: final SQLDatabaseMetaData md = _session.getSQLConnection()
116: .getSQLMetaData();
117: _session.getApplication().getThreadPool().addTask(
118: new Runnable() {
119: public void run() {
120: try {
121: getInsertionOrder(fdbObjs, md);
122: _plugin.setPasteMenuEnabled(true);
123: } catch (SQLException e) {
124: log.error("Unexected exception: ", e);
125: }
126: }
127: });
128:
129: }
130: }
131:
132: private void getInsertionOrder(IDatabaseObjectInfo[] dbObjs,
133: SQLDatabaseMetaData md) throws SQLException {
134:
135: // Only concerned about order when more than one table.
136: if (dbObjs.length > 1) {
137: List<ITableInfo> selectedTables = new ArrayList<ITableInfo>();
138: for (int i = 0; i < dbObjs.length; i++) {
139: selectedTables.add((ITableInfo) dbObjs[i]);
140: }
141:
142: ProgessCallBackDialog cb = new ProgessCallBackDialog(
143: _session.getApplication().getMainFrame(),
144: i18n.PROGRESS_DIALOG_TITLE, dbObjs.length);
145:
146: cb.setLoadingPrefix(i18n.LOADING_PREFIX);
147: selectedTables = SQLUtilities.getInsertionOrder(
148: selectedTables, md, cb);
149: cb.setVisible(false);
150:
151: _plugin.setSelectedDatabaseObjects(selectedTables
152: .toArray(new IDatabaseObjectInfo[dbObjs.length]));
153:
154: } else {
155: _plugin.setSelectedDatabaseObjects(dbObjs);
156: }
157: }
158:
159: }
|