Search in sources :

Example 36 with SObject

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

the class SalesforceConnectionImpl method upsert.

public int upsert(DataPayload data) throws ResourceException {
    SObject toCreate = new SObject();
    toCreate.setType(data.getType());
    for (DataPayload.Field field : data.getMessageElements()) {
        toCreate.addField(field.name, field.value);
    }
    SObject[] objects = new SObject[] { toCreate };
    UpsertResult[] results;
    try {
        results = partnerConnection.upsert(ID_FIELD_NAME, objects);
    } catch (InvalidFieldFault e) {
        throw new ResourceException(e);
    } catch (InvalidSObjectFault e) {
        throw new ResourceException(e);
    } catch (InvalidIdFault e) {
        throw new ResourceException(e);
    } catch (UnexpectedErrorFault e) {
        throw new ResourceException(e);
    } catch (ConnectionException e) {
        throw new ResourceException(e);
    }
    for (UpsertResult result : results) {
        if (!result.isSuccess()) {
            throw new ResourceException(result.getErrors()[0].getMessage());
        }
    }
    return results.length;
}
Also used : InvalidFieldFault(com.sforce.soap.partner.fault.InvalidFieldFault) InvalidSObjectFault(com.sforce.soap.partner.fault.InvalidSObjectFault) SObject(com.sforce.soap.partner.sobject.SObject) DataPayload(org.teiid.translator.salesforce.execution.DataPayload) ResourceException(javax.resource.ResourceException) UnexpectedErrorFault(com.sforce.soap.partner.fault.UnexpectedErrorFault) InvalidIdFault(com.sforce.soap.partner.fault.InvalidIdFault) ConnectionException(com.sforce.ws.ConnectionException)

Example 37 with SObject

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

the class QueryExecutionImpl method loadBatch.

private void loadBatch() throws TranslatorException {
    try {
        if (null != resultBatch) {
            // if we have an old batch, then we have to get new results
            results = connection.queryMore(results.getQueryLocator(), context.getBatchSize());
        }
        resultBatch = new ArrayList<List<Object>>();
        topResultIndex = 0;
        for (SObject sObject : results.getRecords()) {
            if (sObject == null) {
                continue;
            }
            List<Object[]> result = getObjectData(sObject);
            for (Iterator<Object[]> i = result.iterator(); i.hasNext(); ) {
                resultBatch.add(Arrays.asList(i.next()));
            }
        }
    } catch (ResourceException e) {
        throw new TranslatorException(e);
    }
}
Also used : SObject(com.sforce.soap.partner.sobject.SObject) ResourceException(javax.resource.ResourceException) TranslatorException(org.teiid.translator.TranslatorException)

Example 38 with SObject

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

the class TestUpdates method testIds.

@Test
public void testIds() throws Exception {
    Delete delete = (Delete) translationUtility.parseCommand("delete from contacts");
    SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
    SalesForceExecutionFactory config = new SalesForceExecutionFactory();
    DeleteExecutionImpl updateExecution = new DeleteExecutionImpl(config, delete, connection, Mockito.mock(RuntimeMetadata.class), Mockito.mock(ExecutionContext.class));
    ArgumentCaptor<String> queryArgument = ArgumentCaptor.forClass(String.class);
    QueryResult qr = new QueryResult();
    SObject so = new SObject();
    so.setType("Contact");
    so.addField("Id", "x");
    qr.setRecords(new SObject[] { so });
    qr.setSize(1);
    qr.setDone(true);
    Mockito.stub(connection.query(queryArgument.capture(), Mockito.anyInt(), Mockito.anyBoolean())).toReturn(qr);
    Mockito.stub(connection.delete(new String[] { "x" })).toReturn(1);
    while (true) {
        try {
            updateExecution.execute();
            org.junit.Assert.assertArrayEquals(new int[] { 1 }, updateExecution.getUpdateCounts());
            break;
        } catch (DataNotAvailableException e) {
            continue;
        }
    }
    Mockito.verify(connection, Mockito.times(1)).query(queryArgument.capture(), Mockito.anyInt(), Mockito.anyBoolean());
    String query = queryArgument.getValue();
    assertEquals("SELECT Id FROM Contact ", query);
}
Also used : Delete(org.teiid.language.Delete) QueryResult(com.sforce.soap.partner.QueryResult) ExecutionContext(org.teiid.translator.ExecutionContext) SalesForceExecutionFactory(org.teiid.translator.salesforce.SalesForceExecutionFactory) SObject(com.sforce.soap.partner.sobject.SObject) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Test(org.junit.Test)

Example 39 with SObject

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

the class QueryExecutionImpl method extractDataFromFields.

private List<Object[]> extractDataFromFields(SObject sObject, List<XmlObject> fields, List<Object[]> result) throws TranslatorException {
    Map<String, Integer> fieldToIndexMap = sObjectToResponseField.get(sObject.getType());
    int aggCount = 0;
    for (int j = 0; j < visitor.getSelectSymbolCount(); j++) {
        Expression ex = visitor.getSelectSymbolMetadata(j);
        if (ex instanceof ColumnReference) {
            Column element = ((ColumnReference) ex).getMetadataObject();
            Table table = (Table) element.getParent();
            if (table.getSourceName().equals(sObject.getType()) || AGGREGATE_RESULT.equalsIgnoreCase(sObject.getType())) {
                Integer index = fieldToIndexMap.get(element.getSourceName());
                if (null == index) {
                    // $NON-NLS-1$
                    throw new TranslatorException(SalesForcePlugin.Util.getString("SalesforceQueryExecutionImpl.missing.field") + element.getSourceName());
                }
                Object cell = getCellDatum(element.getSourceName(), element.getJavaType(), fields.get(index));
                setValueInColumn(j, cell, result);
            }
        } else if (ex instanceof AggregateFunction) {
            String name = SelectVisitor.AGG_PREFIX + (aggCount++);
            Integer index = fieldToIndexMap.get(name);
            if (null == index) {
                // $NON-NLS-1$
                throw new TranslatorException(SalesForcePlugin.Util.getString("SalesforceQueryExecutionImpl.missing.field") + ex);
            }
            Object cell = getCellDatum(name, ex.getType(), fields.get(index));
            setValueInColumn(j, cell, result);
        }
    }
    return result;
}
Also used : Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) TranslatorException(org.teiid.translator.TranslatorException) XmlObject(com.sforce.ws.bind.XmlObject) SObject(com.sforce.soap.partner.sobject.SObject)

Example 40 with SObject

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

the class SObjectAdapterFactoryTest method testConvertToAvroBasic.

@Test
public void testConvertToAvroBasic() throws IOException {
    converter.setSchema(SCHEMA);
    assertNotNull(converter.getSchema());
    assertEquals(SObject.class, converter.getDatumClass());
    SObject sObject = new SObject("Account");
    sObject.addField("Id", "12345");
    sObject.addField("Name", "Qwerty");
    sObject.addField("FieldX", 42);
    sObject.addField("FieldY", true);
    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));
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) SObject(com.sforce.soap.partner.sobject.SObject) Test(org.junit.Test)

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