Subversion Repositories SmartDukaan

Rev

Rev 1950 | Rev 1977 | 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
 
3
import in.shop2020.serving.utils.FileUtils;
4
import in.shop2020.serving.utils.Utils;
5
 
6
import java.io.BufferedReader;
7
import java.io.File;
8
import java.io.FileInputStream;
9
import java.io.IOException;
10
import java.io.InputStreamReader;
1950 rajveer 11
import java.text.DecimalFormat;
1778 rajveer 12
import java.util.ArrayList;
13
import java.util.LinkedHashMap;
14
import java.util.List;
15
import java.util.Map;
16
 
17
import org.apache.log4j.Logger;
18
import org.apache.struts2.convention.annotation.Action;
19
import org.apache.struts2.convention.annotation.Actions;
20
import org.apache.struts2.convention.annotation.Result;
21
 
22
/**
23
 * 
24
 * @author rajveer
25
 * 
26
 */
27
 
28
@Result(name = "show", location = "compare-show.vm")
29
public class CompareController extends BaseController {
30
 
31
    private static final long serialVersionUID = 1L;
32
 
33
    private static Logger log = Logger.getLogger(Class.class);
34
 
1950 rajveer 35
    List<String> slides = new ArrayList<String>();
36
 
1778 rajveer 37
    private List<String> productList;
38
    private List<String> productNames;
39
    private Map<String, String> snippets;
1950 rajveer 40
    private List<String> unComparedSlides;
41
    private List<String> commonSlides;
42
 
43
    private Map<String, String> slideNameImportanceMap;
44
 
45
    private Map<String, Map<String, Double>> productSlideScores;
46
 
47
    private List<String> importances = new ArrayList<String>();
48
 
1778 rajveer 49
    public CompareController() {
50
        super();
1950 rajveer 51
        importances.add("V. Imp");
52
        importances.add("Imp");
53
        importances.add("Not Imp");
1778 rajveer 54
    }
55
 
56
    @Actions({
57
        @Action("/compare-mobile-phones"),
58
        @Action("/compare")
59
    })
60
 
61
    public String index() throws SecurityException, IOException {
62
        productList = new ArrayList<String>();
1821 rajveer 63
        if(request.getParameter("p1") != null){
64
            this.productList.add(request.getParameter("p1"));
1778 rajveer 65
        }
1821 rajveer 66
        if(request.getParameter("p2") != null){
67
            this.productList.add(request.getParameter("p2"));
1778 rajveer 68
        }
1821 rajveer 69
        if(request.getParameter("p3") != null){
70
            this.productList.add(request.getParameter("p3"));
1778 rajveer 71
        }
1821 rajveer 72
        if(request.getParameter("p4") != null){
73
            this.productList.add(request.getParameter("p4"));
1778 rajveer 74
        }
1821 rajveer 75
        if(request.getParameter("p5") != null){
76
            this.productList.add(request.getParameter("p5"));
1778 rajveer 77
        }
78
 
79
        return "show";
80
    }
81
 
82
    public List<String> getSlideNames() {
83
        productNames = new ArrayList<String>();
1950 rajveer 84
        slideNameImportanceMap = new LinkedHashMap<String, String>();
85
        productSlideScores = new LinkedHashMap<String, Map<String, Double>>();
86
 
1778 rajveer 87
        for (String productId : productList) {
88
            File f = new File(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideNamesSnippet.html");
89
            FileInputStream fis = null;
90
            try {
91
                fis = new FileInputStream(f);
92
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
93
                String line;
94
                boolean isProductName = true;;
1950 rajveer 95
                Map<String, Double> slideScores = new LinkedHashMap<String, Double>();
1778 rajveer 96
                while((line = br.readLine()) != null){
97
                    if(isProductName){
1950 rajveer 98
                        productNames.add(line);
99
                        productSlideScores.put(line, slideScores);
1778 rajveer 100
                        isProductName = false;
101
                        continue;
102
                    }
1950 rajveer 103
 
104
                    String[] parts = line.split("!!!");
105
                    String slideName = parts[0];
106
                    String importance = parts[1];
107
                    double score = Double.parseDouble(parts[2]);
108
                    if(score > 0){
109
                        if(!slideNameImportanceMap.containsKey(slideName)){
110
                            if(!importance.equals("None")){
111
                                slideNameImportanceMap.put(slideName, importance);
112
                            }
113
                        }
114
                        if(!slideScores.containsKey(slideName)){
115
                            slideScores.put(slideName, score);
116
                        }
1778 rajveer 117
                    }
1950 rajveer 118
                    if(!slides.contains(slideName)){
119
                        slides.add(slideName);
120
                    }
121
 
1778 rajveer 122
                }
123
            } catch (IOException e) {
124
                e.printStackTrace();
125
            }
126
        }
127
        log.info(productNames);
1950 rajveer 128
 
129
        unComparedSlides = new ArrayList<String>();
130
        for(String slideName: slides){
131
            if(!slideNameImportanceMap.containsKey(slideName)){
132
                unComparedSlides.add(slideName);
133
            }
134
        }
1778 rajveer 135
        return slides;
136
    }
137
 
1950 rajveer 138
    public List<String> getUncomparedSlides(){
139
        return this.unComparedSlides;
140
    }
1778 rajveer 141
 
1950 rajveer 142
    public List<String> getcommonSlides(){
143
        return this.commonSlides;
144
    }
145
 
146
    public List<Double> getOverallScores(){
147
        List<Double> overallScores = new ArrayList<Double>();
148
        for(String productId : productSlideScores.keySet()){
149
            Map<String, Double> slideScores = productSlideScores.get(productId);
150
            double veryImportantSlidesScore = 0.0;
151
            double regularSlidesScore = 0.0;
152
            double notSoImportantSlidesScore = 0.0;
153
            int veryImportantSlidesCount = 0;
154
            int regularSlidesCount = 0;
155
            int notSoImportantSlidesCount = 0;
156
 
157
            for(String slideName: slideNameImportanceMap.keySet()){
158
                double score = 0.0;
159
                if(slideScores.get(slideName) != null){
160
                    score = slideScores.get(slideName);
161
                }
162
                String importance = slideNameImportanceMap.get(slideName);
163
                if(importance.equals("V. Imp")){
164
                    veryImportantSlidesScore += score;
165
                    veryImportantSlidesCount++;
166
                }
167
                if(importance.equals("Imp")){
168
                    regularSlidesScore += score;
169
                    regularSlidesCount++;
170
                }
171
                if(importance.equals("Not Imp")){
172
                    notSoImportantSlidesScore += score;
173
                    notSoImportantSlidesCount++;
174
                }
175
            }
176
            System.out.println(veryImportantSlidesScore + " \n" + veryImportantSlidesCount + " \n" + regularSlidesScore + " \n" + regularSlidesCount + " \n" + notSoImportantSlidesScore + " \n" + notSoImportantSlidesCount);
177
            double weightedScore = (55*veryImportantSlidesScore/veryImportantSlidesCount + 30*regularSlidesScore/regularSlidesCount + 15*notSoImportantSlidesScore/notSoImportantSlidesCount)/100;
178
 
179
            DecimalFormat oneDForm = new DecimalFormat("#.#");
180
            weightedScore = Double.valueOf(oneDForm.format(weightedScore));
181
 
182
            overallScores.add(weightedScore);
183
        }
184
 
185
 
186
        return overallScores;
187
    }
188
 
1778 rajveer 189
    public Map<String, String> getSnippets(){
190
        if (productList != null) {
1950 rajveer 191
            snippets = new LinkedHashMap<String, String>();
1778 rajveer 192
            for (String productId : productList) {
193
                try {
194
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ComparisonSnippet.html"));
195
                } catch (IOException e) {
196
                    log.error(e);
197
                }
198
            }
199
        }
1950 rajveer 200
        this.commonSlides = new ArrayList<String>();
201
 
1778 rajveer 202
        return snippets;
203
    }
1950 rajveer 204
 
205
    public Map<String, String> getProductSummarySnippets(){
206
        if (productList != null) {
207
            snippets = new LinkedHashMap<String, String>();
208
            for (String productId : productList) {
209
                try {
210
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "CompareProductSummarySnippet.html"));
211
                } catch (IOException e) {
212
                    log.error(e);
213
                }
214
            }
215
        }
216
        return snippets;
217
    }
1778 rajveer 218
    public Map<String, String> getProductSnippets(){
219
        if (productList != null) {
1950 rajveer 220
            snippets = new LinkedHashMap<String, String>();
1778 rajveer 221
            for (String productId : productList) {
222
                try {
223
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "CompareProductSnippet.html"));
224
                } catch (IOException e) {
225
                    log.error(e);
226
                }
227
            }
228
 
229
        }
230
        return snippets;
231
    }
232
 
1950 rajveer 233
    public List<String> getProductList(){
234
        return this.productList;
235
    }
236
 
1778 rajveer 237
    public List<String> getProductNames(){
238
        return this.productNames;
239
    }
1950 rajveer 240
 
241
    public Map<String, String> getSlideNameImportanceMap(){
242
        return this.slideNameImportanceMap;
243
    }
244
 
245
    public Map<String, Map<String, Double>> getProductSlideScores(){
246
        return this.productSlideScores;
247
    }
248
 
249
    public List<String> getImportances(){
250
        return this.importances;
251
    }
1821 rajveer 252
}