Subversion Repositories SmartDukaan

Rev

Rev 15022 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.support.utils;

import in.shop2020.model.v1.catalog.CatalogService.Client;
import in.shop2020.model.v1.catalog.CatalogServiceException;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.order.OrderSource;
import in.shop2020.serving.services.SnapdealSessionCookie;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.utils.GmailUtils;

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

import javax.mail.MessagingException;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UpdateSnapdealVoiPricingUsingPanel extends Thread{
        static ClientConnectionManager connManager = new PoolingClientConnectionManager();
        private static DefaultHttpClient httpClient = new DefaultHttpClient(connManager);
        //private static DefaultHttpClient httpClient = new DefaultHttpClient();
        private Float price;
        private String supc;
        private Item item;
        private Long timestamp;
        //public String[] sendTo = new String[]{"vikram.raghav@shop2020.in"};
        private static Logger logger = LoggerFactory.getLogger(UpdateSDPricingUsingPanel.class);
        public String[] sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "manish.sharma@shop2020.in", "rajneesh.arora@shop2020.in",
                        "khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
                        "yukti.jain@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","kshitij.sood@shop2020.in"};
        public String emailFromAddress = "build@shop2020.in";
        public String password = "cafe@nes";
        private static Map<String, String> headers = new HashMap<String, String>();
        static {
                headers.put("User-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");
                headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                headers.put("Accept-Language", "en-US,en;q=0.8");
                headers.put("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
        }
        public UpdateSnapdealVoiPricingUsingPanel(Float price,String supc,Item item,Long timestamp){
                logger.info("Calling Update Snapdeal Price VOI Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());
                this.price = price;
                this.supc = supc;
                this.item = item;
                this.timestamp = timestamp; 
        }
        
        public UpdateSnapdealVoiPricingUsingPanel() {
        }
        public static void main(String... args) throws ClientProtocolException, IOException, TTransportException, CatalogServiceException, TException{
                Item item = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient().getItem(2231);
                UpdateSnapdealVoiPricingUsingPanel updatePriceOnSnapdeal = new UpdateSnapdealVoiPricingUsingPanel(22200f,"1108903",item,System.currentTimeMillis());
                logger.info("Calling Thread to update price at snapdeal");
                updatePriceOnSnapdeal.start();  

        }
        int updatePricing(Float price,String supc,Item item,Long timestamp) throws Exception{
                logger.info("Calling Update Snapdeal VOI Price Constructor --" + " Price :" +price + " supc :"+supc +" Item ID:" +item.getId());
                SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();
                String cookies = "";
                JSONObject cookieObject = null;
                long priceVal = (long)price.floatValue();
                HttpPost post = new HttpPost("http://seller.snapdeal.com/pricing/update");
                cookies = sdSessionCookie.getVoiCookies();
                cookieObject = new JSONObject(cookies);
                for(String key:headers.keySet())
                        post.addHeader(key, headers.get(key));
                post.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("changedField",
                "sellingPrice"));
                nameValuePairs.add(new BasicNameValuePair("changedValue",
                                priceVal+""));
                nameValuePairs.add(new BasicNameValuePair("supc",
                                supc));
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
                HttpResponse response = httpClient.execute(post);
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String line = "";
                while ((line = rd.readLine()) != null) {
                        logger.info(line);
                        System.out.println(line);
                }
                return 1;
        }

        public void run()
        {
                try {
                        this.updatePricing(this.price,this.supc,this.item,timestamp);
                        boolean retry =true;
                        SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();
                        String cookies = "";
                        JSONObject cookieObject = null;
                        while(retry){
                                Thread.sleep(2*60*1000);
                                HttpGet get = new HttpGet("http://seller.snapdeal.com/pricing/search?searchType=SUPC&searchValue="+this.supc+"&gridType=normal&_search=false&nd="+System.currentTimeMillis()+"&rows=30&page=1&sidx=&sord=asc");
                                cookies = sdSessionCookie.getVoiCookies();
                                cookieObject = new JSONObject(cookies);
                                for(String key:headers.keySet())
                                        get.addHeader(key, headers.get(key));
                                get.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");
                                HttpResponse response = httpClient.execute(get);
                                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                                String line = "";
                                while ((line = rd.readLine()) != null) {
                                        logger.info(line);
                                        if(line.contains("\"pendingUpdate\":false")){
                                                retry =false;
                                        }
                                }
                        }
                        GmailUtils mailer = new GmailUtils();
                        String text = "Product : " +getProductName(this.item) +"\n"+ 
                        "Item ID : " +this.item.getId() +"\n"+
                        "SUPC : " +this.supc +"\n"+
                        "Updated Price : " +this.price;
                        try{
                                mailer.sendSSLMessage(sendTo, "Price updated on Snapdeal  VOI( Item ID " + this.item.getId()+ " )",text, emailFromAddress , password,new ArrayList<File>());
                        }
                        catch(Exception e){
                                logger.info("Exception"+e);
                        }
                        ArrayList<Long> updateList = new ArrayList<Long>();
                        try {
                                Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();

                                updateList.add(this.item.getId());
                                catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,OrderSource.SNAPDEAL.getValue());
                        } catch (TException e) {
                                try {
                                        new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,OrderSource.SNAPDEAL.getValue());
                                } catch (TTransportException e1) {
                                        e1.printStackTrace();
                                        logger.info("Exception" + e1);
                                } catch (TException e1) {
                                        e1.printStackTrace();
                                        logger.info("Exception" + e1);
                                }
                                logger.info("Exception" + e);
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                        //logger.error("Exception" + e);
                        GmailUtils mailer = new GmailUtils();
                        String text = "Product       : " +getProductName(this.item) +"\n"+ 
                        "Item ID       : " +this.item.getId() +"\n"+
                        "SUPC          : " +this.supc +"\n"+
                        "Updated Price : " +this.price;
                        try {
                                mailer.sendSSLMessage(sendTo, "Failed to update Price on Snapdeal VOI ( Item ID " + this.item.getId()+" )",text, emailFromAddress , password,new ArrayList<File>());
                        } catch (MessagingException e1) {
                                e1.printStackTrace();
                                logger.info("Exception" + e1);
                        }

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

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


}