Search in sources :

Example 6 with BatchInfo

use of org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo in project camel by apache.

the class BulkApiProcessor method processCreateBatch.

private void processCreateBatch(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    String jobId;
    // since request is in the body, use headers or endpoint params
    ContentType contentType = ContentType.fromValue(getParameter(CONTENT_TYPE, exchange, IGNORE_BODY, NOT_OPTIONAL));
    jobId = getParameter(JOB_ID, exchange, IGNORE_BODY, NOT_OPTIONAL);
    InputStream request;
    try {
        request = exchange.getIn().getMandatoryBody(InputStream.class);
    } catch (CamelException e) {
        String msg = "Error preparing batch request: " + e.getMessage();
        throw new SalesforceException(msg, e);
    }
    bulkClient.createBatch(request, jobId, contentType, new BulkApiClient.BatchInfoResponseCallback() {

        @Override
        public void onResponse(BatchInfo batchInfo, SalesforceException ex) {
            processResponse(exchange, batchInfo, ex, callback);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) ContentType(org.apache.camel.component.salesforce.api.dto.bulk.ContentType) CamelException(org.apache.camel.CamelException) InputStream(java.io.InputStream) DefaultBulkApiClient(org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient) BulkApiClient(org.apache.camel.component.salesforce.internal.client.BulkApiClient) BatchInfo(org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)

Example 7 with BatchInfo

use of org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo in project camel by apache.

the class BulkApiBatchIntegrationTest method testBatchLifecycle.

@Theory
public void testBatchLifecycle(BatchTest request) throws Exception {
    log.info("Testing Batch lifecycle with {} content", request.contentType);
    // create an UPSERT test Job for this batch request
    JobInfo jobInfo = new JobInfo();
    jobInfo.setOperation(OperationEnum.UPSERT);
    jobInfo.setContentType(request.contentType);
    jobInfo.setObject(Merchandise__c.class.getSimpleName());
    jobInfo.setExternalIdFieldName("Name");
    jobInfo = createJob(jobInfo);
    // test createBatch
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(SalesforceEndpointConfig.JOB_ID, jobInfo.getId());
    headers.put(SalesforceEndpointConfig.CONTENT_TYPE, jobInfo.getContentType());
    BatchInfo batchInfo = template().requestBodyAndHeaders("direct:createBatch", request.stream, headers, BatchInfo.class);
    assertNotNull("Null batch", batchInfo);
    assertNotNull("Null batch id", batchInfo.getId());
    // test getAllBatches
    @SuppressWarnings("unchecked") List<BatchInfo> batches = template().requestBody("direct:getAllBatches", jobInfo, List.class);
    assertNotNull("Null batches", batches);
    assertFalse("Empty batch list", batches.isEmpty());
    // test getBatch
    batchInfo = batches.get(0);
    batchInfo = getBatchInfo(batchInfo);
    // test getRequest
    InputStream requestStream = template().requestBody("direct:getRequest", batchInfo, InputStream.class);
    assertNotNull("Null batch request", requestStream);
    // wait for batch to finish
    log.info("Waiting for batch to finish...");
    while (!batchProcessed(batchInfo)) {
        // sleep 5 seconds
        Thread.sleep(5000);
        // check again
        batchInfo = getBatchInfo(batchInfo);
    }
    log.info("Batch finished with state " + batchInfo.getState());
    assertEquals("Batch did not succeed", BatchStateEnum.COMPLETED, batchInfo.getState());
    // test getResults
    InputStream results = template().requestBody("direct:getResults", batchInfo, InputStream.class);
    assertNotNull("Null batch results", results);
    // close the test job
    template().requestBody("direct:closeJob", jobInfo, JobInfo.class);
}
Also used : JobInfo(org.apache.camel.component.salesforce.api.dto.bulk.JobInfo) HashMap(java.util.HashMap) InputStream(java.io.InputStream) Merchandise__c(org.apache.camel.component.salesforce.dto.generated.Merchandise__c) BatchInfo(org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo) Theory(org.junit.experimental.theories.Theory)

Example 8 with BatchInfo

use of org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo in project camel by apache.

the class BulkApiQueryIntegrationTest method testQueryLifecycle.

@Theory
public void testQueryLifecycle(ContentType contentType) throws Exception {
    log.info("Testing Query lifecycle with {} content", contentType);
    // create a QUERY test Job
    JobInfo jobInfo = new JobInfo();
    jobInfo.setOperation(OperationEnum.QUERY);
    jobInfo.setContentType(contentType);
    jobInfo.setObject(Merchandise__c.class.getSimpleName());
    jobInfo = createJob(jobInfo);
    // test createQuery
    BatchInfo batchInfo = template().requestBody("direct:createBatchQuery", jobInfo, BatchInfo.class);
    assertNotNull("Null batch query", batchInfo);
    assertNotNull("Null batch query id", batchInfo.getId());
    // test getRequest
    InputStream requestStream = template().requestBody("direct:getRequest", batchInfo, InputStream.class);
    assertNotNull("Null batch request", requestStream);
    // wait for batch to finish
    log.info("Waiting for query batch to finish...");
    while (!batchProcessed(batchInfo)) {
        // sleep 5 seconds
        Thread.sleep(5000);
        // check again
        batchInfo = getBatchInfo(batchInfo);
    }
    log.info("Query finished with state " + batchInfo.getState());
    assertEquals("Query did not succeed", BatchStateEnum.COMPLETED, batchInfo.getState());
    // test getQueryResultList
    @SuppressWarnings("unchecked") List<String> resultIds = template().requestBody("direct:getQueryResultIds", batchInfo, List.class);
    assertNotNull("Null query result ids", resultIds);
    assertFalse("Empty result ids", resultIds.isEmpty());
    // test getQueryResult
    for (String resultId : resultIds) {
        InputStream results = template().requestBodyAndHeader("direct:getQueryResult", batchInfo, SalesforceEndpointConfig.RESULT_ID, resultId, InputStream.class);
        assertNotNull("Null query result", results);
    }
    // close the test job
    template().requestBody("direct:closeJob", jobInfo, JobInfo.class);
}
Also used : JobInfo(org.apache.camel.component.salesforce.api.dto.bulk.JobInfo) InputStream(java.io.InputStream) Merchandise__c(org.apache.camel.component.salesforce.dto.generated.Merchandise__c) BatchInfo(org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo) Theory(org.junit.experimental.theories.Theory)

Example 9 with BatchInfo

use of org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo in project camel by apache.

the class BulkApiProcessor method processCreateBatchQuery.

private void processCreateBatchQuery(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    JobInfo jobBody;
    String jobId;
    ContentType contentType;
    jobBody = exchange.getIn().getBody(JobInfo.class);
    String soqlQuery;
    if (jobBody != null) {
        jobId = jobBody.getId();
        contentType = jobBody.getContentType();
        // use SOQL query from header or endpoint config
        soqlQuery = getParameter(SOBJECT_QUERY, exchange, IGNORE_BODY, NOT_OPTIONAL);
    } else {
        jobId = getParameter(JOB_ID, exchange, IGNORE_BODY, NOT_OPTIONAL);
        contentType = ContentType.fromValue(getParameter(CONTENT_TYPE, exchange, IGNORE_BODY, NOT_OPTIONAL));
        // reuse SOBJECT_QUERY property
        soqlQuery = getParameter(SOBJECT_QUERY, exchange, USE_BODY, NOT_OPTIONAL);
    }
    bulkClient.createBatchQuery(jobId, soqlQuery, contentType, new BulkApiClient.BatchInfoResponseCallback() {

        @Override
        public void onResponse(BatchInfo batchInfo, SalesforceException ex) {
            processResponse(exchange, batchInfo, ex, callback);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) ContentType(org.apache.camel.component.salesforce.api.dto.bulk.ContentType) JobInfo(org.apache.camel.component.salesforce.api.dto.bulk.JobInfo) DefaultBulkApiClient(org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient) BulkApiClient(org.apache.camel.component.salesforce.internal.client.BulkApiClient) BatchInfo(org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)

Example 10 with BatchInfo

use of org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo in project camel by apache.

the class BulkApiProcessor method processGetQueryResultIds.

private void processGetQueryResultIds(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    String jobId;
    BatchInfo batchBody;
    String batchId;
    batchBody = exchange.getIn().getBody(BatchInfo.class);
    if (batchBody != null) {
        jobId = batchBody.getJobId();
        batchId = batchBody.getId();
    } else {
        jobId = getParameter(JOB_ID, exchange, IGNORE_BODY, NOT_OPTIONAL);
        batchId = getParameter(BATCH_ID, exchange, USE_BODY, NOT_OPTIONAL);
    }
    bulkClient.getQueryResultIds(jobId, batchId, new BulkApiClient.QueryResultIdsCallback() {

        @Override
        public void onResponse(List<String> ids, SalesforceException ex) {
            processResponse(exchange, ids, ex, callback);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) DefaultBulkApiClient(org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient) BulkApiClient(org.apache.camel.component.salesforce.internal.client.BulkApiClient) BatchInfo(org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)

Aggregations

BatchInfo (org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)13 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)11 InputStream (java.io.InputStream)9 BulkApiClient (org.apache.camel.component.salesforce.internal.client.BulkApiClient)8 DefaultBulkApiClient (org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient)8 JobInfo (org.apache.camel.component.salesforce.api.dto.bulk.JobInfo)4 IOException (java.io.IOException)3 StreamCache (org.apache.camel.StreamCache)3 Request (org.eclipse.jetty.client.api.Request)3 ContentType (org.apache.camel.component.salesforce.api.dto.bulk.ContentType)2 Merchandise__c (org.apache.camel.component.salesforce.dto.generated.Merchandise__c)2 Theory (org.junit.experimental.theories.Theory)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 CamelException (org.apache.camel.CamelException)1 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)1 InputStreamContentProvider (org.eclipse.jetty.client.util.InputStreamContentProvider)1