Search in sources :

Example 31 with Transaction

use of org.jaffa.transaction.domain.Transaction in project jaffa-framework by jaffa-projects.

the class TransactionAdmin method messageQuery.

public MessageQueryResponse messageQuery(MessageCriteria criteria) {
    if (log.isDebugEnabled()) {
        log.debug("Input to messageQuery: " + criteria);
    }
    MessageQueryResponse output = new MessageQueryResponse();
    try {
        // Match the queueSystemId as well apply checks for unsupported criteria
        if (FinderTx.match(QUEUE_SYSTEM_ID, criteria.getQueueSystemId()) && FinderTx.match(null, criteria.getPriority())) {
            PropertyFilter pf = PropertyFilter.getInstance(MessageGraph.class, criteria.getResultGraphRules());
            Collection<MessageGraph> graphs = new LinkedList<MessageGraph>();
            String type = getMessageCriteriaType(criteria);
            String subType = getMessageCriteriaSubType(criteria);
            // we will be getting an ordered list
            LinkedHashMap<String, Boolean> orderBy = getOrderByFields(criteria);
            // the fields the transactions must have
            HashMap<String, List<String>> fields = new HashMap<String, List<String>>();
            if (criteria.getApplicationFields() != null) {
                for (MessageFieldCriteria messageFieldCriteria : criteria.getApplicationFields()) {
                    List<String> fieldValues = getFieldCriteriaValues(messageFieldCriteria);
                    fields.put(messageFieldCriteria.getName(), fieldValues);
                }
            }
            // get an ordered list of all transactions of the specified type and subType
            List<Transaction> allTransactions = new ArrayList<Transaction>();
            allTransactions.addAll(transactionDAO.getTransactionsByTypeSubTypeFieldsOrderBy(type, subType, fields, orderBy));
            // filter the list down based on other criteria
            List<Transaction> filteredTransactions = new ArrayList<Transaction>();
            filteredTransactions.addAll(allTransactions);
            for (Transaction transaction : allTransactions) {
                if (doesTransactionFailFilterCheck(transaction, criteria)) {
                    filteredTransactions.remove(transaction);
                }
            }
            // now we have a filtered and ordered list of Transactions, check if we only want a page of the results
            int firstIndex = criteria.getObjectStart() == null ? 0 : criteria.getObjectStart();
            int resultsCount = criteria.getObjectLimit() == null ? 0 : criteria.getObjectLimit();
            int lastIndex = firstIndex + resultsCount;
            if (lastIndex > filteredTransactions.size()) {
                lastIndex = filteredTransactions.size();
            }
            List<Transaction> filteredPagedTransactions = new ArrayList<Transaction>();
            if ((lastIndex > firstIndex) && ((firstIndex > 0) || (lastIndex > 0))) {
                filteredPagedTransactions.addAll(filteredTransactions.subList(firstIndex, lastIndex));
            } else {
                filteredPagedTransactions.addAll(filteredTransactions);
            }
            // create a graph from the filtered and paged list of transactions
            for (Transaction transaction : filteredPagedTransactions) {
                MessageGraph graph = createMessageGraph(transaction, pf);
                graphs.add(graph);
            }
            if (graphs.size() > 0) {
                output.setGraphs(graphs.toArray(new MessageGraph[graphs.size()]));
            }
            // set the total count of rows being output
            int rowsCount = graphs.size();
            if ((firstIndex > 0) || (resultsCount > 0)) {
                if ((firstIndex <= 0) && (rowsCount < resultsCount)) {
                    output.setTotalRecords(rowsCount);
                } else {
                    output.setTotalRecords(filteredTransactions.size());
                }
            } else {
                output.setTotalRecords(rowsCount);
            }
        }
    } catch (Exception e) {
        // add errors to the response
        ApplicationExceptions appExps = ExceptionHelper.extractApplicationExceptions(e);
        if (appExps != null) {
            if (log.isDebugEnabled()) {
                log.debug("Error in messageQuery execution", appExps);
            }
            output.setErrors(ServiceError.generate(appExps));
        } else {
            log.error("Internal Error in messageQuery execution", e);
            output.setErrors(ServiceError.generate(e));
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Output from messageQuery: " + output);
    }
    return output;
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) MessageQueryResponse(org.jaffa.qm.apis.data.MessageQueryResponse) LinkedList(java.util.LinkedList) IllegalPersistentStateRuntimeException(org.jaffa.persistence.exceptions.IllegalPersistentStateRuntimeException) JaffaMessagingFrameworkException(org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException) PostLoadFailedException(org.jaffa.persistence.exceptions.PostLoadFailedException) FrameworkException(org.jaffa.exceptions.FrameworkException) QueryFailedException(org.jaffa.persistence.exceptions.QueryFailedException) ApplicationException(org.jaffa.exceptions.ApplicationException) MessageFieldCriteria(org.jaffa.qm.apis.data.MessageFieldCriteria) Transaction(org.jaffa.transaction.domain.Transaction) MessageGraph(org.jaffa.qm.apis.data.MessageGraph) PropertyFilter(org.jaffa.qm.util.PropertyFilter) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Aggregations

Transaction (org.jaffa.transaction.domain.Transaction)31 UOW (org.jaffa.persistence.UOW)19 TransactionCriteria (org.jaffa.transaction.apis.data.TransactionCriteria)14 TransactionFieldCriteria (org.jaffa.transaction.apis.data.TransactionFieldCriteria)14 AtomicCriteria (org.jaffa.persistence.AtomicCriteria)13 Criteria (org.jaffa.persistence.Criteria)13 ArrayList (java.util.ArrayList)11 FrameworkException (org.jaffa.exceptions.FrameworkException)10 ApplicationException (org.jaffa.exceptions.ApplicationException)9 HashMap (java.util.HashMap)8 LinkedHashMap (java.util.LinkedHashMap)8 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)8 Map (java.util.Map)7 LinkedList (java.util.LinkedList)5 InvalidForeignKeyException (org.jaffa.datatypes.exceptions.InvalidForeignKeyException)5 JaffaMessagingFrameworkException (org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException)4 TransactionMessageDAO (org.jaffa.transaction.daos.TransactionMessageDAO)4 List (java.util.List)3 DomainObjectNotFoundException (org.jaffa.exceptions.DomainObjectNotFoundException)3 IllegalPersistentStateRuntimeException (org.jaffa.persistence.exceptions.IllegalPersistentStateRuntimeException)3