Subversion Repositories SmartDukaan

Rev

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