Subversion Repositories SmartDukaan

Rev

Rev 5001 | Rev 5026 | 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
 
832 rajveer 28
import org.apache.log4j.Logger;
507 rajveer 29
import org.apache.struts2.convention.annotation.Action;
30
import org.apache.struts2.convention.annotation.Actions;
974 vikas 31
import org.apache.struts2.convention.annotation.Result;
2306 vikas 32
import org.apache.struts2.convention.annotation.Results;
33
import org.json.JSONException;
34
import org.json.JSONObject;
317 ashish 35
 
4934 amit.gupta 36
import com.google.gson.Gson;
37
 
317 ashish 38
/**
39
 * 
650 rajveer 40
 * @author rajveer
317 ashish 41
 *
42
 */
650 rajveer 43
 
2306 vikas 44
@Results({
45
    @Result(name = "show", location = "entity-show.vm"),
46
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
47
})
650 rajveer 48
public class EntityController extends BaseController {
49
 
50
	private static final long serialVersionUID = 1L;
1957 vikas 51
	private static Logger log = Logger.getLogger(Class.class);
3225 vikas 52
 
53
	private static final String PRODUCT_PROERTIES_SNIPPET_KEY = "PRODUCT_PROPERTIES";
54
	private static final String PRODUCT_SUMMARY_SNIPPET_KEY = "PRODUCT_SUMMARY";
55
	private static final String PRODUCT_SLIDEGUIDE_KEY = "SLIDEGUIDE";
5008 rajveer 56
 
57
	private static final String DEFAULT_PINCODE = "110001";
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
 
5001 amar.kumar 169
		String refererUrl  = request.getHeader("referer");
3185 vikas 170
		DataLogger.logData(EventType.PRODUCT_VIEW, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
5001 amar.kumar 171
                productName, Long.toString(productId), 
172
                (refererUrl.length()<500)?refererUrl:refererUrl.substring(0, 499));
2149 vikas 173
		return "show";
317 ashish 174
    }
3225 vikas 175
 
317 ashish 176
    /**
177
     * 
178
     * @param id
179
     */
180
    public void setId(String id) {
4831 varun.gupt 181
    	String[] tokens = id.split("-");
182
    	this.id = tokens[tokens.length - 1];
974 vikas 183
    	this.productId = Long.parseLong(this.id);
317 ashish 184
    }
185
 
387 rajveer 186
	public Map<String,String> getHtmlSnippets(){
388 rajveer 187
		System.out.println(" getHtmlSnippets  is called");
387 rajveer 188
		return htmlSnippets;
375 ashish 189
	}
388 rajveer 190
 
449 rajveer 191
	public String getSlideGuideSnippet(){
388 rajveer 192
		return htmlSnippets.get("SLIDE_GUIDE");
193
	}
194
 
2306 vikas 195
	public String getRedirectUrl(){
196
        return redirectUrl;
197
    }
198
 
449 rajveer 199
	public String getProductSummarySnippet(){
200
		return htmlSnippets.get("PRODUCT_SUMMARY");
201
	}
202
 
974 vikas 203
	public String getPageTitleSnippet(){
204
		return htmlSnippets.get("PAGE_TITLE");
205
	}
206
 
207
	public String getPageMetaDescSnippet(){
208
		return htmlSnippets.get("PAGE_METADESC");
209
	}
210
 
211
	public String getPageMetaKeywordsSnippet(){
212
		return htmlSnippets.get("PAGE_METAKEYWORDS");
213
	}
214
 
1197 varun.gupt 215
	public String getProductName()	{
216
		return htmlSnippets.get("PRODUCT_NAME");
217
	}
1258 varun.gupt 218
 
3830 chandransh 219
	public String getCategoryName()	{
220
		return htmlSnippets.get("CATEGORY_NAME");
221
	}
222
 
223
	public String getCategoryUrl()	{
224
		return htmlSnippets.get("CATEGORY_URL");
225
	}
226
 
1258 varun.gupt 227
	public String getProductId()	{
228
		return htmlSnippets.get("PRODUCT_ID");
229
	}
230
 
231
	public String getProductUrl()	{
232
		return htmlSnippets.get("PRODUCT_URL");
233
	}
1364 varun.gupt 234
 
235
	public String getRootUrl()	{
236
		return htmlSnippets.get("ROOT_URL");
237
	}
2867 rajveer 238
 
3830 chandransh 239
	public String getBreadCrumb(){
240
		return htmlSnippets.get("BREADCRUMB");
241
	}
242
 
2867 rajveer 243
	/**
244
	 * @param isMobile the isMobile to set
245
	 */
246
	public void setMobile(boolean isMobile) {
247
		this.isMobile = isMobile;
2652 rajveer 248
	}
2867 rajveer 249
 
250
	/**
251
	 * @return the isMobile
252
	 */
253
	public boolean isMobile() {
254
		return isMobile;
255
	}
3225 vikas 256
 
257
    private void setSnippets() {
258
        EhcacheWrapper<Long, Map<String, String>> productSnippetsCache = new EhcacheWrapper<Long, Map<String, String>>(
259
                EhcacheWrapper.PRODUCT_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
3561 rajveer 260
        if(sourceId == -1){
261
        	snippets = productSnippetsCache.get(productId);
262
        }
3225 vikas 263
        if (snippets == null) {
264
            log.info("Getting product snippet for :" + productId);
265
            snippets = new HashMap<String, String>();
3561 rajveer 266
            snippets.put(PRODUCT_PROERTIES_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, productId+"", sourceId));
267
            snippets.put(PRODUCT_SUMMARY_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_DETAIL_SNIPPET, productId+"", sourceId));
268
            snippets.put(PRODUCT_SLIDEGUIDE_KEY, ContentServingService.getSnippet(SnippetType.SLIDE_GUIDE_SNIPPET, productId+"", sourceId));
269
            if(sourceId == -1){
270
            	productSnippetsCache.put(productId, snippets);
271
            }
3225 vikas 272
            return;
273
        }
274
        log.info("Loaded from cache product snippet for :" + productId);
275
    }
4494 varun.gupt 276
 
277
    public Map<String, Double> getDiscounts()	{
278
    	return discounts;
279
    }
4934 amit.gupta 280
 
281
    public String getEntityLogisticsEstimation(){
282
    	List<Long> items=null; 
283
    	try {
284
    		LogisticsClient cl = new LogisticsClient();
285
            LogisticsService.Client client = cl.getClient();
5008 rajveer 286
            items = client.getEntityLogisticsEstimation(productId, DEFAULT_PINCODE, DeliveryType.PREPAID );
4934 amit.gupta 287
        } catch (Exception e1) {
288
            log.error("Unable to get items by catalog item id", e1);
289
        }
290
        return new Gson().toJson(items);
291
    }
1197 varun.gupt 292
}