Subversion Repositories SmartDukaan

Rev

Rev 6902 | Rev 8602 | 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;
43
 
507 rajveer 44
	public HomeController(){
45
		super();
46
	}
47
 
2930 chandransh 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("/")
650 rajveer 57
    public String index() throws SecurityException, IOException {
2930 chandransh 58
    	logger.debug("userinfo:" + userinfo.toString());
59
    	logger.info("Rendering the home page");
3050 vikas 60
    	setSnippets();
3185 vikas 61
    	DataLogger.logData(EventType.HOME_PAGE, getSessionId(), userinfo.getUserId(), userinfo.getEmail());
650 rajveer 62
    	return "index";
507 rajveer 63
    }
2578 chandransh 64
 
2930 chandransh 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
     */
74
	@Action("/refresh-home")
75
	public String destroy() throws SecurityException, IOException{
3050 vikas 76
        logger.info("Reloading best deals, best sellers and latest arrivals");
77
        EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
3112 vikas 78
                EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
3050 vikas 79
        homeSnippetCache.removeAll();
2930 chandransh 80
	    setSnippets();
81
	    return "index";
2578 chandransh 82
	}
2930 chandransh 83
 
2578 chandransh 84
	public List<String> getBestDealSnippets(){
85
		return bestDealSnippets; 
507 rajveer 86
	}
87
 
2578 chandransh 88
	public List<String> getBestSellerSnippets(){
89
		return bestSellerSnippets; 
507 rajveer 90
	}
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
	 */
100
    private void setSnippets() {
3050 vikas 101
        EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
3112 vikas 102
                EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
8578 kshitij.so 103
        logger.info("Source id "+sourceId);
3561 rajveer 104
        if(sourceId == -1){
105
        	bestDealSnippets = homeSnippetCache.get(BEST_DEALS_CACHE_KEY);
106
        	bestSellerSnippets = homeSnippetCache.get(BEST_SELLERS_CACHE_KEY);
107
        	latestArrivalSnippets = homeSnippetCache.get(LATEST_ARRIVALS_CACHE_KEY);
3050 vikas 108
 
3561 rajveer 109
	        if (bestDealSnippets != null && bestSellerSnippets != null && latestArrivalSnippets != null) {
110
	            return;
111
	        }
3050 vikas 112
        }
3561 rajveer 113
 
2930 chandransh 114
        List<Long> bestDealCatalogIds = null;
115
        List<Long> bestSellerCatalogIds = null;
116
        List<Long> latestArrivalCatalogIds = null;
117
 
118
        int bestSellerCount = 4;
119
        int latestArrivalCount = 4;
120
 
121
        try {
8578 kshitij.so 122
        	activeBanners=null;
123
    	    allBannersMap=null;
124
    	    setBanners();
3126 rajveer 125
            CatalogClient catalogServiceClient = new CatalogClient();
2930 chandransh 126
            Client client = catalogServiceClient.getClient();
127
            //Get top 4 best deals 
128
            bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
129
 
130
            //Get top 8 best sellers b'coz 4 of them may overlap with best deals.
131
            bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
132
            bestSellerCatalogIds.removeAll(bestDealCatalogIds);
133
            if(bestSellerCatalogIds.size() < bestSellerCount)
134
                bestSellerCount = bestSellerCatalogIds.size();
135
 
136
            //Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
137
            //while another 4 may overlap with best sellers.
5073 rajveer 138
            latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, Arrays.asList(new Long[]{ 10002L, 10003L, 10004L, 10010L, 10050L}));
2930 chandransh 139
            latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
140
            latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
141
            if(latestArrivalCatalogIds.size() < latestArrivalCount)
142
                latestArrivalCount = latestArrivalCatalogIds.size();
5945 mandeep.dh 143
        } catch (CatalogServiceException e) {
2930 chandransh 144
            logger.error("Error while fetching data from the catalog service", e);
145
        } catch (TException e) {
146
            logger.error("Error while fetching data from the catalog service", e);
147
        } catch (Exception e) {
148
            logger.error("Unexpected exception", e);
149
        }
150
 
151
        bestDealSnippets = getSnippets(bestDealCatalogIds);
152
        bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
3169 chandransh 153
        latestArrivalSnippets = getSnippets(latestArrivalCatalogIds.subList(0, latestArrivalCount));
3561 rajveer 154
        if(sourceId == -1){
155
	        if (!bestDealSnippets.isEmpty() && !bestSellerSnippets.isEmpty() && !latestArrivalSnippets.isEmpty()) {
156
	            homeSnippetCache.put(BEST_DEALS_CACHE_KEY, bestDealSnippets);
157
	            homeSnippetCache.put(BEST_SELLERS_CACHE_KEY, bestSellerSnippets);
158
	            homeSnippetCache.put(LATEST_ARRIVALS_CACHE_KEY, latestArrivalSnippets);
159
	        }
3185 vikas 160
        }
2930 chandransh 161
    }
162
 
163
    /**
164
     * Returns a list of snippets to be used on the home page corresponding to
165
     * the given catalog ids. The snippets are in the same order as the ids in
166
     * the input list. In case a snippet is not found for an entity, this method
167
     * simply logs the problem and moves on.
168
     * 
169
     * @param catalogIds
170
     *            Ids of the entities which we want to show.
171
     * @return The snippet corresponding to each catalog entity
172
     */
173
    private List<String> getSnippets(List<Long> catalogIds) {
174
        List<String> snippets = new ArrayList<String>();
175
        if(catalogIds == null)
176
            return snippets;
177
        for(Long item: catalogIds){
3561 rajveer 178
        	snippets.add(ContentServingService.getSnippet(SnippetType.HOME_SNIPPET, item+"", sourceId));
2930 chandransh 179
        }
180
        return snippets;
181
    }
3830 chandransh 182
 
6885 kshitij.so 183
 
184
 
3830 chandransh 185
    @Override
186
	public String getHeaderSnippet() {
187
		String url = request.getQueryString();
188
		if (url == null) {
189
			url = "";
190
		} else {
191
			url = "?" + url;
192
		}
193
		url = request.getRequestURI() + url;
6152 amit.gupta 194
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(),userinfo.getTotalItems(), url , 0, true);
3830 chandransh 195
	}
6708 kshitij.so 196
    @Override
197
	public String getCartWidgetSnippet() {
198
		return pageLoader.getCartWidgetSnippet(userinfo.getTotalItems(), userinfo.getTotalAmount(),0);
199
	}
3903 varun.gupt 200
}