Subversion Repositories SmartDukaan

Rev

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