001: package net.sourceforge.squirrel_sql.plugins.refactoring.commands;
002:
003: import java.awt.event.ActionEvent;
004: import java.awt.event.ActionListener;
005: import java.sql.SQLException;
006: import java.util.ArrayList;
007:
008: import net.sourceforge.squirrel_sql.client.gui.db.ColumnDetailDialog;
009: import net.sourceforge.squirrel_sql.client.gui.db.ColumnListDialog;
010: import net.sourceforge.squirrel_sql.client.gui.mainframe.MainFrame;
011: import net.sourceforge.squirrel_sql.client.session.DefaultSQLExecuterHandler;
012: import net.sourceforge.squirrel_sql.client.session.ISession;
013: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
014: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
015: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
016: import net.sourceforge.squirrel_sql.fw.sql.PrimaryKeyInfo;
017: import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
018: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
019: import net.sourceforge.squirrel_sql.fw.util.ICommand;
020: import net.sourceforge.squirrel_sql.plugins.refactoring.gui.DropTableDialog;
021:
022: public abstract class AbstractRefactoringCommand implements ICommand {
023:
024: /** Current session */
025: protected ISession _session;
026:
027: /** Selected table(s) */
028: protected final IDatabaseObjectInfo[] _info;
029:
030: protected ColumnListDialog columnListDialog = null;
031:
032: protected ColumnDetailDialog columnDetailDialog = null;
033:
034: protected DropTableDialog dropTableDialog = null;
035:
036: protected String pkName = null;
037:
038: public AbstractRefactoringCommand(ISession session,
039: IDatabaseObjectInfo[] info) {
040: if (session == null) {
041: throw new IllegalArgumentException(
042: "ISession cannot be null");
043: }
044: if (info == null) {
045: throw new IllegalArgumentException(
046: "IDatabaseObjectInfo[] cannot be null");
047: }
048: _session = session;
049: _info = info;
050: }
051:
052: protected void showColumnListDialog(ActionListener oklistener,
053: ActionListener showSqlListener, int mode)
054: throws SQLException {
055:
056: ITableInfo ti = (ITableInfo) _info[0];
057: TableColumnInfo[] columns = getTableColumns(ti, mode);
058:
059: //Show the user a dialog with a list of columns and ask them to select
060: // one or more columns to drop
061: if (columnListDialog == null) {
062: columnListDialog = new ColumnListDialog(columns, mode);
063: columnListDialog.addColumnSelectionListener(oklistener);
064: columnListDialog.addEditSQLListener(new EditSQLListener());
065: columnListDialog.addShowSQLListener(showSqlListener);
066: MainFrame mainFrame = _session.getApplication()
067: .getMainFrame();
068: columnListDialog.setLocationRelativeTo(mainFrame);
069: columnListDialog.setMultiSelection();
070: }
071: columnListDialog.setTableName(ti.getQualifiedName());
072: if (mode == ColumnListDialog.ADD_PRIMARY_KEY_MODE) {
073: // Set a default primary key name based on the name of the table
074: String pkName = "PK_"
075: + columns[0].getTableName().toUpperCase();
076: columnListDialog.setPrimaryKeyName(pkName);
077: }
078: if (mode == ColumnListDialog.DROP_PRIMARY_KEY_MODE) {
079: SQLDatabaseMetaData md = _session.getSQLConnection()
080: .getSQLMetaData();
081: PrimaryKeyInfo[] infos = md.getPrimaryKey(ti);
082: String pkName = infos[0].getSimpleName();
083: columnListDialog.setPrimaryKeyName(pkName);
084: }
085: columnListDialog.setVisible(true);
086: }
087:
088: protected void showDropTableDialog(ActionListener oklistener,
089: ActionListener showSqlListener) {
090: if (dropTableDialog == null) {
091: ITableInfo[] tableInfos = new ITableInfo[_info.length];
092: for (int i = 0; i < tableInfos.length; i++) {
093: tableInfos[i] = (ITableInfo) _info[i];
094: }
095: dropTableDialog = new DropTableDialog(tableInfos);
096: dropTableDialog.addExecuteListener(oklistener);
097: dropTableDialog.addShowSQLListener(showSqlListener);
098: dropTableDialog.addEditSQLListener(new EditSQLListener());
099: MainFrame mainFrame = _session.getApplication()
100: .getMainFrame();
101: dropTableDialog.setLocationRelativeTo(mainFrame);
102: }
103: dropTableDialog.setVisible(true);
104: }
105:
106: protected void setPKName(String pkName) {
107: this .pkName = pkName;
108: }
109:
110: protected String getPKName() {
111: return this .pkName;
112: }
113:
114: private TableColumnInfo[] getTableColumns(ITableInfo ti, int mode)
115: throws SQLException {
116: SQLDatabaseMetaData md = _session.getSQLConnection()
117: .getSQLMetaData();
118: if (mode == ColumnListDialog.DROP_PRIMARY_KEY_MODE) {
119: ArrayList<TableColumnInfo> result = new ArrayList<TableColumnInfo>();
120: PrimaryKeyInfo[] pkCols = md.getPrimaryKey(ti);
121: TableColumnInfo[] colInfos = md.getColumnInfo(ti);
122: for (int i = 0; i < pkCols.length; i++) {
123: PrimaryKeyInfo pkInfo = pkCols[i];
124: setPKName(pkInfo.getSimpleName());
125: String pkColName = pkInfo.getQualifiedColumnName();
126: for (int j = 0; j < colInfos.length; j++) {
127: TableColumnInfo colInfo = colInfos[j];
128: if (colInfo.getSimpleName().equals(pkColName)) {
129: result.add(colInfo);
130: }
131: }
132: }
133: return result.toArray(new TableColumnInfo[result.size()]);
134: }
135: return _session.getSQLConnection().getSQLMetaData()
136: .getColumnInfo(ti);
137: }
138:
139: protected class CommandExecHandler extends
140: DefaultSQLExecuterHandler {
141: private boolean exceptionEncountered = false;
142:
143: public CommandExecHandler(ISession session) {
144: super (session);
145: }
146:
147: /* (non-Javadoc)
148: * @see net.sourceforge.squirrel_sql.client.session.DefaultSQLExecuterHandler#sqlExecutionException(java.lang.Throwable, java.lang.String)
149: */
150: public void sqlExecutionException(Throwable th,
151: String postErrorString) {
152: super .sqlExecutionException(th, postErrorString);
153: setExceptionEncountered(true);
154: }
155:
156: /**
157: * @param exceptionEncountered the exceptionEncountered to set
158: */
159: public void setExceptionEncountered(boolean exceptionEncountered) {
160: this .exceptionEncountered = exceptionEncountered;
161: }
162:
163: /**
164: * @return the exceptionEncountered
165: */
166: public boolean exceptionEncountered() {
167: return exceptionEncountered;
168: }
169: }
170:
171: /**
172: * The subclass should implement this so that the action listeners can get
173: * the SQL
174: * @param listener TODO
175: * @return
176: */
177: protected abstract void getSQLFromDialog(SQLResultListener listener);
178:
179: /**
180: *
181: *
182: *
183: */
184: protected class EditSQLListener implements ActionListener,
185: SQLResultListener {
186:
187: /* (non-Javadoc)
188: * @see net.sourceforge.squirrel_sql.plugins.refactoring.commands.SQLResultListener#finished(java.lang.String[])
189: */
190: public void finished(String[] sqls) {
191: if (sqls == null) {
192: return;
193: }
194: final StringBuffer sql = new StringBuffer();
195: for (int i = 0; i < sqls.length; i++) {
196: sql.append(sqls[i]);
197: if (i < sqls.length) {
198: sql.append("\n\n");
199: }
200: }
201: GUIUtils.processOnSwingEventThread(new Runnable() {
202: public void run() {
203: if (columnListDialog != null) {
204: columnListDialog.setVisible(false);
205: }
206: if (columnDetailDialog != null) {
207: columnDetailDialog.setVisible(false);
208: }
209: if (dropTableDialog != null) {
210: dropTableDialog.setVisible(false);
211: }
212: _session.getSQLPanelAPIOfActiveSessionWindow()
213: .appendSQLScript(sql.toString(), true);
214: _session
215: .selectMainTab(ISession.IMainPanelTabIndexes.SQL_TAB);
216: }
217: });
218: }
219:
220: public void actionPerformed(ActionEvent e) {
221: final SQLResultListener listener = this ;
222: _session.getApplication().getThreadPool().addTask(
223: new Runnable() {
224: public void run() {
225: getSQLFromDialog(listener);
226: }
227: });
228: }
229:
230: }
231:
232: protected boolean tableHasPrimaryKey() throws SQLException {
233: if (!(_info[0] instanceof ITableInfo)) {
234: return false;
235: }
236: ITableInfo ti = (ITableInfo) _info[0];
237: SQLDatabaseMetaData md = _session.getSQLConnection()
238: .getSQLMetaData();
239: PrimaryKeyInfo[] pks = md.getPrimaryKey(ti);
240: return (pks != null && pks.length > 0);
241: }
242:
243: }
|