view geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/DefaultQueryExceutor.java @ 132:5a583cff97ea

Implementation of the Datainfrastructure for fetching Data from different DataStores. geo-backend/trunk@12 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 04 Sep 2009 08:11:30 +0000
parents
children 9f2eaefe9dd4
line wrap: on
line source
/**
 *
 */
package de.intevation.gnv.geobackend.base.query;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;

import org.apache.log4j.Logger;

import de.intevation.gnv.geobackend.base.Result;
import de.intevation.gnv.geobackend.base.connectionpool.ConnectionPool;
import de.intevation.gnv.geobackend.base.connectionpool.ConnectionPoolFactory;
import de.intevation.gnv.geobackend.base.connectionpool.exception.ConnectionException;
import de.intevation.gnv.geobackend.base.query.container.QueryContainerFactory;
import de.intevation.gnv.geobackend.base.query.container.exception.QueryContainerException;
import de.intevation.gnv.geobackend.base.query.exception.QueryException;

/**
 * This is an Standard Implementation of the Interface QueryExecutor.
 * It fetchs the Query from the Querycontainer an put the Filtervalues into the Query.
 * @author Tim Englich <tim.englich@intevation.de>
 *
 */
public class DefaultQueryExceutor extends QueryExecutorBase{
    
    
    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = Logger.getLogger(DefaultQueryExceutor.class);
    
    /**
     * The ConnectionID identifing the Connection to use executing the Query.
     */
    private String connectionID = "N/N";
    
    /**
     * Constructor
     */
    public DefaultQueryExceutor() {
        super();
    }

    /**
     * @see de.intevation.gnv.geobackend.base.query.QueryExecutor#executeQuery(java.lang.String, java.lang.String[])
     */
    public Collection<Result> executeQuery(String queryID, String[] filter) throws QueryException {
        Collection<Result> returnValue = null;
        try {
            String queryString = QueryContainerFactory.getInstance().getQueryContainer().getQuery(queryID);
            if (queryString != null){
                if (filter != null && filter.length > 0){
                  //Insert the Filtervalues into the QueryString
                    queryString = super.setFilterValues(queryString, filter);
                }
                
                Connection connection = null;
                ConnectionPool connectionPool = ConnectionPoolFactory.getInstance().getConnectionPool();
                try {
                    // Submit the Query
                    connection = connectionPool.getConnection(this.connectionID);
                    if (connection != null){
                        Statement stmt = connection.createStatement();
                        ResultSet rs = stmt.executeQuery(queryString);
                        returnValue = super.createResultCollection(rs);
                        
                    }else{
                        log.error("Could not establish Databaseconnection.");
                        throw new QueryException("Could not establish Databaseconnection.");
                    }
                    
                } catch (ConnectionException e) {
                    log.error(e,e);
                    throw new QueryException("Could not establish Databaseconnection.",e);
                } catch (SQLException e) {
                    log.error(e,e);
                    throw new QueryException(e);
                }finally{
                    if (connection != null){
                        try {
                            connectionPool.closeConnection(connection);
                        } catch (ConnectionException e) {
                            log.error("Connection could not be returned to ConnectionPool.");
                            log.error(e,e);
                        }
                    }
                }
            }else{
                log.error("No QueryString defined for "+queryID);
                throw new QueryException("Cannot get the Querystring");
            }
        
        } catch (QueryContainerException e) {
            log.error(e,e);
            throw new QueryException("Cannot get the Querystring",e);
        }
        return returnValue;
    }
}

http://dive4elements.wald.intevation.org