| 8616 |
vikram.rag |
1 |
package in.shop2020;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.logistics.DeliveryType;
|
|
|
4 |
import in.shop2020.logistics.LogisticsInfo;
|
|
|
5 |
import in.shop2020.logistics.LogisticsServiceException;
|
|
|
6 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
7 |
import in.shop2020.model.v1.catalog.CatalogService.Client;
|
|
|
8 |
import in.shop2020.model.v1.inventory.InventoryType;
|
|
|
9 |
import in.shop2020.model.v1.inventory.ItemInventory;
|
|
|
10 |
import in.shop2020.model.v1.inventory.Warehouse;
|
|
|
11 |
import in.shop2020.model.v1.inventory.WarehouseType;
|
|
|
12 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
13 |
import in.shop2020.thrift.clients.InventoryClient;
|
|
|
14 |
import in.shop2020.thrift.clients.LogisticsClient;
|
|
|
15 |
import in.shop2020.utils.GmailUtils;
|
|
|
16 |
|
|
|
17 |
import java.io.File;
|
|
|
18 |
import java.io.PrintWriter;
|
|
|
19 |
import java.io.StringWriter;
|
|
|
20 |
import java.text.DateFormat;
|
|
|
21 |
import java.text.SimpleDateFormat;
|
|
|
22 |
import java.util.ArrayList;
|
|
|
23 |
import java.util.Date;
|
|
|
24 |
import java.util.HashMap;
|
|
|
25 |
import java.util.List;
|
|
|
26 |
import java.util.Map;
|
|
|
27 |
import java.util.Random;
|
|
|
28 |
|
|
|
29 |
import javax.xml.bind.JAXBContext;
|
|
|
30 |
import javax.xml.bind.JAXBException;
|
|
|
31 |
import javax.xml.bind.Marshaller;
|
|
|
32 |
|
|
|
33 |
import org.apache.thrift.TException;
|
|
|
34 |
import org.apache.thrift.transport.TTransportException;
|
|
|
35 |
|
|
|
36 |
import utilities.ScpService;
|
|
|
37 |
import inventory.SaholicAPI;
|
|
|
38 |
import inventory.SaholicAPI.Inventory;;
|
|
|
39 |
|
|
|
40 |
public class CreateSnapDealInventoryFeed {
|
|
|
41 |
private static final String SNAPDEAL_INVENTORY_XML = "/home/snapdeal-inventory.xml";
|
|
|
42 |
|
|
|
43 |
public static void main(String... args){
|
|
|
44 |
CatalogClient catalogServiceClient = null;
|
|
|
45 |
Map<Long,Long> itemInventoryMap = new HashMap<Long,Long>();
|
|
|
46 |
try {
|
|
|
47 |
catalogServiceClient = new CatalogClient();
|
|
|
48 |
} catch (TTransportException e) {
|
|
|
49 |
// TODO Auto-generated catch block
|
|
|
50 |
e.printStackTrace();
|
|
|
51 |
}
|
|
|
52 |
Client catalogClient = catalogServiceClient.getClient();
|
|
|
53 |
try {
|
|
|
54 |
List<Item> items = catalogClient.getAllSnapdealListedActiveItems();
|
|
|
55 |
File file = new File(SNAPDEAL_INVENTORY_XML);
|
|
|
56 |
JAXBContext context;
|
|
|
57 |
Marshaller m=null;
|
|
|
58 |
try {
|
|
|
59 |
context = JAXBContext.newInstance(SaholicAPI.class);
|
|
|
60 |
m = context.createMarshaller();
|
|
|
61 |
} catch (JAXBException e1) {
|
|
|
62 |
e1.printStackTrace();
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
SaholicAPI saholicAPI = new SaholicAPI();
|
|
|
66 |
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
67 |
Date date = new Date();
|
|
|
68 |
Random rand = new Random();
|
|
|
69 |
saholicAPI.setCreateDate(dateFormat.format(date));
|
|
|
70 |
saholicAPI.setFeedIdentifier(Math.abs(rand.nextInt()));
|
|
|
71 |
List<Inventory> inventoryList = saholicAPI.getInventory();
|
|
|
72 |
InventoryClient inventoryServiceClient = null;
|
|
|
73 |
try {
|
|
|
74 |
inventoryServiceClient = new InventoryClient();
|
|
|
75 |
} catch (TTransportException e) {
|
|
|
76 |
// TODO Auto-generated catch block
|
|
|
77 |
e.printStackTrace();
|
|
|
78 |
}
|
|
|
79 |
in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
|
|
|
80 |
List<in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems> ignoreItems = inventoryClient.getIgnoredWarehouseidsAndItemids();
|
|
|
81 |
Map<Long, ItemInventory> availability = inventoryClient.getInventorySnapshot(0);
|
|
|
82 |
List<Warehouse> warehouses = inventoryClient.getWarehouses(WarehouseType.OURS_THIRDPARTY,null,0,0,0);
|
|
|
83 |
List<Warehouse> ours_warehouses = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, 0, 0, 0);
|
|
|
84 |
List<Long> thirdpartywarehouseids = new ArrayList<Long>();
|
|
|
85 |
List<Long> oursGoodWarehouse = new ArrayList<Long>();
|
|
|
86 |
for(Warehouse warehouse:warehouses){
|
|
|
87 |
thirdpartywarehouseids.add(warehouse.getId());
|
|
|
88 |
//System.out.println("Warehouse id is " + warehouse.getId());
|
|
|
89 |
}
|
|
|
90 |
for (Warehouse warehouse:ours_warehouses){
|
|
|
91 |
oursGoodWarehouse.add(warehouse.getId());
|
|
|
92 |
}
|
|
|
93 |
for(in.shop2020.model.v1.catalog.Item thriftItem:items){
|
|
|
94 |
long available=0;
|
|
|
95 |
long reserve=0;
|
|
|
96 |
List<Warehouse> vendor_warehouses=null;
|
|
|
97 |
if(availability.get(thriftItem.getId())!=null){
|
|
|
98 |
ItemInventory iteminventory = availability.get(thriftItem.getId());
|
|
|
99 |
Map<Long,Long> itemavailability = new HashMap<Long,Long>();
|
|
|
100 |
itemavailability = iteminventory.getAvailability();
|
|
|
101 |
if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
|
|
|
102 |
vendor_warehouses = inventoryClient.getWarehouses(WarehouseType.THIRD_PARTY,InventoryType.GOOD , thriftItem.getPreferredVendor(), 0, 0);
|
|
|
103 |
vendor_warehouses.addAll(ours_warehouses);
|
|
|
104 |
for (Warehouse warehouse:vendor_warehouses){
|
|
|
105 |
in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
|
|
|
106 |
ignoredItem.setItemId(thriftItem.getId());
|
|
|
107 |
ignoredItem.setWarehouseId(warehouse.getId());
|
|
|
108 |
if (itemavailability.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(ignoredItem))
|
|
|
109 |
available = available + itemavailability.get(warehouse.getId());
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
else{
|
|
|
113 |
for (Map.Entry<Long,Long> entry : itemavailability.entrySet()) {
|
|
|
114 |
in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
|
|
|
115 |
ignoredItem.setItemId(thriftItem.getId());
|
|
|
116 |
ignoredItem.setWarehouseId(entry.getKey());
|
|
|
117 |
if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
|
|
|
118 |
available = available + entry.getValue();
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
Map<Long,Long> itemreserve = new HashMap<Long,Long>();
|
|
|
123 |
itemreserve = iteminventory.getReserved();
|
|
|
124 |
if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
|
|
|
125 |
System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating Reserve)"+thriftItem.getPreferredVendor());
|
|
|
126 |
for (Warehouse warehouse:vendor_warehouses){
|
|
|
127 |
in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
|
|
|
128 |
ignoredItem.setItemId(thriftItem.getId());
|
|
|
129 |
ignoredItem.setWarehouseId(warehouse.getId());
|
|
|
130 |
if (itemreserve.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(warehouse.getId()))
|
|
|
131 |
reserve = reserve + itemreserve.get(warehouse.getId());
|
|
|
132 |
}
|
|
|
133 |
}else{
|
|
|
134 |
for (Map.Entry<Long,Long> entry : itemreserve.entrySet()) {
|
|
|
135 |
in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
|
|
|
136 |
ignoredItem.setItemId(thriftItem.getId());
|
|
|
137 |
ignoredItem.setWarehouseId(entry.getKey());
|
|
|
138 |
if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
|
|
|
139 |
reserve = reserve + entry.getValue();
|
|
|
140 |
}
|
|
|
141 |
else{
|
|
|
142 |
System.out.println("skipping inventory for warehouse id " + entry.getKey());
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
itemInventoryMap.put(thriftItem.getId(), available);
|
|
|
147 |
}
|
|
|
148 |
else{
|
|
|
149 |
itemInventoryMap.put(thriftItem.getId(),(long) 0);
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
if(!itemInventoryMap.isEmpty()){
|
|
|
153 |
in.shop2020.logistics.LogisticsService.Client logisticsClient=null;
|
|
|
154 |
LogisticsClient logisticsServiceClient = null;
|
|
|
155 |
try {
|
|
|
156 |
logisticsServiceClient = new LogisticsClient("logistics_service_prod_server_host","logistics_service_prod_server_port");
|
|
|
157 |
logisticsClient = logisticsServiceClient.getClient();
|
|
|
158 |
|
|
|
159 |
} catch (TTransportException e) {
|
|
|
160 |
// TODO Auto-generated catch block
|
|
|
161 |
e.printStackTrace();
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
for (Map.Entry<Long, Long> entry : itemInventoryMap.entrySet()) {
|
|
|
165 |
LogisticsInfo logisticinfo=new LogisticsInfo();
|
|
|
166 |
boolean ex=true;
|
|
|
167 |
int logisticCheck=0;
|
|
|
168 |
while(ex){
|
|
|
169 |
try {
|
|
|
170 |
System.out.println("Fetching Logistics estimation for Item Id " + " " + entry.getKey());
|
|
|
171 |
try {
|
|
|
172 |
logisticinfo = logisticsClient.getLogisticsEstimation(entry.getKey(),"110001",DeliveryType.COD);
|
|
|
173 |
} catch (LogisticsServiceException e) {
|
|
|
174 |
// TODO Auto-generated catch block
|
|
|
175 |
e.printStackTrace();
|
|
|
176 |
}
|
|
|
177 |
ex=false;
|
|
|
178 |
|
|
|
179 |
} catch (TTransportException e) {
|
|
|
180 |
// TODO Auto-generated catch lock
|
|
|
181 |
logisticsServiceClient = new LogisticsClient("logistics_service_prod_server_host","logistics_service_prod_server_port");
|
|
|
182 |
logisticsClient = logisticsServiceClient.getClient();
|
|
|
183 |
ex =true;
|
|
|
184 |
e.printStackTrace();
|
|
|
185 |
if (++logisticCheck==10){
|
|
|
186 |
StringWriter errors = new StringWriter();
|
|
|
187 |
e.printStackTrace(new PrintWriter(errors));
|
|
|
188 |
String text = errors.toString();
|
|
|
189 |
String[] sendTo = { "eng@shop2020.in" };
|
|
|
190 |
String emailSubjectTxt = "Unable To Send Non FBA Inventory Feed ";
|
|
|
191 |
String emailFromAddress = "build@shop2020.in";
|
|
|
192 |
String password = "cafe@nes";
|
|
|
193 |
GmailUtils mailer = new GmailUtils();
|
|
|
194 |
try {
|
|
|
195 |
mailer.sendSSLMessage(sendTo, emailSubjectTxt, text, emailFromAddress, password, new ArrayList<File>());
|
|
|
196 |
}
|
|
|
197 |
catch (Exception exc) {
|
|
|
198 |
exc.printStackTrace();
|
|
|
199 |
}
|
|
|
200 |
System.exit(1);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
Inventory inventory = new Inventory();
|
|
|
206 |
inventory.setSKU(entry.getKey());
|
|
|
207 |
inventory.setFulfillmentLatency(logisticinfo.getShippingTime());
|
|
|
208 |
if(entry.getValue() >=0){
|
|
|
209 |
inventory.setQuantity(entry.getValue());
|
|
|
210 |
}
|
|
|
211 |
else{
|
|
|
212 |
inventory.setQuantity((long) 0);
|
|
|
213 |
}
|
|
|
214 |
inventoryList.add(inventory);
|
|
|
215 |
}
|
|
|
216 |
try {
|
|
|
217 |
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
|
|
218 |
m.marshal(saholicAPI, file);
|
|
|
219 |
m.marshal(saholicAPI, System.out);
|
|
|
220 |
|
|
|
221 |
} catch (Exception e) {
|
|
|
222 |
// TODO Auto-generated catch block
|
|
|
223 |
e.printStackTrace();
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
else{
|
|
|
227 |
System.out.println("No Snapdeal listed Active items");
|
|
|
228 |
}
|
|
|
229 |
ScpService scpService = new ScpService();
|
| 8641 |
vikram.rag |
230 |
scpService.scpFile("/var/www/static.saholic.com/partners",SNAPDEAL_INVENTORY_XML);
|
| 8616 |
vikram.rag |
231 |
} catch (TException e) {
|
|
|
232 |
// TODO Auto-generated catch block
|
|
|
233 |
e.printStackTrace();
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
}
|