001: package net.sourceforge.squirrel_sql.plugins.sqlval.action;
002:
003: /*
004: * Copyright (C) 2002-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library 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 GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; 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 javax.swing.JDialog;
024:
025: import net.sourceforge.squirrel_sql.fw.util.*;
026: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
027: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
028: import net.sourceforge.squirrel_sql.plugins.sqlval.LogonDialog;
029: import net.sourceforge.squirrel_sql.plugins.sqlval.SQLValidatorPlugin;
030: import net.sourceforge.squirrel_sql.plugins.sqlval.WebServicePreferences;
031: import net.sourceforge.squirrel_sql.plugins.sqlval.WebServiceSessionProperties;
032: import net.sourceforge.squirrel_sql.plugins.sqlval.cmd.ValidateSQLCommand;
033:
034: import net.sourceforge.squirrel_sql.client.IApplication;
035: import net.sourceforge.squirrel_sql.client.action.SquirrelAction;
036: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
037: import net.sourceforge.squirrel_sql.client.session.ISession;
038: import net.sourceforge.squirrel_sql.client.session.action.ISessionAction;
039: import net.sourceforge.squirrel_sql.client.session.properties.SessionProperties;
040:
041: /**
042: * This action will validate the current SQL.
043: *
044: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
045: */
046: public class ValidateSQLAction extends SquirrelAction implements
047: ISessionAction {
048: private static final StringManager s_stringMgr = StringManagerFactory
049: .getStringManager(ValidateSQLAction.class);
050:
051: /** Logger for this class. */
052: private final static ILogger s_log = LoggerController
053: .createLogger(ValidateSQLAction.class);
054:
055: /** Preferences. */
056: private final WebServicePreferences _prefs;
057:
058: /** Current plugin. */
059: private final SQLValidatorPlugin _plugin;
060:
061: /** Current session. */
062: private ISession _session;
063:
064: /**
065: * Ctor.
066: *
067: * @param app Application API.
068: * @param rsrc Resources to build this action from.
069: * @param prefs Plugin preferences.
070: * @param plugin Plugin
071: *
072: * @throws IllegalArgumentException
073: * Thrown if <TT>null</TT>WebServicePreferences</TT> passed.
074: */
075: public ValidateSQLAction(IApplication app, Resources rsrc,
076: WebServicePreferences prefs, SQLValidatorPlugin plugin) {
077: super (app, rsrc);
078: if (prefs == null) {
079: throw new IllegalArgumentException(
080: "WebServicePreferences == null");
081: }
082: _prefs = prefs;
083: _plugin = plugin;
084: }
085:
086: public void actionPerformed(ActionEvent evt) {
087: if (_session != null) {
088: final WebServiceSessionProperties sessionProps = _plugin
089: .getWebServiceSessionProperties(_session);
090: if (!sessionProps.getWebServiceSession().isOpen()) {
091: JDialog dlog = new LogonDialog(_session, _prefs,
092: sessionProps);
093: dlog.setVisible(true);
094: }
095:
096: if (sessionProps.getWebServiceSession().isOpen()) {
097: validateSQL();
098: }
099: }
100: }
101:
102: public void setSession(ISession session) {
103: _session = session;
104: }
105:
106: private void validateSQL() {
107: final ISQLPanelAPI api = _session.getSessionInternalFrame()
108: .getSQLPanelAPI();
109: final String sql = api.getSQLScriptToBeExecuted();
110: if (sql != null && sql.trim().length() > 0) {
111: final WebServiceSessionProperties wssProps = _plugin
112: .getWebServiceSessionProperties(_session);
113: final String stmtSep = _session.getQueryTokenizer()
114: .getSQLStatementSeparator();
115: final String solComment = _session.getQueryTokenizer()
116: .getLineCommentBegin();
117: final ValidationProps valProps = new ValidationProps(
118: _prefs, wssProps, sql, stmtSep, solComment,
119: _session);
120: new Executor(_session.getApplication(), valProps, _session
121: .getProperties()).execute();
122: } else {
123: // i18n[sqlval.noSql=No SQL specified]
124: _session.showErrorMessage(s_stringMgr
125: .getString("sqlval.noSql"));
126: }
127: }
128:
129: static final class ValidationProps {
130: final WebServicePreferences _prefs;
131: final WebServiceSessionProperties _sessionProps;
132: final String _sql;
133: final String _stmtSep;
134: final String _solComment;
135: final ISession _session;
136:
137: ValidationProps(WebServicePreferences prefs,
138: WebServiceSessionProperties sessionProps, String sql,
139: String stmtSep, String solComment, ISession session) {
140: super ();
141: _prefs = prefs;
142: _sessionProps = sessionProps;
143: _sql = sql;
144: _stmtSep = stmtSep;
145: _solComment = solComment;
146: _session = session;
147: }
148: }
149:
150: static class Executor implements ICommand {
151: private final IApplication _app;
152: private final ValidationProps _valProps;
153: private SessionProperties _sessionProperties;
154:
155: Executor(IApplication app, ValidationProps valProps,
156: SessionProperties sessionProperties) {
157: super ();
158: _app = app;
159: _valProps = valProps;
160: _sessionProperties = sessionProperties;
161: }
162:
163: public void execute() {
164: ValidateSQLCommand cmd = new ValidateSQLCommand(
165: _valProps._prefs, _valProps._sessionProps,
166: _valProps._sql, _valProps._stmtSep,
167: _valProps._solComment, _sessionProperties,
168: _valProps._session);
169: try {
170: cmd.execute();
171: _valProps._session.showMessage(cmd.getResults());
172: } catch (Throwable th) {
173: final String msg = "Error occured when talking to the web service";
174: s_log.error(msg, th);
175: _app.showErrorDialog(msg, th);
176: }
177: }
178: }
179: }
|