1
|
package eu.dnetlib.functionality.modular.ui.db;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.StringReader;
|
5
|
import java.util.ArrayList;
|
6
|
import java.util.HashMap;
|
7
|
import java.util.List;
|
8
|
import java.util.Map;
|
9
|
|
10
|
import javax.annotation.Resource;
|
11
|
import javax.servlet.ServletOutputStream;
|
12
|
import javax.servlet.ServletResponse;
|
13
|
import javax.servlet.http.HttpServletResponse;
|
14
|
|
15
|
import org.apache.commons.io.IOUtils;
|
16
|
import org.springframework.jdbc.support.rowset.SqlRowSet;
|
17
|
import org.springframework.stereotype.Controller;
|
18
|
import org.springframework.web.bind.annotation.RequestMapping;
|
19
|
import org.springframework.web.bind.annotation.RequestParam;
|
20
|
import org.springframework.web.bind.annotation.ResponseBody;
|
21
|
|
22
|
import com.google.common.collect.Iterables;
|
23
|
import com.google.common.collect.Lists;
|
24
|
|
25
|
import eu.dnetlib.enabling.database.DatabaseServiceCore;
|
26
|
import eu.dnetlib.enabling.database.objects.DnetDatabase;
|
27
|
import eu.dnetlib.enabling.database.resultset.IterableRowSet;
|
28
|
import eu.dnetlib.enabling.resultset.client.utils.EPRUtils;
|
29
|
|
30
|
@Controller
|
31
|
public class DbManagerInternalController {
|
32
|
public static final String CTYPE_CSS = "text/css";
|
33
|
public static final String CTYPE_JS = "text/javascript";
|
34
|
public static final String CTYPE_XML = "text/xml";
|
35
|
public static final String CTYPE_TEXT = "text/plain";
|
36
|
public static final String CTYPE_HTML = "text/html";
|
37
|
|
38
|
@Resource
|
39
|
private DatabaseServiceCore core;
|
40
|
|
41
|
@RequestMapping("/ui/listDBs.do")
|
42
|
public @ResponseBody List<DnetDatabase> listDatabases() throws Exception {
|
43
|
return core.listDatabases();
|
44
|
}
|
45
|
|
46
|
@RequestMapping("/ui/listTables.do")
|
47
|
public @ResponseBody List<Map<String, Object>> listTables(@RequestParam(value = "db", required = true) String db) throws Exception {
|
48
|
|
49
|
final List<Map<String, Object>> tables = new ArrayList<Map<String, Object>>();
|
50
|
|
51
|
final String sql = IOUtils.toString(getClass().getResourceAsStream("tables_info.sql"));
|
52
|
|
53
|
final SqlRowSet rows = core.getDbUtils().executeSql(db, sql, SqlRowSet.class);
|
54
|
while (rows.next()) {
|
55
|
final String tname = rows.getString("name");
|
56
|
|
57
|
final Map<String, Object> t = new HashMap<String, Object>();
|
58
|
t.put("name", rows.getString("name"));
|
59
|
t.put("view", rows.getString("kind").equalsIgnoreCase("v"));
|
60
|
t.put("data", rows.getString("data"));
|
61
|
t.put("indices", rows.getString("indices"));
|
62
|
t.put("total", rows.getString("total"));
|
63
|
t.put("managed", core.getDbUtils().isManagedTable(db, tname));
|
64
|
t.put("dnetIdentifier", core.getDbUtils().getDefaultDnetIdentifier(db, tname));
|
65
|
tables.add(t);
|
66
|
}
|
67
|
|
68
|
return tables;
|
69
|
}
|
70
|
|
71
|
@RequestMapping("/ui/manageDB.do")
|
72
|
public @ResponseBody boolean manageDB(@RequestParam(value = "db", required = true) String db,
|
73
|
@RequestParam(value = "manage", required = true) boolean b) throws Exception {
|
74
|
core.changeDatabaseStatus(db, b);
|
75
|
return true;
|
76
|
}
|
77
|
|
78
|
@RequestMapping("/ui/importEPR.do")
|
79
|
public @ResponseBody boolean importEPR(@RequestParam(value = "db", required = true) String db,
|
80
|
@RequestParam(value = "epr", required = true) String epr) throws Exception {
|
81
|
|
82
|
core.importFromResultset(db, (new EPRUtils()).getEpr(epr));
|
83
|
|
84
|
return true;
|
85
|
}
|
86
|
|
87
|
|
88
|
@RequestMapping("/ui/query.do")
|
89
|
public @ResponseBody List<String> query(@RequestParam(value = "query", required = true) final String query,
|
90
|
@RequestParam(value = "db", required = true) final String db,
|
91
|
@RequestParam(value = "limit", required = true) int limit) throws Exception {
|
92
|
|
93
|
return Lists.newArrayList(Iterables.limit(new IterableRowSet(db, query, null, core.getDbUtils()), limit));
|
94
|
|
95
|
}
|
96
|
|
97
|
@RequestMapping("/ui/changeTableManagement.do")
|
98
|
public @ResponseBody boolean changeTableManagement(@RequestParam(value = "db", required = true) final String db,
|
99
|
@RequestParam(value = "t", required = true) final String t) throws Exception {
|
100
|
|
101
|
if (core.getDbUtils().isManagedTable(db, t)) {
|
102
|
core.getDbUtils().removeManagementOfTable(db, t);
|
103
|
} else {
|
104
|
core.getDbUtils().prepareManagementOfTable(db, t);
|
105
|
}
|
106
|
|
107
|
return true;
|
108
|
}
|
109
|
|
110
|
|
111
|
@RequestMapping("/ui/changeIdentifiers.do")
|
112
|
public @ResponseBody boolean changeId(HttpServletResponse res,
|
113
|
@RequestParam(value = "db", required = true) final String db,
|
114
|
@RequestParam(value = "t", required = false) final String t) throws Exception {
|
115
|
|
116
|
if (t == null || t.isEmpty()) {
|
117
|
core.getDbUtils().reassignDefaultDnetIdentifiers(db);
|
118
|
} else {
|
119
|
core.getDbUtils().reassignDefaultDnetIdentifiers(db, t);
|
120
|
}
|
121
|
|
122
|
return true;
|
123
|
}
|
124
|
|
125
|
@RequestMapping("/ui/describeTable.do")
|
126
|
public @ResponseBody List<Map<?, ?>> describeTable(@RequestParam(value = "db", required = true) final String db,
|
127
|
@RequestParam(value = "t", required = true) final String t) throws Exception {
|
128
|
return core.getDbUtils().describeTable(db, t);
|
129
|
}
|
130
|
|
131
|
@RequestMapping("/ui/dumpTable.do")
|
132
|
public void dumpTable(final ServletResponse response, @RequestParam(value = "db", required = true) final String db,
|
133
|
@RequestParam(value = "t", required = true) final String t) throws Exception {
|
134
|
sendXML(response, core.getDbUtils().dumpTableAsXML(db,t));
|
135
|
}
|
136
|
|
137
|
@RequestMapping("/ui/dumpTableEPR.do")
|
138
|
public void dumpTableEPR(final ServletResponse response,
|
139
|
@RequestParam(value = "db", required = true) final String db,
|
140
|
@RequestParam(value = "t", required = true) final String t) throws Exception {
|
141
|
|
142
|
sendXML(response, core.generateResultSet(db, t, null).toString());
|
143
|
}
|
144
|
|
145
|
|
146
|
public void sendXML(final ServletResponse response, final String xml) throws IOException {
|
147
|
response.setContentType("text/xml");
|
148
|
|
149
|
final ServletOutputStream out = response.getOutputStream();
|
150
|
IOUtils.copy(new StringReader(xml), out);
|
151
|
|
152
|
out.flush();
|
153
|
out.close();
|
154
|
}
|
155
|
}
|