Rev 4987 | Rev 5212 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.controllers;import in.shop2020.model.v1.catalog.InventoryService.Client;import in.shop2020.model.v1.catalog.Warehouse;import in.shop2020.support.models.BillingUpdate;import in.shop2020.support.models.InventoryUpdate;import in.shop2020.support.models.PLBDetails;import in.shop2020.support.models.Update;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.thrift.clients.TransactionClient;import in.shop2020.utils.ConfigClientKeys;import in.shop2020.utils.GmailUtils;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.struts2.rest.DefaultHttpHeaders;import org.apache.struts2.rest.HttpHeaders;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.opensymphony.xwork2.ModelDriven;public class UpdatesController implements ModelDriven<InventoryUpdate>{/****/private static final String FIELD_DELIMITER = "~#~#~#";private static Logger logger = LoggerFactory.getLogger(UpdatesController.class);private InventoryUpdate inventoryUpdate = new InventoryUpdate();private String timestampAndItemIds;private int errorCode = 0;private String errorMessage;private String id;public UpdatesController(){}@Overridepublic InventoryUpdate getModel() {return this.inventoryUpdate;}public HttpHeaders show(){logger.info("Status requested for warehouse id: " + getId());CatalogClient catalogServiceClient;Client client;try {catalogServiceClient = new CatalogClient();client = catalogServiceClient.getClient();long warehouseId = Long.parseLong(getId());Warehouse warehouse = client.getWarehouse(warehouseId);timestampAndItemIds = warehouse.getVendorString();List<String> itemKeys = client.getItemKeysToBeProcessed(warehouseId);if (!itemKeys.isEmpty()) {for (String itemKey : itemKeys) {timestampAndItemIds += FIELD_DELIMITER + itemKey;}}} catch (Exception e) {logger.error("Error getting the warehouse or setting the timestamp", e);}return new DefaultHttpHeaders("lsuccess");}/*** Stores the inventory updates on the history and the production server.* @return*/public HttpHeaders create(){logger.info(inventoryUpdate.toString());try {//The snapshot client is used to update the inventory snapshot for production systems.CatalogClient snapshotCatalogServiceClient = new CatalogClient();Client snapshotClient = snapshotCatalogServiceClient.getClient();//The history client is used to update the inventory history on the processing system.CatalogClient historyCatalogServiceClient = new CatalogClient(ConfigClientKeys.inventory_history_service_server_host.toString(),ConfigClientKeys.inventory_history_service_server_port.toString());Client historyClient = historyCatalogServiceClient.getClient();//The snapshot client is used to update the inventory snapshot for production systems.TransactionClient transactionClient = new TransactionClient();in.shop2020.model.v1.order.TransactionService.Client tClient = transactionClient.getClient();Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());String timestamp = inventoryUpdate.getCurrentTimestamp();Map<String, Long> availability = new HashMap<String, Long>();List<Update> updates = inventoryUpdate.getUpdates();for(Update update: updates){String key = update.getKey();long quantity = (long) Double.parseDouble(update.getQuantity());if(availability.containsKey(key)){quantity = quantity + availability.get(key);}availability.put(key, quantity);}//Update the snapshot before the history since that is more important.snapshotClient.updateInventory(warehouseId, timestamp, availability);historyClient.updateInventoryHistory(warehouseId, timestamp, availability);List<BillingUpdate> billingUpdates = inventoryUpdate.getBillingUpdates();if(billingUpdates!=null){for(BillingUpdate billingUpdate: billingUpdates){tClient.addInvoiceNumber(Long.parseLong(billingUpdate.getOrderId()), billingUpdate.getInvoiceNumber(), billingUpdate.getColor());}}List<PLBDetails> plbDetails = inventoryUpdate.getPlbDetails();if (plbDetails != null) {for (PLBDetails plbDetail : plbDetails) {try {snapshotClient.resetAvailability(plbDetail.getItemKey(), 1,plbDetail.getQuantity().longValue(), warehouseId);snapshotClient.markMissedInventoryUpdatesAsProcessed(plbDetail.getItemKey(), warehouseId);} catch (Exception e) {logger.error("Could not reset availability of: " + plbDetail.getItemKey(), e);GmailUtils g = new GmailUtils();g.sendSSLMessage(new String[]{ "mandeep.dhir@shop2020.in" }, "Error resetting availability for " + plbDetail.getItemKey() + " in warehouseId: " + warehouseId + " to " + plbDetail.getQuantity(), "", "cnc.center@shop2020.in", "5hop2o2o", new ArrayList<File>());}}}} catch (Exception e) {logger.error("Unable to update inventory", e);}return new DefaultHttpHeaders("psuccess");}public int getErrorCode() {return errorCode;}public String getErrorMessage() {return errorMessage;}public String getId(){return id;}public void setId(String id){this.id = id;}public InventoryUpdate getInventoryUpdate() {return inventoryUpdate;}public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {this.inventoryUpdate = inventoryUpdate;}public void setTimestampAndItemIds(String timestampAndItemIds) {this.timestampAndItemIds = timestampAndItemIds;}public String getTimestampAndItemIds() {return timestampAndItemIds;}}