Subversion Repositories SmartDukaan

Rev

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