Projekt

Obecné

Profil

Stáhnout (26.9 KB) Statistiky
| Větev: | Revize:
1
package vldc.aswi.utils;
2

    
3
import vldc.aswi.model.table.*;
4
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow;
5
import vldc.aswi.model.table.contingencyTable.ContingencyTableRowCell;
6

    
7
import java.text.DecimalFormat;
8
import java.util.ArrayList;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.concurrent.TimeUnit;
12

    
13
/**
14
 * Class for converting classes.
15
 */
16
public class Converter {
17

    
18
    private final Map<String, TableColumn> tableColumns;
19
    private final List<NameUserName> rowNames;
20
    private final List<NameUserName> columnNames;
21
    private final List<ValueFunction> valueFunctions;
22

    
23
    private List<Node> columnNodes;
24
    private final DecimalFormat decimalFormat = new DecimalFormat("0.###");
25

    
26
    public Converter(Map<String, TableColumn> tableColumns, List<NameUserName> rowNames, List<NameUserName> columnNames,
27
                     List<ValueFunction> valueFunctions) {
28
        this.tableColumns = tableColumns;
29
        this.rowNames = rowNames;
30
        this.columnNames = columnNames;
31
        this.valueFunctions = valueFunctions;
32
    }
33

    
34
    /**
35
     * Convert Map of TableColumns into list of ContingencyTableRow.
36
     * @return List of ContingencyTableRow.
37
     */
38
    public List<ContingencyTableRow> convertTableColumnsToContingencyTableRows() {
39
        long startTime = System.nanoTime();
40

    
41
        long startTime1 = System.nanoTime();
42
        columnNodes = new ArrayList<>();
43
        List<List<String>> values = new ArrayList<>();
44
        for(NameUserName nameUserName : columnNames) {
45
            TableColumn column = tableColumns.get(nameUserName.getName());
46
            values.add(getOriginalValuesFromList(column.getValues()));
47
        }
48

    
49
        Node startNode = new Node();
50
        generateColumns(0, values, columnNodes, startNode);
51

    
52
        long stopTime1 = System.nanoTime();
53
        System.out.println("Colums list:" + TimeUnit.MILLISECONDS.convert((stopTime1 - startTime1), TimeUnit.NANOSECONDS));
54

    
55
        List<ContingencyTableRow> contingencyTableRows = new ArrayList<>();
56

    
57
        long startTime2 = System.nanoTime();
58
        List<ContingencyTableRow> dataRows = createDataRows();
59
        long stopTime2 = System.nanoTime();
60
        System.out.println("Rows:" + TimeUnit.MILLISECONDS.convert((stopTime2 - startTime2), TimeUnit.NANOSECONDS));
61

    
62
        long startTime3 = System.nanoTime();
63
        for (ContingencyTableRow dataRow : dataRows) {
64
            for (int i = 0; i < columnNodes.size(); i++) {
65
                if (!columnNodes.get(i).isUsable()) {
66
                    dataRow.getCells().remove(i + 1);
67
                }
68
            }
69
        }
70

    
71
        columnNodes.removeIf(node -> !node.isUsable());
72
        long stopTime3 = System.nanoTime();
73
        System.out.println("Columns remove:" + TimeUnit.MILLISECONDS.convert((stopTime3 - startTime3), TimeUnit.NANOSECONDS));
74

    
75
        long startTime4 = System.nanoTime();
76
        List<ContingencyTableRow> headerRows = createHeaderOnlyRows();
77
        long stopTime4 = System.nanoTime();
78
        System.out.println("Headers:" + TimeUnit.MILLISECONDS.convert((stopTime4 - startTime4), TimeUnit.NANOSECONDS));
79

    
80
        contingencyTableRows.addAll(headerRows);
81
        contingencyTableRows.addAll(dataRows);
82

    
83
        long stopTime = System.nanoTime();
84
        System.out.println("Converter:" + TimeUnit.SECONDS.convert((stopTime - startTime), TimeUnit.NANOSECONDS) + "s");
85

    
86
        return contingencyTableRows;
87
    }
88

    
89
    /**
90
     * Generates header rows.
91
     * @return list of contingency table rows
92
     */
93
    private List<ContingencyTableRow> createHeaderOnlyRows() {
94
        List<ContingencyTableRow> headerRows = new ArrayList<>();
95

    
96
        headerRows.add(new ContingencyTableRow(true, 0));
97

    
98
        for (int i = 0; i < columnNames.size(); i++) {
99
            headerRows.add(new ContingencyTableRow(true, 0));
100
        }
101

    
102
        if (valueFunctions.get(0).getFunction() != null && valueFunctions.get(0).getFunction().equals("NIC") &&
103
                columnNames.size() == 0) {
104
            headerRows.get(0).addTableRowCell(new ContingencyTableRowCell("Seznam", 1));
105

    
106
            return headerRows;
107
        }
108

    
109
        for (int i = 0; i < headerRows.size(); i++) {
110
            ContingencyTableRow headerRow = headerRows.get(i);
111

    
112
            if (i == 0) {
113
                headerRow.addTableRowCell(new ContingencyTableRowCell("Tabulka", 1));
114
            }
115
            else {
116
                headerRow.addTableRowCell(new ContingencyTableRowCell("", 1));
117
            }
118
        }
119

    
120

    
121
        List<String> prevValues = new ArrayList<>();
122
        int prevSize = 0;
123
        for (ValueFunction valueFunction : valueFunctions) {
124
            headerRows.get(0).addTableRowCell(new ContingencyTableRowCell(valueFunction.getFunction() + " z " + valueFunction.getName(), 0));
125
            for (Node columnNode : columnNodes) {
126
                for (int i = 0; i < columnNode.getValues().size(); i++) {
127
                    String value = columnNode.getValues().get(i);
128
                    if (!prevValues.contains(value)) {
129
                        prevValues.add(value);
130
                        headerRows.get(i + 1).addTableRowCell(new ContingencyTableRowCell(value, 1));
131
                        headerRows.get(0).getCells().get(headerRows.get(0).getCells().size() - 1).setColSpan(headerRows.get(0).getCells().get(headerRows.get(0).getCells().size() - 1).getColSpan() + 1);
132
                    } else if (columnNode.getValues().size() >= prevSize) {
133
                        headerRows.get(i + 1).getCells().get(headerRows.get(i + 1).getCells().size() - 1).setColSpan(headerRows.get(i + 1).getCells().get(headerRows.get(i + 1).getCells().size() - 1).getColSpan() + 1);
134
                    }
135
                }
136

    
137
                if (columnNode.getValues().size() < prevSize) {
138
                    for (int s = prevSize; s < headerRows.size(); s++) {
139
                        headerRows.get(s).addTableRowCell(new ContingencyTableRowCell("", 1));
140
                    }
141
                    for (int s = 1; s < columnNode.getValues().size(); s++) {
142
                        headerRows.get(s).getCells().get(headerRows.get(s).getCells().size() - 1).setColSpan(headerRows.get(s).getCells().get(headerRows.get(s).getCells().size() - 1).getColSpan() + 1);
143
                    }
144
                    headerRows.get(columnNode.getValues().size()).addTableRowCell(new ContingencyTableRowCell(columnNode.getValues().get(columnNode.getValues().size() - 1) + " Celkem", 1));
145
                }
146

    
147
                prevSize = columnNode.getValues().size();
148

    
149
                if (!prevValues.isEmpty()) {
150
                    prevValues.remove(prevValues.size() - 1);
151
                }
152
            }
153
        }
154

    
155
        for (ValueFunction valueFunction : valueFunctions) {
156
            for (int i = 1; i < headerRows.size(); i++) {
157
                headerRows.get(i).addTableRowCell(new ContingencyTableRowCell("", 1));
158
            }
159
            headerRows.get(0).addTableRowCell(new ContingencyTableRowCell("Celkový " + valueFunction.getFunction() +
160
                    " z " + valueFunction.getName(), 1));
161
        }
162

    
163
        return headerRows;
164
    }
165

    
166
    /**
167
     * Gets original values of a given list.
168
     * @param list list from which original values should be taken
169
     * @return list of original values
170
     */
171
    private List<String> getOriginalValuesFromList(List<String> list) {
172
        List<String> origList = new ArrayList<>();
173

    
174
        for (String string : list) {
175
            if (origList.stream().noneMatch(s -> s.equals(string))) {
176
                origList.add(string);
177
            }
178
        }
179

    
180
        return origList;
181
    }
182

    
183
    /**
184
     * Creates data rows.
185
     * @return list of contingency table rows
186
     */
187
    private List<ContingencyTableRow> createDataRows() {
188
        List<ContingencyTableRow> dataRows = new ArrayList<>();
189
        List<Node> rowNodes = new ArrayList<>();
190
        List<List<String>> values = new ArrayList<>();
191
        for(NameUserName nameUserName : rowNames) {
192
            TableColumn column = tableColumns.get(nameUserName.getName());
193
            values.add(getOriginalValuesFromList(column.getValues()));
194
        }
195

    
196
        long startTime = System.nanoTime();
197
        Node node = new Node();
198
        generateRows(dataRows, 0, values, rowNodes, node);
199
        long stopTime = System.nanoTime();
200
        System.out.println("Rows List:" + TimeUnit.MILLISECONDS.convert((stopTime - startTime), TimeUnit.NANOSECONDS));
201

    
202
        List<ContingencyTableRow> rowsToBeRemoved = new ArrayList<>();
203
        for (int i = 0; i < dataRows.size(); i++) {
204
            if (!fillRowWithData(dataRows.get(i), rowNodes.get(i))) {
205
                rowsToBeRemoved.add(dataRows.get(i));
206
            }
207
        }
208

    
209
        dataRows.removeAll(rowsToBeRemoved);
210

    
211
        if (valueFunctions.get(0).getFunction() != null && (!valueFunctions.get(0).getFunction().equals("NIC") ||
212
                (valueFunctions.get(0).getFunction().equals("NIC") && columnNames.size() > 0))) {
213
            dataRows.add(createFinalRow());
214
        }
215

    
216
        return dataRows;
217
    }
218

    
219
    /**
220
     * Recursively generates all possible row combinations, sorted as in contingency table.
221
     * @param rows list of sorted contingency table rows
222
     * @param index index in the values list
223
     * @param values list of lists of values of each query row
224
     * @param rowNodes list of row nodes
225
     * @param prevNode parent node of the nodes
226
     */
227
    private void generateRows(List<ContingencyTableRow> rows, int index, List<List<String>> values, List<Node> rowNodes,
228
                              Node prevNode) {
229
        if (index < values.size()) {
230
            List<String> list = values.get(index);
231
            for (String s : list) {
232
                StringBuilder levelString = new StringBuilder();
233
                for (int i = 0; i < index; i++) {
234
                    levelString.append("      ");
235
                }
236
                levelString.append(s);
237
                ContingencyTableRow row = new ContingencyTableRow(false, index);
238
                row.addTableRowCell(new ContingencyTableRowCell(levelString.toString(), 1));
239
                rows.add(row);
240
                Node node = new Node(prevNode);
241
                node.getValues().add(s);
242
                rowNodes.add(node);
243
                int newIndex = index + 1;
244
                generateRows(rows, newIndex, values, rowNodes, node);
245
            }
246
        }
247
    }
248

    
249
    /**
250
     * Recursively generates all possible column combinations, sorted as in contingency table.
251
     * @param index index in the values list
252
     * @param values list of lists of values of each query row
253
     * @param columnNodes list of column nodes
254
     * @param prevNode parent node of the nodes
255
     */
256
    private void generateColumns(int index, List<List<String>> values, List<Node> columnNodes, Node prevNode) {
257
        if (index < values.size()) {
258
            List<String> list = values.get(index);
259
            for (String s : list) {
260
                Node node = new Node(prevNode);
261
                node.getValues().add(s);
262
                if (checkIfColumnIsUsable(node.getValues())) {
263
                    int newIndex = index + 1;
264
                    generateColumns(newIndex, values, columnNodes, node);
265
                    columnNodes.add(node);
266
                }
267
            }
268
        }
269
    }
270

    
271
    /**
272
     * Fills each data row with data.
273
     * @param row row to ne filled with data
274
     * @param node node of the row
275
     */
276
    private boolean fillRowWithData(ContingencyTableRow row, Node node) {
277
        if (valueFunctions.size() == 0) {
278
            return true;
279
        }
280

    
281
        boolean isSingleValueRow = false;
282

    
283
        if (node.getValues().size() <= 1) {
284
            isSingleValueRow = true;
285
        }
286

    
287
        boolean isRowUsable = false;
288
        if (columnNodes.size() == 0) {
289
            columnNodes.add(new Node());
290
        }
291

    
292
        List<Integer> usableIDs = getUsableIDs(node);
293

    
294
        if (valueFunctions.get(0).getFunction() != null && valueFunctions.get(0).getFunction().equals("NIC") &&
295
                !usableIDs.isEmpty()) {
296
            return true;
297
        }
298

    
299
        DoubleWrapper[] totals = new DoubleWrapper[valueFunctions.size()];
300
        initializeWrapperField(totals);
301

    
302
        DoubleWrapper[] totalsAvgSum = new DoubleWrapper[valueFunctions.size()];
303
        initializeWrapperField(totalsAvgSum);
304

    
305
        boolean[] isFilledValueCells = new boolean[valueFunctions.size()];
306
        //hodnota
307
        for (int v = 0; v < valueFunctions.size(); v++) {
308
            if (valueFunctions.get(v).getFunction() != null && (valueFunctions.get(v).getFunction().equals("") ||
309
                    valueFunctions.get(v).getFunction().equals("NIC"))) {
310
                isRowUsable = true;
311
                break;
312
            }
313
            BooleanWrapper isFirstMinMaxTotal = new BooleanWrapper(true);
314
            ValueFunction valueFunction = valueFunctions.get(v);
315
            //sloupec v kont. tabulce
316
            for (Node columnNode : columnNodes) {
317
                DoubleWrapper result = new DoubleWrapper(0);
318
                DoubleWrapper resultAvgSum = new DoubleWrapper(0);
319
                BooleanWrapper isFirstMinMax = new BooleanWrapper(true);
320
                boolean isFilledCell = false;
321
                // radek v orig. tabulce
322
                for (Integer usableID : usableIDs) {
323
                    boolean isUsable = true;
324
                    int j;
325
                    // zvoleny sloupec ze sloupcu, ziska hodnoty z pouzitelne radky danych sloupcu originalni tabulky a
326
                    // pokud se vsechny rovnaji hodnotam, ktere jsou ulozeny v hodnotach uzlu na pozici vybraneho
327
                    // sloupce, je kommbinace pouzitelna k vypoctum
328
                    for (j = 0; j < columnNode.getValues().size(); j++) {
329
                        if (!tableColumns.get(columnNames.get(j).getName()).getValues().get(usableID).equals(columnNode.getValues().get(j))) {
330
                            isUsable = false;
331
                            break;
332
                        }
333
                    }
334

    
335
                    // zvoleny sloupec z hodnot
336
                    if (tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID) == null) {
337
                        isUsable = false;
338
                    }
339

    
340
                    if (isUsable) { // pokud lze hodnotu pouzit, prunik vsech hodnot sedi (ma nejakou hodnotu)
341
                        columnNode.setUsable(true);
342
                        isRowUsable = true;
343
                        isFilledCell = true;
344
                        computeValues(valueFunction, result, resultAvgSum, columnNode, isFirstMinMax,
345
                                usableID, false, isSingleValueRow);
346
                        if (j <= 1) {
347
                            isFilledValueCells[v] = true;
348
                            computeValues(valueFunction, totals[v], totalsAvgSum[v], null, isFirstMinMaxTotal,
349
                                    usableID, true, isSingleValueRow);
350
                        }
351
                    }
352
                }
353
                if (!isFilledCell) {
354
                    row.addTableRowCell(new ContingencyTableRowCell("", 0));
355
                }
356
                else {
357
                    row.addTableRowCell(generateFilledCell(valueFunction.getFunction(), result.getValue(), result.getValue(), result.getValue(),
358
                            result.getValue(), resultAvgSum.getValue(), result.getValue()));
359
                }
360
            }
361
        }
362
        for (int i = 0; i < valueFunctions.size(); i++) {
363
            if (valueFunctions.get(i).getFunction() != null && valueFunctions.get(i).getFunction().equals("NIC")) {
364
                break;
365
            }
366
            if (!isFilledValueCells[i]) {
367
                row.addTableRowCell(new ContingencyTableRowCell("", 0));
368
            }
369
            else {
370
                row.addTableRowCell(generateFilledCell(valueFunctions.get(i).getFunction(), totals[i].getValue(), totals[i].getValue(),
371
                        totals[i].getValue(), totals[i].getValue(), totalsAvgSum[i].getValue(), totals[i].getValue()));
372
            }
373
        }
374

    
375
        return isRowUsable;
376
    }
377

    
378
    /**
379
     * Gets a list of IDs of the query rows that fit the contingency table row settings.
380
     * @param node node of the row
381
     * @return list of usable IDs
382
     */
383
    private List<Integer> getUsableIDs(Node node) {
384
        List<Integer> ids = new ArrayList<>();
385

    
386
        for (int i = 0; i < tableColumns.get(rowNames.get(0).getName()).getValues().size(); i++) {
387
            boolean isUsable = true;
388
            for (int j = 0; j < node.getValues().size(); j++) {
389
                if (!tableColumns.get(rowNames.get(j).getName()).getValues().get(i).equals(node.getValues().get(j))) {
390
                    isUsable = false;
391
                    break;
392
                }
393
            }
394
            if (isUsable) {
395
                ids.add(i);
396
            }
397
        }
398

    
399
        return ids;
400
    }
401

    
402
    private boolean checkIfColumnIsUsable(List<String> columnNodeValues) {
403
        for (int i = 0; i < tableColumns.get(columnNames.get(0).getName()).getValues().size(); i++) {
404
            boolean isUsable = true;
405
            for (int j = 0; j < columnNodeValues.size(); j++) {
406
                if (!tableColumns.get(columnNames.get(j).getName()).getValues().get(i).equals(columnNodeValues.get(j))) {
407
                    isUsable = false;
408
                    break;
409
                }
410
            }
411

    
412
            if (isUsable) {
413
                return true;
414
            }
415
        }
416

    
417
        return false;
418
    }
419

    
420
    private void computeValues(ValueFunction valueFunction, DoubleWrapper result, DoubleWrapper resultAvgSum, Node columnNode,
421
                               BooleanWrapper isFirstMinMax, int usableID, boolean isValueTotal, boolean isSingleValueRow) {
422
        switch (valueFunction.getFunction()) {
423
            case "COUNT":
424
                count(valueFunction, result, columnNode, isValueTotal, isSingleValueRow);
425
                break;
426
            case "SUM":
427
                sum(valueFunction, result, columnNode, usableID, isValueTotal, isSingleValueRow);
428
                break;
429
            case "MIN":
430
                min(valueFunction, result, columnNode, isFirstMinMax, usableID, isValueTotal, isSingleValueRow);
431
                break;
432
            case "MAX":
433
                max(valueFunction, result, columnNode, isFirstMinMax, usableID, isValueTotal, isSingleValueRow);
434
                break;
435
            case "AVG":
436
                average(valueFunction, result, resultAvgSum, columnNode, usableID, isValueTotal, isSingleValueRow);
437
                break;
438
        }
439
    }
440

    
441
    private ContingencyTableRowCell generateFilledCell(String function, double totalResult, double totalResultSum,
442
                                                       double totalMin, double totalMax, double totalResultAvgSum,
443
                                                       double totalResultAvg) {
444
        ContingencyTableRowCell cell = null;
445
        switch (function) {
446
            case "COUNT":
447
                cell = new ContingencyTableRowCell(decimalFormat.format(totalResult), 1);
448
                break;
449
            case "SUM":
450
                cell = new ContingencyTableRowCell(decimalFormat.format(totalResultSum), 1);
451
                break;
452
            case "MIN":
453
                cell = new ContingencyTableRowCell(decimalFormat.format(totalMin), 1);
454
                break;
455
            case "MAX":
456
                cell = new ContingencyTableRowCell(decimalFormat.format(totalMax), 1);
457
                break;
458
            case "AVG":
459
                if (totalResultAvg == 0) {
460
                    cell = new ContingencyTableRowCell("!!Dělení nulou!!", 0);
461
                } else {
462
                    cell = new ContingencyTableRowCell(decimalFormat.format(totalResultAvgSum / totalResultAvg), 0);
463
                }
464
                break;
465
        }
466

    
467
        return cell;
468
    }
469

    
470
    private void initializeWrapperField(DoubleWrapper[] field) {
471
        for (int i = 0; i < field.length; i++) {
472
            field[i] = new DoubleWrapper(0);
473
        }
474
    }
475

    
476
    private void count(ValueFunction valueFunction, DoubleWrapper result, Node columnNode, boolean isValueTotal,
477
                       boolean isSingleValueRow) {
478
        result.setValue(result.getValue() + 1);
479
        if (columnNode != null && isSingleValueRow) {
480
            columnNode.setTotalResult(columnNode.getTotalResult() + 1);
481
        }
482
        if (isValueTotal && isSingleValueRow) {
483
            valueFunction.setTotalResult(valueFunction.getTotalResult() + 1);
484
        }
485
    }
486

    
487
    private void sum(ValueFunction valueFunction, DoubleWrapper result, Node columnNode, int usableID, boolean isValueTotal,
488
                     boolean isSingleValueRow) {
489
        if (valueFunction.getType().equals("Číslo")) {
490
            result.setValue(result.getValue() + Double.parseDouble(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID)));
491
            if (columnNode != null && isSingleValueRow) {
492
                columnNode.setTotalResultSum(columnNode.getTotalResultSum() + Double.parseDouble(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID)));
493
            }
494
            if (isValueTotal && isSingleValueRow) {
495
                valueFunction.setTotalResultSum(valueFunction.getTotalResultSum() + Double.parseDouble(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID)));
496
            }
497
        }
498
    }
499

    
500
    private void min(ValueFunction valueFunction, DoubleWrapper result, Node columnNode, BooleanWrapper isFirstMinMax,
501
                     int usableID, boolean isValueTotal, boolean isSingleValueRow) {
502
        if (valueFunction.getType().equals("Číslo")) {
503
            if (isFirstMinMax.isValue()) {
504
                isFirstMinMax.setValue(false);
505
                result.setValue(Double.parseDouble(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID)));
506
            }
507
            else {
508
                result.setValue(Math.min(result.getValue(), Integer.parseInt(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID))));
509
            }
510
            if (columnNode != null && isSingleValueRow) {
511
                if (columnNode.isFirstMin()) {
512
                    columnNode.setFirstMin(false);
513
                    columnNode.setTotalMin(Double.parseDouble(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID)));
514
                } else {
515
                    columnNode.setTotalMin(Math.min(columnNode.getTotalMin(), Integer.parseInt(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID))));
516
                }
517
            }
518
            if (isValueTotal && isSingleValueRow) {
519
                if (valueFunction.isFirstMin()) {
520
                    valueFunction.setFirstMin(false);
521
                    valueFunction.setTotalMin(Double.parseDouble(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID)));
522
                } else {
523
                    valueFunction.setTotalMin(Math.min(valueFunction.getTotalMin(), Integer.parseInt(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID))));
524
                }
525
            }
526
        }
527
    }
528

    
529
    private void max(ValueFunction valueFunction, DoubleWrapper result, Node columnNode, BooleanWrapper isFirstMinMax,
530
                     int usableID, boolean isValueTotal, boolean isSingleValueRow) {
531
        if (valueFunction.getType().equals("Číslo")) {
532
            if (isFirstMinMax.isValue()) {
533
                isFirstMinMax.setValue(false);
534
                result.setValue(Double.parseDouble(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID)));
535
            }
536
            else {
537
                result.setValue(Math.max(result.getValue(), Integer.parseInt(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID))));
538
            }
539
            if (columnNode != null && isSingleValueRow) {
540
                if (columnNode.isFirstMax()) {
541
                    columnNode.setFirstMax(false);
542
                    columnNode.setTotalMax(Double.parseDouble(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID)));
543
                } else {
544
                    columnNode.setTotalMax(Math.max(columnNode.getTotalMax(), Integer.parseInt(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID))));
545
                }
546
            }
547
            if (isValueTotal && isSingleValueRow) {
548
                if (valueFunction.isFirstMax()) {
549
                    valueFunction.setFirstMax(false);
550
                    valueFunction.setTotalMax(Double.parseDouble(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID)));
551
                } else {
552
                    valueFunction.setTotalMax(Math.max(valueFunction.getTotalMax(), Integer.parseInt(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID))));
553
                }
554
            }
555
        }
556
    }
557

    
558
    private void average(ValueFunction valueFunction, DoubleWrapper result, DoubleWrapper resultAvgSum, Node columnNode,
559
                         int usableID, boolean isValueTotal, boolean isSingleValueRow) {
560
        if (valueFunction.getType().equals("Číslo")) {
561
            result.setValue(result.getValue() + 1);
562
            resultAvgSum.setValue(resultAvgSum.getValue() + Double.parseDouble(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID)));
563
            if (columnNode != null && isSingleValueRow) {
564
                columnNode.setTotalResultAvg(columnNode.getTotalResultAvg() + 1);
565
                columnNode.setTotalResultAvgSum(columnNode.getTotalResultAvgSum() + Double.parseDouble(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID)));
566
            }
567
            if (isValueTotal && isSingleValueRow) {
568
                valueFunction.setTotalResultAvg(valueFunction.getTotalResultAvg() + 1);
569
                valueFunction.setTotalResultAvgSum(valueFunction.getTotalResultAvgSum() + Double.parseDouble(tableColumns.get(valueFunction.getNameOfSelect()).getValues().get(usableID)));
570
            }
571
        }
572
    }
573

    
574
    private ContingencyTableRow createFinalRow() {
575
        ContingencyTableRow finalRow = new ContingencyTableRow(false, 0);
576
        finalRow.addTableRowCell(new ContingencyTableRowCell("Celkem", 1));
577

    
578
        if (valueFunctions.get(0).getFunction() != null && valueFunctions.get(0).getFunction().equals("NIC")) {
579
            return finalRow;
580
        }
581

    
582
        List<ContingencyTableRowCell> valueCells = new ArrayList<>();
583
        for (ValueFunction valueFunction : valueFunctions) {
584
            for (Node columnNode : columnNodes) {
585
                    finalRow.addTableRowCell(generateFilledCell(valueFunction.getFunction(), columnNode.getTotalResult(), columnNode.getTotalResultSum(),
586
                            columnNode.getTotalMin(), columnNode.getTotalMax(), columnNode.getTotalResultAvgSum(),
587
                            columnNode.getTotalResultAvg()));
588
            }
589
            valueCells.add(generateFilledCell(valueFunction.getFunction(), valueFunction.getTotalResult(), valueFunction.getTotalResultSum(),
590
                    valueFunction.getTotalMin(), valueFunction.getTotalMax(), valueFunction.getTotalResultAvgSum(),
591
                    valueFunction.getTotalResultAvg()));
592
        }
593

    
594
        finalRow.addTableRowCells(valueCells);
595

    
596
        return finalRow;
597
    }
598
}
(2-2/5)