Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
1778 rajveer 1
package in.shop2020.serving.controllers;
2
 
2263 vikas 3
import in.shop2020.datalogger.EventType;
1778 rajveer 4
import in.shop2020.serving.utils.FileUtils;
5
import in.shop2020.serving.utils.Utils;
2511 vikas 6
import in.shop2020.utils.DataLogger;
1778 rajveer 7
 
8
import java.io.BufferedReader;
9
import java.io.File;
10
import java.io.FileInputStream;
11
import java.io.IOException;
12
import java.io.InputStreamReader;
1950 rajveer 13
import java.text.DecimalFormat;
1778 rajveer 14
import java.util.ArrayList;
5547 amit.gupta 15
import java.util.HashMap;
5422 amit.gupta 16
import java.util.Iterator;
1778 rajveer 17
import java.util.LinkedHashMap;
18
import java.util.List;
19
import java.util.Map;
20
 
2056 vikas 21
import org.apache.commons.lang.StringUtils;
1778 rajveer 22
import org.apache.log4j.Logger;
23
import org.apache.struts2.convention.annotation.Action;
24
import org.apache.struts2.convention.annotation.Actions;
25
import org.apache.struts2.convention.annotation.Result;
5322 amit.gupta 26
import org.apache.struts2.convention.annotation.Results;
1778 rajveer 27
 
5547 amit.gupta 28
import com.opensymphony.xwork2.ActionContext;
29
 
1778 rajveer 30
/**
31
 * 
32
 * @author rajveer
33
 * 
34
 */
35
 
5322 amit.gupta 36
@Results({
37
    @Result(name = "show", location = "compare-show.vm"),
38
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
39
})
1778 rajveer 40
public class CompareController extends BaseController {
41
 
42
    private static final long serialVersionUID = 1L;
43
 
2944 chandransh 44
    private static Logger logger = Logger.getLogger(Class.class);
2157 vikas 45
 
1950 rajveer 46
    List<String> slides = new ArrayList<String>();
47
 
1778 rajveer 48
    private List<String> productList;
49
    private List<String> productNames;
5048 amit.gupta 50
    private List<String> productTitles;
1778 rajveer 51
    private Map<String, String> snippets;
1950 rajveer 52
    private List<String> unComparedSlides;
53
    private List<String> commonSlides;
5547 amit.gupta 54
    private Map<String, String> hyphenatedNames = new HashMap<String, String>();
5322 amit.gupta 55
    private String redirectUrl;
5930 amit.gupta 56
    private boolean comparable = false;
1950 rajveer 57
 
58
    private Map<String, String> slideNameImportanceMap;
59
 
60
    private Map<String, Map<String, Double>> productSlideScores;
61
 
62
    private List<String> importances = new ArrayList<String>();
63
 
1778 rajveer 64
    public CompareController() {
65
        super();
1950 rajveer 66
        importances.add("V. Imp");
67
        importances.add("Imp");
68
        importances.add("Not Imp");
1778 rajveer 69
    }
70
 
71
    @Actions({
72
        @Action("/compare-mobile-phones"),
5347 amit.gupta 73
        @Action("/compare-laptops"),
74
        @Action("/compare-tablets"),
5930 amit.gupta 75
        @Action("/compare-compact-cameras"),
76
        @Action("/compare-battery"),
77
        @Action("/compare-bluetooth-headset"),
78
        @Action("/compare-external-dard-disks"),
79
        @Action("/compare-pen-drive"),
80
        @Action("/compare-memory-card"),
81
        @Action("/compare-face-plate"),
82
        @Action("/compare-car-charger"),
83
        @Action("/compare-decal"),
84
        @Action("/compare-headset"),
85
        @Action("/compare-data-cable"),
86
        @Action("/compare-carrying-case"),
87
        @Action("/compare-headphones"),
88
        @Action("/compare-speaker"),
89
        @Action("/compare-charger"),
90
        @Action("/compare-sceen-guard")
91
 
1778 rajveer 92
    })
93
 
94
    public String index() throws SecurityException, IOException {
95
        productList = new ArrayList<String>();
5322 amit.gupta 96
        StringBuffer prodNames = new StringBuffer("");
97
        StringBuffer requestParams = new StringBuffer("");
1821 rajveer 98
        if(request.getParameter("p1") != null){
99
            this.productList.add(request.getParameter("p1"));
1778 rajveer 100
        }
1821 rajveer 101
        if(request.getParameter("p2") != null){
102
            this.productList.add(request.getParameter("p2"));
1778 rajveer 103
        }
1821 rajveer 104
        if(request.getParameter("p3") != null){
105
            this.productList.add(request.getParameter("p3"));
1778 rajveer 106
        }
1821 rajveer 107
        if(request.getParameter("p4") != null){
108
            this.productList.add(request.getParameter("p4"));
1778 rajveer 109
        }
1821 rajveer 110
        if(request.getParameter("p5") != null){
111
            this.productList.add(request.getParameter("p5"));
1778 rajveer 112
        }
5322 amit.gupta 113
        boolean first = true;
114
        int i=1;
115
        for(String product : productList){        	
116
        	String hyphenatedName = getHyphenatedName(product);
117
        	if(!hyphenatedName.equals("")){
118
        		if(first){
119
        			prodNames.append(hyphenatedName);
120
        			requestParams.append("?p"+ (i++) + "=" + product);
121
        			first = false;
122
        		} else {
123
        			prodNames.append("-vs-" + hyphenatedName);
124
        			requestParams.append("&p"+ (i++) + "=" + product);
125
        		}
126
        	}
127
        }
5552 phani.kuma 128
        if(request.getParameter("fromsrc") != null){
129
	        if(first){
130
	 			requestParams.append("?fromsrc=" + request.getParameter("fromsrc"));
131
	 			first = false;
132
	 		} else {
133
	 			requestParams.append("&fromsrc=" + request.getParameter("fromsrc"));
134
	 		}
135
        }
5547 amit.gupta 136
        redirectUrl = "/" + ActionContext.getContext().getName() + "/" + prodNames.append(requestParams).toString().toLowerCase();
5322 amit.gupta 137
        logger.info("301 Redirection to " + redirectUrl);
138
        return "redirect";
1778 rajveer 139
    }
140
 
5322 amit.gupta 141
    public String show() throws SecurityException, IOException {
5552 phani.kuma 142
         logger.info(ActionContext.getContext().getName());
5322 amit.gupta 143
    	 productList = new ArrayList<String>();
5552 phani.kuma 144
    	 String fromsrc = "";
5322 amit.gupta 145
         if(request.getParameter("p1") != null){
146
             this.productList.add(request.getParameter("p1"));
147
         }
148
         if(request.getParameter("p2") != null){
149
             this.productList.add(request.getParameter("p2"));
150
         }
151
         if(request.getParameter("p3") != null){
152
             this.productList.add(request.getParameter("p3"));
153
         }
154
         if(request.getParameter("p4") != null){
155
             this.productList.add(request.getParameter("p4"));
156
         }
157
         if(request.getParameter("p5") != null){
158
             this.productList.add(request.getParameter("p5"));
159
         }
5552 phani.kuma 160
         if(request.getParameter("fromsrc") != null){
161
        	 fromsrc = request.getParameter("fromsrc");
162
         }
5752 amar.kumar 163
         if(fromsrc!=null) {
164
        	 DataLogger.logData(EventType.PRODUCT_COMPARE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
165
        			 StringUtils.join(productList, ", "), fromsrc);
166
         }
5322 amit.gupta 167
         return "show";
168
    }
169
 
1778 rajveer 170
    public List<String> getSlideNames() {
171
        productNames = new ArrayList<String>();
5048 amit.gupta 172
        productTitles = new ArrayList<String>();
1950 rajveer 173
        slideNameImportanceMap = new LinkedHashMap<String, String>();
174
        productSlideScores = new LinkedHashMap<String, Map<String, Double>>();
5422 amit.gupta 175
        Iterator<String> productListIterator = productList.iterator();
176
        while(productListIterator.hasNext()) {
5547 amit.gupta 177
        	String productId = productListIterator.next();
178
            File f = new File(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideNamesSnippet.html");
1778 rajveer 179
            FileInputStream fis = null;
180
            try {
181
                fis = new FileInputStream(f);
182
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
183
                String line;
5048 amit.gupta 184
                boolean isProductName = true;
185
                boolean isProductTitle = true;
1950 rajveer 186
                Map<String, Double> slideScores = new LinkedHashMap<String, Double>();
1778 rajveer 187
                while((line = br.readLine()) != null){
188
                    if(isProductName){
1950 rajveer 189
                        productNames.add(line);
5547 amit.gupta 190
                        hyphenatedNames.put(productId, line.replaceAll(" +", "-").replaceAll("/", "-").replaceAll("-+", "-"));
1950 rajveer 191
                        productSlideScores.put(line, slideScores);
1778 rajveer 192
                        isProductName = false;
193
                        continue;
194
                    }
5048 amit.gupta 195
                    if(isProductTitle){
196
                    	productTitles.add(line);
197
                    	isProductTitle = false;
198
                    	continue;
199
                    }
1950 rajveer 200
 
201
                    String[] parts = line.split("!!!");
202
                    String slideName = parts[0];
203
                    String importance = parts[1];
204
                    double score = Double.parseDouble(parts[2]);
205
                    if(score > 0){
206
                        if(!slideNameImportanceMap.containsKey(slideName)){
207
                            if(!importance.equals("None")){
208
                                slideNameImportanceMap.put(slideName, importance);
5930 amit.gupta 209
                                comparable = true;
1950 rajveer 210
                            }
211
                        }
212
                        if(!slideScores.containsKey(slideName)){
213
                            slideScores.put(slideName, score);
214
                        }
1778 rajveer 215
                    }
1950 rajveer 216
                    if(!slides.contains(slideName)){
217
                        slides.add(slideName);
218
                    }
219
 
1778 rajveer 220
                }
221
            } catch (IOException e) {
5422 amit.gupta 222
            	productListIterator.remove();
2944 chandransh 223
                logger.error("Error while reading snippet", e);
1778 rajveer 224
            }
225
        }
2944 chandransh 226
        logger.info(productNames);
1950 rajveer 227
 
228
        unComparedSlides = new ArrayList<String>();
229
        for(String slideName: slides){
230
            if(!slideNameImportanceMap.containsKey(slideName)){
231
                unComparedSlides.add(slideName);
232
            }
233
        }
1778 rajveer 234
        return slides;
235
    }
236
 
1950 rajveer 237
    public List<String> getUncomparedSlides(){
238
        return this.unComparedSlides;
239
    }
1778 rajveer 240
 
1950 rajveer 241
    public List<String> getcommonSlides(){
242
        return this.commonSlides;
243
    }
244
 
5322 amit.gupta 245
    public String getRedirectUrl(){
246
    	return this.redirectUrl;	
247
    }
248
 
1950 rajveer 249
    public List<Double> getOverallScores(){
250
        List<Double> overallScores = new ArrayList<Double>();
251
        for(String productId : productSlideScores.keySet()){
252
            Map<String, Double> slideScores = productSlideScores.get(productId);
1977 rajveer 253
            double commulativeWeightedScore = 0.0;
254
            double commulativeWeights = 0.0;
1950 rajveer 255
 
256
            for(String slideName: slideNameImportanceMap.keySet()){
257
                double score = 0.0;
258
                if(slideScores.get(slideName) != null){
259
                    score = slideScores.get(slideName);
260
                }
261
                String importance = slideNameImportanceMap.get(slideName);
262
                if(importance.equals("V. Imp")){
1977 rajveer 263
                    commulativeWeightedScore += score*55;
264
                    commulativeWeights += 55;
1950 rajveer 265
                }
266
                if(importance.equals("Imp")){
1977 rajveer 267
                    commulativeWeightedScore += score*30;
268
                    commulativeWeights += 30;
1950 rajveer 269
                }
270
                if(importance.equals("Not Imp")){
1977 rajveer 271
                    commulativeWeightedScore += score*15;
272
                    commulativeWeights += 15;
1950 rajveer 273
                }
274
            }
1977 rajveer 275
            double weightedScore = commulativeWeightedScore/commulativeWeights;
1950 rajveer 276
 
277
            DecimalFormat oneDForm = new DecimalFormat("#.#");
278
            weightedScore = Double.valueOf(oneDForm.format(weightedScore));
279
 
280
            overallScores.add(weightedScore);
281
        }
282
 
283
 
284
        return overallScores;
285
    }
286
 
1778 rajveer 287
    public Map<String, String> getSnippets(){
288
        if (productList != null) {
1950 rajveer 289
            snippets = new LinkedHashMap<String, String>();
1778 rajveer 290
            for (String productId : productList) {
291
                try {
292
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ComparisonSnippet.html"));
293
                } catch (IOException e) {
2944 chandransh 294
                    logger.error(e);
1778 rajveer 295
                }
296
            }
297
        }
1950 rajveer 298
        this.commonSlides = new ArrayList<String>();
299
 
1778 rajveer 300
        return snippets;
301
    }
1950 rajveer 302
 
303
    public Map<String, String> getProductSummarySnippets(){
304
        if (productList != null) {
305
            snippets = new LinkedHashMap<String, String>();
306
            for (String productId : productList) {
307
                try {
308
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "CompareProductSummarySnippet.html"));
309
                } catch (IOException e) {
2944 chandransh 310
                    logger.error(e);
1950 rajveer 311
                }
312
            }
313
        }
314
        return snippets;
315
    }
1778 rajveer 316
    public Map<String, String> getProductSnippets(){
317
        if (productList != null) {
1950 rajveer 318
            snippets = new LinkedHashMap<String, String>();
1778 rajveer 319
            for (String productId : productList) {
320
                try {
321
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "CompareProductSnippet.html"));
322
                } catch (IOException e) {
2944 chandransh 323
                    logger.error(e);
1778 rajveer 324
                }
325
            }
326
 
327
        }
328
        return snippets;
329
    }
330
 
1950 rajveer 331
    public List<String> getProductList(){
332
        return this.productList;
333
    }
334
 
1778 rajveer 335
    public List<String> getProductNames(){
336
        return this.productNames;
337
    }
1950 rajveer 338
 
5048 amit.gupta 339
    public String getPageTitle(){
340
    	StringBuffer sb1 = new StringBuffer("Compare ");
341
    	StringBuffer sb2 = new StringBuffer("");
342
    	for(String title : this.productTitles) {
343
    		sb2.append(" vs " + title);
344
    	}
345
    	sb2.replace(0, 3, "");
346
    	return sb1.append(sb2).toString();
347
    }
348
 
349
    public String getPageDescription(){
350
    	StringBuffer sb1 = new StringBuffer("Compare ");
351
    	StringBuffer sb2 = new StringBuffer("");
352
    	for(String name : this.productNames) {
353
    		sb2.append(" vs " + name);
354
    	}
355
    	sb2.replace(0, 3, "");
356
    	return sb1.append(sb2).append(" | Price, Specifications and Reviews ").toString();
357
    }
358
 
359
    public String getKeywords(){
360
    	StringBuffer sb1 = new StringBuffer("Compare, ");
361
    	StringBuffer sb2 = new StringBuffer("");
362
    	for(String name : this.productNames) {
363
    		sb2.append(", " + name);
364
    	}
365
    	sb2.replace(0, 2, "");
366
    	return sb1.append(sb2).append(", Price, Specifications and Reviews ").toString();
367
 
368
    }
369
 
1950 rajveer 370
    public Map<String, String> getSlideNameImportanceMap(){
371
        return this.slideNameImportanceMap;
372
    }
373
 
374
    public Map<String, Map<String, Double>> getProductSlideScores(){
375
        return this.productSlideScores;
376
    }
377
 
378
    public List<String> getImportances(){
379
        return this.importances;
380
    }
5322 amit.gupta 381
 
382
    private String getHyphenatedName(String productId) {
383
    	File f = new File(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideNamesSnippet.html");
384
        FileInputStream fis = null;
385
        String hyphenatedName = "";
386
    	 try {
387
             fis = new FileInputStream(f);
388
             BufferedReader br = new BufferedReader(new InputStreamReader(fis));
5358 amit.gupta 389
             hyphenatedName = br.readLine().replaceAll(" +", "-").replaceAll("/", "-").replaceAll("-+", "-");
5322 amit.gupta 390
         } catch (IOException e) {
391
             logger.error("Error while reading snippet", e);
392
         }
393
         return hyphenatedName;
394
    }
5547 amit.gupta 395
 
396
    public String getUrlForGeneratedCompareWith(String productId){
397
    	return "/generated/" + hyphenatedNames.get(productId).toLowerCase() + "?p1=" + productId;
398
    }
399
 
400
    public String getUrlForProductPage(String productId){
401
    	String hyphenatedCategory = ActionContext.getContext().getName();
402
    	hyphenatedCategory = hyphenatedCategory.split("-", 2)[1];
403
    	return "/" + hyphenatedCategory+ "/" + hyphenatedNames.get(productId).toLowerCase() + "-" + productId;
404
    }
405
 
406
    public String getActionName(){
407
    	return ActionContext.getContext().getName();
408
    }
5930 amit.gupta 409
 
410
    public boolean isComparable() {
411
    	return this.comparable;
412
    }
1821 rajveer 413
}