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);
}
}
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);
}
});
}
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);
}
});
}
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;
}
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;
}
Aggregations