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

    
162
        return userVerification;
163
    }
164

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

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

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

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

    
188
            ResultSet set = stmt.executeQuery();
189

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

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

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

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

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

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

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

    
226
            ResultSet set = stmt.executeQuery();
227

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

    
240

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

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

    
249
}
(6-6/6)