Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;

import in.shop2020.model.v1.catalog.InventoryServiceException;
import in.shop2020.model.v1.catalog.InventoryService.Client;
import in.shop2020.serving.utils.FileUtils;
import in.shop2020.serving.utils.Utils;
import in.shop2020.thrift.clients.CatalogServiceClient;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.thrift.TException;

/**
 * 
 * @author rajveer
 *
 */
@SuppressWarnings("serial")
public class HomeController extends BaseController {

        private static Logger logger = Logger.getLogger(Class.class);
        
        private List<String> bestDealSnippets = null;
        private List<String> bestSellerSnippets = null;
        private List<String> latestArrivalSnippets = null;
        
        public HomeController(){
                super();
        }

        @Action("/")
    public String index() throws SecurityException, IOException {
        logger.info("userinfo:" + userinfo.toString());
        
                List<Long> bestDealCatalogIds = null;
                List<Long> bestSellerCatalogIds = null;
                List<Long> latestArrivalCatalogIds = null;
                
                int bestSellerCount = 4;
                int latestArrivalCount = 4;
                
                try {
                        CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
                        Client client = catalogServiceClient.getClient();
                        
                        //Get top 4 best deals 
                        bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
                        
                        //Get top 8 best sellers b'coz 4 of them may overlap with best deals.
                        bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
                        bestSellerCatalogIds.removeAll(bestDealCatalogIds);
                        if(bestSellerCatalogIds.size() < bestSellerCount)
                                bestSellerCount = bestSellerCatalogIds.size();
                        
                        //Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
                        //while another 4 may overlap with best sellers.
                        latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, 10003);
                        latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
                        latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
                        if(latestArrivalCatalogIds.size() < latestArrivalCount)
                                latestArrivalCount = latestArrivalCatalogIds.size();
                } catch (InventoryServiceException e) {
                        logger.error("Error while fetching data from the catalog service", e);
                } catch (TException e) {
                        logger.error("Error while fetching data from the catalog service", e);
                } catch (Exception e) {
                        logger.error("Unexpected exception", e);
                }
        
                bestDealSnippets = getSnippets(bestDealCatalogIds);
                bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
                latestArrivalSnippets = getSnippets(latestArrivalCatalogIds).subList(0, latestArrivalCount);
                
        htmlSnippets.put("MAIN_BANNER", pageLoader.getMainBannerHtml());
        return "index";
    }

        /**
         * Returns a list of snippets to be used on the home page corresponding to
         * the given catalog ids. The snippets are in the same order as the ids in
         * the input list. In case a snippet is not found for an entity, this method
         * simply logs the problem and moves on.
         * 
         * @param catalogIds
         *            Ids of the entities which we want to show.
         * @return The snippet corresponding to each catalog entity
         */
        private List<String> getSnippets(List<Long> catalogIds) {
                List<String> snippets = new ArrayList<String>();
                if(catalogIds == null)
                        return snippets;
                for(Long item: catalogIds){
                        try{
                                snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
                        }catch(IOException ioex){
                                logger.error("Unable to get home page snippet for " + item, ioex);
                        }
                }
                return snippets;
        }
    
        public String getMainBannerSnippet(){
                return htmlSnippets.get("MAIN_BANNER");
        }
        
        public List<String> getBestDealSnippets(){
                return bestDealSnippets; 
        }
        
        public List<String> getBestSellerSnippets(){
                return bestSellerSnippets; 
        }
        
        public List<String> getLatestArrivalSnippets(){
                return latestArrivalSnippets;
        }
}