Subversion Repositories SmartDukaan

Rev

Rev 3242 | Rev 3715 | 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;
14
import in.shop2020.thrift.clients.UserClient;
2511 vikas 15
import in.shop2020.utils.DataLogger;
375 ashish 16
 
317 ashish 17
import java.io.IOException;
3225 vikas 18
import java.util.HashMap;
375 ashish 19
import java.util.Map;
507 rajveer 20
import java.util.StringTokenizer;
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
 
3225 vikas 56
	private Map<String, String> snippets;
57
 
375 ashish 58
	public EntityController(){
59
		super();
60
	}
507 rajveer 61
 
974 vikas 62
	// GET /*/1000001
507 rajveer 63
	@Actions({
974 vikas 64
		@Action("/mobile-phones"),
1005 vikas 65
		@Action("/mobile-accessories"),
2460 rajveer 66
		@Action("/tablets"),
1005 vikas 67
		@Action("/entity")
507 rajveer 68
	})
2306 vikas 69
    public String show() throws SecurityException, IOException, JSONException {
317 ashish 70
    	log.info("id=" + id);
637 rajveer 71
 
2306 vikas 72
    	String entityUrl = "";
73
    	String metaKeywords = "";
74
    	String metaDescription = "";
75
    	String pageTitle = "";
2434 rajveer 76
    	String productName = "";
2652 rajveer 77
    	String displayAccessories = "FALSE";
2306 vikas 78
    	try {
3225 vikas 79
    	    setSnippets();
80
            htmlSnippets.put("PRODUCT_PROPERTIES", snippets.get(PRODUCT_PROERTIES_SNIPPET_KEY));
81
    	    JSONObject productPropertiesInJson = new JSONObject(htmlSnippets.get("PRODUCT_PROPERTIES"));
2306 vikas 82
    	    entityUrl = productPropertiesInJson.getString("entityUrl");
83
    	    metaDescription = productPropertiesInJson.getString("metaDescription");
84
    	    metaKeywords = productPropertiesInJson.getString("metaKeywords");
85
    	    pageTitle = productPropertiesInJson.getString("title");
2434 rajveer 86
    	    productName = productPropertiesInJson.getString("name");
2652 rajveer 87
    	    displayAccessories = productPropertiesInJson.getString("displayAccessories");
2306 vikas 88
    	}
89
    	catch (JSONException e) {
2949 chandransh 90
            log.error("Unable to parse product properties JSON", e);
2306 vikas 91
            try {
3126 rajveer 92
                CatalogClient catalogClientService = new CatalogClient();
2306 vikas 93
                in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
94
 
95
                Item item = client.getItemsByCatalogId(productId).get(0);
2307 vikas 96
                redirectUrl = "/" + item.getBrand().toLowerCase().replace(" ", "-");
2306 vikas 97
            } catch (Exception e1) {
2949 chandransh 98
                log.error("Unable to get items by catalog id", e1);
2306 vikas 99
                redirectUrl = "/";
100
            }
101
            log.info(redirectUrl);
102
            return "redirect";
103
       }
104
    	String currentUrl = request.getRequestURL().toString();
1197 varun.gupt 105
 
2306 vikas 106
    	if (!currentUrl.contains(entityUrl)) {
107
    	    redirectUrl = entityUrl;
108
    	    return "redirect";
109
    	}
110
 
2434 rajveer 111
    	//Extracting base url
1689 rajveer 112
    	String rootUrl = currentUrl.split("/")[2];
2194 varun.gupt 113
 
3225 vikas 114
    	htmlSnippets.put("PRODUCT_SUMMARY", snippets.get(PRODUCT_SUMMARY_SNIPPET_KEY));
1258 varun.gupt 115
    	htmlSnippets.put("PRODUCT_ID", productId + "");
116
    	htmlSnippets.put("PRODUCT_NAME", productName);
1689 rajveer 117
    	htmlSnippets.put("PRODUCT_URL", currentUrl);
2194 varun.gupt 118
    	htmlSnippets.put("ROOT_URL", "http://" + rootUrl);
3225 vikas 119
		htmlSnippets.put("SLIDE_GUIDE", snippets.get(PRODUCT_SLIDEGUIDE_KEY));
1269 varun.gupt 120
		htmlSnippets.put("PAGE_TITLE", pageTitle.trim());
2306 vikas 121
		htmlSnippets.put("PAGE_METADESC", metaDescription);
122
		htmlSnippets.put("PAGE_METAKEYWORDS", metaKeywords);
2652 rajveer 123
		if(displayAccessories.equals("TRUE")){
2867 rajveer 124
			setMobile(true);
2652 rajveer 125
		}
126
 
650 rajveer 127
		try {
3126 rajveer 128
			UserClient userServiceClient = new UserClient();
555 chandransh 129
			Client client = userServiceClient.getClient();
773 rajveer 130
			long itemId = Long.parseLong(id);
131
			long userId = userinfo.getUserId();
1511 rajveer 132
			if(userId != -1){
133
				client.updateBrowseHistory(userId, itemId);
134
			}
650 rajveer 135
 
449 rajveer 136
		} catch (Exception e) {
2949 chandransh 137
			log.warn("Unable to update the browsing history because of: ", e);
449 rajveer 138
		}
139
 
3185 vikas 140
		DataLogger.logData(EventType.PRODUCT_VIEW, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
3074 vikas 141
                productName, Long.toString(productId));
2149 vikas 142
		return "show";
317 ashish 143
    }
3225 vikas 144
 
317 ashish 145
    /**
146
     * 
147
     * @param id
148
     */
149
    public void setId(String id) {
507 rajveer 150
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
151
    	while(tokenizer.hasMoreTokens()){
152
    		this.id = tokenizer.nextToken();
153
    	}
974 vikas 154
    	this.productId = Long.parseLong(this.id);
317 ashish 155
    }
156
 
387 rajveer 157
	public Map<String,String> getHtmlSnippets(){
388 rajveer 158
		System.out.println(" getHtmlSnippets  is called");
387 rajveer 159
		return htmlSnippets;
375 ashish 160
	}
388 rajveer 161
 
449 rajveer 162
	public String getSlideGuideSnippet(){
388 rajveer 163
		return htmlSnippets.get("SLIDE_GUIDE");
164
	}
165
 
2306 vikas 166
	public String getRedirectUrl(){
167
        return redirectUrl;
168
    }
169
 
449 rajveer 170
	public String getProductSummarySnippet(){
171
		return htmlSnippets.get("PRODUCT_SUMMARY");
172
	}
173
 
974 vikas 174
	public String getPageTitleSnippet(){
175
		return htmlSnippets.get("PAGE_TITLE");
176
	}
177
 
178
	public String getPageMetaDescSnippet(){
179
		return htmlSnippets.get("PAGE_METADESC");
180
	}
181
 
182
	public String getPageMetaKeywordsSnippet(){
183
		return htmlSnippets.get("PAGE_METAKEYWORDS");
184
	}
185
 
1197 varun.gupt 186
	public String getProductName()	{
187
		return htmlSnippets.get("PRODUCT_NAME");
188
	}
1258 varun.gupt 189
 
190
	public String getProductId()	{
191
		return htmlSnippets.get("PRODUCT_ID");
192
	}
193
 
194
	public String getProductUrl()	{
195
		return htmlSnippets.get("PRODUCT_URL");
196
	}
1364 varun.gupt 197
 
198
	public String getRootUrl()	{
199
		return htmlSnippets.get("ROOT_URL");
200
	}
2867 rajveer 201
 
202
	/**
203
	 * @param isMobile the isMobile to set
204
	 */
205
	public void setMobile(boolean isMobile) {
206
		this.isMobile = isMobile;
2652 rajveer 207
	}
2867 rajveer 208
 
209
	/**
210
	 * @return the isMobile
211
	 */
212
	public boolean isMobile() {
213
		return isMobile;
214
	}
3225 vikas 215
 
216
    private void setSnippets() {
217
        EhcacheWrapper<Long, Map<String, String>> productSnippetsCache = new EhcacheWrapper<Long, Map<String, String>>(
218
                EhcacheWrapper.PRODUCT_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
3561 rajveer 219
        if(sourceId == -1){
220
        	snippets = productSnippetsCache.get(productId);
221
        }
3225 vikas 222
        if (snippets == null) {
223
            log.info("Getting product snippet for :" + productId);
224
            snippets = new HashMap<String, String>();
3561 rajveer 225
            snippets.put(PRODUCT_PROERTIES_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, productId+"", sourceId));
226
            snippets.put(PRODUCT_SUMMARY_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_DETAIL_SNIPPET, productId+"", sourceId));
227
            snippets.put(PRODUCT_SLIDEGUIDE_KEY, ContentServingService.getSnippet(SnippetType.SLIDE_GUIDE_SNIPPET, productId+"", sourceId));
228
            if(sourceId == -1){
229
            	productSnippetsCache.put(productId, snippets);
230
            }
3225 vikas 231
            return;
232
        }
233
        log.info("Loaded from cache product snippet for :" + productId);
234
    }
235
 
1197 varun.gupt 236
}