Search in sources :

Example 1 with JobInfo

use of com.sforce.async.JobInfo in project components by Talend.

the class SalesforceBulkRuntime method closeJob.

/**
 * Close the job
 *
 * @throws AsyncApiException
 * @throws ConnectionException
 */
public void closeJob() throws AsyncApiException, ConnectionException {
    JobInfo closeJob = new JobInfo();
    closeJob.setId(job.getId());
    closeJob.setState(JobStateEnum.Closed);
    try {
        bulkConnection.updateJob(closeJob);
    } catch (AsyncApiException sfException) {
        if (AsyncExceptionCode.InvalidSessionId.equals(sfException.getExceptionCode())) {
            SalesforceRuntimeCommon.renewSession(bulkConnection.getConfig());
            closeJob();
        } else if (AsyncExceptionCode.InvalidJobState.equals(sfException.getExceptionCode())) {
            // Job is already closed on Salesforce side. We don't need to close it again.
            return;
        }
        throw sfException;
    }
}
Also used : JobInfo(com.sforce.async.JobInfo) AsyncApiException(com.sforce.async.AsyncApiException)

Example 2 with JobInfo

use of com.sforce.async.JobInfo in project components by Talend.

the class SalesforceBulkRuntime method createJob.

/**
 * Create a new job using the Bulk API.
 *
 * @return The JobInfo for the new job.
 * @throws AsyncApiException
 * @throws ConnectionException
 */
private JobInfo createJob() throws AsyncApiException, ConnectionException {
    JobInfo job = new JobInfo();
    if (concurrencyMode != null) {
        job.setConcurrencyMode(concurrencyMode);
    }
    job.setObject(sObjectType);
    job.setOperation(operation);
    if (OperationEnum.upsert.equals(operation)) {
        job.setExternalIdFieldName(externalIdFieldName);
    }
    job.setContentType(contentType);
    job = createJob(job);
    return job;
}
Also used : JobInfo(com.sforce.async.JobInfo)

Example 3 with JobInfo

use of com.sforce.async.JobInfo in project teiid by teiid.

the class TestQueryExecutionImpl method testBulkFlow.

@Test
public void testBulkFlow() throws Exception {
    // $NON-NLS-1$
    Select command = (Select) translationUtility.parseCommand("select Name from Account");
    SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
    JobInfo jobInfo = Mockito.mock(JobInfo.class);
    Mockito.when(connection.createBulkJob(Mockito.anyString(), Mockito.eq(OperationEnum.query), Mockito.eq(true))).thenReturn(jobInfo);
    final BatchResultInfo info = new BatchResultInfo("x");
    Mockito.when(connection.getBatchQueryResults(Mockito.anyString(), Mockito.eq(info))).thenAnswer(new Answer<BulkBatchResult>() {

        boolean first = true;

        @Override
        public BulkBatchResult answer(InvocationOnMock invocation) throws Throwable {
            if (first) {
                first = false;
                throw new DataNotAvailableException();
            }
            if (info.getAndIncrementResultNum() == 0) {
                final Iterator<List<String>> i = Arrays.asList(Arrays.asList("Name"), Arrays.asList("X")).iterator();
                return new BulkBatchResult() {

                    @Override
                    public List<String> nextRecord() throws IOException {
                        if (!i.hasNext()) {
                            return null;
                        }
                        return i.next();
                    }

                    @Override
                    public void close() {
                    }
                };
            }
            return null;
        }
    });
    Mockito.when(connection.addBatch("SELECT Account.Name FROM Account", jobInfo)).thenReturn(info);
    ExecutionContext mock = Mockito.mock(ExecutionContext.class);
    Mockito.stub(mock.getSourceHints()).toReturn(Arrays.asList("bulk"));
    QueryExecutionImpl execution = new QueryExecutionImpl(command, connection, Mockito.mock(RuntimeMetadata.class), mock, new SalesForceExecutionFactory());
    execution.execute();
    try {
        execution.next();
        fail();
    } catch (DataNotAvailableException e) {
    }
    List<?> row = execution.next();
    assertEquals(Arrays.asList("X"), row);
    assertNull(execution.next());
}
Also used : SalesForceExecutionFactory(org.teiid.translator.salesforce.SalesForceExecutionFactory) BatchResultInfo(org.teiid.translator.salesforce.SalesforceConnection.BatchResultInfo) IOException(java.io.IOException) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ExecutionContext(org.teiid.translator.ExecutionContext) JobInfo(com.sforce.async.JobInfo) BulkBatchResult(org.teiid.translator.salesforce.SalesforceConnection.BulkBatchResult) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Select(org.teiid.language.Select) Iterator(java.util.Iterator) List(java.util.List) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) Test(org.junit.Test)

Example 4 with JobInfo

use of com.sforce.async.JobInfo in project tdi-studio-se by Talend.

the class SalesforceBulkAPI method closeJob.

private void closeJob() throws AsyncApiException, ConnectionException {
    JobInfo closeJob = new JobInfo();
    closeJob.setId(job.getId());
    closeJob.setState(JobStateEnum.Closed);
    connection.updateJob(closeJob);
}
Also used : JobInfo(com.sforce.async.JobInfo)

Example 5 with JobInfo

use of com.sforce.async.JobInfo in project tdi-studio-se by Talend.

the class SalesforceBulkAPI method doBulkQuery.

public void doBulkQuery(String moduleName, String queryStatement, int secToWait) throws AsyncApiException, InterruptedException, ConnectionException {
    job = new JobInfo();
    job.setObject(moduleName);
    job.setOperation(OperationEnum.query);
    if (concurrencyMode != null) {
        job.setConcurrencyMode(concurrencyMode);
    }
    job.setContentType(ContentType.CSV);
    job = connection.createJob(job);
    job = connection.getJobStatus(job.getId());
    batchInfoList = new ArrayList<BatchInfo>();
    BatchInfo info = null;
    ByteArrayInputStream bout = new ByteArrayInputStream(queryStatement.getBytes());
    info = connection.createBatchFromStream(job, bout);
    while (true) {
        // default is 30 sec
        Thread.sleep(secToWait * 1000);
        info = connection.getBatchInfo(job.getId(), info.getId());
        if (info.getState() == BatchStateEnum.Completed) {
            QueryResultList list = connection.getQueryResultList(job.getId(), info.getId());
            queryResultIDs = list.getResult();
            break;
        } else if (info.getState() == BatchStateEnum.Failed) {
            throw new RuntimeException("-------------- failed ----------" + info);
        } else {
            System.out.println("-------------- waiting ----------" + info);
        }
    }
    batchInfoList.add(info);
    // For TDI-27909
    closeJob();
}
Also used : QueryResultList(com.sforce.async.QueryResultList) JobInfo(com.sforce.async.JobInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) BatchInfo(com.sforce.async.BatchInfo)

Aggregations

JobInfo (com.sforce.async.JobInfo)9 Test (org.junit.Test)3 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)3 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)3 ExecutionContext (org.teiid.translator.ExecutionContext)3 SalesForceExecutionFactory (org.teiid.translator.salesforce.SalesForceExecutionFactory)3 SalesforceConnection (org.teiid.translator.salesforce.SalesforceConnection)3 BatchInfo (com.sforce.async.BatchInfo)2 BatchResult (com.sforce.async.BatchResult)2 Result (com.sforce.async.Result)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 ColumnReference (org.teiid.language.ColumnReference)2 Expression (org.teiid.language.Expression)2 ExpressionValueSource (org.teiid.language.ExpressionValueSource)2 Insert (org.teiid.language.Insert)2 NamedTable (org.teiid.language.NamedTable)2 Parameter (org.teiid.language.Parameter)2 Table (org.teiid.metadata.Table)2 AsyncApiException (com.sforce.async.AsyncApiException)1