| Line 1... |
Line 1... |
| 1 |
package in.shop2020.support.controllers;
|
1 |
package in.shop2020.support.controllers;
|
| 2 |
|
2 |
|
| 3 |
import in.shop2020.model.v1.catalog.InventoryService.Client;
|
3 |
import in.shop2020.model.v1.inventory.InventoryService.Client;
|
| 4 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
4 |
import in.shop2020.model.v1.inventory.InventoryServiceException;
|
| 5 |
import in.shop2020.model.v1.catalog.Item;
|
5 |
import in.shop2020.model.v1.catalog.Item;
|
| 6 |
import in.shop2020.model.v1.catalog.ItemInventory;
|
6 |
import in.shop2020.model.v1.inventory.ItemInventory;
|
| 7 |
import in.shop2020.model.v1.catalog.VendorItemMapping;
|
7 |
import in.shop2020.model.v1.inventory.VendorItemMapping;
|
| 8 |
import in.shop2020.model.v1.catalog.Warehouse;
|
8 |
import in.shop2020.model.v1.inventory.Warehouse;
|
| 9 |
import in.shop2020.support.models.BillingUpdate;
|
9 |
import in.shop2020.support.models.BillingUpdate;
|
| 10 |
import in.shop2020.support.models.InventoryUpdate;
|
10 |
import in.shop2020.support.models.InventoryUpdate;
|
| 11 |
import in.shop2020.support.models.PLBDetails;
|
11 |
import in.shop2020.support.models.PLBDetails;
|
| 12 |
import in.shop2020.support.models.Update;
|
12 |
import in.shop2020.support.models.Update;
|
| 13 |
import in.shop2020.thrift.clients.CatalogClient;
|
13 |
import in.shop2020.thrift.clients.CatalogClient;
|
| - |
|
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.ConfigClientKeys;
|
16 |
import in.shop2020.utils.ConfigClientKeys;
|
| 16 |
import in.shop2020.utils.GmailUtils;
|
17 |
import in.shop2020.utils.GmailUtils;
|
| 17 |
|
18 |
|
| 18 |
import java.io.BufferedWriter;
|
19 |
import java.io.BufferedWriter;
|
| Line 39... |
Line 40... |
| 39 |
public class UpdatesController implements ModelDriven<InventoryUpdate>{
|
40 |
public class UpdatesController implements ModelDriven<InventoryUpdate>{
|
| 40 |
|
41 |
|
| 41 |
/**
|
42 |
/**
|
| 42 |
*
|
43 |
*
|
| 43 |
*/
|
44 |
*/
|
| - |
|
45 |
private static final int HOTSPOT_VENDOR_ID = 1;
|
| - |
|
46 |
|
| - |
|
47 |
/**
|
| - |
|
48 |
*
|
| - |
|
49 |
*/
|
| 44 |
private static final String FIELD_DELIMITER = "~#~#~#";
|
50 |
private static final String FIELD_DELIMITER = "~#~#~#";
|
| 45 |
|
51 |
|
| 46 |
private static Logger logger = LoggerFactory.getLogger(UpdatesController.class);
|
52 |
private static Logger logger = LoggerFactory.getLogger(UpdatesController.class);
|
| 47 |
|
53 |
|
| 48 |
private InventoryUpdate inventoryUpdate = new InventoryUpdate();
|
54 |
private InventoryUpdate inventoryUpdate = new InventoryUpdate();
|
| 49 |
private String timestampAndItemIds;
|
55 |
private String timestampAndItemIds;
|
| 50 |
|
56 |
|
| 51 |
private Map<Long, String> hotspotMappings = new HashMap<Long, String>();
|
57 |
private Map<Long, List<String>> hotspotMappings = new HashMap<Long, List<String>>();
|
| 52 |
|
58 |
|
| 53 |
private int errorCode = 0;
|
59 |
private int errorCode = 0;
|
| 54 |
private String errorMessage;
|
60 |
private String errorMessage;
|
| 55 |
private boolean fullPlbSync = false;
|
61 |
private boolean fullPlbSync = false;
|
| 56 |
private String id;
|
62 |
private String id;
|
| Line 70... |
Line 76... |
| 70 |
public HttpHeaders show(){
|
76 |
public HttpHeaders show(){
|
| 71 |
logger.info("Status requested for warehouse id: " + getId());
|
77 |
logger.info("Status requested for warehouse id: " + getId());
|
| 72 |
Client client;
|
78 |
Client client;
|
| 73 |
|
79 |
|
| 74 |
try {
|
80 |
try {
|
| 75 |
client = new CatalogClient().getClient();
|
81 |
client = new InventoryClient().getClient();
|
| 76 |
long warehouseId = Long.parseLong(getId());
|
82 |
long warehouseId = Long.parseLong(getId());
|
| 77 |
Warehouse warehouse = client.getWarehouse(warehouseId);
|
83 |
Warehouse warehouse = client.getWarehouse(warehouseId);
|
| 78 |
timestampAndItemIds = warehouse.getVendorString();
|
84 |
timestampAndItemIds = warehouse.getVendorString();
|
| 79 |
|
85 |
|
| 80 |
List<String> itemKeys = new ArrayList<String>();
|
86 |
List<String> itemKeys = new ArrayList<String>();
|
| 81 |
|
87 |
|
| 82 |
if (!fullPlbSync) {
|
88 |
if (!fullPlbSync) {
|
| 83 |
client = new CatalogClient().getClient();
|
89 |
client = new InventoryClient().getClient();
|
| 84 |
itemKeys = client.getItemKeysToBeProcessed(warehouseId);
|
90 |
itemKeys = client.getItemKeysToBeProcessed(warehouseId);
|
| 85 |
}
|
91 |
}
|
| 86 |
|
92 |
|
| 87 |
for (String itemKey : itemKeys) {
|
93 |
for (String itemKey : itemKeys) {
|
| 88 |
timestampAndItemIds += FIELD_DELIMITER + itemKey;
|
94 |
timestampAndItemIds += FIELD_DELIMITER + itemKey;
|
| Line 96... |
Line 102... |
| 96 |
/**
|
102 |
/**
|
| 97 |
* @return
|
103 |
* @return
|
| 98 |
*/
|
104 |
*/
|
| 99 |
private List<Item> fetchItems() {
|
105 |
private List<Item> fetchItems() {
|
| 100 |
try {
|
106 |
try {
|
| 101 |
Client snapshotClient = new CatalogClient().getClient();
|
107 |
in.shop2020.model.v1.catalog.CatalogService.Client snapshotClient = new CatalogClient().getClient();
|
| 102 |
return snapshotClient.getAllItems(true);
|
108 |
return snapshotClient.getAllItems(true);
|
| 103 |
} catch (Exception e) {
|
109 |
} catch (Exception e) {
|
| 104 |
logger.error("Could not fetch all items. Retrying.", e);
|
110 |
logger.error("Could not fetch all items. Retrying.", e);
|
| 105 |
try {
|
111 |
try {
|
| 106 |
Thread.sleep(30000 * Long.parseLong(inventoryUpdate.getwarehouseId()));
|
112 |
Thread.sleep(30000 * Long.parseLong(inventoryUpdate.getwarehouseId()));
|
| Line 119... |
Line 125... |
| 119 |
try {
|
125 |
try {
|
| 120 |
return snapshotClient.getItemInventoryByItemId(itemId);
|
126 |
return snapshotClient.getItemInventoryByItemId(itemId);
|
| 121 |
} catch (Exception e) {
|
127 |
} catch (Exception e) {
|
| 122 |
logger.error("Could not fetch all items. Retrying.", e);
|
128 |
logger.error("Could not fetch all items. Retrying.", e);
|
| 123 |
try {
|
129 |
try {
|
| 124 |
snapshotClient = new CatalogClient().getClient();
|
130 |
snapshotClient = new InventoryClient().getClient();
|
| 125 |
return fetchInventory(itemId);
|
131 |
return fetchInventory(itemId);
|
| 126 |
} catch (TTransportException e1) {
|
132 |
} catch (TTransportException e1) {
|
| 127 |
logger.error("Could not create client", e);
|
133 |
logger.error("Could not create client", e);
|
| 128 |
return null;
|
134 |
return null;
|
| 129 |
}
|
135 |
}
|
| Line 134... |
Line 140... |
| 134 |
*
|
140 |
*
|
| 135 |
*/
|
141 |
*/
|
| 136 |
private void loadHotspotMappings() {
|
142 |
private void loadHotspotMappings() {
|
| 137 |
try {
|
143 |
try {
|
| 138 |
for (VendorItemMapping mapping : snapshotClient.getAllVendorItemMappings()) {
|
144 |
for (VendorItemMapping mapping : snapshotClient.getAllVendorItemMappings()) {
|
| 139 |
if (mapping.getVendorId() == 1) {
|
145 |
if (mapping.getVendorId() == HOTSPOT_VENDOR_ID) {
|
| 140 |
hotspotMappings.put(mapping.getItemId(),
|
146 |
long itemId = mapping.getItemId();
|
| - |
|
147 |
if (!hotspotMappings.containsKey(itemId)) {
|
| - |
|
148 |
hotspotMappings.put(itemId, new ArrayList<String>());
|
| - |
|
149 |
}
|
| - |
|
150 |
|
| 141 |
mapping.getItemKey().trim().toLowerCase());
|
151 |
hotspotMappings.get(itemId).add(mapping.getItemKey().trim().toLowerCase());
|
| 142 |
}
|
152 |
}
|
| 143 |
}
|
153 |
}
|
| 144 |
} catch (Exception e) {
|
154 |
} catch (Exception e) {
|
| 145 |
logger.error("Could not load vendor item mappings. Trying to reload.", e);
|
155 |
logger.error("Could not load vendor item mappings. Trying to reload.", e);
|
| 146 |
|
156 |
|
| Line 149... |
Line 159... |
| 149 |
} catch (Exception e1) {
|
159 |
} catch (Exception e1) {
|
| 150 |
logger.error("Could not sleep.", e1);
|
160 |
logger.error("Could not sleep.", e1);
|
| 151 |
}
|
161 |
}
|
| 152 |
|
162 |
|
| 153 |
try {
|
163 |
try {
|
| 154 |
snapshotClient = new CatalogClient().getClient();
|
164 |
snapshotClient = new InventoryClient().getClient();
|
| 155 |
loadHotspotMappings();
|
165 |
loadHotspotMappings();
|
| 156 |
} catch (TTransportException e1) {
|
166 |
} catch (TTransportException e1) {
|
| 157 |
logger.error("Could not create client", e);
|
167 |
logger.error("Could not create client", e);
|
| 158 |
}
|
168 |
}
|
| 159 |
}
|
169 |
}
|
| Line 165... |
Line 175... |
| 165 |
*/
|
175 |
*/
|
| 166 |
public HttpHeaders create(){
|
176 |
public HttpHeaders create(){
|
| 167 |
logger.info(inventoryUpdate.toString());
|
177 |
logger.info(inventoryUpdate.toString());
|
| 168 |
try {
|
178 |
try {
|
| 169 |
//The snapshot client is used to update the inventory snapshot for production systems.
|
179 |
//The snapshot client is used to update the inventory snapshot for production systems.
|
| 170 |
CatalogClient snapshotCatalogServiceClient = new CatalogClient();
|
180 |
InventoryClient snapshotCatalogServiceClient = new InventoryClient();
|
| 171 |
Client snapshotClient = snapshotCatalogServiceClient.getClient();
|
181 |
Client snapshotClient = snapshotCatalogServiceClient.getClient();
|
| 172 |
|
182 |
|
| 173 |
//The history client is used to update the inventory history on the processing system.
|
183 |
//The history client is used to update the inventory history on the processing system.
|
| 174 |
CatalogClient historyCatalogServiceClient = new CatalogClient(
|
184 |
InventoryClient historyInventoryServiceClient = new InventoryClient(
|
| 175 |
ConfigClientKeys.inventory_history_service_server_host.toString(),
|
185 |
ConfigClientKeys.inventory_history_service_server_host.toString(),
|
| 176 |
ConfigClientKeys.inventory_history_service_server_port.toString());
|
186 |
ConfigClientKeys.inventory_history_service_server_port.toString());
|
| 177 |
Client historyClient = historyCatalogServiceClient.getClient();
|
187 |
Client historyClient = historyInventoryServiceClient.getClient();
|
| 178 |
|
188 |
|
| 179 |
Client stagingCatalogClient = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
|
189 |
Client stagingCatalogClient = new InventoryClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
|
| 180 |
ConfigClientKeys.catalog_service_server_port.toString()).getClient();
|
190 |
ConfigClientKeys.catalog_service_server_port.toString()).getClient();
|
| 181 |
|
191 |
|
| 182 |
//The snapshot client is used to update the inventory snapshot for production systems.
|
192 |
//The snapshot client is used to update the inventory snapshot for production systems.
|
| 183 |
TransactionClient transactionClient = new TransactionClient();
|
193 |
TransactionClient transactionClient = new TransactionClient();
|
| 184 |
in.shop2020.model.v1.order.TransactionService.Client tClient = transactionClient.getClient();
|
194 |
in.shop2020.model.v1.order.TransactionService.Client tClient = transactionClient.getClient();
|
| Line 242... |
Line 252... |
| 242 |
}
|
252 |
}
|
| 243 |
|
253 |
|
| 244 |
private void sendMailForPLBMismatches(Long warehouseId, Map<String, PLBDetails> currentInventory)
|
254 |
private void sendMailForPLBMismatches(Long warehouseId, Map<String, PLBDetails> currentInventory)
|
| 245 |
throws InventoryServiceException, TException, MessagingException {
|
255 |
throws InventoryServiceException, TException, MessagingException {
|
| 246 |
if (fullPlbSync) {
|
256 |
if (fullPlbSync) {
|
| 247 |
snapshotClient = new CatalogClient().getClient();
|
257 |
snapshotClient = new InventoryClient().getClient();
|
| 248 |
loadHotspotMappings();
|
258 |
loadHotspotMappings();
|
| 249 |
Map<Item, PLBDetails> mismatches = new HashMap<Item, PLBDetails>();
|
259 |
Map<Item, PLBDetails> mismatches = new HashMap<Item, PLBDetails>();
|
| 250 |
List<Item> items = fetchItems();
|
260 |
List<Item> items = fetchItems();
|
| 251 |
for (Item item : items) {
|
261 |
for (Item item : items) {
|
| 252 |
if (hotspotMappings.containsKey(item.getId())) {
|
262 |
if (hotspotMappings.containsKey(item.getId())) {
|
| - |
|
263 |
PLBDetails plbDetails = null;
|
| - |
|
264 |
for (String s : hotspotMappings.get(item.getId())) {
|
| - |
|
265 |
if (currentInventory.containsKey(s.trim().toLowerCase())) {
|
| 253 |
PLBDetails plbDetails = currentInventory.get(hotspotMappings.get(item.getId()).trim().toLowerCase());
|
266 |
plbDetails = currentInventory.get(s.trim().toLowerCase());
|
| - |
|
267 |
}
|
| - |
|
268 |
}
|
| - |
|
269 |
|
| 254 |
Long currentSnapshotAvailability = fetchInventory(item.getId()).getAvailability().get(warehouseId);
|
270 |
Long currentSnapshotAvailability = fetchInventory(item.getId()).getAvailability().get(warehouseId);
|
| 255 |
if ((currentSnapshotAvailability == null || currentSnapshotAvailability.equals(0l)) &&
|
271 |
if ((currentSnapshotAvailability == null || currentSnapshotAvailability.equals(0l)) &&
|
| 256 |
(plbDetails == null || plbDetails.getQuantity().longValue() == 0))
|
272 |
(plbDetails == null || plbDetails.getQuantity().longValue() == 0))
|
| 257 |
{
|
273 |
{
|
| 258 |
continue;
|
274 |
continue;
|