| 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.
|
| 2975 |
chandransh |
112 |
List<Long> latestArrivalCategories = new ArrayList<Long>();
|
|
|
113 |
latestArrivalCategories.add(10002L);
|
|
|
114 |
latestArrivalCategories.add(10003L);
|
|
|
115 |
latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, latestArrivalCategories);
|
| 2930 |
chandransh |
116 |
latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
|
|
|
117 |
latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
|
|
|
118 |
if(latestArrivalCatalogIds.size() < latestArrivalCount)
|
|
|
119 |
latestArrivalCount = latestArrivalCatalogIds.size();
|
|
|
120 |
} catch (InventoryServiceException e) {
|
|
|
121 |
logger.error("Error while fetching data from the catalog service", e);
|
|
|
122 |
} catch (TException e) {
|
|
|
123 |
logger.error("Error while fetching data from the catalog service", e);
|
|
|
124 |
} catch (Exception e) {
|
|
|
125 |
logger.error("Unexpected exception", e);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
bestDealSnippets = getSnippets(bestDealCatalogIds);
|
|
|
129 |
bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
|
|
|
130 |
latestArrivalSnippets = getSnippets(latestArrivalCatalogIds).subList(0, latestArrivalCount);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Returns a list of snippets to be used on the home page corresponding to
|
|
|
135 |
* the given catalog ids. The snippets are in the same order as the ids in
|
|
|
136 |
* the input list. In case a snippet is not found for an entity, this method
|
|
|
137 |
* simply logs the problem and moves on.
|
|
|
138 |
*
|
|
|
139 |
* @param catalogIds
|
|
|
140 |
* Ids of the entities which we want to show.
|
|
|
141 |
* @return The snippet corresponding to each catalog entity
|
|
|
142 |
*/
|
|
|
143 |
private List<String> getSnippets(List<Long> catalogIds) {
|
|
|
144 |
List<String> snippets = new ArrayList<String>();
|
|
|
145 |
if(catalogIds == null)
|
|
|
146 |
return snippets;
|
|
|
147 |
for(Long item: catalogIds){
|
|
|
148 |
try{
|
|
|
149 |
snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
|
|
|
150 |
}catch(IOException ioex){
|
|
|
151 |
logger.error("Unable to get home page snippet for " + item, ioex);
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
return snippets;
|
|
|
155 |
}
|
| 507 |
rajveer |
156 |
}
|