Subversion Repositories SmartDukaan

Rev

Rev 2579 | Rev 2595 | 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
 
2579 chandransh 44
 		int bestSellerCount = 4;
45
 		int latestArrivalCount = 4;
46
 
2578 chandransh 47
		try {
48
			CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
49
			Client client = catalogServiceClient.getClient();
50
 
51
			//Get top 4 best deals 
2594 chandransh 52
			bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
2578 chandransh 53
 
54
			//Get top 8 best deals b'coz 4 of them may overlap with best deals.
55
			bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
56
			bestSellerCatalogIds.removeAll(bestDealCatalogIds);
2579 chandransh 57
			if(bestSellerCatalogIds.size() < bestSellerCount)
58
				bestSellerCount = bestSellerCatalogIds.size();
2578 chandransh 59
 
60
			//Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
61
			//while another 4 may overlap with best sellers.
62
			latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, 10003);
63
			latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
2579 chandransh 64
			latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
65
			if(latestArrivalCatalogIds.size() < latestArrivalCount)
66
				latestArrivalCount = latestArrivalCatalogIds.size();
2578 chandransh 67
		} catch (InventoryServiceException e) {
68
			logger.error("Error while fetching data from the catalog service", e);
69
		} catch (TException e) {
70
			logger.error("Error while fetching data from the catalog service", e);
71
		} catch (Exception e) {
72
			logger.error("Unexpected exception", e);
73
		}
74
 
75
		bestDealSnippets = getSnippets(bestDealCatalogIds);
2579 chandransh 76
		bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
77
		latestArrivalSnippets = getSnippets(latestArrivalCatalogIds).subList(0, latestArrivalCount);
2578 chandransh 78
 
650 rajveer 79
    	htmlSnippets.put("MAIN_BANNER", pageLoader.getMainBannerHtml());
80
    	return "index";
507 rajveer 81
    }
2578 chandransh 82
 
2594 chandransh 83
	/**
84
	 * Returns a list of snippets to be used on the home page corresponding to
85
	 * the given catalog ids. The snippets are in the same order as the ids in
86
	 * the input list. In case a snippet is not found for an entity, this method
87
	 * simply logs the problem and moves on.
88
	 * 
89
	 * @param catalogIds
90
	 *            Ids of the entities which we want to show.
91
	 * @return The snippet corresponding to each catalog entity
92
	 */
2578 chandransh 93
	private List<String> getSnippets(List<Long> catalogIds) {
94
		List<String> snippets = new ArrayList<String>();
95
		if(catalogIds == null)
96
			return snippets;
97
		for(Long item: catalogIds){
98
			try{
99
				snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
100
			}catch(IOException ioex){
101
				logger.error("Unable to get home page snippet for " + item, ioex);
102
			}
103
		}
104
		return snippets;
105
	}
507 rajveer 106
 
107
	public String getMainBannerSnippet(){
108
		return htmlSnippets.get("MAIN_BANNER");
109
	}
110
 
2578 chandransh 111
	public List<String> getBestDealSnippets(){
112
		return bestDealSnippets; 
507 rajveer 113
	}
114
 
2578 chandransh 115
	public List<String> getBestSellerSnippets(){
116
		return bestSellerSnippets; 
507 rajveer 117
	}
118
 
2578 chandransh 119
	public List<String> getLatestArrivalSnippets(){
120
		return latestArrivalSnippets;
507 rajveer 121
	}
122
}