ebay.dts.client.BulkDataExchangeActions.java Source code

Java tutorial

Introduction

Here is the source code for ebay.dts.client.BulkDataExchangeActions.java

Source

/*
 *  2009-2013 eBay Inc., All Rights Reserved
 * Licensed under CDDL 1.0 -  http://opensource.org/licenses/cddl1.php
 */

package ebay.dts.client;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ebay.marketplace.services.AbortJobResponse;
import com.ebay.marketplace.services.BulkDataExchangeServicePort;
import com.ebay.marketplace.services.CreateUploadJobRequest;
import com.ebay.marketplace.services.CreateUploadJobResponse;
import com.ebay.marketplace.services.DownloadRequestFilter;
import com.ebay.marketplace.services.FeeSettlementReportFilter;
import com.ebay.marketplace.services.GetJobStatusResponse;
import com.ebay.marketplace.services.GetJobsResponse;
import com.ebay.marketplace.services.JobProfile;
import com.ebay.marketplace.services.JobStatus;
import com.ebay.marketplace.services.StartDownloadJobRequest;
import com.ebay.marketplace.services.StartDownloadJobResponse;
import com.ebay.marketplace.services.StartUploadJobRequest;
import com.ebay.marketplace.services.StartUploadJobResponse;

import it.ebaypusher.controller.EbayConnectorException;

/**
 *
 * @author zhuyang
 */
public class BulkDataExchangeActions {

    private BulkDataExchangeCall call;

    private static Log logger = LogFactory.getLog(BulkDataExchangeActions.class);

    public BulkDataExchangeActions(Properties prop) {
        call = new BulkDataExchangeCall(prop.getProperty("bulkDataExchangeURL"), prop.getProperty("userToken"));
    }

    public GetJobsResponse getJobs(String conditionsStr) throws Exception {
        String callName = "getJobs";
        BulkDataExchangeServicePort port = call.setRequestContext(callName);
        com.ebay.marketplace.services.GetJobsRequest getJobsReq = new com.ebay.marketplace.services.GetJobsRequest();
        if (conditionsStr != null && conditionsStr.length() > 0) {
            XMLGregorianCalendar fromCal = null;
            // validating the input conditions
            XMLGregorianCalendar toCal = null;
            // java.util.Map<String, String> conditions =
            // getPairs(conditionsStr, "&");
            java.util.Map<String, String> conditions = getPairs(conditionsStr, "&");
            if (conditions.get("creationTimeFrom") != null && !conditions.get("creationTimeFrom").equals("")) {
                fromCal = parseDate(conditions.get("creationTimeFrom"));
                if (fromCal == null) {
                    logger.info("Criteria creationTimeFrom has been ignored.");
                }
                getJobsReq.setCreationTimeFrom(fromCal);
            }
            if (conditions.get("creationTimeTo") != null && !conditions.get("creationTimeTo").equals("")) {
                toCal = parseDate(conditions.get("creationTimeTo"));
                if (toCal == null) {
                    logger.info("Criteria creationTimeTo has been ignored.");
                }
                getJobsReq.setCreationTimeTo(toCal);
            }

            if (conditions.get("jobType") != null && !conditions.get("jobType").equals("")) {
                List<String> jobType = getJobsReq.getJobType();
                jobType.add(conditions.get("jobType"));
            }

            if (conditions.get("jobStatus") != null && !conditions.get("jobStatus").equals("")) {
                logger.info(" jobStatus condition= " + conditions.get("jobStatus").toString());
                List<JobStatus> jobStatuss = getJobsReq.getJobStatus();
                try {
                    jobStatuss.add(JobStatus.fromValue(conditions.get("jobStatus")));
                } catch (java.lang.IllegalArgumentException e) {
                    logger.error(
                            "Service call failed on the client side. \nThe input jobStatus is not valid, please check it and try again.");
                    return null;
                }
            }
        }
        com.ebay.marketplace.services.GetJobsResponse getJobsResp = port.getJobs(getJobsReq);
        List<JobProfile> jobs = getJobsResp.getJobProfile();
        Iterator<JobProfile> itr = jobs.iterator();
        while (itr.hasNext()) {
            JobProfile job = (JobProfile) itr.next();
            logger.trace(job.getJobId() + " : " + job.getJobType() + " : " + job.getJobStatus());
        }

        return getJobsResp;

    }// ENDOF getJobs()

    public GetJobStatusResponse getJobStatus(String jobId) throws EbayConnectorException {
        String callName = "getJobStatus";
        BulkDataExchangeServicePort port = call.setRequestContext(callName);
        com.ebay.marketplace.services.GetJobStatusRequest req = new com.ebay.marketplace.services.GetJobStatusRequest();
        req.setJobId(jobId);

        com.ebay.marketplace.services.GetJobStatusResponse getJobsResp = port.getJobStatus(req);
        return getJobsResp;
    }// ENDOF getJobStatus()

    public AbortJobResponse abortJobs(String jobId) throws EbayConnectorException {
        String callName = "abortJob";
        BulkDataExchangeServicePort port = call.setRequestContext(callName);
        com.ebay.marketplace.services.AbortJobRequest abortJobsReq = new com.ebay.marketplace.services.AbortJobRequest();
        abortJobsReq.setJobId(jobId);
        com.ebay.marketplace.services.AbortJobResponse abortJobsResp = port.abortJob(abortJobsReq);
        return abortJobsResp;

    }// ENDOF abortJobs()

    public CreateUploadJobResponse createUploadJob(String uploadJobType) throws EbayConnectorException {
        String callName = "createUploadJob";
        BulkDataExchangeServicePort port = call.setRequestContext(callName);
        CreateUploadJobRequest createUploadJobReq = new CreateUploadJobRequest();
        createUploadJobReq.setUploadJobType(uploadJobType);// ("AddFixedPriceItem");
        String uuid = java.util.UUID.randomUUID().toString();
        createUploadJobReq.setUUID(uuid);
        // process result here
        CreateUploadJobResponse result = port.createUploadJob(createUploadJobReq);
        return result;

    }// ENDOF createUploadJob()

    public StartDownloadJobResponse startDownloadJob(String downloadJobType, String startTimeString)
            throws Exception {
        String callName = "startDownloadJob";
        BulkDataExchangeServicePort port = call.setRequestContext(callName);

        com.ebay.marketplace.services.StartDownloadJobRequest startDownloadReq = new StartDownloadJobRequest();
        startDownloadReq.setDownloadJobType(downloadJobType);

        XMLGregorianCalendar xmlGregorianStart = null;
        if (startTimeString != null && startTimeString.length() > 0) {
            FeeSettlementReportFilter feeFilter = new FeeSettlementReportFilter();
            DownloadRequestFilter reqFilter = new DownloadRequestFilter();
            xmlGregorianStart = parseDateTime(startTimeString);
            feeFilter.setStartTime(xmlGregorianStart);
            reqFilter.setFeeSettlementReportFilter(feeFilter);
            startDownloadReq.setDownloadRequestFilter(reqFilter);
        }
        String uuid = java.util.UUID.randomUUID().toString();
        startDownloadReq.setUUID(uuid);
        // process result here
        StartDownloadJobResponse result = port.startDownloadJob(startDownloadReq);
        return result;

    }// ENDOF startDownloadJob()

    public StartUploadJobResponse startUploadJob(String jobid) throws EbayConnectorException {
        String callName = "startUploadJob";
        BulkDataExchangeServicePort port = call.setRequestContext(callName);
        com.ebay.marketplace.services.StartUploadJobRequest request = new StartUploadJobRequest();

        request.setJobId(jobid);
        // process result here
        StartUploadJobResponse result = port.startUploadJob(request);
        return result;

    } // ENDOF startUploadJob();

    /*
     * util method, construct the Calendar object from the input string, it
     * accepts format of "yyyy-MM-dd"
     */
    private static XMLGregorianCalendar parseDate(String cal) throws DatatypeConfigurationException {
        GregorianCalendar ret = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            df.parse(cal);
            String[] parts = cal.split("-");
            ret = (GregorianCalendar) Calendar.getInstance();
            ret.set(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]) - 1, Integer.parseInt(parts[2]), 0, 0,
                    0);
            // get here and we know the format is correct
        } catch (ParseException e) {
            logger.error("ParseException caught when parsing your date string, please fix it and retry.");
            return null;
        }
        DatatypeFactory factory = DatatypeFactory.newInstance();
        XMLGregorianCalendar xmlGregorianCalendar = factory.newXMLGregorianCalendar(ret);
        return xmlGregorianCalendar;
    }

    private static XMLGregorianCalendar parseDateTime(String cal) throws DatatypeConfigurationException {

        DatatypeFactory factory1 = DatatypeFactory.newInstance();
        XMLGregorianCalendar calendar1 = factory1.newXMLGregorianCalendar();
        String[] dateItems;
        try {
            // df.parse(cal);
            if (cal.indexOf("_") == -1) {
                dateItems = cal.split("-");
                calendar1.setYear(Integer.parseInt(dateItems[0]));
                calendar1.setMonth(Integer.parseInt(dateItems[1]));
                calendar1.setDay(Integer.parseInt(dateItems[2]));
                // calendar1.setTime(00, 00, 00,000);
                calendar1.setTime(00, 00, 00);
            } else {
                String[] parts = cal.split("_");
                dateItems = parts[0].split("-");
                String[] timeItems = parts[1].split(":");
                calendar1.setYear(Integer.parseInt(dateItems[0]));
                calendar1.setMonth(Integer.parseInt(dateItems[1]));
                calendar1.setDay(Integer.parseInt(dateItems[2]));
                if (timeItems.length != 0) {
                    switch (timeItems.length) {
                    case 1: {
                        calendar1.setTime(Integer.parseInt(timeItems[0]), 00, 00, 000);
                        break;
                    }
                    case 2: {
                        calendar1.setTime(Integer.parseInt(timeItems[0]), Integer.parseInt(timeItems[1]), 00, 000);
                        break;
                    }
                    case 3: {
                        calendar1.setTime(Integer.parseInt(timeItems[0]), Integer.parseInt(timeItems[1]),
                                Integer.parseInt(timeItems[2]), 000);
                        break;
                    }
                    case 4: {
                        calendar1.setTime(Integer.parseInt(timeItems[0]), Integer.parseInt(timeItems[1]),
                                Integer.parseInt(timeItems[2]), Integer.parseInt(timeItems[3]));
                        break;
                    }
                    }

                }
            }

            // get here and we know the format is correct
        } catch (java.lang.NumberFormatException e) {
            logger.error("NumberFormatException caught when parse the DateTime string: " + cal);
            return null;
        }

        calendar1.setTimezone(0);
        logger.debug(calendar1.toXMLFormat());
        return calendar1;
    }

    /*
     * getPairs method parse the getJobs condition String, and put the name
     * value pair in to Map object for easier accessing. Sample condition string
     * can be:
     * "creationTimeFrom=2008-09-01&creationTimeTo=2008-10-02&jobType=RelistItem&jobStatus=Failed"
     * if the delimited char is "&"
     */
    public static Map<String, String> getPairs(String pairs, String splitStr) {
        if (pairs == null || pairs.equals("")) {
            return null;
        }
        String[] reqFields = pairs.split(splitStr);
        Map<String, String> parameterMap = new HashMap<String, String>();
        for (int i = 0; i < reqFields.length; i++) {
            String nameValuepair = reqFields[i];
            String[] nameValueArray = nameValuepair.split("=");
            parameterMap.put(nameValueArray[0].trim(),
                    nameValueArray.length <= 1 ? null : nameValueArray[1].trim());
        }
        return parameterMap;
    }

}