use of org.talend.components.marketo.runtime.client.rest.type.BulkImport in project components by Talend.
the class MarketoBulkExecClient method bulkImport.
/**
* Imports a spreadsheet of leads or custom objects into the target instance
*
* POST /bulk/v1/leads/import.json
*
* POST /bulk/v1/customobjects/{apiName}/import.json
*
* @param parameters
* @return
*/
public MarketoRecordResult bulkImport(TMarketoBulkExecProperties parameters) {
String importFilename = parameters.bulkFilePath.getValue();
String format = parameters.bulkFileFormat.getValue().name();
Integer pollWaitTime = parameters.pollWaitTime.getValue();
String logDownloadPath = parameters.logDownloadPath.getValue();
String lookupField;
Integer listId;
String partitionName;
String customObjectName;
current_uri = new StringBuilder(bulkPath);
Boolean isImportingLeads = parameters.bulkImportTo.getValue().equals(BulkImportTo.Leads);
if (isImportingLeads) {
lookupField = parameters.lookupField.getValue().name();
listId = parameters.listId.getValue();
partitionName = parameters.partitionName.getValue();
//
current_uri.append(API_PATH_BULK_LEADS).append(//
fmtParams(FIELD_ACCESS_TOKEN, accessToken, true)).append(fmtParams(FIELD_LOOKUP_FIELD, lookupField));
if (listId != null) {
current_uri.append(fmtParams(FIELD_LIST_ID, listId));
}
if (!StringUtils.isEmpty(partitionName)) {
current_uri.append(fmtParams(FIELD_PARTITION_NAME, partitionName));
}
} else {
customObjectName = parameters.customObjectName.getValue();
//
current_uri.append(String.format(API_PATH_BULK_CUSTOMOBJECTS, customObjectName)).append(fmtParams(FIELD_ACCESS_TOKEN, accessToken, true));
}
current_uri.append(fmtParams(FIELD_FORMAT, format));
//
MarketoRecordResult mkto = new MarketoRecordResult();
try {
LOG.debug("bulkImport {}.", current_uri);
BulkImportResult rs = executePostFileRequest(BulkImportResult.class, importFilename);
LOG.debug("rs = {}.", rs);
mkto.setRequestId(REST + "::" + rs.getRequestId());
mkto.setSuccess(rs.isSuccess());
if (!mkto.isSuccess()) {
mkto.setRecordCount(0);
mkto.setErrors(Arrays.asList(new MarketoError(REST, rs.getErrors().get(0).getCode(), messages.getMessage("bulkimport.error.import", rs.getErrors().get(0).getMessage()))));
return mkto;
}
BulkImport bulkResult = rs.getResult().get(0);
if (bulkResult.getStatus().equals(BULK_STATUS_FAILED)) {
// request Failed
String err = messages.getMessage("bulkimport.status.failed", bulkResult.getMessage());
LOG.error("{}.", err);
mkto.setSuccess(false);
mkto.setErrors(Arrays.asList(new MarketoError(REST, err)));
} else if (bulkResult.getStatus().equals(BULK_STATUS_COMPLETE)) {
// already Complete
bulkResult = getStatusesForBatch(bulkResult, logDownloadPath);
} else {
// Importing, Queued
while (true) {
try {
LOG.warn(messages.getMessage("bulkimport.status.waiting", pollWaitTime));
Thread.sleep(pollWaitTime * 1000L);
current_uri = new StringBuilder(bulkPath);
if (bulkResult.isBulkLeadsImport()) {
current_uri.append(String.format(API_PATH_BULK_LEADS_RESULT_STATUS, bulkResult.getBatchId()));
} else {
current_uri.append(String.format(API_PATH_BULK_CUSTOMOBJECTS_RESULT, bulkResult.getObjectApiName(), bulkResult.getBatchId(), "status"));
}
current_uri.append(fmtParams(FIELD_ACCESS_TOKEN, accessToken, true));
LOG.debug("status = {}.", current_uri);
rs = (BulkImportResult) executeGetRequest(BulkImportResult.class);
if (rs.isSuccess() && rs.getResult().get(0).getStatus().equals(BULK_STATUS_COMPLETE)) {
bulkResult = getStatusesForBatch(rs.getResult().get(0), logDownloadPath);
break;
} else {
LOG.warn(messages.getMessage("bulkimport.status.current", rs.getResult().get(0).getStatus()));
}
} catch (InterruptedException e) {
LOG.error("Sleep interrupted : {}", e);
throw new MarketoException(REST, "Sleep interrupted : " + e.getLocalizedMessage());
}
}
}
mkto.setRecords(Arrays.asList(bulkResult.toIndexedRecord()));
mkto.setRecordCount(1);
mkto.setRemainCount(0);
} catch (MarketoException e) {
mkto.setSuccess(false);
mkto.setErrors(Arrays.asList(e.toMarketoError()));
}
return mkto;
}
Aggregations