Subversion Repositories SmartDukaan

Rev

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