Search in sources :

Example 1 with QueryFailedException

use of org.jaffa.persistence.exceptions.QueryFailedException in project jaffa-framework by jaffa-projects.

the class QueryInterceptor method invoke.

/**
 *Performs the logic associated with querying the database. It will perform queries for each Criteria object in the PersistentTransaction's QUERY collection.
 * Ideally there will be just one Criteria object in the collection.
 * Control will be passed to the next interceptor in the chain, if any, else a Collection will be returned containing the Results of the last query.
 * @param pt The PersistentTransaction object, on which the Interceptor is to be executed.
 * @throws UOWException if any error occurs.
 * @return the output from the next Interceptor in the chain, if any, else a Collection will be returned containing the Results of the last query. However, for a Criteria having a StoredProcedure, the output will be null.
 */
public Object invoke(PersistentTransaction pt) throws UOWException {
    Object output = null;
    if (pt.getQuery() != null) {
        // check if the 1st entry in the criteria is a StoredPreocdure
        Criteria criteria = pt.getQuery();
        Object firstCriteriaElement = null;
        Collection criteriaEntries = criteria.getCriteriaEntries();
        if (criteriaEntries != null && criteriaEntries.size() > 0)
            firstCriteriaElement = criteria.getCriteriaEntries().iterator().next();
        if (firstCriteriaElement != null)
            firstCriteriaElement = ((Criteria.CriteriaEntry) firstCriteriaElement).getValue();
        if (firstCriteriaElement != null && firstCriteriaElement instanceof IStoredProcedure) {
            try {
                if (log.isDebugEnabled())
                    log.debug("Invoking JdbcBridge.executeStoredProcedure()");
                JdbcBridge.executeStoredProcedure((IStoredProcedure) firstCriteriaElement, criteria, pt.getDataSource());
            } catch (Exception e) {
                String str = "Error in executing the StoredProcedure: " + firstCriteriaElement;
                if (log.isDebugEnabled())
                    log.debug(str, e);
                throw new QueryFailedException(null, e);
            }
        } else {
            try {
                if (log.isDebugEnabled())
                    log.debug("Invoking JdbcBridge.executeQuery()");
                output = JdbcBridge.executeQuery(criteria, pt.getDataSource());
            } catch (Exception e) {
                String str = "Error in executing the query on : " + criteria.getTable();
                if (log.isDebugEnabled())
                    log.debug(str, e);
                throw new QueryFailedException(null, e);
            }
        }
        pt.setQuery(null);
    }
    // pass control to the next interceptor in the chain
    if (getNextInterceptor() != null) {
        if (log.isDebugEnabled())
            log.debug("Invoking the next Interceptor in the chain " + getNextInterceptor().getClass().getName());
        return getNextInterceptor().invoke(pt);
    } else {
        if (log.isDebugEnabled())
            log.debug("This is the end of the Interceptor chain");
        return output;
    }
}
Also used : QueryFailedException(org.jaffa.persistence.exceptions.QueryFailedException) Criteria(org.jaffa.persistence.Criteria) IStoredProcedure(org.jaffa.persistence.engines.jdbcengine.IStoredProcedure) UOWException(org.jaffa.persistence.exceptions.UOWException) QueryFailedException(org.jaffa.persistence.exceptions.QueryFailedException)

Aggregations

Criteria (org.jaffa.persistence.Criteria)1 IStoredProcedure (org.jaffa.persistence.engines.jdbcengine.IStoredProcedure)1 QueryFailedException (org.jaffa.persistence.exceptions.QueryFailedException)1 UOWException (org.jaffa.persistence.exceptions.UOWException)1