| 1 | package com.popx.presentazione; | |
| 2 | ||
| 3 | import com.popx.modello.ProdottoBean; | |
| 4 | import com.popx.persistenza.ProdottoDAO; | |
| 5 | import com.popx.persistenza.ProdottoDAOImpl; | |
| 6 | ||
| 7 | import javax.servlet.*; | |
| 8 | import javax.servlet.http.*; | |
| 9 | import javax.servlet.annotation.*; | |
| 10 | import java.io.IOException; | |
| 11 | import java.util.ArrayList; | |
| 12 | import java.util.List; | |
| 13 | ||
| 14 | @WebServlet(name = "GetProductsServlet", urlPatterns = {"/getProductsServlet"}) | |
| 15 | public class GetProductsServlet extends HttpServlet { | |
| 16 | ||
| 17 | private static final int PRODUCTS_PER_PAGE = 6; | |
| 18 | private ProdottoDAO prodottoDAO; | |
| 19 | ||
| 20 | // costruttore production | |
| 21 | public GetProductsServlet() { | |
| 22 | this.prodottoDAO = new ProdottoDAOImpl(); | |
| 23 | } | |
| 24 | ||
| 25 | // costruttore test | |
| 26 | public GetProductsServlet(ProdottoDAO prodottoDAO) { | |
| 27 | this.prodottoDAO = prodottoDAO; | |
| 28 | } | |
| 29 | ||
| 30 | @Override | |
| 31 | public void doGet(HttpServletRequest request, HttpServletResponse response) | |
| 32 | throws ServletException, IOException { | |
| 33 | ||
| 34 | String category = request.getParameter("category"); | |
| 35 | String price = request.getParameter("price"); | |
| 36 | String pageParam = request.getParameter("page"); | |
| 37 | ||
| 38 | int currentPage = 1; | |
| 39 | try { | |
| 40 |
1
1. doGet : negated conditional → SURVIVED |
if (pageParam != null) { |
| 41 | currentPage = Math.max(1, Integer.parseInt(pageParam)); | |
| 42 | } | |
| 43 | } catch (NumberFormatException ignored) { | |
| 44 | currentPage = 1; | |
| 45 | } | |
| 46 | ||
| 47 | try { | |
| 48 | List<ProdottoBean> prodotti; | |
| 49 | ||
| 50 |
2
1. doGet : negated conditional → KILLED 2. doGet : negated conditional → KILLED |
if (category != null && !category.isEmpty()) { |
| 51 |
2
1. doGet : negated conditional → KILLED 2. doGet : negated conditional → KILLED |
if (price != null && !price.isEmpty()) { |
| 52 | boolean ascending = "low".equalsIgnoreCase(price); | |
| 53 | prodotti = prodottoDAO.getProdottiByBrandAndPrice(category, ascending); | |
| 54 | } else { | |
| 55 | prodotti = prodottoDAO.getProdottiByBrand(category); | |
| 56 | } | |
| 57 |
2
1. doGet : negated conditional → NO_COVERAGE 2. doGet : negated conditional → KILLED |
} else if (price != null && !price.isEmpty()) { |
| 58 | boolean ascending = "low".equalsIgnoreCase(price); | |
| 59 | prodotti = prodottoDAO.getProdottiSortedByPrice(ascending); | |
| 60 | } else { | |
| 61 | prodotti = prodottoDAO.getAllProducts(); | |
| 62 | } | |
| 63 | ||
| 64 | int totalProducts = prodotti.size(); | |
| 65 |
1
1. doGet : Replaced double division with multiplication → KILLED |
int totalPages = (int) Math.ceil((double) totalProducts / PRODUCTS_PER_PAGE); |
| 66 |
1
1. doGet : negated conditional → SURVIVED |
if (totalPages == 0) totalPages = 1; |
| 67 | ||
| 68 | currentPage = Math.min(currentPage, totalPages); | |
| 69 | ||
| 70 |
2
1. doGet : Replaced integer multiplication with division → SURVIVED 2. doGet : Replaced integer subtraction with addition → KILLED |
int start = (currentPage - 1) * PRODUCTS_PER_PAGE; |
| 71 |
1
1. doGet : Replaced integer addition with subtraction → KILLED |
int end = Math.min(start + PRODUCTS_PER_PAGE, totalProducts); |
| 72 | ||
| 73 | List<ProdottoBean> productsForPage = prodotti.subList(start, end); | |
| 74 | ||
| 75 |
1
1. doGet : removed call to javax/servlet/http/HttpServletRequest::setAttribute → KILLED |
request.setAttribute("products", productsForPage); |
| 76 |
1
1. doGet : removed call to javax/servlet/http/HttpServletRequest::setAttribute → KILLED |
request.setAttribute("currentPage", currentPage); |
| 77 |
1
1. doGet : removed call to javax/servlet/http/HttpServletRequest::setAttribute → KILLED |
request.setAttribute("totalPages", totalPages); |
| 78 | ||
| 79 |
1
1. doGet : removed call to javax/servlet/RequestDispatcher::forward → KILLED |
request.getRequestDispatcher("/jsp/Catalog.jsp").forward(request, response); |
| 80 | ||
| 81 | } catch (Exception e) { | |
| 82 |
1
1. doGet : removed call to javax/servlet/http/HttpServletResponse::sendError → KILLED |
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, |
| 83 | "Errore durante il recupero dei prodotti."); | |
| 84 | } | |
| 85 | } | |
| 86 | } | |
Mutations | ||
| 40 |
1.1 |
|
| 50 |
1.1 2.2 |
|
| 51 |
1.1 2.2 |
|
| 57 |
1.1 2.2 |
|
| 65 |
1.1 |
|
| 66 |
1.1 |
|
| 70 |
1.1 2.2 |
|
| 71 |
1.1 |
|
| 75 |
1.1 |
|
| 76 |
1.1 |
|
| 77 |
1.1 |
|
| 79 |
1.1 |
|
| 82 |
1.1 |