Subversion Repositories SmartDukaan

Rev

Rev 10196 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020;

import in.shop2020.logistics.DeliveryType;
import in.shop2020.logistics.LogisticsInfo;
import in.shop2020.logistics.LogisticsServiceException;
import in.shop2020.model.v1.catalog.CatalogService.Client;
import in.shop2020.model.v1.catalog.FlipkartItem;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.catalog.MarketPlaceItemPrice;
import in.shop2020.model.v1.catalog.status;
import in.shop2020.model.v1.inventory.InventoryType;
import in.shop2020.model.v1.inventory.ItemInventory;
import in.shop2020.model.v1.inventory.Warehouse;
import in.shop2020.model.v1.inventory.WarehouseLocation;
import in.shop2020.model.v1.inventory.WarehouseType;
import in.shop2020.model.v1.order.OrderSource;
import in.shop2020.serving.model.Order;
import in.shop2020.serving.model.OrderItems;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.InventoryClient;
import in.shop2020.thrift.clients.LogisticsClient;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.utils.GmailUtils;
import inventory.FlipkartInventoryItem;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.mail.MessagingException;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;

import com.google.gson.Gson;



public class CreateSubmitFlipkartPricingFeed {
        private static List<MarketPlaceItemPrice> flipkartPricingItems;
        private static Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>();
        private static Map<Long,FlipkartItem> flipkartItemsMap = new HashMap<Long,FlipkartItem>();
        static java.text.SimpleDateFormat sdf;
        static String emailFromAddress;
        static String password;
        static GmailUtils mailer;
        static String sendTo[];
        static
        {
                sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
                emailFromAddress = "build@shop2020.in";
                password = "cafe@nes";
                mailer = new GmailUtils();
                sendTo = new String[]{"vikram.raghav@shop2020.in"};
                /*sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "vikram.raghav@shop2020.in", "rajneesh.arora@shop2020.in",
                                "khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
                                "yukti.jain@shop2020.in","manisha.sharma@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","amar.kumar@shop2020.in"};
                 */
                 Client catalogServiceClient = null;
                 try {
                         catalogServiceClient = new CatalogClient().getClient();
                 } catch (TTransportException e) {
                         e.printStackTrace();
                 }
                 try {
                         flipkartPricingItems = catalogServiceClient.getMarketPlaceItemsForPriceUpdate(8);
                         List<Item> aliveItems = catalogServiceClient.getAllAliveItems();
                                for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
                                        aliveItemsMap.put(thriftItem.getId(), thriftItem);
                                }
                        List<FlipkartItem> flipkartItems = catalogServiceClient.getAllFlipkartItems();
                        for(FlipkartItem flipkartItem:flipkartItems){
                                flipkartItemsMap.put(flipkartItem.getItem_id(),flipkartItem);
                        }
                         
                 } catch (TException e) {
                         e.printStackTrace();
                 }
                 if(flipkartPricingItems.size()==0){
                         String text = "";
                         try {
                                 mailer.sendSSLMessage(sendTo,"No items to send prices ", emailFromAddress, password, text);
                                 System.exit(0);
                         } catch (MessagingException e) {
                                 e.printStackTrace();
                         }
                 }

        }

        public static void main(String[] args) throws ClientProtocolException, TException{
                DefaultHttpClient httpclient = new DefaultHttpClient();
                httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);
                /*httpclient.getCredentialsProvider().setCredentials(
                                new AuthScope("sandbox-api.flipkart.net", 443),
                                new UsernamePasswordCredentials("og3yg5994bxbxxc1", "8a137068-417c-4e4f-ae95-9295ab938d88"));/// Test Server Access Keys
                 */
                httpclient.getCredentialsProvider().setCredentials(
                                new AuthScope("api.flipkart.net", 443),
                                new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4")); //Prod Server Access Keys
                HttpPost httppost = new HttpPost("https://api.flipkart.net/sellers/skus/listings/bulk"); // PROD
                //HttpPost httppost = new HttpPost("https://sandbox-api.flipkart.net/sellers/skus/listings/bulk"); // TEST
                StringBuffer jsonRequest = new StringBuffer();
                StringBuffer jsonStart = new StringBuffer();
                jsonStart = jsonStart.append("{\"listings\":[");
                StringBuffer jsonEnd = new StringBuffer();
                jsonEnd.append("]}");
                //System.out.println("JSON request  " + jsonRequest);
                int i=0;
                StringBuffer pricingItems =new StringBuffer();
                String tableHeader = "<html><table border=\"1\" align=\"center\"><tr>" 
                        + "<td><b>" + "Product Name" +"</b></td>"
                        +"<td><b>" + "Item ID" + "</b></td>"
                        +"<td><b>" + "Selling Price" + "</b></td>"
                        +"</tr>";
                String tableFooter = "</table></html>";
                Item item;
                StringBuffer feedResponse = null;
                for(MarketPlaceItemPrice entry:flipkartPricingItems){
                        feedResponse=  new StringBuffer();
                        item = aliveItemsMap.get(entry.getItem_id());
                        System.out.println("Item ID  " + entry.getItem_id());
                        System.out.println("Product Name " + getProductName(item));
                        System.out.println("Selling Price " + entry.getSellingPrice());
                        jsonRequest.append("{\"skuId\":"+"\""+flipkartItemsMap.get(entry.getItem_id()).getSkuAtFlipkart()+
                                        "\","+"\"attributeValues\""+":"+
                                        "{\"selling_price\""+":"+"\""+entry.getSellingPrice()+"}}");
                        
                        pricingItems.append("<tr>" 
                                        + "<td>" +getProductName(item)+"</td>"
                                        +"<td>" + item.getId() + "</td>"
                                        +"<td>" + entry.getSellingPrice() + "</td>"
                                        +"</tr>");
                        if(flipkartPricingItems.size()!=1 && (i!=flipkartPricingItems.size() -1 ) ){
                                jsonRequest.append(",");        
                        }
                        i++;
                        if(i%10==0 || i==flipkartPricingItems.size()){
                                StringEntity input = null;
                                try {
                                        input = new StringEntity(jsonStart+jsonRequest.toString()+jsonEnd);
                                        System.out.println("Json input " + jsonStart+jsonRequest.toString()+jsonEnd);
                                        jsonRequest = new StringBuffer();
                                } catch (UnsupportedEncodingException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                                input.setContentType("application/json");

                                httppost.setEntity(input);
                                HttpResponse response = null;
                                try {
                                        System.out.println("Before send " );
                                        response = httpclient.execute(httppost);
                                        System.out.println("After send " );
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                                BufferedReader rd = null;
                                try {
                                        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                                } catch (IllegalStateException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                                String line;
                                try {
                                        while ((line = rd.readLine()) != null) {
                                                System.out.println("Response " + line);
                                                if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
                                                }
                                                else{
                                                        feedResponse.append(line);
                                                }

                                        }
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                }
                System.out.println("Before sending email");
                String text = tableHeader+pricingItems.toString()+tableFooter;
                try {
                        mailer.sendSSLMessage(sendTo,"Flipkart Inventory Sent Details ", emailFromAddress, password, text);
                } catch (MessagingException e) {
                        e.printStackTrace();
                }
                if(feedResponse.toString().length()>0){
                        try {
                                mailer.sendSSLMessage(sendTo, "Flipkart Inventory could not be updated ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
                        } catch (MessagingException e) {
                                e.printStackTrace();
                        }
                }
                else{
                        try {
                                mailer.sendSSLMessage(sendTo, "Flipkart Inventory updated successfully ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
                        } catch (MessagingException e) {
                                e.printStackTrace();
                        }
                }
        }

        static String getProductName(Item item){
                return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 

        }
        static String getName(String name){
                if(name==null || name.length()==0){
                        return "";
                }
                else{
                        return name;
                }
        }
}