Project

General

Profile

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

    
3
import eu.dnetlib.openaire.user.pojos.migration.MigrationUser;
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
import org.springframework.stereotype.Component;
8

    
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
@Component(value = "sqlMigrationUserDAO")
23
public class SQLMigrationUserDAO {
24

    
25
    @Autowired
26
    private DataSourceConnector dataSourceConnector;
27

    
28
    // FETCH
29
    public List<MigrationUser> fetchAll()
30
            throws SQLException
31
    {
32
        return (List<MigrationUser>) executeQuery(FETCH_ALL);
33
    }
34

    
35
    public MigrationUser fetchById(final int id)
36
            throws SQLException {
37
        List<MigrationUser> users = executeQuery(FETCH_BY_ID, new Statement.Initializer() {
38
            @Override
39
            public void prepare(PreparedStatement stmt) throws SQLException {
40
                stmt.setInt(1, id);
41
            }
42
        });
43

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

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

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

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

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

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

    
76
        if (users.isEmpty())
77
            return null;
78

    
79
        return users.get(0);
80
    }
81

    
82
    public MigrationUser fetchByEmail(final String email)
83
            throws SQLException
84
    {
85
        List<MigrationUser> users = executeQuery(FETCH_BY_EMAIL, new Statement.Initializer() {
86
            @Override
87
            public void prepare(PreparedStatement stmt) throws SQLException {
88
                stmt.setString(1, email);
89
            }
90
        });
91

    
92
        if (users.isEmpty())
93
            return null;
94

    
95
        return users.get(0);
96
    }
97

    
98
    public List<MigrationUser> fetchByRoleId(String roleId)
99
            throws SQLException
100
    {
101
        return executeQuery(FETCH_BY_ROLE_ID);
102
    }
103

    
104

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

    
110

    
111
    public boolean insert(final MigrationUser mUser)
112
            throws SQLException
113
    {
114
        return executeUpdate(INSERT, new Statement.Initializer() {
115
            @Override
116
            public void prepare(PreparedStatement stmt)
117
                    throws SQLException {
118
                stmt.setString(1, mUser.getUsername());
119
                stmt.setString(2, mUser.getFullname());
120
                stmt.setString(3, mUser.getEmail());
121
                stmt.setInt(4, mUser.getRoleId());
122
            }
123
        }) > 0;
124

    
125
    }
126

    
127
    public boolean delete(final String username)
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, username);
135
            }
136
        }) > 0;
137
    }
138

    
139

    
140
    public boolean update(final MigrationUser mUser)
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, mUser.getUsername());
148
                stmt.setString(2, mUser.getFullname());
149
                stmt.setString(3, mUser.getEmail());
150
                stmt.setInt(4, mUser.getRoleId());
151
                stmt.setInt(5, mUser.getId());
152
            }
153
        }) > 0;
154
    }
155

    
156

    
157
    protected MigrationUser fromResultSet(ResultSet set)
158
            throws SQLException
159
    {
160
        MigrationUser mUser = new MigrationUser(set.getString("username"));
161

    
162
        mUser.setId(set.getInt("id"));
163
        mUser.setFullname(set.getString("fullname"));
164
        mUser.setEmail(set.getString("email"));
165
        mUser.setRoleId(set.getInt("role_id"));
166

    
167
        return mUser;
168
    }
169

    
170
    protected int executeUpdate(String sql, Statement.Initializer init)
171
            throws SQLException
172
    {
173
        Connection connection = dataSourceConnector.getConnection();
174

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

    
184

    
185
    protected List<MigrationUser> executeQuery(String sql, Statement.Initializer init)
186
            throws SQLException {
187
        Connection connection = dataSourceConnector.getConnection();
188

    
189
        try {
190
            PreparedStatement stmt = connection.prepareStatement(sql);
191
            init.prepare(stmt);
192

    
193
            ResultSet set = stmt.executeQuery();
194

    
195
            try {
196
                List<MigrationUser> results = new ArrayList<>();
197

    
198
                while (set.next())
199
                    results.add(fromResultSet(set));
200

    
201
                return results;
202
            } finally {
203
                set.close();
204
            }
205
        } finally {
206
            connection.close();
207
        }
208
    }
209

    
210
    protected int executeUpdate(String sql)
211
            throws SQLException
212
    {
213
        return executeUpdate(sql, Statement.emptyInitializer());
214
    }
215

    
216
    protected List<MigrationUser> executeQuery(String sql)
217
            throws SQLException
218
    {
219
        return executeQuery(sql, Statement.emptyInitializer());
220
    }
221

    
222
    public long executeCount(String sql)
223
            throws SQLException
224
    {
225
        return executeCount(sql, Statement.emptyInitializer());
226
    }
227

    
228
    public long executeCount(String sql, Statement.Initializer init)
229
            throws SQLException
230
    {
231
        Connection connection = dataSourceConnector.getConnection();
232

    
233
        try {
234
            PreparedStatement stmt = connection.prepareStatement(sql);
235
            init.prepare(stmt);
236

    
237
            ResultSet set = stmt.executeQuery();
238

    
239
            try {
240
                if (set.next())
241
                    return set.getLong(1);
242
                throw new IllegalArgumentException(stmt.toString());
243
            } finally {
244
                set.close();
245
            }
246
        } finally {
247
            connection.close();
248
        }
249
    }
250

    
251
    public DataSourceConnector getDataSourceConnector() {
252
        return dataSourceConnector;
253
    }
254

    
255
    public void setDataSourceConnector(DataSourceConnector dataSourceConnector) {
256
        this.dataSourceConnector = dataSourceConnector;
257
    }
258
}
(3-3/6)