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.sql.TableColumnInfo;
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:
040: /**
041: * Implements showing a list of columns for a selected table to the
042: * user and making the ones that are selected become the primary key for
043: * the table
044: *
045: * @author rmmannin
046: *
047: */
048: public class AddPrimaryKeyCommand extends AbstractRefactoringCommand {
049:
050: /** Logger for this class. */
051: private final static ILogger log = LoggerController
052: .createLogger(RemoveColumnCommand.class);
053:
054: /** Internationalized strings for this class. */
055: private static final StringManager s_stringMgr = StringManagerFactory
056: .getStringManager(AddPrimaryKeyCommand.class);
057:
058: /**
059: * Ctor specifying the current session.
060: */
061: public AddPrimaryKeyCommand(ISession session,
062: IDatabaseObjectInfo[] info) {
063: super (session, info);
064: }
065:
066: /**
067: * Execute this command.
068: */
069: public void execute() {
070: if (!(_info[0] instanceof ITableInfo)) {
071: return;
072: }
073:
074: try {
075: ITableInfo ti = (ITableInfo) _info[0];
076: if (tableHasPrimaryKey()) {
077: //i18n[AddPrimaryKeyCommand.primaryKeyExists=Table {0} already
078: //has a primary key]
079: String msg = s_stringMgr.getString(
080: "AddPrimaryKeyCommand.primaryKeyExists", ti
081: .getSimpleName());
082: _session.showErrorMessage(msg);
083: return;
084: }
085:
086: super .showColumnListDialog(
087: new AddPrimaryKeyActionListener(),
088: new ShowSQLListener(),
089: ColumnListDialog.ADD_PRIMARY_KEY_MODE);
090: } catch (Exception e) {
091: _session.showErrorMessage(e);
092: log.error("Unexpected exception " + e.getMessage(), e);
093: }
094:
095: }
096:
097: protected void getSQLFromDialog(SQLResultListener listener) {
098: TableColumnInfo[] columns = columnListDialog
099: .getSelectedColumnList();
100: HibernateDialect dialect = null;
101:
102: String[] result = null;
103: try {
104: dialect = DialectFactory.getDialect(
105: DialectFactory.DEST_TYPE, _session.getApplication()
106: .getMainFrame(), _session.getMetaData());
107:
108: String pkName = columnListDialog.getPrimaryKeyName();
109:
110: result = dialect.getAddPrimaryKeySQL(pkName, columns,
111: (ITableInfo) _info[0]);
112: } catch (UnsupportedOperationException e2) {
113: //i18n[AddPrimaryKeyCommand.unsupportedOperationMsg=The {0}
114: //dialect doesn't support adding primary keys to tables]
115: String msg = s_stringMgr.getString(
116: "AddPrimaryKeyCommand.unsupportedOperationMsg",
117: dialect.getDisplayName());
118:
119: _session.showErrorMessage(msg);
120: } catch (UserCancelledOperationException e) {
121: // user cancelled selecting a dialog. do nothing?
122: }
123: listener.finished(result);
124:
125: }
126:
127: private class ShowSQLListener implements ActionListener,
128: SQLResultListener {
129: public void actionPerformed(ActionEvent e) {
130: getSQLFromDialog(this );
131: }
132:
133: public void finished(String[] addPKSQLs) {
134: if (addPKSQLs == null || addPKSQLs.length == 0) {
135: // TODO: tell the user no changes
136: return;
137: }
138:
139: StringBuffer script = new StringBuffer();
140: for (int i = 0; i < addPKSQLs.length; i++) {
141: script.append(addPKSQLs[i]);
142: script.append(";\n\n");
143: }
144:
145: ErrorDialog sqldialog = new ErrorDialog(columnListDialog,
146: script.toString());
147: //i18n[AddPrimaryKeyCommand.sqlDialogTitle=Add Primary Key SQL]
148: String title = s_stringMgr
149: .getString("AddPrimaryKeyCommand.sqlDialogTitle");
150: sqldialog.setTitle(title);
151: sqldialog.setVisible(true);
152:
153: }
154: }
155:
156: private class AddPrimaryKeyActionListener implements
157: ActionListener, SQLResultListener {
158: public void finished(String[] addPKSQLs) {
159: CommandExecHandler handler = new CommandExecHandler(
160: _session);
161: if (addPKSQLs != null) {
162: for (int i = 0; i < addPKSQLs.length; i++) {
163: String addPKSQL = addPKSQLs[i];
164: log.info("AddPrimaryKeyCommand: executing SQL - "
165: + addPKSQL);
166: SQLExecuterTask executer = new SQLExecuterTask(
167: _session, addPKSQL, handler);
168: executer.run();
169: }
170: }
171: columnListDialog.setVisible(false);
172: }
173:
174: public void actionPerformed(ActionEvent e) {
175: if (columnListDialog == null) {
176: System.err.println("dialog was null");
177: return;
178: }
179: getSQLFromDialog(this);
180: }
181:
182: }
183:
184: }
|