Search in sources :

Example 1 with MessageQueryResponse

use of org.jaffa.qm.apis.data.MessageQueryResponse in project jaffa-framework by jaffa-projects.

the class JmsQueueAdmin 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.getSubType()) && FinderTx.match(null, criteria.getDirection()) && FinderTx.match(null, criteria.getLastChangedOn()) && FinderTx.match(null, criteria.getLastChangedBy())) {
            String[] queueNames = JmsBrowser.getAccessibleQueueNames();
            if (queueNames != null && queueNames.length > 0) {
                String jmsFilter = createJmsFilter(criteria);
                PropertyFilter pf = PropertyFilter.getInstance(MessageGraph.class, criteria.getResultGraphRules());
                Collection<MessageGraph> graphs = new LinkedList<MessageGraph>();
                for (String queueName : queueNames) {
                    if (FinderTx.match(queueName, criteria.getType())) {
                        // Retrieve messages for each requested status
                        for (MessageGraph.Status status : SUPPORTED_MESSAGE_STATUS) {
                            if (FinderTx.match(status.toString(), criteria.getStatus())) {
                                Message[] messages = null;
                                if (status == MessageGraph.Status.OPEN)
                                    messages = JmsBrowser.getPendingMessages(queueName, jmsFilter);
                                if (messages != null) {
                                    for (Message message : messages) {
                                        MessageGraph graph = createMessageGraph(message, queueName, pf);
                                        if (pf.isFieldIncluded("status"))
                                            graph.setStatus(status);
                                        graphs.add(graph);
                                    }
                                }
                            }
                        }
                    }
                }
                if (graphs.size() > 0) {
                    output.setGraphs(graphs.toArray(new MessageGraph[graphs.size()]));
                    if (criteria.getOrderByFields() != null && criteria.getOrderByFields().length > 0)
                        Arrays.sort(output.getGraphs(), new GraphComparator(criteria.getOrderByFields()));
                    handlePaging(output, criteria);
                }
            }
        }
    } 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) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageQueryResponse(org.jaffa.qm.apis.data.MessageQueryResponse) LinkedList(java.util.LinkedList) JMSException(javax.jms.JMSException) IntrospectionException(java.beans.IntrospectionException) MessageGraph(org.jaffa.qm.apis.data.MessageGraph) GraphComparator(org.jaffa.soa.graph.GraphComparator) PropertyFilter(org.jaffa.qm.util.PropertyFilter)

Example 2 with MessageQueryResponse

use of org.jaffa.qm.apis.data.MessageQueryResponse in project jaffa-framework by jaffa-projects.

the class QueueManager method messageQuery.

public MessageQueryResponse messageQuery(MessageCriteria criteria) throws ApplicationExceptions {
    try {
        if (log.isDebugEnabled())
            log.debug("Input to messageQuery: " + criteria);
        MessageQueryResponse aggregateResponse = new MessageQueryResponse();
        IQueueAdmin[] implementations = findImplementations();
        // Find which implementation of queue admin can be used to query messages for supplied queue
        IQueueAdmin queryImplementation = null;
        boolean foundMultiple = false;
        if (implementations != null && implementations.length > 0) {
            for (IQueueAdmin implementation : implementations) {
                QueueCriteria queueCriteria = new QueueCriteria();
                queueCriteria.setType(criteria.getType());
                queueCriteria.setQueueSystemId(criteria.getQueueSystemId());
                QueueQueryResponse queueResponse = implementation.queueQuery(queueCriteria);
                if (queueResponse != null && queueResponse.getGraphs() != null && queueResponse.getGraphs().length == 1) {
                    if (queryImplementation != null)
                        foundMultiple = true;
                    queryImplementation = implementation;
                } else if (queueResponse.getGraphs() != null && queueResponse.getGraphs().length > 1) {
                    foundMultiple = true;
                }
            }
        }
        if (foundMultiple) {
            log.error("Found multiple IQueueAdmin implementations for message query");
            throw new ApplicationExceptions(new QueueAdminException(QueueAdminException.MULTIPLE_QUEUES));
        }
        // Use correct queue admin to retrieve messages. Admin class should perform sorting and paging.
        if (queryImplementation != null) {
            MessageQueryResponse response = queryImplementation.messageQuery(criteria);
            if (response != null && response.getErrors() != null && response.getErrors().length > 0) {
                aggregateResponse.setErrors(response.getErrors());
            } else if (response != null && response.getGraphs() != null && response.getGraphs().length > 0) {
                aggregateResponse.setGraphs(concatenate(aggregateResponse.getGraphs(), response.getGraphs()));
            }
            if (response.getTotalRecords() != null && response.getTotalRecords() > 0)
                aggregateResponse.setTotalRecords(response.getTotalRecords());
        }
        if (aggregateResponse.getGraphs() == null)
            aggregateResponse.setGraphs(new MessageGraph[0]);
        if (log.isDebugEnabled())
            log.debug("Response from messageQuery: " + aggregateResponse);
        return aggregateResponse;
    } catch (Exception e) {
        log.error("Error in obtaining IQueueAdmin implementations", e);
        return new MessageQueryResponse(null, ServiceError.generate(e));
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) QueueCriteria(org.jaffa.qm.apis.data.QueueCriteria) QueueQueryResponse(org.jaffa.qm.apis.data.QueueQueryResponse) MessageGraph(org.jaffa.qm.apis.data.MessageGraph) MessageQueryResponse(org.jaffa.qm.apis.data.MessageQueryResponse)

Example 3 with MessageQueryResponse

use of org.jaffa.qm.apis.data.MessageQueryResponse 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

ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)3 MessageGraph (org.jaffa.qm.apis.data.MessageGraph)3 MessageQueryResponse (org.jaffa.qm.apis.data.MessageQueryResponse)3 LinkedList (java.util.LinkedList)2 PropertyFilter (org.jaffa.qm.util.PropertyFilter)2 IntrospectionException (java.beans.IntrospectionException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 JMSException (javax.jms.JMSException)1 Message (javax.jms.Message)1 TextMessage (javax.jms.TextMessage)1 ApplicationException (org.jaffa.exceptions.ApplicationException)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1 JaffaMessagingFrameworkException (org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException)1 IllegalPersistentStateRuntimeException (org.jaffa.persistence.exceptions.IllegalPersistentStateRuntimeException)1 PostLoadFailedException (org.jaffa.persistence.exceptions.PostLoadFailedException)1 QueryFailedException (org.jaffa.persistence.exceptions.QueryFailedException)1 MessageFieldCriteria (org.jaffa.qm.apis.data.MessageFieldCriteria)1