Subversion Repositories SmartDukaan

Rev

Rev 11829 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
507 rajveer 1
package in.shop2020.serving.controllers;
2
 
2989 vikas 3
import in.shop2020.datalogger.EventType;
5945 mandeep.dh 4
import in.shop2020.model.v1.catalog.CatalogService.Client;
5
import in.shop2020.model.v1.catalog.CatalogServiceException;
3242 vikas 6
import in.shop2020.serving.cache.EhcacheWrapper;
3561 rajveer 7
import in.shop2020.serving.services.ContentServingService;
8
import in.shop2020.serving.utils.SnippetType;
3126 rajveer 9
import in.shop2020.thrift.clients.CatalogClient;
2989 vikas 10
import in.shop2020.utils.DataLogger;
507 rajveer 11
 
12
import java.io.IOException;
2578 chandransh 13
import java.util.ArrayList;
3266 chandransh 14
import java.util.Arrays;
10216 amit.gupta 15
import java.util.HashMap;
2578 chandransh 16
import java.util.List;
10216 amit.gupta 17
import java.util.Map;
507 rajveer 18
 
3050 vikas 19
import net.sf.ehcache.CacheManager;
20
 
832 rajveer 21
import org.apache.log4j.Logger;
773 rajveer 22
import org.apache.struts2.convention.annotation.Action;
2578 chandransh 23
import org.apache.thrift.TException;
507 rajveer 24
 
25
/**
26
 * 
2930 chandransh 27
 * @author chandranshu
507 rajveer 28
 *
29
 */
2578 chandransh 30
@SuppressWarnings("serial")
650 rajveer 31
public class HomeController extends BaseController {
32
 
2578 chandransh 33
	private static Logger logger = Logger.getLogger(Class.class);
3050 vikas 34
	private static final String BEST_DEALS_CACHE_KEY = "home_best_deals_snippet";
35
	private static final String BEST_SELLERS_CACHE_KEY = "home_best_sellers_snippet";
36
	private static final String LATEST_ARRIVALS_CACHE_KEY = "home_latest_arrivals_snippet";
10216 amit.gupta 37
	private static final String HOME_PAGE_CACHE_KEY = "home_page_cache_snippets";
8917 kshitij.so 38
 
10216 amit.gupta 39
 
3050 vikas 40
	private List<String> bestDealSnippets = null;
41
	private List<String> bestSellerSnippets = null;
42
	private List<String> latestArrivalSnippets = null;
8917 kshitij.so 43
 
507 rajveer 44
	public HomeController(){
45
		super();
46
	}
47
 
8917 kshitij.so 48
	/**
49
	 * Renders the home page. Loads the snippets for best deals, best sellers
50
	 * and latest arrivals if they haven't been loaded already.
51
	 * 
52
	 * @return Name of the view to render.
53
	 * @throws SecurityException
54
	 * @throws IOException
55
	 */
773 rajveer 56
	@Action("/")
8917 kshitij.so 57
	public String index() throws SecurityException, IOException {
58
		logger.debug("userinfo:" + userinfo.toString());
59
		logger.info("Rendering the home page");
60
		setSnippets();
12616 anikendra 61
//		DataLogger.logData(EventType.HOME_PAGE, getSessionId(), userinfo.getUserId(), userinfo.getEmail());
8917 kshitij.so 62
		return "index";
63
	}
2578 chandransh 64
 
8917 kshitij.so 65
	/**
66
	 * Renders the home page but always loads the snippets for best deals, best
67
	 * sellers and latest arrivals unconditionally. Should be used to reset the
68
	 * snippets.
69
	 * 
70
	 * @return Name of the view to render.
71
	 * @throws SecurityException
72
	 * @throws IOException
73
	 */
2930 chandransh 74
	@Action("/refresh-home")
75
	public String destroy() throws SecurityException, IOException{
8917 kshitij.so 76
		logger.info("Reloading best deals, best sellers and latest arrivals");
77
		EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
78
				EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
79
		homeSnippetCache.removeAll();
80
		setSnippets();
81
		return "index";
2578 chandransh 82
	}
8917 kshitij.so 83
 
2578 chandransh 84
	public List<String> getBestDealSnippets(){
85
		return bestDealSnippets; 
507 rajveer 86
	}
8917 kshitij.so 87
 
2578 chandransh 88
	public List<String> getBestSellerSnippets(){
89
		return bestSellerSnippets; 
507 rajveer 90
	}
8917 kshitij.so 91
 
2578 chandransh 92
	public List<String> getLatestArrivalSnippets(){
93
		return latestArrivalSnippets;
507 rajveer 94
	}
2930 chandransh 95
 
96
	/**
97
	 * Loads the snippets for best deals, best sellers and latest arrivals
98
	 * into memory.
99
	 */
8917 kshitij.so 100
	private void setSnippets() {
10216 amit.gupta 101
		EhcacheWrapper<String,Map<String, List<String>>> homeSnippetCache = new EhcacheWrapper<String,Map<String, List<String>>>(
8917 kshitij.so 102
				EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
10216 amit.gupta 103
		//logger.info("Source id "+sourceId);
104
		Map<String, List<String>> homePageSnippets = homeSnippetCache.get(HOME_PAGE_CACHE_KEY);
8917 kshitij.so 105
 
10216 amit.gupta 106
		if(homePageSnippets == null) {
107
			homePageSnippets = new HashMap<String, List<String>>();
108
			List<Long> bestDealCatalogIds = null;
109
			List<Long> bestSellerCatalogIds = null;
110
			List<Long> latestArrivalCatalogIds = null;
8917 kshitij.so 111
 
10216 amit.gupta 112
			int bestSellerCount = 4;
113
			int latestArrivalCount = 4;
114
			try {
115
				CatalogClient catalogServiceClient = new CatalogClient();
116
				Client client = catalogServiceClient.getClient();
117
				//Get top 4 best deals 
118
				bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
8917 kshitij.so 119
 
10216 amit.gupta 120
				//Get top 8 best sellers b'coz 4 of them may overlap with best deals.
121
				bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
122
				bestSellerCatalogIds.removeAll(bestDealCatalogIds);
123
				if(bestSellerCatalogIds.size() < bestSellerCount)
124
					bestSellerCount = bestSellerCatalogIds.size();
8917 kshitij.so 125
 
10216 amit.gupta 126
				//Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
127
				//while another 4 may overlap with best sellers.
128
				latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, Arrays.asList(new Long[]{ 10006L, 10010L, 10050L}));
129
				latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
130
				latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
131
				if(latestArrivalCatalogIds.size() < latestArrivalCount)
132
					latestArrivalCount = latestArrivalCatalogIds.size();
8917 kshitij.so 133
 
10216 amit.gupta 134
			} catch (CatalogServiceException e) {
135
				logger.error("Error while fetching data from the catalog service", e);
136
			} catch (TException e) {
137
				logger.error("Error while fetching data from the catalog service", e);
138
			} catch (Exception e) {
139
				logger.error("Unexpected exception", e);
8917 kshitij.so 140
			}
10216 amit.gupta 141
			bestDealSnippets = getSnippets(bestDealCatalogIds);
142
			bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
143
			latestArrivalSnippets = getSnippets(latestArrivalCatalogIds.subList(0, latestArrivalCount));
144
			homePageSnippets.put(BEST_DEALS_CACHE_KEY, bestDealSnippets);
145
			homePageSnippets.put(BEST_SELLERS_CACHE_KEY, bestSellerSnippets);
146
			homePageSnippets.put(LATEST_ARRIVALS_CACHE_KEY, latestArrivalSnippets);
147
			homeSnippetCache.put(HOME_PAGE_CACHE_KEY, homePageSnippets);
10232 kshitij.so 148
			logger.info("Populated home snippet cache");
10216 amit.gupta 149
			return;
8917 kshitij.so 150
		}
10216 amit.gupta 151
 
152
		bestDealSnippets = homePageSnippets.get(BEST_DEALS_CACHE_KEY);
153
		bestSellerSnippets = homePageSnippets.get(BEST_SELLERS_CACHE_KEY);
154
		latestArrivalSnippets = homePageSnippets.get(LATEST_ARRIVALS_CACHE_KEY);
10232 kshitij.so 155
		logger.info("Returning snippets from cache");
8917 kshitij.so 156
	}
157
 
158
	/**
159
	 * Returns a list of snippets to be used on the home page corresponding to
160
	 * the given catalog ids. The snippets are in the same order as the ids in
161
	 * the input list. In case a snippet is not found for an entity, this method
162
	 * simply logs the problem and moves on.
163
	 * 
164
	 * @param catalogIds
165
	 *            Ids of the entities which we want to show.
166
	 * @return The snippet corresponding to each catalog entity
167
	 */
168
	private List<String> getSnippets(List<Long> catalogIds) {
169
		List<String> snippets = new ArrayList<String>();
170
		if(catalogIds == null)
171
			return snippets;
172
		for(Long item: catalogIds){
173
			snippets.add(ContentServingService.getSnippet(SnippetType.HOME_SNIPPET, item+"", sourceId));
174
		}
175
		return snippets;
176
	}
177
 
178
 
179
 
180
	@Override
3830 chandransh 181
	public String getHeaderSnippet() {
182
		String url = request.getQueryString();
183
		if (url == null) {
184
			url = "";
185
		} else {
186
			url = "?" + url;
187
		}
188
		url = request.getRequestURI() + url;
11829 amit.gupta 189
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(),userinfo.getTotalItems(), url , 0, true, userinfo.isPrivateDealUser());
3830 chandransh 190
	}
8917 kshitij.so 191
	@Override
6708 kshitij.so 192
	public String getCartWidgetSnippet() {
9155 kshitij.so 193
		return pageLoader.getCartWidgetSnippet(userinfo.getTotalItems(), userinfo.getTotalAmount(),0, activeBanners.get("side-banner"),sideBannersMap);
6708 kshitij.so 194
	}
3903 varun.gupt 195
}