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