Subversion Repositories SmartDukaan

Rev

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