| 1 | package com.popx.presentazione; | |
| 2 | ||
| 3 | import com.popx.modello.ProdottoBean; | |
| 4 | import com.popx.modello.UserBean; | |
| 5 | import com.popx.persistenza.*; | |
| 6 | ||
| 7 | import javax.servlet.ServletException; | |
| 8 | import javax.servlet.annotation.WebServlet; | |
| 9 | import javax.servlet.http.*; | |
| 10 | import java.io.IOException; | |
| 11 | import java.sql.SQLException; | |
| 12 | import java.util.ArrayList; | |
| 13 | import java.util.List; | |
| 14 | ||
| 15 | @WebServlet("/addToCart") | |
| 16 | public class AddToCartServlet extends HttpServlet { | |
| 17 | ||
| 18 | private final ProdottoDAO prodottoDAO; | |
| 19 | private final UserDAO<UserBean> userDAO; | |
| 20 | ||
| 21 | // costruttore production | |
| 22 | public AddToCartServlet() { | |
| 23 | this.prodottoDAO = new ProdottoDAOImpl(); | |
| 24 | this.userDAO = new UserDAOImpl(); | |
| 25 | } | |
| 26 | ||
| 27 | // costruttore test | |
| 28 | public AddToCartServlet(ProdottoDAO prodottoDAO, UserDAO<UserBean> userDAO) { | |
| 29 | this.prodottoDAO = prodottoDAO; | |
| 30 | this.userDAO = userDAO; | |
| 31 | } | |
| 32 | ||
| 33 | @Override | |
| 34 | public void doPost(HttpServletRequest request, HttpServletResponse response) | |
| 35 | throws ServletException, IOException { | |
| 36 | ||
| 37 | String productId = request.getParameter("productId"); | |
| 38 | int quantity = Integer.parseInt(request.getParameter("quantity")); | |
| 39 | ||
| 40 | HttpSession session = request.getSession(); | |
| 41 | String userEmail = (String) session.getAttribute("userEmail"); | |
| 42 | ||
| 43 | // controllo ruolo | |
| 44 |
1
1. doPost : negated conditional → KILLED |
if (userEmail != null) { |
| 45 | try { | |
| 46 | UserBean userBean = userDAO.getUserByEmail(userEmail); | |
| 47 |
2
1. doPost : negated conditional → KILLED 2. doPost : negated conditional → KILLED |
if (userBean != null && !"User".equals(userBean.getRole())) { |
| 48 |
1
1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED |
writeJson(response, false, |
| 49 | "Accesso negato: solo gli utenti con il ruolo 'User' possono aggiungere prodotti al carrello."); | |
| 50 | return; | |
| 51 | } | |
| 52 | } catch (SQLException e) { | |
| 53 |
1
1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED |
writeJson(response, false, "Errore interno durante il controllo dei permessi."); |
| 54 | return; | |
| 55 | } | |
| 56 | } | |
| 57 | ||
| 58 | // carrello | |
| 59 | List<ProdottoBean> cart = (List<ProdottoBean>) session.getAttribute("cart"); | |
| 60 |
1
1. doPost : negated conditional → KILLED |
if (cart == null) { |
| 61 | cart = new ArrayList<>(); | |
| 62 |
1
1. doPost : removed call to javax/servlet/http/HttpSession::setAttribute → KILLED |
session.setAttribute("cart", cart); |
| 63 | } | |
| 64 | ||
| 65 | ProdottoBean prodotto = prodottoDAO.getProdottoById(productId); | |
| 66 | ||
| 67 |
1
1. doPost : negated conditional → KILLED |
if (prodotto == null) { |
| 68 |
1
1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED |
writeJson(response, false, "Errore nell'aggiungere il prodotto al carrello."); |
| 69 | return; | |
| 70 | } | |
| 71 | ||
| 72 | for (ProdottoBean cartItem : cart) { | |
| 73 |
1
1. doPost : negated conditional → KILLED |
if (cartItem.getId().equals(prodotto.getId())) { |
| 74 | int currentQty = prodottoDAO.getProductQtyInCart(session, productId); | |
| 75 |
3
1. doPost : changed conditional boundary → SURVIVED 2. doPost : negated conditional → KILLED 3. doPost : Replaced integer addition with subtraction → KILLED |
if (currentQty + quantity <= prodotto.getPiecesInStock()) { |
| 76 |
2
1. doPost : removed call to com/popx/persistenza/ProdottoDAO::updateProductQtyInCart → KILLED 2. doPost : Replaced integer addition with subtraction → KILLED |
prodottoDAO.updateProductQtyInCart(session, productId, currentQty + quantity); |
| 77 |
1
1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED |
writeJson(response, true, "Prodotto aggiunto al carrello!"); |
| 78 | return; | |
| 79 | } else { | |
| 80 |
1
1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED |
writeJson(response, false, "Quantità non disponibile nel magazzino."); |
| 81 | return; | |
| 82 | } | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | cart.add(prodotto); | |
| 87 |
1
1. doPost : removed call to com/popx/persistenza/ProdottoDAO::updateProductQtyInCart → KILLED |
prodottoDAO.updateProductQtyInCart(session, productId, quantity); |
| 88 |
1
1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED |
writeJson(response, true, "Prodotto aggiunto al carrello!"); |
| 89 | } | |
| 90 | ||
| 91 | private void writeJson(HttpServletResponse response, boolean success, String message) throws IOException { | |
| 92 |
1
1. writeJson : removed call to javax/servlet/http/HttpServletResponse::setContentType → SURVIVED |
response.setContentType("application/json"); |
| 93 | response.getWriter() | |
| 94 |
1
1. writeJson : removed call to java/io/PrintWriter::write → KILLED |
.write("{\"success\": " + success + ", \"message\": \"" + message + "\"}"); |
| 95 | } | |
| 96 | } | |
Mutations | ||
| 44 |
1.1 |
|
| 47 |
1.1 2.2 |
|
| 48 |
1.1 |
|
| 53 |
1.1 |
|
| 60 |
1.1 |
|
| 62 |
1.1 |
|
| 67 |
1.1 |
|
| 68 |
1.1 |
|
| 73 |
1.1 |
|
| 75 |
1.1 2.2 3.3 |
|
| 76 |
1.1 2.2 |
|
| 77 |
1.1 |
|
| 80 |
1.1 |
|
| 87 |
1.1 |
|
| 88 |
1.1 |
|
| 92 |
1.1 |
|
| 94 |
1.1 |