Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package in.shop2020.support.utils;

import in.shop2020.model.v1.catalog.AmazonPromotion;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import com.amazonaws.mws.*;
import com.amazonaws.mws.model.GetFeedSubmissionResultRequest;
import com.amazonaws.mws.model.GetFeedSubmissionResultResponse;


public class CheckPromoFeedSubmission{

    public boolean ProcessFeed(String feedSubmissionId) throws NumberFormatException, IOException{
        String accessKeyId = "AKIAII3SGRXBJDPCHSGQ";
        String secretAccessKey = "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";
        String appName = "Test";
        String appVersion = "1.0";
        MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
        config.setServiceURL("https://mws.amazonservices.in");
        MarketplaceWebService service = new MarketplaceWebServiceClient(accessKeyId, secretAccessKey, appName, appVersion, config);
        GetFeedSubmissionResultRequest request = new GetFeedSubmissionResultRequest();
        request.setMerchant("AF6E3O0VE0X4D");
        request.setFeedSubmissionId(feedSubmissionId);
        OutputStream processingResult = null;
        String fileName = "";
        try
        {   
            fileName = "/home/amazon/feedSubmissionPromotion"+getTimeInMilliseconds()+"Result.xml";
            processingResult = new FileOutputStream(fileName);
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }
        request.setFeedSubmissionResultOutputStream(processingResult);
        try {
            GetFeedSubmissionResultResponse response = service.getFeedSubmissionResult(request);
        } catch (MarketplaceWebServiceException e) {
            e.printStackTrace();
            return false;
        }
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String str="";
        int lineNumber = 0;
        int numberOfRecords = 0;
        int numberOfRecordsSuccessful = 0;
        List<String> failedSkus = new ArrayList<String>();
        while ((str = reader.readLine()) != null) {
            lineNumber++;
            if (lineNumber<2) {
                continue;
            }
            String[] values = str.split("\t");
            if (lineNumber==2){
                numberOfRecords = Integer.valueOf(values[3]);
                continue;
            }
            
            if (lineNumber==3){
                numberOfRecordsSuccessful = Integer.valueOf(values[3]);
                continue;
            }
        }
        if (numberOfRecords==numberOfRecordsSuccessful && numberOfRecords>0){
            //All Processed Successfully
        }
        else{
            //Find the ones not processed
            lineNumber = 0;
            while ((str = reader.readLine()) != null) {
                lineNumber++;
                if (lineNumber<6) {
                    continue;
                }
                String[] values = str.split("\t");
                if (values[1].length()==0){
                    continue;
                }
                failedSkus.add(values[1]);
            }
        }
        System.out.println(failedSkus);
        return true;
    }

    public long getTimeInMilliseconds(){
        Calendar cal=GregorianCalendar.getInstance();
        return cal.getTimeInMillis();
    }
    
    public static void main(String[] args) throws NumberFormatException, IOException{
        CheckPromoFeedSubmission  ck = new CheckPromoFeedSubmission();
        ck.ProcessFeed("10360586988");
    }
}