Subversion Repositories SmartDukaan

Rev

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