|
35 | 35 | import org.teiid.logging.LogConstants;
|
36 | 36 | import org.teiid.logging.LogManager;
|
37 | 37 | import org.teiid.resource.spi.BasicConnection;
|
| 38 | +import org.teiid.translator.DataNotAvailableException; |
38 | 39 | import org.teiid.translator.salesforce.SalesforceConnection;
|
39 | 40 | import org.teiid.translator.salesforce.execution.DataPayload;
|
40 | 41 | import org.teiid.translator.salesforce.execution.DeletedObject;
|
41 | 42 | import org.teiid.translator.salesforce.execution.DeletedResult;
|
42 | 43 | import org.teiid.translator.salesforce.execution.UpdatedResult;
|
43 | 44 |
|
| 45 | +import com.sforce.async.AsyncApiException; |
| 46 | +import com.sforce.async.BatchInfo; |
| 47 | +import com.sforce.async.BatchInfoList; |
| 48 | +import com.sforce.async.BatchRequest; |
| 49 | +import com.sforce.async.BatchResult; |
| 50 | +import com.sforce.async.BulkConnection; |
| 51 | +import com.sforce.async.ContentType; |
| 52 | +import com.sforce.async.JobInfo; |
| 53 | +import com.sforce.async.OperationEnum; |
44 | 54 | import com.sforce.soap.partner.*;
|
45 | 55 | import com.sforce.soap.partner.sobject.SObject;
|
| 56 | +import com.sforce.ws.ConnectorConfig; |
46 | 57 |
|
47 | 58 | public class SalesforceConnectionImpl extends BasicConnection implements SalesforceConnection {
|
48 | 59 | private Soap sfSoap;
|
| 60 | + private BulkConnection bulkConnection; |
49 | 61 |
|
50 | 62 | private ObjectFactory partnerFactory = new ObjectFactory();
|
51 | 63 |
|
@@ -99,12 +111,15 @@ private void login(String username, String password, URL url, SalesForceManagedC
|
99 | 111 |
|
100 | 112 | // Set the SessionId after login, for subsequent calls
|
101 | 113 | sh.setSessionId(loginResult.getSessionId());
|
| 114 | + this.bulkConnection = getBulkConnection(loginResult.getServerUrl(), loginResult.getSessionId()); |
102 | 115 | } catch (LoginFault e) {
|
103 | 116 | throw new ResourceException(e);
|
104 | 117 | } catch (InvalidIdFault e) {
|
105 | 118 | throw new ResourceException(e);
|
106 | 119 | } catch (com.sforce.soap.partner.UnexpectedErrorFault e) {
|
107 | 120 | throw new ResourceException(e);
|
| 121 | + } catch(AsyncApiException e) { |
| 122 | + throw new ResourceException(e); |
108 | 123 | } finally {
|
109 | 124 | BusFactory.setThreadDefaultBus(bus);
|
110 | 125 | }
|
@@ -375,4 +390,72 @@ public void close() throws ResourceException {
|
375 | 390 | public boolean isAlive() {
|
376 | 391 | return isValid();
|
377 | 392 | }
|
| 393 | + |
| 394 | + private JobInfo createBulkJob(String objectName) throws ResourceException { |
| 395 | + try { |
| 396 | + JobInfo job = new JobInfo(); |
| 397 | + job.setObject(objectName); |
| 398 | + job.setOperation(OperationEnum.insert); |
| 399 | + job.setContentType(ContentType.XML); |
| 400 | + return this.bulkConnection.createJob(job); |
| 401 | + } catch (AsyncApiException e) { |
| 402 | + throw new ResourceException(e); |
| 403 | + } |
| 404 | + } |
| 405 | + |
| 406 | + @Override |
| 407 | + public JobInfo executeBulkJob(String objectName, List<com.sforce.async.SObject> payload) throws ResourceException { |
| 408 | + try { |
| 409 | + JobInfo job = createBulkJob(objectName); |
| 410 | + BatchRequest request = this.bulkConnection.createBatch(job); |
| 411 | + request.addSObjects(payload.toArray(new com.sforce.async.SObject[payload.size()])); |
| 412 | + request.completeRequest(); |
| 413 | + return this.bulkConnection.closeJob(job.getId()); |
| 414 | + } catch (AsyncApiException e) { |
| 415 | + throw new ResourceException(e); |
| 416 | + } |
| 417 | + } |
| 418 | + |
| 419 | + @Override |
| 420 | + public BatchResult getBulkResults(JobInfo job) throws ResourceException { |
| 421 | + try { |
| 422 | + BatchInfoList batchInfo = this.bulkConnection.getBatchInfoList(job.getId()); |
| 423 | + BatchInfo[] batches = batchInfo.getBatchInfo(); |
| 424 | + if (batches.length > 0) { |
| 425 | + BatchResult batchResult = this.bulkConnection.getBatchResult(job.getId(), batches[0].getId()); |
| 426 | + if (batchResult.isPartialResult()) { |
| 427 | + throw new DataNotAvailableException(500); |
| 428 | + } |
| 429 | + return batchResult; |
| 430 | + } |
| 431 | + throw new DataNotAvailableException(500); |
| 432 | + } catch (AsyncApiException e) { |
| 433 | + throw new ResourceException(e); |
| 434 | + } |
| 435 | + } |
| 436 | + |
| 437 | + private BulkConnection getBulkConnection(String endpoint, String sessionid) throws AsyncApiException { |
| 438 | + ConnectorConfig config = new ConnectorConfig(); |
| 439 | + config.setSessionId(sessionid); |
| 440 | + // The endpoint for the Bulk API service is the same as for the normal |
| 441 | + // SOAP uri until the /Soap/ part. From here it's '/async/versionNumber' |
| 442 | + int index = endpoint.indexOf("Soap/u/"); //$NON-NLS-1$ |
| 443 | + int endIndex = endpoint.indexOf('/', index+7); |
| 444 | + String apiVersion = endpoint.substring(index+7,endIndex); |
| 445 | + String restEndpoint = endpoint.substring(0, endpoint.indexOf("Soap/"))+ "async/" + apiVersion;//$NON-NLS-1$ //$NON-NLS-2$ |
| 446 | + config.setRestEndpoint(restEndpoint); |
| 447 | + config.setCompression(true); |
| 448 | + config.setTraceMessage(false); |
| 449 | + BulkConnection connection = new BulkConnection(config); |
| 450 | + return connection; |
| 451 | + } |
| 452 | + |
| 453 | + @Override |
| 454 | + public void cancelBulkJob(JobInfo job) throws ResourceException { |
| 455 | + try { |
| 456 | + this.bulkConnection.abortJob(job.getId()); |
| 457 | + } catch (AsyncApiException e) { |
| 458 | + throw new ResourceException(e); |
| 459 | + } |
| 460 | + } |
378 | 461 | }
|
0 commit comments