Search in sources :

Example 76 with Criteria

use of org.jaffa.persistence.Criteria 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)

Example 77 with Criteria

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

the class JdbcBridge method executeQuery.

/**
 * Executes the query based on the Criteria object.
 * @param criteria The input Criteria.
 * @param dataSource The DataSource against which the query is to be executed.
 * @throws IOException if any error occurs while extracting the String from the criteria.
 * @throws SQLException if any database error occurs.
 * @throws PostLoadFailedException if any error is thrown in the PostLoad trigger of the persistent object.
 * @throws DataSourceCursorRuntimeException if any error occurs while molding the row into the Persistent object.
 * @return a Collection of Persistent objects as a result of the query.
 */
public static Collection executeQuery(Criteria criteria, DataSource dataSource) throws IOException, SQLException, PostLoadFailedException, DataSourceCursorRuntimeException {
    ClassMetaData classMetaData = criteria.getTable() != null ? ConfigurationService.getInstance().getMetaData(criteria.getTable()) : null;
    // The optimization will also be used, if the criteria contains a count(*) for performing an existence-check based on the primary-key
    if (usePreparedStatement(dataSource) && hasPKCriteriaOnly(criteria, classMetaData)) {
        if (criteria.getFunctionEntries() == null || criteria.getFunctionEntries().size() == 0) {
            // Check the cache before executing the query
            IPersistent object = dataSource.lookupObjectCache(criteria);
            if (object != null) {
                if (log.isDebugEnabled())
                    log.debug("Found the cached object: " + object);
                Collection output = new LinkedList();
                output.add(object);
                return output;
            }
            if (log.isDebugEnabled())
                log.debug("Optimized to use the PreparedStatement for querying by primary-key");
            return executeFindByPKWithPreparedStatement(criteria, dataSource, classMetaData);
        } else if (criteria.getFunctionEntries().size() == 1) {
            Criteria.FunctionEntry fe = (Criteria.FunctionEntry) criteria.getFunctionEntries().iterator().next();
            if (fe.getName() == null && fe.getFunction() == Criteria.FUNCTION_COUNT) {
                // Check the cache before executing the query
                IPersistent object = dataSource.lookupObjectCache(criteria);
                if (object != null) {
                    if (log.isDebugEnabled())
                        log.debug("Found the cached object for existence-check by primary-key: " + object);
                    Collection output = new LinkedList();
                    Map map = new HashMap();
                    map.put(fe.getId(), 1);
                    output.add(map);
                    return output;
                }
                if (log.isDebugEnabled())
                    log.debug("Optimized to use the PreparedStatement for existence-check by primary-key");
                return executeExistsWithPreparedStatement(criteria, dataSource, classMetaData);
            }
        }
    }
    // Utilize the pagingPlugin if the Criteria contains values for the firstResult and/or maxResults properties
    IPagingPlugin pagingPlugin = createPagingPlugin(criteria, dataSource.getEngineType());
    if (usePreparedStatement(dataSource)) {
        // Perform a query using a PreparedStatement
        PreparedStatement pstmt = QueryStatementHelper.getPreparedStatement(criteria, dataSource, pagingPlugin);
        return dataSource.executeQuery(pstmt, classMetaData, criteria, (criteria.getLocking() == Criteria.LOCKING_PARANOID ? QUERY_TIMEOUT_FOR_LOCKING : 0), pagingPlugin);
    } else {
        // Perform a query using a regular Statement
        String sql = QueryStatementHelper.getSQL(criteria, dataSource, pagingPlugin);
        return dataSource.executeQuery(sql, classMetaData, criteria, (criteria.getLocking() == Criteria.LOCKING_PARANOID ? QUERY_TIMEOUT_FOR_LOCKING : 0), pagingPlugin);
    }
}
Also used : IPersistent(org.jaffa.persistence.IPersistent) PreparedStatement(java.sql.PreparedStatement) Criteria(org.jaffa.persistence.Criteria) IPagingPlugin(org.jaffa.persistence.engines.jdbcengine.paging.IPagingPlugin) ClassMetaData(org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData)

Example 78 with Criteria

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

the class MoldingService method getFunctionQueryMap.

/**
 * Transfers the contents of the ResultSet into a Map for a query involving functions.
 * @return a Map containing the result of a query involving functions.
 * @param criteria The criteria used for the query.
 * @param classMetaData The ClassMetaData object
 * @param rs The ResultSet
 * @param engineType The engine type as defined in init.xml
 * @throws SQLException if a database access error occurs.
 * @throws IOException if any error occurs in reading the data from the database.
 */
public static Map getFunctionQueryMap(Criteria criteria, ClassMetaData classMetaData, ResultSet rs, String engineType) throws SQLException, IOException {
    Map m = new LinkedHashMap();
    if (criteria.getGroupBys() != null) {
        for (Iterator i = criteria.getGroupBys().iterator(); i.hasNext(); ) {
            Criteria.GroupBy gb = (Criteria.GroupBy) i.next();
            String fieldName = gb.getName();
            String columnName = gb.getId();
            String typeName = classMetaData.getSqlType(fieldName);
            Object value = DataTranslator.getAppObject(rs, columnName, typeName, engineType);
            m.put(columnName, value);
        }
    }
    if (criteria.getFunctionEntries() != null) {
        for (Iterator i = criteria.getFunctionEntries().iterator(); i.hasNext(); ) {
            Criteria.FunctionEntry fe = (Criteria.FunctionEntry) i.next();
            String fieldName = fe.getName();
            String columnName = fe.getId();
            String typeName = null;
            if (fe.getFunction() == Criteria.FUNCTION_COUNT)
                typeName = Defaults.INTEGER;
            else if (fe.getFunction() == Criteria.FUNCTION_AVG)
                typeName = Defaults.DECIMAL;
            else if (fe.getFunction() == Criteria.FUNCTION_CURRENT_DATE_TIME)
                typeName = Defaults.DATETIME;
            else
                typeName = classMetaData.getSqlType(fieldName);
            Object value = DataTranslator.getAppObject(rs, columnName, typeName, engineType);
            m.put(columnName, value);
        }
    }
    return m;
}
Also used : AccessibleObject(java.lang.reflect.AccessibleObject) Criteria(org.jaffa.persistence.Criteria)

Example 79 with Criteria

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

the class AuditTransactionCriteria method returnQueryClauseAgainstTable.

/**
 * Returns the criteria object used for retrieving AuditTransaction records from the table.
 * @param c Criteria object
 * @return the criteria object used for retrieving AuditTransaction records.
 */
private Criteria returnQueryClauseAgainstTable(Criteria c) {
    c.setTable(AuditTransactionMeta.getName());
    FinderTx.addCriteria(getTransactionId(), AuditTransactionMeta.TRANSACTION_ID, c);
    FinderTx.addCriteria(getProcessName(), AuditTransactionMeta.PROCESS_NAME, c);
    FinderTx.addCriteria(getSubProcessName(), AuditTransactionMeta.SUB_PROCESS_NAME, c);
    FinderTx.addCriteria(getReason(), AuditTransactionMeta.REASON, c);
    FinderTx.addCriteria(getCreatedBy(), AuditTransactionMeta.CREATED_BY, c);
    FinderTx.addCriteria(getCreatedOn(), AuditTransactionMeta.CREATED_ON, c);
    // Join AuditTransactionObject with AuditTransaction
    if (getObjectName() != null) {
        Criteria oc = new Criteria();
        oc.setTable(AuditTransactionObjectMeta.getName());
        oc.addInnerCriteria(AuditTransactionObjectMeta.TRANSACTION_ID, AuditTransactionMeta.TRANSACTION_ID);
        oc.addCriteria(AuditTransactionObjectMeta.OBJECT_NAME, getObjectName());
        c.addAggregate(oc);
        // Join each AuditTransactionField with AuditTransactionObject
        if (getAuditTransactionFields() != null) {
            for (AuditTransactionFieldCriteria atf : getAuditTransactionFields()) {
                if (atf.getFieldName() != null) {
                    Criteria fc = new Criteria();
                    fc.setTable(AuditTransactionFieldMeta.getName());
                    fc.addInnerCriteria(AuditTransactionFieldMeta.OBJECT_ID, AuditTransactionObjectMeta.OBJECT_ID);
                    fc.addCriteria(AuditTransactionFieldMeta.FIELD_NAME, atf.getFieldName());
                    FinderTx.addCriteria(atf.getToValue(), AuditTransactionFieldMeta.TO_VALUE, fc);
                    FinderTx.addCriteria(atf.getChanged(), AuditTransactionFieldMeta.CHANGED, fc);
                    FinderTx.addCriteria(atf.getFlex(), AuditTransactionFieldMeta.FLEX, fc);
                    oc.addAggregate(fc);
                }
            }
        }
    }
    return c;
}
Also used : GraphCriteria(org.jaffa.soa.graph.GraphCriteria) Criteria(org.jaffa.persistence.Criteria)

Example 80 with Criteria

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

the class AuditTransactionCriteria method returnQueryClauseAgainstView.

/**
 * Returns the criteria object used for retrieving AuditTransaction records from the view.
 * @param c Criteria object
 * @return the criteria object used for retrieving AuditTransaction records.
 */
private Criteria returnQueryClauseAgainstView(Criteria c) {
    c.setTable(AuditTransactionViewMeta.getName());
    FinderTx.addCriteria(getTransactionId(), AuditTransactionViewMeta.TRANSACTION_ID, c);
    FinderTx.addCriteria(getProcessName(), AuditTransactionViewMeta.PROCESS_NAME, c);
    FinderTx.addCriteria(getSubProcessName(), AuditTransactionViewMeta.SUB_PROCESS_NAME, c);
    FinderTx.addCriteria(getReason(), AuditTransactionViewMeta.REASON, c);
    FinderTx.addCriteria(getChangeType(), AuditTransactionViewMeta.CHANGE_TYPE, c);
    FinderTx.addCriteria(getCreatedBy(), AuditTransactionViewMeta.CREATED_BY, c);
    FinderTx.addCriteria(getCreatedOn(), AuditTransactionViewMeta.CREATED_ON, c);
    if (getObjectName() != null || getObjectId() != null) {
        if (getObjectName() != null) {
            c.addCriteria(AuditTransactionViewMeta.OBJECT_NAME, getObjectName());
        } else {
            c.addCriteria(AuditTransactionViewMeta.OBJECT_ID, getObjectId());
        }
        // Join each AuditTransactionField with AuditTransactionObject
        if (getAuditTransactionFields() != null) {
            for (AuditTransactionFieldCriteria atf : getAuditTransactionFields()) {
                if (atf.getFieldName() != null) {
                    Criteria fc = new Criteria();
                    fc.setTable(AuditTransactionFieldMeta.getName());
                    fc.addInnerCriteria(AuditTransactionFieldMeta.OBJECT_ID, AuditTransactionViewMeta.OBJECT_ID);
                    fc.addCriteria(AuditTransactionFieldMeta.FIELD_NAME, atf.getFieldName());
                    FinderTx.addCriteria(atf.getToValue(), AuditTransactionFieldMeta.TO_VALUE, fc);
                    FinderTx.addCriteria(atf.getChanged(), AuditTransactionFieldMeta.CHANGED, fc);
                    FinderTx.addCriteria(atf.getFlex(), AuditTransactionFieldMeta.FLEX, fc);
                    c.addAggregate(fc);
                }
            }
        }
    }
    return c;
}
Also used : GraphCriteria(org.jaffa.soa.graph.GraphCriteria) Criteria(org.jaffa.persistence.Criteria)

Aggregations

Criteria (org.jaffa.persistence.Criteria)300 UOW (org.jaffa.persistence.UOW)136 AtomicCriteria (org.jaffa.persistence.AtomicCriteria)124 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)66 Iterator (java.util.Iterator)53 DomainObjectNotFoundException (org.jaffa.exceptions.DomainObjectNotFoundException)39 TransactionCriteria (org.jaffa.transaction.apis.data.TransactionCriteria)32 TransactionFieldCriteria (org.jaffa.transaction.apis.data.TransactionFieldCriteria)32 OrderByField (org.jaffa.components.finder.OrderByField)28 Map (java.util.Map)23 ArrayList (java.util.ArrayList)17 DateTime (org.jaffa.datatypes.DateTime)16 FrameworkException (org.jaffa.exceptions.FrameworkException)14 ApplicationException (org.jaffa.exceptions.ApplicationException)13 DuplicateKeyException (org.jaffa.exceptions.DuplicateKeyException)13 Transaction (org.jaffa.transaction.domain.Transaction)13 Collection (java.util.Collection)12 LinkedHashMap (java.util.LinkedHashMap)11 IPersistent (org.jaffa.persistence.IPersistent)11 HashMap (java.util.HashMap)9