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
|
|
12
|
import javax.sql.DataSource;
|
13
|
import java.sql.PreparedStatement;
|
14
|
import java.sql.ResultSet;
|
15
|
import java.sql.SQLException;
|
16
|
import java.sql.Types;
|
17
|
import java.util.ArrayList;
|
18
|
import java.util.Arrays;
|
19
|
import java.util.List;
|
20
|
|
21
|
/**
|
22
|
* Created by antleb on 3/13/15.
|
23
|
*/
|
24
|
public class OrganizationDAO {
|
25
|
@Autowired
|
26
|
private DataSource dataSource;
|
27
|
|
28
|
private static final String SEARCH_ORGANIZATIONS = "select id, name from organisation where lower(name) like lower(?)";
|
29
|
private static final String GET_ORGANIZATION = "select o.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 o.id, name, shortname";
|
30
|
private static final String GET_AFFILIATED_WITH_USER = "select o.id, name, shortname, array_agg(distinct budget) as budgets \n" +
|
31
|
"from organisation o \n" +
|
32
|
"left join organization_budget ob on ob.organization=o.id \n" +
|
33
|
"join affiliation a on a.organization=o.id\n" +
|
34
|
"where a.person=?\n" +
|
35
|
"group by o.id, name, shortname";
|
36
|
|
37
|
private final String INSERT_ORGANIZATION = "insert into organisation (name, shortname, source, id) values (?,?,?,?)";
|
38
|
private final String UPDATE_ORGANIZATION = "update organisation set name=?, shortname=?, source=? where id=?";
|
39
|
|
40
|
private final String INSERT_ORGANIZATION_BUDGET = "insert into organization_budget (organization, budget) values (?, ?)";
|
41
|
|
42
|
public Organization getOrganization(String id) {
|
43
|
return new JdbcTemplate(dataSource).queryForObject(GET_ORGANIZATION, new Object[]{id}, new int[]{Types.VARCHAR}, new RowMapper<Organization>() {
|
44
|
|
45
|
@Override
|
46
|
public Organization mapRow(ResultSet rs, int rowNum) throws SQLException {
|
47
|
Organization org = new Organization();
|
48
|
|
49
|
org.setId(rs.getString("id"));
|
50
|
org.setName(rs.getString("name"));
|
51
|
org.setShortName(rs.getString("shortname"));
|
52
|
|
53
|
org.setBudgets(new ArrayList<Budget>());
|
54
|
for (String budget : Arrays.asList((String[]) rs.getArray("budgets").getArray()))
|
55
|
if (budget != null)
|
56
|
org.getBudgets().add(new Budget(budget));
|
57
|
|
58
|
return org;
|
59
|
}
|
60
|
});
|
61
|
}
|
62
|
|
63
|
public List<Vocabulary> search(String term) {
|
64
|
return new JdbcTemplate(dataSource).query(SEARCH_ORGANIZATIONS, new Object[]{"%" + term + "%"}, new int[]{Types.VARCHAR}, new RowMapper<Vocabulary>() {
|
65
|
@Override
|
66
|
public Vocabulary mapRow(ResultSet rs, int rowNum) throws SQLException {
|
67
|
return new Vocabulary(rs.getString("id"), rs.getString("name"));
|
68
|
}
|
69
|
});
|
70
|
}
|
71
|
|
72
|
public Organization saveOrganization(Organization organization) {
|
73
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
74
|
Object[] args = {organization.getName(), organization.getShortName(), organization.getSource(), organization.getId()};
|
75
|
int[] types = {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
|
76
|
|
77
|
if (jdbcTemplate.update(UPDATE_ORGANIZATION, args, types) == 0)
|
78
|
jdbcTemplate.update(INSERT_ORGANIZATION, args, types);
|
79
|
|
80
|
return organization;
|
81
|
}
|
82
|
|
83
|
public void insertOrganization(final Organization organization) {
|
84
|
new JdbcTemplate(dataSource).update(INSERT_ORGANIZATION, new PreparedStatementSetter() {
|
85
|
@Override
|
86
|
public void setValues(PreparedStatement ps) throws SQLException {
|
87
|
ps.setString(1, organization.getId());
|
88
|
ps.setString(2, organization.getName());
|
89
|
ps.setString(3, organization.getShortName());
|
90
|
}
|
91
|
});
|
92
|
}
|
93
|
|
94
|
public void insertOrganizationBudgets(final Organization organization) {
|
95
|
new JdbcTemplate(dataSource).batchUpdate(INSERT_ORGANIZATION_BUDGET, new BatchPreparedStatementSetter() {
|
96
|
@Override
|
97
|
public void setValues(PreparedStatement ps, int i) throws SQLException {
|
98
|
ps.setString(1, organization.getId());
|
99
|
ps.setString(2, organization.getBudgets().get(i).getId());
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
public int getBatchSize() {
|
104
|
return organization.getBudgets() != null ? organization.getBudgets().size() : 0;
|
105
|
}
|
106
|
});
|
107
|
}
|
108
|
|
109
|
public List<Organization> getAffiliatedWithUser(String userId) {
|
110
|
return new JdbcTemplate(dataSource).query(GET_AFFILIATED_WITH_USER, new Object[]{userId}, new int[]{Types.VARCHAR}, new RowMapper<Organization>() {
|
111
|
|
112
|
@Override
|
113
|
public Organization mapRow(ResultSet rs, int rowNum) throws SQLException {
|
114
|
Organization org = new Organization();
|
115
|
|
116
|
org.setId(rs.getString("id"));
|
117
|
org.setName(rs.getString("name"));
|
118
|
org.setShortName(rs.getString("shortname"));
|
119
|
|
120
|
org.setBudgets(new ArrayList<Budget>());
|
121
|
for (String budget : Arrays.asList((String[]) rs.getArray("budgets").getArray()))
|
122
|
if (budget != null)
|
123
|
org.getBudgets().add(new Budget(budget));
|
124
|
|
125
|
return org;
|
126
|
}
|
127
|
});
|
128
|
}
|
129
|
|
130
|
public void deleteOrganizationBudgets(Organization organization) {
|
131
|
new JdbcTemplate(dataSource).update("delete from organization_budget where organization = ?", new String[]{organization.getId()}, new int[]{Types.VARCHAR});
|
132
|
}
|
133
|
}
|