Subversion Repositories SmartDukaan

Rev

Rev 1821 | Rev 1952 | 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
        for(String slideName: slides){
144
            for(String productId: snippets.keySet()){
145
 
146
            }
147
        }
148
        return this.commonSlides;
149
    }
150
 
151
    public List<Double> getOverallScores(){
152
        List<Double> overallScores = new ArrayList<Double>();
153
        for(String productId : productSlideScores.keySet()){
154
            Map<String, Double> slideScores = productSlideScores.get(productId);
155
            double veryImportantSlidesScore = 0.0;
156
            double regularSlidesScore = 0.0;
157
            double notSoImportantSlidesScore = 0.0;
158
            int veryImportantSlidesCount = 0;
159
            int regularSlidesCount = 0;
160
            int notSoImportantSlidesCount = 0;
161
 
162
            for(String slideName: slideNameImportanceMap.keySet()){
163
                double score = 0.0;
164
                if(slideScores.get(slideName) != null){
165
                    score = slideScores.get(slideName);
166
                }
167
                String importance = slideNameImportanceMap.get(slideName);
168
                if(importance.equals("V. Imp")){
169
                    veryImportantSlidesScore += score;
170
                    veryImportantSlidesCount++;
171
                }
172
                if(importance.equals("Imp")){
173
                    regularSlidesScore += score;
174
                    regularSlidesCount++;
175
                }
176
                if(importance.equals("Not Imp")){
177
                    notSoImportantSlidesScore += score;
178
                    notSoImportantSlidesCount++;
179
                }
180
            }
181
            System.out.println(veryImportantSlidesScore + " \n" + veryImportantSlidesCount + " \n" + regularSlidesScore + " \n" + regularSlidesCount + " \n" + notSoImportantSlidesScore + " \n" + notSoImportantSlidesCount);
182
            double weightedScore = (55*veryImportantSlidesScore/veryImportantSlidesCount + 30*regularSlidesScore/regularSlidesCount + 15*notSoImportantSlidesScore/notSoImportantSlidesCount)/100;
183
 
184
            DecimalFormat oneDForm = new DecimalFormat("#.#");
185
            weightedScore = Double.valueOf(oneDForm.format(weightedScore));
186
 
187
            overallScores.add(weightedScore);
188
        }
189
 
190
 
191
        return overallScores;
192
    }
193
 
1778 rajveer 194
    public Map<String, String> getSnippets(){
195
        if (productList != null) {
1950 rajveer 196
            snippets = new LinkedHashMap<String, String>();
1778 rajveer 197
            for (String productId : productList) {
198
                try {
199
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ComparisonSnippet.html"));
200
                } catch (IOException e) {
201
                    log.error(e);
202
                }
203
            }
204
        }
1950 rajveer 205
        this.commonSlides = new ArrayList<String>();
206
 
1778 rajveer 207
        return snippets;
208
    }
1950 rajveer 209
 
210
    public Map<String, String> getProductSummarySnippets(){
211
        if (productList != null) {
212
            snippets = new LinkedHashMap<String, String>();
213
            for (String productId : productList) {
214
                try {
215
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "CompareProductSummarySnippet.html"));
216
                } catch (IOException e) {
217
                    log.error(e);
218
                }
219
            }
220
        }
221
        return snippets;
222
    }
1778 rajveer 223
    public Map<String, String> getProductSnippets(){
224
        if (productList != null) {
1950 rajveer 225
            snippets = new LinkedHashMap<String, String>();
1778 rajveer 226
            for (String productId : productList) {
227
                try {
228
                    snippets.put(productId, FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "CompareProductSnippet.html"));
229
                } catch (IOException e) {
230
                    log.error(e);
231
                }
232
            }
233
 
234
        }
235
        return snippets;
236
    }
237
 
1950 rajveer 238
    public List<String> getProductList(){
239
        return this.productList;
240
    }
241
 
1778 rajveer 242
    public List<String> getProductNames(){
243
        return this.productNames;
244
    }
1950 rajveer 245
 
246
    public Map<String, String> getSlideNameImportanceMap(){
247
        return this.slideNameImportanceMap;
248
    }
249
 
250
    public Map<String, Map<String, Double>> getProductSlideScores(){
251
        return this.productSlideScores;
252
    }
253
 
254
    public List<String> getImportances(){
255
        return this.importances;
256
    }
1821 rajveer 257
}