Subversion Repositories SmartDukaan

Rev

Rev 4632 | Rev 4882 | 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;
8
import in.shop2020.model.v1.catalog.Item;
9
import in.shop2020.model.v1.user.UserContextService.Client;
3242 vikas 10
import in.shop2020.serving.cache.EhcacheWrapper;
3561 rajveer 11
import in.shop2020.serving.services.ContentServingService;
12
import in.shop2020.serving.utils.SnippetType;
3126 rajveer 13
import in.shop2020.thrift.clients.CatalogClient;
4494 varun.gupt 14
import in.shop2020.thrift.clients.PromotionClient;
3126 rajveer 15
import in.shop2020.thrift.clients.UserClient;
2511 vikas 16
import in.shop2020.utils.DataLogger;
375 ashish 17
 
317 ashish 18
import java.io.IOException;
3225 vikas 19
import java.util.HashMap;
375 ashish 20
import java.util.Map;
507 rajveer 21
import java.util.StringTokenizer;
317 ashish 22
 
3225 vikas 23
import net.sf.ehcache.CacheManager;
24
 
832 rajveer 25
import org.apache.log4j.Logger;
507 rajveer 26
import org.apache.struts2.convention.annotation.Action;
27
import org.apache.struts2.convention.annotation.Actions;
974 vikas 28
import org.apache.struts2.convention.annotation.Result;
2306 vikas 29
import org.apache.struts2.convention.annotation.Results;
30
import org.json.JSONException;
31
import org.json.JSONObject;
317 ashish 32
 
33
/**
34
 * 
650 rajveer 35
 * @author rajveer
317 ashish 36
 *
37
 */
650 rajveer 38
 
2306 vikas 39
@Results({
40
    @Result(name = "show", location = "entity-show.vm"),
41
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
42
})
650 rajveer 43
public class EntityController extends BaseController {
44
 
45
	private static final long serialVersionUID = 1L;
1957 vikas 46
	private static Logger log = Logger.getLogger(Class.class);
3225 vikas 47
 
48
	private static final String PRODUCT_PROERTIES_SNIPPET_KEY = "PRODUCT_PROPERTIES";
49
	private static final String PRODUCT_SUMMARY_SNIPPET_KEY = "PRODUCT_SUMMARY";
50
	private static final String PRODUCT_SLIDEGUIDE_KEY = "SLIDEGUIDE";
51
 
317 ashish 52
	private String id;
2306 vikas 53
	private String redirectUrl;
620 rajveer 54
	private long productId;
2867 rajveer 55
	private boolean isMobile = false;
56
 
4494 varun.gupt 57
	private Map<String, Double> discounts = null;
3225 vikas 58
	private Map<String, String> snippets;
59
 
375 ashish 60
	public EntityController(){
61
		super();
62
	}
507 rajveer 63
 
974 vikas 64
	// GET /*/1000001
507 rajveer 65
	@Actions({
974 vikas 66
		@Action("/mobile-phones"),
1005 vikas 67
		@Action("/mobile-accessories"),
2460 rajveer 68
		@Action("/tablets"),
3715 rajveer 69
		@Action("/laptops"),
1005 vikas 70
		@Action("/entity")
507 rajveer 71
	})
2306 vikas 72
    public String show() throws SecurityException, IOException, JSONException {
317 ashish 73
    	log.info("id=" + id);
637 rajveer 74
 
2306 vikas 75
    	String entityUrl = "";
76
    	String metaKeywords = "";
77
    	String metaDescription = "";
78
    	String pageTitle = "";
2434 rajveer 79
    	String productName = "";
3830 chandransh 80
    	String categoryName = "";
81
    	String categoryUrl = "";
2652 rajveer 82
    	String displayAccessories = "FALSE";
3830 chandransh 83
    	String breadCrumb = "";
2306 vikas 84
    	try {
3225 vikas 85
    	    setSnippets();
86
            htmlSnippets.put("PRODUCT_PROPERTIES", snippets.get(PRODUCT_PROERTIES_SNIPPET_KEY));
87
    	    JSONObject productPropertiesInJson = new JSONObject(htmlSnippets.get("PRODUCT_PROPERTIES"));
2306 vikas 88
    	    entityUrl = productPropertiesInJson.getString("entityUrl");
89
    	    metaDescription = productPropertiesInJson.getString("metaDescription");
90
    	    metaKeywords = productPropertiesInJson.getString("metaKeywords");
91
    	    pageTitle = productPropertiesInJson.getString("title");
2434 rajveer 92
    	    productName = productPropertiesInJson.getString("name");
3830 chandransh 93
    	    categoryName = productPropertiesInJson.getString("categoryName");
94
    	    categoryUrl = productPropertiesInJson.getString("categoryUrl");
2652 rajveer 95
    	    displayAccessories = productPropertiesInJson.getString("displayAccessories");
3830 chandransh 96
    	    breadCrumb = productPropertiesInJson.getString("breadCrumb");
2306 vikas 97
    	}
98
    	catch (JSONException e) {
2949 chandransh 99
            log.error("Unable to parse product properties JSON", e);
2306 vikas 100
            try {
3126 rajveer 101
                CatalogClient catalogClientService = new CatalogClient();
2306 vikas 102
                in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
103
 
104
                Item item = client.getItemsByCatalogId(productId).get(0);
2307 vikas 105
                redirectUrl = "/" + item.getBrand().toLowerCase().replace(" ", "-");
2306 vikas 106
            } catch (Exception e1) {
2949 chandransh 107
                log.error("Unable to get items by catalog id", e1);
2306 vikas 108
                redirectUrl = "/";
109
            }
110
            log.info(redirectUrl);
111
            return "redirect";
112
       }
113
    	String currentUrl = request.getRequestURL().toString();
1197 varun.gupt 114
 
2306 vikas 115
    	if (!currentUrl.contains(entityUrl)) {
116
    	    redirectUrl = entityUrl;
117
    	    return "redirect";
118
    	}
119
 
2434 rajveer 120
    	//Extracting base url
1689 rajveer 121
    	String rootUrl = currentUrl.split("/")[2];
2194 varun.gupt 122
 
3225 vikas 123
    	htmlSnippets.put("PRODUCT_SUMMARY", snippets.get(PRODUCT_SUMMARY_SNIPPET_KEY));
1258 varun.gupt 124
    	htmlSnippets.put("PRODUCT_ID", productId + "");
125
    	htmlSnippets.put("PRODUCT_NAME", productName);
3830 chandransh 126
    	htmlSnippets.put("CATEGORY_NAME", categoryName);
127
    	htmlSnippets.put("CATEGORY_URL", categoryUrl);
1689 rajveer 128
    	htmlSnippets.put("PRODUCT_URL", currentUrl);
2194 varun.gupt 129
    	htmlSnippets.put("ROOT_URL", "http://" + rootUrl);
3225 vikas 130
		htmlSnippets.put("SLIDE_GUIDE", snippets.get(PRODUCT_SLIDEGUIDE_KEY));
1269 varun.gupt 131
		htmlSnippets.put("PAGE_TITLE", pageTitle.trim());
2306 vikas 132
		htmlSnippets.put("PAGE_METADESC", metaDescription);
133
		htmlSnippets.put("PAGE_METAKEYWORDS", metaKeywords);
3830 chandransh 134
		htmlSnippets.put("BREADCRUMB", breadCrumb);
135
 
2652 rajveer 136
		if(displayAccessories.equals("TRUE")){
2867 rajveer 137
			setMobile(true);
2652 rajveer 138
		}
139
 
650 rajveer 140
		try {
3126 rajveer 141
			UserClient userServiceClient = new UserClient();
555 chandransh 142
			Client client = userServiceClient.getClient();
773 rajveer 143
			long itemId = Long.parseLong(id);
144
			long userId = userinfo.getUserId();
1511 rajveer 145
			if(userId != -1){
146
				client.updateBrowseHistory(userId, itemId);
147
			}
650 rajveer 148
 
449 rajveer 149
		} catch (Exception e) {
2949 chandransh 150
			log.warn("Unable to update the browsing history because of: ", e);
449 rajveer 151
		}
4494 varun.gupt 152
 
153
		try	{
154
			PromotionClient promotionServiceClient = new PromotionClient();
155
			in.shop2020.model.v1.user.PromotionService.Client promotionClient = promotionServiceClient.getClient();
156
 
157
			discounts = promotionClient.getDiscountsForEntity(productId);
158
 
159
		} catch (Exception e) {
160
			log.error("Unable to retrieve discounts", e);
161
		}
449 rajveer 162
 
3185 vikas 163
		DataLogger.logData(EventType.PRODUCT_VIEW, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
4632 amar.kumar 164
                productName, Long.toString(productId), request.getHeader("referer"));
2149 vikas 165
		return "show";
317 ashish 166
    }
3225 vikas 167
 
317 ashish 168
    /**
169
     * 
170
     * @param id
171
     */
172
    public void setId(String id) {
4831 varun.gupt 173
    	String[] tokens = id.split("-");
174
    	this.id = tokens[tokens.length - 1];
974 vikas 175
    	this.productId = Long.parseLong(this.id);
317 ashish 176
    }
177
 
387 rajveer 178
	public Map<String,String> getHtmlSnippets(){
388 rajveer 179
		System.out.println(" getHtmlSnippets  is called");
387 rajveer 180
		return htmlSnippets;
375 ashish 181
	}
388 rajveer 182
 
449 rajveer 183
	public String getSlideGuideSnippet(){
388 rajveer 184
		return htmlSnippets.get("SLIDE_GUIDE");
185
	}
186
 
2306 vikas 187
	public String getRedirectUrl(){
188
        return redirectUrl;
189
    }
190
 
449 rajveer 191
	public String getProductSummarySnippet(){
192
		return htmlSnippets.get("PRODUCT_SUMMARY");
193
	}
194
 
974 vikas 195
	public String getPageTitleSnippet(){
196
		return htmlSnippets.get("PAGE_TITLE");
197
	}
198
 
199
	public String getPageMetaDescSnippet(){
200
		return htmlSnippets.get("PAGE_METADESC");
201
	}
202
 
203
	public String getPageMetaKeywordsSnippet(){
204
		return htmlSnippets.get("PAGE_METAKEYWORDS");
205
	}
206
 
1197 varun.gupt 207
	public String getProductName()	{
208
		return htmlSnippets.get("PRODUCT_NAME");
209
	}
1258 varun.gupt 210
 
3830 chandransh 211
	public String getCategoryName()	{
212
		return htmlSnippets.get("CATEGORY_NAME");
213
	}
214
 
215
	public String getCategoryUrl()	{
216
		return htmlSnippets.get("CATEGORY_URL");
217
	}
218
 
1258 varun.gupt 219
	public String getProductId()	{
220
		return htmlSnippets.get("PRODUCT_ID");
221
	}
222
 
223
	public String getProductUrl()	{
224
		return htmlSnippets.get("PRODUCT_URL");
225
	}
1364 varun.gupt 226
 
227
	public String getRootUrl()	{
228
		return htmlSnippets.get("ROOT_URL");
229
	}
2867 rajveer 230
 
3830 chandransh 231
	public String getBreadCrumb(){
232
		return htmlSnippets.get("BREADCRUMB");
233
	}
234
 
2867 rajveer 235
	/**
236
	 * @param isMobile the isMobile to set
237
	 */
238
	public void setMobile(boolean isMobile) {
239
		this.isMobile = isMobile;
2652 rajveer 240
	}
2867 rajveer 241
 
242
	/**
243
	 * @return the isMobile
244
	 */
245
	public boolean isMobile() {
246
		return isMobile;
247
	}
3225 vikas 248
 
249
    private void setSnippets() {
250
        EhcacheWrapper<Long, Map<String, String>> productSnippetsCache = new EhcacheWrapper<Long, Map<String, String>>(
251
                EhcacheWrapper.PRODUCT_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
3561 rajveer 252
        if(sourceId == -1){
253
        	snippets = productSnippetsCache.get(productId);
254
        }
3225 vikas 255
        if (snippets == null) {
256
            log.info("Getting product snippet for :" + productId);
257
            snippets = new HashMap<String, String>();
3561 rajveer 258
            snippets.put(PRODUCT_PROERTIES_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, productId+"", sourceId));
259
            snippets.put(PRODUCT_SUMMARY_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_DETAIL_SNIPPET, productId+"", sourceId));
260
            snippets.put(PRODUCT_SLIDEGUIDE_KEY, ContentServingService.getSnippet(SnippetType.SLIDE_GUIDE_SNIPPET, productId+"", sourceId));
261
            if(sourceId == -1){
262
            	productSnippetsCache.put(productId, snippets);
263
            }
3225 vikas 264
            return;
265
        }
266
        log.info("Loaded from cache product snippet for :" + productId);
267
    }
4494 varun.gupt 268
 
269
    public Map<String, Double> getDiscounts()	{
270
    	return discounts;
271
    }
1197 varun.gupt 272
}