| 1 | package com.popx.persistenza; | |
| 2 | ||
| 3 | import com.popx.modello.CarrelloBean; | |
| 4 | import com.popx.modello.ProdottoCarrelloBean; | |
| 5 | import javax.sql.DataSource; | |
| 6 | import java.sql.*; | |
| 7 | import java.util.*; | |
| 8 | ||
| 9 | public class CarrelloDAOImpl implements CarrelloDAO { | |
| 10 | private DataSource ds; | |
| 11 | ||
| 12 | /*@ public model boolean available; | |
| 13 | @ public invariant ds != null && available; | |
| 14 | @ represents available <- ds != null; | |
| 15 | @*/ | |
| 16 | ||
| 17 | public CarrelloDAOImpl() { | |
| 18 | this.ds = DataSourceSingleton.getInstance(); | |
| 19 | } | |
| 20 | ||
| 21 | ||
| 22 | /*@ public normal_behavior | |
| 23 | @ requires carrello != null | |
| 24 | @ && carrello.getClienteEmail() != null && !carrello.getClienteEmail().isEmpty() | |
| 25 | @ && carrello.getProdottiCarrello() != null | |
| 26 | @ && (\forall int i; 0 <= i && i < carrello.getProdottiCarrello().size(); | |
| 27 | @ carrello.getProdottiCarrello().get(i) != null | |
| 28 | @ && carrello.getProdottiCarrello().get(i).getProdottoId() != null | |
| 29 | @ && !carrello.getProdottiCarrello().get(i).getProdottoId().isEmpty() | |
| 30 | @ && carrello.getProdottiCarrello().get(i).getQuantity() >= 0 | |
| 31 | @ && carrello.getProdottiCarrello().get(i).getUnitaryCost() >= 0); | |
| 32 | @ assignable \everything; | |
| 33 | @ ensures available; | |
| 34 | @*/ | |
| 35 | @Override | |
| 36 | public void salvaCarrello(CarrelloBean carrello) { | |
| 37 | String upsertCart = | |
| 38 | "INSERT INTO Carrello (cliente_email) VALUES (?) " + | |
| 39 | "ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)"; | |
| 40 | ||
| 41 | String insertProdottoCarrello = | |
| 42 | "INSERT INTO ProdottoCarrello (carrello_id, prodotto_id, quantity, unitary_cost) " + | |
| 43 | "VALUES (?, ?, ?, ?) " + | |
| 44 | "ON DUPLICATE KEY UPDATE quantity = VALUES(quantity), unitary_cost = VALUES(unitary_cost)"; | |
| 45 | ||
| 46 | try (Connection connection = ds.getConnection()) { | |
| 47 |
1
1. salvaCarrello : removed call to java/sql/Connection::setAutoCommit → SURVIVED |
connection.setAutoCommit(false); |
| 48 | ||
| 49 | int carrelloId; | |
| 50 | ||
| 51 | // 1) Inserisci (o recupera) il carrello e ottieni l'id | |
| 52 | try (PreparedStatement psCart = connection.prepareStatement(upsertCart, Statement.RETURN_GENERATED_KEYS)) { | |
| 53 |
1
1. salvaCarrello : removed call to java/sql/PreparedStatement::setString → KILLED |
psCart.setString(1, carrello.getClienteEmail()); |
| 54 | psCart.executeUpdate(); | |
| 55 | ||
| 56 | try (ResultSet keys = psCart.getGeneratedKeys()) { | |
| 57 |
1
1. salvaCarrello : negated conditional → KILLED |
if (keys.next()) { |
| 58 | carrelloId = keys.getInt(1); | |
| 59 | } else { | |
| 60 | throw new SQLException("Impossibile ottenere l'ID del carrello."); | |
| 61 | } | |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 65 | // 2) Inserisci/aggiorna prodotti del carrello (batch) | |
| 66 | try (PreparedStatement psProd = connection.prepareStatement(insertProdottoCarrello)) { | |
| 67 | for (ProdottoCarrelloBean prodotto : carrello.getProdottiCarrello()) { | |
| 68 | psProd.setInt(1, carrelloId); | |
| 69 | psProd.setString(2, prodotto.getProdottoId()); | |
| 70 | psProd.setInt(3, prodotto.getQuantity()); | |
| 71 | psProd.setFloat(4, prodotto.getUnitaryCost()); | |
| 72 | psProd.addBatch(); | |
| 73 | } | |
| 74 | psProd.executeBatch(); | |
| 75 | } | |
| 76 | ||
| 77 | connection.commit(); | |
| 78 | ||
| 79 | } catch (SQLException e) { | |
| 80 |
1
1. salvaCarrello : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
e.printStackTrace(); |
| 81 | } | |
| 82 | } | |
| 83 | ||
| 84 | ||
| 85 | @Override | |
| 86 | /*@ public normal_behavior | |
| 87 | @ requires email != null && !email.isEmpty(); | |
| 88 | @ assignable \everything; | |
| 89 | @ ensures \result != null | |
| 90 | @ && \result.getClienteEmail().equals(email) | |
| 91 | @ && \result.getProdottiCarrello() != null | |
| 92 | @ && (\forall int i; 0 <= i && i < \result.getProdottiCarrello().size(); | |
| 93 | @ \result.getProdottiCarrello().get(i) != null | |
| 94 | @ && \result.getProdottiCarrello().get(i).getProdottoId() != null | |
| 95 | @ && !\result.getProdottiCarrello().get(i).getProdottoId().isEmpty() | |
| 96 | @ && \result.getProdottiCarrello().get(i).getQuantity() >= 0 | |
| 97 | @ && \result.getProdottiCarrello().get(i).getUnitaryCost() >= 0); | |
| 98 | @*/ | |
| 99 | public CarrelloBean ottieniCarrelloPerEmail(String email) { | |
| 100 | String queryCarrello = "SELECT * FROM Carrello WHERE cliente_email = ?"; | |
| 101 | String queryProdotti = "SELECT * FROM ProdottoCarrello WHERE carrello_id = ?"; | |
| 102 | ||
| 103 | CarrelloBean carrello = null; | |
| 104 | List<ProdottoCarrelloBean> prodottiCarrello = new ArrayList<>(); | |
| 105 | ||
| 106 | try (Connection connection = ds.getConnection()) { | |
| 107 | // Recupera il carrello | |
| 108 | try (PreparedStatement psCarrello = connection.prepareStatement(queryCarrello)) { | |
| 109 |
1
1. ottieniCarrelloPerEmail : removed call to java/sql/PreparedStatement::setString → KILLED |
psCarrello.setString(1, email); |
| 110 | ResultSet rsCarrello = psCarrello.executeQuery(); | |
| 111 | ||
| 112 |
1
1. ottieniCarrelloPerEmail : negated conditional → KILLED |
if (rsCarrello.next()) { |
| 113 | // Recupera l'ID del carrello | |
| 114 | int carrelloId = rsCarrello.getInt("id"); | |
| 115 | ||
| 116 | // Recupera i prodotti associati al carrello | |
| 117 | try (PreparedStatement psProdotti = connection.prepareStatement(queryProdotti)) { | |
| 118 |
1
1. ottieniCarrelloPerEmail : removed call to java/sql/PreparedStatement::setInt → KILLED |
psProdotti.setInt(1, carrelloId); |
| 119 | ResultSet rsProdotti = psProdotti.executeQuery(); | |
| 120 | ||
| 121 |
1
1. ottieniCarrelloPerEmail : negated conditional → KILLED |
while (rsProdotti.next()) { |
| 122 | ProdottoCarrelloBean prodotto = new ProdottoCarrelloBean( | |
| 123 | email, | |
| 124 | rsProdotti.getString("prodotto_id"), | |
| 125 | rsProdotti.getInt("quantity"), | |
| 126 | rsProdotti.getFloat("unitary_cost") | |
| 127 | ); | |
| 128 | prodottiCarrello.add(prodotto); | |
| 129 | } | |
| 130 | } | |
| 131 | ||
| 132 | // Crea il carrello | |
| 133 | carrello = new CarrelloBean(email, prodottiCarrello); | |
| 134 | } | |
| 135 | } | |
| 136 | } catch (SQLException e) { | |
| 137 |
1
1. ottieniCarrelloPerEmail : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
e.printStackTrace(); |
| 138 | } | |
| 139 | ||
| 140 |
2
1. ottieniCarrelloPerEmail : replaced return value with null for com/popx/persistenza/CarrelloDAOImpl::ottieniCarrelloPerEmail → KILLED 2. ottieniCarrelloPerEmail : negated conditional → KILLED |
return carrello != null ? carrello : new CarrelloBean(email, prodottiCarrello); |
| 141 | } | |
| 142 | ||
| 143 | @Override | |
| 144 | /*@ public normal_behavior | |
| 145 | @ requires email != null && !email.isEmpty(); | |
| 146 | @ assignable \everything; | |
| 147 | @ ensures available; | |
| 148 | @*/ | |
| 149 | public void clearCartByUserEmail(String email) { | |
| 150 | String queryProdottoCarrello = "DELETE FROM ProdottoCarrello WHERE carrello_id = (SELECT id FROM Carrello WHERE cliente_email = ?)"; | |
| 151 | String queryCarrello = "DELETE FROM Carrello WHERE cliente_email = ?"; | |
| 152 | Connection connection = null; | |
| 153 | ||
| 154 | try { | |
| 155 | connection = ds.getConnection(); | |
| 156 | ||
| 157 | // Avvia una transazione | |
| 158 |
1
1. clearCartByUserEmail : removed call to java/sql/Connection::setAutoCommit → SURVIVED |
connection.setAutoCommit(false); |
| 159 | ||
| 160 | try (PreparedStatement psProdottoCarrello = connection.prepareStatement(queryProdottoCarrello)) { | |
| 161 |
1
1. clearCartByUserEmail : removed call to java/sql/PreparedStatement::setString → KILLED |
psProdottoCarrello.setString(1, email); |
| 162 | psProdottoCarrello.executeUpdate(); | |
| 163 | } | |
| 164 | ||
| 165 | try (PreparedStatement psCarrello = connection.prepareStatement(queryCarrello)) { | |
| 166 | psCarrello.setString(1, email); | |
| 167 | psCarrello.executeUpdate(); | |
| 168 | } | |
| 169 | ||
| 170 | // Conferma la transazione | |
| 171 |
1
1. clearCartByUserEmail : removed call to java/sql/Connection::commit → SURVIVED |
connection.commit(); |
| 172 | ||
| 173 | } catch (SQLException e) { | |
| 174 |
1
1. clearCartByUserEmail : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
e.printStackTrace(); |
| 175 | try { | |
| 176 | // In caso di errore, esegui il rollback | |
| 177 |
1
1. clearCartByUserEmail : negated conditional → NO_COVERAGE |
if (connection != null) { |
| 178 |
1
1. clearCartByUserEmail : removed call to java/sql/Connection::rollback → NO_COVERAGE |
connection.rollback(); |
| 179 | } | |
| 180 | } catch (SQLException rollbackEx) { | |
| 181 |
1
1. clearCartByUserEmail : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
rollbackEx.printStackTrace(); |
| 182 | } | |
| 183 | } finally { | |
| 184 | try { | |
| 185 | // Ripristina l'auto-commit | |
| 186 |
1
1. clearCartByUserEmail : negated conditional → SURVIVED |
if (connection != null) { |
| 187 |
1
1. clearCartByUserEmail : removed call to java/sql/Connection::setAutoCommit → SURVIVED |
connection.setAutoCommit(true); |
| 188 |
1
1. clearCartByUserEmail : removed call to java/sql/Connection::close → SURVIVED |
connection.close(); |
| 189 | } | |
| 190 | } catch (SQLException ex) { | |
| 191 |
1
1. clearCartByUserEmail : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
ex.printStackTrace(); |
| 192 | } | |
| 193 | } | |
| 194 | } | |
| 195 | ||
| 196 | ||
| 197 | ||
| 198 | ||
| 199 | } | |
Mutations | ||
| 47 |
1.1 |
|
| 53 |
1.1 |
|
| 57 |
1.1 |
|
| 80 |
1.1 |
|
| 109 |
1.1 |
|
| 112 |
1.1 |
|
| 118 |
1.1 |
|
| 121 |
1.1 |
|
| 137 |
1.1 |
|
| 140 |
1.1 2.2 |
|
| 158 |
1.1 |
|
| 161 |
1.1 |
|
| 171 |
1.1 |
|
| 174 |
1.1 |
|
| 177 |
1.1 |
|
| 178 |
1.1 |
|
| 181 |
1.1 |
|
| 186 |
1.1 |
|
| 187 |
1.1 |
|
| 188 |
1.1 |
|
| 191 |
1.1 |