Subversion Repositories SmartDukaan

Rev

Rev 4494 | Rev 4831 | 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) {
507 rajveer 173
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
174
    	while(tokenizer.hasMoreTokens()){
175
    		this.id = tokenizer.nextToken();
176
    	}
974 vikas 177
    	this.productId = Long.parseLong(this.id);
317 ashish 178
    }
179
 
387 rajveer 180
	public Map<String,String> getHtmlSnippets(){
388 rajveer 181
		System.out.println(" getHtmlSnippets  is called");
387 rajveer 182
		return htmlSnippets;
375 ashish 183
	}
388 rajveer 184
 
449 rajveer 185
	public String getSlideGuideSnippet(){
388 rajveer 186
		return htmlSnippets.get("SLIDE_GUIDE");
187
	}
188
 
2306 vikas 189
	public String getRedirectUrl(){
190
        return redirectUrl;
191
    }
192
 
449 rajveer 193
	public String getProductSummarySnippet(){
194
		return htmlSnippets.get("PRODUCT_SUMMARY");
195
	}
196
 
974 vikas 197
	public String getPageTitleSnippet(){
198
		return htmlSnippets.get("PAGE_TITLE");
199
	}
200
 
201
	public String getPageMetaDescSnippet(){
202
		return htmlSnippets.get("PAGE_METADESC");
203
	}
204
 
205
	public String getPageMetaKeywordsSnippet(){
206
		return htmlSnippets.get("PAGE_METAKEYWORDS");
207
	}
208
 
1197 varun.gupt 209
	public String getProductName()	{
210
		return htmlSnippets.get("PRODUCT_NAME");
211
	}
1258 varun.gupt 212
 
3830 chandransh 213
	public String getCategoryName()	{
214
		return htmlSnippets.get("CATEGORY_NAME");
215
	}
216
 
217
	public String getCategoryUrl()	{
218
		return htmlSnippets.get("CATEGORY_URL");
219
	}
220
 
1258 varun.gupt 221
	public String getProductId()	{
222
		return htmlSnippets.get("PRODUCT_ID");
223
	}
224
 
225
	public String getProductUrl()	{
226
		return htmlSnippets.get("PRODUCT_URL");
227
	}
1364 varun.gupt 228
 
229
	public String getRootUrl()	{
230
		return htmlSnippets.get("ROOT_URL");
231
	}
2867 rajveer 232
 
3830 chandransh 233
	public String getBreadCrumb(){
234
		return htmlSnippets.get("BREADCRUMB");
235
	}
236
 
2867 rajveer 237
	/**
238
	 * @param isMobile the isMobile to set
239
	 */
240
	public void setMobile(boolean isMobile) {
241
		this.isMobile = isMobile;
2652 rajveer 242
	}
2867 rajveer 243
 
244
	/**
245
	 * @return the isMobile
246
	 */
247
	public boolean isMobile() {
248
		return isMobile;
249
	}
3225 vikas 250
 
251
    private void setSnippets() {
252
        EhcacheWrapper<Long, Map<String, String>> productSnippetsCache = new EhcacheWrapper<Long, Map<String, String>>(
253
                EhcacheWrapper.PRODUCT_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
3561 rajveer 254
        if(sourceId == -1){
255
        	snippets = productSnippetsCache.get(productId);
256
        }
3225 vikas 257
        if (snippets == null) {
258
            log.info("Getting product snippet for :" + productId);
259
            snippets = new HashMap<String, String>();
3561 rajveer 260
            snippets.put(PRODUCT_PROERTIES_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, productId+"", sourceId));
261
            snippets.put(PRODUCT_SUMMARY_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_DETAIL_SNIPPET, productId+"", sourceId));
262
            snippets.put(PRODUCT_SLIDEGUIDE_KEY, ContentServingService.getSnippet(SnippetType.SLIDE_GUIDE_SNIPPET, productId+"", sourceId));
263
            if(sourceId == -1){
264
            	productSnippetsCache.put(productId, snippets);
265
            }
3225 vikas 266
            return;
267
        }
268
        log.info("Loaded from cache product snippet for :" + productId);
269
    }
4494 varun.gupt 270
 
271
    public Map<String, Double> getDiscounts()	{
272
    	return discounts;
273
    }
1197 varun.gupt 274
}