Subversion Repositories SmartDukaan

Rev

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