| Line 1... |
Line 1... |
| 1 |
/**
|
1 |
/**
|
| 2 |
*
|
2 |
*
|
| 3 |
*/
|
3 |
*/
|
| 4 |
package in.shop2020.serving.controllers;
|
4 |
package in.shop2020.serving.controllers;
|
| 5 |
|
5 |
|
| - |
|
6 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
| - |
|
7 |
import in.shop2020.serving.utils.EhcacheWrapper;
|
| - |
|
8 |
import in.shop2020.serving.utils.FileUtils;
|
| - |
|
9 |
import in.shop2020.serving.utils.Utils;
|
| - |
|
10 |
import in.shop2020.thrift.clients.CatalogServiceClient;
|
| - |
|
11 |
|
| 6 |
import java.io.File;
|
12 |
import java.io.File;
|
| 7 |
import java.io.IOException;
|
13 |
import java.io.IOException;
|
| 8 |
import java.util.ArrayList;
|
14 |
import java.util.ArrayList;
|
| 9 |
import java.util.List;
|
15 |
import java.util.List;
|
| 10 |
|
16 |
|
| 11 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
- |
|
| 12 |
import in.shop2020.serving.utils.FileUtils;
|
- |
|
| 13 |
import in.shop2020.serving.utils.Utils;
|
17 |
import net.sf.ehcache.CacheManager;
|
| 14 |
import in.shop2020.thrift.clients.CatalogServiceClient;
|
- |
|
| 15 |
|
18 |
|
| 16 |
import org.apache.log4j.Logger;
|
19 |
import org.apache.log4j.Logger;
|
| 17 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
20 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
| 18 |
import org.apache.struts2.rest.HttpHeaders;
|
21 |
import org.apache.struts2.rest.HttpHeaders;
|
| 19 |
import org.apache.thrift.TException;
|
22 |
import org.apache.thrift.TException;
|
| Line 27... |
Line 30... |
| 27 |
|
30 |
|
| 28 |
private static final long serialVersionUID = -6862524269260661024L;
|
31 |
private static final long serialVersionUID = -6862524269260661024L;
|
| 29 |
|
32 |
|
| 30 |
private static Logger log = Logger.getLogger(Class.class);
|
33 |
private static Logger log = Logger.getLogger(Class.class);
|
| 31 |
private static final int windowSize = 20;
|
34 |
private static final int windowSize = 20;
|
| - |
|
35 |
|
| - |
|
36 |
private static final String SHOWCASE_BEST_DEALS_CACHE_KEY = "SHOWCASE_BEST_DEALS";
|
| - |
|
37 |
private static final String SHOWCASE_BEST_DEALS_COUNT_CACHE_KEY = "SHOWCASE_BEST_DEALS_COUNT";
|
| 32 |
|
38 |
|
| 33 |
private long beginIndex = 0;
|
39 |
private long beginIndex = 0;
|
| 34 |
|
40 |
|
| 35 |
private long totalItems;
|
41 |
private Long totalItems = 0l;
|
| 36 |
|
42 |
|
| 37 |
private String id;
|
43 |
private String id;
|
| 38 |
|
44 |
|
| 39 |
List<Long> items = null;
|
45 |
List<Long> items = null;
|
| 40 |
|
46 |
|
| Line 59... |
Line 65... |
| 59 |
if(brandName != null){
|
65 |
if(brandName != null){
|
| 60 |
categoryId = -1;
|
66 |
categoryId = -1;
|
| 61 |
}
|
67 |
}
|
| 62 |
in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
|
68 |
in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
|
| 63 |
this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, brandName, categoryId);
|
69 |
this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, brandName, categoryId);
|
| 64 |
setSnippets(items);
|
70 |
setSnippets();
|
| 65 |
return new DefaultHttpHeaders("index");
|
71 |
return new DefaultHttpHeaders("index");
|
| 66 |
}
|
72 |
}
|
| 67 |
|
73 |
|
| 68 |
// GET /show
|
74 |
// GET /show
|
| 69 |
public HttpHeaders show() {
|
75 |
public HttpHeaders show() {
|
| - |
|
76 |
EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(
|
| - |
|
77 |
EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
|
| - |
|
78 |
this.snippets = (List<String>)showcasePageSnippetsCache.get(SHOWCASE_BEST_DEALS_CACHE_KEY + beginIndex);
|
| - |
|
79 |
this.totalItems = (Long)showcasePageSnippetsCache.get(SHOWCASE_BEST_DEALS_COUNT_CACHE_KEY);
|
| - |
|
80 |
if(this.snippets != null && !this.snippets.isEmpty() && this.totalItems > 0) {
|
| - |
|
81 |
log.info("Using Cache.");
|
| - |
|
82 |
return new DefaultHttpHeaders("show");
|
| - |
|
83 |
}
|
| - |
|
84 |
log.info("Getting best deals from snippets.");
|
| 70 |
in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
|
85 |
in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
|
| 71 |
try {
|
86 |
try {
|
| 72 |
this.setTotalItems(client.getBestDealsCount());
|
87 |
this.setTotalItems(client.getBestDealsCount());
|
| - |
|
88 |
showcasePageSnippetsCache.put(SHOWCASE_BEST_DEALS_COUNT_CACHE_KEY, this.totalItems);
|
| 73 |
this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, null, -1);
|
89 |
this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, null, -1);
|
| 74 |
setSnippets(items);
|
90 |
setSnippets();
|
| - |
|
91 |
showcasePageSnippetsCache.put(SHOWCASE_BEST_DEALS_CACHE_KEY + beginIndex, this.snippets);
|
| 75 |
} catch (InventoryServiceException e) {
|
92 |
} catch (InventoryServiceException e) {
|
| 76 |
log.error("Unable to get best deals from inventory service.", e);
|
93 |
log.error("Unable to get best deals from inventory service.", e);
|
| 77 |
} catch (TException e) {
|
94 |
} catch (TException e) {
|
| 78 |
log.error("Unable to get best deals from inventory service.", e);
|
95 |
log.error("Unable to get best deals from inventory service.", e);
|
| 79 |
}
|
96 |
}
|
| 80 |
return new DefaultHttpHeaders("show");
|
97 |
return new DefaultHttpHeaders("show");
|
| 81 |
}
|
98 |
}
|
| 82 |
|
99 |
|
| 83 |
private void setSnippets(List<Long> items){
|
100 |
private void setSnippets(){
|
| 84 |
this.snippets = new ArrayList<String>();
|
101 |
this.snippets = new ArrayList<String>();
|
| 85 |
if(items != null){
|
102 |
if(items != null){
|
| 86 |
for(long item: items){
|
103 |
for(long item: items){
|
| 87 |
try {
|
104 |
try {
|
| 88 |
snippets.add(FileUtils.read(Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
|
105 |
snippets.add(FileUtils.read(Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
|