Project

General

Profile

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.apache.commons.dbcp.BasicDataSource;
7

    
8
import javax.sql.DataSource;
9
import java.sql.Connection;
10
import java.sql.PreparedStatement;
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.util.ArrayList;
14
import java.util.List;
15

    
16
import static eu.dnetlib.openaire.user.queries.SQLMigrationUserQueries.*;
17

    
18

    
19
/**
20
 * Created by sofia on 1/11/2016.
21
 */
22
public class SQLMigrationUserDAO {
23

    
24
    private final DataSource ds;
25

    
26
    public SQLMigrationUserDAO() {
27
        this.ds = DataSourceConnector.v().getDatasource();
28
    }
29

    
30
    // FETCH
31

    
32
    public List<MigrationUser> fetchAll()
33
            throws SQLException
34
    {
35
        return (List<MigrationUser>) executeQuery(FETCH_ALL);
36
    }
37

    
38
    public MigrationUser fetchById(int id)
39
            throws SQLException
40
    {
41
        List<MigrationUser> users = executeQuery(FETCH_BY_ID);
42

    
43
        if (users.isEmpty())
44
            return null;
45

    
46
        return users.get(0);
47
    }
48

    
49
    public MigrationUser fetchByUsername(final String username)
50
            throws SQLException
51
    {
52
        List<MigrationUser> users = executeQuery(FETCH_BY_USERNAME, new Statement.Initializer() {
53
            @Override
54
            public void prepare(PreparedStatement stmt) throws SQLException {
55
                stmt.setString(1, username);
56
            }
57
        });
58

    
59
        if (users.isEmpty())
60
            return null;
61

    
62
        return users.get(0);
63
    }
64

    
65
    public MigrationUser fetchByEmail(final String email)
66
            throws SQLException
67
    {
68
        List<MigrationUser> users = executeQuery(FETCH_BY_EMAIL, new Statement.Initializer() {
69
            @Override
70
            public void prepare(PreparedStatement stmt) throws SQLException {
71
                stmt.setString(1, email);
72
            }
73
        });
74

    
75
        if (users.isEmpty())
76
            return null;
77
        System.out.println("tralal");
78
        return users.get(0);
79
    }
80

    
81
    public List<MigrationUser> fetchByRoleId(String roleId)
82
            throws SQLException
83
    {
84
        return executeQuery(FETCH_BY_ROLE_ID);
85
    }
86

    
87

    
88
    public long countAll() throws SQLException
89
    {
90
        return executeCount(COUNT_ALL);
91
    }
92

    
93

    
94
    public boolean insert(final MigrationUser mUser)
95
            throws SQLException
96
    {
97
        return executeUpdate(INSERT, new Statement.Initializer() {
98
            @Override
99
            public void prepare(PreparedStatement stmt)
100
                    throws SQLException {
101
                stmt.setString(1, mUser.getUsername());
102
                stmt.setString(2, mUser.getFullname());
103
                stmt.setString(3, mUser.getEmail());
104
                stmt.setInt(4, mUser.getRoleId());
105
            }
106
        }) > 0;
107

    
108
    }
109

    
110
    public boolean delete(final String username)
111
            throws SQLException
112
    {
113
        return executeUpdate(DELETE, new Statement.Initializer() {
114
            @Override
115
            public void prepare(PreparedStatement stmt)
116
                    throws SQLException {
117
                stmt.setString(1, username);
118
            }
119
        }) > 0;
120
    }
121

    
122

    
123
    public boolean update(final MigrationUser mUser)
124
            throws SQLException
125
    {
126
        return executeUpdate(UPDATE, new Statement.Initializer() {
127
            @Override
128
            public void prepare(PreparedStatement stmt)
129
                    throws SQLException {
130
                stmt.setString(1, mUser.getUsername());
131
                stmt.setString(2, mUser.getFullname());
132
                stmt.setString(3, mUser.getEmail());
133
                stmt.setInt(4, mUser.getRoleId());
134
            }
135
        }) > 0;
136
    }
137

    
138

    
139
    protected MigrationUser fromResultSet(ResultSet set)
140
            throws SQLException
141
    {
142
        MigrationUser mUser = new MigrationUser(set.getString("username"));
143

    
144
        mUser.setId(set.getInt("id"));
145
        mUser.setEmail(set.getString("fullname"));
146
        mUser.setEmail(set.getString("email"));
147
        mUser.setRoleId(set.getInt("role_id"));
148

    
149
        return mUser;
150
    }
151

    
152
    protected int executeUpdate(String sql, Statement.Initializer init)
153
            throws SQLException
154
    {
155
        Connection connection = ds.getConnection();
156

    
157
        try {
158
            PreparedStatement stmt = connection.prepareStatement(sql);
159
            init.prepare(stmt);
160
            return stmt.executeUpdate();
161
        } finally {
162
            connection.close();
163
        }
164
    }
165

    
166

    
167
    protected List<MigrationUser> 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<MigrationUser> 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 int executeUpdate(String sql)
194
            throws SQLException
195
    {
196
        return executeUpdate(sql, Statement.emptyInitializer());
197
    }
198

    
199
    protected List<MigrationUser> executeQuery(String sql)
200
            throws SQLException
201
    {
202
        return executeQuery(sql, Statement.emptyInitializer());
203
    }
204

    
205
    public long executeCount(String sql)
206
            throws SQLException
207
    {
208
        return executeCount(sql, Statement.emptyInitializer());
209
    }
210

    
211
    public long executeCount(String sql, Statement.Initializer init)
212
            throws SQLException
213
    {
214
        Connection connection = ds.getConnection();
215

    
216
        try {
217
            PreparedStatement stmt = connection.prepareStatement(sql);
218
            init.prepare(stmt);
219

    
220
            ResultSet set = stmt.executeQuery();
221

    
222
            try {
223
                if (set.next())
224
                    return set.getLong(1);
225
                throw new IllegalArgumentException(stmt.toString());
226
            } finally {
227
                set.close();
228
            }
229
        } finally {
230
            connection.close();
231
        }
232
    }
233
}
(3-3/5)