001: package net.sourceforge.squirrel_sql.plugins.dbcopy.actions;
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: import java.awt.event.ActionEvent;
022:
023: import net.sourceforge.squirrel_sql.client.IApplication;
024: import net.sourceforge.squirrel_sql.client.action.SquirrelAction;
025: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
026: import net.sourceforge.squirrel_sql.client.session.ISession;
027: import net.sourceforge.squirrel_sql.client.session.action.ISessionAction;
028: import net.sourceforge.squirrel_sql.fw.dialects.DialectFactory;
029: import net.sourceforge.squirrel_sql.fw.dialects.HibernateDialect;
030: import net.sourceforge.squirrel_sql.fw.dialects.UserCancelledOperationException;
031: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectInfo;
032: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
033: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
034: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
035: import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
036: import net.sourceforge.squirrel_sql.fw.util.Resources;
037: import net.sourceforge.squirrel_sql.fw.util.StringManager;
038: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
039: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
040: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
041: import net.sourceforge.squirrel_sql.plugins.dbcopy.DBCopyPlugin;
042: import net.sourceforge.squirrel_sql.plugins.dbcopy.SessionInfoProvider;
043: import net.sourceforge.squirrel_sql.plugins.dbcopy.commands.PasteTableCommand;
044:
045: public class PasteTableAction extends SquirrelAction implements
046: ISessionAction {
047:
048: /** Current plugin. */
049: private final SessionInfoProvider sessionInfoProv;
050:
051: /** The IApplication that we can use to display error dialogs */
052: private IApplication app = null;
053:
054: /** Logger for this class. */
055: private final static ILogger log = LoggerController
056: .createLogger(PasteTableAction.class);
057:
058: /** Internationalized strings for this class */
059: private static final StringManager s_stringMgr = StringManagerFactory
060: .getStringManager(PasteTableAction.class);
061:
062: /**
063: * Creates a new SQuirreL action that gets fired whenever the user chooses
064: * the paste operation.
065: *
066: * @param app
067: * @param rsrc
068: * @param plugin
069: */
070: public PasteTableAction(IApplication app, Resources rsrc,
071: DBCopyPlugin plugin) {
072: super (app, rsrc);
073: this .app = app;
074: sessionInfoProv = plugin;
075: }
076:
077: /* (non-Javadoc)
078: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
079: */
080: public void actionPerformed(ActionEvent evt) {
081: ISession destSession = sessionInfoProv.getCopyDestSession();
082: IObjectTreeAPI api = destSession
083: .getObjectTreeAPIOfActiveSessionWindow();
084: if (api == null) {
085: return;
086: }
087: IDatabaseObjectInfo[] dbObjs = api.getSelectedDatabaseObjects();
088: if (dbObjs.length > 1) {
089: sessionInfoProv.setDestSelectedDatabaseObject(null);
090: //i18n[PasteTableAction.error.multischemapaste=The paste
091: //operation may only be applied to one schema at a time]
092: String msg = s_stringMgr
093: .getString("PasteTableAction.error.multischemapaste");
094: app.showErrorDialog(msg);
095:
096: return;
097: } else {
098: // When the user pastes on a TABLE label which is located under a
099: // schema/catalog, build the schema DatabaseObjectInfo.
100: if (DatabaseObjectType.TABLE_TYPE_DBO.equals(dbObjs[0]
101: .getDatabaseObjectType())) {
102: IDatabaseObjectInfo tableLabelInfo = dbObjs[0];
103: ISQLConnection destCon = destSession.getSQLConnection();
104: SQLDatabaseMetaData md = null;
105: if (destCon != null) {
106: md = destCon.getSQLMetaData();
107: }
108: IDatabaseObjectInfo schema = new DatabaseObjectInfo(
109: null, tableLabelInfo.getSchemaName(),
110: tableLabelInfo.getSchemaName(),
111: DatabaseObjectType.SCHEMA, md);
112: sessionInfoProv.setDestSelectedDatabaseObject(schema);
113: } else {
114: sessionInfoProv
115: .setDestSelectedDatabaseObject(dbObjs[0]);
116: }
117:
118: }
119:
120: try {
121: IDatabaseObjectInfo info = sessionInfoProv
122: .getDestSelectedDatabaseObject();
123: if (info == null || destSession == null) {
124: return;
125: }
126: if (!checkSession(destSession, info)) {
127: return;
128: }
129: } catch (UserCancelledOperationException e) {
130: return;
131: }
132: if (sessionInfoProv.getCopySourceSession() == null) {
133: return;
134: }
135: if (!sourceDestSchemasDiffer()) {
136: // TODO: tell the user that the selected destination schema is
137: // the same as the source schema.
138: //monitor.showMessageDialog(...)
139: return;
140: }
141: new PasteTableCommand(sessionInfoProv).execute();
142: }
143:
144: /**
145: * Set the current session.
146: *
147: * @param session The current session.
148: */
149: public void setSession(ISession session) {
150: sessionInfoProv.setDestCopySession(session);
151: }
152:
153: /**
154: * This a work-around for the fact that some databases in SQuirreL show
155: * "schemas" as catalogs (MySQL) while most others show them as "schemas".
156: * If we restrict the Paste menu-item to schemas, then it won't appear in
157: * the context menu in the MySQL object tree. However, if add catalogs to
158: * the list of database objects that the paste menu item appears in, then
159: * we must be careful not to attempt the copy operation on databases where
160: * schema != catalog.(Otherwise the copy operation will fail as the qualified
161: * name will be [catalog].[tablename] instead of [schema].[tablename]
162: *
163: * @param session
164: * @param dbObjs
165: *
166: * @return true if it is ok to proceed with the copy operation; false otherwise.
167: */
168: private boolean checkSession(ISession session,
169: IDatabaseObjectInfo dbObj)
170: throws UserCancelledOperationException {
171: if (session == null || dbObj == null) {
172: return true;
173: }
174: String typeName = dbObj.getDatabaseObjectType().getName();
175:
176: log.debug("PasteTableAction.checkSession: dbObj type="
177: + typeName + " name=" + dbObj.getSimpleName());
178:
179: HibernateDialect d = DialectFactory.getDialect(
180: DialectFactory.DEST_TYPE, session.getApplication()
181: .getMainFrame(), session.getMetaData());
182: if (!d.canPasteTo(dbObj)) {
183: //i18n[PasteTableAction.error.destdbobj=The destination database
184: //doesn't support copying tables into '{0}' objects.\n Please
185: //select a schema to paste into.]
186: String errmsg = s_stringMgr.getString(
187: "PasteTableAction.error.destdbobj",
188: new Object[] { typeName });
189: app.showErrorDialog(errmsg);
190: return false;
191: }
192: return true;
193: }
194:
195: /**
196: * Returns a boolean value indicating whether or not the source and
197: * destination sessions refer to the same schema.
198: *
199: * @return
200: */
201: private boolean sourceDestSchemasDiffer() {
202: //ISession sourceSession = sessionInfoProv.getCopySourceSession();
203: //ISession destSession = sessionInfoProv.getCopyDestSession();
204:
205: // TODO: check to be sure that the source and destination schemas are
206: // different. Abort if they are the same and inform the user.
207:
208: return true;
209: }
210: }
|