Subversion Repositories SmartDukaan

Rev

Rev 2119 | Rev 2208 | 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.VendorItemMapping;
import in.shop2020.catalog.dashboard.shared.VendorPricings;
import in.shop2020.model.v1.catalog.InventoryService;
import in.shop2020.model.v1.catalog.status;
import in.shop2020.thrift.clients.CatalogServiceClient;
import in.shop2020.utils.CategoryManager;

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 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(false);
            
            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> getAllActiveItems(){
        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, vim);
        }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;
    }
    
    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, VendorItemMapping> vItemMap = new HashMap<Long, VendorItemMapping>();
        VendorItemMapping vItemMapping;
        if(tVendorMappings != null) {
            for(in.shop2020.model.v1.catalog.VendorItemMapping vim : tVendorMappings) {
                vItemMapping = new VendorItemMapping();
                vItemMapping.setVendorId(vim.getVendorId());
                vItemMapping.setItemKey(vim.getItemKey());
                vItemMap.put(vItemMapping.getVendorId(), vItemMapping);
            }
        }
        
        // TODO: For testing. Remove this code before moving to production.
        /* 
        Map<Long, VendorItemMapping> vItemMap = new HashMap<Long, VendorItemMapping>();
        vItemMap.put(1L, new VendorItemMapping(1, "Item Key Test 1"));
        vItemMap.put(1L, new VendorItemMapping(1, "Item Key Test 2"));
        */
        
        Map<Long, VendorPricings> vendorPricingMap = new HashMap<Long, VendorPricings>();
        VendorPricings vPricings;
        if(tVendorPricings != null) {
            for(in.shop2020.model.v1.catalog.VendorItemPricing vip : tVendorPricings) {
                vPricings = new VendorPricings();
                vPricings.setVendorId(vip.getVendorId());
                vPricings.setMop(vip.getMop());
                vPricings.setDealerPrice(vip.getDealerPrice());
                vPricings.setTransferPrice(vip.getTransferPrice());
                vendorPricingMap.put(vPricings.getVendorId(), vPricings);
            }
        }
        
        Item item = new Item(thriftItem.getId(),
                thriftItem.getProductGroup(),
                thriftItem.getBrand(),
                thriftItem.getModelNumber(),
                thriftItem.getModelName(),
                thriftItem.getColor(),
                //thriftItem.getCategory(),
                CategoryManager.getCategoryManager().getCategoryLabel(thriftItem.getCategory()),
                thriftItem.getCategory(),
                thriftItem.getComments(),
                thriftItem.getCatalogItemId(),
                thriftItem.getFeatureId(),
                thriftItem.getFeatureDescription(),
                thriftItem.isSetMrp() ? thriftItem.getMrp() : -1,
                thriftItem.getMop(),
                thriftItem.isSetSellingPrice() ? thriftItem.getSellingPrice() : -1,
                thriftItem.getDealerPrice(),
                thriftItem.isSetWeight() ? thriftItem.getWeight() : -1,
                thriftItem.getAddedOn(),
                thriftItem.getStartDate(),
                thriftItem.getRetireDate(),
                thriftItem.getUpdatedOn(),
                thriftItem.getItemStatus().name(),
                thriftItem.getItemStatus().getValue(),
                thriftItem.getOtherInfo(),
                thriftItem.getBestDealText(),
                thriftItem.isSetBestDealValue() ? thriftItem.getBestDealValue() : -1,
                thriftItem.isSetBestSellingRank() ? thriftItem.getBestSellingRank() : -1,
                thriftItem.isDefaultForEntity(),
                (thriftItem.getItemInventory() != null ? thriftItem.getItemInventory().getAvailability() : null),
                vendorPricingMap,
                vItemMap
                );
        return item;
    }

    @Override
    public 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());
            setThriftItemParams(tItem, item);
//          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, VendorItemMapping> vendorMappings = item.getVendorMappingsMap();
            if(vendorMappings != null && !vendorMappings.isEmpty()) {
                in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;
                for(VendorItemMapping v : vendorMappings.values()) {
                    tVendorMapping = new in.shop2020.model.v1.catalog.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());
                }
            }
            
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
                for(VendorPricings v : vendorPricings.values()) {
                    tVendorPricing = new in.shop2020.model.v1.catalog.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());
                }
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return true;
    }
    
    
    @Override
    public 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;
    }

    @Override
    public 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;
    }

    @Override
    public 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();
            setThriftItemParams(tItem, item);
            itemId = catalogClient.addItem(tItem);

            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
                for(VendorPricings v : vendorPricings.values()) {
                    tVendorPricing = new in.shop2020.model.v1.catalog.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, VendorItemMapping> vendorKeysMap = item.getVendorMappingsMap();
            if(vendorKeysMap != null && !vendorKeysMap.isEmpty()) {
                in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;
                for(VendorItemMapping v : vendorKeysMap.values()) {
                    tVendorMapping = new in.shop2020.model.v1.catalog.VendorItemMapping();
                    tVendorMapping.setVendorId(v.getVendorId());
                    tVendorMapping.setItemKey(v.getItemKey());
                    tVendorMapping.setItemId(itemId);
                    tVendorMapping.setVendorCategory(item.getVendorCategory());
                    catalogClient.addVendorItemMapping(tVendorMapping);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return itemId;
    }

    @Override
    public long checkSimilarItem(String productGroup, String brand, String modelNumber, String color) {
        
        try {
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
            return catalogClient.checkSimilarItem(productGroup, brand, modelNumber, color);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }
    
    private void setThriftItemParams(in.shop2020.model.v1.catalog.Item tItem, Item item) {
        tItem.setId(tItem.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());
        
        if(item.getMrp() != -1) {
            tItem.setMrp(item.getMrp());
        } else {
            tItem.setMrpIsSet(false);
        }
        if(item.getSellingPrice() != -1) {
            tItem.setSellingPrice(item.getSellingPrice());
        } else {
            tItem.setSellingPriceIsSet(false);
        }
        if(item.getWeight() != -1) {
            tItem.setWeight(item.getWeight());
        } else {
            tItem.setWeightIsSet(false);
        }
        
        tItem.setMop(item.getMop());
        tItem.setDealerPrice(item.getDealerPrice());
        tItem.setTransferPrice(item.getTransferPrice());

        tItem.setBestDealText(item.getBestDealsText());
        if(item.getBestDealsValue() != -1) {
            tItem.setBestDealValue(item.getBestDealsValue());
        } else {
            tItem.setBestDealValueIsSet(false);
        }
        if(item.getBestSellingRank() != -1) {
            tItem.setBestSellingRank(item.getBestSellingRank());
        } else {
            tItem.setBestSellingRankIsSet(false);
        }
        tItem.setDefaultForEntity(item.isDefaultForEntity());
        
        tItem.setStartDate(item.getStartDate());
        tItem.setUpdatedOn(Calendar.getInstance().getTimeInMillis());
        tItem.setRetireDate(item.getRetireDate());
        tItem.setHotspotCategory(item.getVendorCategory());
    }

    @Override
    public void pauseItem(long itemId) {
        try {
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
            
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PAUSED);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

    @Override
    public void markInProcess(long itemId) {
        try {
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
            
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.IN_PROCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
    @Override
    public 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();
        }
    }
    
    @Override
    public 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();
        }
    }
}