Search in sources :

Example 6 with SObject

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

the class SalesforceInsert method processRow.

@Override
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    // get one row ... This does some basic initialization of the objects, including loading the info coming in
    Object[] outputRowData = getRow();
    if (outputRowData == null) {
        if (data.iBufferPos > 0) {
            flushBuffers();
        }
        setOutputDone();
        return false;
    }
    // If we haven't looked at a row before then do some basic setup.
    if (first) {
        first = false;
        data.sfBuffer = new SObject[meta.getBatchSizeInt()];
        data.outputBuffer = new Object[meta.getBatchSizeInt()][];
        // get total fields in the grid
        data.nrfields = meta.getUpdateLookup().length;
        // Check if field list is filled
        if (data.nrfields == 0) {
            throw new KettleException(BaseMessages.getString(PKG, "SalesforceInsertDialog.FieldsMissing.DialogMessage"));
        }
        // Create the output row meta-data
        data.inputRowMeta = getInputRowMeta().clone();
        data.outputRowMeta = data.inputRowMeta.clone();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
        // Build the mapping of input position to field name
        data.fieldnrs = new int[meta.getUpdateStream().length];
        for (int i = 0; i < meta.getUpdateStream().length; i++) {
            data.fieldnrs[i] = getInputRowMeta().indexOfValue(meta.getUpdateStream()[i]);
            if (data.fieldnrs[i] < 0) {
                throw new KettleException(BaseMessages.getString(PKG, "SalesforceInsert.CanNotFindField", meta.getUpdateStream()[i]));
            }
        }
    }
    try {
        writeToSalesForce(outputRowData);
    } catch (Exception e) {
        throw new KettleStepException(BaseMessages.getString(PKG, "SalesforceInsert.log.Exception", e));
    }
    return true;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) 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 7 with SObject

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

the class SalesforceInsert method writeToSalesForce.

@VisibleForTesting
void writeToSalesForce(Object[] rowData) throws KettleException {
    try {
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "SalesforceInsert.WriteToSalesforce", data.iBufferPos, meta.getBatchSizeInt()));
        }
        // if there is room in the buffer
        if (data.iBufferPos < meta.getBatchSizeInt()) {
            ArrayList<XmlObject> insertfields = new ArrayList<>();
            // Reserve for empty fields
            ArrayList<String> fieldsToNull = new ArrayList<String>();
            // Add fields to insert
            for (int i = 0; i < data.nrfields; i++) {
                ValueMetaInterface valueMeta = data.inputRowMeta.getValueMeta(data.fieldnrs[i]);
                Object value = rowData[data.fieldnrs[i]];
                if (valueMeta.isNull(value)) {
                    // 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, value);
                    insertfields.add(SalesforceConnection.createMessageElement(meta.getUpdateLookup()[i], normalObject, meta.getUseExternalId()[i]));
                }
            }
            // build the SObject
            SObject sobjPass = new SObject();
            sobjPass.setType(data.connection.getModule());
            if (insertfields.size() > 0) {
                for (XmlObject element : insertfields) {
                    sobjPass.setSObjectField(element.getName().getLocalPart(), element.getValue());
                }
            }
            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(BaseMessages.getString(PKG, "SalesforceInsert.CallingFlushBuffer"));
            }
            flushBuffers();
        }
    } catch (Exception e) {
        throw new KettleException(BaseMessages.getString(PKG, "SalesforceInsert.Error", e.getMessage()));
    }
}
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 8 with SObject

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

the class SalesforceUpdate 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;
        }
        // update the object(s) by sending the array to the web service
        data.saveResult = data.connection.update(data.sfBuffer);
        int nr = data.saveResult.length;
        for (int j = 0; j < nr; j++) {
            if (data.saveResult[j].isSuccess()) {
                // Row was updated
                String id = data.saveResult[j].getId();
                if (log.isDetailed()) {
                    logDetailed("Row updated with id: " + id);
                }
                // write out the row with the SalesForce ID
                Object[] newRow = RowDataUtil.resizeArray(data.outputBuffer[j], data.outputRowMeta.size());
                if (log.isDetailed()) {
                    logDetailed("The new row has an id value of : " + newRow[0]);
                }
                // copy row to output rowset(s);
                putRow(data.outputRowMeta, newRow);
                incrementLinesUpdated();
                if (checkFeedback(getLinesInput())) {
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "SalesforceUpdate.log.LineRow", "" + getLinesInput()));
                    }
                }
            } else {
                if (!getStepMeta().isDoingErrorHandling()) {
                    if (log.isDetailed()) {
                        logDetailed("Found error from SalesForce and raising the exception");
                    }
                    // Only send the first error
                    // 
                    com.sforce.soap.partner.Error err = data.saveResult[j].getErrors()[0];
                    throw new KettleException(BaseMessages.getString(PKG, "SalesforceUpdate.Error.FlushBuffer", new Integer(j), err.getStatusCode(), err.getMessage()));
                }
                String errorMessage = "";
                for (int i = 0; i < data.saveResult[j].getErrors().length; i++) {
                    // get the next error
                    com.sforce.soap.partner.Error err = data.saveResult[j].getErrors()[i];
                    errorMessage += BaseMessages.getString(PKG, "SalesforceUpdate.Error.FlushBuffer", new Integer(j), err.getStatusCode(), err.getMessage());
                }
                // Simply add this row to the error row
                if (log.isDebug()) {
                    logDebug("Passing row to error step");
                }
                putError(getInputRowMeta(), data.outputBuffer[j], 1, errorMessage, null, "SalesforceUpdate001");
            }
        }
        // reset the buffers
        data.sfBuffer = new SObject[meta.getBatchSizeInt()];
        data.outputBuffer = new Object[meta.getBatchSizeInt()][];
        data.iBufferPos = 0;
    } catch (Exception e) {
        if (!getStepMeta().isDoingErrorHandling()) {
            throw new KettleException("\nFailed to update object, error message was: \n" + e.getMessage());
        }
        // 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, "SalesforceUpdate002");
        }
    } finally {
        if (data.saveResult != null) {
            data.saveResult = 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 9 with SObject

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

the class SalesforceUpdate method writeToSalesForce.

@VisibleForTesting
void writeToSalesForce(Object[] rowData) throws KettleException {
    try {
        if (log.isDetailed()) {
            logDetailed("Called writeToSalesForce with " + data.iBufferPos + " out of " + 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> updatefields = new ArrayList<>();
            // Add fields to update
            for (int i = 0; i < data.nrfields; i++) {
                boolean valueIsNull = data.inputRowMeta.isNull(rowData, data.fieldnrs[i]);
                if (valueIsNull) {
                    // The value is null
                    // We need to keep track of this field
                    fieldsToNull.add(SalesforceUtils.getFieldToNullName(log, meta.getUpdateLookup()[i], meta.getUseExternalId()[i]));
                } else {
                    ValueMetaInterface valueMeta = data.inputRowMeta.getValueMeta(data.fieldnrs[i]);
                    Object value = rowData[data.fieldnrs[i]];
                    Object normalObject = normalizeValue(valueMeta, value);
                    updatefields.add(SalesforceConnection.createMessageElement(meta.getUpdateLookup()[i], normalObject, meta.getUseExternalId()[i]));
                }
            }
            // build the SObject
            SObject sobjPass = new SObject();
            sobjPass.setType(data.connection.getModule());
            if (updatefields.size() > 0) {
                for (XmlObject element : updatefields) {
                    sobjPass.setSObjectField(element.getName().getLocalPart(), element.getValue());
                }
            }
            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 (Exception e) {
        throw new KettleException("\nFailed in writeToSalesForce: " + e.getMessage());
    }
}
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 10 with SObject

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

the class SalesforceUpsert method processRow.

@Override
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    // get one row ... This does some basic initialization of the objects, including loading the info coming in
    Object[] outputRowData = getRow();
    if (outputRowData == null) {
        if (data.iBufferPos > 0) {
            flushBuffers();
        }
        setOutputDone();
        return false;
    }
    // If we haven't looked at a row before then do some basic setup.
    if (first) {
        first = false;
        data.sfBuffer = new SObject[meta.getBatchSizeInt()];
        data.outputBuffer = new Object[meta.getBatchSizeInt()][];
        // get total fields in the grid
        data.nrfields = meta.getUpdateLookup().length;
        // Check if field list is filled
        if (data.nrfields == 0) {
            throw new KettleException(BaseMessages.getString(PKG, "SalesforceUpsertDialog.FieldsMissing.DialogMessage"));
        }
        // Create the output row meta-data
        data.inputRowMeta = getInputRowMeta().clone();
        data.outputRowMeta = data.inputRowMeta.clone();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
        // Build the mapping of input position to field name
        data.fieldnrs = new int[meta.getUpdateStream().length];
        for (int i = 0; i < meta.getUpdateStream().length; i++) {
            data.fieldnrs[i] = getInputRowMeta().indexOfValue(meta.getUpdateStream()[i]);
            if (data.fieldnrs[i] < 0) {
                throw new KettleException(BaseMessages.getString(PKG, "SalesforceUpsert.FieldNotFound", meta.getUpdateStream()[i]));
            }
        }
    }
    try {
        writeToSalesForce(outputRowData);
    } catch (Exception e) {
        throw new KettleStepException(BaseMessages.getString(PKG, "SalesforceUpsert.log.Exception"), e);
    }
    return true;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) 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)

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