Search in sources :

Example 1 with SObject

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

the class SalesforceConnectionTest method createObject.

private SObject createObject(String fieldName, String value) {
    SObject result = new SObject();
    result.setName(new QName(Constants.PARTNER_SOBJECT_NS, fieldName));
    result.setValue(value);
    return result;
}
Also used : QName(javax.xml.namespace.QName) SObject(com.sforce.soap.partner.sobject.SObject)

Example 2 with SObject

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

the class SalesforceConnection method getMessageElementForHierarchy.

/**
 * Drill down the SObject hierarchy based on the given field hierarchy until either null or the correct MessageElement
 * is found
 */
private XmlObject getMessageElementForHierarchy(SObject con, String[] fieldHierarchy) {
    final int lastIndex = fieldHierarchy.length - 1;
    SObject currentSObject = con;
    for (int index = 0; index <= lastIndex; index++) {
        for (XmlObject element : getChildren(currentSObject)) {
            if (element.getName().getLocalPart().equals(fieldHierarchy[index])) {
                if (index == lastIndex) {
                    return element;
                } else {
                    if (element instanceof SObject) {
                        // Found the next level, keep going
                        currentSObject = (SObject) element;
                    }
                    break;
                }
            }
        }
    }
    return null;
}
Also used : SObject(com.sforce.soap.partner.sobject.SObject) XmlObject(com.sforce.ws.bind.XmlObject)

Example 3 with SObject

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

the class SalesforceConnection method query.

public void query(boolean specifyQuery) throws KettleException {
    if (getBinding() == null) {
        throw new KettleException(BaseMessages.getString(PKG, "SalesforceInput.Exception.CanNotGetBiding"));
    }
    try {
        if (!specifyQuery) {
            // check if we can query this Object
            DescribeSObjectResult describeSObjectResult = getBinding().describeSObject(getModule());
            if (describeSObjectResult == null) {
                throw new KettleException(BaseMessages.getString(PKG, "SalesforceInput.ErrorGettingObject"));
            }
            if (!describeSObjectResult.isQueryable()) {
                throw new KettleException(BaseMessages.getString(PKG, "SalesforceInputDialog.ObjectNotQueryable", module));
            }
            if (this.recordsFilter == SalesforceConnectionUtils.RECORDS_FILTER_UPDATED || this.recordsFilter == SalesforceConnectionUtils.RECORDS_FILTER_DELETED) {
                // The object must be replicateable
                if (!describeSObjectResult.isReplicateable()) {
                    throw new KettleException(BaseMessages.getString(PKG, "SalesforceInput.Error.ObjectNotReplicateable", getModule()));
                }
            }
        }
        if (getSQL() != null && log.isDetailed()) {
            log.logDetailed(BaseMessages.getString(PKG, "SalesforceInput.Log.SQLString") + " : " + getSQL());
        }
        switch(this.recordsFilter) {
            case SalesforceConnectionUtils.RECORDS_FILTER_UPDATED:
                // Updated records ...
                GetUpdatedResult updatedRecords = getBinding().getUpdated(getModule(), this.startDate, this.endDate);
                if (updatedRecords.getIds() != null) {
                    int nr = updatedRecords.getIds().length;
                    if (nr > 0) {
                        String[] ids = updatedRecords.getIds();
                        // We can pass a maximum of 2000 object IDs
                        if (nr > SalesforceConnectionUtils.MAX_UPDATED_OBJECTS_IDS) {
                            this.sObjects = new SObject[nr];
                            List<String> list = new ArrayList<String>();
                            int desPos = 0;
                            for (int i = 0; i < nr; i++) {
                                list.add(updatedRecords.getIds()[i]);
                                if (i % SalesforceConnectionUtils.MAX_UPDATED_OBJECTS_IDS == 0 || i == nr - 1) {
                                    SObject[] s = getBinding().retrieve(this.fieldsList, getModule(), list.toArray(new String[list.size()]));
                                    System.arraycopy(s, 0, this.sObjects, desPos, s.length);
                                    desPos += s.length;
                                    s = null;
                                    list = new ArrayList<String>();
                                }
                            }
                        } else {
                            this.sObjects = getBinding().retrieve(this.fieldsList, getModule(), ids);
                        }
                        if (this.sObjects != null) {
                            this.queryResultSize = this.sObjects.length;
                        }
                    }
                }
                break;
            case SalesforceConnectionUtils.RECORDS_FILTER_DELETED:
                // Deleted records ...
                GetDeletedResult deletedRecordsResult = getBinding().getDeleted(getModule(), this.startDate, this.endDate);
                DeletedRecord[] deletedRecords = deletedRecordsResult.getDeletedRecords();
                if (log.isDebug()) {
                    log.logDebug(toString(), BaseMessages.getString(PKG, "SalesforceConnection.DeletedRecordsFound", String.valueOf(deletedRecords == null ? 0 : deletedRecords.length)));
                }
                if (deletedRecords != null && deletedRecords.length > 0) {
                    getDeletedList = new HashMap<String, Date>();
                    for (DeletedRecord dr : deletedRecords) {
                        getDeletedList.put(dr.getId(), dr.getDeletedDate().getTime());
                    }
                    this.qr = getBinding().queryAll(getSQL());
                    this.sObjects = getQueryResult().getRecords();
                    if (this.sObjects != null) {
                        this.queryResultSize = this.sObjects.length;
                    }
                }
                break;
            default:
                // return query result
                this.qr = isQueryAll() ? getBinding().queryAll(getSQL()) : getBinding().query(getSQL());
                this.sObjects = getQueryResult().getRecords();
                this.queryResultSize = getQueryResult().getSize();
                break;
        }
        if (this.sObjects != null) {
            this.recordsCount = this.sObjects.length;
        }
    } catch (Exception e) {
        log.logError(Const.getStackTracker(e));
        throw new KettleException(BaseMessages.getString(PKG, "SalesforceConnection.Exception.Query"), e);
    }
}
Also used : GetUpdatedResult(com.sforce.soap.partner.GetUpdatedResult) KettleException(org.pentaho.di.core.exception.KettleException) DeletedRecord(com.sforce.soap.partner.DeletedRecord) DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) ArrayList(java.util.ArrayList) Date(java.util.Date) SOAPException(javax.xml.soap.SOAPException) KettleException(org.pentaho.di.core.exception.KettleException) ConnectionException(com.sforce.ws.ConnectionException) IOException(java.io.IOException) SObject(com.sforce.soap.partner.sobject.SObject) GetDeletedResult(com.sforce.soap.partner.GetDeletedResult)

Example 4 with SObject

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

the class SalesforceConnection method buildJsonQueryResult.

@SuppressWarnings("unchecked")
private String buildJsonQueryResult(QueryResult queryResult) throws KettleException {
    JSONArray list = new JSONArray();
    for (SObject sobject : queryResult.getRecords()) {
        list.add(buildJSONSObject(sobject));
    }
    StringWriter sw = new StringWriter();
    try {
        list.writeJSONString(sw);
    } catch (IOException e) {
        throw new KettleException(e);
    }
    return sw.toString();
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) StringWriter(java.io.StringWriter) JSONArray(org.json.simple.JSONArray) SObject(com.sforce.soap.partner.sobject.SObject) IOException(java.io.IOException)

Example 5 with SObject

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

the class SalesforceInsert method flushBuffers.

private void flushBuffers() throws KettleException {
    try {
        // create the object(s) by sending the array to the web service
        data.saveResult = data.connection.insert(data.sfBuffer);
        for (int j = 0; j < data.saveResult.length; j++) {
            if (data.saveResult[j].isSuccess()) {
                // Row was inserted
                String id = data.saveResult[j].getId();
                if (log.isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "SalesforceInsert.RowInserted", 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 = getInputRowMeta().size();
                    newRow[newIndex++] = id;
                }
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "SalesforceInsert.NewRow", newRow[0]));
                }
                // copy row to output rowset(s);
                putRow(data.outputRowMeta, newRow);
                incrementLinesOutput();
                if (checkFeedback(getLinesInput())) {
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "SalesforceInsert.log.LineRow", getLinesInput()));
                    }
                }
            } else {
                if (!getStepMeta().isDoingErrorHandling()) {
                    if (log.isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "SalesforceInsert.ErrorFound"));
                    }
                    // Only show the first error
                    // 
                    com.sforce.soap.partner.Error err = data.saveResult[j].getErrors()[0];
                    throw new KettleException(BaseMessages.getString(PKG, "SalesforceInsert.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, "SalesforceInsert.Error.FlushBuffer", new Integer(j), err.getStatusCode(), err.getMessage());
                }
                // Simply add this row to the error row
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "SalesforceInsert.PassingRowToErrorStep"));
                }
                putError(getInputRowMeta(), data.outputBuffer[j], 1, errorMessage, null, "SalesforceInsert001");
            }
        }
        // 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(BaseMessages.getString(PKG, "SalesforceInsert.FailedToInsertObject", 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, "SalesforceInsert002");
        }
    } finally {
        if (data.saveResult != null) {
            data.saveResult = null;
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) 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