Subversion Repositories SmartDukaan

Rev

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