Search in sources :

Example 6 with AtomicCriteria

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

the class JaffaTransactionMessageService method getTransactionsByFields.

/**
 * Gets all Transactions that have the specified fields with the input values.
 * If there are more than one value for a key, this will return fields that have any of the specified values.
 *
 * @param fields the field name/value pairs
 * @return a collection of Transactions that contain the specified fields with the input values
 * @throws FrameworkException
 */
@Override
public Collection<Transaction> getTransactionsByFields(HashMap<String, List<Serializable>> fields) throws FrameworkException {
    UOW uow = null;
    List<Transaction> results = new ArrayList<Transaction>();
    try {
        uow = new UOW();
        Criteria transactionCriteria = new Criteria();
        transactionCriteria.setTable(TransactionMeta.getName());
        // add each field in the field map to the query
        for (Map.Entry<String, List<Serializable>> field : fields.entrySet()) {
            // if there is no field or values defined, skip to the next entry
            if ((field.getKey() == null) || field.getKey().isEmpty() || field.getValue().isEmpty()) {
                continue;
            }
            // if the value list has one entry, create a criteria
            if (field.getValue().size() == 1) {
                Criteria singleValueCriteria = new Criteria();
                singleValueCriteria.setTable(TransactionFieldMeta.getName());
                singleValueCriteria.addInnerCriteria(TransactionFieldMeta.TRANSACTION_ID, TransactionMeta.ID);
                singleValueCriteria.addCriteria(TransactionFieldMeta.FIELD_NAME, field.getKey());
                if (field.getValue().get(0) == null) {
                    singleValueCriteria.addCriteria(TransactionFieldMeta.VALUE, Criteria.RELATIONAL_IS_NULL);
                } else {
                    singleValueCriteria.addCriteria(TransactionFieldMeta.VALUE, field.getValue().get(0));
                }
                transactionCriteria.addAggregate(singleValueCriteria);
                continue;
            }
            // if the value list has multiple entries, create an atomic criteria for each one
            Criteria multiValueCriteria = new AtomicCriteria();
            multiValueCriteria.setTable(TransactionFieldMeta.getName());
            multiValueCriteria.addInnerCriteria(TransactionFieldMeta.TRANSACTION_ID, TransactionMeta.ID);
            multiValueCriteria.addCriteria(TransactionFieldMeta.FIELD_NAME, field.getKey());
            AtomicCriteria ac = new AtomicCriteria();
            for (Object value : field.getValue()) {
                // check if this is the first criteria or an 'or' criteria when adding it
                if (value == null) {
                    if ((ac.getCriteriaEntries() == null) || ac.getCriteriaEntries().isEmpty()) {
                        ac.addCriteria(TransactionFieldMeta.VALUE, Criteria.RELATIONAL_IS_NULL);
                    } else {
                        ac.addOrCriteria(TransactionFieldMeta.VALUE, Criteria.RELATIONAL_IS_NULL);
                    }
                } else {
                    if ((ac.getCriteriaEntries() == null) || ac.getCriteriaEntries().isEmpty()) {
                        ac.addCriteria(TransactionFieldMeta.VALUE, value);
                    } else {
                        ac.addOrCriteria(TransactionFieldMeta.VALUE, value);
                    }
                }
            }
            multiValueCriteria.addAtomic(ac);
            transactionCriteria.addAggregate(multiValueCriteria);
        }
        for (Object result : uow.query(transactionCriteria)) {
            if (!(result instanceof Transaction)) {
                continue;
            }
            results.add((Transaction) result);
        }
    } finally {
        if (uow != null) {
            uow.close();
        }
    }
    return results;
}
Also used : Transaction(org.jaffa.transaction.domain.Transaction) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) 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) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with AtomicCriteria

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

the class JaffaTransactionMessageService method getTransactionsByTypeSubTypeFieldsOrderBy.

/**
 * Gets an ordered list of Transaction that match the input type, subType and fields.
 * If more than one value is defined for a field, that field will be checked against all values to see if it
 * matched any of them (they will be OR-ed).
 *
 * @param type    the type of Transactions to return
 * @param subType the subType of Transactions to return
 * @param fields  the fields to check the Transaction for, if multiple values are defined for a field, the
 *                field will be checked to see if it matches any of the values (they will be OR-ed)
 * @param orderBy the fields to sort the return list by, each field has a boolean value that determines the
 *                direction.  True for ascending and false for descending.
 * @return an ordered list of Transactions that match the input type, subType and fields
 * @throws FrameworkException
 */
@Override
public List<Transaction> getTransactionsByTypeSubTypeFieldsOrderBy(String type, String subType, HashMap<String, List<String>> fields, LinkedHashMap<String, Boolean> orderBy) throws FrameworkException {
    List<Transaction> results = new ArrayList<Transaction>();
    UOW uow = null;
    try {
        uow = new UOW();
        Criteria c = new Criteria();
        c.setTable(TransactionMeta.getName());
        // query on type and subType
        StringCriteriaField typeField = StringCriteriaField.getStringCriteriaField(CriteriaField.RELATIONAL_EQUALS, type, null);
        StringCriteriaField subTypeField = StringCriteriaField.getStringCriteriaField(CriteriaField.RELATIONAL_EQUALS, subType, null);
        FinderTx.addCriteria(typeField, TransactionMeta.TYPE, c);
        FinderTx.addCriteria(subTypeField, TransactionMeta.SUB_TYPE, c);
        // add the fields
        for (Map.Entry<String, List<String>> entry : fields.entrySet()) {
            Criteria joinCriteria = new Criteria();
            joinCriteria.setTable(TransactionFieldMeta.getName());
            joinCriteria.addInnerCriteria(TransactionFieldMeta.TRANSACTION_ID, TransactionMeta.ID);
            // the key is the field name
            joinCriteria.addCriteria(TransactionFieldMeta.FIELD_NAME, entry.getKey());
            // the values are OR-ed together
            AtomicCriteria ac = new AtomicCriteria();
            for (String value : entry.getValue()) {
                if (value == null) {
                    if ((ac.getCriteriaEntries() == null) || ac.getCriteriaEntries().isEmpty()) {
                        ac.addCriteria(TransactionFieldMeta.VALUE, Criteria.RELATIONAL_IS_NULL);
                    } else {
                        ac.addOrCriteria(TransactionFieldMeta.VALUE, Criteria.RELATIONAL_IS_NULL);
                    }
                } else {
                    if ((ac.getCriteriaEntries() == null) || ac.getCriteriaEntries().isEmpty()) {
                        ac.addCriteria(TransactionFieldMeta.VALUE, value);
                    } else {
                        ac.addOrCriteria(TransactionFieldMeta.VALUE, value);
                    }
                }
            }
            joinCriteria.addAtomic(ac);
            c.addAggregate(joinCriteria);
        }
        // add the orderBy
        for (Map.Entry<String, Boolean> entry : orderBy.entrySet()) {
            int sort = Criteria.ORDER_BY_ASC;
            if (entry.getValue() != null && !entry.getValue()) {
                sort = Criteria.ORDER_BY_DESC;
            }
            c.addOrderBy(entry.getKey(), sort);
        }
        // add the query results to the return list
        for (Object o : uow.query(c)) {
            results.add((Transaction) o);
        }
    } finally {
        if (uow != null) {
            uow.close();
        }
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) 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) Transaction(org.jaffa.transaction.domain.Transaction) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) UOW(org.jaffa.persistence.UOW) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) StringCriteriaField(org.jaffa.components.finder.StringCriteriaField)

Example 8 with AtomicCriteria

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

the class JaffaTransactionMessageService method getCountOpenInProgress.

/**
 * Gets the count of all Transactions with the specified field-value 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
 * @return the count of all Transactions with the specified field-value that are in the Open or InProgress state
 * @throws FrameworkException
 */
@Override
public long getCountOpenInProgress(String field, String value) throws FrameworkException {
    UOW uow = null;
    long transactionCount = 0;
    try {
        uow = new UOW();
        Criteria criteria = new Criteria();
        criteria.setTable(TransactionMeta.getName());
        AtomicCriteria ac0 = new AtomicCriteria();
        ac0.addCriteria(TransactionMeta.STATUS, Transaction.Status.O.toString());
        ac0.addOrCriteria(TransactionMeta.STATUS, Transaction.Status.I.toString());
        criteria.addAtomic(ac0);
        Criteria transactionFieldCriteria = new Criteria();
        transactionFieldCriteria.setTable(TransactionFieldMeta.getName());
        transactionFieldCriteria.addInnerCriteria(TransactionFieldMeta.TRANSACTION_ID, TransactionMeta.ID);
        criteria.addAggregate(transactionFieldCriteria);
        AtomicCriteria ac = new AtomicCriteria();
        ac.addCriteria(TransactionFieldMeta.FIELD_NAME, field);
        ac.addCriteria(TransactionFieldMeta.VALUE, value);
        transactionFieldCriteria.addAtomic(ac);
        // return the count, or zero
        for (Object result : uow.query(criteria)) {
            if (!(result instanceof Transaction)) {
                continue;
            }
            transactionCount++;
        }
    } finally {
        if (uow != null) {
            uow.close();
        }
    }
    return transactionCount;
}
Also used : Transaction(org.jaffa.transaction.domain.Transaction) 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 9 with AtomicCriteria

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

the class JaffaTransactionMessageService method getTransactionsByFieldsOred.

/**
 * Gets all Transactions that have any of the specified fields with the input values.
 * If there are more than one value for a key, this will return fields that have any of the specified values.
 *
 * @param fields the field name/value pairs that a Transaction must have at least one of to ne returned
 * @return a collection of Transactions that contain the specified fields with the input values
 * @throws FrameworkException
 */
@Override
public Collection<Transaction> getTransactionsByFieldsOred(HashMap<String, Serializable> fields) throws FrameworkException {
    UOW uow = null;
    List<Transaction> results = new ArrayList<Transaction>();
    try {
        uow = new UOW();
        Criteria transactionCriteria = new Criteria();
        transactionCriteria.setTable(TransactionMeta.getName());
        Criteria transactionFieldCriteria = new Criteria();
        transactionFieldCriteria.setTable(TransactionFieldMeta.getName());
        transactionFieldCriteria.addInnerCriteria(TransactionFieldMeta.TRANSACTION_ID, TransactionMeta.ID);
        transactionCriteria.addAggregate(transactionFieldCriteria);
        AtomicCriteria ac = new AtomicCriteria();
        // add each field in the field map to the query
        AtomicCriteria firstCriteria = null;
        for (Map.Entry<String, Serializable> field : fields.entrySet()) {
            // if there is no field defined, skip to the next entry
            if ((field.getKey() == null) || field.getKey().isEmpty()) {
                continue;
            }
            AtomicCriteria ac1 = new AtomicCriteria();
            ac1.addCriteria(TransactionFieldMeta.FIELD_NAME, field.getKey());
            if (field.getValue() == null) {
                ac1.addCriteria(TransactionFieldMeta.VALUE, Criteria.RELATIONAL_IS_NULL);
            } else {
                ac1.addCriteria(TransactionFieldMeta.VALUE, field.getValue());
            }
            if (firstCriteria != null) {
                firstCriteria.addOrAtomic(ac1);
            } else {
                firstCriteria = ac1;
                ac.addAtomic(firstCriteria);
            }
        }
        transactionFieldCriteria.addAtomic(ac);
        for (Object result : uow.query(transactionCriteria)) {
            if (!(result instanceof Transaction)) {
                continue;
            }
            results.add((Transaction) result);
        }
    } finally {
        if (uow != null) {
            uow.close();
        }
    }
    return results;
}
Also used : Serializable(java.io.Serializable) Transaction(org.jaffa.transaction.domain.Transaction) ArrayList(java.util.ArrayList) 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) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with AtomicCriteria

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

the class JaffaTransactionMessageService method getCountTxInError.

/**
 * Gets the count of all Transactions in the input list of IDs that are in the error state.
 *
 * @param transactionIds the IDs of the Transactions to check for the error state
 * @return the count of all Transactions in the error state
 * @throws FrameworkException
 */
@Override
public long getCountTxInError(List<String> transactionIds) throws FrameworkException {
    UOW uow = null;
    long errorCount = 0;
    try {
        uow = new UOW();
        AtomicCriteria ac = new AtomicCriteria();
        for (String transactionId : transactionIds) {
            ac.addOrCriteria(TransactionMeta.ID, transactionId);
        }
        Criteria criteria = new Criteria();
        criteria.setTable(TransactionMeta.getName());
        criteria.addCriteria(TransactionMeta.STATUS, Transaction.Status.E.toString());
        criteria.addAtomic(ac);
        criteria.addFunction(Criteria.FUNCTION_COUNT, null, Criteria.ID_FUNCTION_COUNT);
        Iterator itr = uow.query(criteria).iterator();
        if (itr.hasNext()) {
            Number count = (Number) ((Map) itr.next()).get(Criteria.ID_FUNCTION_COUNT);
            errorCount = count.longValue();
        }
    } finally {
        if (uow != null) {
            uow.close();
        }
    }
    return errorCount;
}
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)

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