Project

General

Profile

1
package eu.dnetlib.openaire.user.dao;
2

    
3
import eu.dnetlib.openaire.user.pojos.UserVerification;
4
import eu.dnetlib.openaire.user.store.DataSourceConnector;
5
import eu.dnetlib.openaire.user.store.Statement;
6
import org.apache.log4j.Logger;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.stereotype.Component;
9

    
10
import java.sql.*;
11
import java.util.ArrayList;
12
import java.util.List;
13

    
14
import static eu.dnetlib.openaire.user.queries.UserVerificationQueries.*;
15

    
16
/**
17
 * Created by sofia on 3/10/2017.
18
 */
19
@Component(value = "userVertificationDao")
20
public class UserVerificationDAO {
21

    
22
    //private final DataSource ds;
23

    
24
    private static final Logger logger = Logger.getLogger(UserVerificationDAO.class);
25

    
26
    @Autowired
27
    private DataSourceConnector dataSourceConnector;
28

    
29
    public UserVerificationDAO() {
30
        //this.ds = null;
31
       // this.ds = dataSourceConnector.getDatasource();
32
    }
33

    
34
    // FETCH
35

    
36
    public List<UserVerification> fetchAll()
37
            throws SQLException
38
    {
39
        return (List<UserVerification>) executeQuery(FETCH_ALL);
40
    }
41

    
42
    public UserVerification fetchById(final int id)
43
            throws SQLException
44
    {
45
        List<UserVerification> userVerifications = executeQuery(FETCH_BY_ID, new Statement.Initializer() {
46
            @Override
47
            public void prepare(PreparedStatement stmt) throws SQLException {
48
                stmt.setInt(1, id);
49
            }
50
        });
51

    
52
        if (userVerifications.isEmpty())
53
            return null;
54

    
55
        return userVerifications.get(0);
56
    }
57

    
58
    public  UserVerification fetchByUsername(final String username)
59
            throws SQLException
60
    {
61
        //logger.info("Datasource -> " + dataSourceConnector.getDatasource());
62
        List<UserVerification> userVerifications = executeQuery(FETCH_BY_USERNAME, new Statement.Initializer() {
63
            @Override
64
            public void prepare(PreparedStatement stmt) throws SQLException {
65
                stmt.setString(1, username);
66
            }
67
        });
68

    
69
        if (userVerifications.isEmpty())
70
            return null;
71

    
72
        return userVerifications.get(0);
73
    }
74

    
75
    public UserVerification fetchByVerificationCode(final String verificationCode)
76
            throws SQLException
77
    {
78
        List<UserVerification> userVerifications = executeQuery(FETCH_BY_VERIFICATION_CODE, new Statement.Initializer() {
79
            @Override
80
            public void prepare(PreparedStatement stmt) throws SQLException {
81
                stmt.setString(1, verificationCode);
82
            }
83
        });
84

    
85
        if (userVerifications.isEmpty())
86
            return null;
87

    
88
        return userVerifications.get(0);
89
    }
90

    
91
    public UserVerification fetchByDate(final String date)
92
            throws SQLException
93
    {
94
        List<UserVerification> userVerifications = executeQuery(FETCH_BY_DATE, new Statement.Initializer() {
95
            @Override
96
            public void prepare(PreparedStatement stmt) throws SQLException {
97
                stmt.setDate(1, Date.valueOf(date));
98
            }
99
        });
100

    
101
        if (userVerifications.isEmpty())
102
            return null;
103

    
104
        return userVerifications.get(0);
105
    }
106

    
107
    public long countAll() throws SQLException
108
    {
109
        return executeCount(COUNT_ALL);
110
    }
111

    
112
    public boolean insert(final UserVerification userVerification)
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, userVerification.getUsername());
120
                stmt.setString(2, userVerification.getVerificationCode());
121
                stmt.setTimestamp(3, userVerification.getDate());
122
            }
123
        }) > 0;
124

    
125
    }
126

    
127
    public boolean delete(final UserVerification userVerification)
128
            throws SQLException
129
    {
130
        return executeUpdate(DELETE, new Statement.Initializer() {
131
            @Override
132
            public void prepare(PreparedStatement stmt)
133
                    throws SQLException {
134
                stmt.setString(1, userVerification.getUsername());
135
            }
136
        }) > 0;
137
    }
138

    
139
    public boolean update(final UserVerification userVerification)
140
            throws SQLException
141
    {
142
        return executeUpdate(UPDATE, new Statement.Initializer() {
143
            @Override
144
            public void prepare(PreparedStatement stmt)
145
                    throws SQLException {
146
                stmt.setString(1, userVerification.getUsername());
147
                stmt.setString(2, userVerification.getVerificationCode());
148
                stmt.setTimestamp(3, userVerification.getDate());
149
                stmt.setInt(4, userVerification.getId());
150
            }
151
        }) > 0;
152
    }
153

    
154
    protected UserVerification fromResultSet(ResultSet set)
155
            throws SQLException
156
    {
157
        UserVerification userVerification = new UserVerification(set.getString("username"));
158

    
159
        userVerification.setId(set.getInt("id"));
160
        userVerification.setVerificationCode(set.getString("verification_code"));
161
        userVerification.setDate(set.getTimestamp("date"));
162

    
163
        return userVerification;
164
    }
165

    
166
    protected int executeUpdate(String sql, Statement.Initializer init)
167
            throws SQLException
168
    {
169
        Connection connection = dataSourceConnector.getDatasource().getConnection();
170

    
171
        try {
172
            PreparedStatement stmt = connection.prepareStatement(sql);
173
            init.prepare(stmt);
174
            return stmt.executeUpdate();
175
        } finally {
176
            connection.close();
177
        }
178
    }
179

    
180
    protected List<UserVerification> executeQuery(String sql, Statement.Initializer init)
181
            throws SQLException
182
    {
183
        Connection connection = dataSourceConnector.getDatasource().getConnection();
184

    
185
        try {
186
            PreparedStatement stmt = connection.prepareStatement(sql);
187
            init.prepare(stmt);
188

    
189
            ResultSet set = stmt.executeQuery();
190

    
191
            try {
192
                List<UserVerification> results = new ArrayList<>();
193

    
194
                while (set.next())
195
                    results.add(fromResultSet(set));
196

    
197
                return results;
198
            } finally {
199
                set.close();
200
            }
201
        } finally {
202
            connection.close();
203
        }
204
    }
205

    
206
    protected List<UserVerification> executeQuery(String sql)
207
            throws SQLException
208
    {
209
        return executeQuery(sql, Statement.emptyInitializer());
210
    }
211

    
212
    public long executeCount(String sql)
213
            throws SQLException
214
    {
215
        return executeCount(sql, Statement.emptyInitializer());
216
    }
217

    
218
    public long executeCount(String sql, Statement.Initializer init)
219
            throws SQLException
220
    {
221
        Connection connection = dataSourceConnector.getDatasource().getConnection();
222

    
223
        try {
224
            PreparedStatement stmt = connection.prepareStatement(sql);
225
            init.prepare(stmt);
226

    
227
            ResultSet set = stmt.executeQuery();
228

    
229
            try {
230
                if (set.next())
231
                    return set.getLong(1);
232
                throw new IllegalArgumentException(stmt.toString());
233
            } finally {
234
                set.close();
235
            }
236
        } finally {
237
            connection.close();
238
        }
239
    }
240

    
241

    
242
    public DataSourceConnector getDataSourceConnector() {
243
        return dataSourceConnector;
244
    }
245

    
246
    public void setDataSourceConnector(DataSourceConnector dataSourceConnector) {
247
        this.dataSourceConnector = dataSourceConnector;
248
    }
249

    
250
}
(6-6/6)