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