1
|
package eu.dnetlib.goldoa.service.dao;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.BankAccount;
|
4
|
import eu.dnetlib.goldoa.domain.Currency;
|
5
|
import eu.dnetlib.goldoa.domain.Person;
|
6
|
import eu.dnetlib.goldoa.domain.Publisher;
|
7
|
import eu.dnetlib.goldoa.domain.Vocabulary;
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
import org.springframework.jdbc.core.JdbcTemplate;
|
10
|
import org.springframework.jdbc.core.RowMapper;
|
11
|
|
12
|
import javax.sql.DataSource;
|
13
|
import java.sql.ResultSet;
|
14
|
import java.sql.SQLException;
|
15
|
import java.sql.Types;
|
16
|
import java.util.List;
|
17
|
|
18
|
/**
|
19
|
* Created by antleb on 3/13/15.
|
20
|
*/
|
21
|
|
22
|
public class PublisherDAO {
|
23
|
|
24
|
@Autowired
|
25
|
private DataSource dataSource;
|
26
|
|
27
|
private static final String SEARCH_PUBLISHERS = "select id, name from publisher where lower(name) like lower(?)";
|
28
|
|
29
|
private static final String GET_PUBLISHER = "select id, name, email, contact, apc, apc_currency, discount, source, bank_name, bank_address, bank_code, bank_holder, bank_iban from publisher where id=?";
|
30
|
|
31
|
private static final String UPDATE_PUBLISHER = "update publisher set name=?, email=?, contact=?, apc=?, apc_currency=?, discount=?, bank_name=?, bank_address=?, bank_code=?, bank_holder=?, bank_iban=?, source=? where id=?";
|
32
|
|
33
|
private static final String INSERT_PUBLISHER = "insert into publisher (name, email, contact, apc, apc_currency, discount, bank_name, bank_address, bank_code, bank_holder, bank_iban, source, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
34
|
|
35
|
|
36
|
public List<Vocabulary> search(String term) {
|
37
|
return new JdbcTemplate(dataSource).query(SEARCH_PUBLISHERS, new String[]{"%" + term + "%"}, new int[]{Types.VARCHAR}, new RowMapper<Vocabulary>() {
|
38
|
@Override
|
39
|
public Vocabulary mapRow(ResultSet resultSet, int i) throws SQLException {
|
40
|
return new Vocabulary(resultSet.getString("id"), resultSet.getString("name"));
|
41
|
}
|
42
|
});
|
43
|
}
|
44
|
|
45
|
public Publisher getPublisher(String id) {
|
46
|
return new JdbcTemplate(dataSource).queryForObject(GET_PUBLISHER, new String[]{id}, new int[]{Types.VARCHAR}, new RowMapper<Publisher>() {
|
47
|
@Override
|
48
|
public Publisher mapRow(ResultSet rs, int i) throws SQLException {
|
49
|
Person person = rs.getString("contact") != null ? new Person(rs.getString("contact")) : null;
|
50
|
BankAccount bankAccount = new BankAccount(rs.getString("bank_name"), rs.getString("bank_address"), rs.getString("bank_code"), rs.getString("bank_holder"), rs.getString("bank_iban"));
|
51
|
|
52
|
return new Publisher(rs.getString("id"), rs.getString("name"), rs.getString("email"), person, rs.getFloat("apc"), Currency.valueOf(rs.getString("apc_currency")), rs.getFloat("discount"), bankAccount, rs.getString("source"));
|
53
|
}
|
54
|
});
|
55
|
}
|
56
|
|
57
|
public Publisher savePublisher(Publisher publisher) {
|
58
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
59
|
int[] types = {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.REAL, Types.VARCHAR, Types.REAL, Types.VARCHAR, Types.VARCHAR,
|
60
|
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
|
61
|
BankAccount bankAccount = publisher.getBankAccount() != null ? publisher.getBankAccount() : new BankAccount();
|
62
|
|
63
|
Object[] args = {publisher.getName(), publisher.getEmail(), publisher.getContact() != null ? publisher.getContact().getId() : null, publisher.getAPC(), publisher.getCurrency().name(),
|
64
|
publisher.getDiscount(), bankAccount.getBankName(), bankAccount.getBankAddress(), bankAccount.getBankCode(), bankAccount.getAccountHolder(),
|
65
|
bankAccount.getIban(), publisher.getSource(), publisher.getId()};
|
66
|
|
67
|
|
68
|
if (jdbcTemplate.update(UPDATE_PUBLISHER, args, types) == 0) {
|
69
|
jdbcTemplate.update(INSERT_PUBLISHER, args, types);
|
70
|
}
|
71
|
|
72
|
return publisher;
|
73
|
}
|
74
|
|
75
|
public DataSource getDataSource() {
|
76
|
return dataSource;
|
77
|
}
|
78
|
|
79
|
public void setDataSource(DataSource dataSource) {
|
80
|
this.dataSource = dataSource;
|
81
|
}
|
82
|
}
|