Rev 2068 | Rev 2119 | 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.catalog.dashboard.shared.ItemsComparator;import in.shop2020.catalog.dashboard.shared.VendorDetails;import in.shop2020.model.v1.catalog.InventoryService;import in.shop2020.model.v1.catalog.VendorItemMapping;import in.shop2020.model.v1.catalog.VendorItemPricing;import in.shop2020.model.v1.catalog.status;import in.shop2020.thrift.clients.CatalogServiceClient;import java.util.ArrayList;import java.util.Calendar;import java.util.Collections;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, null, null));}} catch (Exception e) {e.printStackTrace();}Collections.sort(itemList, new ItemsComparator());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, null, null));}} catch(Exception e){e.printStackTrace();}Collections.sort(itemList, new ItemsComparator());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, null, null));}} catch(Exception e){e.printStackTrace();}Collections.sort(itemList, new ItemsComparator());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, null, null));}} catch(Exception e){e.printStackTrace();}Collections.sort(itemList, new ItemsComparator());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());//List<in.shop2020.model.v1.catalog.VendorItemMapping> vim = catalogClient.getVendorItemMappings(thriftItem.getId());return getItemFromThriftItem(thriftItem, vip, null);}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> tVendorPricings,List<in.shop2020.model.v1.catalog.VendorItemMapping> tVendorMappings){Map<Long, VendorDetails> vendorDetailsMap = new HashMap<Long, VendorDetails>();VendorDetails vDetails;if(tVendorMappings != null) {for(VendorItemMapping vim : tVendorMappings) {vDetails = vendorDetailsMap.get(vim.getVendorId());if(vDetails == null) {vDetails = new VendorDetails();vDetails.setVendorId(vim.getVendorId());}vDetails.setVendorId(vim.getVendorId());vDetails.setItemKey(vim.getItemKey());vendorDetailsMap.put(vDetails.getVendorId(), vDetails);}}if(tVendorPricings != null) {for(VendorItemPricing vip : tVendorPricings) {vDetails = vendorDetailsMap.get(vip.getVendorId());if(vDetails == null) {vDetails = new VendorDetails();vDetails.setVendorId(vip.getVendorId());}vDetails.setMop(vip.getMop());vDetails.setDealerPrice(vip.getDealerPrice());vDetails.setTransferPrice(vip.getTransferPrice());vendorDetailsMap.put(vDetails.getVendorId(), vDetails);}}Item item = new Item(thriftItem.getId(),thriftItem.getProductGroup(),thriftItem.getBrand(),thriftItem.getModelNumber(),thriftItem.getModelName(),thriftItem.getColor(),thriftItem.getCategory(),//CategoryManager.getCategoryManager().getCategoryLabel(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.getBestSellingRank(),thriftItem.isDefaultForEntity(),(thriftItem.getItemInventory() != null ? thriftItem.getItemInventory().getAvailability() : null),vendorDetailsMap);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.setProductGroup(item.getProductGroup());tItem.setBrand(item.getBrand());tItem.setModelName(item.getModelName());tItem.setModelNumber(item.getModelNumber());tItem.setColor(item.getColor());tItem.setComments(item.getComments());tItem.setMrp(item.getMrp());tItem.setSellingPrice(item.getSellingPrice());tItem.setWeight(item.getWeight());tItem.setMop(item.getMop());tItem.setDealerPrice(item.getDealerPrice());tItem.setTransferPrice(item.getTransferPrice());tItem.setBestDealText(item.getBestDealsText());tItem.setBestDealValue(item.getBestDealsValue());tItem.setBestSellingRank(item.getBestSellingRank());tItem.setDefaultForEntity(item.isDefaultForEntity());tItem.setStartDate(item.getStartDate());// tItem.setAddedOn(item.getAddedOn());// tItem.setCatalogItemId(item.getCatalogItemId());// tItem.setCategory(item.getCategory());// tItem.setFeatureId(item.getFeatureId());// tItem.setFeatureDescription(item.getFeatureDescription());long rItemId;if((rItemId = catalogClient.updateItem(tItem)) != item.getId()) {GWT.log("Error updating item, returned Item Id: " + rItemId);return false;}GWT.log("Item updated successfully, Item Id: " + item.getId());Map<Long, VendorDetails> vendorDetails = item.getVendorDetails();if(vendorDetails != null && !vendorDetails.isEmpty()) {in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;for(VendorDetails v : vendorDetails.values()) {tVendorPricing = new VendorItemPricing();tVendorPricing.setVendorId(v.getVendorId());tVendorPricing.setItemId(item.getId());tVendorPricing.setMop(v.getMop());tVendorPricing.setTransferPrice(v.getTransferPrice());tVendorPricing.setDealerPrice(v.getDealerPrice());catalogClient.addVendorItemPricing(tVendorPricing);GWT.log("VendorItemPricing updated. " + tVendorPricing.getVendorId() + ", " + tVendorPricing.getItemId() + ", " +tVendorPricing.getMop() + ", " + tVendorPricing.getTransferPrice() + ", " + tVendorPricing.getDealerPrice());/*tVendorMapping = new VendorItemMapping();tVendorMapping.setVendorId(v.getVendorId());tVendorMapping.setItemKey(v.getItemKey());tVendorMapping.setItemId(item.getId());tVendorMapping.setVendorCategory(item.getVendorCategory());catalogClient.addVendorItemMapping(tVendorMapping);GWT.log("VendorItemMapping updated. " + tVendorMapping.getVendorId() + ", " + tVendorMapping.getItemId() + ", " +tVendorMapping.getItemKey() + ", " + tVendorMapping.getVendorCategory());*/}}}catch(Exception e){e.printStackTrace();}return true;/*List<VendorItemPricing> tVendorPrices = tItem.getVendorPrices();if(tVendorPrices == null) {tVendorPrices = new ArrayList<VendorItemPricing>();}for(Entry<Long, double[]> vendorItemPricing : item.getVendorPricings().entrySet()) {VendorItemPricing vip = null;for(VendorItemPricing v : tVendorPrices) {if(v.getVendorId() == vendorItemPricing.getKey()) {vip = v;break;}}if(vip == null) {vip = new VendorItemPricing();tVendorPrices.add(vip);}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);}*/}@Overridepublic Map<Long, String> getAllVendors() {Map<Long, String> vendorMap = new HashMap<Long, String>();try {CatalogServiceClient catalogServiceClient = new CatalogServiceClient();InventoryService.Client catalogClient = catalogServiceClient.getClient();List<in.shop2020.model.v1.catalog.Vendor> vendors = catalogClient.getAllVendors();for(in.shop2020.model.v1.catalog.Vendor v : vendors) {vendorMap.put(v.getId(), v.getName());}} catch (Exception e) {e.printStackTrace();}return vendorMap;}@Overridepublic Map<Long, String> getAllWarehouses() {Map<Long, String> warehouseMap = new HashMap<Long, String>();try {CatalogServiceClient catalogServiceClient = new CatalogServiceClient();InventoryService.Client catalogClient = catalogServiceClient.getClient();List<in.shop2020.model.v1.catalog.Warehouse> warehouses = catalogClient.getAllWarehouses(true);for(in.shop2020.model.v1.catalog.Warehouse w : warehouses) {warehouseMap.put(w.getId(), w.getDisplayName());}} catch (Exception e) {e.printStackTrace();}return warehouseMap;}@Overridepublic void phaseoutItem(long itemId) {try {CatalogServiceClient catalogServiceClient = new CatalogServiceClient();InventoryService.Client catalogClient = catalogServiceClient.getClient();catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PHASED_OUT);} catch (Exception e) {e.printStackTrace();}}@Overridepublic void activateItem(long itemId) {try {CatalogServiceClient catalogServiceClient = new CatalogServiceClient();InventoryService.Client catalogClient = catalogServiceClient.getClient();catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.ACTIVE);} catch (Exception e) {e.printStackTrace();}}@Overridepublic long addItem(Item item) {long itemId = 0;try {CatalogServiceClient catalogServiceClient = new CatalogServiceClient();InventoryService.Client catalogClient = catalogServiceClient.getClient();in.shop2020.model.v1.catalog.Item tItem = new in.shop2020.model.v1.catalog.Item();tItem.setProductGroup(item.getProductGroup());tItem.setBrand(item.getBrand());tItem.setModelNumber(item.getModelNumber());tItem.setModelName(item.getModelName());tItem.setColor(item.getColor());tItem.setComments(item.getComments());tItem.setMrp(item.getMrp());tItem.setSellingPrice(item.getSellingPrice());tItem.setWeight(item.getWeight());tItem.setAddedOn(Calendar.getInstance().getTimeInMillis());tItem.setUpdatedOn(Calendar.getInstance().getTimeInMillis());tItem.setRetireDate(item.getRetireDate());tItem.setStartDate(item.getStartDate());tItem.setBestDealText(item.getBestDealsText());tItem.setBestDealValue(item.getBestDealsValue());tItem.setBestSellingRank(item.getBestSellingRank());tItem.setDefaultForEntity(item.isDefaultForEntity());tItem.setHotspotCategory(item.getVendorCategory());itemId = catalogClient.addItem(tItem);Map<Long, VendorDetails> vendorDetails = item.getVendorDetails();if(vendorDetails != null && !vendorDetails.isEmpty()) {in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;for(VendorDetails v : vendorDetails.values()) {tVendorMapping = new VendorItemMapping();tVendorMapping.setVendorId(v.getVendorId());tVendorMapping.setItemKey(v.getItemKey());tVendorMapping.setItemId(itemId);tVendorMapping.setVendorCategory(item.getVendorCategory());catalogClient.addVendorItemMapping(tVendorMapping);/*tVendorPricing = new VendorItemPricing();tVendorPricing.setVendorId(v.getVendorId());tVendorPricing.setItemId(itemId);tVendorPricing.setMop(v.getMop());tVendorPricing.setTransferPrice(v.getTransferPrice());tVendorPricing.setDealerPrice(v.getDealerPrice());catalogClient.addVendorItemPricing(tVendorPricing);*/}}/*Map<Long, double[]> vendors = item.getVendorPricings();List<VendorItemPricing> vPricesList;if(vendors != null && !vendors.isEmpty()) {vPricesList = new ArrayList<VendorItemPricing>();VendorItemPricing vendorPrices = null;for(Entry<Long, double[]> v : vendors.entrySet()) {vendorPrices = new VendorItemPricing(v.getKey(), item.getId(),v.getValue()[Item.INDEX_TP], v.getValue()[Item.INDEX_MOP], v.getValue()[Item.INDEX_DP]);vPricesList.add(vendorPrices);}boolean itemExists = catalogClient.itemExists(item.getProductGroup(), item.getHbrand(), item.getHmodelNumber(),item.getHcolor(), vendorPrices.getVendorId(), item.getVendorCategory());if(itemExists) {return itemId;}} else {return 0;}catalogClient.addVendorItemPricing(vendorItemPricing)*/} catch (Exception e) {e.printStackTrace();}return itemId;}}