Subversion Repositories SmartDukaan

Rev

Rev 15223 | Rev 15634 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 15223 Rev 15602
Line 7... Line 7...
7
import in.shop2020.model.v1.inventory.ItemInventory;
7
import in.shop2020.model.v1.inventory.ItemInventory;
8
import in.shop2020.model.v1.inventory.SnapdealInventoryItem;
8
import in.shop2020.model.v1.inventory.SnapdealInventoryItem;
9
import in.shop2020.model.v1.inventory.Warehouse;
9
import in.shop2020.model.v1.inventory.Warehouse;
10
import in.shop2020.model.v1.inventory.WarehouseLocation;
10
import in.shop2020.model.v1.inventory.WarehouseLocation;
11
import in.shop2020.model.v1.inventory.WarehouseType;
11
import in.shop2020.model.v1.inventory.WarehouseType;
-
 
12
import in.shop2020.serving.services.SnapdealSessionCookie;
12
import in.shop2020.thrift.clients.CatalogClient;
13
import in.shop2020.thrift.clients.CatalogClient;
13
import in.shop2020.thrift.clients.InventoryClient;
14
import in.shop2020.thrift.clients.InventoryClient;
14
import in.shop2020.thrift.clients.TransactionClient;
15
import in.shop2020.thrift.clients.TransactionClient;
15
import in.shop2020.utils.GmailUtils;
16
import in.shop2020.utils.GmailUtils;
16
import inventory.Inventory;
17
import inventory.Inventory;
Line 52... Line 53...
52
import org.apache.poi.ss.usermodel.Row;
53
import org.apache.poi.ss.usermodel.Row;
53
import org.apache.poi.ss.usermodel.Sheet;
54
import org.apache.poi.ss.usermodel.Sheet;
54
import org.apache.poi.ss.usermodel.Workbook;
55
import org.apache.poi.ss.usermodel.Workbook;
55
import org.apache.thrift.TException;
56
import org.apache.thrift.TException;
56
import org.apache.thrift.transport.TTransportException;
57
import org.apache.thrift.transport.TTransportException;
-
 
58
import org.json.JSONException;
-
 
59
import org.json.JSONObject;
57
 
60
 
58
import com.google.gson.Gson;
61
import com.google.gson.Gson;
59
 
62
 
60
public class UpdateSDInventoryUsingPanelNew {
63
public class UpdateSDInventoryUsingPanelNew {
61
	private static final boolean PANEL = true;
64
	private static final boolean PANEL = true;
Line 74... Line 77...
74
	static String emailFromAddress;
77
	static String emailFromAddress;
75
	static String password;
78
	static String password;
76
	static GmailUtils mailer;
79
	static GmailUtils mailer;
77
	static String sendTo[];
80
	static String sendTo[];
78
	static StringBuffer notMappedItems=null;
81
	static StringBuffer notMappedItems=null;
-
 
82
	
-
 
83
	static Map<String, String> headers = new HashMap<String, String>();
-
 
84
	static {
-
 
85
		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");
-
 
86
		headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
-
 
87
		headers.put("Accept-Language", "en-US,en;q=0.8");
-
 
88
		headers.put("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
-
 
89
	}
79
 
90
 
80
	public static void initilaize() {
91
	public static void initilaize() {
81
		sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
92
		sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
82
		emailFromAddress = "build@shop2020.in";
93
		emailFromAddress = "build@shop2020.in";
83
		password = "cafe@nes";
94
		password = "cafe@nes";
Line 371... Line 382...
371
 
382
 
372
			e.printStackTrace();
383
			e.printStackTrace();
373
		}
384
		}
374
 
385
 
375
	}
386
	}
376
	private static void fetchinventoryhistoryfromsnapdeal() throws UnsupportedEncodingException, TException{
387
	private static void fetchinventoryhistoryfromsnapdeal() throws Exception{
377
		int i = 1;
388
		int i = 1;
378
		Gson gson = new Gson();
389
		Gson gson = new Gson();
379
		inventoryhistoryItemMap = new HashMap<String,InventoryHistory>();
390
		inventoryhistoryItemMap = new HashMap<String,InventoryHistory>();
380
		boolean exitfetchinghistory = true;
391
		boolean exitfetchinghistory = true;
381
		System.out.println("Fetching history inventory");
392
		System.out.println("Fetching history inventory");
382
		HttpGet get;
393
		HttpGet get;
383
		HttpResponse response = null;
394
		HttpResponse response = null;
384
		BufferedReader rd = null;
395
		BufferedReader rd = null;
-
 
396
		SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();
-
 
397
		String cookies = "";
-
 
398
		JSONObject cookieObject = null;
385
		while(exitfetchinghistory){
399
		while(exitfetchinghistory){
386
			System.out.println("Fetching inventory history page " +i);
400
			System.out.println("Fetching inventory history page " +i);
387
			get = new HttpGet("http://seller.snapdeal.com/inventory/search?gridType=history&_search=false&nd="+time+"&rows=30&page="+i+"&sidx=&sord=dsc");
401
			get = new HttpGet("http://seller.snapdeal.com/inventory/search?gridType=history&_search=false&nd="+time+"&rows=30&page="+i+"&sidx=&sord=dsc");
388
 
-
 
-
 
402
			cookies = sdSessionCookie.getCookies();
-
 
403
			cookieObject = new JSONObject(cookies);
-
 
404
			for(String key:headers.keySet())
-
 
405
				get.addHeader(key, headers.get(key));
-
 
406
			get.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");
389
			try {
407
			try {
390
				response = client.execute(get);
408
				response = client.execute(get);
391
			} catch (ClientProtocolException e) {
409
			} catch (ClientProtocolException e) {
392
 
410
 
393
				e.printStackTrace();
411
				e.printStackTrace();
Line 461... Line 479...
461
				PendingOrdersInventoryHistoryMap.put(inventoryHistoryItem.getValue().getSellerSku(),pendingOrdersInventoryHistory);
479
				PendingOrdersInventoryHistoryMap.put(inventoryHistoryItem.getValue().getSellerSku(),pendingOrdersInventoryHistory);
462
			}
480
			}
463
		}*/
481
		}*/
464
	}
482
	}
465
 
483
 
466
	private static void fetchcurrentinventoryfromsnapdeal() throws UnsupportedEncodingException, TException{
484
	private static void fetchcurrentinventoryfromsnapdeal() throws Exception{
467
		int i = 1;
485
		int i = 1;
468
		int items=0;
486
		int items=0;
469
		Gson gson = new Gson();
487
		Gson gson = new Gson();
470
		in.shop2020.model.v1.order.TransactionService.Client transactionClient = null;
488
		in.shop2020.model.v1.order.TransactionService.Client transactionClient = null;
471
		currentInventoryItemList = new ArrayList<Inventory>();
489
		currentInventoryItemList = new ArrayList<Inventory>();
472
		HttpGet get;
490
		HttpGet get;
473
		HttpResponse response = null;
491
		HttpResponse response = null;
474
		BufferedReader rd= null;
492
		BufferedReader rd= null;
475
		System.out.println("Fetching current inventory ");
493
		System.out.println("Fetching current inventory ");
476
		notMappedItems = new StringBuffer();
494
		notMappedItems = new StringBuffer();
-
 
495
		SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();
-
 
496
		String cookies = "";
-
 
497
		JSONObject cookieObject = null;
477
		while(true){
498
		while(true){
478
			//System.out.println("Fetching current inventory page " +i);
499
			//System.out.println("Fetching current inventory page " +i);
479
			get = new HttpGet("http://seller.snapdeal.com/inventory/search?gridType=normal&_search=false&nd="+time+"&rows="+30+"&page="+i+"&sidx=&sord=asc");
500
			get = new HttpGet("http://seller.snapdeal.com/inventory/search?gridType=normal&_search=false&nd="+time+"&rows="+30+"&page="+i+"&sidx=&sord=asc");
-
 
501
			cookies = sdSessionCookie.getCookies();
-
 
502
			cookieObject = new JSONObject(cookies);
-
 
503
			for(String key:headers.keySet())
-
 
504
				get.addHeader(key, headers.get(key));
-
 
505
			get.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");
480
			try {
506
			try {
481
				response = client.execute(get);
507
				response = client.execute(get);
482
			} catch (ClientProtocolException e) {
508
			} catch (ClientProtocolException e) {
483
 
509
 
484
				e.printStackTrace();
510
				e.printStackTrace();
Line 594... Line 620...
594
			}
620
			}
595
			itemIdpendingOrdersMap.put(sku, pending_orders);
621
			itemIdpendingOrdersMap.put(sku, pending_orders);
596
 
622
 
597
		}
623
		}
598
	}
624
	}
599
	
-
 
600
	public static void handleLogout(){
-
 
601
		HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/logout?service=http://seller.snapdeal.com/");
-
 
602
		HttpResponse response = null;
-
 
603
		try {
-
 
604
			response = client.execute(get);
-
 
605
		} catch (ClientProtocolException e) {
-
 
606
 
-
 
607
			e.printStackTrace();
-
 
608
		} catch (IOException e) {
-
 
609
 
-
 
610
			e.printStackTrace();
-
 
611
		}
-
 
612
		BufferedReader rd = null;
-
 
613
		try {
-
 
614
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
-
 
615
		} catch (IllegalStateException e1) {
-
 
616
 
-
 
617
			e1.printStackTrace();
-
 
618
		} catch (IOException e1) {
-
 
619
 
-
 
620
			e1.printStackTrace();
-
 
621
		}
-
 
622
		String line = "";
-
 
623
		try{
-
 
624
			while ((line = rd.readLine()) != null) {
-
 
625
				//System.out.println(line);
-
 
626
			}
-
 
627
		}catch (IOException e1) {
-
 
628
 
-
 
629
			e1.printStackTrace();
-
 
630
		}
-
 
631
	}
-
 
632
 
-
 
633
	public static void handleLogin() throws ClientProtocolException, IOException{
-
 
634
		HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
-
 
635
		HttpResponse response = null;
-
 
636
		try {
-
 
637
			response = client.execute(get);
-
 
638
		} catch (ClientProtocolException e) {
-
 
639
 
-
 
640
			e.printStackTrace();
-
 
641
		} catch (IOException e) {
-
 
642
 
-
 
643
			e.printStackTrace();
-
 
644
		}
-
 
645
		BufferedReader rd = null;
-
 
646
		try {
-
 
647
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
-
 
648
		} catch (IllegalStateException e1) {
-
 
649
 
-
 
650
			e1.printStackTrace();
-
 
651
		} catch (IOException e1) {
-
 
652
 
-
 
653
			e1.printStackTrace();
-
 
654
		}
-
 
655
		String line = "";
-
 
656
		StringBuffer sb = new StringBuffer();
-
 
657
		try {
-
 
658
			while ((line = rd.readLine()) != null) {
-
 
659
				sb.append(line);
-
 
660
				//System.out.println(line);
-
 
661
			}
-
 
662
		} catch (IOException e) {
-
 
663
 
-
 
664
			e.printStackTrace();
-
 
665
		}
-
 
666
		int i= sb.toString().indexOf("name=\"lt\" value=");
-
 
667
		char[] charArray = sb.toString().toCharArray();
-
 
668
		String lt = "";
-
 
669
		int j=0;
-
 
670
		for(j=i+16;j<=charArray.length;j++){
-
 
671
 
-
 
672
			if(charArray[j]==' '){
-
 
673
				break;
-
 
674
			}
-
 
675
		}
-
 
676
		lt = sb.substring(i+17,j-1);
-
 
677
		System.out.println("LT VALUE " + lt);
-
 
678
		i= sb.toString().indexOf("name=\"execution\" value=");
-
 
679
		charArray = sb.toString().toCharArray();
-
 
680
		String ex = "";
-
 
681
		j=0;
-
 
682
		for(j=i+24;j<=charArray.length;j++){
-
 
683
			if(charArray[j]==' '){
-
 
684
				break;
-
 
685
			}
-
 
686
		}
-
 
687
		ex = sb.substring(i+24,j-1);
-
 
688
		System.out.println("EXECUTION VALUE " + ex);
-
 
689
		HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
-
 
690
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
-
 
691
		//nameValuePairs.add(new BasicNameValuePair("username",
-
 
692
		//"khushal.bhatia@saholic.com"));
-
 
693
		nameValuePairs.add(new BasicNameValuePair("username",
-
 
694
		"saholic-snapdeal@saholic.com"));
-
 
695
		//nameValuePairs.add(new BasicNameValuePair("password",
-
 
696
		//"sonline"));
-
 
697
		nameValuePairs.add(new BasicNameValuePair("password",
-
 
698
		"saholic15"));
-
 
699
		nameValuePairs.add(new BasicNameValuePair("_eventId","submit"));
-
 
700
		nameValuePairs.add(new BasicNameValuePair("execution",ex));
-
 
701
		nameValuePairs.add(new BasicNameValuePair("lt",lt));
-
 
702
		//nameValuePairs.add(new BasicNameValuePair("submit","LOGIN"));
-
 
703
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
-
 
704
		response = client.execute(post);
-
 
705
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
-
 
706
		line = "";
-
 
707
		while ((line = rd.readLine()) != null) {
-
 
708
			System.out.println(line);
-
 
709
		}
-
 
710
		get = new HttpGet("http://seller.snapdeal.com/inventory/");
-
 
711
		response = client.execute(get);
-
 
712
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
-
 
713
		while ((line = rd.readLine()) != null) {
-
 
714
			//System.out.println(line);
-
 
715
		}
-
 
716
	}
-
 
717
 
-
 
718
 
-
 
719
 
-
 
720
 
625
 
721
	@SuppressWarnings("deprecation")
626
	@SuppressWarnings("deprecation")
722
	public static void sendsnapdealinventory (){
627
	public static void sendsnapdealinventory (){
723
		initilaize();
628
		initilaize();
724
		try {
629
		try {
725
			handleLogin();
-
 
726
		} catch (ClientProtocolException e2) {
-
 
727
			System.out.println("Handle login failed");
-
 
728
			e2.printStackTrace();
-
 
729
		} catch (IOException e2) {
-
 
730
			System.out.println("Handle login failed");
-
 
731
			e2.printStackTrace();
-
 
732
		}
-
 
733
		try {
-
 
734
			fetchinventoryhistoryfromsnapdeal();
630
			fetchinventoryhistoryfromsnapdeal();
735
		} catch (UnsupportedEncodingException e2) {
631
		} catch (UnsupportedEncodingException e2) {
736
			System.out.println("History Inventory Fetch failed");
632
			System.out.println("History Inventory Fetch failed");
737
			e2.printStackTrace();
633
			e2.printStackTrace();
738
		} catch (TException e) {
634
		} catch (TException e) {
739
			e.printStackTrace();
635
			e.printStackTrace();
-
 
636
		} catch (Exception e) {
-
 
637
			e.printStackTrace();
740
		}
638
		}
741
		try {
639
		try {
742
			fetchcurrentinventoryfromsnapdeal();
640
			fetchcurrentinventoryfromsnapdeal();
743
		} catch (UnsupportedEncodingException e2) {
641
		} catch (UnsupportedEncodingException e2) {
744
			System.out.println("Current Inventory Fetch failed");
642
			System.out.println("Current Inventory Fetch failed");
745
			e2.printStackTrace();
643
			e2.printStackTrace();
746
		} catch (TException e) {
644
		} catch (TException e) {
747
			e.printStackTrace();
645
			e.printStackTrace();
-
 
646
		} catch (Exception e) {
-
 
647
			e.printStackTrace();
748
		}
648
		}
749
		calculateinventory();
649
		calculateinventory();
750
		FileInputStream fis = null;
650
		FileInputStream fis = null;
751
		Workbook hwb = null;
651
		Workbook hwb = null;
752
		Sheet sheet = null;
652
		Sheet sheet = null;
-
 
653
		SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();
-
 
654
		String cookies = "";
-
 
655
		JSONObject cookieObject = null;
753
		if(!PANEL){
656
		if(!PANEL){
754
			try {
657
			try {
755
				fis = new FileInputStream("/root/code/trunk/SnapDealFeeds/SellerInventory.xls");
658
				fis = new FileInputStream("/root/code/trunk/SnapDealFeeds/SellerInventory.xls");
756
			} catch (FileNotFoundException e) {
659
			} catch (FileNotFoundException e) {
757
				System.out.println("Template File not found");
660
				System.out.println("Template File not found");
Line 898... Line 801...
898
					}
801
					}
899
					else{
802
					else{
900
						System.out.println("Incremental check success");
803
						System.out.println("Incremental check success");
901
						try {
804
						try {
902
							HttpPost post = new HttpPost("http://seller.snapdeal.com/inventory/update");
805
							HttpPost post = new HttpPost("http://seller.snapdeal.com/inventory/update");
-
 
806
							try {
-
 
807
								cookies = sdSessionCookie.getCookies();
-
 
808
							} catch (Exception e) {
-
 
809
								e.printStackTrace();
-
 
810
							}
-
 
811
							try {
-
 
812
								cookieObject = new JSONObject(cookies);
-
 
813
							} catch (JSONException e) {
-
 
814
								e.printStackTrace();
-
 
815
							}
-
 
816
							for(String key:headers.keySet())
-
 
817
								post.addHeader(key, headers.get(key));
-
 
818
							try {
-
 
819
								post.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");
-
 
820
							} catch (JSONException e) {
-
 
821
								e.printStackTrace();
-
 
822
							}
903
							List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
823
							List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
904
							System.out.println("Calling update inventory: Supc: "+inventory.getSupc()+ " to be update stock: "+ sent_inventory);
824
							System.out.println("Calling update inventory: Supc: "+inventory.getSupc()+ " to be update stock: "+ sent_inventory);
905
							nameValuePairs.add(new BasicNameValuePair("changedField",
825
							nameValuePairs.add(new BasicNameValuePair("changedField",
906
									"inventory"));
826
									"inventory"));
907
							nameValuePairs.add(new BasicNameValuePair("changedValue",
827
							nameValuePairs.add(new BasicNameValuePair("changedValue",
Line 1038... Line 958...
1038
				} catch (UnsupportedEncodingException e1) {
958
				} catch (UnsupportedEncodingException e1) {
1039
					System.out.println("Error while fetching inventory update result");
959
					System.out.println("Error while fetching inventory update result");
1040
					e1.printStackTrace();
960
					e1.printStackTrace();
1041
				} catch (TException e) {
961
				} catch (TException e) {
1042
					e.printStackTrace();
962
					e.printStackTrace();
-
 
963
				} catch (Exception e) {
-
 
964
					e.printStackTrace();
1043
				}
965
				}
1044
				if(inventoryhistoryItemMap.entrySet().size()==iterator -1){
966
				if(inventoryhistoryItemMap.entrySet().size()==iterator -1){
1045
					feedsuccess = true;
967
					feedsuccess = true;
1046
					break;
968
					break;
1047
				}
969
				}
Line 1162... Line 1084...
1162
				System.out.println("++++++++++++++++++++++++++++++");
1084
				System.out.println("++++++++++++++++++++++++++++++");
1163
				Thread.sleep(30*60*1000);
1085
				Thread.sleep(30*60*1000);
1164
			} catch (InterruptedException e) {
1086
			} catch (InterruptedException e) {
1165
				e.printStackTrace();
1087
				e.printStackTrace();
1166
			}
1088
			}
1167
			handleLogout();
-
 
1168
		}
1089
		}
1169
		else{
1090
		else{
1170
			try {
1091
			try {
1171
				mailer.sendSSLMessage(sendTo,"No changes in Snapdeal inventory to update "+ sdf.format(System.currentTimeMillis()),"No change in Snapdeal Inventory to be updated ", emailFromAddress, password, new ArrayList<File>());
1092
				mailer.sendSSLMessage(sendTo,"No changes in Snapdeal inventory to update "+ sdf.format(System.currentTimeMillis()),"No change in Snapdeal Inventory to be updated ", emailFromAddress, password, new ArrayList<File>());
1172
			} catch (Exception e) {
1093
			} catch (Exception e) {
Line 1178... Line 1099...
1178
				System.out.println("++++++++++++++++++++++++++++++");
1099
				System.out.println("++++++++++++++++++++++++++++++");
1179
				Thread.sleep(30*60*1000);
1100
				Thread.sleep(30*60*1000);
1180
			} catch (InterruptedException e) {
1101
			} catch (InterruptedException e) {
1181
				e.printStackTrace();
1102
				e.printStackTrace();
1182
			}
1103
			}
1183
			handleLogout();
-
 
1184
		}
1104
		}
1185
		String tableHeader = "<tr>" 
1105
		String tableHeader = "<tr>" 
1186
			+ "<td>" + "Product Name" +"</td>"
1106
			+ "<td>" + "Product Name" +"</td>"
1187
			+"<td>" + "SKU at Snapdeal" + "</td>"
1107
			+"<td>" + "SKU at Snapdeal" + "</td>"
1188
			+"<td>" + "SUPC" + "</td>"
1108
			+"<td>" + "SUPC" + "</td>"