1
|
package eu.dnetlib.openaire.user.dao;
|
2
|
|
3
|
import eu.dnetlib.openaire.user.Role;
|
4
|
import eu.dnetlib.openaire.user.store.DataSourceConnector;
|
5
|
import eu.dnetlib.openaire.user.store.Statement;
|
6
|
import org.springframework.beans.factory.annotation.Autowired;
|
7
|
|
8
|
import javax.sql.DataSource;
|
9
|
|
10
|
import java.sql.Connection;
|
11
|
import java.sql.PreparedStatement;
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.List;
|
16
|
|
17
|
import static eu.dnetlib.openaire.user.queries.RoleQueries.*;
|
18
|
|
19
|
/**
|
20
|
* Created by sofia on 8/11/2016.
|
21
|
*/
|
22
|
public class RoleDAO {
|
23
|
|
24
|
private final DataSource ds;
|
25
|
|
26
|
@Autowired
|
27
|
private DataSourceConnector dataSourceConnector;
|
28
|
|
29
|
public RoleDAO() {
|
30
|
this.ds = dataSourceConnector.getDatasource();
|
31
|
}
|
32
|
|
33
|
// FETCH
|
34
|
|
35
|
public List<Role> fetchAll()
|
36
|
throws SQLException
|
37
|
{
|
38
|
return (List<Role>) executeQuery(FETCH_ALL);
|
39
|
}
|
40
|
|
41
|
public Role fetchById(final int id)
|
42
|
throws SQLException
|
43
|
{
|
44
|
List<Role> roles = executeQuery(FETCH_BY_ID, new Statement.Initializer() {
|
45
|
@Override
|
46
|
public void prepare(PreparedStatement stmt) throws SQLException {
|
47
|
stmt.setInt(1, id);
|
48
|
}
|
49
|
});
|
50
|
|
51
|
if (roles.isEmpty())
|
52
|
return null;
|
53
|
|
54
|
return roles.get(0);
|
55
|
}
|
56
|
|
57
|
public Role fetchByRole(final String role)
|
58
|
throws SQLException
|
59
|
{
|
60
|
List<Role> roles = executeQuery(FETCH_BY_ROLE, new Statement.Initializer() {
|
61
|
@Override
|
62
|
public void prepare(PreparedStatement stmt) throws SQLException {
|
63
|
stmt.setString(1, role);
|
64
|
}
|
65
|
});
|
66
|
|
67
|
if (roles.isEmpty())
|
68
|
return null;
|
69
|
|
70
|
return roles.get(0);
|
71
|
}
|
72
|
|
73
|
public long countAll() throws SQLException
|
74
|
{
|
75
|
return executeCount(COUNT_ALL);
|
76
|
}
|
77
|
|
78
|
|
79
|
public boolean insert(final Role role)
|
80
|
throws SQLException
|
81
|
{
|
82
|
return executeUpdate(INSERT, new Statement.Initializer() {
|
83
|
@Override
|
84
|
public void prepare(PreparedStatement stmt)
|
85
|
throws SQLException {
|
86
|
stmt.setString(1, role.getRole());
|
87
|
}
|
88
|
}) > 0;
|
89
|
|
90
|
}
|
91
|
|
92
|
public boolean delete(final String role)
|
93
|
throws SQLException
|
94
|
{
|
95
|
return executeUpdate(DELETE, new Statement.Initializer() {
|
96
|
@Override
|
97
|
public void prepare(PreparedStatement stmt)
|
98
|
throws SQLException {
|
99
|
stmt.setString(1, role);
|
100
|
}
|
101
|
}) > 0;
|
102
|
}
|
103
|
|
104
|
public boolean update(final Role role)
|
105
|
throws SQLException
|
106
|
{
|
107
|
return executeUpdate(UPDATE, new Statement.Initializer() {
|
108
|
@Override
|
109
|
public void prepare(PreparedStatement stmt)
|
110
|
throws SQLException {
|
111
|
stmt.setString(1, role.getRole());
|
112
|
}
|
113
|
}) > 0;
|
114
|
}
|
115
|
|
116
|
protected Role fromResultSet(ResultSet set)
|
117
|
throws SQLException
|
118
|
{
|
119
|
Role role = new Role(set.getString("role"));
|
120
|
|
121
|
role.setId(set.getInt("id"));
|
122
|
|
123
|
return role;
|
124
|
}
|
125
|
|
126
|
protected int executeUpdate(String sql, Statement.Initializer init)
|
127
|
throws SQLException
|
128
|
{
|
129
|
Connection connection = ds.getConnection();
|
130
|
|
131
|
try {
|
132
|
PreparedStatement stmt = connection.prepareStatement(sql);
|
133
|
init.prepare(stmt);
|
134
|
return stmt.executeUpdate();
|
135
|
} finally {
|
136
|
connection.close();
|
137
|
}
|
138
|
}
|
139
|
|
140
|
protected List<Role> executeQuery(String sql, Statement.Initializer init)
|
141
|
throws SQLException
|
142
|
{
|
143
|
Connection connection = ds.getConnection();
|
144
|
|
145
|
try {
|
146
|
PreparedStatement stmt = connection.prepareStatement(sql);
|
147
|
init.prepare(stmt);
|
148
|
|
149
|
ResultSet set = stmt.executeQuery();
|
150
|
|
151
|
try {
|
152
|
List<Role> results = new ArrayList<>();
|
153
|
|
154
|
while (set.next())
|
155
|
results.add(fromResultSet(set));
|
156
|
|
157
|
return results;
|
158
|
} finally {
|
159
|
set.close();
|
160
|
}
|
161
|
} finally {
|
162
|
connection.close();
|
163
|
}
|
164
|
}
|
165
|
|
166
|
protected int executeUpdate(String sql)
|
167
|
throws SQLException
|
168
|
{
|
169
|
return executeUpdate(sql, Statement.emptyInitializer());
|
170
|
}
|
171
|
|
172
|
protected List<Role> executeQuery(String sql)
|
173
|
throws SQLException
|
174
|
{
|
175
|
return executeQuery(sql, Statement.emptyInitializer());
|
176
|
}
|
177
|
|
178
|
public long executeCount(String sql)
|
179
|
throws SQLException
|
180
|
{
|
181
|
return executeCount(sql, Statement.emptyInitializer());
|
182
|
}
|
183
|
|
184
|
public long executeCount(String sql, Statement.Initializer init)
|
185
|
throws SQLException
|
186
|
{
|
187
|
Connection connection = ds.getConnection();
|
188
|
|
189
|
try {
|
190
|
PreparedStatement stmt = connection.prepareStatement(sql);
|
191
|
init.prepare(stmt);
|
192
|
|
193
|
ResultSet set = stmt.executeQuery();
|
194
|
|
195
|
try {
|
196
|
if (set.next())
|
197
|
return set.getLong(1);
|
198
|
throw new IllegalArgumentException(stmt.toString());
|
199
|
} finally {
|
200
|
set.close();
|
201
|
}
|
202
|
} finally {
|
203
|
connection.close();
|
204
|
}
|
205
|
}
|
206
|
}
|