Subversion Repositories SmartDukaan

Rev

Rev 2989 | Rev 3112 | 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;
9
import in.shop2020.thrift.clients.CatalogServiceClient;
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 CACHE_NAME = "HomePageSnippets";
33
	private static final String BEST_DEALS_CACHE_KEY = "home_best_deals_snippet";
34
	private static final String BEST_SELLERS_CACHE_KEY = "home_best_sellers_snippet";
35
	private static final String LATEST_ARRIVALS_CACHE_KEY = "home_latest_arrivals_snippet";
36
 
507 rajveer 37
 
2578 chandransh 38
 
3050 vikas 39
	private List<String> bestDealSnippets = null;
40
	private List<String> bestSellerSnippets = null;
41
	private List<String> latestArrivalSnippets = null;
42
 
507 rajveer 43
	public HomeController(){
44
		super();
45
	}
46
 
2930 chandransh 47
    /**
48
     * Renders the home page. Loads the snippets for best deals, best sellers
49
     * and latest arrivals if they haven't been loaded already.
50
     * 
51
     * @return Name of the view to render.
52
     * @throws SecurityException
53
     * @throws IOException
54
     */
773 rajveer 55
	@Action("/")
650 rajveer 56
    public String index() throws SecurityException, IOException {
2930 chandransh 57
    	logger.debug("userinfo:" + userinfo.toString());
58
    	logger.info("Rendering the home page");
3050 vikas 59
    	setSnippets();
2989 vikas 60
    	DataLogger.logData(EventType.HOME_PAGE, session.getId(), userinfo.getUserId(), userinfo.getEmail());
650 rajveer 61
    	return "index";
507 rajveer 62
    }
2578 chandransh 63
 
2930 chandransh 64
    /**
65
     * Renders the home page but always loads the snippets for best deals, best
66
     * sellers and latest arrivals unconditionally. Should be used to reset the
67
     * snippets.
68
     * 
69
     * @return Name of the view to render.
70
     * @throws SecurityException
71
     * @throws IOException
72
     */
73
	@Action("/refresh-home")
74
	public String destroy() throws SecurityException, IOException{
3050 vikas 75
        logger.info("Reloading best deals, best sellers and latest arrivals");
76
        EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
77
                CACHE_NAME, CacheManager.create());
78
        homeSnippetCache.removeAll();
2930 chandransh 79
	    setSnippets();
80
	    return "index";
2578 chandransh 81
	}
2930 chandransh 82
 
2578 chandransh 83
	public List<String> getBestDealSnippets(){
84
		return bestDealSnippets; 
507 rajveer 85
	}
86
 
2578 chandransh 87
	public List<String> getBestSellerSnippets(){
88
		return bestSellerSnippets; 
507 rajveer 89
	}
90
 
2578 chandransh 91
	public List<String> getLatestArrivalSnippets(){
92
		return latestArrivalSnippets;
507 rajveer 93
	}
2930 chandransh 94
 
95
	/**
96
	 * Loads the snippets for best deals, best sellers and latest arrivals
97
	 * into memory.
98
	 */
99
    private void setSnippets() {
3050 vikas 100
        EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
101
                CACHE_NAME, CacheManager.create());
102
        bestDealSnippets = homeSnippetCache.get(BEST_DEALS_CACHE_KEY);
103
        bestSellerSnippets = homeSnippetCache.get(BEST_SELLERS_CACHE_KEY);
104
        latestArrivalSnippets = homeSnippetCache.get(LATEST_ARRIVALS_CACHE_KEY);
105
 
106
        if (bestDealSnippets != null && bestSellerSnippets != null && latestArrivalSnippets != null) {
107
            return;
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 {
117
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
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.
2975 chandransh 131
            List<Long> latestArrivalCategories = new ArrayList<Long>();
132
            latestArrivalCategories.add(10002L);
133
            latestArrivalCategories.add(10003L);
134
            latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, latestArrivalCategories);
2930 chandransh 135
            latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
136
            latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
137
            if(latestArrivalCatalogIds.size() < latestArrivalCount)
138
                latestArrivalCount = latestArrivalCatalogIds.size();
139
        } catch (InventoryServiceException e) {
140
            logger.error("Error while fetching data from the catalog service", e);
141
        } catch (TException e) {
142
            logger.error("Error while fetching data from the catalog service", e);
143
        } catch (Exception e) {
144
            logger.error("Unexpected exception", e);
145
        }
146
 
147
        bestDealSnippets = getSnippets(bestDealCatalogIds);
148
        bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
149
        latestArrivalSnippets = getSnippets(latestArrivalCatalogIds).subList(0, latestArrivalCount);
3050 vikas 150
        homeSnippetCache.put(BEST_DEALS_CACHE_KEY, bestDealSnippets);
151
        homeSnippetCache.put(BEST_SELLERS_CACHE_KEY, bestSellerSnippets);
152
        homeSnippetCache.put(LATEST_ARRIVALS_CACHE_KEY, latestArrivalSnippets);
2930 chandransh 153
    }
154
 
155
    /**
156
     * Returns a list of snippets to be used on the home page corresponding to
157
     * the given catalog ids. The snippets are in the same order as the ids in
158
     * the input list. In case a snippet is not found for an entity, this method
159
     * simply logs the problem and moves on.
160
     * 
161
     * @param catalogIds
162
     *            Ids of the entities which we want to show.
163
     * @return The snippet corresponding to each catalog entity
164
     */
165
    private List<String> getSnippets(List<Long> catalogIds) {
166
        List<String> snippets = new ArrayList<String>();
167
        if(catalogIds == null)
168
            return snippets;
169
        for(Long item: catalogIds){
170
            try{
171
                snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
172
            }catch(IOException ioex){
173
                logger.error("Unable to get home page snippet for " + item, ioex);
174
            }
175
        }
176
        return snippets;
177
    }
507 rajveer 178
}
3050 vikas 179