1
|
package eu.dnetlib.goldoa.service.dao;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.Help;
|
4
|
import org.springframework.beans.factory.annotation.Autowired;
|
5
|
import org.springframework.jdbc.core.JdbcTemplate;
|
6
|
import org.springframework.jdbc.core.RowMapper;
|
7
|
|
8
|
import javax.sql.DataSource;
|
9
|
import java.sql.ResultSet;
|
10
|
import java.sql.SQLException;
|
11
|
import java.sql.Types;
|
12
|
import java.util.List;
|
13
|
|
14
|
/**
|
15
|
* Created by antleb on 4/17/15.
|
16
|
*/
|
17
|
public class HelpDAO {
|
18
|
|
19
|
@Autowired
|
20
|
private DataSource dataSource;
|
21
|
|
22
|
private final static String UPDATE_HELP = "update help set name=?, text=? where id=?";
|
23
|
private final static String INSERT_HELP = "insert into help (name, text, id) values (?, ?, ?)";
|
24
|
private final static String GET_BY_ID = "select id, name, text from help where id = ?";
|
25
|
private final static String GET_ALL = "select id, name, text from help";
|
26
|
private final static String DELETE = "delete from help where id = ?";
|
27
|
|
28
|
|
29
|
private RowMapper<Help> helpRowMapper = new RowMapper<Help>() {
|
30
|
@Override
|
31
|
public Help mapRow(ResultSet rs, int i) throws SQLException {
|
32
|
return new Help(rs.getString("id"), rs.getString("name"), rs.getString("text"));
|
33
|
}
|
34
|
};
|
35
|
|
36
|
public Help saveHelp(Help help) {
|
37
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
38
|
|
39
|
if (jdbcTemplate.update(UPDATE_HELP, new String[]{help.getName(), help.getText(), help.getId()}, new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR}) == 0)
|
40
|
jdbcTemplate.update(INSERT_HELP, new String[]{help.getName(), help.getText(), help.getId()}, new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR});
|
41
|
|
42
|
return help;
|
43
|
}
|
44
|
|
45
|
public Help getById(String id) {
|
46
|
return new JdbcTemplate(dataSource).queryForObject(GET_BY_ID, new String[]{id}, new int[]{Types.VARCHAR}, helpRowMapper);
|
47
|
}
|
48
|
|
49
|
public List<Help> getAll() {
|
50
|
return new JdbcTemplate(dataSource).query(GET_ALL, helpRowMapper);
|
51
|
}
|
52
|
|
53
|
public void delete(String helpId) {
|
54
|
new JdbcTemplate(dataSource).update(DELETE, new String[]{helpId}, new int[]{Types.VARCHAR});
|
55
|
}
|
56
|
}
|