Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 403c9955

Přidáno uživatelem Michal Linha před více než 4 roky(ů)

re #7885 basic contingency table generation (not completely finished)

Zobrazit rozdíly:

src/main/java/vldc/aswi/model/table/NameUserName.java
1
package vldc.aswi.model.table;
2

  
3
public class NameUserName {
4

  
5
    private String name;
6
    private String userName;
7

  
8
    public NameUserName(String name, String userName) {
9
        this.name = name;
10
        this.userName = userName;
11
    }
12

  
13
    public String getName() {
14
        return name;
15
    }
16

  
17
    public void setName(String name) {
18
        this.name = name;
19
    }
20

  
21
    public String getUserName() {
22
        return userName;
23
    }
24

  
25
    public void setUserName(String userName) {
26
        this.userName = userName;
27
    }
28
}
src/main/java/vldc/aswi/model/table/Node.java
1
package vldc.aswi.model.table;
2

  
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.List;
6

  
7
public class Node {
8

  
9
    private List<String> values;
10

  
11
    public Node() {
12
        this.values = new ArrayList<>();
13
    }
14

  
15
    public Node(Node node) {
16
        this.values = new ArrayList<>(node.values);
17
    }
18

  
19
    public List<String> getValues() {
20
        return values;
21
    }
22

  
23
    public void setValues(List<String> values) {
24
        this.values = values;
25
    }
26
}
src/main/java/vldc/aswi/model/table/ValueFunction.java
1
package vldc.aswi.model.table;
2

  
3
public class ValueFunction {
4

  
5
    private String value;
6
    private String function;
7

  
8
    public ValueFunction(String value, String function) {
9
        this.value = value;
10
        this.function = function;
11
    }
12

  
13
    public String getValue() {
14
        return value;
15
    }
16

  
17
    public void setValue(String value) {
18
        this.value = value;
19
    }
20

  
21
    public String getFunction() {
22
        return function;
23
    }
24

  
25
    public void setFunction(String function) {
26
        this.function = function;
27
    }
28
}
src/main/java/vldc/aswi/service/SqlQueryManagerImpl.java
4 4
import org.springframework.beans.factory.annotation.Autowired;
5 5
import org.springframework.stereotype.Service;
6 6
import vldc.aswi.database.DatabaseInterface;
7
import vldc.aswi.model.table.NameUserName;
7 8
import vldc.aswi.model.table.TableColumn;
9
import vldc.aswi.model.table.ValueFunction;
8 10
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow;
9 11
import vldc.aswi.utils.Converter;
10 12

  
11 13
import javax.sql.DataSource;
14
import java.util.ArrayList;
12 15
import java.util.List;
13 16
import java.util.Map;
14 17

  
......
31 34
    @Override
32 35
    public List<ContingencyTableRow> getContingencyTableRow(String sqlQuery) {
33 36
        Map<String, TableColumn> tableColumns = this.databaseInterface.executeQueryAndReturnTableColumns(sqlQuery);
34
        return Converter.convertTableColumnsToContingencyTableRows(tableColumns);
37
        List<NameUserName> rowNames = new ArrayList<>();
38
        rowNames.add(new NameUserName("PROGRAM_STUDIA", "d"));
39
        rowNames.add(new NameUserName("TYP_STUDIA", "d"));
40

  
41
        List<NameUserName> columnNames = new ArrayList<>();
42
        columnNames.add(new NameUserName("FAKULTA_STUDIA", "d"));
43
        columnNames.add(new NameUserName("FORMA_STUDIA", "d"));
44

  
45
        List<ValueFunction> valueFunctions = new ArrayList<>();
46
        valueFunctions.add(new ValueFunction("VEK", "d"));
47
        Converter c = new Converter();
48
        return c.convertTableColumnsToContingencyTableRows(tableColumns, rowNames, columnNames, valueFunctions);
35 49
    }
36 50

  
37 51
    /**
src/main/java/vldc/aswi/utils/Converter.java
1 1
package vldc.aswi.utils;
2 2

  
3
import vldc.aswi.model.table.NameUserName;
4
import vldc.aswi.model.table.Node;
3 5
import vldc.aswi.model.table.TableColumn;
6
import vldc.aswi.model.table.ValueFunction;
4 7
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow;
5 8
import vldc.aswi.model.table.contingencyTable.ContingencyTableRowCell;
6 9

  
......
13 16
 */
14 17
public class Converter {
15 18

  
19
    private List<Node> rowNodes;
20
    private List<Node> columnNodes;
21

  
16 22
    /**
17 23
     * Convert Map of TableColumns into list of ContingencyTableRow.
18 24
     * @param tableColumns - Map of TableColumn.
19 25
     * @return List of ContingencyTableRow.
20 26
     */
21
    public static List<ContingencyTableRow> convertTableColumnsToContingencyTableRows(Map<String, TableColumn> tableColumns) {
27
    public List<ContingencyTableRow> convertTableColumnsToContingencyTableRows(Map<String, TableColumn> tableColumns,
28
                                                                                      List<NameUserName> rowNames,
29
                                                                                      List<NameUserName> columnNames,
30
                                                                                      List<ValueFunction> valueFunctions) {
31
        /*
22 32
        List<ContingencyTableRow> contingencyTableRows = new ArrayList<>();
23 33

  
24 34
        // Create header row with column names.
......
50 60
            }
51 61
        }
52 62

  
63
          */
64
        List<ContingencyTableRow> contingencyTableRows = new ArrayList<>();
65

  
66
        List<ContingencyTableRow> headerRows = createHeaderOnlyRows(tableColumns, columnNames);
67
        List<ContingencyTableRow> dataRows = createDataRows(tableColumns, columnNames, rowNames, valueFunctions);
68

  
69
        contingencyTableRows.addAll(headerRows);
70
        contingencyTableRows.addAll(dataRows);
71

  
53 72
        return contingencyTableRows;
54 73
    }
74

  
75
    private List<ContingencyTableRow> createHeaderOnlyRows(Map<String, TableColumn> tableColumns, List<NameUserName> columnNames) {
76
        List<List<String>> values = new ArrayList<>();
77
        columnNodes = new ArrayList<>();
78
        List<ContingencyTableRow> headerRows = new ArrayList<>();
79
        for(NameUserName nameUserName : columnNames) {
80
            TableColumn column = tableColumns.get(nameUserName.getName());
81
            values.add(getOriginalValuesFromList(column.getValues()));
82
            headerRows.add(new ContingencyTableRow(true, 0));
83
        }
84

  
85
        Node node = new Node();
86
        generateColumns(0, values, columnNodes, node);
87

  
88
        for (int i = 0; i < values.size(); i++) {
89
            int colSpan = 1;
90
            for (int j = values.size() - 1; j > i; j--) {
91
                if (values.size() - 1 == j) {
92
                    colSpan = values.get(j).size();
93
                }
94
                else {
95
                    colSpan = (colSpan + 1) * values.get(j).size();
96
                }
97
            }
98
            int loopCount = 1;
99
            for (int k = 0; k < i; k++) {
100
                loopCount = loopCount * values.get(k).size();
101
            }
102
            headerRows.get(i).addTableRowCell(new ContingencyTableRowCell("", 1));
103
            for (int k = 0; k < loopCount; k++) {
104
                for (String value : values.get(i)) {
105
                    headerRows.get(i).addTableRowCell(new ContingencyTableRowCell(value, colSpan));
106
                    if (i != values.size() - 1) {
107
                        headerRows.get(i).addTableRowCell(new ContingencyTableRowCell(value + " Celkem", 1));
108
                    }
109
                }
110

  
111
                for (int l = 0; l < i; l++) {
112
                    headerRows.get(i).addTableRowCell(new ContingencyTableRowCell("", 1));
113
                }
114
            }
115
            if (i == 0) {
116
                headerRows.get(i).addTableRowCell(new ContingencyTableRowCell("Celkový počet", 1));
117
            }
118
        }
119

  
120
        return headerRows;
121
    }
122

  
123
    private List<String> getOriginalValuesFromList(List<String> list) {
124
        List<String> origList = new ArrayList<>();
125

  
126
        for (String string : list) {
127
            if (origList.stream().noneMatch(s -> s.equals(string))) {
128
                origList.add(string);
129
            }
130
        }
131

  
132
        return origList;
133
    }
134

  
135
    private List<ContingencyTableRow> createDataRows(Map<String, TableColumn> tableColumns, List<NameUserName> columnNames,
136
                                                     List<NameUserName> rowNames, List<ValueFunction> valueFunctions) {
137
        List<ContingencyTableRow> dataRows = new ArrayList<>();
138
        rowNodes = new ArrayList<>();
139
        List<List<String>> values = new ArrayList<>();
140
        for(NameUserName nameUserName : rowNames) {
141
            TableColumn column = tableColumns.get(nameUserName.getName());
142
            values.add(getOriginalValuesFromList(column.getValues()));
143
        }
144

  
145
        Node node = new Node();
146
        generateRows(dataRows, 0, values, rowNodes, node);
147

  
148
        for (int i = 0; i < dataRows.size(); i++) {
149
            fillRowWithCountData(dataRows.get(i), tableColumns, rowNodes.get(i), rowNames, columnNames, valueFunctions);
150
        }
151

  
152
        return dataRows;
153
    }
154

  
155
    private void generateRows(List<ContingencyTableRow> rows, int index, List<List<String>> values, List<Node> rowNodes,
156
                              Node prevNode) {
157
        if (index < values.size()) {
158
            List<String> list = values.get(index);
159
            for (String s : list) {
160
                StringBuilder levelString = new StringBuilder();
161
                for (int i = 0; i < index; i++) {
162
                    levelString.append("\t");
163
                }
164
                levelString.append(s);
165
                ContingencyTableRow row = new ContingencyTableRow(false, index);
166
                row.addTableRowCell(new ContingencyTableRowCell(levelString.toString(), 1));
167
                rows.add(row);
168
                Node node = new Node(prevNode);
169
                node.getValues().add(s);
170
                rowNodes.add(node);
171
                int newIndex = index + 1;
172
                generateRows(rows, newIndex, values, rowNodes, node);
173
            }
174
        }
175
    }
176

  
177
    private void generateColumns(int index, List<List<String>> values, List<Node> columnNodes, Node prevNode) {
178
        if (index < values.size()) {
179
            List<String> list = values.get(index);
180
            for (String s : list) {
181
                Node node = new Node(prevNode);
182
                node.getValues().add(s);
183
                int newIndex = index + 1;
184
                generateColumns(newIndex, values, columnNodes, node);
185
                columnNodes.add(node);
186
            }
187
        }
188
    }
189

  
190
    private void fillRowWithCountData(ContingencyTableRow row, Map<String, TableColumn> tableColumns, Node node,
191
                                      List<NameUserName> rowNames, List<NameUserName> columnNames, List<ValueFunction> valueFunctions) {
192
        //sloupec v kont. tabulce
193
        for (Node columnNode : columnNodes) {
194
            int count = 0;
195
            // radek v orig. tabulce
196
            for (int i = 0; i < tableColumns.get(rowNames.get(0).getName()).getValues().size(); i++) {
197
                boolean isUsable = true;
198
                //zvoleny sloupec z radku
199
                for (int j = 0; j < node.getValues().size(); j++) {
200
                    if (!tableColumns.get(rowNames.get(j).getName()).getValues().get(i).equals(node.getValues().get(j))) {
201
                        isUsable = false;
202
                        break;
203
                    }
204
                }
205
                // zvoleny sloupec ze sloupcu
206
                for (int j = 0; j < columnNode.getValues().size(); j++) {
207
                    if (!tableColumns.get(columnNames.get(j).getName()).getValues().get(i).equals(columnNode.getValues().get(j))) {
208
                        isUsable = false;
209
                        break;
210
                    }
211
                }
212
                // zvoleny sloupec z hodnot
213
                for (ValueFunction valueFunction : valueFunctions) {
214
                    if (tableColumns.get(valueFunction.getValue()).getValues().get(i) == null) {
215
                        isUsable = false;
216
                        break;
217
                    }
218
                }
219

  
220
                if (isUsable) {
221
                    count++;
222
                }
223
            }
224

  
225
            row.addTableRowCell(new ContingencyTableRowCell(Integer.toString(count), 0));
226
        }
227
    }
55 228
}
src/main/java/vldc/aswi/web/controller/ConfigurationController.java
17 17
import vldc.aswi.domain.Operator;
18 18
import vldc.aswi.domain.parameter.ParameterInConfiguration;
19 19
import vldc.aswi.service.ConfigurationManager;
20
import vldc.aswi.service.SqlQueryManager;
20 21
import vldc.aswi.utils.AuthControl;
21 22
import vldc.aswi.utils.Utils;
22 23

  
......
31 32
@Controller
32 33
public class ConfigurationController extends BasicController{
33 34

  
35
    /** Autowired sql query manager. */
36
    @Autowired
37
    private SqlQueryManager sqlQueryManager;
38

  
34 39
    /** Autowired configuration manager */
35 40
    @Autowired
36 41
    private ConfigurationManager configurationManager;
......
91 96
            return modelAndView;
92 97
        }
93 98

  
99

  
100

  
94 101
        Configuration configuration = configurationManager.saveConfiguration(newConfiguration, id);
95 102

  
103
        this.sqlQueryManager.getContingencyTableRow(configuration.getAssembly().getSQLQuery());
104

  
96 105
        initializeFields(configuration);
97 106

  
98 107
        modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());

Také k dispozici: Unified diff