| Line 10... |
Line 10... |
| 10 |
import in.shop2020.utils.DataLogger;
|
10 |
import in.shop2020.utils.DataLogger;
|
| 11 |
|
11 |
|
| 12 |
import java.io.IOException;
|
12 |
import java.io.IOException;
|
| 13 |
import java.util.ArrayList;
|
13 |
import java.util.ArrayList;
|
| 14 |
import java.util.Arrays;
|
14 |
import java.util.Arrays;
|
| - |
|
15 |
import java.util.HashMap;
|
| 15 |
import java.util.List;
|
16 |
import java.util.List;
|
| - |
|
17 |
import java.util.Map;
|
| 16 |
|
18 |
|
| 17 |
import net.sf.ehcache.CacheManager;
|
19 |
import net.sf.ehcache.CacheManager;
|
| 18 |
|
20 |
|
| 19 |
import org.apache.log4j.Logger;
|
21 |
import org.apache.log4j.Logger;
|
| 20 |
import org.apache.struts2.convention.annotation.Action;
|
22 |
import org.apache.struts2.convention.annotation.Action;
|
| Line 30... |
Line 32... |
| 30 |
|
32 |
|
| 31 |
private static Logger logger = Logger.getLogger(Class.class);
|
33 |
private static Logger logger = Logger.getLogger(Class.class);
|
| 32 |
private static final String BEST_DEALS_CACHE_KEY = "home_best_deals_snippet";
|
34 |
private static final String BEST_DEALS_CACHE_KEY = "home_best_deals_snippet";
|
| 33 |
private static final String BEST_SELLERS_CACHE_KEY = "home_best_sellers_snippet";
|
35 |
private static final String BEST_SELLERS_CACHE_KEY = "home_best_sellers_snippet";
|
| 34 |
private static final String LATEST_ARRIVALS_CACHE_KEY = "home_latest_arrivals_snippet";
|
36 |
private static final String LATEST_ARRIVALS_CACHE_KEY = "home_latest_arrivals_snippet";
|
| - |
|
37 |
private static final String HOME_PAGE_CACHE_KEY = "home_page_cache_snippets";
|
| 35 |
|
38 |
|
| - |
|
39 |
|
| 36 |
private List<String> bestDealSnippets = null;
|
40 |
private List<String> bestDealSnippets = null;
|
| 37 |
private List<String> bestSellerSnippets = null;
|
41 |
private List<String> bestSellerSnippets = null;
|
| 38 |
private List<String> latestArrivalSnippets = null;
|
42 |
private List<String> latestArrivalSnippets = null;
|
| 39 |
|
43 |
|
| 40 |
public HomeController(){
|
44 |
public HomeController(){
|
| Line 92... |
Line 96... |
| 92 |
/**
|
96 |
/**
|
| 93 |
* Loads the snippets for best deals, best sellers and latest arrivals
|
97 |
* Loads the snippets for best deals, best sellers and latest arrivals
|
| 94 |
* into memory.
|
98 |
* into memory.
|
| 95 |
*/
|
99 |
*/
|
| 96 |
private void setSnippets() {
|
100 |
private void setSnippets() {
|
| 97 |
EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
|
101 |
EhcacheWrapper<String,Map<String, List<String>>> homeSnippetCache = new EhcacheWrapper<String,Map<String, List<String>>>(
|
| 98 |
EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
|
102 |
EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
|
| 99 |
logger.info("Source id "+sourceId);
|
103 |
//logger.info("Source id "+sourceId);
|
| 100 |
if(sourceId == -1){
|
- |
|
| 101 |
bestDealSnippets = homeSnippetCache.get(BEST_DEALS_CACHE_KEY);
|
- |
|
| 102 |
bestSellerSnippets = homeSnippetCache.get(BEST_SELLERS_CACHE_KEY);
|
- |
|
| 103 |
latestArrivalSnippets = homeSnippetCache.get(LATEST_ARRIVALS_CACHE_KEY);
|
104 |
Map<String, List<String>> homePageSnippets = homeSnippetCache.get(HOME_PAGE_CACHE_KEY);
|
| 104 |
|
- |
|
| 105 |
if (bestDealSnippets != null && bestSellerSnippets != null && latestArrivalSnippets != null) {
|
- |
|
| 106 |
logger.info("returning in set snippets");
|
- |
|
| 107 |
return;
|
- |
|
| 108 |
}
|
- |
|
| 109 |
}
|
- |
|
| 110 |
|
- |
|
| 111 |
List<Long> bestDealCatalogIds = null;
|
- |
|
| 112 |
List<Long> bestSellerCatalogIds = null;
|
- |
|
| 113 |
List<Long> latestArrivalCatalogIds = null;
|
- |
|
| 114 |
|
- |
|
| 115 |
int bestSellerCount = 4;
|
- |
|
| 116 |
int latestArrivalCount = 4;
|
- |
|
| 117 |
|
- |
|
| 118 |
try {
|
- |
|
| 119 |
activeBanners=null;
|
- |
|
| 120 |
allBannersMap=null;
|
- |
|
| 121 |
setBanners();
|
- |
|
| 122 |
CatalogClient catalogServiceClient = new CatalogClient();
|
- |
|
| 123 |
Client client = catalogServiceClient.getClient();
|
- |
|
| 124 |
//Get top 4 best deals
|
- |
|
| 125 |
bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
|
- |
|
| 126 |
|
- |
|
| 127 |
//Get top 8 best sellers b'coz 4 of them may overlap with best deals.
|
- |
|
| 128 |
bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
|
- |
|
| 129 |
bestSellerCatalogIds.removeAll(bestDealCatalogIds);
|
- |
|
| 130 |
if(bestSellerCatalogIds.size() < bestSellerCount)
|
- |
|
| 131 |
bestSellerCount = bestSellerCatalogIds.size();
|
- |
|
| 132 |
|
- |
|
| 133 |
//Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
|
- |
|
| 134 |
//while another 4 may overlap with best sellers.
|
- |
|
| 135 |
latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, Arrays.asList(new Long[]{ 10006L, 10010L, 10050L}));
|
- |
|
| 136 |
latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
|
- |
|
| 137 |
latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
|
- |
|
| 138 |
if(latestArrivalCatalogIds.size() < latestArrivalCount)
|
- |
|
| 139 |
latestArrivalCount = latestArrivalCatalogIds.size();
|
- |
|
| 140 |
} catch (CatalogServiceException e) {
|
- |
|
| 141 |
logger.error("Error while fetching data from the catalog service", e);
|
- |
|
| 142 |
} catch (TException e) {
|
- |
|
| 143 |
logger.error("Error while fetching data from the catalog service", e);
|
- |
|
| 144 |
} catch (Exception e) {
|
- |
|
| 145 |
logger.error("Unexpected exception", e);
|
- |
|
| 146 |
}
|
- |
|
| 147 |
|
105 |
|
| - |
|
106 |
if(homePageSnippets == null) {
|
| - |
|
107 |
homePageSnippets = new HashMap<String, List<String>>();
|
| - |
|
108 |
List<Long> bestDealCatalogIds = null;
|
| - |
|
109 |
List<Long> bestSellerCatalogIds = null;
|
| - |
|
110 |
List<Long> latestArrivalCatalogIds = null;
|
| - |
|
111 |
|
| - |
|
112 |
int bestSellerCount = 4;
|
| - |
|
113 |
int latestArrivalCount = 4;
|
| - |
|
114 |
try {
|
| - |
|
115 |
CatalogClient catalogServiceClient = new CatalogClient();
|
| - |
|
116 |
Client client = catalogServiceClient.getClient();
|
| - |
|
117 |
//Get top 4 best deals
|
| 148 |
bestDealSnippets = getSnippets(bestDealCatalogIds);
|
118 |
bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
|
| - |
|
119 |
|
| - |
|
120 |
//Get top 8 best sellers b'coz 4 of them may overlap with best deals.
|
| 149 |
bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
|
121 |
bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
|
| - |
|
122 |
bestSellerCatalogIds.removeAll(bestDealCatalogIds);
|
| 150 |
latestArrivalSnippets = getSnippets(latestArrivalCatalogIds.subList(0, latestArrivalCount));
|
123 |
if(bestSellerCatalogIds.size() < bestSellerCount)
|
| 151 |
if(sourceId == -1){
|
124 |
bestSellerCount = bestSellerCatalogIds.size();
|
| - |
|
125 |
|
| 152 |
if (!bestDealSnippets.isEmpty() && !bestSellerSnippets.isEmpty() && !latestArrivalSnippets.isEmpty()) {
|
126 |
//Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
|
| 153 |
homeSnippetCache.put(BEST_DEALS_CACHE_KEY, bestDealSnippets);
|
127 |
//while another 4 may overlap with best sellers.
|
| - |
|
128 |
latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, Arrays.asList(new Long[]{ 10006L, 10010L, 10050L}));
|
| 154 |
homeSnippetCache.put(BEST_SELLERS_CACHE_KEY, bestSellerSnippets);
|
129 |
latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
|
| - |
|
130 |
latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
|
| 155 |
homeSnippetCache.put(LATEST_ARRIVALS_CACHE_KEY, latestArrivalSnippets);
|
131 |
if(latestArrivalCatalogIds.size() < latestArrivalCount)
|
| - |
|
132 |
latestArrivalCount = latestArrivalCatalogIds.size();
|
| - |
|
133 |
|
| - |
|
134 |
} catch (CatalogServiceException e) {
|
| - |
|
135 |
logger.error("Error while fetching data from the catalog service", e);
|
| - |
|
136 |
} catch (TException e) {
|
| - |
|
137 |
logger.error("Error while fetching data from the catalog service", e);
|
| - |
|
138 |
} catch (Exception e) {
|
| - |
|
139 |
logger.error("Unexpected exception", e);
|
| 156 |
}
|
140 |
}
|
| - |
|
141 |
bestDealSnippets = getSnippets(bestDealCatalogIds);
|
| - |
|
142 |
bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
|
| - |
|
143 |
latestArrivalSnippets = getSnippets(latestArrivalCatalogIds.subList(0, latestArrivalCount));
|
| - |
|
144 |
homePageSnippets.put(BEST_DEALS_CACHE_KEY, bestDealSnippets);
|
| - |
|
145 |
homePageSnippets.put(BEST_SELLERS_CACHE_KEY, bestSellerSnippets);
|
| - |
|
146 |
homePageSnippets.put(LATEST_ARRIVALS_CACHE_KEY, latestArrivalSnippets);
|
| - |
|
147 |
homeSnippetCache.put(HOME_PAGE_CACHE_KEY, homePageSnippets);
|
| - |
|
148 |
return;
|
| 157 |
}
|
149 |
}
|
| - |
|
150 |
|
| - |
|
151 |
bestDealSnippets = homePageSnippets.get(BEST_DEALS_CACHE_KEY);
|
| - |
|
152 |
bestSellerSnippets = homePageSnippets.get(BEST_SELLERS_CACHE_KEY);
|
| - |
|
153 |
latestArrivalSnippets = homePageSnippets.get(LATEST_ARRIVALS_CACHE_KEY);
|
| 158 |
}
|
154 |
}
|
| 159 |
|
155 |
|
| 160 |
/**
|
156 |
/**
|
| 161 |
* Returns a list of snippets to be used on the home page corresponding to
|
157 |
* Returns a list of snippets to be used on the home page corresponding to
|
| 162 |
* the given catalog ids. The snippets are in the same order as the ids in
|
158 |
* the given catalog ids. The snippets are in the same order as the ids in
|