| 490 |
rajveer |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import java.util.List;
|
|
|
4 |
import java.util.HashMap;
|
|
|
5 |
import java.util.Map;
|
|
|
6 |
|
|
|
7 |
import in.shop2020.model.v1.catalog.InventoryService.Client;
|
|
|
8 |
import in.shop2020.model.v1.catalog.Warehouse;
|
| 4585 |
rajveer |
9 |
import in.shop2020.support.models.BillingUpdate;
|
| 490 |
rajveer |
10 |
import in.shop2020.support.models.InventoryUpdate;
|
|
|
11 |
import in.shop2020.support.models.Update;
|
| 3125 |
rajveer |
12 |
import in.shop2020.thrift.clients.CatalogClient;
|
| 4585 |
rajveer |
13 |
import in.shop2020.thrift.clients.TransactionClient;
|
| 3326 |
chandransh |
14 |
import in.shop2020.utils.ConfigClientKeys;
|
| 490 |
rajveer |
15 |
|
|
|
16 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
17 |
import org.apache.struts2.rest.HttpHeaders;
|
| 3104 |
chandransh |
18 |
import org.slf4j.Logger;
|
|
|
19 |
import org.slf4j.LoggerFactory;
|
|
|
20 |
|
| 490 |
rajveer |
21 |
import com.opensymphony.xwork2.ModelDriven;
|
|
|
22 |
|
| 1367 |
chandransh |
23 |
public class UpdatesController implements ModelDriven<InventoryUpdate>{
|
| 490 |
rajveer |
24 |
|
| 3104 |
chandransh |
25 |
private static Logger logger = LoggerFactory.getLogger(UpdatesController.class);
|
|
|
26 |
|
| 490 |
rajveer |
27 |
private InventoryUpdate inventoryUpdate = new InventoryUpdate();
|
|
|
28 |
private String timestamp;
|
|
|
29 |
|
|
|
30 |
private int errorCode = 0;
|
|
|
31 |
private String errorMessage;
|
|
|
32 |
|
|
|
33 |
private String id;
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
public UpdatesController(){
|
|
|
37 |
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
@Override
|
| 1367 |
chandransh |
41 |
public InventoryUpdate getModel() {
|
| 490 |
rajveer |
42 |
return this.inventoryUpdate;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public HttpHeaders show(){
|
| 3104 |
chandransh |
46 |
logger.info("Status requested for warehouse id: " + getId());
|
| 3125 |
rajveer |
47 |
CatalogClient catalogServiceClient;
|
| 490 |
rajveer |
48 |
Client client;
|
|
|
49 |
|
|
|
50 |
try {
|
| 3125 |
rajveer |
51 |
catalogServiceClient = new CatalogClient();
|
| 490 |
rajveer |
52 |
client = catalogServiceClient.getClient();
|
|
|
53 |
long warehouseId = Long.parseLong(getId());
|
|
|
54 |
Warehouse warehouse = client.getWarehouse(warehouseId);
|
|
|
55 |
setTimestamp(warehouse.getVendorString());
|
|
|
56 |
} catch (Exception e) {
|
| 3104 |
chandransh |
57 |
logger.error("Error getting the warehouse or setting the timestamp", e);
|
| 490 |
rajveer |
58 |
}
|
|
|
59 |
return new DefaultHttpHeaders("lsuccess");
|
|
|
60 |
}
|
|
|
61 |
|
| 3326 |
chandransh |
62 |
/**
|
|
|
63 |
* Stores the inventory updates on the history and the production server.
|
|
|
64 |
* @return
|
|
|
65 |
*/
|
| 490 |
rajveer |
66 |
public HttpHeaders create(){
|
| 3104 |
chandransh |
67 |
logger.info(inventoryUpdate.toString());
|
| 490 |
rajveer |
68 |
try {
|
| 3326 |
chandransh |
69 |
//The snapshot client is used to update the inventory snapshot for production systems.
|
|
|
70 |
CatalogClient snapshotCatalogServiceClient = new CatalogClient();
|
|
|
71 |
Client snapshotClient = snapshotCatalogServiceClient.getClient();
|
|
|
72 |
|
|
|
73 |
//The history client is used to update the inventory history on the processing system.
|
|
|
74 |
CatalogClient historyCatalogServiceClient = new CatalogClient(
|
|
|
75 |
ConfigClientKeys.inventory_history_service_server_host.toString(),
|
|
|
76 |
ConfigClientKeys.inventory_history_service_server_port.toString());
|
|
|
77 |
Client historyClient = historyCatalogServiceClient.getClient();
|
| 4585 |
rajveer |
78 |
|
|
|
79 |
//The snapshot client is used to update the inventory snapshot for production systems.
|
|
|
80 |
TransactionClient transactionClient = new TransactionClient();
|
|
|
81 |
in.shop2020.model.v1.order.TransactionService.Client tClient = transactionClient.getClient();
|
|
|
82 |
|
| 490 |
rajveer |
83 |
Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());
|
|
|
84 |
String timestamp = inventoryUpdate.getCurrentTimestamp();
|
|
|
85 |
Map<String, Long> availability = new HashMap<String, Long>();
|
|
|
86 |
List<Update> updates = inventoryUpdate.getUpdates();
|
|
|
87 |
for(Update update: updates){
|
| 1367 |
chandransh |
88 |
String key = update.getKey();
|
| 490 |
rajveer |
89 |
long quantity = (long) Double.parseDouble(update.getQuantity());
|
| 616 |
chandransh |
90 |
if(availability.containsKey(key)){
|
|
|
91 |
quantity = quantity + availability.get(key);
|
| 490 |
rajveer |
92 |
}
|
| 616 |
chandransh |
93 |
availability.put(key, quantity);
|
| 490 |
rajveer |
94 |
}
|
| 4585 |
rajveer |
95 |
|
| 3326 |
chandransh |
96 |
//Update the snapshot before the history since that is more important.
|
|
|
97 |
snapshotClient.updateInventory(warehouseId, timestamp, availability);
|
|
|
98 |
historyClient.updateInventoryHistory(warehouseId, timestamp, availability);
|
| 4585 |
rajveer |
99 |
|
|
|
100 |
List<BillingUpdate> billingUpdates = inventoryUpdate.getBillingUpdates();
|
|
|
101 |
if(billingUpdates!=null){
|
|
|
102 |
for(BillingUpdate billingUpdate: billingUpdates){
|
|
|
103 |
tClient.addInvoiceNumber(Long.parseLong(billingUpdate.getOrderId()), billingUpdate.getInvoiceNumber());
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
|
| 490 |
rajveer |
107 |
} catch (Exception e) {
|
| 3104 |
chandransh |
108 |
logger.error("Unable to update inventory", e);
|
| 490 |
rajveer |
109 |
}
|
|
|
110 |
|
|
|
111 |
return new DefaultHttpHeaders("psuccess");
|
|
|
112 |
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
public int getErrorCode() {
|
|
|
116 |
return errorCode;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public String getErrorMessage() {
|
|
|
120 |
return errorMessage;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
public String getId(){
|
|
|
124 |
return id;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
public void setId(String id){
|
|
|
128 |
this.id = id;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
public InventoryUpdate getInventoryUpdate() {
|
|
|
132 |
return inventoryUpdate;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
|
|
|
136 |
this.inventoryUpdate = inventoryUpdate;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
public void setTimestamp(String timestamp) {
|
|
|
140 |
this.timestamp = timestamp;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
public String getTimestamp() {
|
|
|
144 |
return timestamp;
|
| 616 |
chandransh |
145 |
}
|
| 490 |
rajveer |
146 |
}
|