| 1 | package com.popx.persistenza; | |
| 2 | ||
| 3 | import com.popx.modello.ProdottoBean; | |
| 4 | ||
| 5 | import javax.servlet.http.HttpSession; | |
| 6 | import javax.sql.DataSource; | |
| 7 | import java.sql.*; | |
| 8 | import java.util.ArrayList; | |
| 9 | import java.util.List; | |
| 10 | ||
| 11 | public class ProdottoDAOImpl implements ProdottoDAO { | |
| 12 | ||
| 13 | private final DataSource ds; | |
| 14 | ||
| 15 | /*@ public model boolean available; | |
| 16 | @ public invariant ds != null && available; | |
| 17 | @ represents available <- ds != null; | |
| 18 | @*/ | |
| 19 | ||
| 20 | public ProdottoDAOImpl(DataSource ds) { | |
| 21 | this.ds = ds; | |
| 22 | } | |
| 23 | ||
| 24 | public ProdottoDAOImpl() { | |
| 25 | this(DataSourceSingleton.getInstance()); | |
| 26 | } | |
| 27 | ||
| 28 | @Override | |
| 29 | /*@ public normal_behavior | |
| 30 | @ requires prodotto != null | |
| 31 | @ && prodotto.getId() != null && !prodotto.getId().isEmpty() | |
| 32 | @ && prodotto.getName() != null && !prodotto.getName().isEmpty() | |
| 33 | @ && prodotto.getBrand() != null && !prodotto.getBrand().isEmpty() | |
| 34 | @ && prodotto.getCost() >= 0 | |
| 35 | @ && prodotto.getPiecesInStock() >= 0; | |
| 36 | @ assignable \everything; | |
| 37 | @ ensures \result ==> true; | |
| 38 | @*/ | |
| 39 | public boolean saveProdotto(ProdottoBean prodotto) { | |
| 40 | String query = "INSERT INTO Prodotto (id, name, description, cost, pieces_in_stock, brand, img, figure) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; | |
| 41 | try (Connection connection = ds.getConnection(); | |
| 42 | PreparedStatement statement = connection.prepareStatement(query)) { | |
| 43 |
1
1. saveProdotto : removed call to java/sql/PreparedStatement::setString → KILLED |
statement.setString(1, prodotto.getId()); |
| 44 |
1
1. saveProdotto : removed call to java/sql/PreparedStatement::setString → KILLED |
statement.setString(2, prodotto.getName()); |
| 45 |
1
1. saveProdotto : removed call to java/sql/PreparedStatement::setString → KILLED |
statement.setString(3, prodotto.getDescription()); |
| 46 |
1
1. saveProdotto : removed call to java/sql/PreparedStatement::setDouble → KILLED |
statement.setDouble(4, prodotto.getCost()); |
| 47 |
1
1. saveProdotto : removed call to java/sql/PreparedStatement::setInt → KILLED |
statement.setInt(5, prodotto.getPiecesInStock()); |
| 48 |
1
1. saveProdotto : removed call to java/sql/PreparedStatement::setString → KILLED |
statement.setString(6, prodotto.getBrand()); |
| 49 |
1
1. saveProdotto : removed call to java/sql/PreparedStatement::setBytes → KILLED |
statement.setBytes(7, prodotto.getImg()); |
| 50 |
1
1. saveProdotto : removed call to java/sql/PreparedStatement::setString → KILLED |
statement.setString(8, prodotto.getFigure()); // Set the figure field |
| 51 |
2
1. saveProdotto : changed conditional boundary → SURVIVED 2. saveProdotto : negated conditional → KILLED |
return statement.executeUpdate() > 0; |
| 52 | } catch (SQLException e) { | |
| 53 |
1
1. saveProdotto : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
e.printStackTrace(); |
| 54 | } | |
| 55 |
1
1. saveProdotto : replaced boolean return with true for com/popx/persistenza/ProdottoDAOImpl::saveProdotto → NO_COVERAGE |
return false; |
| 56 | } | |
| 57 | ||
| 58 | @Override | |
| 59 | /*@ public normal_behavior | |
| 60 | @ requires id != null && !id.isEmpty(); | |
| 61 | @ assignable \everything; | |
| 62 | @ ensures \result == null || \result.getId().equals(id); | |
| 63 | @*/ | |
| 64 | public ProdottoBean getProdottoById(String id) { | |
| 65 | String query = "SELECT * FROM Prodotto WHERE id = ?"; | |
| 66 | try (Connection connection = ds.getConnection(); | |
| 67 | PreparedStatement statement = connection.prepareStatement(query)) { | |
| 68 |
1
1. getProdottoById : removed call to java/sql/PreparedStatement::setString → KILLED |
statement.setString(1, id); |
| 69 | ResultSet resultSet = statement.executeQuery(); | |
| 70 |
1
1. getProdottoById : negated conditional → KILLED |
if (resultSet.next()) { |
| 71 | return new ProdottoBean( | |
| 72 | resultSet.getString("id"), | |
| 73 | resultSet.getString("name"), | |
| 74 | resultSet.getString("description"), | |
| 75 | resultSet.getDouble("cost"), | |
| 76 | resultSet.getInt("pieces_in_stock"), | |
| 77 | resultSet.getString("brand"), | |
| 78 | resultSet.getBytes("img"), | |
| 79 | resultSet.getString("figure") // Retrieve the figure field | |
| 80 | ); | |
| 81 | } | |
| 82 | } catch (SQLException e) { | |
| 83 |
1
1. getProdottoById : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
e.printStackTrace(); |
| 84 | } | |
| 85 | return null; | |
| 86 | } | |
| 87 | ||
| 88 | @Override | |
| 89 | /*@ public normal_behavior | |
| 90 | @ requires brand != null && !brand.isEmpty(); | |
| 91 | @ assignable \everything; | |
| 92 | @ ensures \result != null | |
| 93 | @ && (\forall int i; 0 <= i && i < \result.size(); | |
| 94 | @ \result.get(i) != null | |
| 95 | @ && brand.equals(\result.get(i).getBrand())); | |
| 96 | @*/ | |
| 97 | public List<ProdottoBean> getProdottiByBrand(String brand) { | |
| 98 | List<ProdottoBean> prodotti = new ArrayList<>(); | |
| 99 | String query = "SELECT * FROM Prodotto WHERE brand = ?"; | |
| 100 | try (Connection connection = ds.getConnection(); | |
| 101 | PreparedStatement statement = connection.prepareStatement(query)) { | |
| 102 |
1
1. getProdottiByBrand : removed call to java/sql/PreparedStatement::setString → KILLED |
statement.setString(1, brand); |
| 103 | ResultSet resultSet = statement.executeQuery(); | |
| 104 |
1
1. getProdottiByBrand : negated conditional → KILLED |
while (resultSet.next()) { |
| 105 | prodotti.add(new ProdottoBean( | |
| 106 | resultSet.getString("id"), | |
| 107 | resultSet.getString("name"), | |
| 108 | resultSet.getString("description"), | |
| 109 | resultSet.getDouble("cost"), | |
| 110 | resultSet.getInt("pieces_in_stock"), | |
| 111 | resultSet.getString("brand"), | |
| 112 | resultSet.getBytes("img"), | |
| 113 | resultSet.getString("figure") // Retrieve the figure field | |
| 114 | )); | |
| 115 | } | |
| 116 | } catch (SQLException e) { | |
| 117 |
1
1. getProdottiByBrand : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
e.printStackTrace(); |
| 118 | } | |
| 119 |
1
1. getProdottiByBrand : replaced return value with Collections.emptyList for com/popx/persistenza/ProdottoDAOImpl::getProdottiByBrand → KILLED |
return prodotti; |
| 120 | } | |
| 121 | ||
| 122 | @Override | |
| 123 | /*@ public normal_behavior | |
| 124 | @ requires brand != null && !brand.isEmpty(); | |
| 125 | @ assignable \everything; | |
| 126 | @ ensures \result != null | |
| 127 | @ && (\forall int i; 0 <= i && i < \result.size(); | |
| 128 | @ \result.get(i) != null | |
| 129 | @ && brand.equals(\result.get(i).getBrand())); | |
| 130 | @*/ | |
| 131 | public List<ProdottoBean> getProdottiByBrandAndPrice(String brand, boolean ascending) { | |
| 132 | List<ProdottoBean> prodotti = new ArrayList<>(); | |
| 133 |
1
1. getProdottiByBrandAndPrice : negated conditional → KILLED |
String query = "SELECT * FROM Prodotto WHERE brand = ? ORDER BY cost " + (ascending ? "ASC" : "DESC"); |
| 134 | try (Connection connection = ds.getConnection(); | |
| 135 | PreparedStatement statement = connection.prepareStatement(query)) { | |
| 136 |
1
1. getProdottiByBrandAndPrice : removed call to java/sql/PreparedStatement::setString → KILLED |
statement.setString(1, brand); |
| 137 | ResultSet resultSet = statement.executeQuery(); | |
| 138 |
1
1. getProdottiByBrandAndPrice : negated conditional → KILLED |
while (resultSet.next()) { |
| 139 | prodotti.add(new ProdottoBean( | |
| 140 | resultSet.getString("id"), | |
| 141 | resultSet.getString("name"), | |
| 142 | resultSet.getString("description"), | |
| 143 | resultSet.getDouble("cost"), | |
| 144 | resultSet.getInt("pieces_in_stock"), | |
| 145 | resultSet.getString("brand"), | |
| 146 | resultSet.getBytes("img"), | |
| 147 | resultSet.getString("figure") // Retrieve the figure field | |
| 148 | )); | |
| 149 | } | |
| 150 | } catch (SQLException e) { | |
| 151 |
1
1. getProdottiByBrandAndPrice : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
e.printStackTrace(); |
| 152 | } | |
| 153 |
1
1. getProdottiByBrandAndPrice : replaced return value with Collections.emptyList for com/popx/persistenza/ProdottoDAOImpl::getProdottiByBrandAndPrice → KILLED |
return prodotti; |
| 154 | } | |
| 155 | ||
| 156 | @Override | |
| 157 | /*@ public normal_behavior | |
| 158 | @ assignable \everything; | |
| 159 | @ ensures \result != null; | |
| 160 | @*/ | |
| 161 | public List<ProdottoBean> getProdottiSortedByPrice(boolean ascending) { | |
| 162 | List<ProdottoBean> prodotti = new ArrayList<>(); | |
| 163 |
1
1. getProdottiSortedByPrice : negated conditional → KILLED |
String query = "SELECT * FROM Prodotto ORDER BY cost " + (ascending ? "ASC" : "DESC"); |
| 164 | try (Connection connection = ds.getConnection(); | |
| 165 | PreparedStatement statement = connection.prepareStatement(query); | |
| 166 | ResultSet resultSet = statement.executeQuery()) { | |
| 167 |
1
1. getProdottiSortedByPrice : negated conditional → KILLED |
while (resultSet.next()) { |
| 168 | prodotti.add(new ProdottoBean( | |
| 169 | resultSet.getString("id"), | |
| 170 | resultSet.getString("name"), | |
| 171 | resultSet.getString("description"), | |
| 172 | resultSet.getDouble("cost"), | |
| 173 | resultSet.getInt("pieces_in_stock"), | |
| 174 | resultSet.getString("brand"), | |
| 175 | resultSet.getBytes("img"), | |
| 176 | resultSet.getString("figure") // Retrieve the figure field | |
| 177 | )); | |
| 178 | } | |
| 179 | } catch (SQLException e) { | |
| 180 |
1
1. getProdottiSortedByPrice : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
e.printStackTrace(); |
| 181 | } | |
| 182 |
1
1. getProdottiSortedByPrice : replaced return value with Collections.emptyList for com/popx/persistenza/ProdottoDAOImpl::getProdottiSortedByPrice → KILLED |
return prodotti; |
| 183 | } | |
| 184 | ||
| 185 | @Override | |
| 186 | /*@ public normal_behavior | |
| 187 | @ requires id != null && !id.isEmpty(); | |
| 188 | @ assignable \everything; | |
| 189 | @ ensures \result == null || \result.length >= 0; | |
| 190 | @*/ | |
| 191 | public byte[] getProductImageById(String id) { | |
| 192 | String query = "SELECT img FROM Prodotto WHERE id = ?"; | |
| 193 | try (Connection connection = ds.getConnection(); | |
| 194 | PreparedStatement statement = connection.prepareStatement(query)) { | |
| 195 |
1
1. getProductImageById : removed call to java/sql/PreparedStatement::setString → KILLED |
statement.setString(1, id); |
| 196 | ResultSet resultSet = statement.executeQuery(); | |
| 197 |
1
1. getProductImageById : negated conditional → KILLED |
if (resultSet.next()) { |
| 198 | return resultSet.getBytes("img"); | |
| 199 | } | |
| 200 | } catch (SQLException e) { | |
| 201 |
1
1. getProductImageById : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
e.printStackTrace(); |
| 202 | } | |
| 203 | return null; | |
| 204 | } | |
| 205 | ||
| 206 | @Override | |
| 207 | /*@ public normal_behavior | |
| 208 | @ assignable \everything; | |
| 209 | @ ensures \result != null; | |
| 210 | @*/ | |
| 211 | public List<ProdottoBean> getAllProducts() { | |
| 212 | List<ProdottoBean> products = new ArrayList<>(); | |
| 213 | String query = "SELECT * FROM Prodotto ORDER BY id"; | |
| 214 | try (Connection con = ds.getConnection(); | |
| 215 | PreparedStatement ps = con.prepareStatement(query); | |
| 216 | ResultSet rs = ps.executeQuery()) { | |
| 217 | ||
| 218 |
1
1. getAllProducts : negated conditional → KILLED |
while (rs.next()) { |
| 219 | ProdottoBean prodotto = new ProdottoBean(); | |
| 220 |
1
1. getAllProducts : removed call to com/popx/modello/ProdottoBean::setId → SURVIVED |
prodotto.setId(rs.getString("id")); |
| 221 |
1
1. getAllProducts : removed call to com/popx/modello/ProdottoBean::setName → SURVIVED |
prodotto.setName(rs.getString("name")); |
| 222 |
1
1. getAllProducts : removed call to com/popx/modello/ProdottoBean::setDescription → SURVIVED |
prodotto.setDescription(rs.getString("description")); |
| 223 |
1
1. getAllProducts : removed call to com/popx/modello/ProdottoBean::setCost → SURVIVED |
prodotto.setCost(rs.getDouble("cost")); |
| 224 |
1
1. getAllProducts : removed call to com/popx/modello/ProdottoBean::setPiecesInStock → SURVIVED |
prodotto.setPiecesInStock(rs.getInt("pieces_in_stock")); // Recupero del valore |
| 225 |
1
1. getAllProducts : removed call to com/popx/modello/ProdottoBean::setBrand → SURVIVED |
prodotto.setBrand(rs.getString("brand")); |
| 226 |
1
1. getAllProducts : removed call to com/popx/modello/ProdottoBean::setImg → SURVIVED |
prodotto.setImg(rs.getBytes("img")); |
| 227 |
1
1. getAllProducts : removed call to com/popx/modello/ProdottoBean::setFigure → SURVIVED |
prodotto.setFigure(rs.getString("figure")); |
| 228 | products.add(prodotto); | |
| 229 | } | |
| 230 | } catch (SQLException e) { | |
| 231 |
1
1. getAllProducts : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
e.printStackTrace(); |
| 232 | } | |
| 233 |
1
1. getAllProducts : replaced return value with Collections.emptyList for com/popx/persistenza/ProdottoDAOImpl::getAllProducts → KILLED |
return products; |
| 234 | } | |
| 235 | ||
| 236 | ||
| 237 | @Override | |
| 238 | /*@ public normal_behavior | |
| 239 | @ requires limit > 0; | |
| 240 | @ assignable \everything; | |
| 241 | @ ensures \result != null && \result.size() <= limit; | |
| 242 | @*/ | |
| 243 | public List<ProdottoBean> getRandomProducts(int limit) throws SQLException { | |
| 244 | List<ProdottoBean> products = new ArrayList<>(); | |
| 245 | String query = "SELECT * FROM Prodotto ORDER BY RAND() LIMIT ?"; // Usato ORDER BY RAND() per ottenere risultati casuali | |
| 246 | ||
| 247 | try (Connection con = ds.getConnection(); // Assicurati che ds sia un DataSource valido | |
| 248 | PreparedStatement ps = con.prepareStatement(query)) { | |
| 249 | ||
| 250 |
1
1. getRandomProducts : removed call to java/sql/PreparedStatement::setInt → KILLED |
ps.setInt(1, limit); // Imposta il parametro limit dinamicamente |
| 251 | ||
| 252 | try (ResultSet rs = ps.executeQuery()) { | |
| 253 |
1
1. getRandomProducts : negated conditional → KILLED |
while (rs.next()) { |
| 254 | ProdottoBean product = new ProdottoBean(); | |
| 255 |
1
1. getRandomProducts : removed call to com/popx/modello/ProdottoBean::setId → SURVIVED |
product.setId(rs.getString("id")); // Verifica che 'id' sia di tipo String, altrimenti cambia a Integer |
| 256 |
1
1. getRandomProducts : removed call to com/popx/modello/ProdottoBean::setName → SURVIVED |
product.setName(rs.getString("name")); |
| 257 |
1
1. getRandomProducts : removed call to com/popx/modello/ProdottoBean::setBrand → SURVIVED |
product.setBrand(rs.getString("brand")); |
| 258 |
1
1. getRandomProducts : removed call to com/popx/modello/ProdottoBean::setCost → SURVIVED |
product.setCost(rs.getDouble("cost")); |
| 259 |
1
1. getRandomProducts : removed call to com/popx/modello/ProdottoBean::setImg → SURVIVED |
product.setImg(rs.getBytes("img")); |
| 260 |
1
1. getRandomProducts : removed call to com/popx/modello/ProdottoBean::setDescription → SURVIVED |
product.setDescription(rs.getString("description")); |
| 261 |
1
1. getRandomProducts : removed call to com/popx/modello/ProdottoBean::setFigure → SURVIVED |
product.setFigure(rs.getString("figure")); // Set the figure field |
| 262 | products.add(product); | |
| 263 | } | |
| 264 | } | |
| 265 | } | |
| 266 |
1
1. getRandomProducts : replaced return value with Collections.emptyList for com/popx/persistenza/ProdottoDAOImpl::getRandomProducts → KILLED |
return products; |
| 267 | } | |
| 268 | ||
| 269 | @Override | |
| 270 | /*@ public normal_behavior | |
| 271 | @ requires session != null | |
| 272 | @ && productId != null && !productId.isEmpty() | |
| 273 | @ && qty >= 0; | |
| 274 | @ assignable \everything; | |
| 275 | @ ensures true; | |
| 276 | @*/ | |
| 277 | public void updateProductQtyInCart(HttpSession session, String productId, int qty) { | |
| 278 | List<ProdottoBean> cart = (List<ProdottoBean>) session.getAttribute("cart"); | |
| 279 |
1
1. updateProductQtyInCart : negated conditional → NO_COVERAGE |
if (cart != null) { |
| 280 | for (ProdottoBean product : cart) { | |
| 281 |
1
1. updateProductQtyInCart : negated conditional → NO_COVERAGE |
if (product.getId().equals(productId)) { |
| 282 |
1
1. updateProductQtyInCart : removed call to com/popx/modello/ProdottoBean::setQty → NO_COVERAGE |
product.setQty(qty); |
| 283 | break; | |
| 284 | } | |
| 285 | } | |
| 286 | } | |
| 287 | } | |
| 288 | ||
| 289 | @Override | |
| 290 | /*@ public normal_behavior | |
| 291 | @ requires session != null | |
| 292 | @ && productId != null && !productId.isEmpty(); | |
| 293 | @ assignable \everything; | |
| 294 | @ ensures \result >= 0; | |
| 295 | @*/ | |
| 296 | public int getProductQtyInCart(HttpSession session, String productId) { | |
| 297 | List<ProdottoBean> cart = (List<ProdottoBean>) session.getAttribute("cart"); | |
| 298 |
1
1. getProductQtyInCart : negated conditional → NO_COVERAGE |
if (cart != null) { |
| 299 | for (ProdottoBean product : cart) { | |
| 300 |
1
1. getProductQtyInCart : negated conditional → NO_COVERAGE |
if (product.getId().equals(productId)) { |
| 301 |
1
1. getProductQtyInCart : replaced int return with 0 for com/popx/persistenza/ProdottoDAOImpl::getProductQtyInCart → NO_COVERAGE |
return product.getQty(); |
| 302 | } | |
| 303 | } | |
| 304 | } | |
| 305 | return 0; | |
| 306 | } | |
| 307 | ||
| 308 | @Override | |
| 309 | /*@ public normal_behavior | |
| 310 | @ requires userEmail != null && !userEmail.isEmpty() | |
| 311 | @ && cart != null | |
| 312 | @ && (\forall int i; 0 <= i && i < cart.size(); | |
| 313 | @ cart.get(i) != null | |
| 314 | @ && cart.get(i).getId() != null && !cart.get(i).getId().isEmpty() | |
| 315 | @ && cart.get(i).getQty() >= 0 | |
| 316 | @ && cart.get(i).getCost() >= 0); | |
| 317 | @ assignable \everything; | |
| 318 | @ ensures available; | |
| 319 | @*/ | |
| 320 | public void saveCartToDatabase(String userEmail, List<ProdottoBean> cart) throws SQLException { | |
| 321 | String insertCartQuery = "INSERT INTO Carrello (cliente_email) VALUES (?) " + | |
| 322 | "ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)"; | |
| 323 | ||
| 324 | String insertProductCartQuery = "INSERT INTO ProdottoCarrello (carrello_id, prodotto_id, quantity, unitary_cost) " + | |
| 325 | "VALUES (?, ?, ?, ?) " + | |
| 326 | "ON DUPLICATE KEY UPDATE quantity = VALUES(quantity), unitary_cost = VALUES(unitary_cost)"; | |
| 327 | ||
| 328 | try (Connection connection = ds.getConnection(); | |
| 329 | PreparedStatement cartStmt = connection.prepareStatement(insertCartQuery, Statement.RETURN_GENERATED_KEYS); | |
| 330 | PreparedStatement productCartStmt = connection.prepareStatement(insertProductCartQuery)) { | |
| 331 | ||
| 332 | // Inserisci o aggiorna il carrello | |
| 333 |
1
1. saveCartToDatabase : removed call to java/sql/PreparedStatement::setString → KILLED |
cartStmt.setString(1, userEmail); |
| 334 | cartStmt.executeUpdate(); | |
| 335 | ||
| 336 | // Ottieni l'ID del carrello | |
| 337 | int cartId; | |
| 338 | try (ResultSet rs = cartStmt.getGeneratedKeys()) { | |
| 339 |
1
1. saveCartToDatabase : negated conditional → KILLED |
if (rs.next()) { |
| 340 | cartId = rs.getInt(1); | |
| 341 | } else { | |
| 342 | throw new SQLException("Impossibile ottenere l'ID del carrello."); | |
| 343 | } | |
| 344 | } | |
| 345 | ||
| 346 | // Inserisci o aggiorna i prodotti nel carrello | |
| 347 | for (ProdottoBean product : cart) { | |
| 348 | productCartStmt.setInt(1, cartId); | |
| 349 | productCartStmt.setString(2, product.getId()); | |
| 350 | productCartStmt.setInt(3, product.getQty()); | |
| 351 | productCartStmt.setDouble(4, product.getCost()); | |
| 352 | ||
| 353 | productCartStmt.executeUpdate(); | |
| 354 | } | |
| 355 | } | |
| 356 | } | |
| 357 | ||
| 358 | @Override | |
| 359 | /*@ public normal_behavior | |
| 360 | @ requires userEmail != null && !userEmail.isEmpty(); | |
| 361 | @ assignable \everything; | |
| 362 | @ ensures \result != null | |
| 363 | @ && (\forall int i; 0 <= i && i < \result.size(); | |
| 364 | @ \result.get(i) != null | |
| 365 | @ && \result.get(i).getId() != null && !\result.get(i).getId().isEmpty() | |
| 366 | @ && \result.get(i).getQty() >= 0); | |
| 367 | @*/ | |
| 368 | public List<ProdottoBean> getCartByUserEmail(String userEmail) throws SQLException { | |
| 369 | List<ProdottoBean> cart = new ArrayList<>(); | |
| 370 | String query = "SELECT p.id, p.name, p.pieces_in_stock, p.cost, pc.quantity " + | |
| 371 | "FROM Prodotto p " + | |
| 372 | "JOIN ProdottoCarrello pc ON p.id = pc.prodotto_id " + | |
| 373 | "JOIN Carrello c ON pc.carrello_id = c.id " + | |
| 374 | "WHERE c.cliente_email = ?"; | |
| 375 | ||
| 376 | ||
| 377 | try (Connection connection = ds.getConnection(); | |
| 378 | PreparedStatement ps = connection.prepareStatement(query)) { | |
| 379 | ||
| 380 |
1
1. getCartByUserEmail : removed call to java/sql/PreparedStatement::setString → KILLED |
ps.setString(1, userEmail); |
| 381 | try (ResultSet rs = ps.executeQuery()) { | |
| 382 |
1
1. getCartByUserEmail : negated conditional → KILLED |
while (rs.next()) { |
| 383 | ProdottoBean prodotto = new ProdottoBean(); | |
| 384 |
1
1. getCartByUserEmail : removed call to com/popx/modello/ProdottoBean::setId → SURVIVED |
prodotto.setId(rs.getString("id")); |
| 385 |
1
1. getCartByUserEmail : removed call to com/popx/modello/ProdottoBean::setName → SURVIVED |
prodotto.setName(rs.getString("name")); |
| 386 |
1
1. getCartByUserEmail : removed call to com/popx/modello/ProdottoBean::setPiecesInStock → SURVIVED |
prodotto.setPiecesInStock(rs.getInt("pieces_in_stock")); |
| 387 |
1
1. getCartByUserEmail : removed call to com/popx/modello/ProdottoBean::setCost → SURVIVED |
prodotto.setCost(rs.getDouble("cost")); |
| 388 |
1
1. getCartByUserEmail : removed call to com/popx/modello/ProdottoBean::setQty → KILLED |
prodotto.setQty(rs.getInt("quantity")); |
| 389 | cart.add(prodotto); | |
| 390 | } | |
| 391 | } | |
| 392 | } | |
| 393 |
1
1. getCartByUserEmail : replaced return value with Collections.emptyList for com/popx/persistenza/ProdottoDAOImpl::getCartByUserEmail → KILLED |
return cart; |
| 394 | } | |
| 395 | ||
| 396 | /*@ public normal_behavior | |
| 397 | @ requires userEmail != null && !userEmail.isEmpty() | |
| 398 | @ && productId != null && !productId.isEmpty() | |
| 399 | @ && qty >= 0; | |
| 400 | @ assignable \everything; | |
| 401 | @ ensures available; | |
| 402 | @*/ | |
| 403 | @Override | |
| 404 | public void updateCartProductQuantityInDatabase(String userEmail, String productId, int qty) throws SQLException { | |
| 405 | String query = """ | |
| 406 | UPDATE ProdottoCarrello | |
| 407 | SET quantity = ? | |
| 408 | WHERE prodotto_id = ? | |
| 409 | AND carrello_id = ( | |
| 410 | SELECT id FROM Carrello WHERE cliente_email = ? | |
| 411 | ) | |
| 412 | """; | |
| 413 | ||
| 414 | try (Connection connection = ds.getConnection(); | |
| 415 | PreparedStatement stmt = connection.prepareStatement(query)) { | |
| 416 | ||
| 417 |
1
1. updateCartProductQuantityInDatabase : removed call to java/sql/PreparedStatement::setInt → KILLED |
stmt.setInt(1, qty); |
| 418 |
1
1. updateCartProductQuantityInDatabase : removed call to java/sql/PreparedStatement::setString → KILLED |
stmt.setString(2, productId); |
| 419 |
1
1. updateCartProductQuantityInDatabase : removed call to java/sql/PreparedStatement::setString → KILLED |
stmt.setString(3, userEmail); |
| 420 | ||
| 421 | stmt.executeUpdate(); | |
| 422 | } | |
| 423 | } | |
| 424 | ||
| 425 | ||
| 426 | /*@ public normal_behavior | |
| 427 | @ requires userEmail != null && !userEmail.isEmpty() | |
| 428 | @ && productId != null && !productId.isEmpty(); | |
| 429 | @ assignable \everything; | |
| 430 | @ ensures available; | |
| 431 | @*/ | |
| 432 | @Override | |
| 433 | public void removeProductFromCart(String userEmail, String productId) throws SQLException { | |
| 434 | String query = """ | |
| 435 | DELETE FROM ProdottoCarrello | |
| 436 | WHERE prodotto_id = ? | |
| 437 | AND carrello_id = ( | |
| 438 | SELECT id FROM Carrello WHERE cliente_email = ? | |
| 439 | ) | |
| 440 | """; | |
| 441 | ||
| 442 | try (Connection connection = ds.getConnection(); | |
| 443 | PreparedStatement stmt = connection.prepareStatement(query)) { | |
| 444 | ||
| 445 |
1
1. removeProductFromCart : removed call to java/sql/PreparedStatement::setString → KILLED |
stmt.setString(1, productId); |
| 446 |
1
1. removeProductFromCart : removed call to java/sql/PreparedStatement::setString → KILLED |
stmt.setString(2, userEmail); |
| 447 | ||
| 448 | stmt.executeUpdate(); | |
| 449 | } | |
| 450 | } | |
| 451 | ||
| 452 | ||
| 453 | @Override | |
| 454 | /*@ public normal_behavior | |
| 455 | @ requires id != null && !id.isEmpty(); | |
| 456 | @ assignable \everything; | |
| 457 | @ ensures available; | |
| 458 | @*/ | |
| 459 | public void deleteProductById(String id) throws SQLException { | |
| 460 | String deleteProductQuery = "DELETE FROM Prodotto WHERE id = ?"; | |
| 461 | String deleteFromCartQuery = "DELETE FROM ProdottoCarrello WHERE prodotto_id = ?"; | |
| 462 | ||
| 463 | try (Connection connection = ds.getConnection()) { | |
| 464 | // Rimuovi il prodotto dalla tabella ProdottoCarrello | |
| 465 | try (PreparedStatement cartStatement = connection.prepareStatement(deleteFromCartQuery)) { | |
| 466 |
1
1. deleteProductById : removed call to java/sql/PreparedStatement::setString → KILLED |
cartStatement.setString(1, id); |
| 467 | cartStatement.executeUpdate(); | |
| 468 | } | |
| 469 | ||
| 470 | // Rimuovi il prodotto dalla tabella Prodotto | |
| 471 | try (PreparedStatement productStatement = connection.prepareStatement(deleteProductQuery)) { | |
| 472 | productStatement.setString(1, id); | |
| 473 | productStatement.executeUpdate(); | |
| 474 | } | |
| 475 | } | |
| 476 | } | |
| 477 | ||
| 478 | @Override | |
| 479 | /*@ public normal_behavior | |
| 480 | @ requires prodottoBean != null | |
| 481 | @ && prodottoBean.getId() != null && !prodottoBean.getId().isEmpty() | |
| 482 | @ && prodottoBean.getName() != null && !prodottoBean.getName().isEmpty() | |
| 483 | @ && prodottoBean.getBrand() != null && !prodottoBean.getBrand().isEmpty() | |
| 484 | @ && prodottoBean.getCost() >= 0 | |
| 485 | @ && prodottoBean.getPiecesInStock() >= 0; | |
| 486 | @ assignable \everything; | |
| 487 | @ ensures \result ==> true; | |
| 488 | @*/ | |
| 489 | public boolean updateProduct(ProdottoBean prodottoBean) { | |
| 490 | String queryProdotto = | |
| 491 | "UPDATE Prodotto SET name = ?, cost = ?, brand = ?, figure = ?, pieces_in_stock = ?, img = ?, description = ? WHERE id = ?"; | |
| 492 | String queryProdottoCarrello = | |
| 493 | "UPDATE ProdottoCarrello SET unitary_cost = ? WHERE prodotto_id = ?"; | |
| 494 | ||
| 495 | try (Connection conn = ds.getConnection(); | |
| 496 | PreparedStatement stmtProdotto = conn.prepareStatement(queryProdotto); | |
| 497 | PreparedStatement stmtProdottoCarrello = conn.prepareStatement(queryProdottoCarrello)) { | |
| 498 | ||
| 499 |
1
1. updateProduct : removed call to java/sql/PreparedStatement::setString → KILLED |
stmtProdotto.setString(1, prodottoBean.getName()); |
| 500 |
1
1. updateProduct : removed call to java/sql/PreparedStatement::setDouble → KILLED |
stmtProdotto.setDouble(2, prodottoBean.getCost()); |
| 501 |
1
1. updateProduct : removed call to java/sql/PreparedStatement::setString → KILLED |
stmtProdotto.setString(3, prodottoBean.getBrand()); |
| 502 |
1
1. updateProduct : removed call to java/sql/PreparedStatement::setString → KILLED |
stmtProdotto.setString(4, prodottoBean.getFigure()); |
| 503 |
1
1. updateProduct : removed call to java/sql/PreparedStatement::setInt → KILLED |
stmtProdotto.setInt(5, prodottoBean.getPiecesInStock()); |
| 504 |
1
1. updateProduct : removed call to java/sql/PreparedStatement::setBytes → KILLED |
stmtProdotto.setBytes(6, prodottoBean.getImg()); |
| 505 |
1
1. updateProduct : removed call to java/sql/PreparedStatement::setString → KILLED |
stmtProdotto.setString(7, prodottoBean.getDescription()); |
| 506 |
1
1. updateProduct : removed call to java/sql/PreparedStatement::setString → KILLED |
stmtProdotto.setString(8, prodottoBean.getId()); |
| 507 | ||
| 508 | int rowsUpdatedProdotto = stmtProdotto.executeUpdate(); | |
| 509 | ||
| 510 | // opzionale: aggiorna carrello se esiste | |
| 511 |
1
1. updateProduct : removed call to java/sql/PreparedStatement::setDouble → KILLED |
stmtProdottoCarrello.setDouble(1, prodottoBean.getCost()); |
| 512 |
1
1. updateProduct : removed call to java/sql/PreparedStatement::setString → KILLED |
stmtProdottoCarrello.setString(2, prodottoBean.getId()); |
| 513 | stmtProdottoCarrello.executeUpdate(); | |
| 514 | ||
| 515 |
2
1. updateProduct : changed conditional boundary → SURVIVED 2. updateProduct : negated conditional → KILLED |
return rowsUpdatedProdotto > 0; |
| 516 | ||
| 517 | } catch (SQLException e) { | |
| 518 |
1
1. updateProduct : removed call to java/sql/SQLException::printStackTrace → NO_COVERAGE |
e.printStackTrace(); |
| 519 |
1
1. updateProduct : replaced boolean return with true for com/popx/persistenza/ProdottoDAOImpl::updateProduct → NO_COVERAGE |
return false; |
| 520 | } | |
| 521 | } | |
| 522 | ||
| 523 | ||
| 524 | /*@ public normal_behavior | |
| 525 | @ requires productId != null && !productId.isEmpty() | |
| 526 | @ && quantity >= 0; | |
| 527 | @ assignable \everything; | |
| 528 | @ ensures available; | |
| 529 | @*/ | |
| 530 | public void updateStock(String productId, int quantity) throws SQLException { | |
| 531 | String query = "UPDATE Prodotto SET pieces_in_stock = CASE WHEN ? <= 0 THEN 10 ELSE ? END WHERE id = ?"; | |
| 532 | ||
| 533 | try (Connection connection = ds.getConnection(); | |
| 534 | PreparedStatement statement = connection.prepareStatement(query)) { | |
| 535 | ||
| 536 |
1
1. updateStock : removed call to java/sql/PreparedStatement::setInt → KILLED |
statement.setInt(1, quantity); // Controlla se la quantità è <= 0 |
| 537 |
1
1. updateStock : removed call to java/sql/PreparedStatement::setInt → KILLED |
statement.setInt(2, quantity); // Usa la stessa quantità se non scende a 0 |
| 538 |
1
1. updateStock : removed call to java/sql/PreparedStatement::setString → KILLED |
statement.setString(3, productId); // ID del prodotto |
| 539 | ||
| 540 | statement.executeUpdate(); | |
| 541 | } | |
| 542 | } | |
| 543 | ||
| 544 | /*@ public normal_behavior | |
| 545 | @ requires productId1 != null && !productId1.isEmpty() | |
| 546 | @ && productId2 != null && !productId2.isEmpty(); | |
| 547 | @ assignable \everything; | |
| 548 | @ ensures \result ==> true || !\result; | |
| 549 | @*/ | |
| 550 | public boolean isAssociatedWith(String productId1, String productId2) throws SQLException { | |
| 551 | String query = "SELECT COUNT(*) FROM ProductAssociations WHERE product_id_1 = ? AND product_id_2 = ?"; | |
| 552 | ||
| 553 | try (Connection connection = ds.getConnection(); | |
| 554 | PreparedStatement preparedStatement = connection.prepareStatement(query)) { | |
| 555 | ||
| 556 |
1
1. isAssociatedWith : removed call to java/sql/PreparedStatement::setString → KILLED |
preparedStatement.setString(1, productId1); |
| 557 |
1
1. isAssociatedWith : removed call to java/sql/PreparedStatement::setString → KILLED |
preparedStatement.setString(2, productId2); |
| 558 | ||
| 559 | try (ResultSet resultSet = preparedStatement.executeQuery()) { | |
| 560 |
1
1. isAssociatedWith : negated conditional → KILLED |
if (resultSet.next()) { |
| 561 |
2
1. isAssociatedWith : negated conditional → KILLED 2. isAssociatedWith : changed conditional boundary → KILLED |
return resultSet.getInt(1) > 0; |
| 562 | } | |
| 563 | } | |
| 564 | } | |
| 565 |
1
1. isAssociatedWith : replaced boolean return with true for com/popx/persistenza/ProdottoDAOImpl::isAssociatedWith → NO_COVERAGE |
return false; |
| 566 | } | |
| 567 | ||
| 568 | ||
| 569 | ||
| 570 | } | |
Mutations | ||
| 43 |
1.1 |
|
| 44 |
1.1 |
|
| 45 |
1.1 |
|
| 46 |
1.1 |
|
| 47 |
1.1 |
|
| 48 |
1.1 |
|
| 49 |
1.1 |
|
| 50 |
1.1 |
|
| 51 |
1.1 2.2 |
|
| 53 |
1.1 |
|
| 55 |
1.1 |
|
| 68 |
1.1 |
|
| 70 |
1.1 |
|
| 83 |
1.1 |
|
| 102 |
1.1 |
|
| 104 |
1.1 |
|
| 117 |
1.1 |
|
| 119 |
1.1 |
|
| 133 |
1.1 |
|
| 136 |
1.1 |
|
| 138 |
1.1 |
|
| 151 |
1.1 |
|
| 153 |
1.1 |
|
| 163 |
1.1 |
|
| 167 |
1.1 |
|
| 180 |
1.1 |
|
| 182 |
1.1 |
|
| 195 |
1.1 |
|
| 197 |
1.1 |
|
| 201 |
1.1 |
|
| 218 |
1.1 |
|
| 220 |
1.1 |
|
| 221 |
1.1 |
|
| 222 |
1.1 |
|
| 223 |
1.1 |
|
| 224 |
1.1 |
|
| 225 |
1.1 |
|
| 226 |
1.1 |
|
| 227 |
1.1 |
|
| 231 |
1.1 |
|
| 233 |
1.1 |
|
| 250 |
1.1 |
|
| 253 |
1.1 |
|
| 255 |
1.1 |
|
| 256 |
1.1 |
|
| 257 |
1.1 |
|
| 258 |
1.1 |
|
| 259 |
1.1 |
|
| 260 |
1.1 |
|
| 261 |
1.1 |
|
| 266 |
1.1 |
|
| 279 |
1.1 |
|
| 281 |
1.1 |
|
| 282 |
1.1 |
|
| 298 |
1.1 |
|
| 300 |
1.1 |
|
| 301 |
1.1 |
|
| 333 |
1.1 |
|
| 339 |
1.1 |
|
| 380 |
1.1 |
|
| 382 |
1.1 |
|
| 384 |
1.1 |
|
| 385 |
1.1 |
|
| 386 |
1.1 |
|
| 387 |
1.1 |
|
| 388 |
1.1 |
|
| 393 |
1.1 |
|
| 417 |
1.1 |
|
| 418 |
1.1 |
|
| 419 |
1.1 |
|
| 445 |
1.1 |
|
| 446 |
1.1 |
|
| 466 |
1.1 |
|
| 499 |
1.1 |
|
| 500 |
1.1 |
|
| 501 |
1.1 |
|
| 502 |
1.1 |
|
| 503 |
1.1 |
|
| 504 |
1.1 |
|
| 505 |
1.1 |
|
| 506 |
1.1 |
|
| 511 |
1.1 |
|
| 512 |
1.1 |
|
| 515 |
1.1 2.2 |
|
| 518 |
1.1 |
|
| 519 |
1.1 |
|
| 536 |
1.1 |
|
| 537 |
1.1 |
|
| 538 |
1.1 |
|
| 556 |
1.1 |
|
| 557 |
1.1 |
|
| 560 |
1.1 |
|
| 561 |
1.1 2.2 |
|
| 565 |
1.1 |