Subversion Repositories SmartDukaan

Rev

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