Subversion Repositories SmartDukaan

Rev

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