Subversion Repositories SmartDukaan

Rev

Rev 5347 | Rev 5422 | 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;
15
import java.util.LinkedHashMap;
16
import java.util.List;
17
import java.util.Map;
18
 
2056 vikas 19
import org.apache.commons.lang.StringUtils;
1778 rajveer 20
import org.apache.log4j.Logger;
21
import org.apache.struts2.convention.annotation.Action;
22
import org.apache.struts2.convention.annotation.Actions;
23
import org.apache.struts2.convention.annotation.Result;
5322 amit.gupta 24
import org.apache.struts2.convention.annotation.Results;
1778 rajveer 25
 
26
/**
27
 * 
28
 * @author rajveer
29
 * 
30
 */
31
 
5322 amit.gupta 32
@Results({
33
    @Result(name = "show", location = "compare-show.vm"),
34
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
35
})
1778 rajveer 36
public class CompareController extends BaseController {
37
 
38
    private static final long serialVersionUID = 1L;
39
 
2944 chandransh 40
    private static Logger logger = Logger.getLogger(Class.class);
2157 vikas 41
 
1950 rajveer 42
    List<String> slides = new ArrayList<String>();
43
 
1778 rajveer 44
    private List<String> productList;
45
    private List<String> productNames;
5048 amit.gupta 46
    private List<String> productTitles;
1778 rajveer 47
    private Map<String, String> snippets;
1950 rajveer 48
    private List<String> unComparedSlides;
49
    private List<String> commonSlides;
5322 amit.gupta 50
    private String redirectUrl;
1950 rajveer 51
 
52
    private Map<String, String> slideNameImportanceMap;
53
 
54
    private Map<String, Map<String, Double>> productSlideScores;
55
 
56
    private List<String> importances = new ArrayList<String>();
57
 
1778 rajveer 58
    public CompareController() {
59
        super();
1950 rajveer 60
        importances.add("V. Imp");
61
        importances.add("Imp");
62
        importances.add("Not Imp");
1778 rajveer 63
    }
64
 
65
    @Actions({
66
        @Action("/compare-mobile-phones"),
5347 amit.gupta 67
        @Action("/compare-laptops"),
68
        @Action("/compare-tablets"),
1778 rajveer 69
        @Action("/compare")
70
    })
71
 
72
    public String index() throws SecurityException, IOException {
73
        productList = new ArrayList<String>();
5322 amit.gupta 74
        StringBuffer prodNames = new StringBuffer("");
75
        StringBuffer requestParams = new StringBuffer("");
1821 rajveer 76
        if(request.getParameter("p1") != null){
77
            this.productList.add(request.getParameter("p1"));
1778 rajveer 78
        }
1821 rajveer 79
        if(request.getParameter("p2") != null){
80
            this.productList.add(request.getParameter("p2"));
1778 rajveer 81
        }
1821 rajveer 82
        if(request.getParameter("p3") != null){
83
            this.productList.add(request.getParameter("p3"));
1778 rajveer 84
        }
1821 rajveer 85
        if(request.getParameter("p4") != null){
86
            this.productList.add(request.getParameter("p4"));
1778 rajveer 87
        }
1821 rajveer 88
        if(request.getParameter("p5") != null){
89
            this.productList.add(request.getParameter("p5"));
1778 rajveer 90
        }
5322 amit.gupta 91
        boolean first = true;
92
        int i=1;
93
        for(String product : productList){        	
94
        	String hyphenatedName = getHyphenatedName(product);
95
        	if(!hyphenatedName.equals("")){
96
        		if(first){
97
        			prodNames.append(hyphenatedName);
98
        			requestParams.append("?p"+ (i++) + "=" + product);
99
        			first = false;
100
        		} else {
101
        			prodNames.append("-vs-" + hyphenatedName);
102
        			requestParams.append("&p"+ (i++) + "=" + product);
103
        		}
104
        	}
105
        }
106
        redirectUrl = "/compare-mobile-phones/" + prodNames.append(requestParams).toString().toLowerCase();
107
        logger.info("301 Redirection to " + redirectUrl);
108
        return "redirect";
1778 rajveer 109
    }
110
 
5322 amit.gupta 111
    public String show() throws SecurityException, IOException {
112
    	 productList = new ArrayList<String>();
113
         if(request.getParameter("p1") != null){
114
             this.productList.add(request.getParameter("p1"));
115
         }
116
         if(request.getParameter("p2") != null){
117
             this.productList.add(request.getParameter("p2"));
118
         }
119
         if(request.getParameter("p3") != null){
120
             this.productList.add(request.getParameter("p3"));
121
         }
122
         if(request.getParameter("p4") != null){
123
             this.productList.add(request.getParameter("p4"));
124
         }
125
         if(request.getParameter("p5") != null){
126
             this.productList.add(request.getParameter("p5"));
127
         }
128
 
129
         DataLogger.logData(EventType.PRODUCT_COMPARE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
130
                 StringUtils.join(productList, ", "));
131
         return "show";
132
    }
133
 
1778 rajveer 134
    public List<String> getSlideNames() {
135
        productNames = new ArrayList<String>();
5048 amit.gupta 136
        productTitles = new ArrayList<String>();
1950 rajveer 137
        slideNameImportanceMap = new LinkedHashMap<String, String>();
138
        productSlideScores = new LinkedHashMap<String, Map<String, Double>>();
139
 
1778 rajveer 140
        for (String productId : productList) {
141
            File f = new File(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideNamesSnippet.html");
142
            FileInputStream fis = null;
143
            try {
144
                fis = new FileInputStream(f);
145
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
146
                String line;
5048 amit.gupta 147
                boolean isProductName = true;
148
                boolean isProductTitle = true;
1950 rajveer 149
                Map<String, Double> slideScores = new LinkedHashMap<String, Double>();
1778 rajveer 150
                while((line = br.readLine()) != null){
151
                    if(isProductName){
1950 rajveer 152
                        productNames.add(line);
153
                        productSlideScores.put(line, slideScores);
1778 rajveer 154
                        isProductName = false;
155
                        continue;
156
                    }
5048 amit.gupta 157
                    if(isProductTitle){
158
                    	productTitles.add(line);
159
                    	isProductTitle = false;
160
                    	continue;
161
                    }
1950 rajveer 162
 
163
                    String[] parts = line.split("!!!");
164
                    String slideName = parts[0];
165
                    String importance = parts[1];
166
                    double score = Double.parseDouble(parts[2]);
167
                    if(score > 0){
168
                        if(!slideNameImportanceMap.containsKey(slideName)){
169
                            if(!importance.equals("None")){
170
                                slideNameImportanceMap.put(slideName, importance);
171
                            }
172
                        }
173
                        if(!slideScores.containsKey(slideName)){
174
                            slideScores.put(slideName, score);
175
                        }
1778 rajveer 176
                    }
1950 rajveer 177
                    if(!slides.contains(slideName)){
178
                        slides.add(slideName);
179
                    }
180
 
1778 rajveer 181
                }
182
            } catch (IOException e) {
2944 chandransh 183
                logger.error("Error while reading snippet", e);
1778 rajveer 184
            }
185
        }
2944 chandransh 186
        logger.info(productNames);
1950 rajveer 187
 
188
        unComparedSlides = new ArrayList<String>();
189
        for(String slideName: slides){
190
            if(!slideNameImportanceMap.containsKey(slideName)){
191
                unComparedSlides.add(slideName);
192
            }
193
        }
1778 rajveer 194
        return slides;
195
    }
196
 
1950 rajveer 197
    public List<String> getUncomparedSlides(){
198
        return this.unComparedSlides;
199
    }
1778 rajveer 200
 
1950 rajveer 201
    public List<String> getcommonSlides(){
202
        return this.commonSlides;
203
    }
204
 
5322 amit.gupta 205
    public String getRedirectUrl(){
206
    	return this.redirectUrl;	
207
    }
208
 
1950 rajveer 209
    public List<Double> getOverallScores(){
210
        List<Double> overallScores = new ArrayList<Double>();
211
        for(String productId : productSlideScores.keySet()){
212
            Map<String, Double> slideScores = productSlideScores.get(productId);
1977 rajveer 213
            double commulativeWeightedScore = 0.0;
214
            double commulativeWeights = 0.0;
1950 rajveer 215
 
216
            for(String slideName: slideNameImportanceMap.keySet()){
217
                double score = 0.0;
218
                if(slideScores.get(slideName) != null){
219
                    score = slideScores.get(slideName);
220
                }
221
                String importance = slideNameImportanceMap.get(slideName);
222
                if(importance.equals("V. Imp")){
1977 rajveer 223
                    commulativeWeightedScore += score*55;
224
                    commulativeWeights += 55;
1950 rajveer 225
                }
226
                if(importance.equals("Imp")){
1977 rajveer 227
                    commulativeWeightedScore += score*30;
228
                    commulativeWeights += 30;
1950 rajveer 229
                }
230
                if(importance.equals("Not Imp")){
1977 rajveer 231
                    commulativeWeightedScore += score*15;
232
                    commulativeWeights += 15;
1950 rajveer 233
                }
234
            }
1977 rajveer 235
            double weightedScore = commulativeWeightedScore/commulativeWeights;
1950 rajveer 236
 
237
            DecimalFormat oneDForm = new DecimalFormat("#.#");
238
            weightedScore = Double.valueOf(oneDForm.format(weightedScore));
239
 
240
            overallScores.add(weightedScore);
241
        }
242
 
243
 
244
        return overallScores;
245
    }
246
 
1778 rajveer 247
    public Map<String, String> getSnippets(){
248
        if (productList != null) {
1950 rajveer 249
            snippets = new LinkedHashMap<String, String>();
1778 rajveer 250
            for (String productId : productList) {
251
                try {
252
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ComparisonSnippet.html"));
253
                } catch (IOException e) {
2944 chandransh 254
                    logger.error(e);
1778 rajveer 255
                }
256
            }
257
        }
1950 rajveer 258
        this.commonSlides = new ArrayList<String>();
259
 
1778 rajveer 260
        return snippets;
261
    }
1950 rajveer 262
 
263
    public Map<String, String> getProductSummarySnippets(){
264
        if (productList != null) {
265
            snippets = new LinkedHashMap<String, String>();
266
            for (String productId : productList) {
267
                try {
268
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "CompareProductSummarySnippet.html"));
269
                } catch (IOException e) {
2944 chandransh 270
                    logger.error(e);
1950 rajveer 271
                }
272
            }
273
        }
274
        return snippets;
275
    }
1778 rajveer 276
    public Map<String, String> getProductSnippets(){
277
        if (productList != null) {
1950 rajveer 278
            snippets = new LinkedHashMap<String, String>();
1778 rajveer 279
            for (String productId : productList) {
280
                try {
281
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "CompareProductSnippet.html"));
282
                } catch (IOException e) {
2944 chandransh 283
                    logger.error(e);
1778 rajveer 284
                }
285
            }
286
 
287
        }
288
        return snippets;
289
    }
290
 
1950 rajveer 291
    public List<String> getProductList(){
292
        return this.productList;
293
    }
294
 
1778 rajveer 295
    public List<String> getProductNames(){
296
        return this.productNames;
297
    }
1950 rajveer 298
 
5048 amit.gupta 299
    public String getPageTitle(){
300
    	StringBuffer sb1 = new StringBuffer("Compare ");
301
    	StringBuffer sb2 = new StringBuffer("");
302
    	for(String title : this.productTitles) {
303
    		sb2.append(" vs " + title);
304
    	}
305
    	sb2.replace(0, 3, "");
306
    	return sb1.append(sb2).toString();
307
    }
308
 
309
    public String getPageDescription(){
310
    	StringBuffer sb1 = new StringBuffer("Compare ");
311
    	StringBuffer sb2 = new StringBuffer("");
312
    	for(String name : this.productNames) {
313
    		sb2.append(" vs " + name);
314
    	}
315
    	sb2.replace(0, 3, "");
316
    	return sb1.append(sb2).append(" | Price, Specifications and Reviews ").toString();
317
    }
318
 
319
    public String getKeywords(){
320
    	StringBuffer sb1 = new StringBuffer("Compare, ");
321
    	StringBuffer sb2 = new StringBuffer("");
322
    	for(String name : this.productNames) {
323
    		sb2.append(", " + name);
324
    	}
325
    	sb2.replace(0, 2, "");
326
    	return sb1.append(sb2).append(", Price, Specifications and Reviews ").toString();
327
 
328
    }
329
 
1950 rajveer 330
    public Map<String, String> getSlideNameImportanceMap(){
331
        return this.slideNameImportanceMap;
332
    }
333
 
334
    public Map<String, Map<String, Double>> getProductSlideScores(){
335
        return this.productSlideScores;
336
    }
337
 
338
    public List<String> getImportances(){
339
        return this.importances;
340
    }
5322 amit.gupta 341
 
342
    private String getHyphenatedName(String productId) {
343
    	File f = new File(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideNamesSnippet.html");
344
        FileInputStream fis = null;
345
        String hyphenatedName = "";
346
    	 try {
347
             fis = new FileInputStream(f);
348
             BufferedReader br = new BufferedReader(new InputStreamReader(fis));
5358 amit.gupta 349
             hyphenatedName = br.readLine().replaceAll(" +", "-").replaceAll("/", "-").replaceAll("-+", "-");
5322 amit.gupta 350
         } catch (IOException e) {
351
             logger.error("Error while reading snippet", e);
352
         }
353
         return hyphenatedName;
354
    }
1821 rajveer 355
}