Subversion Repositories SmartDukaan

Rev

Rev 10161 | 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"),
5941 amit.gupta 76
        @Action("/compare-dslr-cameras"),
5930 amit.gupta 77
        @Action("/compare-battery"),
78
        @Action("/compare-bluetooth-headset"),
79
        @Action("/compare-external-dard-disks"),
80
        @Action("/compare-pen-drive"),
81
        @Action("/compare-memory-card"),
82
        @Action("/compare-face-plate"),
83
        @Action("/compare-car-charger"),
84
        @Action("/compare-decal"),
85
        @Action("/compare-headset"),
86
        @Action("/compare-data-cable"),
87
        @Action("/compare-carrying-case"),
88
        @Action("/compare-headphones"),
89
        @Action("/compare-speaker"),
90
        @Action("/compare-charger"),
10161 amit.gupta 91
        @Action("/compare-screen-guard"),
92
        @Action("/compare-smart-watches"),
93
        @Action("/compare-printer-cartridges"),
94
        @Action("/compare-mouse-pads"),
95
        @Action("/compare-laptop-chargers"),
96
        @Action("/compare-data-card"),
97
        @Action("/compare-laptop-bags"),
98
        @Action("/compare-vehicle-mounts"),
99
        @Action("/compare-power-bank")
5930 amit.gupta 100
 
1778 rajveer 101
    })
102
 
103
    public String index() throws SecurityException, IOException {
104
        productList = new ArrayList<String>();
5322 amit.gupta 105
        StringBuffer prodNames = new StringBuffer("");
106
        StringBuffer requestParams = new StringBuffer("");
1821 rajveer 107
        if(request.getParameter("p1") != null){
10241 amit.gupta 108
            this.productList.add(request.getParameter("p1").trim());
1778 rajveer 109
        }
1821 rajveer 110
        if(request.getParameter("p2") != null){
10241 amit.gupta 111
            this.productList.add(request.getParameter("p2").trim());
1778 rajveer 112
        }
1821 rajveer 113
        if(request.getParameter("p3") != null){
10241 amit.gupta 114
            this.productList.add(request.getParameter("p3").trim());
1778 rajveer 115
        }
1821 rajveer 116
        if(request.getParameter("p4") != null){
10241 amit.gupta 117
            this.productList.add(request.getParameter("p4").trim());
1778 rajveer 118
        }
1821 rajveer 119
        if(request.getParameter("p5") != null){
10241 amit.gupta 120
            this.productList.add(request.getParameter("p5").trim());
1778 rajveer 121
        }
5322 amit.gupta 122
        boolean first = true;
123
        int i=1;
124
        for(String product : productList){        	
125
        	String hyphenatedName = getHyphenatedName(product);
126
        	if(!hyphenatedName.equals("")){
127
        		if(first){
128
        			prodNames.append(hyphenatedName);
129
        			requestParams.append("?p"+ (i++) + "=" + product);
130
        			first = false;
131
        		} else {
132
        			prodNames.append("-vs-" + hyphenatedName);
133
        			requestParams.append("&p"+ (i++) + "=" + product);
134
        		}
135
        	}
136
        }
5552 phani.kuma 137
        if(request.getParameter("fromsrc") != null){
138
	        if(first){
139
	 			requestParams.append("?fromsrc=" + request.getParameter("fromsrc"));
140
	 			first = false;
141
	 		} else {
142
	 			requestParams.append("&fromsrc=" + request.getParameter("fromsrc"));
143
	 		}
144
        }
5547 amit.gupta 145
        redirectUrl = "/" + ActionContext.getContext().getName() + "/" + prodNames.append(requestParams).toString().toLowerCase();
5322 amit.gupta 146
        logger.info("301 Redirection to " + redirectUrl);
147
        return "redirect";
1778 rajveer 148
    }
149
 
5322 amit.gupta 150
    public String show() throws SecurityException, IOException {
5552 phani.kuma 151
         logger.info(ActionContext.getContext().getName());
5322 amit.gupta 152
    	 productList = new ArrayList<String>();
5552 phani.kuma 153
    	 String fromsrc = "";
5322 amit.gupta 154
         if(request.getParameter("p1") != null){
155
             this.productList.add(request.getParameter("p1"));
156
         }
157
         if(request.getParameter("p2") != null){
158
             this.productList.add(request.getParameter("p2"));
159
         }
160
         if(request.getParameter("p3") != null){
161
             this.productList.add(request.getParameter("p3"));
162
         }
163
         if(request.getParameter("p4") != null){
164
             this.productList.add(request.getParameter("p4"));
165
         }
166
         if(request.getParameter("p5") != null){
167
             this.productList.add(request.getParameter("p5"));
168
         }
5552 phani.kuma 169
         if(request.getParameter("fromsrc") != null){
170
        	 fromsrc = request.getParameter("fromsrc");
171
         }
5752 amar.kumar 172
         if(fromsrc!=null) {
173
        	 DataLogger.logData(EventType.PRODUCT_COMPARE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
174
        			 StringUtils.join(productList, ", "), fromsrc);
175
         }
5322 amit.gupta 176
         return "show";
177
    }
178
 
1778 rajveer 179
    public List<String> getSlideNames() {
180
        productNames = new ArrayList<String>();
5048 amit.gupta 181
        productTitles = new ArrayList<String>();
1950 rajveer 182
        slideNameImportanceMap = new LinkedHashMap<String, String>();
183
        productSlideScores = new LinkedHashMap<String, Map<String, Double>>();
5422 amit.gupta 184
        Iterator<String> productListIterator = productList.iterator();
185
        while(productListIterator.hasNext()) {
5547 amit.gupta 186
        	String productId = productListIterator.next();
187
            File f = new File(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideNamesSnippet.html");
1778 rajveer 188
            FileInputStream fis = null;
189
            try {
190
                fis = new FileInputStream(f);
191
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
192
                String line;
5048 amit.gupta 193
                boolean isProductName = true;
194
                boolean isProductTitle = true;
1950 rajveer 195
                Map<String, Double> slideScores = new LinkedHashMap<String, Double>();
1778 rajveer 196
                while((line = br.readLine()) != null){
197
                    if(isProductName){
1950 rajveer 198
                        productNames.add(line);
5547 amit.gupta 199
                        hyphenatedNames.put(productId, line.replaceAll(" +", "-").replaceAll("/", "-").replaceAll("-+", "-"));
1950 rajveer 200
                        productSlideScores.put(line, slideScores);
1778 rajveer 201
                        isProductName = false;
202
                        continue;
203
                    }
5048 amit.gupta 204
                    if(isProductTitle){
205
                    	productTitles.add(line);
206
                    	isProductTitle = false;
207
                    	continue;
208
                    }
1950 rajveer 209
 
210
                    String[] parts = line.split("!!!");
211
                    String slideName = parts[0];
212
                    String importance = parts[1];
213
                    double score = Double.parseDouble(parts[2]);
214
                    if(score > 0){
215
                        if(!slideNameImportanceMap.containsKey(slideName)){
216
                            if(!importance.equals("None")){
217
                                slideNameImportanceMap.put(slideName, importance);
5930 amit.gupta 218
                                comparable = true;
1950 rajveer 219
                            }
220
                        }
221
                        if(!slideScores.containsKey(slideName)){
222
                            slideScores.put(slideName, score);
223
                        }
1778 rajveer 224
                    }
1950 rajveer 225
                    if(!slides.contains(slideName)){
226
                        slides.add(slideName);
227
                    }
228
 
1778 rajveer 229
                }
230
            } catch (IOException e) {
5422 amit.gupta 231
            	productListIterator.remove();
2944 chandransh 232
                logger.error("Error while reading snippet", e);
1778 rajveer 233
            }
234
        }
2944 chandransh 235
        logger.info(productNames);
1950 rajveer 236
 
237
        unComparedSlides = new ArrayList<String>();
238
        for(String slideName: slides){
239
            if(!slideNameImportanceMap.containsKey(slideName)){
240
                unComparedSlides.add(slideName);
241
            }
242
        }
1778 rajveer 243
        return slides;
244
    }
245
 
1950 rajveer 246
    public List<String> getUncomparedSlides(){
247
        return this.unComparedSlides;
248
    }
1778 rajveer 249
 
1950 rajveer 250
    public List<String> getcommonSlides(){
251
        return this.commonSlides;
252
    }
253
 
5322 amit.gupta 254
    public String getRedirectUrl(){
255
    	return this.redirectUrl;	
256
    }
257
 
1950 rajveer 258
    public List<Double> getOverallScores(){
259
        List<Double> overallScores = new ArrayList<Double>();
260
        for(String productId : productSlideScores.keySet()){
261
            Map<String, Double> slideScores = productSlideScores.get(productId);
1977 rajveer 262
            double commulativeWeightedScore = 0.0;
263
            double commulativeWeights = 0.0;
1950 rajveer 264
 
265
            for(String slideName: slideNameImportanceMap.keySet()){
266
                double score = 0.0;
267
                if(slideScores.get(slideName) != null){
268
                    score = slideScores.get(slideName);
269
                }
270
                String importance = slideNameImportanceMap.get(slideName);
271
                if(importance.equals("V. Imp")){
1977 rajveer 272
                    commulativeWeightedScore += score*55;
273
                    commulativeWeights += 55;
1950 rajveer 274
                }
275
                if(importance.equals("Imp")){
1977 rajveer 276
                    commulativeWeightedScore += score*30;
277
                    commulativeWeights += 30;
1950 rajveer 278
                }
279
                if(importance.equals("Not Imp")){
1977 rajveer 280
                    commulativeWeightedScore += score*15;
281
                    commulativeWeights += 15;
1950 rajveer 282
                }
283
            }
1977 rajveer 284
            double weightedScore = commulativeWeightedScore/commulativeWeights;
1950 rajveer 285
 
286
            DecimalFormat oneDForm = new DecimalFormat("#.#");
287
            weightedScore = Double.valueOf(oneDForm.format(weightedScore));
288
 
289
            overallScores.add(weightedScore);
290
        }
291
 
292
 
293
        return overallScores;
294
    }
295
 
1778 rajveer 296
    public Map<String, String> getSnippets(){
297
        if (productList != null) {
1950 rajveer 298
            snippets = new LinkedHashMap<String, String>();
1778 rajveer 299
            for (String productId : productList) {
300
                try {
301
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ComparisonSnippet.html"));
302
                } catch (IOException e) {
2944 chandransh 303
                    logger.error(e);
1778 rajveer 304
                }
305
            }
306
        }
1950 rajveer 307
        this.commonSlides = new ArrayList<String>();
308
 
1778 rajveer 309
        return snippets;
310
    }
1950 rajveer 311
 
312
    public Map<String, String> getProductSummarySnippets(){
313
        if (productList != null) {
314
            snippets = new LinkedHashMap<String, String>();
315
            for (String productId : productList) {
316
                try {
317
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "CompareProductSummarySnippet.html"));
318
                } catch (IOException e) {
2944 chandransh 319
                    logger.error(e);
1950 rajveer 320
                }
321
            }
322
        }
323
        return snippets;
324
    }
1778 rajveer 325
    public Map<String, String> getProductSnippets(){
326
        if (productList != null) {
1950 rajveer 327
            snippets = new LinkedHashMap<String, String>();
1778 rajveer 328
            for (String productId : productList) {
329
                try {
330
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "CompareProductSnippet.html"));
331
                } catch (IOException e) {
2944 chandransh 332
                    logger.error(e);
1778 rajveer 333
                }
334
            }
335
 
336
        }
337
        return snippets;
338
    }
339
 
1950 rajveer 340
    public List<String> getProductList(){
341
        return this.productList;
342
    }
343
 
1778 rajveer 344
    public List<String> getProductNames(){
345
        return this.productNames;
346
    }
1950 rajveer 347
 
5048 amit.gupta 348
    public String getPageTitle(){
349
    	StringBuffer sb1 = new StringBuffer("Compare ");
350
    	StringBuffer sb2 = new StringBuffer("");
351
    	for(String title : this.productTitles) {
352
    		sb2.append(" vs " + title);
353
    	}
354
    	sb2.replace(0, 3, "");
355
    	return sb1.append(sb2).toString();
356
    }
357
 
358
    public String getPageDescription(){
359
    	StringBuffer sb1 = new StringBuffer("Compare ");
360
    	StringBuffer sb2 = new StringBuffer("");
361
    	for(String name : this.productNames) {
362
    		sb2.append(" vs " + name);
363
    	}
364
    	sb2.replace(0, 3, "");
365
    	return sb1.append(sb2).append(" | Price, Specifications and Reviews ").toString();
366
    }
367
 
368
    public String getKeywords(){
369
    	StringBuffer sb1 = new StringBuffer("Compare, ");
370
    	StringBuffer sb2 = new StringBuffer("");
371
    	for(String name : this.productNames) {
372
    		sb2.append(", " + name);
373
    	}
374
    	sb2.replace(0, 2, "");
375
    	return sb1.append(sb2).append(", Price, Specifications and Reviews ").toString();
376
 
377
    }
378
 
1950 rajveer 379
    public Map<String, String> getSlideNameImportanceMap(){
380
        return this.slideNameImportanceMap;
381
    }
382
 
383
    public Map<String, Map<String, Double>> getProductSlideScores(){
384
        return this.productSlideScores;
385
    }
386
 
387
    public List<String> getImportances(){
388
        return this.importances;
389
    }
5322 amit.gupta 390
 
391
    private String getHyphenatedName(String productId) {
392
    	File f = new File(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideNamesSnippet.html");
393
        FileInputStream fis = null;
394
        String hyphenatedName = "";
395
    	 try {
396
             fis = new FileInputStream(f);
397
             BufferedReader br = new BufferedReader(new InputStreamReader(fis));
5358 amit.gupta 398
             hyphenatedName = br.readLine().replaceAll(" +", "-").replaceAll("/", "-").replaceAll("-+", "-");
5322 amit.gupta 399
         } catch (IOException e) {
400
             logger.error("Error while reading snippet", e);
401
         }
402
         return hyphenatedName;
403
    }
5547 amit.gupta 404
 
405
    public String getUrlForGeneratedCompareWith(String productId){
406
    	return "/generated/" + hyphenatedNames.get(productId).toLowerCase() + "?p1=" + productId;
407
    }
408
 
409
    public String getUrlForProductPage(String productId){
410
    	String hyphenatedCategory = ActionContext.getContext().getName();
411
    	hyphenatedCategory = hyphenatedCategory.split("-", 2)[1];
412
    	return "/" + hyphenatedCategory+ "/" + hyphenatedNames.get(productId).toLowerCase() + "-" + productId;
413
    }
414
 
415
    public String getActionName(){
416
    	return ActionContext.getContext().getName();
417
    }
5930 amit.gupta 418
 
419
    public boolean isComparable() {
420
    	return this.comparable;
421
    }
1821 rajveer 422
}