Search in sources :

Example 1 with OutputAction

use of org.talend.components.salesforce.SalesforceOutputProperties.OutputAction in project components by Talend.

the class SalesforceWriter method write.

@SuppressWarnings("unchecked")
@Override
public void write(Object datum) throws IOException {
    dataCount++;
    // Ignore empty rows.
    if (null == datum) {
        return;
    }
    // This is all we need to do in order to ensure that we can process the incoming value as an IndexedRecord.
    if (null == factory) {
        factory = (IndexedRecordConverter<Object, ? extends IndexedRecord>) SalesforceAvroRegistry.get().createIndexedRecordConverter(datum.getClass());
    }
    IndexedRecord input = factory.convertToAvro(datum);
    Property<OutputAction> outputAction = sprops.outputAction;
    LOGGER.info(MESSAGES.getMessage("info.startMessage", sprops.outputAction.getPossibleValuesDisplayName(outputAction.getValue()).toLowerCase(), dataCount));
    switch(outputAction.getValue()) {
        case INSERT:
            insert(input);
            break;
        case UPDATE:
            update(input);
            break;
        case UPSERT:
            upsert(input);
            break;
        case DELETE:
            delete(input);
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) OutputAction(org.talend.components.salesforce.SalesforceOutputProperties.OutputAction) XmlObject(com.sforce.ws.bind.XmlObject) SObject(com.sforce.soap.partner.sobject.SObject)

Example 2 with OutputAction

use of org.talend.components.salesforce.SalesforceOutputProperties.OutputAction in project components by Talend.

the class SalesforceWriter method handleReject.

private void handleReject(IndexedRecord input, Error[] resultErrors, String[] changedItemKeys, int batchIdx) throws IOException {
    String changedItemKey = null;
    if (batchIdx < changedItemKeys.length) {
        if (changedItemKeys[batchIdx] != null) {
            changedItemKey = changedItemKeys[batchIdx];
        } else {
            changedItemKey = String.valueOf(batchIdx + 1);
        }
    } else {
        changedItemKey = "Batch index out of bounds";
    }
    StringBuilder errors = SalesforceRuntime.addLog(resultErrors, changedItemKey, logWriter);
    if (exceptionForErrors) {
        if (errors.toString().length() > 0) {
            if (logWriter != null) {
                logWriter.close();
            }
            throw new IOException(errors.toString());
        }
    } else {
        rejectCount++;
        Schema outSchema = sprops.schemaReject.schema.getValue();
        if (outSchema == null || outSchema.getFields().size() == 0) {
            return;
        }
        if (input.getSchema().equals(outSchema)) {
            rejectedWrites.add(input);
        } else {
            IndexedRecord reject = null;
            if (AvroUtils.isIncludeAllFields(outSchema)) {
                Schema runtimeSchema = input.getSchema();
                List<Schema.Field> addedFields = new ArrayList<>();
                // Check whether design schema has additional field
                Schema.Field errorCodeField = outSchema.getField(TSalesforceOutputProperties.FIELD_ERROR_CODE);
                Schema.Field errorField = outSchema.getField(TSalesforceOutputProperties.FIELD_ERROR_FIELDS);
                Schema.Field errorMsgField = outSchema.getField(TSalesforceOutputProperties.FIELD_ERROR_MESSAGE);
                if (errorCodeField != null) {
                    addedFields.add(new Schema.Field(errorCodeField.name(), errorCodeField.schema(), errorCodeField.doc(), errorCodeField.defaultVal()));
                }
                if (errorField != null) {
                    addedFields.add(new Schema.Field(errorField.name(), errorField.schema(), errorField.doc(), errorField.defaultVal()));
                }
                if (errorMsgField != null) {
                    addedFields.add(new Schema.Field(errorMsgField.name(), errorMsgField.schema(), errorMsgField.doc(), errorMsgField.defaultVal()));
                }
                if (addedFields.size() > 0) {
                    // Append additional fields to the runtime schema
                    runtimeSchema = AvroUtils.appendFields(runtimeSchema, addedFields.toArray(new Schema.Field[addedFields.size()]));
                }
                reject = new GenericData.Record(runtimeSchema);
            } else {
                reject = new GenericData.Record(outSchema);
            }
            for (Schema.Field outField : reject.getSchema().getFields()) {
                Object outValue = null;
                Schema.Field inField = input.getSchema().getField(outField.name());
                if (inField != null) {
                    outValue = input.get(inField.pos());
                } else if (resultErrors.length > 0) {
                    Error error = resultErrors[0];
                    if (TSalesforceOutputProperties.FIELD_ERROR_CODE.equals(outField.name())) {
                        outValue = error.getStatusCode() != null ? error.getStatusCode().toString() : null;
                    } else if (TSalesforceOutputProperties.FIELD_ERROR_FIELDS.equals(outField.name())) {
                        StringBuffer fields = new StringBuffer();
                        for (String field : error.getFields()) {
                            fields.append(field);
                            fields.append(",");
                        }
                        if (fields.length() > 0) {
                            fields.deleteCharAt(fields.length() - 1);
                        }
                        outValue = fields.toString();
                    } else if (TSalesforceOutputProperties.FIELD_ERROR_MESSAGE.equals(outField.name())) {
                        outValue = error.getMessage();
                    }
                }
                reject.put(outField.pos(), outValue);
            }
            rejectedWrites.add(reject);
        }
        Property<OutputAction> outputAction = sprops.outputAction;
        LOGGER.info(MESSAGES.getMessage("info.rejectedRecord", sprops.outputAction.getPossibleValuesDisplayName(outputAction.getValue()).toLowerCase(), dataCount));
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) Error(com.sforce.soap.partner.Error) IOException(java.io.IOException) GenericData(org.apache.avro.generic.GenericData) OutputAction(org.talend.components.salesforce.SalesforceOutputProperties.OutputAction) XmlObject(com.sforce.ws.bind.XmlObject) SObject(com.sforce.soap.partner.sobject.SObject)

Aggregations

SObject (com.sforce.soap.partner.sobject.SObject)2 XmlObject (com.sforce.ws.bind.XmlObject)2 IndexedRecord (org.apache.avro.generic.IndexedRecord)2 OutputAction (org.talend.components.salesforce.SalesforceOutputProperties.OutputAction)2 Error (com.sforce.soap.partner.Error)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Schema (org.apache.avro.Schema)1 GenericData (org.apache.avro.generic.GenericData)1