Subversion Repositories SmartDukaan

Rev

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