| 317 |
ashish |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.serving.controllers;
|
|
|
5 |
|
| 650 |
rajveer |
6 |
|
| 2306 |
vikas |
7 |
import in.shop2020.datalogger.EventType;
|
| 4934 |
amit.gupta |
8 |
import in.shop2020.logistics.DeliveryType;
|
| 9840 |
amit.gupta |
9 |
import in.shop2020.logistics.ItemText;
|
| 4934 |
amit.gupta |
10 |
import in.shop2020.logistics.LogisticsService;
|
| 2306 |
vikas |
11 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
12 |
import in.shop2020.model.v1.user.UserContextService.Client;
|
| 3242 |
vikas |
13 |
import in.shop2020.serving.cache.EhcacheWrapper;
|
| 3561 |
rajveer |
14 |
import in.shop2020.serving.services.ContentServingService;
|
|
|
15 |
import in.shop2020.serving.utils.SnippetType;
|
| 3126 |
rajveer |
16 |
import in.shop2020.thrift.clients.CatalogClient;
|
| 4934 |
amit.gupta |
17 |
import in.shop2020.thrift.clients.LogisticsClient;
|
| 3126 |
rajveer |
18 |
import in.shop2020.thrift.clients.UserClient;
|
| 2511 |
vikas |
19 |
import in.shop2020.utils.DataLogger;
|
| 375 |
ashish |
20 |
|
| 317 |
ashish |
21 |
import java.io.IOException;
|
| 9840 |
amit.gupta |
22 |
import java.util.ArrayList;
|
| 3225 |
vikas |
23 |
import java.util.HashMap;
|
| 4934 |
amit.gupta |
24 |
import java.util.List;
|
| 375 |
ashish |
25 |
import java.util.Map;
|
| 317 |
ashish |
26 |
|
| 3225 |
vikas |
27 |
import net.sf.ehcache.CacheManager;
|
|
|
28 |
|
| 5026 |
varun.gupt |
29 |
import org.apache.commons.lang.StringUtils;
|
| 832 |
rajveer |
30 |
import org.apache.log4j.Logger;
|
| 507 |
rajveer |
31 |
import org.apache.struts2.convention.annotation.Action;
|
|
|
32 |
import org.apache.struts2.convention.annotation.Actions;
|
| 974 |
vikas |
33 |
import org.apache.struts2.convention.annotation.Result;
|
| 2306 |
vikas |
34 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
35 |
import org.json.JSONException;
|
|
|
36 |
import org.json.JSONObject;
|
| 317 |
ashish |
37 |
|
| 4934 |
amit.gupta |
38 |
import com.google.gson.Gson;
|
|
|
39 |
|
| 317 |
ashish |
40 |
/**
|
|
|
41 |
*
|
| 650 |
rajveer |
42 |
* @author rajveer
|
| 317 |
ashish |
43 |
*
|
|
|
44 |
*/
|
| 650 |
rajveer |
45 |
|
| 2306 |
vikas |
46 |
@Results({
|
|
|
47 |
@Result(name = "show", location = "entity-show.vm"),
|
| 5266 |
rajveer |
48 |
@Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
|
| 2306 |
vikas |
49 |
})
|
| 650 |
rajveer |
50 |
public class EntityController extends BaseController {
|
|
|
51 |
|
|
|
52 |
private static final long serialVersionUID = 1L;
|
| 1957 |
vikas |
53 |
private static Logger log = Logger.getLogger(Class.class);
|
| 3225 |
vikas |
54 |
|
|
|
55 |
private static final String PRODUCT_PROERTIES_SNIPPET_KEY = "PRODUCT_PROPERTIES";
|
|
|
56 |
private static final String PRODUCT_SUMMARY_SNIPPET_KEY = "PRODUCT_SUMMARY";
|
|
|
57 |
private static final String PRODUCT_SLIDEGUIDE_KEY = "SLIDEGUIDE";
|
| 5026 |
varun.gupt |
58 |
|
| 5037 |
rajveer |
59 |
private static final String DEFAULT_PINCODE = "110001";
|
| 317 |
ashish |
60 |
private String id;
|
| 2306 |
vikas |
61 |
private String redirectUrl;
|
| 620 |
rajveer |
62 |
private long productId;
|
| 2867 |
rajveer |
63 |
private boolean isMobile = false;
|
|
|
64 |
|
| 6084 |
rajveer |
65 |
private Map<String, Double> discounts = new HashMap<String, Double>();
|
| 3225 |
vikas |
66 |
private Map<String, String> snippets;
|
| 5094 |
phani.kuma |
67 |
private static Map<Long, String> label = new HashMap<Long, String>();
|
|
|
68 |
static{
|
|
|
69 |
setRetargettingLabel();
|
|
|
70 |
}
|
| 3225 |
vikas |
71 |
|
| 375 |
ashish |
72 |
public EntityController(){
|
|
|
73 |
super();
|
|
|
74 |
}
|
| 507 |
rajveer |
75 |
|
| 974 |
vikas |
76 |
// GET /*/1000001
|
| 507 |
rajveer |
77 |
@Actions({
|
| 974 |
vikas |
78 |
@Action("/mobile-phones"),
|
| 5873 |
amit.gupta |
79 |
@Action("/cameras"),
|
| 1005 |
vikas |
80 |
@Action("/mobile-accessories"),
|
| 2460 |
rajveer |
81 |
@Action("/tablets"),
|
| 3715 |
rajveer |
82 |
@Action("/laptops"),
|
| 5182 |
varun.gupt |
83 |
@Action("/laptop-accessories"),
|
| 6593 |
amit.gupta |
84 |
@Action("/entity"),
|
| 8315 |
amit.gupta |
85 |
@Action("/music-players"),
|
|
|
86 |
@Action("/smart-watches")
|
| 507 |
rajveer |
87 |
})
|
| 2306 |
vikas |
88 |
public String show() throws SecurityException, IOException, JSONException {
|
| 317 |
ashish |
89 |
log.info("id=" + id);
|
| 637 |
rajveer |
90 |
|
| 2306 |
vikas |
91 |
String entityUrl = "";
|
|
|
92 |
String metaDescription = "";
|
|
|
93 |
String pageTitle = "";
|
| 2434 |
rajveer |
94 |
String productName = "";
|
| 3830 |
chandransh |
95 |
String categoryName = "";
|
|
|
96 |
String categoryUrl = "";
|
| 2652 |
rajveer |
97 |
String displayAccessories = "FALSE";
|
| 3830 |
chandransh |
98 |
String breadCrumb = "";
|
| 5347 |
amit.gupta |
99 |
String parentCategory = "";
|
| 5930 |
amit.gupta |
100 |
String compareCategory = "";
|
| 2306 |
vikas |
101 |
try {
|
| 3225 |
vikas |
102 |
setSnippets();
|
|
|
103 |
htmlSnippets.put("PRODUCT_PROPERTIES", snippets.get(PRODUCT_PROERTIES_SNIPPET_KEY));
|
|
|
104 |
JSONObject productPropertiesInJson = new JSONObject(htmlSnippets.get("PRODUCT_PROPERTIES"));
|
| 2306 |
vikas |
105 |
entityUrl = productPropertiesInJson.getString("entityUrl");
|
|
|
106 |
metaDescription = productPropertiesInJson.getString("metaDescription");
|
|
|
107 |
pageTitle = productPropertiesInJson.getString("title");
|
| 2434 |
rajveer |
108 |
productName = productPropertiesInJson.getString("name");
|
| 3830 |
chandransh |
109 |
categoryName = productPropertiesInJson.getString("categoryName");
|
|
|
110 |
categoryUrl = productPropertiesInJson.getString("categoryUrl");
|
| 2652 |
rajveer |
111 |
displayAccessories = productPropertiesInJson.getString("displayAccessories");
|
| 3830 |
chandransh |
112 |
breadCrumb = productPropertiesInJson.getString("breadCrumb");
|
| 5347 |
amit.gupta |
113 |
parentCategory = productPropertiesInJson.getString("parentCategory");
|
| 5930 |
amit.gupta |
114 |
compareCategory = productPropertiesInJson.getString("compareCategory");
|
| 2306 |
vikas |
115 |
}
|
|
|
116 |
catch (JSONException e) {
|
| 4965 |
rajveer |
117 |
log.warn("Unable to parse product properties JSON", e);
|
| 2306 |
vikas |
118 |
try {
|
| 3126 |
rajveer |
119 |
CatalogClient catalogClientService = new CatalogClient();
|
| 5945 |
mandeep.dh |
120 |
in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
|
| 2306 |
vikas |
121 |
|
|
|
122 |
Item item = client.getItemsByCatalogId(productId).get(0);
|
| 2307 |
vikas |
123 |
redirectUrl = "/" + item.getBrand().toLowerCase().replace(" ", "-");
|
| 2306 |
vikas |
124 |
} catch (Exception e1) {
|
| 4965 |
rajveer |
125 |
log.warn("Unable to get items by catalog id", e1);
|
| 2306 |
vikas |
126 |
redirectUrl = "/";
|
|
|
127 |
}
|
|
|
128 |
log.info(redirectUrl);
|
|
|
129 |
return "redirect";
|
|
|
130 |
}
|
|
|
131 |
String currentUrl = request.getRequestURL().toString();
|
| 1197 |
varun.gupt |
132 |
|
| 2306 |
vikas |
133 |
if (!currentUrl.contains(entityUrl)) {
|
|
|
134 |
redirectUrl = entityUrl;
|
|
|
135 |
return "redirect";
|
|
|
136 |
}
|
|
|
137 |
|
| 2434 |
rajveer |
138 |
//Extracting base url
|
| 1689 |
rajveer |
139 |
String rootUrl = currentUrl.split("/")[2];
|
| 2194 |
varun.gupt |
140 |
|
| 3225 |
vikas |
141 |
htmlSnippets.put("PRODUCT_SUMMARY", snippets.get(PRODUCT_SUMMARY_SNIPPET_KEY));
|
| 1258 |
varun.gupt |
142 |
htmlSnippets.put("PRODUCT_ID", productId + "");
|
|
|
143 |
htmlSnippets.put("PRODUCT_NAME", productName);
|
| 3830 |
chandransh |
144 |
htmlSnippets.put("CATEGORY_NAME", categoryName);
|
|
|
145 |
htmlSnippets.put("CATEGORY_URL", categoryUrl);
|
| 1689 |
rajveer |
146 |
htmlSnippets.put("PRODUCT_URL", currentUrl);
|
| 2194 |
varun.gupt |
147 |
htmlSnippets.put("ROOT_URL", "http://" + rootUrl);
|
| 3225 |
vikas |
148 |
htmlSnippets.put("SLIDE_GUIDE", snippets.get(PRODUCT_SLIDEGUIDE_KEY));
|
| 1269 |
varun.gupt |
149 |
htmlSnippets.put("PAGE_TITLE", pageTitle.trim());
|
| 2306 |
vikas |
150 |
htmlSnippets.put("PAGE_METADESC", metaDescription);
|
| 3830 |
chandransh |
151 |
htmlSnippets.put("BREADCRUMB", breadCrumb);
|
| 5347 |
amit.gupta |
152 |
htmlSnippets.put("PARENT_CATEGORY", parentCategory);
|
| 5930 |
amit.gupta |
153 |
htmlSnippets.put("COMPARE_CATEGORY", compareCategory);
|
| 3830 |
chandransh |
154 |
|
| 2652 |
rajveer |
155 |
if(displayAccessories.equals("TRUE")){
|
| 2867 |
rajveer |
156 |
setMobile(true);
|
| 2652 |
rajveer |
157 |
}
|
|
|
158 |
|
| 650 |
rajveer |
159 |
try {
|
| 5188 |
rajveer |
160 |
if(userinfo.getUserId() != -1){
|
|
|
161 |
long itemId = Long.parseLong(id);
|
|
|
162 |
UserClient userServiceClient = new UserClient();
|
|
|
163 |
Client client = userServiceClient.getClient();
|
|
|
164 |
client.updateBrowseHistory(userinfo.getUserId(), itemId);
|
| 1511 |
rajveer |
165 |
}
|
| 650 |
rajveer |
166 |
|
| 449 |
rajveer |
167 |
} catch (Exception e) {
|
| 2949 |
chandransh |
168 |
log.warn("Unable to update the browsing history because of: ", e);
|
| 449 |
rajveer |
169 |
}
|
| 4494 |
varun.gupt |
170 |
|
| 3185 |
vikas |
171 |
DataLogger.logData(EventType.PRODUCT_VIEW, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
|
| 5026 |
varun.gupt |
172 |
productName, Long.toString(productId), StringUtils.substring(request.getHeader("referer"), 0, 500));
|
| 2149 |
vikas |
173 |
return "show";
|
| 317 |
ashish |
174 |
}
|
| 3225 |
vikas |
175 |
|
| 317 |
ashish |
176 |
/**
|
|
|
177 |
*
|
|
|
178 |
* @param id
|
|
|
179 |
*/
|
|
|
180 |
public void setId(String id) {
|
| 4831 |
varun.gupt |
181 |
String[] tokens = id.split("-");
|
|
|
182 |
this.id = tokens[tokens.length - 1];
|
| 974 |
vikas |
183 |
this.productId = Long.parseLong(this.id);
|
| 317 |
ashish |
184 |
}
|
|
|
185 |
|
| 387 |
rajveer |
186 |
public Map<String,String> getHtmlSnippets(){
|
| 388 |
rajveer |
187 |
System.out.println(" getHtmlSnippets is called");
|
| 387 |
rajveer |
188 |
return htmlSnippets;
|
| 375 |
ashish |
189 |
}
|
| 388 |
rajveer |
190 |
|
| 449 |
rajveer |
191 |
public String getSlideGuideSnippet(){
|
| 388 |
rajveer |
192 |
return htmlSnippets.get("SLIDE_GUIDE");
|
|
|
193 |
}
|
|
|
194 |
|
| 2306 |
vikas |
195 |
public String getRedirectUrl(){
|
| 5493 |
rajveer |
196 |
String query = request.getQueryString();
|
|
|
197 |
if(query!=null){
|
|
|
198 |
return redirectUrl + "?" + query;
|
|
|
199 |
}
|
|
|
200 |
return redirectUrl;
|
| 2306 |
vikas |
201 |
}
|
|
|
202 |
|
| 449 |
rajveer |
203 |
public String getProductSummarySnippet(){
|
|
|
204 |
return htmlSnippets.get("PRODUCT_SUMMARY");
|
|
|
205 |
}
|
|
|
206 |
|
| 974 |
vikas |
207 |
public String getPageTitleSnippet(){
|
|
|
208 |
return htmlSnippets.get("PAGE_TITLE");
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
public String getPageMetaDescSnippet(){
|
|
|
212 |
return htmlSnippets.get("PAGE_METADESC");
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
|
| 1197 |
varun.gupt |
216 |
public String getProductName() {
|
|
|
217 |
return htmlSnippets.get("PRODUCT_NAME");
|
|
|
218 |
}
|
| 5347 |
amit.gupta |
219 |
|
| 5930 |
amit.gupta |
220 |
public String getCompareCategory(){
|
|
|
221 |
return htmlSnippets.get("COMPARE_CATEGORY");
|
| 5347 |
amit.gupta |
222 |
}
|
|
|
223 |
|
| 5930 |
amit.gupta |
224 |
public String getCompareCategoryLowerCase(){
|
|
|
225 |
String compareCategory = htmlSnippets.get("COMPARE_CATEGORY");
|
|
|
226 |
return compareCategory.toLowerCase();
|
| 5347 |
amit.gupta |
227 |
}
|
| 1258 |
varun.gupt |
228 |
|
| 3830 |
chandransh |
229 |
public String getCategoryName() {
|
|
|
230 |
return htmlSnippets.get("CATEGORY_NAME");
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
public String getCategoryUrl() {
|
|
|
234 |
return htmlSnippets.get("CATEGORY_URL");
|
|
|
235 |
}
|
|
|
236 |
|
| 1258 |
varun.gupt |
237 |
public String getProductId() {
|
|
|
238 |
return htmlSnippets.get("PRODUCT_ID");
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
public String getProductUrl() {
|
|
|
242 |
return htmlSnippets.get("PRODUCT_URL");
|
|
|
243 |
}
|
| 1364 |
varun.gupt |
244 |
|
|
|
245 |
public String getRootUrl() {
|
|
|
246 |
return htmlSnippets.get("ROOT_URL");
|
|
|
247 |
}
|
| 2867 |
rajveer |
248 |
|
| 3830 |
chandransh |
249 |
public String getBreadCrumb(){
|
|
|
250 |
return htmlSnippets.get("BREADCRUMB");
|
|
|
251 |
}
|
|
|
252 |
|
| 2867 |
rajveer |
253 |
/**
|
|
|
254 |
* @param isMobile the isMobile to set
|
|
|
255 |
*/
|
|
|
256 |
public void setMobile(boolean isMobile) {
|
|
|
257 |
this.isMobile = isMobile;
|
| 2652 |
rajveer |
258 |
}
|
| 2867 |
rajveer |
259 |
|
|
|
260 |
/**
|
|
|
261 |
* @return the isMobile
|
|
|
262 |
*/
|
|
|
263 |
public boolean isMobile() {
|
|
|
264 |
return isMobile;
|
|
|
265 |
}
|
| 3225 |
vikas |
266 |
|
|
|
267 |
private void setSnippets() {
|
|
|
268 |
EhcacheWrapper<Long, Map<String, String>> productSnippetsCache = new EhcacheWrapper<Long, Map<String, String>>(
|
|
|
269 |
EhcacheWrapper.PRODUCT_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
|
| 3561 |
rajveer |
270 |
if(sourceId == -1){
|
|
|
271 |
snippets = productSnippetsCache.get(productId);
|
|
|
272 |
}
|
| 3225 |
vikas |
273 |
if (snippets == null) {
|
|
|
274 |
log.info("Getting product snippet for :" + productId);
|
|
|
275 |
snippets = new HashMap<String, String>();
|
| 3561 |
rajveer |
276 |
snippets.put(PRODUCT_PROERTIES_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, productId+"", sourceId));
|
|
|
277 |
snippets.put(PRODUCT_SUMMARY_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_DETAIL_SNIPPET, productId+"", sourceId));
|
|
|
278 |
snippets.put(PRODUCT_SLIDEGUIDE_KEY, ContentServingService.getSnippet(SnippetType.SLIDE_GUIDE_SNIPPET, productId+"", sourceId));
|
|
|
279 |
if(sourceId == -1){
|
|
|
280 |
productSnippetsCache.put(productId, snippets);
|
|
|
281 |
}
|
| 3225 |
vikas |
282 |
return;
|
|
|
283 |
}
|
|
|
284 |
log.info("Loaded from cache product snippet for :" + productId);
|
|
|
285 |
}
|
| 4494 |
varun.gupt |
286 |
|
|
|
287 |
public Map<String, Double> getDiscounts() {
|
|
|
288 |
return discounts;
|
|
|
289 |
}
|
| 4934 |
amit.gupta |
290 |
|
|
|
291 |
public String getEntityLogisticsEstimation(){
|
| 9840 |
amit.gupta |
292 |
List<ItemText> itemTexts = null;
|
| 4934 |
amit.gupta |
293 |
try {
|
|
|
294 |
LogisticsClient cl = new LogisticsClient();
|
|
|
295 |
LogisticsService.Client client = cl.getClient();
|
| 9840 |
amit.gupta |
296 |
itemTexts = client.getEntityLogisticsEstimation(productId, DEFAULT_PINCODE, DeliveryType.PREPAID );
|
| 4934 |
amit.gupta |
297 |
} catch (Exception e1) {
|
|
|
298 |
log.error("Unable to get items by catalog item id", e1);
|
|
|
299 |
}
|
| 9840 |
amit.gupta |
300 |
List<Long> items = new ArrayList<Long>();
|
|
|
301 |
for(ItemText itemText : itemTexts) {
|
|
|
302 |
items.add(itemText.getItemId());
|
|
|
303 |
}
|
| 4934 |
amit.gupta |
304 |
return new Gson().toJson(items);
|
|
|
305 |
}
|
| 5094 |
phani.kuma |
306 |
|
|
|
307 |
private static void setRetargettingLabel() {
|
|
|
308 |
label.put((long) 1002160, "6EIBCKjf1wMQ6Pua0wM"); //Samsung Galaxy Y S5360
|
|
|
309 |
label.put((long) 1002106, "JFGVCKCt8gMQ6Pua0wM"); //Sony Ericsson Xperia Neo V MT11i
|
|
|
310 |
label.put((long) 1003230, "ArAYCJiu8gMQ6Pua0wM"); //Micromax Funbook Tab P300
|
|
|
311 |
label.put((long) 1002151, "j0c_CJCv8gMQ6Pua0wM"); //HTC Explorer (Pico) A310e
|
|
|
312 |
label.put((long) 1000607, "7H3QCKDh8wMQ6Pua0wM"); //SanDisk MicroSD 8GB
|
|
|
313 |
label.put((long) 1003213, "QtGQCJji8wMQ6Pua0wM"); //Micromax SUPERFONE Gossip A78
|
|
|
314 |
label.put((long) 1002530, "0YNbCJDj8wMQ6Pua0wM"); //Samsung Champ Deluxe Duos C3312
|
|
|
315 |
label.put((long) 1003039, "pQUaCIjk8wMQ6Pua0wM"); //Samsung Star 3 Duos S5222
|
|
|
316 |
label.put((long) 1002575, "zcYTCIDl8wMQ6Pua0wM"); //Samsung Galaxy Y Duos S6102
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
public String getRetargettingLabel() {
|
|
|
320 |
return label.get(this.productId);
|
|
|
321 |
}
|
| 7853 |
amit.gupta |
322 |
|
|
|
323 |
|
|
|
324 |
public String getEmiObject(){
|
|
|
325 |
return ProceedToPayController.getEmiSchemesInJSON();
|
|
|
326 |
}
|
| 5491 |
rajveer |
327 |
|
| 1197 |
varun.gupt |
328 |
}
|