Subversion Repositories SmartDukaan

Rev

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