Search in sources :

Example 1 with JdbcResultSet

use of com.adaptris.jdbc.JdbcResultSet in project interlok by adaptris.

the class XmlPayloadTranslator method toDocument.

private DocumentWrapper toDocument(JdbcResult rs, AdaptrisMessage msg) throws Exception {
    XmlUtils xu = createXmlUtils(msg);
    DocumentWrapper wrapper = createWrapper(msg);
    Document doc = wrapper.document;
    ColumnStyle elementNameStyle = getColumnNameStyle();
    Element results = doc.createElement(elementNameStyle.format(ELEMENT_NAME_RESULTS));
    doc.appendChild(results);
    if (isPreserveOriginalMessage()) {
        Element originalMessage = doc.createElement(elementNameStyle.format(ORIGINAL_MESSAGE_ELEMENT));
        if (xu.isDocumentValid()) {
            Node n = xu.getSingleNode("/").getFirstChild();
            originalMessage.appendChild(doc.importNode(n, true));
        } else {
            // Not XML, so let's add it in as a CDATA node.
            originalMessage.appendChild(createTextNode(doc, msg.getContent(), true));
            wrapper.hasCDATA = true;
        }
        results.appendChild(originalMessage);
    }
    for (JdbcResultSet rSet : rs.getResultSets()) {
        List<Element> elements = createListFromResultSet(wrapper, rSet);
        for (Element element : elements) {
            results.appendChild(element);
        }
        wrapper.resultSetCount += elements.size();
    }
    return wrapper;
}
Also used : XmlUtils(com.adaptris.util.XmlUtils) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) JdbcResultSet(com.adaptris.jdbc.JdbcResultSet)

Example 2 with JdbcResultSet

use of com.adaptris.jdbc.JdbcResultSet in project interlok by adaptris.

the class AllRowsMetadataTranslator method translateResult.

@Override
public long translateResult(JdbcResult source, AdaptrisMessage target) throws SQLException, ServiceException {
    List<MetadataElement> added = new ArrayList<MetadataElement>();
    long resultSetCount = 0;
    for (JdbcResultSet resultSet : source.getResultSets()) {
        int counter = 0;
        String resultSetPrefix = source.countResultSets() > 1 ? Long.toString(resultSetCount) + getResultSetCounterPrefix() : "";
        for (JdbcResultRow row : resultSet.getRows()) {
            for (int i = 0; i < row.getFieldCount(); i++) {
                MetadataElement md = new MetadataElement(resultSetPrefix + getMetadataKeyPrefix() + getSeparator() + getColumnNameStyle().format(row.getFieldName(i)) + getSeparator() + counter, toString(row, i));
                if (log.isTraceEnabled()) {
                    added.add(md);
                }
                target.addMetadata(md);
            }
            counter++;
        }
        // In the event that we have multiple selects (unlikely from a JdbcDataQuery) then
        // row-total will have the total for each result set
        // resultSetCount will have the total for all resultSets.
        // if (!isEmpty(getRowTotalMetadataKey())) {
        // target.addMetadata(resultSetPrefix + getRowTotalMetadataKey(), String.valueOf(counter));
        // }
        resultSetCount += counter;
    }
    if (log.isTraceEnabled()) {
        log.debug("Added metadata : " + added);
    }
    return resultSetCount;
}
Also used : JdbcResultRow(com.adaptris.jdbc.JdbcResultRow) ArrayList(java.util.ArrayList) MetadataElement(com.adaptris.core.MetadataElement) JdbcResultSet(com.adaptris.jdbc.JdbcResultSet)

Example 3 with JdbcResultSet

use of com.adaptris.jdbc.JdbcResultSet in project interlok by adaptris.

the class SplittingXmlPayloadTranslator method translateResult.

/**
 * Split the JdbcResult into possibly multiple output messages. Each ResultSet will start in a new message if there are multiple.
 */
@Override
public long translateResult(JdbcResult source, AdaptrisMessage inputMessage) throws SQLException, ServiceException {
    long resultSetCount = 0;
    try {
        // Handle each ResultSet separately and create as many messages as required
        for (JdbcResultSet rs : source.getResultSets()) {
            // While we still have rows in this ResultSet
            final Iterator<JdbcResultRow> rows = rs.getRows().iterator();
            while (rows.hasNext()) {
                AdaptrisMessage outputMessage = newMessage(inputMessage);
                // is limit the number of iterations you make.
                try (LimitedResultSet lrs = new LimitedResultSet(rows, maxRowsPerMessage())) {
                    DocumentWrapper wrapper = toDocument(outputMessage, lrs);
                    XmlHelper.writeXmlDocument(wrapper.document, outputMessage, getOutputMessageEncoding(), createTransformer(wrapper));
                    // Use the configured producer to send the message on its way
                    getProducer().produce(outputMessage);
                    resultSetCount += wrapper.resultSetCount;
                }
            }
        }
    } catch (Exception e) {
        throw ExceptionHelper.wrapServiceException("Failed to process message", e);
    }
    return resultSetCount;
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) JdbcResultRow(com.adaptris.jdbc.JdbcResultRow) ServiceException(com.adaptris.core.ServiceException) SQLException(java.sql.SQLException) NoSuchElementException(java.util.NoSuchElementException) CoreException(com.adaptris.core.CoreException) JdbcResultSet(com.adaptris.jdbc.JdbcResultSet)

Example 4 with JdbcResultSet

use of com.adaptris.jdbc.JdbcResultSet in project interlok by adaptris.

the class FirstRowMetadataTranslator method translateResult.

@Override
public long translateResult(JdbcResult source, AdaptrisMessage target) throws SQLException, ServiceException {
    List<MetadataElement> added = new ArrayList<MetadataElement>();
    long resultSetCount = 0;
    Iterator<JdbcResultRow> iter;
    for (JdbcResultSet resultSet : source.getResultSets()) {
        if (resultSet.getRows() != null && (iter = resultSet.getRows().iterator()) != null && iter.hasNext()) {
            JdbcResultRow storedProcedureResultRow = iter.next();
            if (storedProcedureResultRow != null) {
                for (int i = 0; i < storedProcedureResultRow.getFieldCount(); i++) {
                    String column = storedProcedureResultRow.getFieldName(i);
                    String resultSetPrefix = source.countResultSets() > 1 ? Long.toString(resultSetCount) + getResultSetCounterPrefix() : "";
                    MetadataElement md = new MetadataElement(resultSetPrefix + getMetadataKeyPrefix() + getSeparator() + getColumnNameStyle().format(column), toString(storedProcedureResultRow, i));
                    if (log.isTraceEnabled()) {
                        added.add(md);
                    }
                    target.addMetadata(md);
                }
                if (log.isTraceEnabled()) {
                    log.debug("Added metadata : " + added);
                }
            } else {
                log.debug("No Rows to process");
            }
            resultSetCount++;
        }
    }
    return resultSetCount;
}
Also used : JdbcResultRow(com.adaptris.jdbc.JdbcResultRow) ArrayList(java.util.ArrayList) MetadataElement(com.adaptris.core.MetadataElement) JdbcResultSet(com.adaptris.jdbc.JdbcResultSet)

Example 5 with JdbcResultSet

use of com.adaptris.jdbc.JdbcResultSet in project interlok by adaptris.

the class MergeResultSetIntoXmlPayload method createDocument.

private DocumentWrapper createDocument(JdbcResult rs, AdaptrisMessage msg) throws Exception {
    DocumentWrapper wrapper = createWrapper(msg);
    Document doc = wrapper.document;
    Element results = doc.createElement(getColumnNameStyle().format(ELEMENT_NAME_RESULTS));
    doc.appendChild(results);
    for (JdbcResultSet rSet : rs.getResultSets()) {
        List<Element> elements = createListFromResultSet(wrapper, rSet);
        for (Element element : elements) {
            results.appendChild(element);
        }
        wrapper.resultSetCount += elements.size();
    }
    return wrapper;
}
Also used : Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) JdbcResultSet(com.adaptris.jdbc.JdbcResultSet)

Aggregations

JdbcResultSet (com.adaptris.jdbc.JdbcResultSet)5 JdbcResultRow (com.adaptris.jdbc.JdbcResultRow)3 MetadataElement (com.adaptris.core.MetadataElement)2 ArrayList (java.util.ArrayList)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)1 CoreException (com.adaptris.core.CoreException)1 ServiceException (com.adaptris.core.ServiceException)1 XmlUtils (com.adaptris.util.XmlUtils)1 SQLException (java.sql.SQLException)1 NoSuchElementException (java.util.NoSuchElementException)1 Node (org.w3c.dom.Node)1