Subversion Repositories SmartDukaan

Rev

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