Search in sources :

Example 11 with SObject

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

the class SalesforceUpsert method flushBuffers.

private void flushBuffers() throws KettleException {
    try {
        if (data.sfBuffer.length > data.iBufferPos) {
            SObject[] smallBuffer = new SObject[data.iBufferPos];
            System.arraycopy(data.sfBuffer, 0, smallBuffer, 0, data.iBufferPos);
            data.sfBuffer = smallBuffer;
        }
        // upsert the object(s) by sending the array to the web service
        data.upsertResult = data.connection.upsert(meta.getUpsertField(), data.sfBuffer);
        int nr = data.upsertResult.length;
        for (int j = 0; j < nr; j++) {
            if (data.upsertResult[j].isSuccess()) {
                String id = data.upsertResult[j].getId();
                if (data.upsertResult[j].isCreated()) {
                    incrementLinesOutput();
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "SalesforceUpsert.ObjectCreated", id));
                    }
                } else {
                    incrementLinesUpdated();
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "SalesforceUpsert.ObjectUpdated", id));
                    }
                }
                // write out the row with the SalesForce ID
                Object[] newRow = RowDataUtil.resizeArray(data.outputBuffer[j], data.outputRowMeta.size());
                if (data.realSalesforceFieldName != null) {
                    int newIndex = data.inputRowMeta.size();
                    newRow[newIndex++] = id;
                }
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "SalesforceUpsert.NewRow", newRow[0]));
                }
                // copy row to output rowset(s);
                putRow(data.outputRowMeta, newRow);
                if (checkFeedback(getLinesInput())) {
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "SalesforceUpsert.log.LineRow", "" + getLinesInput()));
                    }
                }
            } else {
                if (!getStepMeta().isDoingErrorHandling()) {
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "SalesforceUpsert.ErrorFound"));
                    }
                    // Only throw the first error
                    // 
                    com.sforce.soap.partner.Error err = data.upsertResult[j].getErrors()[0];
                    throw new KettleException(BaseMessages.getString(PKG, "SalesforceUpsert.Error.FlushBuffer", new Integer(j), err.getStatusCode(), err.getMessage()));
                }
                String errorMessage = "";
                for (int i = 0; i < data.upsertResult[j].getErrors().length; i++) {
                    // get the next error
                    com.sforce.soap.partner.Error err = data.upsertResult[j].getErrors()[i];
                    errorMessage += BaseMessages.getString(PKG, "SalesforceUpsert.Error.FlushBuffer", new Integer(j), err.getStatusCode(), err.getMessage());
                }
                // Simply add this row to the error row
                if (log.isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "SalesforceUpsert.PassingRowToErrorStep"));
                }
                putError(getInputRowMeta(), data.outputBuffer[j], 1, errorMessage, null, "SalesforceUpsert001");
            }
        }
        // reset the buffers
        data.sfBuffer = new SObject[meta.getBatchSizeInt()];
        data.outputBuffer = new Object[meta.getBatchSizeInt()][];
        data.iBufferPos = 0;
    } catch (Exception e) {
        if (!getStepMeta().isDoingErrorHandling()) {
            if (e instanceof KettleException) {
                // I know, bad form usually. But I didn't want to duplicate the logic with a catch(KettleException). MB
                throw (KettleException) e;
            } else {
                throw new KettleException(BaseMessages.getString(PKG, "SalesforceUpsert.FailedUpsert", e.getMessage()), e);
            }
        }
        // Simply add this row to the error row
        if (log.isDebug()) {
            logDebug("Passing row to error step");
        }
        for (int i = 0; i < data.iBufferPos; i++) {
            putError(data.inputRowMeta, data.outputBuffer[i], 1, e.getMessage(), null, "SalesforceUpsert002");
        }
    } finally {
        if (data.upsertResult != null) {
            data.upsertResult = null;
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) SObject(com.sforce.soap.partner.sobject.SObject) 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)

Example 12 with SObject

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

the class SalesforceUpsert method setFieldInSObject.

void setFieldInSObject(SObject sobjPass, XmlObject element) {
    Iterator<XmlObject> children = element.getChildren();
    String name = element.getName().getLocalPart();
    if (!children.hasNext()) {
        sobjPass.setSObjectField(name, element.getValue());
    } else {
        SObject child = new SObject();
        child.setName(new QName(name));
        while (children.hasNext()) {
            setFieldInSObject(child, children.next());
        }
        sobjPass.setSObjectField(name, child);
    }
}
Also used : QName(javax.xml.namespace.QName) SObject(com.sforce.soap.partner.sobject.SObject) XmlObject(com.sforce.ws.bind.XmlObject)

Example 13 with SObject

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

the class SalesforceUpsertTest method testSetFieldInSObjectForeignKey.

@Test
public void testSetFieldInSObjectForeignKey() throws Exception {
    SalesforceUpsert salesforceUpsert = new SalesforceUpsert(smh.stepMeta, smh.stepDataInterface, 0, smh.transMeta, smh.trans);
    SObject sobjPass = new SObject();
    XmlObject parentObject = new XmlObject();
    String parentParam = "parentParam";
    String parentValue = "parentValue";
    parentObject.setName(new QName(parentParam));
    parentObject.setValue(parentValue);
    String child = "child";
    String childParam = "childParam";
    String childValue = "childValue";
    XmlObject childObject = new XmlObject();
    childObject.setName(new QName(child));
    childObject.setField(childParam, childValue);
    salesforceUpsert.setFieldInSObject(sobjPass, parentObject);
    salesforceUpsert.setFieldInSObject(sobjPass, childObject);
    Assert.assertEquals(parentValue, sobjPass.getField(parentParam));
    Assert.assertEquals(childValue, ((SObject) sobjPass.getField(child)).getField(childParam));
}
Also used : QName(javax.xml.namespace.QName) SObject(com.sforce.soap.partner.sobject.SObject) XmlObject(com.sforce.ws.bind.XmlObject) Matchers.anyString(org.mockito.Matchers.anyString) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Test(org.junit.Test)

Example 14 with SObject

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

the class SalesforceConnectionImpl method create.

public int create(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 };
    SaveResult[] result;
    try {
        result = partnerConnection.create(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);
    }
    return analyzeResult(result);
}
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 15 with SObject

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

the class SalesforceConnectionImpl method update.

public int update(List<DataPayload> updateDataList) throws ResourceException {
    List<SObject> params = new ArrayList<SObject>(updateDataList.size());
    for (int i = 0; i < updateDataList.size(); i++) {
        DataPayload data = updateDataList.get(i);
        SObject toCreate = new SObject();
        toCreate.setType(data.getType());
        toCreate.setId(data.getID());
        for (DataPayload.Field field : data.getMessageElements()) {
            toCreate.addField(field.name, field.value);
        }
        params.add(i, toCreate);
    }
    SaveResult[] result;
    try {
        result = partnerConnection.update(params.toArray(new SObject[params.size()]));
    } 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);
    }
    return analyzeResult(result);
}
Also used : InvalidSObjectFault(com.sforce.soap.partner.fault.InvalidSObjectFault) ArrayList(java.util.ArrayList) UnexpectedErrorFault(com.sforce.soap.partner.fault.UnexpectedErrorFault) InvalidFieldFault(com.sforce.soap.partner.fault.InvalidFieldFault) SObject(com.sforce.soap.partner.sobject.SObject) DataPayload(org.teiid.translator.salesforce.execution.DataPayload) ResourceException(javax.resource.ResourceException) InvalidIdFault(com.sforce.soap.partner.fault.InvalidIdFault) 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