Subversion Repositories SmartDukaan

Rev

Rev 10225 | Rev 10230 | Go to most recent revision | 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.Item;
import in.shop2020.support.controllers.SnapdealListController;
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.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

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.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UpdateSDPricingUsingPanel extends Thread{
        private static DefaultHttpClient client = 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", "vikram.raghav@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"};
        */public String emailFromAddress = "build@shop2020.in";
        public String password = "cafe@nes";
        public UpdateSDPricingUsingPanel(Float price,String supc,Item item,Long timestamp){
                logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());
                this.price = price;
                this.supc = supc;
                this.item = item;
                this.timestamp = timestamp; 
        }
        public UpdateSDPricingUsingPanel() {
        }
        public static void main(String... args) throws ClientProtocolException, IOException{
                UpdateSDPricingUsingPanel UDSI = new UpdateSDPricingUsingPanel();
                //UDSI.updatePricing(10131f,"SDL668125910","");

        }
        int updatePricing(Float price,String supc,Item item,Long timestamp) throws ClientProtocolException, IOException{
                logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());
                HttpPost post = new HttpPost("http://seller.snapdeal.com/pricing/update");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("sellingPrice",
                                String.valueOf(price)));
                nameValuePairs.add(new BasicNameValuePair("id",
                                supc));
                nameValuePairs.add(new BasicNameValuePair("oper","edit"));
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
                HttpResponse response = client.execute(post);
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String line = "";
                while ((line = rd.readLine()) != null) {
                        System.out.println(line);
                }
                return 1;
        }

        public void handleLogin() throws ClientProtocolException, IOException{
                logger.info("----Inside Handle Login ----" );
                HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/keymaker/login");
                HttpResponse response = null;
                try {
                        response = client.execute(get);
                } catch (ClientProtocolException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                BufferedReader rd = null;
                try {
                        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                } catch (IllegalStateException e1) {
                        e1.printStackTrace();
                } catch (IOException e1) {
                        e1.printStackTrace();
                }
                String line = "";
                StringBuffer sb = new StringBuffer();
                try {
                        while ((line = rd.readLine()) != null) {
                                sb.append(line);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }
                int i= sb.toString().indexOf("name=\"lt\" value=");
                char[] charArray = sb.toString().toCharArray();
                String lt = "";
                int j=0;
                for(j=i+16;j<=charArray.length;j++){

                        if(charArray[j]==' '){
                                break;
                        }
                }
                lt = sb.substring(i+17,j-1);
                System.out.println("LT VALUE " + lt);
                i= sb.toString().indexOf("name=\"execution\" value=");
                charArray = sb.toString().toCharArray();
                String ex = "";
                j=0;
                for(j=i+24;j<=charArray.length;j++){
                        if(charArray[j]==' '){
                                break;
                        }
                }
                ex = sb.substring(i+24,j-1);
                System.out.println("EXECUTION VALUE " + ex);
                HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                //nameValuePairs.add(new BasicNameValuePair("username",
                //"khushal.bhatia@saholic.com"));
                nameValuePairs.add(new BasicNameValuePair("username",
                "saholic-snapdeal@saholic.com"));
                //nameValuePairs.add(new BasicNameValuePair("password",
                //"sonline"));
                nameValuePairs.add(new BasicNameValuePair("password",
                "bc452ce4"));
                nameValuePairs.add(new BasicNameValuePair("_eventId","submit"));
                nameValuePairs.add(new BasicNameValuePair("execution",ex));
                nameValuePairs.add(new BasicNameValuePair("lt",lt));
                nameValuePairs.add(new BasicNameValuePair("submit","LOGIN"));
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
                response = client.execute(post);
                rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                line = "";
                while ((line = rd.readLine()) != null) {
                        System.out.println(line);
                }
                get = new HttpGet("http://seller.snapdeal.com/pricing/");
                response = client.execute(get);
                rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                while ((line = rd.readLine()) != null) {
                        //System.out.println(line);
                }
                logger.info("Login Successful");
        }
        public void run()
    {
                try {
                        this.handleLogin();
                        this.updatePricing(this.price,this.supc,this.item,timestamp);
                        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;
                        mailer.sendSSLMessage(sendTo, "Price updated on Snapdeal Item ID " + this.item.getId(),text, emailFromAddress , password,new ArrayList<File>());
                        ArrayList<Long> updateList = new ArrayList<Long>();
                        try {
                                Client catalogClient = new CatalogClient().getClient();
                                
                                updateList.add(this.item.getId());
                                catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,7L);
                        } catch (TException e) {
                                try {
                                        new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,8);
                                } catch (TTransportException e1) {
                                        e1.printStackTrace();
                                } catch (TException e1) {
                                        e1.printStackTrace();
                                }
                                e.printStackTrace();
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                        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 Item ID " + this.item.getId(),text, emailFromAddress , password,new ArrayList<File>());
                        } catch (MessagingException e1) {
                                e1.printStackTrace();
                        }
                        
                } 
    }
        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;
                }
        }


}