view geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/DefaultQueryExceutor.java @ 1127:ebeb56428409

Added license headers and license file. geo-backend/trunk@1261 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:52:22 +0000
parents f76541120bcb
children 81f0a5e66d71
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.geobackend.base.query;

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;

import de.intevation.gnv.geobackend.sde.datasources.Uncacheable;

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

import java.util.Collection;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * This is an Standard Implementation of the Interface QueryExecutor.
 * It fetchs the Query from the Querycontainer an put the Filtervalues into the Query.
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 *
 */
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);
                }

                if (log.isDebugEnabled()) {
                    log.debug("############   QUERY ##################");
                    log.debug(queryString);
                    log.debug("#######################################");
                }

                returnValue = cachedResults(queryString);

                if (returnValue != null) {
                    return returnValue;
                }

                boolean cacheable             = true;
                Connection connection         = null;
                ConnectionPool connectionPool = ConnectionPoolFactory.getInstance().getConnectionPool();
                try {
                    // Submit the Query
                    connection = connectionPool.getConnection(this.connectionID);
                    if (connection != null){
                        Statement stmt = connection.createStatement();
                        Date start = new Date();
                        ResultSet rs = stmt.executeQuery(queryString);
                        Date end = new Date();
                        log.info("-> Database query took " +
                            (end.getTime() - start.getTime()) + " ms.");

                        cacheable = rs instanceof Uncacheable ? false : true;

                        returnValue = super.createResultCollection(rs);
                    }else{
                        log.error("Could not establish Databaseconnection.");
                        throw new QueryException("Could not establish Databaseconnection.");
                    }

                    if (cacheable) {
                        log.debug("Elements are cacheable.");
                        cacheResults(queryString, returnValue);
                    }
                    else {
                        log.debug("Elements are NOT cacheable.");
                    }

                } 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