Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8842 anupam.sin 1
package in.shop2020.serving.controllers;
2
 
3
import in.shop2020.datalogger.EventType;
4
import in.shop2020.model.v1.catalog.CatalogService.Client;
5
import in.shop2020.model.v1.catalog.CatalogServiceException;
6
import in.shop2020.serving.cache.EhcacheWrapper;
7
import in.shop2020.serving.services.ContentServingService;
8
import in.shop2020.serving.utils.SnippetType;
9
import in.shop2020.thrift.clients.CatalogClient;
10
import in.shop2020.utils.DataLogger;
11
 
12
import java.io.IOException;
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.List;
16
 
17
import net.sf.ehcache.CacheManager;
18
 
19
import org.apache.log4j.Logger;
20
import org.apache.struts2.convention.annotation.Action;
21
import org.apache.thrift.TException;
22
 
23
/**
24
 * 
25
 * @author chandranshu
26
 *
27
 */
28
@SuppressWarnings("serial")
29
public class HomeController extends BaseController {
30
 
31
	private static Logger logger = Logger.getLogger(Class.class);
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";
35
 
36
	private List<String> bestDealSnippets = null;
37
	private List<String> bestSellerSnippets = null;
38
	private List<String> latestArrivalSnippets = null;
39
 
40
	public HomeController(){
41
		super();
42
	}
43
 
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
     */
52
	@Action("/")
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
    }
60
 
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
     */
70
	@Action("/refresh-home")
71
	public String destroy() throws SecurityException, IOException{
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";
78
	}
79
 
80
	public List<String> getBestDealSnippets(){
81
		return bestDealSnippets; 
82
	}
83
 
84
	public List<String> getBestSellerSnippets(){
85
		return bestSellerSnippets; 
86
	}
87
 
88
	public List<String> getLatestArrivalSnippets(){
89
		return latestArrivalSnippets;
90
	}
91
 
92
	/**
93
	 * Loads the snippets for best deals, best sellers and latest arrivals
94
	 * into memory.
95
	 */
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
	            return;
107
	        }
108
        }
109
 
110
        List<Long> bestDealCatalogIds = null;
111
        List<Long> bestSellerCatalogIds = null;
112
        List<Long> latestArrivalCatalogIds = null;
113
 
114
        int bestSellerCount = 4;
115
        int latestArrivalCount = 4;
116
 
117
        try {
118
        	activeBanners=null;
119
    	    allBannersMap=null;
120
    	    setBanners();
121
            CatalogClient catalogServiceClient = new CatalogClient();
122
            Client client = catalogServiceClient.getClient();
123
            //Get top 4 best deals 
124
            bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
125
 
126
            //Get top 8 best sellers b'coz 4 of them may overlap with best deals.
127
            bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
128
            bestSellerCatalogIds.removeAll(bestDealCatalogIds);
129
            if(bestSellerCatalogIds.size() < bestSellerCount)
130
                bestSellerCount = bestSellerCatalogIds.size();
131
 
132
            //Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
133
            //while another 4 may overlap with best sellers.
134
            latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, Arrays.asList(new Long[]{ 10006L, 10010L, 10050L}));
135
            latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
136
            latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
137
            if(latestArrivalCatalogIds.size() < latestArrivalCount)
138
                latestArrivalCount = latestArrivalCatalogIds.size();
139
        } catch (CatalogServiceException e) {
140
            logger.error("Error while fetching data from the catalog service", e);
141
        } catch (TException e) {
142
            logger.error("Error while fetching data from the catalog service", e);
143
        } catch (Exception e) {
144
            logger.error("Unexpected exception", e);
145
        }
146
 
147
        bestDealSnippets = getSnippets(bestDealCatalogIds);
148
        bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
149
        latestArrivalSnippets = getSnippets(latestArrivalCatalogIds.subList(0, latestArrivalCount));
150
        if(sourceId == -1){
151
	        if (!bestDealSnippets.isEmpty() && !bestSellerSnippets.isEmpty() && !latestArrivalSnippets.isEmpty()) {
152
	            homeSnippetCache.put(BEST_DEALS_CACHE_KEY, bestDealSnippets);
153
	            homeSnippetCache.put(BEST_SELLERS_CACHE_KEY, bestSellerSnippets);
154
	            homeSnippetCache.put(LATEST_ARRIVALS_CACHE_KEY, latestArrivalSnippets);
155
	        }
156
        }
157
    }
158
 
159
    /**
160
     * Returns a list of snippets to be used on the home page corresponding to
161
     * the given catalog ids. The snippets are in the same order as the ids in
162
     * the input list. In case a snippet is not found for an entity, this method
163
     * simply logs the problem and moves on.
164
     * 
165
     * @param catalogIds
166
     *            Ids of the entities which we want to show.
167
     * @return The snippet corresponding to each catalog entity
168
     */
169
    private List<String> getSnippets(List<Long> catalogIds) {
170
        List<String> snippets = new ArrayList<String>();
171
        if(catalogIds == null)
172
            return snippets;
173
        for(Long item: catalogIds){
174
        	snippets.add(ContentServingService.getSnippet(SnippetType.HOME_SNIPPET, item+"", sourceId));
175
        }
176
        return snippets;
177
    }
178
 
179
 
180
 
181
    @Override
182
	public String getHeaderSnippet() {
183
		String url = request.getQueryString();
184
		if (url == null) {
185
			url = "";
186
		} else {
187
			url = "?" + url;
188
		}
189
		url = request.getRequestURI() + url;
190
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(),userinfo.getTotalItems(), url , 0, true);
191
	}
192
    @Override
193
	public String getCartWidgetSnippet() {
194
		return pageLoader.getCartWidgetSnippet(userinfo.getTotalItems(), userinfo.getTotalAmount(),0);
195
	}
196
}