Subversion Repositories SmartDukaan

Rev

Rev 7386 | 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;
7515 anupam.sin 64
	private String storePin;
7272 amit.gupta 65
 
66
	private Map<String, Double> discounts = new HashMap<String, Double>();
67
	private Map<String, String> snippets;
68
	private static Map<Long, String> label = new HashMap<Long, String>();
69
	static{
70
		setRetargettingLabel();
71
	}
72
 
73
	public EntityController(){
74
		super();
75
	}
76
 
77
	// GET /*/1000001
78
	@Actions({
79
		@Action("/mobile-phones"),
80
		@Action("/cameras"),
81
		@Action("/mobile-accessories"),
82
		@Action("/tablets"),
83
		@Action("/laptops"),
84
		@Action("/laptop-accessories"),
85
		@Action("/entity"),
86
		@Action("/music-players")
87
	})
88
    public String show() throws SecurityException, IOException, JSONException {
7386 anupam.sin 89
	    String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
90
        if(loginStatus == null || !loginStatus.equals("TRUE")){
91
            return "authfail";
92
        }
93
 
94
        storeId = Long.parseLong((String) request.getSession().getAttribute("STORE_ID"));
95
        if(!hotspotStores.containsKey(storeId)){
96
            try{
97
                HotspotStore hotSpotStore = (new TransactionClient()).getClient().getHotspotStore(storeId, "");
98
                hotspotStores.put(storeId, hotSpotStore);
7515 anupam.sin 99
                setStorePin(hotSpotStore.getPin());
7386 anupam.sin 100
            } catch (Exception e) {
101
                log.error("Unable to get store", e);
7515 anupam.sin 102
                setStorePin("110001");
7386 anupam.sin 103
            }
7515 anupam.sin 104
        } else {
105
            HotspotStore hotSpotStore = hotspotStores.get(storeId);
106
            setStorePin(hotSpotStore.getPin());
7386 anupam.sin 107
        }
7515 anupam.sin 108
 
7272 amit.gupta 109
    	log.info("id=" + id);
110
 
111
    	String entityUrl = "";
112
    	String metaDescription = "";
113
    	String pageTitle = "";
114
    	String productName = "";
115
    	String categoryName = "";
116
    	String categoryUrl = "";
117
    	String displayAccessories = "FALSE";
118
    	String breadCrumb = "";
119
    	String parentCategory = "";
120
    	String compareCategory = "";
121
    	try {
122
    	    setSnippets();
123
            htmlSnippets.put("PRODUCT_PROPERTIES", snippets.get(PRODUCT_PROERTIES_SNIPPET_KEY));
124
    	    JSONObject productPropertiesInJson = new JSONObject(htmlSnippets.get("PRODUCT_PROPERTIES"));
125
    	    entityUrl = productPropertiesInJson.getString("entityUrl");
126
    	    metaDescription = productPropertiesInJson.getString("metaDescription");
127
    	    pageTitle = productPropertiesInJson.getString("title");
128
    	    productName = productPropertiesInJson.getString("name");
129
    	    categoryName = productPropertiesInJson.getString("categoryName");
130
    	    categoryUrl = productPropertiesInJson.getString("categoryUrl");
131
    	    displayAccessories = productPropertiesInJson.getString("displayAccessories");
132
    	    breadCrumb = productPropertiesInJson.getString("breadCrumb");
133
    	    parentCategory = productPropertiesInJson.getString("parentCategory");
134
    	    compareCategory = productPropertiesInJson.getString("compareCategory");
135
    	}
136
    	catch (JSONException e) {
137
            log.warn("Unable to parse product properties JSON", e);
138
            try {
139
                CatalogClient catalogClientService = new CatalogClient();
140
                in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
141
 
142
                Item item = client.getItemsByCatalogId(productId).get(0);
143
                redirectUrl = "/" + item.getBrand().toLowerCase().replace(" ", "-");
144
            } catch (Exception e1) {
145
                log.warn("Unable to get items by catalog id", e1);
146
                redirectUrl = "/";
147
            }
148
            log.info(redirectUrl);
149
            return "redirect";
150
       }
151
    	String currentUrl = request.getRequestURL().toString();
152
 
153
    	if (!currentUrl.contains(entityUrl)) {
154
    	    redirectUrl = entityUrl;
155
    	    return "redirect";
156
    	}
157
 
158
    	//Extracting base url
159
    	String rootUrl = currentUrl.split("/")[2];
160
 
161
    	htmlSnippets.put("PRODUCT_SUMMARY", snippets.get(PRODUCT_SUMMARY_SNIPPET_KEY));
162
    	htmlSnippets.put("PRODUCT_ID", productId + "");
163
    	htmlSnippets.put("PRODUCT_NAME", productName);
164
    	htmlSnippets.put("CATEGORY_NAME", categoryName);
165
    	htmlSnippets.put("CATEGORY_URL", categoryUrl);
166
    	htmlSnippets.put("PRODUCT_URL", currentUrl);
167
    	htmlSnippets.put("ROOT_URL", "http://" + rootUrl);
168
		htmlSnippets.put("SLIDE_GUIDE", snippets.get(PRODUCT_SLIDEGUIDE_KEY));
169
		htmlSnippets.put("PAGE_TITLE", pageTitle.trim());
170
		htmlSnippets.put("PAGE_METADESC", metaDescription);
171
		htmlSnippets.put("BREADCRUMB", breadCrumb);
172
		htmlSnippets.put("PARENT_CATEGORY", parentCategory);
173
		htmlSnippets.put("COMPARE_CATEGORY", compareCategory);
174
 
175
		if(displayAccessories.equals("TRUE")){
176
			setMobile(true);
177
		}
178
		return "show";
179
    }
180
 
181
    /**
182
     * 
183
     * @param id
184
     */
185
    public void setId(String id) {
186
    	String[] tokens = id.split("-");
187
    	this.id = tokens[tokens.length - 1];
188
    	this.productId = Long.parseLong(this.id);
189
    }
190
 
191
	public Map<String,String> getHtmlSnippets(){
192
		System.out.println(" getHtmlSnippets  is called");
193
		return htmlSnippets;
194
	}
195
 
196
	public String getSlideGuideSnippet(){
197
		return htmlSnippets.get("SLIDE_GUIDE");
198
	}
199
 
200
	public String getRedirectUrl(){
201
		String query = request.getQueryString();
202
		if(query!=null){
203
			return redirectUrl + "?" + query;
204
		}
205
		return redirectUrl;
206
    }
207
 
208
	public String getProductSummarySnippet(){
209
		return htmlSnippets.get("PRODUCT_SUMMARY");
210
	}
211
 
212
	public String getPageTitleSnippet(){
213
		return htmlSnippets.get("PAGE_TITLE");
214
	}
215
 
216
	public String getPageMetaDescSnippet(){
217
		return htmlSnippets.get("PAGE_METADESC");
218
	}
219
 
220
 
221
	public String getProductName()	{
222
		return htmlSnippets.get("PRODUCT_NAME");
223
	}
224
 
225
	public String getCompareCategory(){
226
		return htmlSnippets.get("COMPARE_CATEGORY");
227
	}
228
 
229
	public String getCompareCategoryLowerCase(){
230
		String compareCategory = htmlSnippets.get("COMPARE_CATEGORY");
231
		return compareCategory.toLowerCase();
232
	}
233
 
234
	public String getCategoryName()	{
235
		return htmlSnippets.get("CATEGORY_NAME");
236
	}
237
 
238
	public String getCategoryUrl()	{
239
		return htmlSnippets.get("CATEGORY_URL");
240
	}
241
 
242
	public String getProductId()	{
243
		return htmlSnippets.get("PRODUCT_ID");
244
	}
245
 
246
	public String getProductUrl()	{
247
		return htmlSnippets.get("PRODUCT_URL");
248
	}
249
 
250
	public String getRootUrl()	{
251
		return htmlSnippets.get("ROOT_URL");
252
	}
253
 
254
	public String getBreadCrumb(){
255
		return htmlSnippets.get("BREADCRUMB");
256
	}
257
 
258
	/**
259
	 * @param isMobile the isMobile to set
260
	 */
261
	public void setMobile(boolean isMobile) {
262
		this.isMobile = isMobile;
263
	}
264
 
265
	/**
266
	 * @return the isMobile
267
	 */
268
	public boolean isMobile() {
269
		return isMobile;
270
	}
271
 
272
    private void setSnippets() {
273
        EhcacheWrapper<Long, Map<String, String>> productSnippetsCache = new EhcacheWrapper<Long, Map<String, String>>(
274
                EhcacheWrapper.PRODUCT_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
275
        if(sourceId == -1){
276
        	snippets = productSnippetsCache.get(productId);
277
        }
278
        if (snippets == null) {
279
            log.info("Getting product snippet for :" + productId);
280
            snippets = new HashMap<String, String>();
281
            snippets.put(PRODUCT_PROERTIES_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, productId+"", sourceId));
282
            snippets.put(PRODUCT_SUMMARY_SNIPPET_KEY, ContentServingService.getSnippet(SnippetType.PRODUCT_DETAIL_SNIPPET, productId+"", sourceId));
283
            snippets.put(PRODUCT_SLIDEGUIDE_KEY, ContentServingService.getSnippet(SnippetType.SLIDE_GUIDE_SNIPPET, productId+"", sourceId));
284
            if(sourceId == -1){
285
            	productSnippetsCache.put(productId, snippets);
286
            }
287
            return;
288
        }
289
        log.info("Loaded from cache product snippet for :" + productId);
290
    }
291
 
292
    public Map<String, Double> getDiscounts()	{
293
    	return discounts;
294
    }
295
 
296
    public String getEntityLogisticsEstimation(){
297
    	List<Long> items=null; 
298
    	try {
299
    		LogisticsClient cl = new LogisticsClient();
300
            LogisticsService.Client client = cl.getClient();
301
            items = client.getEntityLogisticsEstimation(productId, DEFAULT_PINCODE, DeliveryType.PREPAID );
302
        } catch (Exception e1) {
303
            log.error("Unable to get items by catalog item id", e1);
304
        }
305
        return new Gson().toJson(items);
306
    }
307
 
308
    private static void setRetargettingLabel() {
309
    	label.put((long) 1002160, "6EIBCKjf1wMQ6Pua0wM"); //Samsung Galaxy Y S5360
310
    	label.put((long) 1002106, "JFGVCKCt8gMQ6Pua0wM"); //Sony Ericsson Xperia Neo V MT11i
311
    	label.put((long) 1003230, "ArAYCJiu8gMQ6Pua0wM"); //Micromax Funbook Tab P300
312
    	label.put((long) 1002151, "j0c_CJCv8gMQ6Pua0wM"); //HTC Explorer (Pico) A310e
313
    	label.put((long) 1000607, "7H3QCKDh8wMQ6Pua0wM"); //SanDisk MicroSD 8GB
314
    	label.put((long) 1003213, "QtGQCJji8wMQ6Pua0wM"); //Micromax SUPERFONE Gossip A78
315
    	label.put((long) 1002530, "0YNbCJDj8wMQ6Pua0wM"); //Samsung Champ Deluxe Duos C3312
316
    	label.put((long) 1003039, "pQUaCIjk8wMQ6Pua0wM"); //Samsung Star 3 Duos S5222
317
    	label.put((long) 1002575, "zcYTCIDl8wMQ6Pua0wM"); //Samsung Galaxy Y Duos S6102
318
    }
319
 
320
    public String getRetargettingLabel() {
321
    	return label.get(this.productId);
322
    }
7515 anupam.sin 323
 
324
    public void setStorePin(String storePin) {
325
        this.storePin = storePin;
326
    }
327
 
328
    public String getStorePin() {
329
        return storePin;
330
    }
7272 amit.gupta 331
 
332
}