Subversion Repositories SmartDukaan

Rev

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