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