Subversion Repositories SmartDukaan

Rev

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