Subversion Repositories SmartDukaan

Rev

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