Subversion Repositories SmartDukaan

Rev

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