public
class
DeliveryDataImport {
private
static
Logger logger = Logger.getLogger(DeliveryDataImport.
class
);
static
String endpoint =
"SALESFORCE ENDPOINT"
;
static
String username =
"USERNAME"
;
static
String password =
"PASSWORD"
;
static
String token =
"TOKEN"
;
static
String orgId =
"ORGID"
;
public
void
runUploadRequest()
throws
ClientProtocolException, IOException{
JobConfig config = createJobConfig();
try
{
JobStatus jobStatus = sendHttpRequest(config);
jobStatus = waitForJobToFinish(jobStatus);
JobResults jobResults = getResults(jobStatus);
if
(jobResults.getResults() !=
null
&& !jobResults.getResults().isEmpty()){
for
(JobResult jobResult : jobResults.getResults()){
if
(
"GenericDataPushImportLineResult"
.equals(jobResult.getDataType())) {
GenericDataPushImportLineResult lineResult = (GenericDataPushImportLineResult) jobResult.getData();
if
(!lineResult.isSuccess()){
logger.error(
"Error at line "
+ lineResult.getLineNr() +
": "
+ lineResult.getMessage());
}
}
else
if
(
"ForceBulkJobInfoResult"
.equals(jobResult.getDataType())) {
ForceBulkJobInfoResult forceBulkJobInfoResult = (ForceBulkJobInfoResult) jobResult.getData();
logger.debug(
"Bulk job id: "
+ forceBulkJobInfoResult.getId());
}
else
{
logger.warn(
"unknown datatype"
);
}
}
}
}
catch
(ClientProtocolException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
}
private
JobStatus sendHttpRequest(JobConfig config)
throws
ClientProtocolException, IOException {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setContentType(ContentType.create(
"multipart/mixed"
));
multipartEntityBuilder.addTextBody(
"JobConfig"
, Utilities.objectToXML(config), ContentType.APPLICATION_XML);
multipartEntityBuilder.addBinaryBody(
"data.csv"
, getDataFile());
HttpPost httpPost =
new
HttpPost(serviceURL +
"startJob"
);
httpPost.setEntity(multipartEntityBuilder.build());
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse httpResponse = httpClient.execute(httpPost);
logger.debug(httpResponse);
logger.debug(httpResponse.getStatusLine());
String response = EntityUtils.toString(httpResponse.getEntity());
logger.debug(response);
JobStatus jobStatus = (JobStatus) Utilities.xmlToObject(response, JobStatus.
class
.getCanonicalName());
logger.debug(jobStatus.getJobId());
logger.debug(jobStatus.getStatus());
return
jobStatus;
}
private
File getDataFile(){
File tempFile =
null
;
try
{
tempFile = File.createTempFile(
"jobdata"
,
".csv"
);
tempFile.deleteOnExit();
CSVWriter csvWriter =
new
CSVWriter(
new
FileWriter(tempFile));
String[] line =
new
String[]{
"Day"
,
"Type"
,
"UniqueKey"
,
"ViewedImpressions"
};
csvWriter.writeNext(line);
line =
new
String[]{
"2016-06-01"
,
"Monthly"
,
"2016-06-01"
,
"200"
};
csvWriter.writeNext(line);
line =
new
String[]{
"2016-06-01"
,
"Monthly"
,
""
,
"200"
};
csvWriter.writeNext(line);
csvWriter.close();
}
catch
(IOException e) {
e.printStackTrace();
}
return
tempFile;
}
private
JobStatus waitForJobToFinish(JobStatus jobStatus)
throws
ClientProtocolException, IOException {
JobIdentifier identifier =
new
JobIdentifier();
identifier.setJobId(jobStatus.getJobId());
identifier.setOrgId(jobStatus.getOrgId());
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
while
(jobStatus.getStatus() ==
0
|| jobStatus.getStatus() ==
1
) {
try
{
Thread.sleep(1000l);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
HttpPost httpPostGetStatus =
new
HttpPost(serviceURL +
"getJobStatus"
);
ByteArrayEntity byteArrayEntity =
new
ByteArrayEntity(Utilities.objectToXML(identifier).getBytes());
byteArrayEntity.setContentType(ContentType.APPLICATION_XML.getMimeType());
httpPostGetStatus.setEntity(byteArrayEntity);
HttpResponse httpResponse = httpClient.execute(httpPostGetStatus);
String response = EntityUtils.toString(httpResponse.getEntity());
logger.debug(response);
jobStatus = (JobStatus) Utilities.xmlToObject(response, JobStatus.
class
.getCanonicalName());
logger.debug(jobStatus.getMessage());
}
return
jobStatus;
}
private
JobResults getResults(JobStatus jobStatus)
throws
ClientProtocolException, IOException{
JobIdentifier identifier =
new
JobIdentifier();
identifier.setJobId(jobStatus.getJobId());
identifier.setOrgId(jobStatus.getOrgId());
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPostGetStatus =
new
HttpPost(serviceURL +
"getJobResults"
);
ByteArrayEntity byteArrayEntity =
new
ByteArrayEntity(Utilities.objectToXML(identifier).getBytes());
byteArrayEntity.setContentType(ContentType.APPLICATION_XML.getMimeType());
httpPostGetStatus.setEntity(byteArrayEntity);
HttpResponse httpResponse = httpClient.execute(httpPostGetStatus);
String response = EntityUtils.toString(httpResponse.getEntity());
logger.debug(response);
return
(JobResults) Utilities.xmlToObject(response, JobResults.
class
.getCanonicalName());
}
private
JobConfig createJobConfig(){
JobConfig config =
new
JobConfig();
config.setEndpoint(endpoint);
config.setPassword(password);
config.setUsername(username);
config.setToken(token);
config.setOrgId(orgId);
config.setDataType(
"csv"
);
config.setOperation(
"GenericDataPushImport"
);
config.setObjectName(
"ADvendio__DeliveryDataImport__c"
);
return
config;
}
}