Subversion Repositories SmartDukaan

Rev

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