Search in sources :

Example 31 with SObject

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

the class SalesforceUpsert method writeToSalesForce.

@VisibleForTesting
void writeToSalesForce(Object[] rowData) throws KettleException {
    try {
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "SalesforceUpsert.CalledWrite", data.iBufferPos, meta.getBatchSizeInt()));
        }
        // if there is room in the buffer
        if (data.iBufferPos < meta.getBatchSizeInt()) {
            // Reserve for empty fields
            ArrayList<String> fieldsToNull = new ArrayList<String>();
            ArrayList<XmlObject> upsertfields = new ArrayList<>();
            // Add fields to update
            for (int i = 0; i < data.nrfields; i++) {
                ValueMetaInterface valueMeta = data.inputRowMeta.getValueMeta(data.fieldnrs[i]);
                Object object = rowData[data.fieldnrs[i]];
                if (valueMeta.isNull(object)) {
                    // The value is null
                    // We need to keep track of this field
                    fieldsToNull.add(SalesforceUtils.getFieldToNullName(log, meta.getUpdateLookup()[i], meta.getUseExternalId()[i]));
                } else {
                    Object normalObject = normalizeValue(valueMeta, rowData[data.fieldnrs[i]]);
                    upsertfields.add(SalesforceConnection.createMessageElement(meta.getUpdateLookup()[i], normalObject, meta.getUseExternalId()[i]));
                }
            }
            // build the SObject
            SObject sobjPass = new SObject();
            sobjPass.setType(data.connection.getModule());
            if (upsertfields.size() > 0) {
                for (XmlObject element : upsertfields) {
                    setFieldInSObject(sobjPass, element);
                }
            }
            if (fieldsToNull.size() > 0) {
                // Set Null to fields
                sobjPass.setFieldsToNull(fieldsToNull.toArray(new String[fieldsToNull.size()]));
            }
            // Load the buffer array
            data.sfBuffer[data.iBufferPos] = sobjPass;
            data.outputBuffer[data.iBufferPos] = rowData;
            data.iBufferPos++;
        }
        if (data.iBufferPos >= meta.getBatchSizeInt()) {
            if (log.isDetailed()) {
                logDetailed("Calling flush buffer from writeToSalesForce");
            }
            flushBuffers();
        }
    } catch (KettleException ke) {
        throw ke;
    } catch (Exception e) {
        throw new KettleException(BaseMessages.getString(PKG, "SalesforceUpsert.FailedInWrite", e.toString()));
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) ArrayList(java.util.ArrayList) SObject(com.sforce.soap.partner.sobject.SObject) XmlObject(com.sforce.ws.bind.XmlObject) XmlObject(com.sforce.ws.bind.XmlObject) SObject(com.sforce.soap.partner.sobject.SObject) KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 32 with SObject

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

the class SalesforceConnectionTest method testGetRecordValue.

// PDI-15973
@Test
public void testGetRecordValue() throws Exception {
    // PDI-15973
    SalesforceConnection conn = mock(SalesforceConnection.class, Mockito.CALLS_REAL_METHODS);
    SObject sObject = new SObject();
    sObject.setName(new QName(Constants.PARTNER_SOBJECT_NS, "sObject"));
    SObject testObject = createObject("field", "value");
    sObject.addField("field", testObject);
    assertEquals("Get value of simple record", "value", conn.getRecordValue(sObject, "field"));
    SObject parentObject = createObject("parentField", null);
    sObject.addField("parentField", parentObject);
    SObject childObject = createObject("subField", "subValue");
    parentObject.addField("subField", childObject);
    assertEquals("Get value of record with hierarchy", "subValue", conn.getRecordValue(sObject, "parentField.subField"));
    XmlObject nullObject = new XmlObject(new QName("nullField"));
    sObject.addField("nullField", nullObject);
    assertEquals("Get null value when relational query id is null", null, conn.getRecordValue(sObject, "nullField.childField"));
}
Also used : QName(javax.xml.namespace.QName) SObject(com.sforce.soap.partner.sobject.SObject) XmlObject(com.sforce.ws.bind.XmlObject) Test(org.junit.Test)

Example 33 with SObject

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

the class PDI_10836_Test method testDateInsert.

@Test
public void testDateInsert() throws Exception {
    SalesforceInsert step = new SalesforceInsert(smh.stepMeta, smh.stepDataInterface, 0, smh.transMeta, smh.trans);
    SalesforceInsertMeta meta = smh.initStepMetaInterface;
    doReturn(UUID.randomUUID().toString()).when(meta).getTargetURL();
    doReturn(UUID.randomUUID().toString()).when(meta).getUsername();
    doReturn(UUID.randomUUID().toString()).when(meta).getPassword();
    doReturn(UUID.randomUUID().toString()).when(meta).getModule();
    doReturn(2).when(meta).getBatchSizeInt();
    doReturn(new String[] { "Date" }).when(meta).getUpdateLookup();
    doReturn(new Boolean[] { false }).when(meta).getUseExternalId();
    SalesforceInsertData data = smh.initStepDataInterface;
    data.nrfields = 1;
    data.fieldnrs = new int[] { 0 };
    data.sfBuffer = new SObject[] { null };
    data.outputBuffer = new Object[][] { null };
    step.init(meta, data);
    RowMeta rowMeta = new RowMeta();
    ValueMetaInterface valueMeta = new ValueMetaDate("date");
    valueMeta.setDateFormatTimeZone(TimeZone.getTimeZone("Europe/Minsk"));
    rowMeta.addValueMeta(valueMeta);
    smh.initStepDataInterface.inputRowMeta = rowMeta;
    Calendar minskTime = Calendar.getInstance(valueMeta.getDateFormatTimeZone());
    minskTime.clear();
    minskTime.set(2013, Calendar.OCTOBER, 16);
    Object[] args = new Object[] { minskTime.getTime() };
    Method m = SalesforceInsert.class.getDeclaredMethod("writeToSalesForce", Object[].class);
    m.setAccessible(true);
    m.invoke(step, new Object[] { args });
    DateFormat utc = new SimpleDateFormat("yyyy-MM-dd");
    utc.setTimeZone(TimeZone.getTimeZone("UTC"));
    XmlObject xmlObject = SalesforceConnection.getChildren(data.sfBuffer[0])[0];
    Assert.assertEquals("2013-10-16", utc.format(((Calendar) xmlObject.getValue()).getTime()));
}
Also used : RowMeta(org.pentaho.di.core.row.RowMeta) Calendar(java.util.Calendar) Method(java.lang.reflect.Method) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) XmlObject(com.sforce.ws.bind.XmlObject) SObject(com.sforce.soap.partner.sobject.SObject) XmlObject(com.sforce.ws.bind.XmlObject) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 34 with SObject

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

the class SalesforceRuntimeTestUtil method createTestData.

public List<String> createTestData() throws ConnectionException {
    this.login(SalesforceConnectionProperties.URL);
    List<String> ids = new ArrayList<String>();
    try {
        List<SObject> contacts = new ArrayList<SObject>();
        for (Map<String, String> row : testData) {
            SObject contact = new SObject();
            contact.setType(module);
            contact.setField("FirstName", row.get("FirstName"));
            contact.setField("LastName", row.get("LastName"));
            contact.setField("Phone", row.get("Phone"));
            contacts.add(contact);
        }
        SaveResult[] results = partnerConnection.create(contacts.toArray(new SObject[0]));
        for (SaveResult result : results) {
            if (result.isSuccess()) {
                ids.add(result.getId());
            } else {
                for (int i = 0; i < result.getErrors().length; i++) {
                    com.sforce.soap.partner.Error err = result.getErrors()[i];
                    Assert.fail(err.getMessage());
                }
            }
        }
    } catch (ConnectionException ce) {
        Assert.fail(ce.getMessage());
    }
    return ids;
}
Also used : ArrayList(java.util.ArrayList) SaveResult(com.sforce.soap.partner.SaveResult) SObject(com.sforce.soap.partner.sobject.SObject) ConnectionException(com.sforce.ws.ConnectionException)

Example 35 with SObject

use of com.sforce.soap.partner.sobject.SObject in project teiid by teiid.

the class AbstractUpdateExecution method execute.

void execute(Condition criteria, IQueryProvidingVisitor visitor) throws TranslatorException {
    // Salesforce limit
    int batchSize = 2000;
    // Salesforce limit
    int updateSize = 200;
    String[] Ids = null;
    if (visitor.hasOnlyIDCriteria()) {
        try {
            String Id = ((Comparison) criteria).getRightExpression().toString();
            Id = Util.stripQutes(Id);
            Ids = new String[] { Id };
            result = processIds(Ids, visitor);
        } catch (ClassCastException cce) {
            throw new RuntimeException(SalesForcePlugin.Util.gs(SalesForcePlugin.Event.TEIID13008));
        } catch (ResourceException e) {
            throw new TranslatorException(e);
        }
    } else {
        try {
            String query = visitor.getQuery();
            context.logCommand(query);
            QueryResult results = getConnection().query(query, batchSize, Boolean.FALSE);
            ArrayList<String> idList = new ArrayList<String>(results.getRecords().length);
            while (results != null) {
                if (results.getSize() > 0) {
                    for (int i = 0; i < results.getRecords().length; i++) {
                        SObject sObject = results.getRecords()[i];
                        idList.add(sObject.getId());
                        if (idList.size() == updateSize) {
                            Ids = idList.toArray(new String[0]);
                            result += processIds(Ids, visitor);
                            idList.clear();
                        }
                    }
                }
                if (results.isDone()) {
                    break;
                }
                results = connection.queryMore(results.getQueryLocator(), batchSize);
            }
            if (!idList.isEmpty()) {
                Ids = idList.toArray(new String[0]);
                result += processIds(Ids, visitor);
            }
        } catch (ResourceException e) {
            throw new TranslatorException(e);
        }
    }
}
Also used : QueryResult(com.sforce.soap.partner.QueryResult) ArrayList(java.util.ArrayList) SObject(com.sforce.soap.partner.sobject.SObject) ResourceException(javax.resource.ResourceException) TranslatorException(org.teiid.translator.TranslatorException)

Aggregations

SObject (com.sforce.soap.partner.sobject.SObject)45 XmlObject (com.sforce.ws.bind.XmlObject)23 Test (org.junit.Test)11 KettleException (org.pentaho.di.core.exception.KettleException)11 ArrayList (java.util.ArrayList)10 ConnectionException (com.sforce.ws.ConnectionException)9 KettleStepException (org.pentaho.di.core.exception.KettleStepException)9 QueryResult (com.sforce.soap.partner.QueryResult)5 IOException (java.io.IOException)5 ResourceException (javax.resource.ResourceException)5 QName (javax.xml.namespace.QName)5 Schema (org.apache.avro.Schema)5 SaveResult (com.sforce.soap.partner.SaveResult)4 IndexedRecord (org.apache.avro.generic.IndexedRecord)4 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)4 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)4 ExecutionContext (org.teiid.translator.ExecutionContext)4 SalesforceConnection (org.teiid.translator.salesforce.SalesforceConnection)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 InvalidFieldFault (com.sforce.soap.partner.fault.InvalidFieldFault)3