/
7.7.3.1 Sample Code for Data Importer

7.7.3.1 Sample Code for Data Importer


Example code


public class DeliveryDataImport {
 
    private static Logger logger = Logger.getLogger(DeliveryDataImport.class);
     
    //Salesforce login-information
    static String endpoint = "SALESFORCE ENDPOINT"//example: https://na7.salesforce.com/services/Soap/u/28.0
    static String username = "USERNAME";
    static String password = "PASSWORD";
    static String token = "TOKEN";
    static String orgId = "ORGID";
    
    //service URL
    static String serviceUrl = "https://altona.advendio.com:8080/AdServerGateway_2.73/";
 
    public void runUploadRequest() throws ClientProtocolException, IOException{
        //storing the request information to a Object witch is later converted to XML
        JobConfig config = createJobConfig();
         
        try {
        JobStatus jobStatus = sendHttpRequest(config);
         
        jobStatus = waitForJobToFinish(jobStatus);
 
        JobResults jobResults = getResults(jobStatus);
        //only faulty lines produce a result 
        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"));
         
        //Converting request information to XML an add it as first part to the http request
        multipartEntityBuilder.addTextBody("JobConfig", Utilities.objectToXML(config), ContentType.APPLICATION_XML);
         
        //adding the data to the http request
        multipartEntityBuilder.addBinaryBody("data.csv", getDataFile());
         
        HttpPost httpPost = new HttpPost(serviceURL + "startJob");
        httpPost.setEntity(multipartEntityBuilder.build());
         
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //run the request
        HttpResponse httpResponse = httpClient.execute(httpPost);
        logger.debug(httpResponse);
        logger.debug(httpResponse.getStatusLine());
        String response = EntityUtils.toString(httpResponse.getEntity());
        logger.debug(response);
 
        //parsing the JobStatus
        JobStatus jobStatus = (JobStatus) Utilities.xmlToObject(response, JobStatus.class.getCanonicalName());
        logger.debug(jobStatus.getJobId());
        logger.debug(jobStatus.getStatus());
 
        return jobStatus;
    }
 
    private File getDataFile(){
        //generating a demo data file
        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"}; //this should generate an error
            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();
        //wait for job to finish
        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;
    }
 }

Related articles

Filter by label

There are no items with the selected labels at this time.

Related content

7.7.3 Import any external Delivery Data
7.7.3 Import any external Delivery Data
Read with this
Programmatic Data Import API - Documentation
Programmatic Data Import API - Documentation
More like this
7.7.4 Dataplan JournalDesigner (Print)
7.7.4 Dataplan JournalDesigner (Print)
Read with this
7.7.1.5 Setup Xandr SSP and AdServer Connection
7.7.1.5 Setup Xandr SSP and AdServer Connection
More like this
7.7.8 Programmatic Add-On - Programmatic Revenue Import
7.7.8 Programmatic Add-On - Programmatic Revenue Import
Read with this
7.7.8.2.1 How to start the Manual programmatic data import for certain time frames and test runs?
7.7.8.2.1 How to start the Manual programmatic data import for certain time frames and test runs?
More like this