Subversion Repositories SmartDukaan

Rev

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