1
|
package eu.dnetlib.goldoa.service.dao;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.Budget;
|
4
|
import eu.dnetlib.goldoa.domain.Organization;
|
5
|
import eu.dnetlib.goldoa.domain.Vocabulary;
|
6
|
import org.springframework.beans.factory.annotation.Autowired;
|
7
|
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
8
|
import org.springframework.jdbc.core.JdbcTemplate;
|
9
|
import org.springframework.jdbc.core.PreparedStatementSetter;
|
10
|
import org.springframework.jdbc.core.RowMapper;
|
11
|
import org.springframework.transaction.annotation.Propagation;
|
12
|
import org.springframework.transaction.annotation.Transactional;
|
13
|
|
14
|
import javax.sql.DataSource;
|
15
|
import java.sql.PreparedStatement;
|
16
|
import java.sql.ResultSet;
|
17
|
import java.sql.SQLException;
|
18
|
import java.sql.Types;
|
19
|
import java.util.ArrayList;
|
20
|
import java.util.Arrays;
|
21
|
import java.util.List;
|
22
|
|
23
|
/**
|
24
|
* Created by antleb on 3/13/15.
|
25
|
*/
|
26
|
public class OrganizationDAO {
|
27
|
@Autowired
|
28
|
private DataSource dataSource;
|
29
|
|
30
|
private final String SEARCH_ORGANIZATIONS = "select id, name from organisation where lower(name) like lower(?)";
|
31
|
private final String GET_ORGANIZATION = "select id, name, shortname, array_agg(distinct budget) as budgets from organisation o left join organization_budget ob on ob.organization=o.id where o.id = ? group by id, name, shortname";
|
32
|
|
33
|
private final String INSERT_ORGANIZATION = "insert into organization (id, name, shortname, source) values (?, ?, ?, 'user')";
|
34
|
|
35
|
private final String INSERT_ORGANIZATION_BUDGET = "insert into organization_budget (organization, budget) values (?, ?)";
|
36
|
|
37
|
public Organization getOrganization(String id) {
|
38
|
return new JdbcTemplate(dataSource).queryForObject(GET_ORGANIZATION, new Object[]{id}, new int[]{Types.VARCHAR}, new RowMapper<Organization>() {
|
39
|
|
40
|
@Override
|
41
|
public Organization mapRow(ResultSet rs, int rowNum) throws SQLException {
|
42
|
Organization org = new Organization();
|
43
|
|
44
|
org.setId(rs.getString("id"));
|
45
|
org.setName(rs.getString("name"));
|
46
|
org.setShortName(rs.getString("shortname"));
|
47
|
|
48
|
org.setBudgets(new ArrayList<Budget>());
|
49
|
for (String budget:Arrays.asList((String[]) rs.getArray("budgets").getArray()))
|
50
|
org.getBudgets().add(new Budget(budget));
|
51
|
|
52
|
return org;
|
53
|
}
|
54
|
});
|
55
|
}
|
56
|
|
57
|
public List<Vocabulary> search(String term) {
|
58
|
return new JdbcTemplate(dataSource).query(SEARCH_ORGANIZATIONS, new Object[]{"%" + term + "%"}, new int[]{Types.VARCHAR}, new RowMapper<Vocabulary>() {
|
59
|
@Override
|
60
|
public Vocabulary mapRow(ResultSet rs, int rowNum) throws SQLException {
|
61
|
return new Vocabulary(rs.getString("id"), rs.getString("name"));
|
62
|
}
|
63
|
});
|
64
|
}
|
65
|
|
66
|
public void insertOrganization(final Organization organization) {
|
67
|
new JdbcTemplate(dataSource).update(INSERT_ORGANIZATION, new PreparedStatementSetter() {
|
68
|
@Override
|
69
|
public void setValues(PreparedStatement ps) throws SQLException {
|
70
|
ps.setString(1, organization.getId());
|
71
|
ps.setString(2, organization.getName());
|
72
|
ps.setString(3, organization.getShortName());
|
73
|
}
|
74
|
});
|
75
|
}
|
76
|
|
77
|
public void insertOrganizationBudget(final Organization organization) {
|
78
|
new JdbcTemplate(dataSource).batchUpdate(INSERT_ORGANIZATION_BUDGET, new BatchPreparedStatementSetter() {
|
79
|
@Override
|
80
|
public void setValues(PreparedStatement ps, int i) throws SQLException {
|
81
|
ps.setString(1, organization.getId());
|
82
|
ps.setString(2, organization.getBudgets().get(i).getId());
|
83
|
}
|
84
|
|
85
|
@Override
|
86
|
public int getBatchSize() {
|
87
|
return organization.getBudgets()!=null?organization.getBudgets().size():0;
|
88
|
}
|
89
|
});
|
90
|
}
|
91
|
|
92
|
public DataSource getDataSource() {
|
93
|
return dataSource;
|
94
|
}
|
95
|
|
96
|
public void setDataSource(DataSource dataSource) {
|
97
|
this.dataSource = dataSource;
|
98
|
}
|
99
|
}
|