Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse CSV batch result responses. #202

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/main/java/com/sforce/async/BulkConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -586,6 +587,11 @@ public BatchResult getBatchResult(String jobId, String batchId, ContentType cont
Result[] results = deserializeJsonToObject(stream, Result[].class);
batchResult.setResult(results);
return batchResult;
} else if (contentType == ContentType.CSV || contentType == ContentType.ZIP_CSV) {
BatchResult batchResult = new BatchResult();
Result[] results = deserializeCsvResults(stream);
batchResult.setResult(results);
return batchResult;
} else {
XmlInputStream xin = new XmlInputStream();
xin.setInput(stream, "UTF-8");
Expand Down Expand Up @@ -808,6 +814,53 @@ static <T> T deserializeJsonToObject (InputStream in, Class<T> tmpClass) throws
return mapper.readValue(in, tmpClass);
}

static Result[] deserializeCsvResults (InputStream in) throws IOException, ConnectionException {
CSVReader reader = new CSVReader(in, "UTF-8");
int idIx, successIx, createdIx, errorIx;
idIx = successIx = createdIx = errorIx = -1;
ArrayList<String> record = reader.nextRecord();
for (int i = 0; i < record.size(); i++) {
switch (record.get(i)) {
case "Id":
idIx = i;
break;
case "Success":
successIx = i;
break;
case "Created":
createdIx = i;
break;
case "Error":
errorIx = i;
break;
}
}
ArrayList<Result> results = new ArrayList<Result>();
record = reader.nextRecord();
while (record != null) {
Result result = new Result();
result.setId(record.get(idIx));
result.setSuccess(Boolean.parseBoolean(record.get(successIx)));
result.setCreated(Boolean.parseBoolean(record.get(createdIx)));
String error = record.get(errorIx);
if (error != null && !error.isEmpty()) {
String[] parts = error.split(":", 2);
Error error_obj = new Error();
if (parts.length == 2 && !parts[0].isEmpty()) {
error_obj.setStatusCode(StatusCode.valueOf(parts[0]));
error_obj.setMessage(parts[1]);
} else {
error_obj.setMessage(error);
}
result.setErrors(new Error[] { error_obj });
}
results.add(result);
record = reader.nextRecord();
}

return results.toArray(new Result[0]);
}

public JobInfo abortJob(String jobId) throws AsyncApiException {
JobInfo job = new JobInfo();
job.setId(jobId);
Expand Down