| 1 | package com.popx.persistenza; | |
| 2 | ||
| 3 | import com.popx.modello.UserBean; | |
| 4 | ||
| 5 | import javax.sql.DataSource; | |
| 6 | import java.sql.*; | |
| 7 | ||
| 8 | public class UserDAOImpl implements UserDAO<UserBean> { | |
| 9 | ||
| 10 | private DataSource ds; | |
| 11 | ||
| 12 | /*@ | |
| 13 | @ ensures this.ds != null; | |
| 14 | @*/ | |
| 15 | public UserDAOImpl() { | |
| 16 | this.ds = DataSourceSingleton.getInstance(); | |
| 17 | } | |
| 18 | ||
| 19 | /*@ | |
| 20 | @ also | |
| 21 | @ public normal_behavior | |
| 22 | @ requires email != null && !email.isEmpty(); | |
| 23 | @ ensures \result == null | |
| 24 | @ || \result.getEmail().equals(email); | |
| 25 | @ signals (SQLException) true; | |
| 26 | @*/ | |
| 27 | @Override | |
| 28 | public UserBean getUserByEmail(String email) throws SQLException { | |
| 29 | String query = "SELECT * FROM UtenteRegistrato WHERE email = ?"; | |
| 30 | try (Connection conn = ds.getConnection(); | |
| 31 | PreparedStatement stmt = conn.prepareStatement(query)) { | |
| 32 |
1
1. getUserByEmail : removed call to java/sql/PreparedStatement::setString → KILLED |
stmt.setString(1, email); |
| 33 | ResultSet rs = stmt.executeQuery(); | |
| 34 |
1
1. getUserByEmail : negated conditional → KILLED |
if (rs.next()) { |
| 35 | return new UserBean( | |
| 36 | rs.getString("username"), | |
| 37 | rs.getString("email"), | |
| 38 | rs.getString("password"), | |
| 39 | rs.getString("role") | |
| 40 | ); | |
| 41 | } | |
| 42 | } | |
| 43 | return null; | |
| 44 | } | |
| 45 | ||
| 46 | /*@ | |
| 47 | @ also | |
| 48 | @ public normal_behavior | |
| 49 | @ requires user != null; | |
| 50 | @ requires user.getEmail() != null && !user.getEmail().isEmpty(); | |
| 51 | @ ensures \result == true || \result == false; | |
| 52 | @ signals (SQLException) true; | |
| 53 | @*/ | |
| 54 | @Override | |
| 55 | public boolean saveUser(UserBean user) throws SQLException { | |
| 56 | String userQuery = "INSERT INTO UtenteRegistrato (username, email, password, role) VALUES (?, ?, ?, ?)"; | |
| 57 | String clienteQuery = "INSERT INTO Cliente (utente_registrato_email) VALUES (?)"; | |
| 58 | ||
| 59 | try (Connection conn = ds.getConnection()) { | |
| 60 |
1
1. saveUser : removed call to java/sql/Connection::setAutoCommit → SURVIVED |
conn.setAutoCommit(false); |
| 61 | ||
| 62 | try (PreparedStatement userStmt = conn.prepareStatement(userQuery); | |
| 63 | PreparedStatement clienteStmt = conn.prepareStatement(clienteQuery)) { | |
| 64 | ||
| 65 |
1
1. saveUser : removed call to java/sql/PreparedStatement::setString → KILLED |
userStmt.setString(1, user.getUsername()); |
| 66 |
1
1. saveUser : removed call to java/sql/PreparedStatement::setString → KILLED |
userStmt.setString(2, user.getEmail()); |
| 67 |
1
1. saveUser : removed call to java/sql/PreparedStatement::setString → KILLED |
userStmt.setString(3, com.popx.servizio.SecurityService.hashPassword(user.getPassword())); |
| 68 |
1
1. saveUser : removed call to java/sql/PreparedStatement::setString → KILLED |
userStmt.setString(4, "User"); |
| 69 | userStmt.executeUpdate(); | |
| 70 | ||
| 71 |
1
1. saveUser : removed call to java/sql/PreparedStatement::setString → KILLED |
clienteStmt.setString(1, user.getEmail()); |
| 72 | clienteStmt.executeUpdate(); | |
| 73 | ||
| 74 |
1
1. saveUser : removed call to java/sql/Connection::commit → KILLED |
conn.commit(); |
| 75 | return true; | |
| 76 | ||
| 77 | } catch (SQLException e) { | |
| 78 | conn.rollback(); | |
| 79 | throw e; | |
| 80 | } | |
| 81 | } | |
| 82 | } | |
| 83 | ||
| 84 | } | |
Mutations | ||
| 32 |
1.1 |
|
| 34 |
1.1 |
|
| 60 |
1.1 |
|
| 65 |
1.1 |
|
| 66 |
1.1 |
|
| 67 |
1.1 |
|
| 68 |
1.1 |
|
| 71 |
1.1 |
|
| 74 |
1.1 |