| 545 |
rajveer |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.serving.controllers;
|
|
|
5 |
|
| 741 |
rajveer |
6 |
import java.io.File;
|
| 1371 |
ankur.sing |
7 |
import java.io.IOException;
|
| 741 |
rajveer |
8 |
import java.util.ArrayList;
|
| 545 |
rajveer |
9 |
import java.util.List;
|
|
|
10 |
|
|
|
11 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
| 741 |
rajveer |
12 |
import in.shop2020.serving.utils.FileUtils;
|
|
|
13 |
import in.shop2020.serving.utils.Utils;
|
| 545 |
rajveer |
14 |
import in.shop2020.thrift.clients.CatalogServiceClient;
|
|
|
15 |
|
| 832 |
rajveer |
16 |
import org.apache.log4j.Logger;
|
| 545 |
rajveer |
17 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
18 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
19 |
import org.apache.thrift.TException;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* get best sellers items from the catalog service
|
|
|
23 |
* @author rajveer
|
|
|
24 |
*
|
|
|
25 |
*/
|
|
|
26 |
public class BestSellersController extends BaseController {
|
|
|
27 |
|
| 1044 |
chandransh |
28 |
private static final long serialVersionUID = 3380274695464543863L;
|
| 545 |
rajveer |
29 |
|
| 832 |
rajveer |
30 |
private static Logger log = Logger.getLogger(Class.class);
|
| 545 |
rajveer |
31 |
private final int windowSize = 20;
|
|
|
32 |
|
|
|
33 |
private long beginIndex = 0;
|
|
|
34 |
|
|
|
35 |
private long totalItems;
|
|
|
36 |
|
|
|
37 |
private String id;
|
|
|
38 |
|
|
|
39 |
List<Long> items = null;
|
|
|
40 |
|
|
|
41 |
CatalogServiceClient catalogClientService = null;
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
public BestSellersController() {
|
|
|
46 |
super();
|
|
|
47 |
try {
|
|
|
48 |
catalogClientService = new CatalogServiceClient();
|
|
|
49 |
} catch (Exception e) {
|
|
|
50 |
log.error("Unable to get catalog service client.");
|
|
|
51 |
e.printStackTrace();
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
// GET /index
|
|
|
56 |
public HttpHeaders index() throws Exception {
|
| 1923 |
rajveer |
57 |
long categoryId = Long.parseLong(request.getParameter("categoryid"));
|
|
|
58 |
String brandName = request.getParameter("brand");
|
|
|
59 |
//Right now if we have brand name, we can just send back data for all categories
|
|
|
60 |
if(brandName!=null){
|
|
|
61 |
categoryId = -1;
|
|
|
62 |
}
|
| 545 |
rajveer |
63 |
in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
|
| 1923 |
rajveer |
64 |
this.items = client.getBestSellersCatalogIds(beginIndex, windowSize, brandName, categoryId);
|
| 545 |
rajveer |
65 |
return new DefaultHttpHeaders("index");
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
// GET /show
|
|
|
69 |
public HttpHeaders show() {
|
|
|
70 |
in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
|
|
|
71 |
try {
|
| 590 |
chandransh |
72 |
this.setTotalItems(client.getBestSellersCount());
|
| 1923 |
rajveer |
73 |
this.items = client.getBestSellersCatalogIds(beginIndex, windowSize, null, -1);
|
| 545 |
rajveer |
74 |
} catch (InventoryServiceException e) {
|
|
|
75 |
log.error("Unable to get best sellers from inventory service.");
|
|
|
76 |
log.error("Exception id is:" + e.getId() + " Exception message is:" + e.getMessage());
|
|
|
77 |
e.printStackTrace();
|
|
|
78 |
} catch (TException e) {
|
|
|
79 |
log.error("Unable to get best sellers from inventory service.");
|
|
|
80 |
log.error(" Exception message is:" + e.getMessage());
|
|
|
81 |
e.printStackTrace();
|
|
|
82 |
}
|
|
|
83 |
return new DefaultHttpHeaders("show");
|
|
|
84 |
}
|
|
|
85 |
|
| 1371 |
ankur.sing |
86 |
public List<String> getItems() {
|
| 741 |
rajveer |
87 |
ArrayList<String> snippets = new ArrayList<String>();
|
|
|
88 |
if(items != null){
|
|
|
89 |
for(long item: items){
|
| 1371 |
ankur.sing |
90 |
try {
|
|
|
91 |
snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
|
|
|
92 |
} catch (IOException ioex) {
|
|
|
93 |
ioex.printStackTrace();
|
|
|
94 |
log.error(ioex);
|
|
|
95 |
}
|
| 741 |
rajveer |
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
return snippets;
|
| 1371 |
ankur.sing |
100 |
}
|
|
|
101 |
|
| 545 |
rajveer |
102 |
public void setId(String id) {
|
|
|
103 |
this.id = id;
|
|
|
104 |
this.beginIndex = this.windowSize * (Long.parseLong(id)-1);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public String getId() {
|
|
|
108 |
return id;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public long getBeginIndex(){
|
|
|
112 |
if(totalItems>0)
|
|
|
113 |
return beginIndex+1;
|
|
|
114 |
return beginIndex;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
public void setTotalItems(long totalItems) {
|
|
|
118 |
this.totalItems = totalItems;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
public long getTotalItems() {
|
|
|
122 |
return totalItems;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
public long getTotalPages() {
|
|
|
126 |
return 1 + totalItems/windowSize;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
public long getCurrentPage() {
|
|
|
130 |
return Long.parseLong(getId());
|
|
|
131 |
}
|
|
|
132 |
}
|