Search in sources :

Example 1 with DeleteResponse

use of com.salesforce.soap.partner.DeleteResponse in project tdi-studio-se by Talend.

the class SforceManagementImpl method logout.

/**
     * logout
     */
@Override
public void logout() throws Exception {
    Object returnObject = null;
    // if there are still records to be commited:
    try {
        if (insertItems.size() > 0) {
            SObject[] accs = insertItems.toArray(new SObject[insertItems.size()]);
            String[] changedItemKeys = new String[accs.length];
            Create create = new Create();
            create.setSObjects(accs);
            SaveResult[] sr = sforceConn.create(create).getResult();
            insertItems.clear();
            accs = null;
            if (sr != null && sr.length != 0) {
                int batch_idx = -1;
                for (SaveResult result : sr) {
                    ++batch_idx;
                    StringBuilder errors = new StringBuilder("");
                    if (result.getSuccess()) {
                    // TODO: send back the ID
                    } else {
                        errors = addLog(result.getErrors(), batch_idx < changedItemKeys.length ? "" + (batch_idx + 1) : "Batch index out of bounds");
                    }
                    if (exceptionForErrors && errors.toString().length() > 0) {
                        if (logWriter != null) {
                            logWriter.close();
                        }
                        throw new Exception(errors.toString());
                    }
                }
            }
            returnObject = sr;
        }
        if (deleteItems.size() > 0) {
            ID[] delIDs = deleteItems.toArray(new ID[deleteItems.size()]);
            String[] changedItemKeys = new String[delIDs.length];
            for (int ix = 0; ix < delIDs.length; ++ix) {
                changedItemKeys[ix] = delIDs[ix].getID();
            }
            Delete dels = new Delete();
            dels.setIds(delIDs);
            DeleteResponse dresp = sforceConn.delete(dels);
            DeleteResult[] dr = dresp.getResult();
            deleteItems.clear();
            delIDs = null;
            if (dr != null && dr.length != 0) {
                int batch_idx = -1;
                for (DeleteResult result : dr) {
                    ++batch_idx;
                    StringBuilder errors = new StringBuilder("");
                    if (result.getSuccess()) {
                    // TODO: send back the ID
                    } else {
                        errors = addLog(result.getErrors(), batch_idx < changedItemKeys.length ? changedItemKeys[batch_idx] : "Batch index out of bounds");
                    }
                    if (exceptionForErrors && errors.toString().length() > 0) {
                        if (logWriter != null) {
                            logWriter.close();
                        }
                        throw new Exception(errors.toString());
                    }
                }
            }
            returnObject = dr;
        }
        if (updateItems.size() > 0) {
            SObject[] upds = updateItems.toArray(new SObject[updateItems.size()]);
            String[] changedItemKeys = new String[upds.length];
            for (int ix = 0; ix < upds.length; ++ix) {
                changedItemKeys[ix] = upds[ix].getId().getID();
            }
            Update update = new Update();
            update.setSObjects(upds);
            SaveResult[] saveResults = sforceConn.update(update).getResult();
            updateItems.clear();
            upds = null;
            if (saveResults != null && saveResults.length != 0) {
                int batch_idx = -1;
                for (SaveResult result : saveResults) {
                    ++batch_idx;
                    StringBuilder errors = new StringBuilder("");
                    if (result.getSuccess()) {
                    // TODO: send back the ID
                    } else {
                        errors = addLog(result.getErrors(), batch_idx < changedItemKeys.length ? changedItemKeys[batch_idx] : "Batch index out of bounds");
                    }
                    if (exceptionForErrors && errors.toString().length() > 0) {
                        if (logWriter != null) {
                            logWriter.close();
                        }
                        throw new Exception(errors.toString());
                    }
                }
            }
            returnObject = saveResults;
        }
        if (upsertItems.size() > 0) {
            SObject[] upds = upsertItems.toArray(new SObject[upsertItems.size()]);
            String[] changedItemKeys = new String[upds.length];
            for (int ix = 0; ix < upds.length; ++ix) {
                changedItemKeys[ix] = "No value for " + upsertKeyColumn + " ";
                OMElement[] oms = upds[ix].getExtraElement();
                for (OMElement om : oms) {
                    if (upsertKeyColumn != null && om != null && upsertKeyColumn.equals(om.getLocalName())) {
                        changedItemKeys[ix] = om.getText();
                        break;
                    }
                }
            }
            Upsert upsert = new Upsert();
            upsert.setSObjects(upds);
            upsert.setExternalIDFieldName(upsertKeyColumn);
            UpsertResult[] upsertResults = sforceConn.upsert(upsert).getResult();
            upsertItems.clear();
            upds = null;
            if (upsertResults != null && upsertResults.length != 0) {
                int batch_idx = -1;
                for (UpsertResult result : upsertResults) {
                    ++batch_idx;
                    StringBuilder errors = new StringBuilder("");
                    if (result.getSuccess()) {
                    // TODO: send back the ID
                    } else {
                        errors = addLog(result.getErrors(), batch_idx < changedItemKeys.length ? changedItemKeys[batch_idx] : "Batch index out of bounds");
                    }
                    if (exceptionForErrors && errors.toString().length() > 0) {
                        if (logWriter != null) {
                            logWriter.close();
                        }
                        throw new Exception(errors.toString());
                    }
                }
            }
            returnObject = upsertResults;
        }
    } catch (Exception e) {
        throw new Exception(e.toString());
    }
    if (logWriter != null) {
        logWriter.close();
    }
}
Also used : Delete(com.salesforce.soap.partner.Delete) Upsert(com.salesforce.soap.partner.Upsert) SaveResult(com.salesforce.soap.partner.SaveResult) OMElement(org.apache.axiom.om.OMElement) Update(com.salesforce.soap.partner.Update) DeleteResponse(com.salesforce.soap.partner.DeleteResponse) Create(com.salesforce.soap.partner.Create) DescribeSObject(com.salesforce.soap.partner.DescribeSObject) SObject(com.salesforce.soap.partner.sobject.SObject) DescribeSObject(com.salesforce.soap.partner.DescribeSObject) SObject(com.salesforce.soap.partner.sobject.SObject) ID(com.salesforce.soap.partner.ID) DeleteResult(com.salesforce.soap.partner.DeleteResult) UpsertResult(com.salesforce.soap.partner.UpsertResult)

Example 2 with DeleteResponse

use of com.salesforce.soap.partner.DeleteResponse in project tdi-studio-se by Talend.

the class SforceManagementImpl method delete.

/**
     * delete, one time one record.
     */
@Override
public DeleteResult[] delete(String id) throws Exception {
    if (id == null) {
        return null;
    }
    // String[] ids = new String[] { id };
    ID dID = new ID();
    dID.setID(id);
    deleteItems.add(dID);
    if (deleteItems.size() >= commitLevel) {
        ID[] delIDs = deleteItems.toArray(new ID[deleteItems.size()]);
        String[] changedItemKeys = new String[delIDs.length];
        for (int ix = 0; ix < delIDs.length; ++ix) {
            changedItemKeys[ix] = delIDs[ix].getID();
        }
        Delete dels = new Delete();
        dels.setIds(delIDs);
        DeleteResponse dresp = sforceConn.delete(dels);
        DeleteResult[] dr = dresp.getResult();
        deleteItems.clear();
        delIDs = null;
        if (dr != null && dr.length != 0) {
            int batch_idx = -1;
            for (DeleteResult result : dr) {
                ++batch_idx;
                StringBuilder errors = new StringBuilder("");
                if (result.getSuccess()) {
                // TODO: send back the ID
                } else {
                    errors = addLog(result.getErrors(), batch_idx < changedItemKeys.length ? changedItemKeys[batch_idx] : "Batch index out of bounds");
                }
                if (exceptionForErrors && errors.toString().length() > 0) {
                    if (logWriter != null) {
                        logWriter.close();
                    }
                    throw new Exception(errors.toString());
                }
            }
        }
        return dr;
    }
    return null;
}
Also used : Delete(com.salesforce.soap.partner.Delete) DeleteResponse(com.salesforce.soap.partner.DeleteResponse) ID(com.salesforce.soap.partner.ID) DeleteResult(com.salesforce.soap.partner.DeleteResult)

Aggregations

Delete (com.salesforce.soap.partner.Delete)2 DeleteResponse (com.salesforce.soap.partner.DeleteResponse)2 DeleteResult (com.salesforce.soap.partner.DeleteResult)2 ID (com.salesforce.soap.partner.ID)2 Create (com.salesforce.soap.partner.Create)1 DescribeSObject (com.salesforce.soap.partner.DescribeSObject)1 SaveResult (com.salesforce.soap.partner.SaveResult)1 Update (com.salesforce.soap.partner.Update)1 Upsert (com.salesforce.soap.partner.Upsert)1 UpsertResult (com.salesforce.soap.partner.UpsertResult)1 SObject (com.salesforce.soap.partner.sobject.SObject)1 OMElement (org.apache.axiom.om.OMElement)1