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