Subversion Repositories SmartDukaan

Rev

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