Revize 417c1106
Přidáno uživatelem Vojtěch Danišík před asi 5 roky(ů)
src/main/java/vldc/aswi/database/DatabaseInterface.java | ||
---|---|---|
1 |
package vldc.aswi.database; |
|
2 |
|
|
3 |
import org.springframework.context.annotation.Bean; |
|
4 |
import org.springframework.dao.DataAccessException; |
|
5 |
import org.springframework.jdbc.core.JdbcTemplate; |
|
6 |
import org.springframework.jdbc.core.ResultSetExtractor; |
|
7 |
import org.springframework.jdbc.core.RowMapper; |
|
8 |
import org.springframework.stereotype.Component; |
|
9 |
import vldc.aswi.model.table.TableColumn; |
|
10 |
|
|
11 |
import java.sql.ResultSet; |
|
12 |
import java.sql.ResultSetMetaData; |
|
13 |
import java.sql.SQLException; |
|
14 |
import java.util.ArrayList; |
|
15 |
import java.util.HashMap; |
|
16 |
import java.util.List; |
|
17 |
import java.util.Map; |
|
18 |
|
|
19 |
@Component |
|
20 |
public class DatabaseInterface { |
|
21 |
|
|
22 |
private static JdbcTemplate jdbcTemplate = null; |
|
23 |
|
|
24 |
public DatabaseInterface(JdbcTemplate jdbcTemplate) { |
|
25 |
this.jdbcTemplate = jdbcTemplate; |
|
26 |
} |
|
27 |
|
|
28 |
public static Map<String, TableColumn> executeQueryAndReturnTableColumns(String sqlQuery) { |
|
29 |
return jdbcTemplate.query( |
|
30 |
sqlQuery, |
|
31 |
new ResultSetExtractor<Map<String, TableColumn>>() { |
|
32 |
@Override |
|
33 |
public Map<String, TableColumn> extractData(ResultSet resultSet) throws SQLException, DataAccessException { |
|
34 |
List<TableColumn> tableColumnsList = new ArrayList<>(); |
|
35 |
|
|
36 |
ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); |
|
37 |
int columnCount = resultSetMetaData.getColumnCount(); |
|
38 |
|
|
39 |
// The column count starts from 1. |
|
40 |
for (int i = 1; i <= columnCount; i++ ) { |
|
41 |
String columnName = resultSetMetaData.getColumnName(i); |
|
42 |
tableColumnsList.add(new TableColumn(columnName)); |
|
43 |
} |
|
44 |
|
|
45 |
while(resultSet.next()) { |
|
46 |
for (int i = 1; i <= columnCount; i++) { |
|
47 |
tableColumnsList.get(i - 1).addValue(resultSet.getString(i)); |
|
48 |
} |
|
49 |
} |
|
50 |
|
|
51 |
Map<String, TableColumn> tableColumnMap = new HashMap<>(); |
|
52 |
|
|
53 |
for (TableColumn column : tableColumnsList) { |
|
54 |
tableColumnMap.put(column.getName(), column); |
|
55 |
} |
|
56 |
|
|
57 |
return tableColumnMap; |
|
58 |
} |
|
59 |
} |
|
60 |
); |
|
61 |
} |
|
62 |
} |
src/main/java/vldc/aswi/service/SqlQueryManager.java | ||
---|---|---|
1 |
package vldc.aswi.service; |
|
2 |
|
|
3 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow; |
|
4 |
|
|
5 |
import java.util.List; |
|
6 |
|
|
7 |
public interface SqlQueryManager { |
|
8 |
|
|
9 |
List<ContingencyTableRow> getContingencyTableRow(String sql); |
|
10 |
} |
src/main/java/vldc/aswi/service/SqlQueryManagerImpl.java | ||
---|---|---|
1 |
package vldc.aswi.service; |
|
2 |
|
|
3 |
import lombok.extern.slf4j.Slf4j; |
|
4 |
import org.springframework.beans.factory.annotation.Autowired; |
|
5 |
import org.springframework.stereotype.Service; |
|
6 |
import vldc.aswi.database.DatabaseInterface; |
|
7 |
import vldc.aswi.model.table.TableColumn; |
|
8 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow; |
|
9 |
import vldc.aswi.utils.Converter; |
|
10 |
|
|
11 |
import javax.sql.DataSource; |
|
12 |
import java.util.List; |
|
13 |
import java.util.Map; |
|
14 |
|
|
15 |
@Service |
|
16 |
@Slf4j |
|
17 |
public class SqlQueryManagerImpl implements SqlQueryManager{ |
|
18 |
|
|
19 |
@Autowired |
|
20 |
private DataSource dataSource; |
|
21 |
|
|
22 |
@Autowired |
|
23 |
private DatabaseInterface databaseInterface; |
|
24 |
|
|
25 |
@Override |
|
26 |
public List<ContingencyTableRow> getContingencyTableRow(String sqlQuery) { |
|
27 |
Map<String, TableColumn> tableColumns = databaseInterface.executeQueryAndReturnTableColumns(sqlQuery); |
|
28 |
return Converter.convertTableColumnsToContingencyTableRows(tableColumns); |
|
29 |
} |
|
30 |
} |
src/main/java/vldc/aswi/utils/Converter.java | ||
---|---|---|
1 |
package vldc.aswi.utils; |
|
2 |
|
|
3 |
import vldc.aswi.model.table.TableColumn; |
|
4 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow; |
|
5 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRowCell; |
|
6 |
|
|
7 |
import java.util.ArrayList; |
|
8 |
import java.util.List; |
|
9 |
import java.util.Map; |
|
10 |
|
|
11 |
public class Converter { |
|
12 |
|
|
13 |
public static List<ContingencyTableRow> convertTableColumnsToContingencyTableRows(Map<String, TableColumn> tableColumns) { |
|
14 |
List<ContingencyTableRow> contingencyTableRows = new ArrayList<>(); |
|
15 |
|
|
16 |
ContingencyTableRow contingencyTableRowHeader = new ContingencyTableRow(true, 0); |
|
17 |
for (String columnName : tableColumns.keySet()) { |
|
18 |
contingencyTableRowHeader.addTableRowCell(new ContingencyTableRowCell(columnName, 0)); |
|
19 |
} |
|
20 |
contingencyTableRows.add(contingencyTableRowHeader); |
|
21 |
|
|
22 |
boolean listExpanded = false; |
|
23 |
|
|
24 |
for (TableColumn tableColumn : tableColumns.values()) { |
|
25 |
List<String> values = tableColumn.getValues(); |
|
26 |
|
|
27 |
if (!listExpanded) { |
|
28 |
for (int j = 0; j < values.size(); j++) { |
|
29 |
ContingencyTableRow contingencyTableRow = new ContingencyTableRow(false, 0); |
|
30 |
contingencyTableRows.add(contingencyTableRow); |
|
31 |
} |
|
32 |
listExpanded = true; |
|
33 |
} |
|
34 |
|
|
35 |
for (int j = 0; j < values.size(); j++) { |
|
36 |
ContingencyTableRowCell contingencyTableRowCell = new ContingencyTableRowCell(values.get(j), 0); |
|
37 |
contingencyTableRows.get(j + 1).addTableRowCell(contingencyTableRowCell); |
|
38 |
} |
|
39 |
} |
|
40 |
|
|
41 |
return contingencyTableRows; |
|
42 |
} |
|
43 |
} |
src/main/java/vldc/aswi/web/controller/IndexController.java | ||
---|---|---|
1 | 1 |
package vldc.aswi.web.controller; |
2 | 2 |
|
3 | 3 |
import org.springframework.beans.factory.annotation.Autowired; |
4 |
import org.springframework.jdbc.core.JdbcTemplate; |
|
5 |
import org.springframework.jdbc.core.RowMapper; |
|
6 | 4 |
import org.springframework.stereotype.Controller; |
7 | 5 |
import org.springframework.ui.ModelMap; |
8 | 6 |
import org.springframework.web.bind.annotation.GetMapping; |
9 | 7 |
import org.springframework.web.servlet.ModelAndView; |
8 |
import vldc.aswi.database.DatabaseInterface; |
|
9 |
import vldc.aswi.model.table.TableColumn; |
|
10 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow; |
|
11 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRowCell; |
|
12 |
import vldc.aswi.service.SqlQueryManager; |
|
13 |
import vldc.aswi.utils.Converter; |
|
10 | 14 |
|
11 | 15 |
import javax.sql.DataSource; |
12 |
import java.sql.*; |
|
13 | 16 |
import java.util.ArrayList; |
14 | 17 |
import java.util.List; |
18 |
import java.util.Map; |
|
15 | 19 |
|
16 | 20 |
@Controller |
17 | 21 |
public class IndexController { |
18 | 22 |
|
19 | 23 |
@Autowired |
20 |
protected DataSource dataSource;
|
|
24 |
private SqlQueryManager sqlQueryManager;
|
|
21 | 25 |
|
22 | 26 |
@GetMapping("/") |
23 | 27 |
public ModelAndView index() throws Exception { |
... | ... | |
25 | 29 |
|
26 | 30 |
ModelMap modelMap = modelAndView.getModelMap(); |
27 | 31 |
|
28 |
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); |
|
29 |
|
|
30 |
String sql = "SELECT SP.fakulta_SP as fakulta_studia, " + |
|
32 |
String sqlQuery = "SELECT SP.fakulta_SP as fakulta_studia, " + |
|
31 | 33 |
"SP.kodSP_i as program_studia, " + |
32 | 34 |
"fn_meaning2(SP.typ,'TYP_OBORU') as typ_studia, " + |
33 | 35 |
"fn_meaning2(SP.forma,'FORMA_OBORU_NEW') as forma_studia, " + |
34 | 36 |
"decode(SP.vykazovan,'A',ST.vykazovan,'N') as vykazovan_na_SIMS, " + |
35 | 37 |
"ST.os_cislo as studium, fn_meaning2(ST.stav,'STAV_STUDENTA') as stav_studia, " + |
36 |
"OS.prijmeni || ' ' || OS.jmeno as osoba, trunc((sysdate - OS.datum_naroz) / 365) as vek, " + |
|
38 |
"OS.prijmeni || ' ' || OS.jmeno as osoba, " + |
|
39 |
"trunc((sysdate - OS.datum_naroz) / 365) as vek, " + |
|
37 | 40 |
"1 as pocet " + |
38 | 41 |
"FROM studijni_programy SP, studenti ST, osoby OS " + |
39 |
"WHERE SP.stpridno = ST.stpridno AND ST.osobidno = OS.osobidno AND ROWNUM <= 20"; |
|
40 |
|
|
41 |
String resultHeader = "fakulta_studia;typ_studia;forma_studia;vykazovan_na_SIMS;stav_studia;vek;pocet"; |
|
42 |
List<String> sqlResults = new ArrayList<String>(); |
|
43 |
sqlResults.add(resultHeader); |
|
44 |
sqlResults.addAll(jdbcTemplate.query( |
|
45 |
sql, |
|
46 |
new RowMapper<String>() { |
|
47 |
public String mapRow(ResultSet rs, int rowNum) throws SQLException { |
|
48 |
String str = |
|
49 |
rs.getString("fakulta_studia") + |
|
50 |
";" + |
|
51 |
rs.getString("typ_studia") + |
|
52 |
";" + |
|
53 |
rs.getString("forma_studia") + |
|
54 |
";" + |
|
55 |
rs.getString("vykazovan_na_SIMS") + |
|
56 |
";" + |
|
57 |
rs.getString("stav_studia") + |
|
58 |
";" + |
|
59 |
rs.getString("vek") + |
|
60 |
";" + |
|
61 |
rs.getString("pocet") |
|
62 |
; |
|
63 |
return str; |
|
64 |
} |
|
65 |
}) |
|
66 |
); |
|
42 |
"WHERE SP.stpridno = ST.stpridno AND ST.osobidno = OS.osobidno AND ROWNUM <= 500"; |
|
67 | 43 |
|
68 | 44 |
modelMap.addAttribute("message", "Hello World!"); |
69 |
modelMap.addAttribute("sqlResults", sqlResults);
|
|
45 |
modelMap.addAttribute("contingencyTableRows", sqlQueryManager.getContingencyTableRow(sqlQuery));
|
|
70 | 46 |
|
71 | 47 |
return modelAndView; |
72 | 48 |
} |
src/main/webapp/WEB-INF/templates/index.html | ||
---|---|---|
10 | 10 |
<body> |
11 | 11 |
<nav th:replace="fragments/headers :: nav"></nav> |
12 | 12 |
<div th:text="${message}"></div> |
13 |
<p th:each="result : ${sqlResults}" th:text="${result}"><p/> |
|
13 |
<table> |
|
14 |
<tr th:each="contingencyTableRow : ${contingencyTableRows}"> |
|
15 |
<div th:if="${contingencyTableRow.isHeader()}"> |
|
16 |
<th th:each="contingencyTableRowCell : ${contingencyTableRow.getCells()}"> |
|
17 |
<span th:text="${contingencyTableRowCell.getValue()}"></span> |
|
18 |
</th> |
|
19 |
</div> |
|
20 |
<div th:unless="${contingencyTableRow.isHeader()}"> |
|
21 |
<td th:each="contingencyTableRowCell : ${contingencyTableRow.getCells()}"> |
|
22 |
<span th:text="${contingencyTableRowCell.getValue()}"></span> |
|
23 |
</td> |
|
24 |
</div> |
|
25 |
</tr> |
|
26 |
</table> |
|
14 | 27 |
</body> |
15 | 28 |
</html> |
Také k dispozici: Unified diff
re #7779 Convert TableColumn -> ContingencyTableRow and send it to frontend. SQL query is now executed in specific class DatabaseInterface via SqlQueryManager.