Subversion Repositories SmartDukaan

Rev

Rev 3242 | Rev 3561 | 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;
3242 vikas 6
import in.shop2020.serving.cache.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;
3266 chandransh 15
import java.util.Arrays;
2578 chandransh 16
import java.util.List;
507 rajveer 17
 
3050 vikas 18
import net.sf.ehcache.CacheManager;
19
 
832 rajveer 20
import org.apache.log4j.Logger;
773 rajveer 21
import org.apache.struts2.convention.annotation.Action;
2578 chandransh 22
import org.apache.thrift.TException;
507 rajveer 23
 
24
/**
25
 * 
2930 chandransh 26
 * @author chandranshu
507 rajveer 27
 *
28
 */
2578 chandransh 29
@SuppressWarnings("serial")
650 rajveer 30
public class HomeController extends BaseController {
31
 
2578 chandransh 32
	private static Logger logger = Logger.getLogger(Class.class);
3050 vikas 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";
507 rajveer 36
 
3050 vikas 37
	private List<String> bestDealSnippets = null;
38
	private List<String> bestSellerSnippets = null;
39
	private List<String> latestArrivalSnippets = null;
40
 
507 rajveer 41
	public HomeController(){
42
		super();
43
	}
44
 
2930 chandransh 45
    /**
46
     * Renders the home page. Loads the snippets for best deals, best sellers
47
     * and latest arrivals if they haven't been loaded already.
48
     * 
49
     * @return Name of the view to render.
50
     * @throws SecurityException
51
     * @throws IOException
52
     */
773 rajveer 53
	@Action("/")
650 rajveer 54
    public String index() throws SecurityException, IOException {
2930 chandransh 55
    	logger.debug("userinfo:" + userinfo.toString());
56
    	logger.info("Rendering the home page");
3050 vikas 57
    	setSnippets();
3185 vikas 58
    	DataLogger.logData(EventType.HOME_PAGE, getSessionId(), userinfo.getUserId(), userinfo.getEmail());
650 rajveer 59
    	return "index";
507 rajveer 60
    }
2578 chandransh 61
 
2930 chandransh 62
    /**
63
     * Renders the home page but always loads the snippets for best deals, best
64
     * sellers and latest arrivals unconditionally. Should be used to reset the
65
     * snippets.
66
     * 
67
     * @return Name of the view to render.
68
     * @throws SecurityException
69
     * @throws IOException
70
     */
71
	@Action("/refresh-home")
72
	public String destroy() throws SecurityException, IOException{
3050 vikas 73
        logger.info("Reloading best deals, best sellers and latest arrivals");
74
        EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
3112 vikas 75
                EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
3050 vikas 76
        homeSnippetCache.removeAll();
2930 chandransh 77
	    setSnippets();
78
	    return "index";
2578 chandransh 79
	}
2930 chandransh 80
 
2578 chandransh 81
	public List<String> getBestDealSnippets(){
82
		return bestDealSnippets; 
507 rajveer 83
	}
84
 
2578 chandransh 85
	public List<String> getBestSellerSnippets(){
86
		return bestSellerSnippets; 
507 rajveer 87
	}
88
 
2578 chandransh 89
	public List<String> getLatestArrivalSnippets(){
90
		return latestArrivalSnippets;
507 rajveer 91
	}
2930 chandransh 92
 
93
	/**
94
	 * Loads the snippets for best deals, best sellers and latest arrivals
95
	 * into memory.
96
	 */
97
    private void setSnippets() {
3050 vikas 98
        EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
3112 vikas 99
                EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
3050 vikas 100
        bestDealSnippets = homeSnippetCache.get(BEST_DEALS_CACHE_KEY);
101
        bestSellerSnippets = homeSnippetCache.get(BEST_SELLERS_CACHE_KEY);
102
        latestArrivalSnippets = homeSnippetCache.get(LATEST_ARRIVALS_CACHE_KEY);
103
 
104
        if (bestDealSnippets != null && bestSellerSnippets != null && latestArrivalSnippets != null) {
105
            return;
106
        }
2930 chandransh 107
        List<Long> bestDealCatalogIds = null;
108
        List<Long> bestSellerCatalogIds = null;
109
        List<Long> latestArrivalCatalogIds = null;
110
 
111
        int bestSellerCount = 4;
112
        int latestArrivalCount = 4;
113
 
114
        try {
3126 rajveer 115
            CatalogClient catalogServiceClient = new CatalogClient();
2930 chandransh 116
            Client client = catalogServiceClient.getClient();
117
 
118
            //Get top 4 best deals 
119
            bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
120
 
121
            //Get top 8 best sellers b'coz 4 of them may overlap with best deals.
122
            bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
123
            bestSellerCatalogIds.removeAll(bestDealCatalogIds);
124
            if(bestSellerCatalogIds.size() < bestSellerCount)
125
                bestSellerCount = bestSellerCatalogIds.size();
126
 
127
            //Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
128
            //while another 4 may overlap with best sellers.
3266 chandransh 129
            latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, Arrays.asList(new Long[]{ 10002L, 10003L, 10010L}));
2930 chandransh 130
            latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
131
            latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
132
            if(latestArrivalCatalogIds.size() < latestArrivalCount)
133
                latestArrivalCount = latestArrivalCatalogIds.size();
134
        } catch (InventoryServiceException e) {
135
            logger.error("Error while fetching data from the catalog service", e);
136
        } catch (TException e) {
137
            logger.error("Error while fetching data from the catalog service", e);
138
        } catch (Exception e) {
139
            logger.error("Unexpected exception", e);
140
        }
141
 
142
        bestDealSnippets = getSnippets(bestDealCatalogIds);
143
        bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
3169 chandransh 144
        latestArrivalSnippets = getSnippets(latestArrivalCatalogIds.subList(0, latestArrivalCount));
3185 vikas 145
        if (!bestDealSnippets.isEmpty() && !bestSellerSnippets.isEmpty() && !latestArrivalSnippets.isEmpty()) {
146
            homeSnippetCache.put(BEST_DEALS_CACHE_KEY, bestDealSnippets);
147
            homeSnippetCache.put(BEST_SELLERS_CACHE_KEY, bestSellerSnippets);
148
            homeSnippetCache.put(LATEST_ARRIVALS_CACHE_KEY, latestArrivalSnippets);
149
        }
2930 chandransh 150
    }
151
 
152
    /**
153
     * Returns a list of snippets to be used on the home page corresponding to
154
     * the given catalog ids. The snippets are in the same order as the ids in
155
     * the input list. In case a snippet is not found for an entity, this method
156
     * simply logs the problem and moves on.
157
     * 
158
     * @param catalogIds
159
     *            Ids of the entities which we want to show.
160
     * @return The snippet corresponding to each catalog entity
161
     */
162
    private List<String> getSnippets(List<Long> catalogIds) {
163
        List<String> snippets = new ArrayList<String>();
164
        if(catalogIds == null)
165
            return snippets;
166
        for(Long item: catalogIds){
167
            try{
168
                snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
169
            }catch(IOException ioex){
170
                logger.error("Unable to get home page snippet for " + item, ioex);
171
            }
172
        }
173
        return snippets;
174
    }
507 rajveer 175
}
3050 vikas 176