Rev 2867 | Rev 2989 | 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 chandranshu**/@SuppressWarnings("serial")public class HomeController extends BaseController {private static Logger logger = Logger.getLogger(Class.class);private static List<String> bestDealSnippets = null;private static List<String> bestSellerSnippets = null;private static List<String> latestArrivalSnippets = null;public HomeController(){super();}/*** Renders the home page. Loads the snippets for best deals, best sellers* and latest arrivals if they haven't been loaded already.** @return Name of the view to render.* @throws SecurityException* @throws IOException*/@Action("/")public String index() throws SecurityException, IOException {logger.debug("userinfo:" + userinfo.toString());logger.info("Rendering the home page");if(bestDealSnippets == null || bestSellerSnippets == null || latestArrivalSnippets == null){logger.info("Cache miss: Reloading best deals, best sellers and latest arrivals");setSnippets();}return "index";}/*** Renders the home page but always loads the snippets for best deals, best* sellers and latest arrivals unconditionally. Should be used to reset the* snippets.** @return Name of the view to render.* @throws SecurityException* @throws IOException*/@Action("/refresh-home")public String destroy() throws SecurityException, IOException{logger.info("Reloading best deals, best sellers and latest arrivals");setSnippets();return "index";}public List<String> getBestDealSnippets(){return bestDealSnippets;}public List<String> getBestSellerSnippets(){return bestSellerSnippets;}public List<String> getLatestArrivalSnippets(){return latestArrivalSnippets;}/*** Loads the snippets for best deals, best sellers and latest arrivals* into memory.*/private void setSnippets() {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 dealsbestDealCatalogIds = 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);}/*** 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;}}