001    package com.sptci.rwt.webui;
002    
003    import java.sql.SQLException;
004    
005    import nextapp.echo2.app.Button;
006    import nextapp.echo2.app.event.ActionEvent;
007    
008    import com.sptci.echo2.Configuration;
009    import com.sptci.echo2.Listener;
010    import com.sptci.rwt.BatchQueryExecutor;
011    import com.sptci.rwt.Rows;
012    
013    /**
014     * The listener for the {@link QueryExecutorView} used to interact with
015     * the {@link com.sptci.rwt.QueryExecutor} class.
016     *
017     * <p>&copy; Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
018     * @author Rakesh Vidyadharan 2007-10-12
019     * @version $Id: ExecuteBatchQueryListener.java 4123 2008-05-25 21:49:01Z rakesh $
020     */
021    public class ExecuteBatchQueryListener extends Listener<MainController>
022    {
023      /**
024       * Create a new instance of the listener using the specified {@link
025       * com.sptci.echo2.Controller} to interact with the application.
026       *
027       * @param controller The controller to use.
028       */
029      public ExecuteBatchQueryListener( MainController controller )
030      {
031        super( controller );
032      }
033    
034      /**
035       * The {@link nextapp.echo2.app.event.ActionListener} implementation.
036       * Submit the entered query to the database server for execution.  Handle
037       * query execution in the background to enable cancellation of the
038       * query.
039       *
040       * @param event The action event that was triggered by the user.
041       */
042      public void actionPerformed( ActionEvent event )
043      {
044        final Button button = (Button) event.getSource();
045        button.setEnabled( false );
046    
047        try
048        {
049          final BatchQueryExecutorView view =
050            (BatchQueryExecutorView) controller.getParentView( button );
051          view.reset();
052          processQuery( view );
053        }
054        catch ( Throwable t )
055        {
056          controller.processFatalException( Configuration.getString(
057                this, "error" ), t );
058        }
059        finally
060        {
061          button.setEnabled( true );
062        }
063      }
064    
065      /**
066       * Process the query entered in {@link QueryExecutorView#query} field.
067       *
068       * @param view The view from which this action was triggered.
069       * @throws SQLException If errors are encountered while executing
070       *   the query.
071       */
072      private void processQuery( final BatchQueryExecutorView view )
073        throws SQLException
074      {
075        final String query = view.getQuery();
076        final int maxResults = view.getMaxResults();
077        final int maxColumnLength = view.getMaxColumnLength();
078    
079        if ( query.length() > 0 )
080        {
081          view.addToHistory( query );
082          view.reset();
083    
084          BatchQueryExecutor executor =
085            new BatchQueryExecutor( view.getConnectionManager() );
086          int count = 0;
087          for ( Rows rows : executor.execute(
088              query, maxResults, maxColumnLength ) )
089          {
090            RowsTableModel model = new RowsTableModel( rows );
091            view.addResults( "Results " + ++count, model );
092          }
093        }
094      }
095    }