1
|
package eu.dnetlib.goldoa.service;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.Budget;
|
4
|
import eu.dnetlib.goldoa.domain.Organization;
|
5
|
import eu.dnetlib.goldoa.domain.OrganizationManagerException;
|
6
|
import eu.dnetlib.goldoa.domain.Vocabulary;
|
7
|
import org.springframework.dao.EmptyResultDataAccessException;
|
8
|
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
9
|
import org.springframework.jdbc.core.JdbcTemplate;
|
10
|
import org.springframework.jdbc.core.PreparedStatementSetter;
|
11
|
import org.springframework.jdbc.core.RowMapper;
|
12
|
import org.springframework.jdbc.datasource.DataSourceUtils;
|
13
|
import org.springframework.transaction.annotation.Transactional;
|
14
|
|
15
|
import javax.sql.DataSource;
|
16
|
import java.sql.Connection;
|
17
|
import java.sql.PreparedStatement;
|
18
|
import java.sql.ResultSet;
|
19
|
import java.sql.SQLException;
|
20
|
import java.sql.Types;
|
21
|
import java.util.ArrayList;
|
22
|
import java.util.Arrays;
|
23
|
import java.util.List;
|
24
|
import java.util.UUID;
|
25
|
|
26
|
/**
|
27
|
* Created by antleb on 3/3/15.
|
28
|
*/
|
29
|
@Transactional
|
30
|
public class OrganizationManagerImpl implements OrganizationManager {
|
31
|
private DataSource dataSource;
|
32
|
|
33
|
private final String SEARCH_ORGANIZATIONS = "select id, name from organisation where lower(name) like lower(?)";
|
34
|
private final String GET_ORGANIZATION = "select * from organisation o left join organization_budget ob on ob.organization=o.id where o.id = ?";
|
35
|
|
36
|
private final String INSERT_ORGANIZATION = "insert into organization (id, name, shortname, source) values (?, ?, ?, 'user')";
|
37
|
|
38
|
private final String INSERT_ORGANIZATION_BUDGET = "insert into organization_budget (organization, budget) values (?, ?)";
|
39
|
|
40
|
@Override
|
41
|
public List<Vocabulary> search(String term) {
|
42
|
|
43
|
return new JdbcTemplate(dataSource).query(SEARCH_ORGANIZATIONS, new Object[]{"%" + term + "%"}, new int[]{Types.VARCHAR}, new RowMapper<Vocabulary>() {
|
44
|
@Override
|
45
|
public Vocabulary mapRow(ResultSet rs, int rowNum) throws SQLException {
|
46
|
return new Vocabulary(rs.getString("id"), rs.getString("name"));
|
47
|
}
|
48
|
});
|
49
|
}
|
50
|
|
51
|
@Override
|
52
|
public Organization getById(String organizationId) throws OrganizationManagerException {
|
53
|
try {
|
54
|
return new JdbcTemplate(dataSource).queryForObject(GET_ORGANIZATION, new Object[]{organizationId}, new int[]{Types.VARCHAR}, new RowMapper<Organization>() {
|
55
|
|
56
|
@Override
|
57
|
public Organization mapRow(ResultSet rs, int rowNum) throws SQLException {
|
58
|
Organization org = new Organization();
|
59
|
|
60
|
org.setId(rs.getString("id"));
|
61
|
org.setName(rs.getString("name"));
|
62
|
org.setShortName(rs.getString("shortname"));
|
63
|
|
64
|
org.setBudgets(new ArrayList<String>());
|
65
|
org.setBudgets(Arrays.asList((String[]) rs.getArray("budgets").getArray()));
|
66
|
|
67
|
return org;
|
68
|
}
|
69
|
});
|
70
|
} catch (EmptyResultDataAccessException e) {
|
71
|
throw new OrganizationManagerException(OrganizationManagerException.ErrorCause.NOT_EXISTS);
|
72
|
}
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
public String saveOrganization(final Organization organization) throws OrganizationManagerException {
|
77
|
|
78
|
if (organization.getId() == null)
|
79
|
organization.setId(organization.getShortName());
|
80
|
|
81
|
try {
|
82
|
getById(organization.getId());
|
83
|
throw new OrganizationManagerException(OrganizationManagerException.ErrorCause.ALREADY_EXISTS);
|
84
|
} catch (OrganizationManagerException e) {
|
85
|
if (e.getErrorCause() == OrganizationManagerException.ErrorCause.ALREADY_EXISTS)
|
86
|
throw e;
|
87
|
}
|
88
|
|
89
|
new JdbcTemplate(dataSource).update(INSERT_ORGANIZATION, new PreparedStatementSetter() {
|
90
|
@Override
|
91
|
public void setValues(PreparedStatement ps) throws SQLException {
|
92
|
ps.setString(1, organization.getId());
|
93
|
ps.setString(2, organization.getName());
|
94
|
ps.setString(3, organization.getShortName());
|
95
|
}
|
96
|
});
|
97
|
|
98
|
new JdbcTemplate(dataSource).batchUpdate(INSERT_ORGANIZATION_BUDGET, new BatchPreparedStatementSetter() {
|
99
|
@Override
|
100
|
public void setValues(PreparedStatement ps, int i) throws SQLException {
|
101
|
ps.setString(1, organization.getId());
|
102
|
ps.setString(2, organization.getBudgets().get(i));
|
103
|
}
|
104
|
|
105
|
@Override
|
106
|
public int getBatchSize() {
|
107
|
return organization.getBudgets()!=null?organization.getBudgets().size():0;
|
108
|
}
|
109
|
});
|
110
|
|
111
|
return organization.getId();
|
112
|
}
|
113
|
|
114
|
private Connection getConnection() {
|
115
|
return DataSourceUtils.getConnection(dataSource);
|
116
|
}
|
117
|
|
118
|
public DataSource getDataSource() {
|
119
|
return dataSource;
|
120
|
}
|
121
|
|
122
|
public void setDataSource(DataSource dataSource) {
|
123
|
this.dataSource = dataSource;
|
124
|
}
|
125
|
|
126
|
}
|