| 545 |
rajveer |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.serving.controllers;
|
|
|
5 |
|
| 5945 |
mandeep.dh |
6 |
import in.shop2020.model.v1.catalog.CatalogServiceException;
|
| 3242 |
vikas |
7 |
import in.shop2020.serving.cache.EhcacheWrapper;
|
| 3561 |
rajveer |
8 |
import in.shop2020.serving.services.ContentServingService;
|
|
|
9 |
import in.shop2020.serving.utils.SnippetType;
|
| 3126 |
rajveer |
10 |
import in.shop2020.thrift.clients.CatalogClient;
|
| 3112 |
vikas |
11 |
|
| 741 |
rajveer |
12 |
import java.util.ArrayList;
|
| 545 |
rajveer |
13 |
import java.util.List;
|
|
|
14 |
|
| 3112 |
vikas |
15 |
import net.sf.ehcache.CacheManager;
|
| 545 |
rajveer |
16 |
|
| 832 |
rajveer |
17 |
import org.apache.log4j.Logger;
|
| 5529 |
amit.gupta |
18 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
19 |
import org.apache.struts2.convention.annotation.Results;
|
| 545 |
rajveer |
20 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
21 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
22 |
import org.apache.thrift.TException;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* get best sellers items from the catalog service
|
|
|
26 |
* @author rajveer
|
|
|
27 |
*
|
|
|
28 |
*/
|
| 5529 |
amit.gupta |
29 |
@Results({
|
|
|
30 |
@Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
|
|
|
31 |
})
|
| 545 |
rajveer |
32 |
public class BestSellersController extends BaseController {
|
|
|
33 |
|
| 1044 |
chandransh |
34 |
private static final long serialVersionUID = 3380274695464543863L;
|
| 545 |
rajveer |
35 |
|
| 832 |
rajveer |
36 |
private static Logger log = Logger.getLogger(Class.class);
|
| 2943 |
chandransh |
37 |
private static final int windowSize = 20;
|
| 545 |
rajveer |
38 |
|
| 3112 |
vikas |
39 |
private static final String SHOWCASE_BEST_SELLERS_CACHE_KEY = "SHOWCASE_BEST_SELLERS";
|
|
|
40 |
private static final String SHOWCASE_BEST_SELLERS_COUNT_CACHE_KEY = "SHOWCASE_BEST_SELLERS_COUNT";
|
|
|
41 |
|
| 545 |
rajveer |
42 |
private long beginIndex = 0;
|
|
|
43 |
|
| 3112 |
vikas |
44 |
private Long totalItems = 0l;
|
| 545 |
rajveer |
45 |
|
|
|
46 |
private String id;
|
| 5529 |
amit.gupta |
47 |
private String redirectUrl;
|
| 545 |
rajveer |
48 |
|
|
|
49 |
List<Long> items = null;
|
| 3112 |
vikas |
50 |
|
|
|
51 |
private List<String> snippets = null;
|
| 545 |
rajveer |
52 |
|
| 3126 |
rajveer |
53 |
CatalogClient catalogClientService = null;
|
| 545 |
rajveer |
54 |
|
|
|
55 |
public BestSellersController() {
|
|
|
56 |
super();
|
|
|
57 |
try {
|
| 3126 |
rajveer |
58 |
catalogClientService = new CatalogClient();
|
| 545 |
rajveer |
59 |
} catch (Exception e) {
|
| 2453 |
chandransh |
60 |
log.error("Unable to get catalog service client.", e);
|
| 545 |
rajveer |
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
// GET /index
|
|
|
65 |
public HttpHeaders index() throws Exception {
|
| 1923 |
rajveer |
66 |
long categoryId = Long.parseLong(request.getParameter("categoryid"));
|
|
|
67 |
String brandName = request.getParameter("brand");
|
|
|
68 |
//Right now if we have brand name, we can just send back data for all categories
|
|
|
69 |
if(brandName!=null){
|
|
|
70 |
categoryId = -1;
|
|
|
71 |
}
|
| 5945 |
mandeep.dh |
72 |
in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
|
| 1923 |
rajveer |
73 |
this.items = client.getBestSellersCatalogIds(beginIndex, windowSize, brandName, categoryId);
|
| 3118 |
vikas |
74 |
setSnippets(items);
|
| 545 |
rajveer |
75 |
return new DefaultHttpHeaders("index");
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
// GET /show
|
| 3268 |
chandransh |
79 |
@SuppressWarnings("unchecked")
|
| 545 |
rajveer |
80 |
public HttpHeaders show() {
|
| 5529 |
amit.gupta |
81 |
if(getCurrentPage() > 1) {
|
|
|
82 |
log.info("Redirecting");
|
|
|
83 |
this.redirectUrl = "/best-sellers/1";
|
|
|
84 |
return new DefaultHttpHeaders("redirect");
|
|
|
85 |
}
|
| 3112 |
vikas |
86 |
EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(
|
|
|
87 |
EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
|
| 3561 |
rajveer |
88 |
if(sourceId == -1){
|
|
|
89 |
this.snippets = (List<String>)showcasePageSnippetsCache.get(SHOWCASE_BEST_SELLERS_CACHE_KEY + beginIndex);
|
|
|
90 |
this.totalItems = (Long)showcasePageSnippetsCache.get(SHOWCASE_BEST_SELLERS_COUNT_CACHE_KEY);
|
|
|
91 |
if(this.snippets != null && !this.snippets.isEmpty() && this.totalItems > 0) {
|
|
|
92 |
log.info("Using Cache.");
|
|
|
93 |
return new DefaultHttpHeaders("show");
|
|
|
94 |
}
|
| 3112 |
vikas |
95 |
}
|
|
|
96 |
log.info("Getting best sellers from snippets.");
|
| 5945 |
mandeep.dh |
97 |
in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
|
| 545 |
rajveer |
98 |
try {
|
| 5529 |
amit.gupta |
99 |
|
|
|
100 |
long count = client.getBestSellersCount();
|
|
|
101 |
this.setTotalItems(count > 20 ? 20 : count);
|
| 3561 |
rajveer |
102 |
if(sourceId == -1){
|
|
|
103 |
showcasePageSnippetsCache.put(SHOWCASE_BEST_SELLERS_COUNT_CACHE_KEY, this.totalItems);
|
|
|
104 |
}
|
| 1923 |
rajveer |
105 |
this.items = client.getBestSellersCatalogIds(beginIndex, windowSize, null, -1);
|
| 3112 |
vikas |
106 |
setSnippets(items);
|
| 3561 |
rajveer |
107 |
if(sourceId == -1){
|
|
|
108 |
showcasePageSnippetsCache.put(SHOWCASE_BEST_SELLERS_CACHE_KEY + beginIndex, this.snippets);
|
|
|
109 |
}
|
| 5945 |
mandeep.dh |
110 |
} catch (CatalogServiceException e) {
|
| 2453 |
chandransh |
111 |
log.error("Unable to get best sellers from inventory service.", e);
|
| 545 |
rajveer |
112 |
} catch (TException e) {
|
| 2453 |
chandransh |
113 |
log.error("Unable to get best sellers from inventory service.", e);
|
| 545 |
rajveer |
114 |
}
|
|
|
115 |
return new DefaultHttpHeaders("show");
|
|
|
116 |
}
|
|
|
117 |
|
| 3112 |
vikas |
118 |
public void setSnippets(List<Long> items) {
|
|
|
119 |
this.snippets = new ArrayList<String>();
|
| 741 |
rajveer |
120 |
if(items != null){
|
|
|
121 |
for(long item: items){
|
| 3561 |
rajveer |
122 |
snippets.add(ContentServingService.getSnippet(SnippetType.CATEGORY_SNIPPET, item+"", sourceId));
|
| 741 |
rajveer |
123 |
}
|
|
|
124 |
}
|
| 1371 |
ankur.sing |
125 |
}
|
|
|
126 |
|
| 3112 |
vikas |
127 |
public List<String> getSnippets() {
|
|
|
128 |
return snippets;
|
|
|
129 |
}
|
|
|
130 |
|
| 545 |
rajveer |
131 |
public void setId(String id) {
|
|
|
132 |
this.id = id;
|
| 2943 |
chandransh |
133 |
this.beginIndex = windowSize * (Long.parseLong(id)-1);
|
| 545 |
rajveer |
134 |
}
|
|
|
135 |
|
|
|
136 |
public String getId() {
|
|
|
137 |
return id;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
public long getBeginIndex(){
|
|
|
141 |
if(totalItems>0)
|
|
|
142 |
return beginIndex+1;
|
|
|
143 |
return beginIndex;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
public void setTotalItems(long totalItems) {
|
|
|
147 |
this.totalItems = totalItems;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
public long getTotalItems() {
|
|
|
151 |
return totalItems;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
public long getTotalPages() {
|
| 2950 |
chandransh |
155 |
return 1 + (totalItems-1)/windowSize;
|
| 545 |
rajveer |
156 |
}
|
|
|
157 |
|
|
|
158 |
public long getCurrentPage() {
|
|
|
159 |
return Long.parseLong(getId());
|
|
|
160 |
}
|
| 5529 |
amit.gupta |
161 |
|
|
|
162 |
public String getRedirectUrl(){
|
|
|
163 |
return this.redirectUrl;
|
|
|
164 |
}
|
| 545 |
rajveer |
165 |
}
|