Search in sources :

Example 1 with ReportMetadata

use of org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata in project camel by apache.

the class AnalyticsApiIntegrationTest method testReport.

@Theory
public void testReport(String reportName) throws Exception {
    log.info("Testing report {}...", reportName);
    // get Report Id
    final QueryRecordsReport reports = template().requestBody("direct:queryReport", "SELECT Id FROM Report WHERE DeveloperName='" + reportName + "'", QueryRecordsReport.class);
    assertNotNull("query", reports);
    final List<Report> reportsRecords = reports.getRecords();
    assertFalse("Report not found", reportsRecords.isEmpty());
    final String testReportId = reportsRecords.get(0).getId();
    assertNotNull(testReportId);
    // 1. getReportDescription
    final ReportDescription reportDescription = template().requestBody("direct:getReportDescription", testReportId, ReportDescription.class);
    assertNotNull("getReportDescriptions", reportDescription);
    LOG.debug("getReportDescriptions: {}", reportDescription);
    final ReportMetadata testReportMetadata = reportDescription.getReportMetadata();
    // 2. executeSyncReport
    // execute with no metadata
    SyncReportResults reportResults = template().requestBodyAndHeader("direct:executeSyncReport", testReportId, SalesforceEndpointConfig.INCLUDE_DETAILS, Boolean.TRUE, SyncReportResults.class);
    assertNotNull("executeSyncReport", reportResults);
    LOG.debug("executeSyncReport: {}", reportResults);
    // execute with metadata
    final Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(SalesforceEndpointConfig.INCLUDE_DETAILS, Boolean.FALSE);
    Object body;
    if (!bodyMetadata) {
        headers.put(SalesforceEndpointConfig.REPORT_METADATA, testReportMetadata);
        body = testReportId;
    } else {
        body = testReportMetadata;
    }
    reportResults = template().requestBodyAndHeaders("direct:executeSyncReport", body, headers, SyncReportResults.class);
    assertNotNull("executeSyncReport with metadata", reportResults);
    LOG.debug("executeSyncReport with metadata: {}", reportResults);
    // 3. executeAsyncReport
    // execute with no metadata
    ReportInstance reportInstance = template().requestBodyAndHeader("direct:executeAsyncReport", testReportId, SalesforceEndpointConfig.INCLUDE_DETAILS, true, ReportInstance.class);
    assertNotNull("executeAsyncReport", reportInstance);
    LOG.debug("executeAsyncReport: {}", reportInstance);
    // execute with metadata
    headers.clear();
    headers.put(SalesforceEndpointConfig.INCLUDE_DETAILS, "true");
    if (!bodyMetadata) {
        headers.put(SalesforceEndpointConfig.REPORT_METADATA, testReportMetadata);
        body = testReportId;
        bodyMetadata = true;
    } else {
        body = testReportMetadata;
        bodyMetadata = false;
    }
    reportInstance = template().requestBodyAndHeaders("direct:executeAsyncReport", body, headers, ReportInstance.class);
    assertNotNull("executeAsyncReport with metadata", reportInstance);
    LOG.debug("executeAsyncReport with metadata: {}", reportInstance);
    final String testReportInstanceId = reportInstance.getId();
    // 4. getReportInstances
    final List reportInstances = template().requestBody("direct:getReportInstances", testReportId, List.class);
    assertNotNull("getReportInstances", reportInstances);
    assertFalse("getReportInstances empty", reportInstances.isEmpty());
    LOG.debug("getReportInstances: {}", reportInstances);
    // 5. getReportResults
    // wait for the report to complete
    boolean done = false;
    int tries = 0;
    AsyncReportResults asyncReportResults = null;
    while (!done) {
        asyncReportResults = template().requestBodyAndHeader("direct:getReportResults", testReportId, SalesforceEndpointConfig.INSTANCE_ID, testReportInstanceId, AsyncReportResults.class);
        done = asyncReportResults != null && (asyncReportResults.getAttributes().getStatus() == ReportStatusEnum.Success || asyncReportResults.getAttributes().getStatus() == ReportStatusEnum.Error);
        if (!done) {
            // avoid flooding calls
            Thread.sleep(RETRY_DELAY);
            if (++tries > REPORT_RESULT_RETRIES) {
                final long retrySeconds = TimeUnit.SECONDS.convert(tries * RETRY_DELAY, TimeUnit.MILLISECONDS);
                fail("Async report result not available in " + retrySeconds + " seconds");
            }
        }
    }
    assertNotNull("getReportResults", asyncReportResults);
    assertEquals("getReportResults status", ReportStatusEnum.Success, asyncReportResults.getAttributes().getStatus());
    LOG.debug("getReportResults: {}", asyncReportResults);
    // 6. SalesforceReportResultsConverter tests
    // defaults
    String convertResults = template.requestBody("direct:convertResults", asyncReportResults, String.class);
    assertNotNull("default convertResults", convertResults);
    LOG.debug("Default options", convertResults);
    LOG.debug("{}", convertResults);
    // permutations of include details, include headers, include summary
    final boolean[] values = new boolean[NUM_OPTIONS];
    final int nIterations = (int) Math.pow(2, NUM_OPTIONS);
    for (int i = 0; i < nIterations; i++) {
        // toggle options
        for (int j = 0; j < NUM_OPTIONS; j++) {
            if (i % POWERS[j] == 0) {
                values[j] = !values[j];
            }
        }
        log.debug("Options {} = {}", REPORT_OPTIONS, values);
        headers.clear();
        for (int j = 0; j < REPORT_OPTIONS.length; j++) {
            headers.put(REPORT_OPTIONS[j], values[j]);
        }
        convertResults = template.requestBodyAndHeaders("direct:convertResults", asyncReportResults, headers, String.class);
        assertNotNull("convertResults", convertResults);
        LOG.debug("{}", convertResults);
    }
}
Also used : Report(org.apache.camel.component.salesforce.api.dto.analytics.reports.Report) QueryRecordsReport(org.apache.camel.component.salesforce.api.dto.analytics.reports.QueryRecordsReport) HashMap(java.util.HashMap) QueryRecordsReport(org.apache.camel.component.salesforce.api.dto.analytics.reports.QueryRecordsReport) SyncReportResults(org.apache.camel.component.salesforce.api.dto.analytics.reports.SyncReportResults) ReportInstance(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportInstance) AsyncReportResults(org.apache.camel.component.salesforce.api.dto.analytics.reports.AsyncReportResults) List(java.util.List) ReportDescription(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportDescription) ReportMetadata(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata) Theory(org.junit.experimental.theories.Theory)

Example 2 with ReportMetadata

use of org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata in project camel by apache.

the class AnalyticsApiProcessor method processExecuteAsyncReport.

private void processExecuteAsyncReport(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    String reportId;
    final Boolean includeDetails = getParameter(INCLUDE_DETAILS, exchange, IGNORE_BODY, IS_OPTIONAL, Boolean.class);
    // try getting report metadata from body first
    ReportMetadata reportMetadata = exchange.getIn().getBody(ReportMetadata.class);
    if (reportMetadata != null) {
        reportId = reportMetadata.getId();
        if (reportId == null) {
            reportId = getParameter(REPORT_ID, exchange, IGNORE_BODY, NOT_OPTIONAL);
        }
    } else {
        reportId = getParameter(REPORT_ID, exchange, USE_BODY, NOT_OPTIONAL);
        reportMetadata = getParameter(REPORT_METADATA, exchange, IGNORE_BODY, IS_OPTIONAL, ReportMetadata.class);
    }
    analyticsClient.executeAsyncReport(reportId, includeDetails, reportMetadata, new AnalyticsApiClient.ReportInstanceResponseCallback() {

        @Override
        public void onResponse(ReportInstance reportInstance, SalesforceException ex) {
            processResponse(exchange, reportInstance, ex, callback);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) AnalyticsApiClient(org.apache.camel.component.salesforce.internal.client.AnalyticsApiClient) DefaultAnalyticsApiClient(org.apache.camel.component.salesforce.internal.client.DefaultAnalyticsApiClient) ReportInstance(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportInstance) ReportMetadata(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata)

Example 3 with ReportMetadata

use of org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata in project camel by apache.

the class AnalyticsApiProcessor method processExecuteSyncReport.

private void processExecuteSyncReport(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    String reportId;
    final Boolean includeDetails = getParameter(INCLUDE_DETAILS, exchange, IGNORE_BODY, IS_OPTIONAL, Boolean.class);
    // try getting report metadata from body first
    ReportMetadata reportMetadata = exchange.getIn().getBody(ReportMetadata.class);
    if (reportMetadata != null) {
        reportId = reportMetadata.getId();
        if (reportId == null) {
            reportId = getParameter(REPORT_ID, exchange, IGNORE_BODY, NOT_OPTIONAL);
        }
    } else {
        reportId = getParameter(REPORT_ID, exchange, USE_BODY, NOT_OPTIONAL);
        reportMetadata = getParameter(REPORT_METADATA, exchange, IGNORE_BODY, IS_OPTIONAL, ReportMetadata.class);
    }
    analyticsClient.executeSyncReport(reportId, includeDetails, reportMetadata, new AnalyticsApiClient.ReportResultsResponseCallback() {

        @Override
        public void onResponse(AbstractReportResultsBase reportResults, SalesforceException ex) {
            processResponse(exchange, reportResults, ex, callback);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) AnalyticsApiClient(org.apache.camel.component.salesforce.internal.client.AnalyticsApiClient) DefaultAnalyticsApiClient(org.apache.camel.component.salesforce.internal.client.DefaultAnalyticsApiClient) AbstractReportResultsBase(org.apache.camel.component.salesforce.api.dto.analytics.reports.AbstractReportResultsBase) ReportMetadata(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata)

Example 4 with ReportMetadata

use of org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata in project camel by apache.

the class SalesforceReportResultsToListConverter method convertTabularResults.

private static List<List<String>> convertTabularResults(final AbstractReportResultsBase reportResults, final Exchange exchange) {
    final ArrayList<List<String>> result = new ArrayList<List<String>>();
    final ReportMetadata reportMetadata = reportResults.getReportMetadata();
    final String[] detailColumns = reportMetadata.getDetailColumns();
    final ReportExtendedMetadata reportExtendedMetadata = reportResults.getReportExtendedMetadata();
    final ReportFactWithDetails factWithDetails = reportResults.getFactMap().get("T!T");
    // include detail rows?
    final String[] aggregates = reportMetadata.getAggregates();
    if (reportResults.getHasDetailRows() && getOption(exchange, INCLUDE_DETAILS, Boolean.TRUE)) {
        final int rowLength = detailColumns.length;
        // include detail headers?
        if (getOption(exchange, INCLUDE_HEADERS, Boolean.TRUE)) {
            final List<String> headers = new ArrayList<String>(rowLength);
            result.add(headers);
            addColumnHeaders(headers, reportExtendedMetadata.getDetailColumnInfo(), detailColumns);
        }
        final ReportRow[] reportRows = factWithDetails.getRows();
        result.ensureCapacity(result.size() + reportRows.length);
        for (ReportRow reportRow : reportRows) {
            final List<String> row = new ArrayList<String>(rowLength);
            result.add(row);
            addRowValues(row, reportRow.getDataCells());
        }
        // include summary values?
        if (aggregates.length > 0 && getOption(exchange, INCLUDE_SUMMARY, Boolean.TRUE)) {
            addSummaryRows(result, detailColumns, null, aggregates, factWithDetails.getAggregates());
        }
    } else if (aggregates.length > 0) {
        final int rowLength = aggregates.length;
        // include summary headers?
        if (getOption(exchange, INCLUDE_HEADERS, Boolean.TRUE)) {
            final List<String> headers = new ArrayList<String>(rowLength);
            result.add(headers);
            addColumnHeaders(headers, reportExtendedMetadata.getAggregateColumnInfo(), aggregates);
        }
        // add summary values
        final List<String> row = new ArrayList<String>(rowLength);
        result.add(row);
        addRowValues(row, factWithDetails.getAggregates());
    }
    return result;
}
Also used : ReportRow(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportRow) ReportFactWithDetails(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportFactWithDetails) ArrayList(java.util.ArrayList) ReportExtendedMetadata(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportExtendedMetadata) ArrayList(java.util.ArrayList) List(java.util.List) ReportMetadata(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata)

Example 5 with ReportMetadata

use of org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata in project camel by apache.

the class SalesforceReportResultsToListConverter method convertSummaryResults.

private static List<List<String>> convertSummaryResults(final AbstractReportResultsBase reportResults, Exchange exchange) {
    final ArrayList<List<String>> result = new ArrayList<List<String>>();
    final ReportMetadata reportMetadata = reportResults.getReportMetadata();
    final ReportExtendedMetadata reportExtendedMetadata = reportResults.getReportExtendedMetadata();
    final String[] aggregates = reportMetadata.getAggregates();
    final boolean includeDetails = reportResults.getHasDetailRows() && getOption(exchange, INCLUDE_DETAILS, Boolean.TRUE);
    final boolean includeSummary = aggregates.length > 0 && getOption(exchange, INCLUDE_SUMMARY, Boolean.TRUE);
    // column list, including grouping columns and details if required
    final ArrayList<DetailColumnInfo> columnInfos = new ArrayList<DetailColumnInfo>();
    final String[] columnNames = getResultColumns(columnInfos, reportMetadata, reportExtendedMetadata, includeDetails, includeSummary);
    // include detail headers?
    if (getOption(exchange, INCLUDE_HEADERS, Boolean.TRUE)) {
        addColumnHeaders(result, columnInfos);
    }
    // process down groups
    for (GroupingValue groupingValue : reportResults.getGroupingsDown().getGroupings()) {
        addSummaryGroupValues(result, reportResults, columnNames, groupingValue, EMPTY_STRING_LIST, includeDetails, includeSummary);
    }
    // add grand total
    if (includeSummary) {
        final ReportFactWithDetails grandTotal = reportResults.getFactMap().get("T!T");
        addSummaryValues(result, includeDetails, columnNames, EMPTY_STRING_LIST, aggregates, grandTotal.getAggregates());
    }
    return result;
}
Also used : ReportExtendedMetadata(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportExtendedMetadata) ReportFactWithDetails(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportFactWithDetails) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GroupingValue(org.apache.camel.component.salesforce.api.dto.analytics.reports.GroupingValue) ReportMetadata(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata) DetailColumnInfo(org.apache.camel.component.salesforce.api.dto.analytics.reports.DetailColumnInfo)

Aggregations

ReportMetadata (org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata)6 List (java.util.List)4 ArrayList (java.util.ArrayList)3 ReportExtendedMetadata (org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportExtendedMetadata)3 ReportFactWithDetails (org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportFactWithDetails)3 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)2 DetailColumnInfo (org.apache.camel.component.salesforce.api.dto.analytics.reports.DetailColumnInfo)2 GroupingValue (org.apache.camel.component.salesforce.api.dto.analytics.reports.GroupingValue)2 ReportInstance (org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportInstance)2 AnalyticsApiClient (org.apache.camel.component.salesforce.internal.client.AnalyticsApiClient)2 DefaultAnalyticsApiClient (org.apache.camel.component.salesforce.internal.client.DefaultAnalyticsApiClient)2 HashMap (java.util.HashMap)1 AbstractReportResultsBase (org.apache.camel.component.salesforce.api.dto.analytics.reports.AbstractReportResultsBase)1 AsyncReportResults (org.apache.camel.component.salesforce.api.dto.analytics.reports.AsyncReportResults)1 QueryRecordsReport (org.apache.camel.component.salesforce.api.dto.analytics.reports.QueryRecordsReport)1 Report (org.apache.camel.component.salesforce.api.dto.analytics.reports.Report)1 ReportDescription (org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportDescription)1 ReportRow (org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportRow)1 SyncReportResults (org.apache.camel.component.salesforce.api.dto.analytics.reports.SyncReportResults)1 Theory (org.junit.experimental.theories.Theory)1