Subversion Repositories SmartDukaan

Rev

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