Rev 1962 | Rev 2027 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.catalog.dashboard.server;import in.shop2020.catalog.dashboard.client.CatalogService;import in.shop2020.catalog.dashboard.shared.Item;import in.shop2020.model.v1.catalog.InventoryService;import in.shop2020.model.v1.catalog.VendorItemPricing;import in.shop2020.thrift.clients.CatalogServiceClient;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;import com.google.gwt.core.client.GWT;import com.google.gwt.user.server.rpc.RemoteServiceServlet;@SuppressWarnings("serial")public class CatalogServiceImpl extends RemoteServiceServlet implements CatalogService {public List<Item> getAllItems(){List<Item> itemList = new ArrayList<Item>();try {CatalogServiceClient catalogServiceClient = new CatalogServiceClient();InventoryService.Client catalogClient = catalogServiceClient.getClient();List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItems(true);for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());itemList.add(getItemFromThriftItem(thriftItem, vip));}} catch (Exception e) {e.printStackTrace();}return itemList;}public List<Item> getBestDeals(){List<Item> itemList = new ArrayList<Item>();try {CatalogServiceClient catalogServiceClient = new CatalogServiceClient();InventoryService.Client catalogClient = catalogServiceClient.getClient();List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getBestDeals();for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());itemList.add(getItemFromThriftItem(thriftItem, vip));}} catch(Exception e){e.printStackTrace();}return itemList;}public List<Item> getBestSellers(){List<Item> itemList = new ArrayList<Item>();try {CatalogServiceClient catalogServiceClient = new CatalogServiceClient();InventoryService.Client catalogClient = catalogServiceClient.getClient();List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getBestSellers();for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());itemList.add(getItemFromThriftItem(thriftItem, vip));}} catch(Exception e){e.printStackTrace();}return itemList;}public List<Item> getLatestArrivals(){List<Item> itemList = new ArrayList<Item>();try {CatalogServiceClient catalogServiceClient = new CatalogServiceClient();InventoryService.Client catalogClient = catalogServiceClient.getClient();List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getLatestArrivals();for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());itemList.add(getItemFromThriftItem(thriftItem, vip));}} catch(Exception e){e.printStackTrace();}return itemList;}public Item getItem(long itemId){try{CatalogServiceClient catalogServiceClient = new CatalogServiceClient();InventoryService.Client catalogClient = catalogServiceClient.getClient();in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.getItem(itemId);List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());return getItemFromThriftItem(thriftItem, vip);}catch(Exception e){// Oops! We didn't receive the details. We should let the user know// that the catalog service is currently unavailable.e.printStackTrace();}return null;}public void reduceReservationCount(long itemId, long warehouseId, double quantity){GWT.log("Got a call to reduce the reservation count for item " + itemId + " and warehouse " + warehouseId);try{CatalogServiceClient catalogServiceClient = new CatalogServiceClient();InventoryService.Client catalogClient = catalogServiceClient.getClient();catalogClient.reduceReservationCount(itemId, warehouseId, quantity);}catch(Exception e){// TODO: Oops! We couldn't reduce the item reservation count. This will// result in underestimation of inventory stock. Should be corrected// periodically.e.printStackTrace();}}private Item getItemFromThriftItem(in.shop2020.model.v1.catalog.Item thriftItem, List<in.shop2020.model.v1.catalog.VendorItemPricing> thriftItemPricings){Map<Long, double[]> vendorPricings = new HashMap<Long, double[]>();double[] prices;if(thriftItemPricings != null) {for(VendorItemPricing v : thriftItemPricings) {prices = new double[3];prices[0] = v.getMop();prices[1] = v.getDealerPrice();prices[2] = v.getTransferPrice();vendorPricings.put(v.getVendorId(), prices);}}Item item = new Item(thriftItem.getId(),thriftItem.getProductGroup(),thriftItem.getBrand(),thriftItem.getModelNumber(),thriftItem.getModelName(),thriftItem.getColor(),thriftItem.getCategory(),thriftItem.getComments(),thriftItem.getCatalogItemId(),thriftItem.getFeatureId(),thriftItem.getFeatureDescription(),thriftItem.getMrp(),thriftItem.getMop(),thriftItem.getSellingPrice(),thriftItem.getDealerPrice(),thriftItem.getWeight(),thriftItem.getAddedOn(),thriftItem.getStartDate(),thriftItem.getRetireDate(),thriftItem.getUpdatedOn(),thriftItem.getItemStatus().name(),thriftItem.getOtherInfo(),thriftItem.getBestDealText(),thriftItem.getBestDealValue(),(thriftItem.getItemInventory() != null ? thriftItem.getItemInventory().getAvailability() : null),vendorPricings);return item;}@Overridepublic boolean updateItem(Item item) {GWT.log("Got a call to update item, Item Id: " + item.getId());try{CatalogServiceClient catalogServiceClient = new CatalogServiceClient();InventoryService.Client catalogClient = catalogServiceClient.getClient();in.shop2020.model.v1.catalog.Item tItem = catalogClient.getItem(item.getId());// tItem.setAddedOn(item.getAddedOn());// tItem.setBestDealText(item.getBestDealsText());// tItem.setBrand(item.getBrand());// tItem.setCatalogItemId(item.getCatalogItemId());// tItem.setCategory(item.getCategory());// tItem.setColor(item.getColor());// tItem.setComments(item.getComments());// tItem.setDealerPrice(item.getDealerPrice());// tItem.setFeatureId(item.getFeatureId());// tItem.setFeatureDescription(item.getFeatureDescription());// tItem.setModelName(item.getModelName());// tItem.setModelNumber(item.getModelNumber());tItem.setMrp(item.getMrp());// tItem.setMop(item.getMop());tItem.setSellingPrice(item.getSellingPrice());// tItem.setTransferPrice(item.getTransferPrice());tItem.setWeight(item.getWeight());VendorItemPricing vip = new VendorItemPricing();for(Entry<Long, double[]> vendorItemPricing : item.getVendorPricings().entrySet()) {vip.setItemId(item.getId());vip.setVendorId(vendorItemPricing.getKey());vip.setMop(vendorItemPricing.getValue()[Item.INDEX_MOP]);vip.setDealerPrice(vendorItemPricing.getValue()[Item.INDEX_DP]);vip.setTransferPrice(vendorItemPricing.getValue()[Item.INDEX_TP]);catalogClient.updateVendorItemPricing(vip);}if(catalogClient.updateItem(tItem) == item.getId());return true;//TODO: update left out fields of item also. Also update vendor prices and availability.}catch(Exception e){e.printStackTrace();}return false;}}