001: package net.sourceforge.squirrel_sql.plugins.refactoring.commands;
002:
003: /*
004: * Copyright (C) 2006 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.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024:
025: import net.sourceforge.squirrel_sql.client.gui.db.ColumnListDialog;
026: import net.sourceforge.squirrel_sql.client.session.ISession;
027: import net.sourceforge.squirrel_sql.client.session.SQLExecuterTask;
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.gui.ErrorDialog;
032: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
033: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
034: import net.sourceforge.squirrel_sql.fw.util.StringManager;
035: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
036: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
037: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
038:
039: /**
040: * Implements showing a list of primay key columns for a selected table to the
041: * user, allowing the user to drop the primary key or view the SQL that will do
042: * this.
043: *
044: * @author manningr
045: *
046: */
047: public class DropPrimaryKeyCommand extends AbstractRefactoringCommand {
048:
049: /** Logger for this class. */
050: private final static ILogger log = LoggerController
051: .createLogger(RemoveColumnCommand.class);
052:
053: /** Internationalized strings for this class. */
054: private static final StringManager s_stringMgr = StringManagerFactory
055: .getStringManager(DropPrimaryKeyCommand.class);
056:
057: /**
058: * Ctor specifying the current session.
059: */
060: public DropPrimaryKeyCommand(ISession session,
061: IDatabaseObjectInfo[] info) {
062: super (session, info);
063: }
064:
065: /**
066: * Execute this command.
067: */
068: public void execute() {
069: if (!(_info[0] instanceof ITableInfo)) {
070: return;
071: }
072: ITableInfo ti = (ITableInfo) _info[0];
073: try {
074: if (!tableHasPrimaryKey()) {
075: //i18n[DropPrimaryKeyCommand.noKeyToDrop=Table {0} does not
076: //have a primary key to drop]
077: String msg = s_stringMgr.getString(
078: "DropPrimaryKeyCommand.noKeyToDrop", ti
079: .getSimpleName());
080: _session.showErrorMessage(msg);
081: return;
082: }
083: super .showColumnListDialog(
084: new DropPrimaryKeyActionListener(),
085: new ShowSQLListener(),
086: ColumnListDialog.DROP_PRIMARY_KEY_MODE);
087: } catch (Exception e) {
088: log.error("Unexpected exception " + e.getMessage(), e);
089: }
090:
091: }
092:
093: protected void getSQLFromDialog(SQLResultListener listener) {
094: HibernateDialect dialect = null;
095:
096: String result = null;
097: try {
098: dialect = DialectFactory.getDialect(
099: DialectFactory.DEST_TYPE, _session.getApplication()
100: .getMainFrame(), _session.getMetaData());
101: result = dialect.getDropPrimaryKeySQL(this .pkName,
102: columnListDialog.getTableName());
103: } catch (UnsupportedOperationException e2) {
104: //i18n[DropPrimaryKeyCommand.unsupportedOperationMsg=The {0}
105: //dialect doesn't support dropping primary keys]
106: String msg = s_stringMgr.getString(
107: "DropPrimaryKeyCommand.unsupportedOperationMsg",
108: dialect.getDisplayName());
109:
110: _session.showMessage(msg);
111: } catch (UserCancelledOperationException e) {
112: // user cancelled selecting a dialect. do nothing?
113: }
114: listener.finished(new String[] { result });
115:
116: }
117:
118: private class ShowSQLListener implements ActionListener,
119: SQLResultListener {
120:
121: public void finished(String[] sql) {
122: if (sql.length == 0) {
123: // TODO: tell the user no changes
124: return;
125: }
126: StringBuffer script = new StringBuffer();
127: for (int i = 0; i < sql.length; i++) {
128: script.append(sql[i]);
129: script.append(";\n\n");
130: }
131:
132: ErrorDialog sqldialog = new ErrorDialog(columnListDialog,
133: script.toString());
134: //i18n[DropPrimaryKeyCommand.sqlDialogTitle=Drop Primary Key SQL]
135: String title = s_stringMgr
136: .getString("DropPrimaryKeyCommand.sqlDialogTitle");
137: sqldialog.setTitle(title);
138: sqldialog.setVisible(true);
139: }
140:
141: public void actionPerformed(ActionEvent e) {
142: getSQLFromDialog(this );
143: }
144: }
145:
146: private class DropPrimaryKeyActionListener implements
147: ActionListener, SQLResultListener {
148: public void finished(String[] sqls) {
149: CommandExecHandler handler = new CommandExecHandler(
150: _session);
151:
152: for (int i = 0; i < sqls.length; i++) {
153: String sql = sqls[i];
154: log.info("DropPrimaryKeyCommand: executing SQL - "
155: + sql);
156: SQLExecuterTask executer = new SQLExecuterTask(
157: _session, sql, handler);
158: executer.run();
159: }
160: columnListDialog.setVisible(false);
161: }
162:
163: public void actionPerformed(ActionEvent e) {
164: if (columnListDialog == null) {
165: System.err.println("dialog was null");
166: return;
167: }
168: getSQLFromDialog(this);
169: }
170:
171: }
172:
173: }
|