| 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 |
*
|
|
|
20 |
* @author 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 |
|
| 2578 |
chandransh |
28 |
private List<String> bestDealSnippets = null;
|
|
|
29 |
private List<String> bestSellerSnippets = null;
|
|
|
30 |
private List<String> latestArrivalSnippets = null;
|
|
|
31 |
|
| 507 |
rajveer |
32 |
public HomeController(){
|
|
|
33 |
super();
|
|
|
34 |
}
|
|
|
35 |
|
| 773 |
rajveer |
36 |
@Action("/")
|
| 650 |
rajveer |
37 |
public String index() throws SecurityException, IOException {
|
| 2578 |
chandransh |
38 |
logger.info("userinfo:" + userinfo.toString());
|
| 507 |
rajveer |
39 |
|
| 2578 |
chandransh |
40 |
List<Long> bestDealCatalogIds = null;
|
|
|
41 |
List<Long> bestSellerCatalogIds = null;
|
|
|
42 |
List<Long> latestArrivalCatalogIds = null;
|
|
|
43 |
|
| 2579 |
chandransh |
44 |
int bestSellerCount = 4;
|
|
|
45 |
int latestArrivalCount = 4;
|
|
|
46 |
|
| 2578 |
chandransh |
47 |
try {
|
|
|
48 |
CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
|
|
|
49 |
Client client = catalogServiceClient.getClient();
|
|
|
50 |
|
|
|
51 |
//Get top 4 best deals
|
|
|
52 |
bestDealCatalogIds = client.getBestDealsCatalogIds(0,4, null, -1);
|
|
|
53 |
|
|
|
54 |
//Get top 8 best deals b'coz 4 of them may overlap with best deals.
|
|
|
55 |
bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
|
|
|
56 |
bestSellerCatalogIds.removeAll(bestDealCatalogIds);
|
| 2579 |
chandransh |
57 |
if(bestSellerCatalogIds.size() < bestSellerCount)
|
|
|
58 |
bestSellerCount = bestSellerCatalogIds.size();
|
| 2578 |
chandransh |
59 |
|
|
|
60 |
//Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
|
|
|
61 |
//while another 4 may overlap with best sellers.
|
|
|
62 |
latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, 10003);
|
|
|
63 |
latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
|
| 2579 |
chandransh |
64 |
latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
|
|
|
65 |
if(latestArrivalCatalogIds.size() < latestArrivalCount)
|
|
|
66 |
latestArrivalCount = latestArrivalCatalogIds.size();
|
| 2578 |
chandransh |
67 |
} catch (InventoryServiceException e) {
|
|
|
68 |
logger.error("Error while fetching data from the catalog service", e);
|
|
|
69 |
} catch (TException e) {
|
|
|
70 |
logger.error("Error while fetching data from the catalog service", e);
|
|
|
71 |
} catch (Exception e) {
|
|
|
72 |
logger.error("Unexpected exception", e);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
bestDealSnippets = getSnippets(bestDealCatalogIds);
|
| 2579 |
chandransh |
76 |
bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
|
|
|
77 |
latestArrivalSnippets = getSnippets(latestArrivalCatalogIds).subList(0, latestArrivalCount);
|
| 2578 |
chandransh |
78 |
|
| 650 |
rajveer |
79 |
htmlSnippets.put("MAIN_BANNER", pageLoader.getMainBannerHtml());
|
|
|
80 |
return "index";
|
| 507 |
rajveer |
81 |
}
|
| 2578 |
chandransh |
82 |
|
|
|
83 |
private List<String> getSnippets(List<Long> catalogIds) {
|
|
|
84 |
List<String> snippets = new ArrayList<String>();
|
|
|
85 |
if(catalogIds == null)
|
|
|
86 |
return snippets;
|
|
|
87 |
for(Long item: catalogIds){
|
|
|
88 |
try{
|
|
|
89 |
snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
|
|
|
90 |
}catch(IOException ioex){
|
|
|
91 |
logger.error("Unable to get home page snippet for " + item, ioex);
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
return snippets;
|
|
|
95 |
}
|
| 507 |
rajveer |
96 |
|
|
|
97 |
public String getMainBannerSnippet(){
|
|
|
98 |
return htmlSnippets.get("MAIN_BANNER");
|
|
|
99 |
}
|
|
|
100 |
|
| 2578 |
chandransh |
101 |
public List<String> getBestDealSnippets(){
|
|
|
102 |
return bestDealSnippets;
|
| 507 |
rajveer |
103 |
}
|
|
|
104 |
|
| 2578 |
chandransh |
105 |
public List<String> getBestSellerSnippets(){
|
|
|
106 |
return bestSellerSnippets;
|
| 507 |
rajveer |
107 |
}
|
|
|
108 |
|
| 2578 |
chandransh |
109 |
public List<String> getLatestArrivalSnippets(){
|
|
|
110 |
return latestArrivalSnippets;
|
| 507 |
rajveer |
111 |
}
|
|
|
112 |
}
|