1
|
package eu.dnetlib.enabling.is.jdbc;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.Collection;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
|
8
|
import javax.sql.DataSource;
|
9
|
|
10
|
import org.antlr.stringtemplate.StringTemplate;
|
11
|
import org.apache.commons.logging.Log;
|
12
|
import org.apache.commons.logging.LogFactory;
|
13
|
import org.apache.cxf.helpers.IOUtils;
|
14
|
import org.springframework.beans.factory.annotation.Required;
|
15
|
import org.springframework.jdbc.core.JdbcTemplate;
|
16
|
|
17
|
import com.google.common.collect.Lists;
|
18
|
|
19
|
import eu.dnetlib.enabling.is.rmi.InformationServiceException;
|
20
|
import eu.dnetlib.enabling.is.rmi.Operation;
|
21
|
|
22
|
public class DatabaseUtils {
|
23
|
|
24
|
private JdbcTemplate jdbcTemplate;
|
25
|
|
26
|
private static final Log log = LogFactory.getLog(DatabaseUtils.class);
|
27
|
|
28
|
public List<String> searchJson(final String sql) {
|
29
|
log.debug(sql);
|
30
|
return jdbcTemplate.query(sql, new JsonRowMapper());
|
31
|
}
|
32
|
|
33
|
public List<Map<String, Object>> searchSimple(final String sql) {
|
34
|
log.debug(sql);
|
35
|
return jdbcTemplate.queryForList(sql);
|
36
|
}
|
37
|
|
38
|
public int update(final String sql) {
|
39
|
log.debug(sql);
|
40
|
return jdbcTemplate.update(sql);
|
41
|
}
|
42
|
|
43
|
public boolean isRegisteredService(final String address) {
|
44
|
final String sql = "SELECT service from service_protocols where address = ?";
|
45
|
final List<String> list = jdbcTemplate.queryForList(sql, String.class, address);
|
46
|
return list != null && !list.isEmpty();
|
47
|
}
|
48
|
|
49
|
public boolean isRegisteredDs(final String code, final String type) {
|
50
|
final String sql = "SELECT id from datastructures where code = ? and type = ?";
|
51
|
final List<String> list = jdbcTemplate.queryForList(sql, String.class, code, type);
|
52
|
return list != null && !list.isEmpty();
|
53
|
}
|
54
|
|
55
|
public String findServiceIdByAddress(final String... addrs) {
|
56
|
return findServiceIdByAddress(Lists.newArrayList(addrs));
|
57
|
}
|
58
|
|
59
|
public String findServiceIdByAddress(final Collection<String> addresses) {
|
60
|
final String sql = "SELECT service from service_protocols where address = ?";
|
61
|
for (String addr : addresses) {
|
62
|
final List<String> list = jdbcTemplate.queryForList(sql, String.class, addr);
|
63
|
if (list != null && !list.isEmpty()) { return list.get(0); }
|
64
|
}
|
65
|
return null;
|
66
|
}
|
67
|
|
68
|
public String findDsIdByCode(final String code, final String type) {
|
69
|
final String sql = "SELECT id from datastructures where code = ? and type = ?";
|
70
|
final List<String> list = jdbcTemplate.queryForList(sql, String.class, code, type);
|
71
|
return list == null || list.isEmpty() ? null : list.get(0);
|
72
|
}
|
73
|
|
74
|
public void registerNotificationTrigger(final Operation operation, final String table) throws InformationServiceException {
|
75
|
try {
|
76
|
final StringTemplate st = new StringTemplate();
|
77
|
|
78
|
switch (operation) {
|
79
|
case INSERT:
|
80
|
st.setTemplate(IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/enabling/is/templates/trigger_insert.sql.st")));
|
81
|
break;
|
82
|
case DELETE:
|
83
|
st.setTemplate(IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/enabling/is/templates/trigger_delete.sql.st")));
|
84
|
break;
|
85
|
case UPDATE:
|
86
|
st.setTemplate(IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/enabling/is/templates/trigger_update.sql.st")));
|
87
|
break;
|
88
|
default:
|
89
|
throw new InformationServiceException("Unknown operation: " + operation);
|
90
|
}
|
91
|
|
92
|
st.setAttribute("table", table);
|
93
|
|
94
|
update(st.toString());
|
95
|
} catch (IOException e) {
|
96
|
throw new InformationServiceException("Error registering trigger on table " + table);
|
97
|
}
|
98
|
}
|
99
|
|
100
|
public DataSource getDataSource() {
|
101
|
return jdbcTemplate.getDataSource();
|
102
|
}
|
103
|
|
104
|
public JdbcTemplate getJdbcTemplate() {
|
105
|
return jdbcTemplate;
|
106
|
}
|
107
|
|
108
|
@Required
|
109
|
public void setJdbcTemplate(final JdbcTemplate jdbcTemplate) {
|
110
|
this.jdbcTemplate = jdbcTemplate;
|
111
|
}
|
112
|
|
113
|
}
|