Project

General

Profile

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.apache.log4j.Logger;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.stereotype.Component;
9

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

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

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

    
23
    //private final DataSource ds;
24

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

    
27
    @Autowired
28
    private DataSourceConnector dataSourceConnector;
29

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

    
35
    // FETCH
36

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
113
    public boolean insert(final UserVerification userVerification)
114
            throws SQLException
115
    {
116
        return executeUpdate(INSERT, new Statement.Initializer() {
117
            @Override
118
            public void prepare(PreparedStatement stmt)
119
                    throws SQLException {
120
                stmt.setString(1, userVerification.getUsername());
121
                stmt.setString(2, userVerification.getVerificationCode());
122
                stmt.setTimestamp(3, userVerification.getDate());
123
            }
124
        }) > 0;
125

    
126
    }
127

    
128
    public boolean delete(final UserVerification userVerification)
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, userVerification.getUsername());
136
            }
137
        }) > 0;
138
    }
139

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

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

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

    
164
        return userVerification;
165
    }
166

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

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

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

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

    
190
            ResultSet set = stmt.executeQuery();
191

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

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

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

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

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

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

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

    
228
            ResultSet set = stmt.executeQuery();
229

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

    
242

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

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

    
251
}
(6-6/6)