Subversion Repositories SmartDukaan

Rev

Rev 5266 | Rev 5491 | 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;
4494 varun.gupt 17
import in.shop2020.thrift.clients.PromotionClient;
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;
3225 vikas 22
import java.util.HashMap;
4934 amit.gupta 23
import java.util.List;
375 ashish 24
import java.util.Map;
317 ashish 25
 
3225 vikas 26
import net.sf.ehcache.CacheManager;
27
 
5026 varun.gupt 28
import org.apache.commons.lang.StringUtils;
832 rajveer 29
import org.apache.log4j.Logger;
507 rajveer 30
import org.apache.struts2.convention.annotation.Action;
31
import org.apache.struts2.convention.annotation.Actions;
974 vikas 32
import org.apache.struts2.convention.annotation.Result;
2306 vikas 33
import org.apache.struts2.convention.annotation.Results;
34
import org.json.JSONException;
35
import org.json.JSONObject;
317 ashish 36
 
4934 amit.gupta 37
import com.google.gson.Gson;
38
 
317 ashish 39
/**
40
 * 
650 rajveer 41
 * @author rajveer
317 ashish 42
 *
43
 */
650 rajveer 44
 
2306 vikas 45
@Results({
46
    @Result(name = "show", location = "entity-show.vm"),
5266 rajveer 47
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
2306 vikas 48
})
650 rajveer 49
public class EntityController extends BaseController {
50
 
51
	private static final long serialVersionUID = 1L;
1957 vikas 52
	private static Logger log = Logger.getLogger(Class.class);
3225 vikas 53
 
54
	private static final String PRODUCT_PROERTIES_SNIPPET_KEY = "PRODUCT_PROPERTIES";
55
	private static final String PRODUCT_SUMMARY_SNIPPET_KEY = "PRODUCT_SUMMARY";
56
	private static final String PRODUCT_SLIDEGUIDE_KEY = "SLIDEGUIDE";
5026 varun.gupt 57
 
5037 rajveer 58
	private static final String DEFAULT_PINCODE = "110001";
317 ashish 59
	private String id;
2306 vikas 60
	private String redirectUrl;
620 rajveer 61
	private long productId;
2867 rajveer 62
	private boolean isMobile = false;
63
 
4494 varun.gupt 64
	private Map<String, Double> discounts = null;
3225 vikas 65
	private Map<String, String> snippets;
5094 phani.kuma 66
	private static Map<Long, String> label = new HashMap<Long, String>();
67
	static{
68
		setRetargettingLabel();
69
	}
3225 vikas 70
 
375 ashish 71
	public EntityController(){
72
		super();
73
	}
507 rajveer 74
 
974 vikas 75
	// GET /*/1000001
507 rajveer 76
	@Actions({
974 vikas 77
		@Action("/mobile-phones"),
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 metaKeywords = "";
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 = "";
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
    	    metaKeywords = productPropertiesInJson.getString("metaKeywords");
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");
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();
2306 vikas 116
                in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
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);
147
		htmlSnippets.put("PAGE_METAKEYWORDS", metaKeywords);
3830 chandransh 148
		htmlSnippets.put("BREADCRUMB", breadCrumb);
5347 amit.gupta 149
		htmlSnippets.put("PARENT_CATEGORY", parentCategory);
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
 
167
		try	{
168
			PromotionClient promotionServiceClient = new PromotionClient();
169
			in.shop2020.model.v1.user.PromotionService.Client promotionClient = promotionServiceClient.getClient();
170
 
171
			discounts = promotionClient.getDiscountsForEntity(productId);
172
 
173
		} catch (Exception e) {
174
			log.error("Unable to retrieve discounts", e);
175
		}
449 rajveer 176
 
3185 vikas 177
		DataLogger.logData(EventType.PRODUCT_VIEW, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
5026 varun.gupt 178
                productName, Long.toString(productId), StringUtils.substring(request.getHeader("referer"), 0, 500));
2149 vikas 179
		return "show";
317 ashish 180
    }
3225 vikas 181
 
317 ashish 182
    /**
183
     * 
184
     * @param id
185
     */
186
    public void setId(String id) {
4831 varun.gupt 187
    	String[] tokens = id.split("-");
188
    	this.id = tokens[tokens.length - 1];
974 vikas 189
    	this.productId = Long.parseLong(this.id);
317 ashish 190
    }
191
 
387 rajveer 192
	public Map<String,String> getHtmlSnippets(){
388 rajveer 193
		System.out.println(" getHtmlSnippets  is called");
387 rajveer 194
		return htmlSnippets;
375 ashish 195
	}
388 rajveer 196
 
449 rajveer 197
	public String getSlideGuideSnippet(){
388 rajveer 198
		return htmlSnippets.get("SLIDE_GUIDE");
199
	}
200
 
2306 vikas 201
	public String getRedirectUrl(){
202
        return redirectUrl;
203
    }
204
 
449 rajveer 205
	public String getProductSummarySnippet(){
206
		return htmlSnippets.get("PRODUCT_SUMMARY");
207
	}
208
 
974 vikas 209
	public String getPageTitleSnippet(){
210
		return htmlSnippets.get("PAGE_TITLE");
211
	}
212
 
213
	public String getPageMetaDescSnippet(){
214
		return htmlSnippets.get("PAGE_METADESC");
215
	}
216
 
217
	public String getPageMetaKeywordsSnippet(){
218
		return htmlSnippets.get("PAGE_METAKEYWORDS");
219
	}
220
 
1197 varun.gupt 221
	public String getProductName()	{
222
		return htmlSnippets.get("PRODUCT_NAME");
223
	}
5347 amit.gupta 224
 
225
	public String getParentCategory(){
226
		return htmlSnippets.get("PARENT_CATEGORY");
227
	}
228
 
229
	public String getParentCategoryLowerCase(){
230
		String parentCategory = htmlSnippets.get("PARENT_CATEGORY");
231
		return parentCategory.substring(0,parentCategory.length()-1).toLowerCase();
232
	}
1258 varun.gupt 233
 
3830 chandransh 234
	public String getCategoryName()	{
235
		return htmlSnippets.get("CATEGORY_NAME");
236
	}
237
 
238
	public String getCategoryUrl()	{
239
		return htmlSnippets.get("CATEGORY_URL");
240
	}
241
 
1258 varun.gupt 242
	public String getProductId()	{
243
		return htmlSnippets.get("PRODUCT_ID");
244
	}
245
 
246
	public String getProductUrl()	{
247
		return htmlSnippets.get("PRODUCT_URL");
248
	}
1364 varun.gupt 249
 
250
	public String getRootUrl()	{
251
		return htmlSnippets.get("ROOT_URL");
252
	}
2867 rajveer 253
 
3830 chandransh 254
	public String getBreadCrumb(){
255
		return htmlSnippets.get("BREADCRUMB");
256
	}
257
 
2867 rajveer 258
	/**
259
	 * @param isMobile the isMobile to set
260
	 */
261
	public void setMobile(boolean isMobile) {
262
		this.isMobile = isMobile;
2652 rajveer 263
	}
2867 rajveer 264
 
265
	/**
266
	 * @return the isMobile
267
	 */
268
	public boolean isMobile() {
269
		return isMobile;
270
	}
3225 vikas 271
 
272
    private void setSnippets() {
273
        EhcacheWrapper<Long, Map<String, String>> productSnippetsCache = new EhcacheWrapper<Long, Map<String, String>>(
274
                EhcacheWrapper.PRODUCT_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
3561 rajveer 275
        if(sourceId == -1){
276
        	snippets = productSnippetsCache.get(productId);
277
        }
3225 vikas 278
        if (snippets == null) {
279
            log.info("Getting product snippet for :" + productId);
280
            snippets = new HashMap<String, String>();
3561 rajveer 281
            snippets.put(PRODUCT_PROERTIES_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, productId+"", sourceId));
282
            snippets.put(PRODUCT_SUMMARY_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_DETAIL_SNIPPET, productId+"", sourceId));
283
            snippets.put(PRODUCT_SLIDEGUIDE_KEY, ContentServingService.getSnippet(SnippetType.SLIDE_GUIDE_SNIPPET, productId+"", sourceId));
284
            if(sourceId == -1){
285
            	productSnippetsCache.put(productId, snippets);
286
            }
3225 vikas 287
            return;
288
        }
289
        log.info("Loaded from cache product snippet for :" + productId);
290
    }
4494 varun.gupt 291
 
292
    public Map<String, Double> getDiscounts()	{
293
    	return discounts;
294
    }
4934 amit.gupta 295
 
296
    public String getEntityLogisticsEstimation(){
297
    	List<Long> items=null; 
298
    	try {
299
    		LogisticsClient cl = new LogisticsClient();
300
            LogisticsService.Client client = cl.getClient();
5037 rajveer 301
            items = client.getEntityLogisticsEstimation(productId, DEFAULT_PINCODE, DeliveryType.PREPAID );
4934 amit.gupta 302
        } catch (Exception e1) {
303
            log.error("Unable to get items by catalog item id", e1);
304
        }
305
        return new Gson().toJson(items);
306
    }
5094 phani.kuma 307
 
308
    private static void setRetargettingLabel() {
309
    	label.put((long) 1002160, "6EIBCKjf1wMQ6Pua0wM"); //Samsung Galaxy Y S5360
310
    	label.put((long) 1002106, "JFGVCKCt8gMQ6Pua0wM"); //Sony Ericsson Xperia Neo V MT11i
311
    	label.put((long) 1003230, "ArAYCJiu8gMQ6Pua0wM"); //Micromax Funbook Tab P300
312
    	label.put((long) 1002151, "j0c_CJCv8gMQ6Pua0wM"); //HTC Explorer (Pico) A310e
313
    	label.put((long) 1000607, "7H3QCKDh8wMQ6Pua0wM"); //SanDisk MicroSD 8GB
314
    	label.put((long) 1003213, "QtGQCJji8wMQ6Pua0wM"); //Micromax SUPERFONE Gossip A78
315
    	label.put((long) 1002530, "0YNbCJDj8wMQ6Pua0wM"); //Samsung Champ Deluxe Duos C3312
316
    	label.put((long) 1003039, "pQUaCIjk8wMQ6Pua0wM"); //Samsung Star 3 Duos S5222
317
    	label.put((long) 1002575, "zcYTCIDl8wMQ6Pua0wM"); //Samsung Galaxy Y Duos S6102
318
    }
319
 
320
    public String getRetargettingLabel() {
321
    	return label.get(this.productId);
322
    }
1197 varun.gupt 323
}