Search in sources :

Example 41 with SObject

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

the class SObjectAdapterFactoryTest method testConvertToAvroForAggregateResult.

@Test
public void testConvertToAvroForAggregateResult() throws Exception {
    Schema schema = // 
    SchemaBuilder.builder().record("Schema").fields().name("Id").prop(SchemaConstants.TALEND_COLUMN_IS_KEY, "true").type().stringType().noDefault().name("Name").type(AvroUtils._string()).noDefault().name("Field_A").type(AvroUtils._int()).noDefault().name("Field_B").type(AvroUtils._boolean()).noDefault().name("Field_C").type(AvroUtils._date()).noDefault().name("Field_D").type(AvroUtils._double()).noDefault().name("Field_E_Value").type(AvroUtils._string()).noDefault().name("Field_F").type(AvroUtils._string()).noDefault().name("Field_G").type(AvroUtils._string()).noDefault().name("Field_H").type(AvroUtils._string()).noDefault().name("Field_I").type(AvroUtils._string()).noDefault().name("Field_J").type(AvroUtils._string()).noDefault().name("Field_K").type(AvroUtils._string()).noDefault().name("Field_L").type(AvroUtils._string()).noDefault().endRecord();
    schema.getField("Field_C").addProp(SchemaConstants.TALEND_COLUMN_PATTERN, "yyyy-MM-dd'T'HH:mm:ss'.000Z'");
    schema.addProp(SalesforceSchemaConstants.COLUMNNAME_DELIMTER, "_");
    schema.addProp(SalesforceSchemaConstants.VALUE_DELIMITER, "|");
    converter.setSchema(schema);
    SObject sObject = new SObject("AggregateResult");
    sObject.addField("Id", "12345");
    sObject.addField("Name", "Qwerty");
    sObject.addField("Field_A", "42");
    sObject.addField("Field_B", "true");
    sObject.addField("Field_C", dateFormat.parse("2017-06-15T18:26:34.000Z"));
    sObject.addField("Field_D", 10200.45);
    SObject sObject1 = new SObject();
    sObject1.addField("Value", 245);
    sObject.addField("Field_E", sObject1);
    SObject sObject2 = new SObject();
    sObject2.addField("Value", 542);
    sObject.addField("Field_E", sObject2);
    sObject.addField("Field_F", new BigDecimal("20000000000000000000000000.123456789"));
    sObject.addField("Field_G", 899.5f);
    sObject.addField("Field_H", Boolean.TRUE);
    sObject.addField("Field_I", new byte[] { 0x0a, 0x0b, 0x0c, 0x0d });
    sObject.addField("Field_J", 102030405060708090L);
    sObject.addField("Field_K", SalesforceRuntime.convertDateToCalendar(dateFormat.parse("2017-06-16T10:45:02.000Z"), false));
    sObject.addField("Field_L", null);
    IndexedRecord indexedRecord = converter.convertToAvro(sObject);
    assertNotNull(indexedRecord);
    assertNotNull(indexedRecord.getSchema());
    assertEquals(schema, indexedRecord.getSchema());
    assertEquals("12345", indexedRecord.get(0));
    assertEquals("Qwerty", indexedRecord.get(1));
    assertEquals(Integer.valueOf(42), indexedRecord.get(2));
    assertEquals(Boolean.TRUE, indexedRecord.get(3));
    assertEquals(dateFormat.parse("2017-06-15T18:26:34.000Z").getTime(), indexedRecord.get(4));
    assertEquals(Double.valueOf(10200.45), indexedRecord.get(5));
    assertEquals("245|542", indexedRecord.get(6));
    assertEquals("20000000000000000000000000.123456789", indexedRecord.get(7));
    assertEquals("899.5", indexedRecord.get(8));
    assertEquals("true", indexedRecord.get(9));
    assertEquals("CgsMDQ==", indexedRecord.get(10));
    assertEquals("102030405060708090", indexedRecord.get(11));
    assertEquals("2017-06-16T07:45:02.000Z", indexedRecord.get(12));
    assertNull(indexedRecord.get(13));
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) SObject(com.sforce.soap.partner.sobject.SObject) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 42 with SObject

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

the class SalesforceWriter method doInsert.

private SaveResult[] doInsert() throws IOException {
    if (insertItems.size() > 0) {
        // Clean the feedback records at each batch write.
        cleanWrites();
        SObject[] accs = new SObject[insertItems.size()];
        for (int i = 0; i < insertItems.size(); i++) {
            accs[i] = createSObject(insertItems.get(i));
        }
        String[] changedItemKeys = new String[accs.length];
        SaveResult[] saveResults;
        try {
            saveResults = connection.create(accs);
            if (saveResults != null && saveResults.length != 0) {
                int batch_idx = -1;
                for (int i = 0; i < saveResults.length; i++) {
                    ++batch_idx;
                    if (saveResults[i].getSuccess()) {
                        handleSuccess(insertItems.get(i), saveResults[i].getId(), null);
                    } else {
                        handleReject(insertItems.get(i), saveResults[i].getErrors(), changedItemKeys, batch_idx);
                    }
                }
            }
            insertItems.clear();
            return saveResults;
        } catch (ConnectionException e) {
            throw new IOException(e);
        }
    }
    return null;
}
Also used : SObject(com.sforce.soap.partner.sobject.SObject) SaveResult(com.sforce.soap.partner.SaveResult) IOException(java.io.IOException) ConnectionException(com.sforce.ws.ConnectionException)

Example 43 with SObject

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

the class SalesforceWriter method doUpsert.

private UpsertResult[] doUpsert() throws IOException {
    if (upsertItems.size() > 0) {
        // Clean the feedback records at each batch write.
        cleanWrites();
        SObject[] upds = new SObject[upsertItems.size()];
        for (int i = 0; i < upsertItems.size(); i++) {
            upds[i] = createSObjectForUpsert(upsertItems.get(i));
        }
        String[] changedItemKeys = new String[upds.length];
        for (int ix = 0; ix < upds.length; ++ix) {
            Object value = upds[ix].getField(upsertKeyColumn);
            if (value != null) {
                changedItemKeys[ix] = String.valueOf(value);
            }
        }
        UpsertResult[] upsertResults;
        try {
            upsertResults = connection.upsert(upsertKeyColumn, upds);
            upds = null;
            if (upsertResults != null && upsertResults.length != 0) {
                int batch_idx = -1;
                for (int i = 0; i < upsertResults.length; i++) {
                    ++batch_idx;
                    if (upsertResults[i].getSuccess()) {
                        if (upsertResults[i].getCreated()) {
                            handleSuccess(upsertItems.get(i), upsertResults[i].getId(), "created");
                        } else {
                            handleSuccess(upsertItems.get(i), upsertResults[i].getId(), "updated");
                        }
                    } else {
                        handleReject(upsertItems.get(0), upsertResults[i].getErrors(), changedItemKeys, batch_idx);
                    }
                }
            }
            upsertItems.clear();
            return upsertResults;
        } catch (ConnectionException e) {
            throw new IOException(e);
        }
    }
    return null;
}
Also used : SObject(com.sforce.soap.partner.sobject.SObject) XmlObject(com.sforce.ws.bind.XmlObject) SObject(com.sforce.soap.partner.sobject.SObject) IOException(java.io.IOException) ConnectionException(com.sforce.ws.ConnectionException) UpsertResult(com.sforce.soap.partner.UpsertResult)

Example 44 with SObject

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

the class SalesforceWriter method createSObjectForUpsert.

private SObject createSObjectForUpsert(IndexedRecord input) {
    SObject so = new SObject();
    so.setType(sprops.module.moduleName.getStringValue());
    Map<String, Map<String, String>> referenceFieldsMap = getReferenceFieldsMap();
    nullValueFields.clear();
    for (Schema.Field f : input.getSchema().getFields()) {
        Object value = input.get(f.pos());
        Schema.Field se = mainSchema.getField(f.name());
        if (se == null) {
            continue;
        }
        if (value != null && !"".equals(value.toString())) {
            if (referenceFieldsMap != null && referenceFieldsMap.get(se.name()) != null) {
                Map<String, String> relationMap = referenceFieldsMap.get(se.name());
                String lookupRelationshipFieldName = relationMap.get("lookupRelationshipFieldName");
                so.setField(lookupRelationshipFieldName, null);
                so.getChild(lookupRelationshipFieldName).setField("type", relationMap.get("lookupFieldModuleName"));
                // No need get the real type. Because of the External IDs should not be special type in addSObjectField()
                addSObjectField(so.getChild(lookupRelationshipFieldName), se.schema(), relationMap.get("lookupFieldExternalIdName"), value);
            } else {
                // Skip column "Id" for upsert, when "Id" is not specified as "upsertKeyColumn"
                if (!"Id".equals(se.name()) || se.name().equals(sprops.upsertKeyColumn.getValue())) {
                    Schema.Field fieldInModule = moduleSchema.getField(se.name());
                    if (fieldInModule != null) {
                        // The real type is need in addSObjectField()
                        addSObjectField(so, fieldInModule.schema(), se.name(), value);
                    } else {
                        // This is keep old behavior, when set a field which is not exist.
                        // It would throw a exception for this.
                        addSObjectField(so, se.schema(), se.name(), value);
                    }
                }
            }
        } else {
            if (referenceFieldsMap != null && referenceFieldsMap.get(se.name()) != null) {
                Map<String, String> relationMap = referenceFieldsMap.get(se.name());
                String lookupFieldName = relationMap.get("lookupFieldName");
                if (lookupFieldName != null && !lookupFieldName.trim().isEmpty()) {
                    nullValueFields.add(lookupFieldName);
                }
            } else if (!("Id".equals(se.name()) || se.name().equals(sprops.upsertKeyColumn.getValue()))) {
                nullValueFields.add(se.name());
            }
        }
    }
    if (!sprops.ignoreNull.getValue()) {
        so.setFieldsToNull(nullValueFields.toArray(new String[0]));
    }
    return so;
}
Also used : Schema(org.apache.avro.Schema) SObject(com.sforce.soap.partner.sobject.SObject) XmlObject(com.sforce.ws.bind.XmlObject) SObject(com.sforce.soap.partner.sobject.SObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 45 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)

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