1
|
package eu.dnetlib.openaire.user.dao;
|
2
|
|
3
|
import eu.dnetlib.openaire.user.UserVerification;
|
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
|
import java.sql.*;
|
10
|
import java.util.ArrayList;
|
11
|
import java.util.List;
|
12
|
|
13
|
import static eu.dnetlib.openaire.user.queries.UserVerificationQueries.*;
|
14
|
|
15
|
/**
|
16
|
* Created by sofia on 3/10/2017.
|
17
|
*/
|
18
|
public class UserVerificationDAO {
|
19
|
|
20
|
private final DataSource ds;
|
21
|
|
22
|
@Autowired
|
23
|
private DataSourceConnector dataSourceConnector;
|
24
|
|
25
|
public UserVerificationDAO() {
|
26
|
this.ds = dataSourceConnector.getDatasource();
|
27
|
}
|
28
|
|
29
|
// FETCH
|
30
|
|
31
|
public List<UserVerification> fetchAll()
|
32
|
throws SQLException
|
33
|
{
|
34
|
return (List<UserVerification>) executeQuery(FETCH_ALL);
|
35
|
}
|
36
|
|
37
|
public UserVerification fetchById(final int id)
|
38
|
throws SQLException
|
39
|
{
|
40
|
List<UserVerification> userVerifications = executeQuery(FETCH_BY_ID, new Statement.Initializer() {
|
41
|
@Override
|
42
|
public void prepare(PreparedStatement stmt) throws SQLException {
|
43
|
stmt.setInt(1, id);
|
44
|
}
|
45
|
});
|
46
|
|
47
|
if (userVerifications.isEmpty())
|
48
|
return null;
|
49
|
|
50
|
return userVerifications.get(0);
|
51
|
}
|
52
|
|
53
|
public UserVerification fetchByUsername(final String username)
|
54
|
throws SQLException
|
55
|
{
|
56
|
List<UserVerification> userVerifications = executeQuery(FETCH_BY_USERNAME, new Statement.Initializer() {
|
57
|
@Override
|
58
|
public void prepare(PreparedStatement stmt) throws SQLException {
|
59
|
stmt.setString(1, username);
|
60
|
}
|
61
|
});
|
62
|
|
63
|
if (userVerifications.isEmpty())
|
64
|
return null;
|
65
|
|
66
|
return userVerifications.get(0);
|
67
|
}
|
68
|
|
69
|
public UserVerification fetchByVerificationCode(final String verificationCode)
|
70
|
throws SQLException
|
71
|
{
|
72
|
List<UserVerification> userVerifications = executeQuery(FETCH_BY_VERIFICATION_CODE, new Statement.Initializer() {
|
73
|
@Override
|
74
|
public void prepare(PreparedStatement stmt) throws SQLException {
|
75
|
stmt.setString(1, verificationCode);
|
76
|
}
|
77
|
});
|
78
|
|
79
|
if (userVerifications.isEmpty())
|
80
|
return null;
|
81
|
|
82
|
return userVerifications.get(0);
|
83
|
}
|
84
|
|
85
|
public UserVerification fetchByDate(final String date)
|
86
|
throws SQLException
|
87
|
{
|
88
|
List<UserVerification> userVerifications = executeQuery(FETCH_BY_DATE, new Statement.Initializer() {
|
89
|
@Override
|
90
|
public void prepare(PreparedStatement stmt) throws SQLException {
|
91
|
stmt.setDate(1, Date.valueOf(date));
|
92
|
}
|
93
|
});
|
94
|
|
95
|
if (userVerifications.isEmpty())
|
96
|
return null;
|
97
|
|
98
|
return userVerifications.get(0);
|
99
|
}
|
100
|
|
101
|
public long countAll() throws SQLException
|
102
|
{
|
103
|
return executeCount(COUNT_ALL);
|
104
|
}
|
105
|
|
106
|
public boolean insert(final UserVerification userVerification)
|
107
|
throws SQLException
|
108
|
{
|
109
|
return executeUpdate(INSERT, new Statement.Initializer() {
|
110
|
@Override
|
111
|
public void prepare(PreparedStatement stmt)
|
112
|
throws SQLException {
|
113
|
stmt.setString(1, userVerification.getUsername());
|
114
|
}
|
115
|
}) > 0;
|
116
|
|
117
|
}
|
118
|
|
119
|
public boolean delete(final UserVerification userVerification)
|
120
|
throws SQLException
|
121
|
{
|
122
|
return executeUpdate(DELETE, new Statement.Initializer() {
|
123
|
@Override
|
124
|
public void prepare(PreparedStatement stmt)
|
125
|
throws SQLException {
|
126
|
stmt.setString(1, userVerification.getUsername());
|
127
|
}
|
128
|
}) > 0;
|
129
|
}
|
130
|
|
131
|
public boolean update(final UserVerification userVerification)
|
132
|
throws SQLException
|
133
|
{
|
134
|
return executeUpdate(UPDATE, new Statement.Initializer() {
|
135
|
@Override
|
136
|
public void prepare(PreparedStatement stmt)
|
137
|
throws SQLException {
|
138
|
stmt.setString(1, userVerification.getUsername());
|
139
|
}
|
140
|
}) > 0;
|
141
|
}
|
142
|
|
143
|
protected UserVerification fromResultSet(ResultSet set)
|
144
|
throws SQLException
|
145
|
{
|
146
|
UserVerification userVerification = new UserVerification(set.getString("username"));
|
147
|
|
148
|
userVerification.setId(set.getString("id"));
|
149
|
|
150
|
return userVerification;
|
151
|
}
|
152
|
|
153
|
protected int executeUpdate(String sql, Statement.Initializer init)
|
154
|
throws SQLException
|
155
|
{
|
156
|
Connection connection = ds.getConnection();
|
157
|
|
158
|
try {
|
159
|
PreparedStatement stmt = connection.prepareStatement(sql);
|
160
|
init.prepare(stmt);
|
161
|
return stmt.executeUpdate();
|
162
|
} finally {
|
163
|
connection.close();
|
164
|
}
|
165
|
}
|
166
|
|
167
|
protected List<UserVerification> executeQuery(String sql, Statement.Initializer init)
|
168
|
throws SQLException
|
169
|
{
|
170
|
Connection connection = ds.getConnection();
|
171
|
|
172
|
try {
|
173
|
PreparedStatement stmt = connection.prepareStatement(sql);
|
174
|
init.prepare(stmt);
|
175
|
|
176
|
ResultSet set = stmt.executeQuery();
|
177
|
|
178
|
try {
|
179
|
List<UserVerification> results = new ArrayList<>();
|
180
|
|
181
|
while (set.next())
|
182
|
results.add(fromResultSet(set));
|
183
|
|
184
|
return results;
|
185
|
} finally {
|
186
|
set.close();
|
187
|
}
|
188
|
} finally {
|
189
|
connection.close();
|
190
|
}
|
191
|
}
|
192
|
|
193
|
protected List<UserVerification> executeQuery(String sql)
|
194
|
throws SQLException
|
195
|
{
|
196
|
return executeQuery(sql, Statement.emptyInitializer());
|
197
|
}
|
198
|
|
199
|
public long executeCount(String sql)
|
200
|
throws SQLException
|
201
|
{
|
202
|
return executeCount(sql, Statement.emptyInitializer());
|
203
|
}
|
204
|
|
205
|
public long executeCount(String sql, Statement.Initializer init)
|
206
|
throws SQLException
|
207
|
{
|
208
|
Connection connection = ds.getConnection();
|
209
|
|
210
|
try {
|
211
|
PreparedStatement stmt = connection.prepareStatement(sql);
|
212
|
init.prepare(stmt);
|
213
|
|
214
|
ResultSet set = stmt.executeQuery();
|
215
|
|
216
|
try {
|
217
|
if (set.next())
|
218
|
return set.getLong(1);
|
219
|
throw new IllegalArgumentException(stmt.toString());
|
220
|
} finally {
|
221
|
set.close();
|
222
|
}
|
223
|
} finally {
|
224
|
connection.close();
|
225
|
}
|
226
|
}
|
227
|
|
228
|
|
229
|
public DataSourceConnector getDataSourceConnector() {
|
230
|
return dataSourceConnector;
|
231
|
}
|
232
|
|
233
|
public void setDataSourceConnector(DataSourceConnector dataSourceConnector) {
|
234
|
this.dataSourceConnector = dataSourceConnector;
|
235
|
}
|
236
|
|
237
|
}
|