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