Search in sources :

Example 11 with AtomicCriteria

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

the class JaffaTransactionMessageService method getFieldByNameValue.

/**
 * Get a TansactionField given its name and value.
 *
 * @param fieldName  the name of the field to return
 * @param fieldValue the value of the field to return
 * @return the TransactionField with the input fieldName and value
 * @throws FrameworkException
 */
@Override
public TransactionField getFieldByNameValue(String fieldName, String fieldValue) throws FrameworkException {
    UOW uow = null;
    TransactionField transactionField = null;
    try {
        uow = new UOW();
        Criteria criteria = new Criteria();
        criteria.setTable(TransactionFieldMeta.getName());
        AtomicCriteria ac = new AtomicCriteria();
        ac.addCriteria(TransactionFieldMeta.FIELD_NAME, fieldName);
        criteria.addAtomic(ac);
        criteria.addCriteria(TransactionFieldMeta.VALUE, fieldValue);
        for (Object result : uow.query(criteria)) {
            if (!(result instanceof TransactionField)) {
                continue;
            }
            transactionField = (TransactionField) result;
        }
    } finally {
        if (uow != null) {
            uow.close();
        }
    }
    return transactionField;
}
Also used : TransactionField(org.jaffa.transaction.domain.TransactionField) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) Criteria(org.jaffa.persistence.Criteria) TransactionFieldCriteria(org.jaffa.transaction.apis.data.TransactionFieldCriteria) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) TransactionCriteria(org.jaffa.transaction.apis.data.TransactionCriteria) UOW(org.jaffa.persistence.UOW)

Example 12 with AtomicCriteria

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

the class JaffaTransactionMessageService method getCountInboundOpenInProgress.

/**
 * Gets the count of inbound Transactions with the specified field-value, type and subType that
 * are in the Open or InProgress state.
 *
 * @param field   the field to check the value of
 * @param value   the input value of the field we are looking for
 * @param type    the type of the Transaction
 * @param subType the subType of the Transaction
 * @return the count of Transactions with the specified field-value, type and subType that are in the
 *         Open or inProgress state.
 * @throws FrameworkException
 */
@Override
public long getCountInboundOpenInProgress(String field, String value, String type, String subType) throws FrameworkException {
    UOW uow = null;
    long transactionCount = 0;
    try {
        uow = new UOW();
        AtomicCriteria ac = new AtomicCriteria();
        // we want Open and In Progress
        ac.addCriteria(TransactionMeta.STATUS, Transaction.Status.O.toString());
        ac.addOrCriteria(TransactionMeta.STATUS, Transaction.Status.I.toString());
        // add the field name and value
        Criteria joinCriteria = new Criteria();
        joinCriteria.setTable(TransactionFieldMeta.getName());
        joinCriteria.addInnerCriteria(TransactionFieldMeta.TRANSACTION_ID, TransactionMeta.ID);
        joinCriteria.addCriteria(TransactionFieldMeta.FIELD_NAME, field);
        joinCriteria.addCriteria(TransactionFieldMeta.VALUE, value);
        // add the type, subType and direction
        Criteria c = new Criteria();
        c.setTable(TransactionMeta.getName());
        c.addCriteria(TransactionMeta.TYPE, type);
        c.addCriteria(TransactionMeta.SUB_TYPE, subType);
        c.addCriteria(TransactionMeta.DIRECTION, Transaction.Direction.IN.toString());
        c.addAtomic(ac);
        c.addAggregate(joinCriteria);
        c.addFunction(Criteria.FUNCTION_COUNT, null, Criteria.ID_FUNCTION_COUNT);
        // return the count, or zero
        Iterator itr = uow.query(c).iterator();
        if (itr.hasNext()) {
            Number count = (Number) ((Map) itr.next()).get(Criteria.ID_FUNCTION_COUNT);
            transactionCount = count.longValue();
        }
    } finally {
        if (uow != null) {
            uow.close();
        }
    }
    return transactionCount;
}
Also used : Iterator(java.util.Iterator) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) Criteria(org.jaffa.persistence.Criteria) TransactionFieldCriteria(org.jaffa.transaction.apis.data.TransactionFieldCriteria) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) TransactionCriteria(org.jaffa.transaction.apis.data.TransactionCriteria) UOW(org.jaffa.persistence.UOW)

Example 13 with AtomicCriteria

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

the class QueryStatementHelper method getClause.

private static String getClause(Criteria.CriteriaEntry criteriaEntry, Criteria criteria, ClassMetaData meta, String tableName, Counter tableCounter, StringBuffer fromBuf, String engineType, List<PreparedStatementArgument> psArguments, boolean indexArguments) throws IOException {
    StringBuffer buf = new StringBuffer();
    if (criteriaEntry instanceof Criteria.AtomicCriteriaEntry) {
        AtomicCriteria atomicCriteria = ((Criteria.AtomicCriteriaEntry) criteriaEntry).getEntry();
        Collection criteriaEntries = atomicCriteria.getCriteriaEntries();
        if (criteriaEntries != null) {
            for (Iterator i = criteriaEntries.iterator(); i.hasNext(); ) {
                Criteria.CriteriaEntry ce = (Criteria.CriteriaEntry) i.next();
                String clause = getClause(ce, criteria, meta, tableName, tableCounter, fromBuf, engineType, psArguments, indexArguments);
                if (clause != null && clause.length() > 0) {
                    if (buf.length() > 0)
                        buf.append(' ').append(logicLookAhead(ce)).append(' ');
                    buf.append('(').append(clause).append(')');
                }
            }
        }
        populateClauseWithAtomicAggregates(criteriaEntry, criteria, meta, tableName, buf, tableCounter, fromBuf, engineType, psArguments, indexArguments);
    } else {
        String name = formatSqlName(meta.getSqlName(criteriaEntry.getName()), engineType);
        if (name == null && criteriaEntry.getTableName() != null) {
            meta = ConfigurationService.getInstance().getMetaData(criteriaEntry.getTableName());
            if (meta != null)
                name = formatSqlName(meta.getSqlName(criteriaEntry.getName()), engineType);
        }
        if (name != null) {
            int operator = criteriaEntry.getOperator();
            if (criteriaEntry.getDual()) {
                String name2 = formatSqlName(meta.getSqlName((String) criteriaEntry.getValue()), engineType);
                if (name2 != null)
                    buf.append(parseDualOperator(tableName + '.' + name, operator, tableName + '.' + name2));
                else if (log.isDebugEnabled())
                    log.debug("Will ignore the unmapped criteria entry " + meta.getTable() + '.' + criteriaEntry.getValue());
            } else {
                Object value = criteriaEntry.getValue();
                String typeName = meta.getSqlType(criteriaEntry.getName());
                Integer rpad = meta.getRpad(criteriaEntry.getName());
                operator = checkForOperatorOverride(operator, typeName, value);
                if (meta != null && criteriaEntry.getTableName() != null)
                    buf.append(parseOperator(meta.getTable() + '.' + name, operator, value, typeName, engineType, rpad, psArguments, indexArguments));
                else
                    buf.append(parseOperator(tableName + '.' + name, operator, value, typeName, engineType, rpad, psArguments, indexArguments));
            }
        } else if (log.isDebugEnabled())
            log.debug("Will ignore the unmapped criteria entry " + meta.getTable() + '.' + criteriaEntry.getName());
    }
    return buf.toString();
}
Also used : AtomicCriteria(org.jaffa.persistence.AtomicCriteria) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) Criteria(org.jaffa.persistence.Criteria)

Example 14 with AtomicCriteria

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

the class QueryStatementHelper method hasAtomicAggregates.

// checks if any of the nested atomicCriteria has aggregates
private static boolean hasAtomicAggregates(Criteria criteria) {
    boolean hasAggregates = false;
    Collection criteriaEntries = criteria.getCriteriaEntries();
    if (criteriaEntries != null && criteriaEntries.size() > 0) {
        for (Iterator i = criteriaEntries.iterator(); i.hasNext() && !hasAggregates; ) {
            Criteria.CriteriaEntry criteriaEntry = (Criteria.CriteriaEntry) i.next();
            if (criteriaEntry instanceof Criteria.AtomicCriteriaEntry) {
                AtomicCriteria atomicCriteria = ((Criteria.AtomicCriteriaEntry) criteriaEntry).getEntry();
                if (atomicCriteria.getAggregates() != null && atomicCriteria.getAggregates().size() > 0) {
                    hasAggregates = true;
                } else {
                    hasAggregates = hasAtomicAggregates(atomicCriteria);
                }
            }
        }
    }
    return hasAggregates;
}
Also used : AtomicCriteria(org.jaffa.persistence.AtomicCriteria) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) Criteria(org.jaffa.persistence.Criteria)

Example 15 with AtomicCriteria

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

the class FinderTx method interpretDateTime.

private static AtomicCriteria interpretDateTime(String name, int operator, DateTime value) {
    AtomicCriteria atomic = new AtomicCriteria();
    // determine the start and end of the day
    DateTime dt1 = determineDateTimeLimit(value, true);
    DateTime dt2 = determineDateTimeLimit(value, false);
    if (dt1.equals(dt2)) {
        // an exact value has been passed !!!
        atomic.addCriteria(name, operator, value);
    } else {
        // now perform datatime interpretation based on the operator
        if (operator == Criteria.RELATIONAL_EQUALS) {
            atomic.addCriteria(name, Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO, dt1);
            atomic.addCriteria(name, Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO, dt2);
        } else if (operator == Criteria.RELATIONAL_NOT_EQUALS) {
            atomic.addCriteria(name, Criteria.RELATIONAL_SMALLER_THAN, dt1);
            atomic.addOrCriteria(name, Criteria.RELATIONAL_GREATER_THAN, dt2);
        } else if (operator == Criteria.RELATIONAL_GREATER_THAN) {
            atomic.addCriteria(name, Criteria.RELATIONAL_GREATER_THAN, dt2);
        } else if (operator == Criteria.RELATIONAL_SMALLER_THAN) {
            atomic.addCriteria(name, Criteria.RELATIONAL_SMALLER_THAN, dt1);
        } else if (operator == Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO) {
            atomic.addCriteria(name, Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO, dt1);
        } else if (operator == Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO) {
            atomic.addCriteria(name, Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO, dt2);
        } else {
            // this should never happen
            atomic.addCriteria(name, operator, value);
        }
    }
    return atomic;
}
Also used : AtomicCriteria(org.jaffa.persistence.AtomicCriteria) DateTime(org.jaffa.datatypes.DateTime)

Aggregations

AtomicCriteria (org.jaffa.persistence.AtomicCriteria)20 Criteria (org.jaffa.persistence.Criteria)14 UOW (org.jaffa.persistence.UOW)10 TransactionCriteria (org.jaffa.transaction.apis.data.TransactionCriteria)10 TransactionFieldCriteria (org.jaffa.transaction.apis.data.TransactionFieldCriteria)10 Transaction (org.jaffa.transaction.domain.Transaction)6 ArrayList (java.util.ArrayList)5 Iterator (java.util.Iterator)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 DateTime (org.jaffa.datatypes.DateTime)2 JaffaRulesFrameworkException (org.jaffa.rules.JaffaRulesFrameworkException)2 Serializable (java.io.Serializable)1 Collection (java.util.Collection)1 StringCriteriaField (org.jaffa.components.finder.StringCriteriaField)1 ApplicationException (org.jaffa.exceptions.ApplicationException)1 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)1