Subversion Repositories SmartDukaan

Rev

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