1
|
package eu.dnetlib.openaire.user.dao;
|
2
|
|
3
|
import eu.dnetlib.openaire.user.MigrationUser;
|
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
|
import org.springframework.beans.factory.annotation.Value;
|
8
|
import org.springframework.stereotype.Component;
|
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.SQLMigrationUserQueries.*;
|
18
|
|
19
|
|
20
|
/**
|
21
|
* Created by sofia on 1/11/2016.
|
22
|
*/
|
23
|
@Component(value = "sqlMigrationUserDAO")
|
24
|
public class SQLMigrationUserDAO {
|
25
|
|
26
|
@Autowired
|
27
|
private DataSourceConnector dataSourceConnector;
|
28
|
|
29
|
// FETCH
|
30
|
public List<MigrationUser> fetchAll()
|
31
|
throws SQLException
|
32
|
{
|
33
|
return (List<MigrationUser>) executeQuery(FETCH_ALL);
|
34
|
}
|
35
|
|
36
|
public MigrationUser fetchById(final int id)
|
37
|
throws SQLException {
|
38
|
List<MigrationUser> users = executeQuery(FETCH_BY_ID, new Statement.Initializer() {
|
39
|
@Override
|
40
|
public void prepare(PreparedStatement stmt) throws SQLException {
|
41
|
stmt.setInt(1, id);
|
42
|
}
|
43
|
});
|
44
|
|
45
|
if (users.isEmpty())
|
46
|
return null;
|
47
|
|
48
|
return users.get(0);
|
49
|
}
|
50
|
|
51
|
public MigrationUser fetchByUsername(final String username)
|
52
|
throws SQLException
|
53
|
{
|
54
|
List<MigrationUser> users = executeQuery(FETCH_BY_USERNAME, new Statement.Initializer() {
|
55
|
@Override
|
56
|
public void prepare(PreparedStatement stmt) throws SQLException {
|
57
|
stmt.setString(1, username);
|
58
|
}
|
59
|
});
|
60
|
|
61
|
if (users.isEmpty())
|
62
|
return null;
|
63
|
|
64
|
return users.get(0);
|
65
|
}
|
66
|
|
67
|
public MigrationUser fetchByFullname(final String fullname)
|
68
|
throws SQLException
|
69
|
{
|
70
|
List<MigrationUser> users = executeQuery(FETCH_BY_FULLNAME, new Statement.Initializer() {
|
71
|
@Override
|
72
|
public void prepare(PreparedStatement stmt) throws SQLException {
|
73
|
stmt.setString(1, fullname);
|
74
|
}
|
75
|
});
|
76
|
|
77
|
if (users.isEmpty())
|
78
|
return null;
|
79
|
|
80
|
return users.get(0);
|
81
|
}
|
82
|
|
83
|
public MigrationUser fetchByEmail(final String email)
|
84
|
throws SQLException
|
85
|
{
|
86
|
List<MigrationUser> users = executeQuery(FETCH_BY_EMAIL, new Statement.Initializer() {
|
87
|
@Override
|
88
|
public void prepare(PreparedStatement stmt) throws SQLException {
|
89
|
stmt.setString(1, email);
|
90
|
}
|
91
|
});
|
92
|
|
93
|
if (users.isEmpty())
|
94
|
return null;
|
95
|
|
96
|
return users.get(0);
|
97
|
}
|
98
|
|
99
|
public List<MigrationUser> fetchByRoleId(String roleId)
|
100
|
throws SQLException
|
101
|
{
|
102
|
return executeQuery(FETCH_BY_ROLE_ID);
|
103
|
}
|
104
|
|
105
|
|
106
|
public long countAll() throws SQLException
|
107
|
{
|
108
|
return executeCount(COUNT_ALL);
|
109
|
}
|
110
|
|
111
|
|
112
|
public boolean insert(final MigrationUser mUser)
|
113
|
throws SQLException
|
114
|
{
|
115
|
return executeUpdate(INSERT, new Statement.Initializer() {
|
116
|
@Override
|
117
|
public void prepare(PreparedStatement stmt)
|
118
|
throws SQLException {
|
119
|
stmt.setString(1, mUser.getUsername());
|
120
|
stmt.setString(2, mUser.getFullname());
|
121
|
stmt.setString(3, mUser.getEmail());
|
122
|
stmt.setInt(4, mUser.getRoleId());
|
123
|
}
|
124
|
}) > 0;
|
125
|
|
126
|
}
|
127
|
|
128
|
public boolean delete(final String username)
|
129
|
throws SQLException
|
130
|
{
|
131
|
return executeUpdate(DELETE, new Statement.Initializer() {
|
132
|
@Override
|
133
|
public void prepare(PreparedStatement stmt)
|
134
|
throws SQLException {
|
135
|
stmt.setString(1, username);
|
136
|
}
|
137
|
}) > 0;
|
138
|
}
|
139
|
|
140
|
|
141
|
public boolean update(final MigrationUser mUser)
|
142
|
throws SQLException
|
143
|
{
|
144
|
return executeUpdate(UPDATE, new Statement.Initializer() {
|
145
|
@Override
|
146
|
public void prepare(PreparedStatement stmt)
|
147
|
throws SQLException {
|
148
|
stmt.setString(1, mUser.getUsername());
|
149
|
stmt.setString(2, mUser.getFullname());
|
150
|
stmt.setString(3, mUser.getEmail());
|
151
|
stmt.setInt(4, mUser.getRoleId());
|
152
|
stmt.setInt(5, mUser.getId());
|
153
|
}
|
154
|
}) > 0;
|
155
|
}
|
156
|
|
157
|
|
158
|
protected MigrationUser fromResultSet(ResultSet set)
|
159
|
throws SQLException
|
160
|
{
|
161
|
MigrationUser mUser = new MigrationUser(set.getString("username"));
|
162
|
|
163
|
mUser.setId(set.getInt("id"));
|
164
|
mUser.setFullname(set.getString("fullname"));
|
165
|
mUser.setEmail(set.getString("email"));
|
166
|
mUser.setRoleId(set.getInt("role_id"));
|
167
|
|
168
|
return mUser;
|
169
|
}
|
170
|
|
171
|
protected int executeUpdate(String sql, Statement.Initializer init)
|
172
|
throws SQLException
|
173
|
{
|
174
|
Connection connection = dataSourceConnector.getConnection();
|
175
|
|
176
|
try {
|
177
|
PreparedStatement stmt = connection.prepareStatement(sql);
|
178
|
init.prepare(stmt);
|
179
|
return stmt.executeUpdate();
|
180
|
} finally {
|
181
|
connection.close();
|
182
|
}
|
183
|
}
|
184
|
|
185
|
|
186
|
protected List<MigrationUser> executeQuery(String sql, Statement.Initializer init)
|
187
|
throws SQLException {
|
188
|
Connection connection = dataSourceConnector.getConnection();
|
189
|
|
190
|
try {
|
191
|
PreparedStatement stmt = connection.prepareStatement(sql);
|
192
|
init.prepare(stmt);
|
193
|
|
194
|
ResultSet set = stmt.executeQuery();
|
195
|
|
196
|
try {
|
197
|
List<MigrationUser> results = new ArrayList<>();
|
198
|
|
199
|
while (set.next())
|
200
|
results.add(fromResultSet(set));
|
201
|
|
202
|
return results;
|
203
|
} finally {
|
204
|
set.close();
|
205
|
}
|
206
|
} finally {
|
207
|
connection.close();
|
208
|
}
|
209
|
}
|
210
|
|
211
|
protected int executeUpdate(String sql)
|
212
|
throws SQLException
|
213
|
{
|
214
|
return executeUpdate(sql, Statement.emptyInitializer());
|
215
|
}
|
216
|
|
217
|
protected List<MigrationUser> executeQuery(String sql)
|
218
|
throws SQLException
|
219
|
{
|
220
|
return executeQuery(sql, Statement.emptyInitializer());
|
221
|
}
|
222
|
|
223
|
public long executeCount(String sql)
|
224
|
throws SQLException
|
225
|
{
|
226
|
return executeCount(sql, Statement.emptyInitializer());
|
227
|
}
|
228
|
|
229
|
public long executeCount(String sql, Statement.Initializer init)
|
230
|
throws SQLException
|
231
|
{
|
232
|
Connection connection = dataSourceConnector.getConnection();
|
233
|
|
234
|
try {
|
235
|
PreparedStatement stmt = connection.prepareStatement(sql);
|
236
|
init.prepare(stmt);
|
237
|
|
238
|
ResultSet set = stmt.executeQuery();
|
239
|
|
240
|
try {
|
241
|
if (set.next())
|
242
|
return set.getLong(1);
|
243
|
throw new IllegalArgumentException(stmt.toString());
|
244
|
} finally {
|
245
|
set.close();
|
246
|
}
|
247
|
} finally {
|
248
|
connection.close();
|
249
|
}
|
250
|
}
|
251
|
|
252
|
public DataSourceConnector getDataSourceConnector() {
|
253
|
return dataSourceConnector;
|
254
|
}
|
255
|
|
256
|
public void setDataSourceConnector(DataSourceConnector dataSourceConnector) {
|
257
|
this.dataSourceConnector = dataSourceConnector;
|
258
|
}
|
259
|
}
|