Subversion Repositories SmartDukaan

Rev

Rev 7272 | Rev 7515 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7272 amit.gupta 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
 
7
import in.shop2020.datalogger.EventType;
8
import in.shop2020.logistics.DeliveryType;
9
import in.shop2020.logistics.LogisticsService;
10
import in.shop2020.model.v1.catalog.Item;
7386 anupam.sin 11
import in.shop2020.model.v1.order.HotspotStore;
7272 amit.gupta 12
import in.shop2020.model.v1.user.UserContextService.Client;
13
import in.shop2020.serving.cache.EhcacheWrapper;
14
import in.shop2020.serving.service.ContentServingService;
15
import in.shop2020.serving.utils.SnippetType;
16
import in.shop2020.thrift.clients.CatalogClient;
17
import in.shop2020.thrift.clients.LogisticsClient;
7386 anupam.sin 18
import in.shop2020.thrift.clients.TransactionClient;
7272 amit.gupta 19
import in.shop2020.thrift.clients.UserClient;
20
import in.shop2020.utils.DataLogger;
21
 
22
import java.io.IOException;
23
import java.util.HashMap;
24
import java.util.List;
25
import java.util.Map;
26
 
27
import net.sf.ehcache.CacheManager;
28
 
29
import org.apache.commons.lang.StringUtils;
30
import org.apache.log4j.Logger;
31
import org.apache.struts2.convention.annotation.Action;
32
import org.apache.struts2.convention.annotation.Actions;
33
import org.apache.struts2.convention.annotation.Result;
34
import org.apache.struts2.convention.annotation.Results;
35
import org.json.JSONException;
36
import org.json.JSONObject;
37
 
38
import com.google.gson.Gson;
39
 
40
/**
41
 * 
42
 * @author rajveer
43
 *
44
 */
45
 
46
@Results({
47
    @Result(name = "show", location = "entity-show.vm"),
48
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
49
})
50
public class EntityController extends BaseController {
51
 
52
	private static final long serialVersionUID = 1L;
53
	private static Logger log = Logger.getLogger(Class.class);
54
 
55
	private static final String PRODUCT_PROERTIES_SNIPPET_KEY = "PRODUCT_PROPERTIES";
56
	private static final String PRODUCT_SUMMARY_SNIPPET_KEY = "PRODUCT_SUMMARY";
57
	private static final String PRODUCT_SLIDEGUIDE_KEY = "SLIDEGUIDE";
58
 
59
	private static final String DEFAULT_PINCODE = "110001";
60
	private String id;
61
	private String redirectUrl;
62
	private long productId;
63
	private boolean isMobile = false;
64
 
65
	private Map<String, Double> discounts = new HashMap<String, Double>();
66
	private Map<String, String> snippets;
67
	private static Map<Long, String> label = new HashMap<Long, String>();
68
	static{
69
		setRetargettingLabel();
70
	}
71
 
72
	public EntityController(){
73
		super();
74
	}
75
 
76
	// GET /*/1000001
77
	@Actions({
78
		@Action("/mobile-phones"),
79
		@Action("/cameras"),
80
		@Action("/mobile-accessories"),
81
		@Action("/tablets"),
82
		@Action("/laptops"),
83
		@Action("/laptop-accessories"),
84
		@Action("/entity"),
85
		@Action("/music-players")
86
	})
87
    public String show() throws SecurityException, IOException, JSONException {
7386 anupam.sin 88
	    String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
89
        if(loginStatus == null || !loginStatus.equals("TRUE")){
90
            return "authfail";
91
        }
92
 
93
        storeId = Long.parseLong((String) request.getSession().getAttribute("STORE_ID"));
94
        if(!hotspotStores.containsKey(storeId)){
95
            try{
96
                HotspotStore hotSpotStore = (new TransactionClient()).getClient().getHotspotStore(storeId, "");
97
                hotspotStores.put(storeId, hotSpotStore);
98
            } catch (Exception e) {
99
                log.error("Unable to get store", e);
100
            }
101
        }
102
 
7272 amit.gupta 103
    	log.info("id=" + id);
104
 
105
    	String entityUrl = "";
106
    	String metaDescription = "";
107
    	String pageTitle = "";
108
    	String productName = "";
109
    	String categoryName = "";
110
    	String categoryUrl = "";
111
    	String displayAccessories = "FALSE";
112
    	String breadCrumb = "";
113
    	String parentCategory = "";
114
    	String compareCategory = "";
115
    	try {
116
    	    setSnippets();
117
            htmlSnippets.put("PRODUCT_PROPERTIES", snippets.get(PRODUCT_PROERTIES_SNIPPET_KEY));
118
    	    JSONObject productPropertiesInJson = new JSONObject(htmlSnippets.get("PRODUCT_PROPERTIES"));
119
    	    entityUrl = productPropertiesInJson.getString("entityUrl");
120
    	    metaDescription = productPropertiesInJson.getString("metaDescription");
121
    	    pageTitle = productPropertiesInJson.getString("title");
122
    	    productName = productPropertiesInJson.getString("name");
123
    	    categoryName = productPropertiesInJson.getString("categoryName");
124
    	    categoryUrl = productPropertiesInJson.getString("categoryUrl");
125
    	    displayAccessories = productPropertiesInJson.getString("displayAccessories");
126
    	    breadCrumb = productPropertiesInJson.getString("breadCrumb");
127
    	    parentCategory = productPropertiesInJson.getString("parentCategory");
128
    	    compareCategory = productPropertiesInJson.getString("compareCategory");
129
    	}
130
    	catch (JSONException e) {
131
            log.warn("Unable to parse product properties JSON", e);
132
            try {
133
                CatalogClient catalogClientService = new CatalogClient();
134
                in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
135
 
136
                Item item = client.getItemsByCatalogId(productId).get(0);
137
                redirectUrl = "/" + item.getBrand().toLowerCase().replace(" ", "-");
138
            } catch (Exception e1) {
139
                log.warn("Unable to get items by catalog id", e1);
140
                redirectUrl = "/";
141
            }
142
            log.info(redirectUrl);
143
            return "redirect";
144
       }
145
    	String currentUrl = request.getRequestURL().toString();
146
 
147
    	if (!currentUrl.contains(entityUrl)) {
148
    	    redirectUrl = entityUrl;
149
    	    return "redirect";
150
    	}
151
 
152
    	//Extracting base url
153
    	String rootUrl = currentUrl.split("/")[2];
154
 
155
    	htmlSnippets.put("PRODUCT_SUMMARY", snippets.get(PRODUCT_SUMMARY_SNIPPET_KEY));
156
    	htmlSnippets.put("PRODUCT_ID", productId + "");
157
    	htmlSnippets.put("PRODUCT_NAME", productName);
158
    	htmlSnippets.put("CATEGORY_NAME", categoryName);
159
    	htmlSnippets.put("CATEGORY_URL", categoryUrl);
160
    	htmlSnippets.put("PRODUCT_URL", currentUrl);
161
    	htmlSnippets.put("ROOT_URL", "http://" + rootUrl);
162
		htmlSnippets.put("SLIDE_GUIDE", snippets.get(PRODUCT_SLIDEGUIDE_KEY));
163
		htmlSnippets.put("PAGE_TITLE", pageTitle.trim());
164
		htmlSnippets.put("PAGE_METADESC", metaDescription);
165
		htmlSnippets.put("BREADCRUMB", breadCrumb);
166
		htmlSnippets.put("PARENT_CATEGORY", parentCategory);
167
		htmlSnippets.put("COMPARE_CATEGORY", compareCategory);
168
 
169
		if(displayAccessories.equals("TRUE")){
170
			setMobile(true);
171
		}
172
		return "show";
173
    }
174
 
175
    /**
176
     * 
177
     * @param id
178
     */
179
    public void setId(String id) {
180
    	String[] tokens = id.split("-");
181
    	this.id = tokens[tokens.length - 1];
182
    	this.productId = Long.parseLong(this.id);
183
    }
184
 
185
	public Map<String,String> getHtmlSnippets(){
186
		System.out.println(" getHtmlSnippets  is called");
187
		return htmlSnippets;
188
	}
189
 
190
	public String getSlideGuideSnippet(){
191
		return htmlSnippets.get("SLIDE_GUIDE");
192
	}
193
 
194
	public String getRedirectUrl(){
195
		String query = request.getQueryString();
196
		if(query!=null){
197
			return redirectUrl + "?" + query;
198
		}
199
		return redirectUrl;
200
    }
201
 
202
	public String getProductSummarySnippet(){
203
		return htmlSnippets.get("PRODUCT_SUMMARY");
204
	}
205
 
206
	public String getPageTitleSnippet(){
207
		return htmlSnippets.get("PAGE_TITLE");
208
	}
209
 
210
	public String getPageMetaDescSnippet(){
211
		return htmlSnippets.get("PAGE_METADESC");
212
	}
213
 
214
 
215
	public String getProductName()	{
216
		return htmlSnippets.get("PRODUCT_NAME");
217
	}
218
 
219
	public String getCompareCategory(){
220
		return htmlSnippets.get("COMPARE_CATEGORY");
221
	}
222
 
223
	public String getCompareCategoryLowerCase(){
224
		String compareCategory = htmlSnippets.get("COMPARE_CATEGORY");
225
		return compareCategory.toLowerCase();
226
	}
227
 
228
	public String getCategoryName()	{
229
		return htmlSnippets.get("CATEGORY_NAME");
230
	}
231
 
232
	public String getCategoryUrl()	{
233
		return htmlSnippets.get("CATEGORY_URL");
234
	}
235
 
236
	public String getProductId()	{
237
		return htmlSnippets.get("PRODUCT_ID");
238
	}
239
 
240
	public String getProductUrl()	{
241
		return htmlSnippets.get("PRODUCT_URL");
242
	}
243
 
244
	public String getRootUrl()	{
245
		return htmlSnippets.get("ROOT_URL");
246
	}
247
 
248
	public String getBreadCrumb(){
249
		return htmlSnippets.get("BREADCRUMB");
250
	}
251
 
252
	/**
253
	 * @param isMobile the isMobile to set
254
	 */
255
	public void setMobile(boolean isMobile) {
256
		this.isMobile = isMobile;
257
	}
258
 
259
	/**
260
	 * @return the isMobile
261
	 */
262
	public boolean isMobile() {
263
		return isMobile;
264
	}
265
 
266
    private void setSnippets() {
267
        EhcacheWrapper<Long, Map<String, String>> productSnippetsCache = new EhcacheWrapper<Long, Map<String, String>>(
268
                EhcacheWrapper.PRODUCT_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
269
        if(sourceId == -1){
270
        	snippets = productSnippetsCache.get(productId);
271
        }
272
        if (snippets == null) {
273
            log.info("Getting product snippet for :" + productId);
274
            snippets = new HashMap<String, String>();
275
            snippets.put(PRODUCT_PROERTIES_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, productId+"", sourceId));
276
            snippets.put(PRODUCT_SUMMARY_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_DETAIL_SNIPPET, productId+"", sourceId));
277
            snippets.put(PRODUCT_SLIDEGUIDE_KEY, ContentServingService.getSnippet(SnippetType.SLIDE_GUIDE_SNIPPET, productId+"", sourceId));
278
            if(sourceId == -1){
279
            	productSnippetsCache.put(productId, snippets);
280
            }
281
            return;
282
        }
283
        log.info("Loaded from cache product snippet for :" + productId);
284
    }
285
 
286
    public Map<String, Double> getDiscounts()	{
287
    	return discounts;
288
    }
289
 
290
    public String getEntityLogisticsEstimation(){
291
    	List<Long> items=null; 
292
    	try {
293
    		LogisticsClient cl = new LogisticsClient();
294
            LogisticsService.Client client = cl.getClient();
295
            items = client.getEntityLogisticsEstimation(productId, DEFAULT_PINCODE, DeliveryType.PREPAID );
296
        } catch (Exception e1) {
297
            log.error("Unable to get items by catalog item id", e1);
298
        }
299
        return new Gson().toJson(items);
300
    }
301
 
302
    private static void setRetargettingLabel() {
303
    	label.put((long) 1002160, "6EIBCKjf1wMQ6Pua0wM"); //Samsung Galaxy Y S5360
304
    	label.put((long) 1002106, "JFGVCKCt8gMQ6Pua0wM"); //Sony Ericsson Xperia Neo V MT11i
305
    	label.put((long) 1003230, "ArAYCJiu8gMQ6Pua0wM"); //Micromax Funbook Tab P300
306
    	label.put((long) 1002151, "j0c_CJCv8gMQ6Pua0wM"); //HTC Explorer (Pico) A310e
307
    	label.put((long) 1000607, "7H3QCKDh8wMQ6Pua0wM"); //SanDisk MicroSD 8GB
308
    	label.put((long) 1003213, "QtGQCJji8wMQ6Pua0wM"); //Micromax SUPERFONE Gossip A78
309
    	label.put((long) 1002530, "0YNbCJDj8wMQ6Pua0wM"); //Samsung Champ Deluxe Duos C3312
310
    	label.put((long) 1003039, "pQUaCIjk8wMQ6Pua0wM"); //Samsung Star 3 Duos S5222
311
    	label.put((long) 1002575, "zcYTCIDl8wMQ6Pua0wM"); //Samsung Galaxy Y Duos S6102
312
    }
313
 
314
    public String getRetargettingLabel() {
315
    	return label.get(this.productId);
316
    }
317
 
318
}