Subversion Repositories SmartDukaan

Rev

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