Skip to content

Commit

Permalink
Merge pull request #3 from tzmfreedom/change-type-value
Browse files Browse the repository at this point in the history
change referenced sobject type and change version
  • Loading branch information
tzmfreedom committed Jun 29, 2015
2 parents 6a55716 + ea70471 commit eb01c25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ configurations {
provided
}

version = "0.1.1"
version = "0.1.2"

dependencies {
compile "org.embulk:embulk-core:0.6.11"
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/org/embulk/output/SalesforceOutputPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.google.common.base.Optional;
import com.sforce.soap.partner.Connector;
import com.sforce.soap.partner.DeleteResult;
import com.sforce.soap.partner.DescribeSObjectResult;
import com.sforce.soap.partner.Field;
import com.sforce.soap.partner.FieldType;
import com.sforce.soap.partner.GetUserInfoResult;
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.soap.partner.SaveResult;
Expand All @@ -18,11 +21,14 @@
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.embulk.config.CommitReport;
import org.embulk.config.Config;
import org.embulk.config.ConfigDefault;
import org.embulk.config.ConfigDiff;
import org.embulk.config.ConfigException;
import org.embulk.config.ConfigSource;
import org.embulk.config.Task;
import org.embulk.config.TaskSource;
Expand All @@ -46,7 +52,8 @@ public class SalesforceOutputPlugin
{
protected static Logger logger;
private static PartnerConnection client = null;

private static Map<String, String> externalIdToObjectNameMap = null;

public interface PluginTask
extends Task
{
Expand Down Expand Up @@ -113,6 +120,13 @@ public ConfigDiff transaction(ConfigSource config,
client = Connector.newConnection(connectorConfig);
GetUserInfoResult userInfo = client.getUserInfo();
logger.info("login successful with {}", userInfo.getUserName());
externalIdToObjectNameMap = new HashMap<>();
DescribeSObjectResult describeResult = client.describeSObject(task.getSObject());
for (Field field : describeResult.getFields()) {
if (field.getType() == FieldType.reference) {
externalIdToObjectNameMap.put(field.getRelationshipName(), field.getReferenceTo()[0]);
}
}
}
} catch(ConnectionException ex) {
logger.error("Login error. Please check your credentials.");
Expand Down Expand Up @@ -226,7 +240,12 @@ private void columnWithReferenceCheck(String name, Object value) {
String externalIdFieldName = tokens[1];

SObject sObjRef = new SObject();
sObjRef.setType(referencesFieldName.replaceAll("__(r|R)", "__c"));
String refFieldApiName = referencesFieldName.replaceAll("__R", "__r");
if (externalIdToObjectNameMap.containsKey(refFieldApiName)) {
sObjRef.setType(externalIdToObjectNameMap.get(refFieldApiName));
} else {
throw new ConfigException("Invalid Relationship Name '" + refFieldApiName + "'");
}
sObjRef.addField(externalIdFieldName, value);
record.addField(referencesFieldName, sObjRef);
} else {
Expand All @@ -247,6 +266,8 @@ private void columnWithReferenceCheck(String name, Object value) {
this.action(this.records);
logger.info("Number of processed records: {}", this.numOfSuccess + this.numOfError);
}
} catch (ConfigException ex) {
logger.error("Configuration Error: {}", ex.getMessage());
} catch (ApiFault ex) {
logger.error("API Error: {}", ex.getExceptionMessage());
} catch (ConnectionException ex) {
Expand Down

0 comments on commit eb01c25

Please sign in to comment.