Subversion Repositories SmartDukaan

Rev

Rev 1044 | Rev 2579 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
507 rajveer 1
package in.shop2020.serving.controllers;
2
 
2578 chandransh 3
import in.shop2020.model.v1.catalog.InventoryServiceException;
4
import in.shop2020.model.v1.catalog.InventoryService.Client;
5
import in.shop2020.serving.utils.FileUtils;
6
import in.shop2020.serving.utils.Utils;
7
import in.shop2020.thrift.clients.CatalogServiceClient;
507 rajveer 8
 
2578 chandransh 9
import java.io.File;
507 rajveer 10
import java.io.IOException;
2578 chandransh 11
import java.util.ArrayList;
12
import java.util.List;
507 rajveer 13
 
832 rajveer 14
import org.apache.log4j.Logger;
773 rajveer 15
import org.apache.struts2.convention.annotation.Action;
2578 chandransh 16
import org.apache.thrift.TException;
507 rajveer 17
 
18
/**
19
 * 
20
 * @author rajveer
21
 *
22
 */
2578 chandransh 23
@SuppressWarnings("serial")
650 rajveer 24
public class HomeController extends BaseController {
25
 
2578 chandransh 26
	private static Logger logger = Logger.getLogger(Class.class);
507 rajveer 27
 
2578 chandransh 28
	private List<String> bestDealSnippets = null;
29
	private List<String> bestSellerSnippets = null;
30
	private List<String> latestArrivalSnippets = null;
31
 
507 rajveer 32
	public HomeController(){
33
		super();
34
	}
35
 
773 rajveer 36
	@Action("/")
650 rajveer 37
    public String index() throws SecurityException, IOException {
2578 chandransh 38
    	logger.info("userinfo:" + userinfo.toString());
507 rajveer 39
 
2578 chandransh 40
		List<Long> bestDealCatalogIds = null;
41
		List<Long> bestSellerCatalogIds = null;
42
 		List<Long> latestArrivalCatalogIds = null;
43
 
44
		try {
45
			CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
46
			Client client = catalogServiceClient.getClient();
47
 
48
			//Get top 4 best deals 
49
			bestDealCatalogIds = client.getBestDealsCatalogIds(0,4, null, -1);
50
 
51
			//Get top 8 best deals b'coz 4 of them may overlap with best deals.
52
			bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
53
			bestSellerCatalogIds.removeAll(bestDealCatalogIds);
54
 
55
			//Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
56
			//while another 4 may overlap with best sellers.
57
			latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, 10003);
58
			latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
59
			latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, 4)); //We're only considering the first 4 best sellers for removal.
60
		} catch (InventoryServiceException e) {
61
			logger.error("Error while fetching data from the catalog service", e);
62
		} catch (TException e) {
63
			logger.error("Error while fetching data from the catalog service", e);
64
		} catch (Exception e) {
65
			logger.error("Unexpected exception", e);
66
		}
67
 
68
		bestDealSnippets = getSnippets(bestDealCatalogIds);
69
		bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, 4));
70
		latestArrivalSnippets = getSnippets(latestArrivalCatalogIds).subList(0, 4);
71
 
650 rajveer 72
    	htmlSnippets.put("MAIN_BANNER", pageLoader.getMainBannerHtml());
73
    	return "index";
507 rajveer 74
    }
2578 chandransh 75
 
76
	private List<String> getSnippets(List<Long> catalogIds) {
77
		List<String> snippets = new ArrayList<String>();
78
		if(catalogIds == null)
79
			return snippets;
80
		for(Long item: catalogIds){
81
			try{
82
				snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
83
			}catch(IOException ioex){
84
				logger.error("Unable to get home page snippet for " + item, ioex);
85
			}
86
		}
87
		return snippets;
88
	}
507 rajveer 89
 
90
	public String getMainBannerSnippet(){
91
		return htmlSnippets.get("MAIN_BANNER");
92
	}
93
 
2578 chandransh 94
	public List<String> getBestDealSnippets(){
95
		return bestDealSnippets; 
507 rajveer 96
	}
97
 
2578 chandransh 98
	public List<String> getBestSellerSnippets(){
99
		return bestSellerSnippets; 
507 rajveer 100
	}
101
 
2578 chandransh 102
	public List<String> getLatestArrivalSnippets(){
103
		return latestArrivalSnippets;
507 rajveer 104
	}
105
}