Subversion Repositories SmartDukaan

Rev

Rev 3169 | Rev 3242 | 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;
4
import in.shop2020.model.v1.catalog.InventoryService.Client;
2578 chandransh 5
import in.shop2020.model.v1.catalog.InventoryServiceException;
3050 vikas 6
import in.shop2020.serving.utils.EhcacheWrapper;
2578 chandransh 7
import in.shop2020.serving.utils.FileUtils;
8
import in.shop2020.serving.utils.Utils;
3126 rajveer 9
import in.shop2020.thrift.clients.CatalogClient;
2989 vikas 10
import in.shop2020.utils.DataLogger;
507 rajveer 11
 
2578 chandransh 12
import java.io.File;
507 rajveer 13
import java.io.IOException;
2578 chandransh 14
import java.util.ArrayList;
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());
3050 vikas 99
        bestDealSnippets = homeSnippetCache.get(BEST_DEALS_CACHE_KEY);
100
        bestSellerSnippets = homeSnippetCache.get(BEST_SELLERS_CACHE_KEY);
101
        latestArrivalSnippets = homeSnippetCache.get(LATEST_ARRIVALS_CACHE_KEY);
102
 
103
        if (bestDealSnippets != null && bestSellerSnippets != null && latestArrivalSnippets != null) {
104
            return;
105
        }
2930 chandransh 106
        List<Long> bestDealCatalogIds = null;
107
        List<Long> bestSellerCatalogIds = null;
108
        List<Long> latestArrivalCatalogIds = null;
109
 
110
        int bestSellerCount = 4;
111
        int latestArrivalCount = 4;
112
 
113
        try {
3126 rajveer 114
            CatalogClient catalogServiceClient = new CatalogClient();
2930 chandransh 115
            Client client = catalogServiceClient.getClient();
116
 
117
            //Get top 4 best deals 
118
            bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
119
 
120
            //Get top 8 best sellers b'coz 4 of them may overlap with best deals.
121
            bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
122
            bestSellerCatalogIds.removeAll(bestDealCatalogIds);
123
            if(bestSellerCatalogIds.size() < bestSellerCount)
124
                bestSellerCount = bestSellerCatalogIds.size();
125
 
126
            //Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
127
            //while another 4 may overlap with best sellers.
2975 chandransh 128
            List<Long> latestArrivalCategories = new ArrayList<Long>();
129
            latestArrivalCategories.add(10002L);
130
            latestArrivalCategories.add(10003L);
131
            latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, latestArrivalCategories);
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();
136
        } catch (InventoryServiceException e) {
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));
3185 vikas 147
        if (!bestDealSnippets.isEmpty() && !bestSellerSnippets.isEmpty() && !latestArrivalSnippets.isEmpty()) {
148
            homeSnippetCache.put(BEST_DEALS_CACHE_KEY, bestDealSnippets);
149
            homeSnippetCache.put(BEST_SELLERS_CACHE_KEY, bestSellerSnippets);
150
            homeSnippetCache.put(LATEST_ARRIVALS_CACHE_KEY, latestArrivalSnippets);
151
        }
2930 chandransh 152
    }
153
 
154
    /**
155
     * Returns a list of snippets to be used on the home page corresponding to
156
     * the given catalog ids. The snippets are in the same order as the ids in
157
     * the input list. In case a snippet is not found for an entity, this method
158
     * simply logs the problem and moves on.
159
     * 
160
     * @param catalogIds
161
     *            Ids of the entities which we want to show.
162
     * @return The snippet corresponding to each catalog entity
163
     */
164
    private List<String> getSnippets(List<Long> catalogIds) {
165
        List<String> snippets = new ArrayList<String>();
166
        if(catalogIds == null)
167
            return snippets;
168
        for(Long item: catalogIds){
169
            try{
170
                snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
171
            }catch(IOException ioex){
172
                logger.error("Unable to get home page snippet for " + item, ioex);
173
            }
174
        }
175
        return snippets;
176
    }
507 rajveer 177
}
3050 vikas 178