Search in sources :

Example 61 with UOW

use of org.jaffa.persistence.UOW 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 62 with UOW

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

the class JaffaTransactionMessageService method getInboundByTypeSubTypeAndFields.

/**
 * Gets all inbound Transactions that have the specified type, subType and 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 type    the type of Transactions to return
 * @param subType the subType of Transactions to return
 * @param fields  the field name/value pairs
 * @return a collection of inbound Transactions of the input type and subType that contain the specified
 *         fields with the input values
 * @throws FrameworkException
 */
@Override
public Collection<Transaction> getInboundByTypeSubTypeAndFields(String type, String subType, HashMap<String, String> fields) throws FrameworkException {
    UOW uow = null;
    Collection<Transaction> transactions = new ArrayList<Transaction>();
    try {
        uow = new UOW();
        Criteria transactionCriteria = new Criteria();
        // query for inbound Transaction with the input type and subType
        transactionCriteria.setTable(TransactionMeta.getName());
        transactionCriteria.addCriteria(TransactionMeta.TYPE, type);
        transactionCriteria.addCriteria(TransactionMeta.SUB_TYPE, subType);
        transactionCriteria.addCriteria(TransactionMeta.DIRECTION, Transaction.Direction.IN.name());
        // add each field in the field map to the query
        for (Map.Entry<String, String> field : fields.entrySet()) {
            if ((field.getKey() == null) || field.getKey().isEmpty()) {
                continue;
            }
            Criteria fieldCriteria = new Criteria();
            fieldCriteria.setTable(TransactionFieldMeta.getName());
            fieldCriteria.addInnerCriteria(TransactionFieldMeta.TRANSACTION_ID, TransactionMeta.ID);
            fieldCriteria.addCriteria(TransactionFieldMeta.FIELD_NAME, field.getKey());
            fieldCriteria.addCriteria(TransactionFieldMeta.VALUE, field.getValue());
            transactionCriteria.addAggregate(fieldCriteria);
        }
        for (Object result : uow.query(transactionCriteria)) {
            if (!(result instanceof Transaction)) {
                continue;
            }
            transactions.add((Transaction) result);
        }
    } finally {
        if (uow != null) {
            uow.close();
        }
    }
    return transactions;
}
Also used : Transaction(org.jaffa.transaction.domain.Transaction) ArrayList(java.util.ArrayList) 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 63 with UOW

use of org.jaffa.persistence.UOW 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 64 with UOW

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

the class JaffaTransactionMessageService method getCountByType.

/**
 * Gets the count of Transactions of a specified type.
 *
 * @param type the type of Transactions to return the count of
 * @return the count of Transactions of the input type
 * @throws FrameworkException
 */
@Override
public long getCountByType(String type) throws FrameworkException {
    UOW uow = null;
    long transactionCount = 0;
    try {
        uow = new UOW();
        Criteria criteria = new Criteria();
        criteria.setTable(TransactionMeta.getName());
        criteria.addFunction(Criteria.FUNCTION_COUNT, null, Criteria.ID_FUNCTION_COUNT);
        criteria.addCriteria(TransactionMeta.TYPE, type);
        Iterator itr = uow.query(criteria).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) 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 65 with UOW

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

Aggregations

UOW (org.jaffa.persistence.UOW)260 Criteria (org.jaffa.persistence.Criteria)139 FrameworkException (org.jaffa.exceptions.FrameworkException)99 ApplicationException (org.jaffa.exceptions.ApplicationException)88 AtomicCriteria (org.jaffa.persistence.AtomicCriteria)87 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)82 Iterator (java.util.Iterator)47 TransactionCriteria (org.jaffa.transaction.apis.data.TransactionCriteria)33 TransactionFieldCriteria (org.jaffa.transaction.apis.data.TransactionFieldCriteria)33 ArrayList (java.util.ArrayList)19 Transaction (org.jaffa.transaction.domain.Transaction)19 Map (java.util.Map)16 LinkedHashMap (java.util.LinkedHashMap)13 HashMap (java.util.HashMap)12 DateTime (org.jaffa.datatypes.DateTime)11 FormTemplate (org.jaffa.modules.printing.domain.FormTemplate)10 IPersistent (org.jaffa.persistence.IPersistent)10 Method (java.lang.reflect.Method)9 Collection (java.util.Collection)8 DomainObjectNotFoundException (org.jaffa.exceptions.DomainObjectNotFoundException)8