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