| 545 |
rajveer |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.serving.controllers;
|
|
|
5 |
|
|
|
6 |
import java.util.List;
|
|
|
7 |
|
|
|
8 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
|
|
9 |
import in.shop2020.thrift.clients.CatalogServiceClient;
|
|
|
10 |
|
|
|
11 |
import org.apache.juli.logging.Log;
|
|
|
12 |
import org.apache.juli.logging.LogFactory;
|
|
|
13 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
14 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
15 |
import org.apache.thrift.TException;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* get best deals items from the catalog service
|
|
|
19 |
* @author rajveer
|
|
|
20 |
*
|
|
|
21 |
*/
|
|
|
22 |
public class BestDealsController extends BaseController {
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
*
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
private static Log log = LogFactory.getLog(BestDealsController.class);
|
|
|
29 |
|
|
|
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 BestDealsController() {
|
|
|
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 {
|
|
|
56 |
in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
|
|
|
57 |
this.setTotalItems(client.totalBestDeals());
|
|
|
58 |
this.items = client.getBestDeals(beginIndex, windowSize);
|
|
|
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 {
|
|
|
66 |
this.setTotalItems(client.totalBestDeals());
|
|
|
67 |
this.items = client.getBestDeals(beginIndex, windowSize);
|
|
|
68 |
} catch (InventoryServiceException e) {
|
|
|
69 |
log.error("Unable to get best deals 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 deals from inventory service.");
|
|
|
74 |
log.error(" Exception message is:" + e.getMessage());
|
|
|
75 |
e.printStackTrace();
|
|
|
76 |
}
|
|
|
77 |
return new DefaultHttpHeaders("show");
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
public List<Long> getItems(){
|
|
|
81 |
return items;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public void setId(String id) {
|
|
|
85 |
this.id = id;
|
|
|
86 |
this.beginIndex = this.windowSize * (Long.parseLong(id)-1);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
public String getId() {
|
|
|
90 |
return id;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public long getBeginIndex(){
|
|
|
94 |
if(totalItems>0)
|
|
|
95 |
return beginIndex+1;
|
|
|
96 |
return beginIndex;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
public void setTotalItems(long totalItems) {
|
|
|
100 |
this.totalItems = totalItems;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public long getTotalItems() {
|
|
|
104 |
return totalItems;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public long getTotalPages() {
|
|
|
108 |
return 1 + totalItems/windowSize;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public long getCurrentPage() {
|
|
|
112 |
return Long.parseLong(getId());
|
|
|
113 |
}
|
|
|
114 |
}
|