Subversion Repositories SmartDukaan

Rev

Rev 19225 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.hotspot.dashbaord.server;

import in.shop2020.hotspot.dashbaord.shared.actions.Item;
import in.shop2020.model.v1.catalog.CatalogService;
import in.shop2020.model.v1.inventory.InventoryService;
import in.shop2020.model.v1.inventory.InventoryType;
import in.shop2020.model.v1.inventory.Warehouse;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.InventoryClient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;

public class CatalogUtils {
        
        private static Map<Long, Map<Long, Warehouse>> billingWarehouseMap = new HashMap<Long, Map<Long,Warehouse>>();
        private static Map<Long, Map<Long, String>> billingWarehouseNameMap = new HashMap<Long, Map<Long,String>>();
        public static List<Item> getAllItems(){
                List<Item> itemList = new ArrayList<Item>();
                
                try {
                        CatalogClient catalogServiceClient = new CatalogClient();
                        CatalogService.Client catalogClient = catalogServiceClient.getClient();
                        
                        List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItems(true);
                        for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems)
                                itemList.add(getItemFromThriftItem(thriftItem));
                } catch (Exception e) {
                        // Not getting any items in the catalog is as good as not having any
                        // items in the catalog.
                        e.printStackTrace();
                }
                
                return itemList;
        }
        
        public static List<Item> getBestDeals(){
                List<Item> itemList = new ArrayList<Item>();
                
                try {
                        CatalogClient catalogServiceClient = new CatalogClient();
                        CatalogService.Client catalogClient = catalogServiceClient.getClient();
                        
                        List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getBestDeals();
                        for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems)
                                itemList.add(getItemFromThriftItem(thriftItem));
                } catch(Exception e){
                        e.printStackTrace();
                }
                
                return itemList;
        }
        
        public static List<Item> getBestSellers(){
                List<Item> itemList = new ArrayList<Item>();
                
                try {
                        CatalogClient catalogServiceClient = new CatalogClient();
                        CatalogService.Client catalogClient = catalogServiceClient.getClient();
                        
                        List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getBestSellers();
                        for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems)
                                itemList.add(getItemFromThriftItem(thriftItem));
                } catch(Exception e){
                        e.printStackTrace();
                }
                
                return itemList;                
        }
        
        public static List<Item> getLatestArrivals(){
                List<Item> itemList = new ArrayList<Item>();
                
                try {
                        CatalogClient catalogServiceClient = new CatalogClient();
                        CatalogService.Client catalogClient = catalogServiceClient.getClient();
                        
                        List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getLatestArrivals();
                        for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems)
                                itemList.add(getItemFromThriftItem(thriftItem));
                } catch(Exception e){
                        e.printStackTrace();
                }
                
                return itemList;
        }
        
        public static Item getItem(long itemId){
                try{
                        CatalogClient catalogServiceClient = new CatalogClient();
                        CatalogService.Client catalogClient = catalogServiceClient.getClient();
                        in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.getItem(itemId);
                        return getItemFromThriftItem(thriftItem);
                }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 static Map<Long, String> getWarehousesForBillingWarehouse(long warehouseId){
                if(billingWarehouseNameMap.get(warehouseId)==null){
                        try{
                                Map<Long, Warehouse> warehouseMap = getAllWarehousesForBillingWarehouse(warehouseId);
                                Map<Long, String> warehouseNameMap = new HashMap<Long, String>();
                                for(Warehouse warehouse: warehouseMap.values()){
                                        warehouseNameMap.put(warehouse.getId(), warehouse.getDisplayName());
                                }
                                billingWarehouseNameMap.put(warehouseId, warehouseNameMap);
                        }catch(Exception e){
                                e.printStackTrace();
                        }
                } else {
                        
                }
                return billingWarehouseNameMap.get(warehouseId);
        }
        
        public static Map<Long, Warehouse> getAllWarehousesForBillingWarehouse(long warehouseId){
                if(billingWarehouseMap.get(warehouseId)==null) {
                        try{
                            InventoryClient catalogServiceClient = new InventoryClient();
                                InventoryService.Client catalogClient = catalogServiceClient.getClient();
                                List<Warehouse> warehouses = catalogClient.getWarehouses(null, InventoryType.GOOD, 0, warehouseId, 0);

                                Map<Long, Warehouse> map = new HashMap<Long, Warehouse>();
                                for(Warehouse warehouse: warehouses){
                                        map.put(warehouse.getId(), warehouse);
                                }
                                billingWarehouseMap.put(warehouseId, map);
                        }catch(Exception e){
                                e.printStackTrace();
                        }
                }
                return billingWarehouseMap.get(warehouseId);
        }

    public static Warehouse getWarehouse(long warehouseId) {
        try{
            InventoryClient catalogServiceClient = new InventoryClient();
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
            return catalogClient.getWarehouse(warehouseId);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private static Item getItemFromThriftItem(in.shop2020.model.v1.catalog.Item thriftItem){
                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.getItemInventory(),
                                thriftItem.getMrp(),
                                //thriftItem.getMop(),
                                thriftItem.getSellingPrice(),
                                //thriftItem.getDealerPrice(),
                                thriftItem.getWeight(),
                                thriftItem.getAddedOn(),
                                thriftItem.getStartDate(),
                                thriftItem.getRetireDate(),
                                thriftItem.getUpdatedOn(),
                                //thriftItem.getItemStatus(),
                                thriftItem.getBestDealText(),
                                thriftItem.getBestDealValue());
                return item;
        }
}