Subversion Repositories SmartDukaan

Rev

Rev 10190 | Rev 11672 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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 = "";
10190 amit.gupta 101
    	String isComparable = "false";
2306 vikas 102
    	try {
3225 vikas 103
    	    setSnippets();
104
            htmlSnippets.put("PRODUCT_PROPERTIES", snippets.get(PRODUCT_PROERTIES_SNIPPET_KEY));
105
    	    JSONObject productPropertiesInJson = new JSONObject(htmlSnippets.get("PRODUCT_PROPERTIES"));
2306 vikas 106
    	    entityUrl = productPropertiesInJson.getString("entityUrl");
107
    	    metaDescription = productPropertiesInJson.getString("metaDescription");
108
    	    pageTitle = productPropertiesInJson.getString("title");
2434 rajveer 109
    	    productName = productPropertiesInJson.getString("name");
3830 chandransh 110
    	    categoryName = productPropertiesInJson.getString("categoryName");
111
    	    categoryUrl = productPropertiesInJson.getString("categoryUrl");
2652 rajveer 112
    	    displayAccessories = productPropertiesInJson.getString("displayAccessories");
3830 chandransh 113
    	    breadCrumb = productPropertiesInJson.getString("breadCrumb");
5347 amit.gupta 114
    	    parentCategory = productPropertiesInJson.getString("parentCategory");
5930 amit.gupta 115
    	    compareCategory = productPropertiesInJson.getString("compareCategory");
10190 amit.gupta 116
    	    isComparable = productPropertiesInJson.getString("isComparable");
2306 vikas 117
    	}
118
    	catch (JSONException e) {
4965 rajveer 119
            log.warn("Unable to parse product properties JSON", e);
2306 vikas 120
            try {
3126 rajveer 121
                CatalogClient catalogClientService = new CatalogClient();
5945 mandeep.dh 122
                in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
2306 vikas 123
 
124
                Item item = client.getItemsByCatalogId(productId).get(0);
2307 vikas 125
                redirectUrl = "/" + item.getBrand().toLowerCase().replace(" ", "-");
2306 vikas 126
            } catch (Exception e1) {
4965 rajveer 127
                log.warn("Unable to get items by catalog id", e1);
2306 vikas 128
                redirectUrl = "/";
129
            }
130
            log.info(redirectUrl);
131
            return "redirect";
132
       }
133
    	String currentUrl = request.getRequestURL().toString();
1197 varun.gupt 134
 
2306 vikas 135
    	if (!currentUrl.contains(entityUrl)) {
136
    	    redirectUrl = entityUrl;
137
    	    return "redirect";
138
    	}
139
 
2434 rajveer 140
    	//Extracting base url
1689 rajveer 141
    	String rootUrl = currentUrl.split("/")[2];
2194 varun.gupt 142
 
3225 vikas 143
    	htmlSnippets.put("PRODUCT_SUMMARY", snippets.get(PRODUCT_SUMMARY_SNIPPET_KEY));
1258 varun.gupt 144
    	htmlSnippets.put("PRODUCT_ID", productId + "");
145
    	htmlSnippets.put("PRODUCT_NAME", productName);
3830 chandransh 146
    	htmlSnippets.put("CATEGORY_NAME", categoryName);
147
    	htmlSnippets.put("CATEGORY_URL", categoryUrl);
1689 rajveer 148
    	htmlSnippets.put("PRODUCT_URL", currentUrl);
2194 varun.gupt 149
    	htmlSnippets.put("ROOT_URL", "http://" + rootUrl);
3225 vikas 150
		htmlSnippets.put("SLIDE_GUIDE", snippets.get(PRODUCT_SLIDEGUIDE_KEY));
1269 varun.gupt 151
		htmlSnippets.put("PAGE_TITLE", pageTitle.trim());
2306 vikas 152
		htmlSnippets.put("PAGE_METADESC", metaDescription);
3830 chandransh 153
		htmlSnippets.put("BREADCRUMB", breadCrumb);
5347 amit.gupta 154
		htmlSnippets.put("PARENT_CATEGORY", parentCategory);
5930 amit.gupta 155
		htmlSnippets.put("COMPARE_CATEGORY", compareCategory);
10190 amit.gupta 156
		htmlSnippets.put("IS_COMPARABLE", isComparable);
3830 chandransh 157
 
2652 rajveer 158
		if(displayAccessories.equals("TRUE")){
2867 rajveer 159
			setMobile(true);
2652 rajveer 160
		}
161
 
650 rajveer 162
		try {
5188 rajveer 163
			if(userinfo.getUserId() != -1){
164
				long itemId = Long.parseLong(id);
165
				UserClient userServiceClient = new UserClient();
166
				Client client = userServiceClient.getClient();
167
				client.updateBrowseHistory(userinfo.getUserId(), itemId);
1511 rajveer 168
			}
650 rajveer 169
 
449 rajveer 170
		} catch (Exception e) {
2949 chandransh 171
			log.warn("Unable to update the browsing history because of: ", e);
449 rajveer 172
		}
4494 varun.gupt 173
 
3185 vikas 174
		DataLogger.logData(EventType.PRODUCT_VIEW, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
5026 varun.gupt 175
                productName, Long.toString(productId), StringUtils.substring(request.getHeader("referer"), 0, 500));
2149 vikas 176
		return "show";
317 ashish 177
    }
3225 vikas 178
 
317 ashish 179
    /**
180
     * 
181
     * @param id
182
     */
183
    public void setId(String id) {
4831 varun.gupt 184
    	String[] tokens = id.split("-");
185
    	this.id = tokens[tokens.length - 1];
974 vikas 186
    	this.productId = Long.parseLong(this.id);
317 ashish 187
    }
188
 
387 rajveer 189
	public Map<String,String> getHtmlSnippets(){
388 rajveer 190
		System.out.println(" getHtmlSnippets  is called");
387 rajveer 191
		return htmlSnippets;
375 ashish 192
	}
388 rajveer 193
 
449 rajveer 194
	public String getSlideGuideSnippet(){
388 rajveer 195
		return htmlSnippets.get("SLIDE_GUIDE");
196
	}
197
 
2306 vikas 198
	public String getRedirectUrl(){
5493 rajveer 199
		String query = request.getQueryString();
200
		if(query!=null){
201
			return redirectUrl + "?" + query;
202
		}
203
		return redirectUrl;
2306 vikas 204
    }
205
 
449 rajveer 206
	public String getProductSummarySnippet(){
207
		return htmlSnippets.get("PRODUCT_SUMMARY");
208
	}
209
 
974 vikas 210
	public String getPageTitleSnippet(){
211
		return htmlSnippets.get("PAGE_TITLE");
212
	}
213
 
214
	public String getPageMetaDescSnippet(){
215
		return htmlSnippets.get("PAGE_METADESC");
216
	}
217
 
218
 
1197 varun.gupt 219
	public String getProductName()	{
220
		return htmlSnippets.get("PRODUCT_NAME");
221
	}
5347 amit.gupta 222
 
5930 amit.gupta 223
	public String getCompareCategory(){
224
		return htmlSnippets.get("COMPARE_CATEGORY");
5347 amit.gupta 225
	}
226
 
5930 amit.gupta 227
	public String getCompareCategoryLowerCase(){
228
		String compareCategory = htmlSnippets.get("COMPARE_CATEGORY");
229
		return compareCategory.toLowerCase();
5347 amit.gupta 230
	}
1258 varun.gupt 231
 
3830 chandransh 232
	public String getCategoryName()	{
233
		return htmlSnippets.get("CATEGORY_NAME");
234
	}
235
 
236
	public String getCategoryUrl()	{
237
		return htmlSnippets.get("CATEGORY_URL");
238
	}
239
 
1258 varun.gupt 240
	public String getProductId()	{
241
		return htmlSnippets.get("PRODUCT_ID");
242
	}
243
 
244
	public String getProductUrl()	{
245
		return htmlSnippets.get("PRODUCT_URL");
246
	}
1364 varun.gupt 247
 
248
	public String getRootUrl()	{
249
		return htmlSnippets.get("ROOT_URL");
250
	}
2867 rajveer 251
 
3830 chandransh 252
	public String getBreadCrumb(){
253
		return htmlSnippets.get("BREADCRUMB");
254
	}
10190 amit.gupta 255
 
256
	public String isComparable(){
257
		return htmlSnippets.get("IS_COMPARABLE");
258
	}
3830 chandransh 259
 
2867 rajveer 260
	/**
261
	 * @param isMobile the isMobile to set
262
	 */
263
	public void setMobile(boolean isMobile) {
264
		this.isMobile = isMobile;
2652 rajveer 265
	}
2867 rajveer 266
 
267
	/**
268
	 * @return the isMobile
269
	 */
270
	public boolean isMobile() {
271
		return isMobile;
272
	}
3225 vikas 273
 
274
    private void setSnippets() {
275
        EhcacheWrapper<Long, Map<String, String>> productSnippetsCache = new EhcacheWrapper<Long, Map<String, String>>(
276
                EhcacheWrapper.PRODUCT_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
3561 rajveer 277
        if(sourceId == -1){
278
        	snippets = productSnippetsCache.get(productId);
279
        }
3225 vikas 280
        if (snippets == null) {
281
            log.info("Getting product snippet for :" + productId);
282
            snippets = new HashMap<String, String>();
3561 rajveer 283
            snippets.put(PRODUCT_PROERTIES_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, productId+"", sourceId));
284
            snippets.put(PRODUCT_SUMMARY_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_DETAIL_SNIPPET, productId+"", sourceId));
285
            snippets.put(PRODUCT_SLIDEGUIDE_KEY, ContentServingService.getSnippet(SnippetType.SLIDE_GUIDE_SNIPPET, productId+"", sourceId));
286
            if(sourceId == -1){
287
            	productSnippetsCache.put(productId, snippets);
288
            }
3225 vikas 289
            return;
290
        }
291
        log.info("Loaded from cache product snippet for :" + productId);
292
    }
4494 varun.gupt 293
 
294
    public Map<String, Double> getDiscounts()	{
295
    	return discounts;
296
    }
4934 amit.gupta 297
 
298
    public String getEntityLogisticsEstimation(){
9840 amit.gupta 299
    	List<ItemText> itemTexts = null; 
4934 amit.gupta 300
    	try {
301
    		LogisticsClient cl = new LogisticsClient();
302
            LogisticsService.Client client = cl.getClient();
9840 amit.gupta 303
            itemTexts = client.getEntityLogisticsEstimation(productId, DEFAULT_PINCODE, DeliveryType.PREPAID );
10195 amit.gupta 304
            if(itemTexts!=null){
305
            	List<Long> items = new ArrayList<Long>();
306
            	for(ItemText itemText : itemTexts) {
307
            		items.add(itemText.getItemId());
308
            	}
309
            	return new Gson().toJson(items);
310
            }
4934 amit.gupta 311
        } catch (Exception e1) {
10190 amit.gupta 312
            log.error("Unable to get items for productId: "+  productId, e1);
4934 amit.gupta 313
        }
10195 amit.gupta 314
        return "[]";
315
 
4934 amit.gupta 316
    }
5094 phani.kuma 317
 
318
    private static void setRetargettingLabel() {
319
    	label.put((long) 1002160, "6EIBCKjf1wMQ6Pua0wM"); //Samsung Galaxy Y S5360
320
    	label.put((long) 1002106, "JFGVCKCt8gMQ6Pua0wM"); //Sony Ericsson Xperia Neo V MT11i
321
    	label.put((long) 1003230, "ArAYCJiu8gMQ6Pua0wM"); //Micromax Funbook Tab P300
322
    	label.put((long) 1002151, "j0c_CJCv8gMQ6Pua0wM"); //HTC Explorer (Pico) A310e
323
    	label.put((long) 1000607, "7H3QCKDh8wMQ6Pua0wM"); //SanDisk MicroSD 8GB
324
    	label.put((long) 1003213, "QtGQCJji8wMQ6Pua0wM"); //Micromax SUPERFONE Gossip A78
325
    	label.put((long) 1002530, "0YNbCJDj8wMQ6Pua0wM"); //Samsung Champ Deluxe Duos C3312
326
    	label.put((long) 1003039, "pQUaCIjk8wMQ6Pua0wM"); //Samsung Star 3 Duos S5222
327
    	label.put((long) 1002575, "zcYTCIDl8wMQ6Pua0wM"); //Samsung Galaxy Y Duos S6102
328
    }
329
 
330
    public String getRetargettingLabel() {
331
    	return label.get(this.productId);
332
    }
7853 amit.gupta 333
 
334
 
335
    public String getEmiObject(){
336
    	return ProceedToPayController.getEmiSchemesInJSON();
337
    }
5491 rajveer 338
 
1197 varun.gupt 339
}