Subversion Repositories SmartDukaan

Rev

Rev 8602 | Rev 9155 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8602 Rev 8917
Line 30... Line 30...
30
 
30
 
31
	private static Logger logger = Logger.getLogger(Class.class);
31
	private static Logger logger = Logger.getLogger(Class.class);
32
	private static final String BEST_DEALS_CACHE_KEY = "home_best_deals_snippet";
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";
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";
34
	private static final String LATEST_ARRIVALS_CACHE_KEY = "home_latest_arrivals_snippet";
35
	
35
 
36
	private List<String> bestDealSnippets = null;
36
	private List<String> bestDealSnippets = null;
37
	private List<String> bestSellerSnippets = null;
37
	private List<String> bestSellerSnippets = null;
38
	private List<String> latestArrivalSnippets = null;
38
	private List<String> latestArrivalSnippets = null;
39
	
39
 
40
	public HomeController(){
40
	public HomeController(){
41
		super();
41
		super();
42
	}
42
	}
43
 
43
 
44
    /**
44
	/**
45
     * Renders the home page. Loads the snippets for best deals, best sellers
45
	 * Renders the home page. Loads the snippets for best deals, best sellers
46
     * and latest arrivals if they haven't been loaded already.
46
	 * and latest arrivals if they haven't been loaded already.
47
     * 
47
	 * 
48
     * @return Name of the view to render.
48
	 * @return Name of the view to render.
49
     * @throws SecurityException
49
	 * @throws SecurityException
50
     * @throws IOException
50
	 * @throws IOException
51
     */
51
	 */
52
	@Action("/")
52
	@Action("/")
53
    public String index() throws SecurityException, IOException {
53
	public String index() throws SecurityException, IOException {
54
    	logger.debug("userinfo:" + userinfo.toString());
54
		logger.debug("userinfo:" + userinfo.toString());
55
    	logger.info("Rendering the home page");
55
		logger.info("Rendering the home page");
56
    	setSnippets();
56
		setSnippets();
57
    	DataLogger.logData(EventType.HOME_PAGE, getSessionId(), userinfo.getUserId(), userinfo.getEmail());
57
		DataLogger.logData(EventType.HOME_PAGE, getSessionId(), userinfo.getUserId(), userinfo.getEmail());
58
    	return "index";
58
		return "index";
59
    }
59
	}
60
 
60
 
61
    /**
61
	/**
62
     * Renders the home page but always loads the snippets for best deals, best
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
63
	 * sellers and latest arrivals unconditionally. Should be used to reset the
64
     * snippets.
64
	 * snippets.
65
     * 
65
	 * 
66
     * @return Name of the view to render.
66
	 * @return Name of the view to render.
67
     * @throws SecurityException
67
	 * @throws SecurityException
68
     * @throws IOException
68
	 * @throws IOException
69
     */
69
	 */
70
	@Action("/refresh-home")
70
	@Action("/refresh-home")
71
	public String destroy() throws SecurityException, IOException{
71
	public String destroy() throws SecurityException, IOException{
72
        logger.info("Reloading best deals, best sellers and latest arrivals");
72
		logger.info("Reloading best deals, best sellers and latest arrivals");
73
        EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
73
		EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
74
                EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
74
				EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
75
        homeSnippetCache.removeAll();
75
		homeSnippetCache.removeAll();
76
	    setSnippets();
76
		setSnippets();
77
	    return "index";
77
		return "index";
78
	}
78
	}
79
	
79
 
80
	public List<String> getBestDealSnippets(){
80
	public List<String> getBestDealSnippets(){
81
		return bestDealSnippets; 
81
		return bestDealSnippets; 
82
	}
82
	}
83
	
83
 
84
	public List<String> getBestSellerSnippets(){
84
	public List<String> getBestSellerSnippets(){
85
		return bestSellerSnippets; 
85
		return bestSellerSnippets; 
86
	}
86
	}
87
	
87
 
88
	public List<String> getLatestArrivalSnippets(){
88
	public List<String> getLatestArrivalSnippets(){
89
		return latestArrivalSnippets;
89
		return latestArrivalSnippets;
90
	}
90
	}
91
 
91
 
92
	/**
92
	/**
93
	 * Loads the snippets for best deals, best sellers and latest arrivals
93
	 * Loads the snippets for best deals, best sellers and latest arrivals
94
	 * into memory.
94
	 * into memory.
95
	 */
95
	 */
96
    private void setSnippets() {
96
	private void setSnippets() {
97
        EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
97
		EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
98
                EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
98
				EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
99
        logger.info("Source id "+sourceId);
99
		logger.info("Source id "+sourceId);
100
        if(sourceId == -1){
100
		if(sourceId == -1){
101
        	bestDealSnippets = homeSnippetCache.get(BEST_DEALS_CACHE_KEY);
101
			bestDealSnippets = homeSnippetCache.get(BEST_DEALS_CACHE_KEY);
102
        	bestSellerSnippets = homeSnippetCache.get(BEST_SELLERS_CACHE_KEY);
102
			bestSellerSnippets = homeSnippetCache.get(BEST_SELLERS_CACHE_KEY);
103
        	latestArrivalSnippets = homeSnippetCache.get(LATEST_ARRIVALS_CACHE_KEY);
103
			latestArrivalSnippets = homeSnippetCache.get(LATEST_ARRIVALS_CACHE_KEY);
104
        
104
 
105
	        if (bestDealSnippets != null && bestSellerSnippets != null && latestArrivalSnippets != null) {
105
			if (bestDealSnippets != null && bestSellerSnippets != null && latestArrivalSnippets != null) {
106
	            return;
106
				logger.info("returning in set snippets");
107
	        }
107
				return;
108
        }
108
			}
109
        
109
		}
-
 
110
 
110
        List<Long> bestDealCatalogIds = null;
111
		List<Long> bestDealCatalogIds = null;
111
        List<Long> bestSellerCatalogIds = null;
112
		List<Long> bestSellerCatalogIds = null;
112
        List<Long> latestArrivalCatalogIds = null;
113
		List<Long> latestArrivalCatalogIds = null;
113
        
114
 
114
        int bestSellerCount = 4;
115
		int bestSellerCount = 4;
115
        int latestArrivalCount = 4;
116
		int latestArrivalCount = 4;
116
        
117
 
117
        try {
118
		try {
118
        	activeBanners=null;
119
			activeBanners=null;
119
    	    allBannersMap=null;
120
			allBannersMap=null;
120
    	    setBanners();
121
			setBanners();
121
            CatalogClient catalogServiceClient = new CatalogClient();
122
			CatalogClient catalogServiceClient = new CatalogClient();
122
            Client client = catalogServiceClient.getClient();
123
			Client client = catalogServiceClient.getClient();
123
            //Get top 4 best deals 
124
			//Get top 4 best deals 
124
            bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
125
			bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
125
            
126
 
126
            //Get top 8 best sellers b'coz 4 of them may overlap with best deals.
127
			//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 = client.getBestSellersCatalogIds(0, 8, null, -1);
128
            bestSellerCatalogIds.removeAll(bestDealCatalogIds);
129
			bestSellerCatalogIds.removeAll(bestDealCatalogIds);
129
            if(bestSellerCatalogIds.size() < bestSellerCount)
130
			if(bestSellerCatalogIds.size() < bestSellerCount)
130
                bestSellerCount = bestSellerCatalogIds.size();
131
				bestSellerCount = bestSellerCatalogIds.size();
131
            
132
 
132
            //Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
133
			//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
			//while another 4 may overlap with best sellers.
134
            latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, Arrays.asList(new Long[]{ 10006L, 10010L, 10050L}));
135
			latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, Arrays.asList(new Long[]{ 10006L, 10010L, 10050L}));
135
            latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
136
			latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
136
            latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
137
			latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
137
            if(latestArrivalCatalogIds.size() < latestArrivalCount)
138
			if(latestArrivalCatalogIds.size() < latestArrivalCount)
138
                latestArrivalCount = latestArrivalCatalogIds.size();
139
				latestArrivalCount = latestArrivalCatalogIds.size();
139
        } catch (CatalogServiceException e) {
140
		} catch (CatalogServiceException e) {
140
            logger.error("Error while fetching data from the catalog service", e);
141
			logger.error("Error while fetching data from the catalog service", e);
141
        } catch (TException e) {
142
		} catch (TException e) {
142
            logger.error("Error while fetching data from the catalog service", e);
143
			logger.error("Error while fetching data from the catalog service", e);
143
        } catch (Exception e) {
144
		} catch (Exception e) {
144
            logger.error("Unexpected exception", e);
145
			logger.error("Unexpected exception", e);
145
        }
146
		}
146
        
147
 
147
        bestDealSnippets = getSnippets(bestDealCatalogIds);
148
		bestDealSnippets = getSnippets(bestDealCatalogIds);
148
        bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
149
		bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
149
        latestArrivalSnippets = getSnippets(latestArrivalCatalogIds.subList(0, latestArrivalCount));
150
		latestArrivalSnippets = getSnippets(latestArrivalCatalogIds.subList(0, latestArrivalCount));
150
        if(sourceId == -1){
151
		if(sourceId == -1){
151
	        if (!bestDealSnippets.isEmpty() && !bestSellerSnippets.isEmpty() && !latestArrivalSnippets.isEmpty()) {
152
			if (!bestDealSnippets.isEmpty() && !bestSellerSnippets.isEmpty() && !latestArrivalSnippets.isEmpty()) {
152
	            homeSnippetCache.put(BEST_DEALS_CACHE_KEY, bestDealSnippets);
153
				homeSnippetCache.put(BEST_DEALS_CACHE_KEY, bestDealSnippets);
153
	            homeSnippetCache.put(BEST_SELLERS_CACHE_KEY, bestSellerSnippets);
154
				homeSnippetCache.put(BEST_SELLERS_CACHE_KEY, bestSellerSnippets);
154
	            homeSnippetCache.put(LATEST_ARRIVALS_CACHE_KEY, latestArrivalSnippets);
155
				homeSnippetCache.put(LATEST_ARRIVALS_CACHE_KEY, latestArrivalSnippets);
155
	        }
156
			}
156
        }
157
		}
157
    }
158
	}
158
	
159
 
159
    /**
160
	/**
160
     * Returns a list of snippets to be used on the home page corresponding to
161
	 * 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 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
	 * the input list. In case a snippet is not found for an entity, this method
163
     * simply logs the problem and moves on.
164
	 * simply logs the problem and moves on.
164
     * 
165
	 * 
165
     * @param catalogIds
166
	 * @param catalogIds
166
     *            Ids of the entities which we want to show.
167
	 *            Ids of the entities which we want to show.
167
     * @return The snippet corresponding to each catalog entity
168
	 * @return The snippet corresponding to each catalog entity
168
     */
169
	 */
169
    private List<String> getSnippets(List<Long> catalogIds) {
170
	private List<String> getSnippets(List<Long> catalogIds) {
170
        List<String> snippets = new ArrayList<String>();
171
		List<String> snippets = new ArrayList<String>();
171
        if(catalogIds == null)
172
		if(catalogIds == null)
172
            return snippets;
173
			return snippets;
173
        for(Long item: catalogIds){
174
		for(Long item: catalogIds){
174
        	snippets.add(ContentServingService.getSnippet(SnippetType.HOME_SNIPPET, item+"", sourceId));
175
			snippets.add(ContentServingService.getSnippet(SnippetType.HOME_SNIPPET, item+"", sourceId));
175
        }
176
		}
176
        return snippets;
177
		return snippets;
177
    }
178
	}
178
    
179
 
179
    
180
 
180
    
181
 
181
    @Override
182
	@Override
182
	public String getHeaderSnippet() {
183
	public String getHeaderSnippet() {
183
		String url = request.getQueryString();
184
		String url = request.getQueryString();
184
		if (url == null) {
185
		if (url == null) {
185
			url = "";
186
			url = "";
186
		} else {
187
		} else {
187
			url = "?" + url;
188
			url = "?" + url;
188
		}
189
		}
189
		url = request.getRequestURI() + url;
190
		url = request.getRequestURI() + url;
190
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(),userinfo.getTotalItems(), url , 0, true);
191
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(),userinfo.getTotalItems(), url , 0, true);
191
	}
192
	}
192
    @Override
193
	@Override
193
	public String getCartWidgetSnippet() {
194
	public String getCartWidgetSnippet() {
194
		return pageLoader.getCartWidgetSnippet(userinfo.getTotalItems(), userinfo.getTotalAmount(),0);
195
		return pageLoader.getCartWidgetSnippet(userinfo.getTotalItems(), userinfo.getTotalAmount(),0, activeBanners.get("side-banner"),allBannersMap);
195
	}
196
	}
196
}
197
}
197
198