Subversion Repositories SmartDukaan

Rev

Rev 20656 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.mobileapi.serving.utils;

import in.shop2020.metamodel.util.ContentPojo;
import in.shop2020.metamodel.util.ProductPojo;
import in.shop2020.mobileapi.serving.cache.EhcacheWrapper;
import in.shop2020.mobileapi.serving.pojos.CartLinePojo;
import in.shop2020.mobileapi.serving.pojos.CartPojo;
import in.shop2020.mobileapi.serving.pojos.MenuPojo;
import in.shop2020.mobileapi.serving.pojos.RechargeAmountPojo;
import in.shop2020.model.v1.catalog.CatalogService;
import in.shop2020.model.v1.catalog.InsurerType;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.user.Cart;
import in.shop2020.model.v1.user.Line;
import in.shop2020.storage.mongo.StorageManager;
import in.shop2020.thrift.clients.CatalogClient;

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

import net.sf.ehcache.CacheManager;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.google.gson.Gson;

public class PojoPopulator {
        
        public static Gson gson = new Gson();
        private static Logger log = Logger.getLogger(PojoPopulator.class);
        
        static EhcacheWrapper<Long, Map<String, Object>> skuCache = new EhcacheWrapper<Long, Map<String, Object>>(
                        EhcacheWrapper.SKU_CACHE, CacheManager.create());

    public static List<MenuPojo> getCurrentMenu() {
        List<MenuPojo> finalList = new ArrayList<MenuPojo>();
        List<MenuPojo> children = new ArrayList<MenuPojo>();
        List<MenuPojo> children1 = new ArrayList<MenuPojo>();
        List<MenuPojo> children2 = new ArrayList<MenuPojo>();
        List<MenuPojo> children3 = new ArrayList<MenuPojo>();
        List<MenuPojo> children4 = new ArrayList<MenuPojo>();
        List<MenuPojo> children5 = new ArrayList<MenuPojo>();

        //  Mobile Menu start
        children.clear();
        children.add(getMenuPojoObject("Phablets", "phablets", "", null));
        children.add(getMenuPojoObject("Android Phones", "android", "", null));
        children.add(getMenuPojoObject("Dual Sim Phones", "dual-sim", "", null));
        children.add(getMenuPojoObject("Windows Phones", "windows-phones", "", null));
        children.add(getMenuPojoObject("CDMA Phones", "cdma-mobiles", "", null));
        children.add(getMenuPojoObject("Basic Phones", "basic-phones", "", null));
        children.add(getMenuPojoObject("Coming Soon", "coming-soon/1", "", null));
        children.add(getMenuPojoObject("All Mobiles", "all-mobiles/10006", "", null));
        finalList.add(getMenuPojoObject("Mobiles", "", Utils.STATIC_SERVER_PATH + "menu/mobile.PNG", children));
        //  Mobile Menu end
        
        //  Tablet Menu start
        children1.add(getMenuPojoObject("With call facility", "tablets-with-calling", "", null));
        children1.add(getMenuPojoObject("Without call facility", "tablets-without-calling", "", null));
        children1.add(getMenuPojoObject("Apple", "apple-tablets", "", null));
        children1.add(getMenuPojoObject("Samsung", "samsung-tablets", "", null));
        children1.add(getMenuPojoObject("Spice", "spice-tablets", "", null));
        finalList.add(getMenuPojoObject("Tablets", "all-tablets/10010", Utils.STATIC_SERVER_PATH + "menu/tablet.PNG", children1));
        //  Tablet Menu end
        
        //  Laptop Menu start
        children2.add(getMenuPojoObject("Windows Laptop", "windows-laptops", "", null));
        children2.add(getMenuPojoObject("Apple Macbook", "mac-laptops", "", null));
        children2.add(getMenuPojoObject("Linux/DOS Laptops", "cheap-laptops", "", null));
        finalList.add(getMenuPojoObject("Laptops", "all-laptops/10050", Utils.STATIC_SERVER_PATH + "menu/laptop.PNG", children2));
        //  Laptop Menu end
        
        //  Accessories Menu start
        children3.add(getMenuPojoObject("Carrying case", "carrying-case/10018", "", null));
        children3.add(getMenuPojoObject("Headphones", "headphones/10082", "", null));
        children3.add(getMenuPojoObject("Headsets", "headset/10015", "", null));
        children3.add(getMenuPojoObject("Chargers", "charger/10016", "", null));
        children3.add(getMenuPojoObject("Music Players", "portable-music-players/12001", "", null));
        children3.add(getMenuPojoObject("Speakers", "speaker/10026", "", null));
        finalList.add(getMenuPojoObject("Accessories", "all-mobile-accessories/10011", Utils.STATIC_SERVER_PATH + "menu/accessories.PNG", children3));
        /*
            <a class="link-menu-secondary menu-secondary-brand" href="/battery/10014">Battery</a>
            <a class="link-menu-secondary menu-secondary-brand" href="/memory-card/10013">Memory Card</a>
            <a class="link-menu-secondary menu-secondary-brand" href="/bluetooth-headset/10012">Bluetooth Headset</a>
            <a class="link-menu-secondary menu-secondary-brand" href="/pen-drive/10017">Pen Drive</a>
            <a class="link-menu-secondary menu-secondary-brand" href="/speaker/10026">Speaker</a>
            <a class="link-menu-secondary menu-secondary-brand" href="/laptop-bags/10085">Laptop Bags</a>
            <a class="link-menu-secondary menu-secondary-brand" href="/mouse/10081">Mouse</a>
            <a class="link-menu-secondary menu-secondary-brand" href="/laptop-batteries/10077">Laptop Batteries</a>
            <a class="link-menu-secondary menu-secondary-brand" href="/data-cards/10083">Data Cards</a>
            <a class="link-menu-secondary menu-secondary-brand" href="/external-hard-disks/10073">External Hard disk</a>
            <a class="link-menu-secondary menu-secondary-brand" href="/mouse-pads/10084">Mouse Pads</a>
        */
        //  Accessories Menu end
        
        //  Camera Menu start
        children4.add(getMenuPojoObject("DSLR Camera", "dslr-camera/11003", "", null));
        children4.add(getMenuPojoObject("Compact Camera", "compact-camera/11002", "", null));
        finalList.add(getMenuPojoObject("Cameras", "all-cameras/11001", Utils.STATIC_SERVER_PATH + "menu/camera.PNG", children4));
        //  Camera Menu end
        
        //  Recharge Menu start
        children5.add(getMenuPojoObject("My Wallet", "my-wallet", "", null));
        children5.add(getMenuPojoObject("My Recharges", "my-recharges", "", null));
        children5.add(getMenuPojoObject("Learn More", "recharge-faq", "", null));
        finalList.add(getMenuPojoObject("Recharge", "recharge", Utils.STATIC_SERVER_PATH + "menu/recharge.PNG", children5));
        //  Recharge Menu end
        
        return finalList;
    }
    
    private static MenuPojo getMenuPojoObject(String name, String url, String iconUrl, List<MenuPojo> children) {
        MenuPojo menuObject = new MenuPojo();
        menuObject.setName(name);
        menuObject.setUrl(url);
        menuObject.setIcon_url(iconUrl);
        menuObject.setChildren(children);
        return menuObject;
    }
    
        
        public static ProductPojo getShortContent(long entityId) {
                ContentPojo cp = StorageManager.getById(StorageManager.views.siteContent, entityId, ContentPojo.class);
                ProductPojo pp = null;
                if(cp!=null){
                        try {
                        pp = new ProductPojo();
                        pp.setId(cp.getId());
                        pp.setDefaultImgUrl(cp.getDefaultImageUrl());
                        pp.setOfferText(cp.getOfferText());
                        pp.setDescription(StringUtils.join(cp.getKeySpecs().subList(0, Math.min(cp.getKeySpecs().size(), 3)),", "));
                        pp.setImageUrl(cp.getIconImageUrl());
                        pp.setUrl(cp.getUrl());
                        pp.setMrp(cp.getItems().get(0).getMrp());
                        pp.setPrice(cp.getItems().get(0).getSellingPrice());
                        pp.setItemPojos(cp.getItems());
                        pp.setTitle(cp.getTitle());
                        } catch (Exception e) {
                                e.printStackTrace();
                                log.info(e.toString());
                                log.error("Error in entity : " + entityId);
                                pp = null;
                        }
                }
                return pp;
        }
        
        public static CatalogService.Client getCatalogCleint(CatalogService.Client catalogClient) throws Exception{
                if(!catalogClient.getOutputProtocol().getTransport().isOpen()) {
                        catalogClient = new CatalogClient().getClient();
                }
                return catalogClient;
        }
        
        public static CartPojo getCartPojo (Cart cart, String errorMsg, String pincode, Boolean needInsuranceInfo) throws Exception{
            CartPojo cartPojo = new CartPojo();
            cartPojo.setCouponCode(cart.getCouponCode());
            cartPojo.setDiscountedPrice(cart.getDiscountedPrice());
            cartPojo.setId(cart.getId());
            cartPojo.setMessage(errorMsg);
            cartPojo.setPickupStoreId(cart.getPickupStoreId());
            cartPojo.setTotalPrice(cart.getTotalPrice());
            cartPojo.setNeedInsuranceInfo(needInsuranceInfo);
            
            List<CartLinePojo> linepojos = new ArrayList<CartLinePojo>();
            CatalogService.Client catalogClient;
            for(Line line : cart.getLines()) {
                CartLinePojo linepojo = new CartLinePojo();
                catalogClient = new CatalogClient().getClient();
                Item item = catalogClient.getItem(line.getItemId());
                linepojo.setDealText(line.isSetDealText()? line.getDealText() : item.getBestDealText());
                //linepojo.setDataProtectionAvailable(getCatalogCleint(catalogClient).getPrefferedInsurerForItem(line.getItemId(), InsurerType.DATA)>0);
                //linepojo.setTheftInsuranceAvailable(getCatalogCleint(catalogClient).getPrefferedInsurerForItem(line.getItemId(), InsurerType.DEVICE)>0);
                linepojo.setItemId(line.getItemId());
                linepojo.setActualPrice(line.getActualPrice());
                linepojo.setDataProtectionAmount(line.getDataProtectionAmount());
                linepojo.setDataProtectionInsurer(line.getDataProtectionInsurer());
                linepojo.setDiscountedPrice(line.getDiscountedPrice());
                linepojo.setInsuranceAmount(line.getInsuranceAmount());
                linepojo.setInsurer(line.getInsurer());
                linepojo.setQuantity(line.getQuantity());
                linepojo.setLogisticsInfo(in.shop2020.mobileapi.serving.services.LogisticsService.getLogisticsInfo(line.getItemId(), pincode));
                if(linepojo.getLogisticsInfo().getBusinessDays()==-1){
                        cartPojo.setLocationServiceable(false);
                        cartPojo.setLocationServiceableMessage("Location not serviceable. Please specify a different address");
                }
            linepojo.setName(item.getBrand() + " " + item.getModelName() + " " + item.getModelNumber());
            linepojo.setColor(item.getColor());
            linepojo.setImageUrl(PojoPopulator.getShortContent(item.getCatalogItemId()).getImageUrl());
            linepojos.add(linepojo);
            }
            cartPojo.setLines(linepojos);
        return cartPojo;
            
        }
    
        public static RechargeAmountPojo getRechargeAmountPojo(Long payAmount, Long walletAmount, Long couponAmount, Long walletAmountLeft, boolean isLoginRequired) {
            RechargeAmountPojo amnt = new RechargeAmountPojo();
            amnt.setCouponAmount(couponAmount);
            amnt.setLoginRequired(isLoginRequired);
            amnt.setPayAmount(payAmount);
            amnt.setWalletAmount(walletAmount);
            amnt.setWalletAmountLeft(walletAmountLeft);
            return amnt;
        }
}