Subversion Repositories SmartDukaan

Rev

Rev 11672 | Rev 11964 | 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;
11672 anikendra 10
import in.shop2020.logistics.LogisticsInfo;
4934 amit.gupta 11
import in.shop2020.logistics.LogisticsService;
11672 anikendra 12
import in.shop2020.logistics.LogisticsServiceException;
2306 vikas 13
import in.shop2020.model.v1.catalog.Item;
14
import in.shop2020.model.v1.user.UserContextService.Client;
3242 vikas 15
import in.shop2020.serving.cache.EhcacheWrapper;
3561 rajveer 16
import in.shop2020.serving.services.ContentServingService;
17
import in.shop2020.serving.utils.SnippetType;
3126 rajveer 18
import in.shop2020.thrift.clients.CatalogClient;
4934 amit.gupta 19
import in.shop2020.thrift.clients.LogisticsClient;
3126 rajveer 20
import in.shop2020.thrift.clients.UserClient;
2511 vikas 21
import in.shop2020.utils.DataLogger;
375 ashish 22
 
317 ashish 23
import java.io.IOException;
11672 anikendra 24
import java.text.SimpleDateFormat;
9840 amit.gupta 25
import java.util.ArrayList;
11672 anikendra 26
import java.util.Calendar;
27
import java.util.GregorianCalendar;
3225 vikas 28
import java.util.HashMap;
4934 amit.gupta 29
import java.util.List;
375 ashish 30
import java.util.Map;
317 ashish 31
 
3225 vikas 32
import net.sf.ehcache.CacheManager;
33
 
5026 varun.gupt 34
import org.apache.commons.lang.StringUtils;
832 rajveer 35
import org.apache.log4j.Logger;
507 rajveer 36
import org.apache.struts2.convention.annotation.Action;
37
import org.apache.struts2.convention.annotation.Actions;
974 vikas 38
import org.apache.struts2.convention.annotation.Result;
2306 vikas 39
import org.apache.struts2.convention.annotation.Results;
11672 anikendra 40
import org.apache.thrift.TException;
2306 vikas 41
import org.json.JSONException;
42
import org.json.JSONObject;
317 ashish 43
 
4934 amit.gupta 44
import com.google.gson.Gson;
45
 
317 ashish 46
/**
47
 * 
650 rajveer 48
 * @author rajveer
317 ashish 49
 *
50
 */
650 rajveer 51
 
2306 vikas 52
@Results({
53
    @Result(name = "show", location = "entity-show.vm"),
5266 rajveer 54
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
2306 vikas 55
})
650 rajveer 56
public class EntityController extends BaseController {
57
 
58
	private static final long serialVersionUID = 1L;
1957 vikas 59
	private static Logger log = Logger.getLogger(Class.class);
11672 anikendra 60
	private Long firstItem =  0l;
3225 vikas 61
	private static final String PRODUCT_PROERTIES_SNIPPET_KEY = "PRODUCT_PROPERTIES";
62
	private static final String PRODUCT_SUMMARY_SNIPPET_KEY = "PRODUCT_SUMMARY";
63
	private static final String PRODUCT_SLIDEGUIDE_KEY = "SLIDEGUIDE";
11672 anikendra 64
    private String entityLogisticsEstimation = "[]";
5037 rajveer 65
	private static final String DEFAULT_PINCODE = "110001";
317 ashish 66
	private String id;
2306 vikas 67
	private String redirectUrl;
620 rajveer 68
	private long productId;
2867 rajveer 69
	private boolean isMobile = false;
70
 
6084 rajveer 71
	private Map<String, Double> discounts = new HashMap<String, Double>();
3225 vikas 72
	private Map<String, String> snippets;
5094 phani.kuma 73
	private static Map<Long, String> label = new HashMap<Long, String>();
11672 anikendra 74
 
75
	public static Map<Integer, String> businessDayToActualDateMap = new HashMap<Integer, String>();
76
	private Map<String, String> deliveryEstimate = new HashMap<String, String>();
77
 
5094 phani.kuma 78
	static{
79
		setRetargettingLabel();
80
	}
3225 vikas 81
 
375 ashish 82
	public EntityController(){
83
		super();
84
	}
507 rajveer 85
 
974 vikas 86
	// GET /*/1000001
507 rajveer 87
	@Actions({
974 vikas 88
		@Action("/mobile-phones"),
5873 amit.gupta 89
		@Action("/cameras"),
1005 vikas 90
		@Action("/mobile-accessories"),
2460 rajveer 91
		@Action("/tablets"),
3715 rajveer 92
		@Action("/laptops"),
5182 varun.gupt 93
		@Action("/laptop-accessories"),
6593 amit.gupta 94
		@Action("/entity"),
8315 amit.gupta 95
		@Action("/music-players"),
96
		@Action("/smart-watches")
507 rajveer 97
	})
2306 vikas 98
    public String show() throws SecurityException, IOException, JSONException {
317 ashish 99
    	log.info("id=" + id);
637 rajveer 100
 
2306 vikas 101
    	String entityUrl = "";
102
    	String metaDescription = "";
103
    	String pageTitle = "";
2434 rajveer 104
    	String productName = "";
3830 chandransh 105
    	String categoryName = "";
106
    	String categoryUrl = "";
2652 rajveer 107
    	String displayAccessories = "FALSE";
3830 chandransh 108
    	String breadCrumb = "";
5347 amit.gupta 109
    	String parentCategory = "";
5930 amit.gupta 110
    	String compareCategory = "";
10190 amit.gupta 111
    	String isComparable = "false";
2306 vikas 112
    	try {
3225 vikas 113
    	    setSnippets();
114
            htmlSnippets.put("PRODUCT_PROPERTIES", snippets.get(PRODUCT_PROERTIES_SNIPPET_KEY));
115
    	    JSONObject productPropertiesInJson = new JSONObject(htmlSnippets.get("PRODUCT_PROPERTIES"));
2306 vikas 116
    	    entityUrl = productPropertiesInJson.getString("entityUrl");
117
    	    metaDescription = productPropertiesInJson.getString("metaDescription");
118
    	    pageTitle = productPropertiesInJson.getString("title");
2434 rajveer 119
    	    productName = productPropertiesInJson.getString("name");
3830 chandransh 120
    	    categoryName = productPropertiesInJson.getString("categoryName");
121
    	    categoryUrl = productPropertiesInJson.getString("categoryUrl");
2652 rajveer 122
    	    displayAccessories = productPropertiesInJson.getString("displayAccessories");
3830 chandransh 123
    	    breadCrumb = productPropertiesInJson.getString("breadCrumb");
5347 amit.gupta 124
    	    parentCategory = productPropertiesInJson.getString("parentCategory");
5930 amit.gupta 125
    	    compareCategory = productPropertiesInJson.getString("compareCategory");
10190 amit.gupta 126
    	    isComparable = productPropertiesInJson.getString("isComparable");
2306 vikas 127
    	}
128
    	catch (JSONException e) {
4965 rajveer 129
            log.warn("Unable to parse product properties JSON", e);
2306 vikas 130
            try {
3126 rajveer 131
                CatalogClient catalogClientService = new CatalogClient();
5945 mandeep.dh 132
                in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
2306 vikas 133
 
134
                Item item = client.getItemsByCatalogId(productId).get(0);
2307 vikas 135
                redirectUrl = "/" + item.getBrand().toLowerCase().replace(" ", "-");
2306 vikas 136
            } catch (Exception e1) {
4965 rajveer 137
                log.warn("Unable to get items by catalog id", e1);
2306 vikas 138
                redirectUrl = "/";
139
            }
140
            log.info(redirectUrl);
141
            return "redirect";
142
       }
143
    	String currentUrl = request.getRequestURL().toString();
1197 varun.gupt 144
 
2306 vikas 145
    	if (!currentUrl.contains(entityUrl)) {
146
    	    redirectUrl = entityUrl;
147
    	    return "redirect";
148
    	}
149
 
2434 rajveer 150
    	//Extracting base url
1689 rajveer 151
    	String rootUrl = currentUrl.split("/")[2];
2194 varun.gupt 152
 
3225 vikas 153
    	htmlSnippets.put("PRODUCT_SUMMARY", snippets.get(PRODUCT_SUMMARY_SNIPPET_KEY));
1258 varun.gupt 154
    	htmlSnippets.put("PRODUCT_ID", productId + "");
155
    	htmlSnippets.put("PRODUCT_NAME", productName);
3830 chandransh 156
    	htmlSnippets.put("CATEGORY_NAME", categoryName);
157
    	htmlSnippets.put("CATEGORY_URL", categoryUrl);
1689 rajveer 158
    	htmlSnippets.put("PRODUCT_URL", currentUrl);
2194 varun.gupt 159
    	htmlSnippets.put("ROOT_URL", "http://" + rootUrl);
3225 vikas 160
		htmlSnippets.put("SLIDE_GUIDE", snippets.get(PRODUCT_SLIDEGUIDE_KEY));
1269 varun.gupt 161
		htmlSnippets.put("PAGE_TITLE", pageTitle.trim());
2306 vikas 162
		htmlSnippets.put("PAGE_METADESC", metaDescription);
3830 chandransh 163
		htmlSnippets.put("BREADCRUMB", breadCrumb);
5347 amit.gupta 164
		htmlSnippets.put("PARENT_CATEGORY", parentCategory);
5930 amit.gupta 165
		htmlSnippets.put("COMPARE_CATEGORY", compareCategory);
10190 amit.gupta 166
		htmlSnippets.put("IS_COMPARABLE", isComparable);
3830 chandransh 167
 
2652 rajveer 168
		if(displayAccessories.equals("TRUE")){
2867 rajveer 169
			setMobile(true);
2652 rajveer 170
		}
171
 
650 rajveer 172
		try {
5188 rajveer 173
			if(userinfo.getUserId() != -1){
174
				long itemId = Long.parseLong(id);
175
				UserClient userServiceClient = new UserClient();
176
				Client client = userServiceClient.getClient();
177
				client.updateBrowseHistory(userinfo.getUserId(), itemId);
1511 rajveer 178
			}
650 rajveer 179
 
449 rajveer 180
		} catch (Exception e) {
2949 chandransh 181
			log.warn("Unable to update the browsing history because of: ", e);
449 rajveer 182
		}
4494 varun.gupt 183
 
11672 anikendra 184
		DataLogger.logData(EventType.PRODUCT_VIEW, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), userinfo.getPincode(), 
185
                productName, Long.toString(productId), StringUtils.substring(request.getHeader("referer"), 0, 500));			       
186
 
187
        try {
188
            CatalogClient catalogClientService = new CatalogClient();
189
            in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
190
 
11676 amit.gupta 191
            setEntityLogisticsEstimation();
11672 anikendra 192
            getDeliveryEstimate( firstItem, userinfo.getPincode());
193
        } catch (Exception e1) {
194
            log.warn("Unable to get delivery estimates for "+ id);
195
        }
2149 vikas 196
		return "show";
317 ashish 197
    }
11672 anikendra 198
 
199
	/**
200
	 * 
201
	 * @return
202
	 */
203
	public void getDeliveryEstimate(Long itemId, String pincode){
204
		LogisticsClient logisticsServiceClient = null;
205
		String days = "-1";
206
		long businessDays = -1;
207
		boolean isCODAvailable=false;
208
		boolean isOTGAvailable=false;
209
		String codDays = "-1";
210
    	try {
211
			logisticsServiceClient = new LogisticsClient();
212
			LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
213
			LogisticsInfo logistincInfo = logisticsClient.getLogisticsEstimation(itemId, pincode, DeliveryType.PREPAID);
214
 
215
			if(logistincInfo.getDeliveryTime()!=-1L){
216
				days = getDeliveryDateString((int)logistincInfo.getDeliveryTime(), DeliveryType.PREPAID);
217
				if(logistincInfo.isCodAllowed()){
218
					codDays = getDeliveryDateString((int)(logistincInfo.getDeliveryTime()), DeliveryType.COD);
219
				}
220
			}
221
			businessDays = logistincInfo.getDeliveryTime();
222
			isCODAvailable = logistincInfo.isCodAllowed();
223
			isOTGAvailable = logistincInfo.isOtgAvailable();
224
 
225
			log.info("busines days = " + businessDays + "is COD avaialbale " + isCODAvailable + "is OTG available " + isOTGAvailable + " days "+days+" cod days "+codDays);
226
    	} catch (LogisticsServiceException e)	{
227
    		log.error("Unable to get estimate/COD availability for " + itemId, e);
228
		} catch(TException e)	{
229
 
230
		} catch (Exception e)	{
231
 
232
		}
233
		deliveryEstimate.put("delivery_estimate", days);
234
		deliveryEstimate.put("cod_delivery_estimate", codDays);
235
		deliveryEstimate.put("is_cod_available_for_location", Boolean.toString(isCODAvailable));
236
		deliveryEstimate.put("on_time_guarantee", Boolean.toString(isOTGAvailable));
237
		deliveryEstimate.put("business_days", businessDays + "");        	
238
	}
239
 
240
	private  static String getDeliveryDateString(int days, DeliveryType type) throws TException {
241
    	Calendar now = new GregorianCalendar();
242
    	int hour = now.get(Calendar.HOUR_OF_DAY);
243
    	if(type == DeliveryType.COD && hour < 15){
244
			days = days + 1;
245
		}
246
    	if(businessDayToActualDateMap.containsKey(days)){
247
    		if(hour != 0){	
248
    			return businessDayToActualDateMap.get(days);
249
    		}
250
    		businessDayToActualDateMap.clear();
251
		}
252
 
253
    	now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
254
 
255
    	LogisticsClient logisticsServiceClient = null;
256
		logisticsServiceClient = new LogisticsClient();
257
		LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
258
 
259
    	int newdays = (int) logisticsClient.adjustDeliveryDays(now.getTimeInMillis(), days);
260
    	now.add(Calendar.DAY_OF_MONTH, newdays);
261
 
262
    	SimpleDateFormat dateformat = new SimpleDateFormat("EEE dd-MMM-yy");
263
    	if(newdays == 1){
264
    		businessDayToActualDateMap.put(days, "Tomorrow, " + dateformat.format(now.getTime()));
265
    	}else{
266
    		businessDayToActualDateMap.put(days, dateformat.format(now.getTime()));
267
    	}
268
    	return businessDayToActualDateMap.get(days);
269
	}
3225 vikas 270
 
317 ashish 271
    /**
272
     * 
273
     * @param id
274
     */
275
    public void setId(String id) {
4831 varun.gupt 276
    	String[] tokens = id.split("-");
277
    	this.id = tokens[tokens.length - 1];
974 vikas 278
    	this.productId = Long.parseLong(this.id);
317 ashish 279
    }
280
 
387 rajveer 281
	public Map<String,String> getHtmlSnippets(){
388 rajveer 282
		System.out.println(" getHtmlSnippets  is called");
387 rajveer 283
		return htmlSnippets;
375 ashish 284
	}
388 rajveer 285
 
449 rajveer 286
	public String getSlideGuideSnippet(){
388 rajveer 287
		return htmlSnippets.get("SLIDE_GUIDE");
288
	}
289
 
2306 vikas 290
	public String getRedirectUrl(){
5493 rajveer 291
		String query = request.getQueryString();
292
		if(query!=null){
293
			return redirectUrl + "?" + query;
294
		}
295
		return redirectUrl;
2306 vikas 296
    }
297
 
449 rajveer 298
	public String getProductSummarySnippet(){
299
		return htmlSnippets.get("PRODUCT_SUMMARY");
300
	}
301
 
974 vikas 302
	public String getPageTitleSnippet(){
303
		return htmlSnippets.get("PAGE_TITLE");
304
	}
305
 
306
	public String getPageMetaDescSnippet(){
307
		return htmlSnippets.get("PAGE_METADESC");
308
	}
309
 
310
 
1197 varun.gupt 311
	public String getProductName()	{
312
		return htmlSnippets.get("PRODUCT_NAME");
313
	}
5347 amit.gupta 314
 
5930 amit.gupta 315
	public String getCompareCategory(){
316
		return htmlSnippets.get("COMPARE_CATEGORY");
5347 amit.gupta 317
	}
318
 
5930 amit.gupta 319
	public String getCompareCategoryLowerCase(){
320
		String compareCategory = htmlSnippets.get("COMPARE_CATEGORY");
321
		return compareCategory.toLowerCase();
5347 amit.gupta 322
	}
1258 varun.gupt 323
 
3830 chandransh 324
	public String getCategoryName()	{
325
		return htmlSnippets.get("CATEGORY_NAME");
326
	}
327
 
328
	public String getCategoryUrl()	{
329
		return htmlSnippets.get("CATEGORY_URL");
330
	}
331
 
1258 varun.gupt 332
	public String getProductId()	{
333
		return htmlSnippets.get("PRODUCT_ID");
334
	}
335
 
336
	public String getProductUrl()	{
337
		return htmlSnippets.get("PRODUCT_URL");
338
	}
1364 varun.gupt 339
 
340
	public String getRootUrl()	{
341
		return htmlSnippets.get("ROOT_URL");
342
	}
2867 rajveer 343
 
3830 chandransh 344
	public String getBreadCrumb(){
345
		return htmlSnippets.get("BREADCRUMB");
346
	}
10190 amit.gupta 347
 
348
	public String isComparable(){
349
		return htmlSnippets.get("IS_COMPARABLE");
350
	}
3830 chandransh 351
 
2867 rajveer 352
	/**
353
	 * @param isMobile the isMobile to set
354
	 */
355
	public void setMobile(boolean isMobile) {
356
		this.isMobile = isMobile;
2652 rajveer 357
	}
2867 rajveer 358
 
359
	/**
360
	 * @return the isMobile
361
	 */
362
	public boolean isMobile() {
363
		return isMobile;
364
	}
3225 vikas 365
 
366
    private void setSnippets() {
367
        EhcacheWrapper<Long, Map<String, String>> productSnippetsCache = new EhcacheWrapper<Long, Map<String, String>>(
368
                EhcacheWrapper.PRODUCT_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
3561 rajveer 369
        if(sourceId == -1){
370
        	snippets = productSnippetsCache.get(productId);
371
        }
3225 vikas 372
        if (snippets == null) {
373
            log.info("Getting product snippet for :" + productId);
374
            snippets = new HashMap<String, String>();
3561 rajveer 375
            snippets.put(PRODUCT_PROERTIES_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, productId+"", sourceId));
376
            snippets.put(PRODUCT_SUMMARY_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_DETAIL_SNIPPET, productId+"", sourceId));
377
            snippets.put(PRODUCT_SLIDEGUIDE_KEY, ContentServingService.getSnippet(SnippetType.SLIDE_GUIDE_SNIPPET, productId+"", sourceId));
378
            if(sourceId == -1){
379
            	productSnippetsCache.put(productId, snippets);
380
            }
3225 vikas 381
            return;
382
        }
383
        log.info("Loaded from cache product snippet for :" + productId);
384
    }
4494 varun.gupt 385
 
386
    public Map<String, Double> getDiscounts()	{
387
    	return discounts;
388
    }
4934 amit.gupta 389
 
11672 anikendra 390
    public void setEntityLogisticsEstimation(){
9840 amit.gupta 391
    	List<ItemText> itemTexts = null; 
4934 amit.gupta 392
    	try {
393
    		LogisticsClient cl = new LogisticsClient();
394
            LogisticsService.Client client = cl.getClient();
9840 amit.gupta 395
            itemTexts = client.getEntityLogisticsEstimation(productId, DEFAULT_PINCODE, DeliveryType.PREPAID );
10195 amit.gupta 396
            if(itemTexts!=null){
397
            	List<Long> items = new ArrayList<Long>();
398
            	for(ItemText itemText : itemTexts) {
399
            		items.add(itemText.getItemId());
400
            	}
11672 anikendra 401
            	firstItem = items.get(0);
402
            	entityLogisticsEstimation = new Gson().toJson(items);
403
            	return;
10195 amit.gupta 404
            }
4934 amit.gupta 405
        } catch (Exception e1) {
10190 amit.gupta 406
            log.error("Unable to get items for productId: "+  productId, e1);
4934 amit.gupta 407
        }
11672 anikendra 408
        entityLogisticsEstimation = "[]";
10195 amit.gupta 409
 
4934 amit.gupta 410
    }
5094 phani.kuma 411
 
11672 anikendra 412
    public String getEntityLogisticsEstimation() {
413
    	return entityLogisticsEstimation;
414
    }
415
 
5094 phani.kuma 416
    private static void setRetargettingLabel() {
417
    	label.put((long) 1002160, "6EIBCKjf1wMQ6Pua0wM"); //Samsung Galaxy Y S5360
418
    	label.put((long) 1002106, "JFGVCKCt8gMQ6Pua0wM"); //Sony Ericsson Xperia Neo V MT11i
419
    	label.put((long) 1003230, "ArAYCJiu8gMQ6Pua0wM"); //Micromax Funbook Tab P300
420
    	label.put((long) 1002151, "j0c_CJCv8gMQ6Pua0wM"); //HTC Explorer (Pico) A310e
421
    	label.put((long) 1000607, "7H3QCKDh8wMQ6Pua0wM"); //SanDisk MicroSD 8GB
422
    	label.put((long) 1003213, "QtGQCJji8wMQ6Pua0wM"); //Micromax SUPERFONE Gossip A78
423
    	label.put((long) 1002530, "0YNbCJDj8wMQ6Pua0wM"); //Samsung Champ Deluxe Duos C3312
424
    	label.put((long) 1003039, "pQUaCIjk8wMQ6Pua0wM"); //Samsung Star 3 Duos S5222
425
    	label.put((long) 1002575, "zcYTCIDl8wMQ6Pua0wM"); //Samsung Galaxy Y Duos S6102
426
    }
427
 
428
    public String getRetargettingLabel() {
429
    	return label.get(this.productId);
430
    }
7853 amit.gupta 431
 
432
 
433
    public String getEmiObject(){
434
    	return ProceedToPayController.getEmiSchemesInJSON();
435
    }
5491 rajveer 436
 
11672 anikendra 437
    public String getDeliveryEstimate() {
438
    	return new Gson().toJson(this.deliveryEstimate);
439
    }
1197 varun.gupt 440
}