Subversion Repositories SmartDukaan

Rev

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