Search in sources :

Example 1 with GetUpdatedResult

use of com.sforce.soap.partner.GetUpdatedResult in project pentaho-kettle by pentaho.

the class SalesforceConnection method query.

public void query(boolean specifyQuery) throws KettleException {
    if (getBinding() == null) {
        throw new KettleException(BaseMessages.getString(PKG, "SalesforceInput.Exception.CanNotGetBiding"));
    }
    try {
        if (!specifyQuery) {
            // check if we can query this Object
            DescribeSObjectResult describeSObjectResult = getBinding().describeSObject(getModule());
            if (describeSObjectResult == null) {
                throw new KettleException(BaseMessages.getString(PKG, "SalesforceInput.ErrorGettingObject"));
            }
            if (!describeSObjectResult.isQueryable()) {
                throw new KettleException(BaseMessages.getString(PKG, "SalesforceInputDialog.ObjectNotQueryable", module));
            }
            if (this.recordsFilter == SalesforceConnectionUtils.RECORDS_FILTER_UPDATED || this.recordsFilter == SalesforceConnectionUtils.RECORDS_FILTER_DELETED) {
                // The object must be replicateable
                if (!describeSObjectResult.isReplicateable()) {
                    throw new KettleException(BaseMessages.getString(PKG, "SalesforceInput.Error.ObjectNotReplicateable", getModule()));
                }
            }
        }
        if (getSQL() != null && log.isDetailed()) {
            log.logDetailed(BaseMessages.getString(PKG, "SalesforceInput.Log.SQLString") + " : " + getSQL());
        }
        switch(this.recordsFilter) {
            case SalesforceConnectionUtils.RECORDS_FILTER_UPDATED:
                // Updated records ...
                GetUpdatedResult updatedRecords = getBinding().getUpdated(getModule(), this.startDate, this.endDate);
                if (updatedRecords.getIds() != null) {
                    int nr = updatedRecords.getIds().length;
                    if (nr > 0) {
                        String[] ids = updatedRecords.getIds();
                        // We can pass a maximum of 2000 object IDs
                        if (nr > SalesforceConnectionUtils.MAX_UPDATED_OBJECTS_IDS) {
                            this.sObjects = new SObject[nr];
                            List<String> list = new ArrayList<String>();
                            int desPos = 0;
                            for (int i = 0; i < nr; i++) {
                                list.add(updatedRecords.getIds()[i]);
                                if (i % SalesforceConnectionUtils.MAX_UPDATED_OBJECTS_IDS == 0 || i == nr - 1) {
                                    SObject[] s = getBinding().retrieve(this.fieldsList, getModule(), list.toArray(new String[list.size()]));
                                    System.arraycopy(s, 0, this.sObjects, desPos, s.length);
                                    desPos += s.length;
                                    s = null;
                                    list = new ArrayList<String>();
                                }
                            }
                        } else {
                            this.sObjects = getBinding().retrieve(this.fieldsList, getModule(), ids);
                        }
                        if (this.sObjects != null) {
                            this.queryResultSize = this.sObjects.length;
                        }
                    }
                }
                break;
            case SalesforceConnectionUtils.RECORDS_FILTER_DELETED:
                // Deleted records ...
                GetDeletedResult deletedRecordsResult = getBinding().getDeleted(getModule(), this.startDate, this.endDate);
                DeletedRecord[] deletedRecords = deletedRecordsResult.getDeletedRecords();
                if (log.isDebug()) {
                    log.logDebug(toString(), BaseMessages.getString(PKG, "SalesforceConnection.DeletedRecordsFound", String.valueOf(deletedRecords == null ? 0 : deletedRecords.length)));
                }
                if (deletedRecords != null && deletedRecords.length > 0) {
                    getDeletedList = new HashMap<String, Date>();
                    for (DeletedRecord dr : deletedRecords) {
                        getDeletedList.put(dr.getId(), dr.getDeletedDate().getTime());
                    }
                    this.qr = getBinding().queryAll(getSQL());
                    this.sObjects = getQueryResult().getRecords();
                    if (this.sObjects != null) {
                        this.queryResultSize = this.sObjects.length;
                    }
                }
                break;
            default:
                // return query result
                this.qr = isQueryAll() ? getBinding().queryAll(getSQL()) : getBinding().query(getSQL());
                this.sObjects = getQueryResult().getRecords();
                this.queryResultSize = getQueryResult().getSize();
                break;
        }
        if (this.sObjects != null) {
            this.recordsCount = this.sObjects.length;
        }
    } catch (Exception e) {
        log.logError(Const.getStackTracker(e));
        throw new KettleException(BaseMessages.getString(PKG, "SalesforceConnection.Exception.Query"), e);
    }
}
Also used : GetUpdatedResult(com.sforce.soap.partner.GetUpdatedResult) KettleException(org.pentaho.di.core.exception.KettleException) DeletedRecord(com.sforce.soap.partner.DeletedRecord) DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) ArrayList(java.util.ArrayList) Date(java.util.Date) SOAPException(javax.xml.soap.SOAPException) KettleException(org.pentaho.di.core.exception.KettleException) ConnectionException(com.sforce.ws.ConnectionException) IOException(java.io.IOException) SObject(com.sforce.soap.partner.sobject.SObject) GetDeletedResult(com.sforce.soap.partner.GetDeletedResult)

Example 2 with GetUpdatedResult

use of com.sforce.soap.partner.GetUpdatedResult in project components by Talend.

the class SalesforceGetDeletedUpdatedReaderTestIT method testSplitIdsList.

/**
 * For tSalesforceGetUpdated splitIds
 */
@Test
public void testSplitIdsList() throws Throwable {
    SalesforceGetUpdatedReader updatedReader = new SalesforceGetUpdatedReader(null, null, createSalesforceGetDeletedUpdatedProperties(false));
    GetUpdatedResult updatedResult = new GetUpdatedResult();
    // 1. ids size 0
    List<String[]> updatedIdList = updatedReader.splitIds(updatedResult);
    assertEquals(0, updatedIdList.size());
    // 2.ids size 1999
    List<String> updteIds = new ArrayList<>();
    for (int i = 1000; i < 2999; i++) {
        updteIds.add("0019000001fvZV" + i);
    }
    updatedResult.setIds(updteIds.toArray(new String[0]));
    updatedIdList = updatedReader.splitIds(updatedResult);
    assertEquals(1, updatedIdList.size());
    assertEquals(1999, updatedIdList.get(0).length);
    // 3.ids size 2000
    updteIds.add("0019000001fvZV2999");
    updatedResult.setIds(updteIds.toArray(new String[0]));
    updatedIdList = updatedReader.splitIds(updatedResult);
    assertEquals(1, updatedIdList.size());
    assertEquals(2000, updatedIdList.get(0).length);
    // 4.ids size 2001
    updteIds.add("0019000001fvZV3000");
    updatedResult.setIds(updteIds.toArray(new String[0]));
    updatedIdList = updatedReader.splitIds(updatedResult);
    assertEquals(2, updatedIdList.size());
    assertEquals(2000, updatedIdList.get(0).length);
    assertEquals(1, updatedIdList.get(1).length);
    assertEquals("Id, Name, ShippingStreet, ShippingPostalCode, BillingStreet, BillingState, BillingPostalCode", updatedReader.getFieldNamesStr());
}
Also used : GetUpdatedResult(com.sforce.soap.partner.GetUpdatedResult) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

GetUpdatedResult (com.sforce.soap.partner.GetUpdatedResult)2 ArrayList (java.util.ArrayList)2 DeletedRecord (com.sforce.soap.partner.DeletedRecord)1 DescribeSObjectResult (com.sforce.soap.partner.DescribeSObjectResult)1 GetDeletedResult (com.sforce.soap.partner.GetDeletedResult)1 SObject (com.sforce.soap.partner.sobject.SObject)1 ConnectionException (com.sforce.ws.ConnectionException)1 IOException (java.io.IOException)1 Date (java.util.Date)1 SOAPException (javax.xml.soap.SOAPException)1 Test (org.junit.Test)1 KettleException (org.pentaho.di.core.exception.KettleException)1