CarrelloDAOImpl.java

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

Mutations

50

1.1
Location : salvaCarrello
Killed by : none
removed call to java/sql/Connection::setAutoCommit → SURVIVED

56

1.1
Location : salvaCarrello
Killed by : com.popx.integration.persistenza.CarrelloDAOImplTest.[engine:junit-jupiter]/[class:com.popx.integration.persistenza.CarrelloDAOImplTest]/[method:salvaCarrello_e_ottieniCarrelloPerEmail_funzionano()]
removed call to java/sql/PreparedStatement::setString → KILLED

60

1.1
Location : salvaCarrello
Killed by : com.popx.integration.persistenza.CarrelloDAOImplTest.[engine:junit-jupiter]/[class:com.popx.integration.persistenza.CarrelloDAOImplTest]/[method:salvaCarrello_e_ottieniCarrelloPerEmail_funzionano()]
negated conditional → KILLED

112

1.1
Location : ottieniCarrelloPerEmail
Killed by : com.popx.integration.persistenza.CarrelloDAOImplTest.[engine:junit-jupiter]/[class:com.popx.integration.persistenza.CarrelloDAOImplTest]/[method:salvaCarrello_e_ottieniCarrelloPerEmail_funzionano()]
removed call to java/sql/PreparedStatement::setString → KILLED

115

1.1
Location : ottieniCarrelloPerEmail
Killed by : com.popx.integration.persistenza.CarrelloDAOImplTest.[engine:junit-jupiter]/[class:com.popx.integration.persistenza.CarrelloDAOImplTest]/[method:salvaCarrello_e_ottieniCarrelloPerEmail_funzionano()]
negated conditional → KILLED

121

1.1
Location : ottieniCarrelloPerEmail
Killed by : com.popx.integration.persistenza.CarrelloDAOImplTest.[engine:junit-jupiter]/[class:com.popx.integration.persistenza.CarrelloDAOImplTest]/[method:salvaCarrello_e_ottieniCarrelloPerEmail_funzionano()]
removed call to java/sql/PreparedStatement::setInt → KILLED

124

1.1
Location : ottieniCarrelloPerEmail
Killed by : com.popx.integration.persistenza.CarrelloDAOImplTest.[engine:junit-jupiter]/[class:com.popx.integration.persistenza.CarrelloDAOImplTest]/[method:salvaCarrello_e_ottieniCarrelloPerEmail_funzionano()]
negated conditional → KILLED

143

1.1
Location : ottieniCarrelloPerEmail
Killed by : com.popx.integration.persistenza.CarrelloDAOImplTest.[engine:junit-jupiter]/[class:com.popx.integration.persistenza.CarrelloDAOImplTest]/[method:clearCartByUserEmail_rimuove_carrello_e_prodotti()]
negated conditional → KILLED

2.2
Location : ottieniCarrelloPerEmail
Killed by : com.popx.integration.persistenza.CarrelloDAOImplTest.[engine:junit-jupiter]/[class:com.popx.integration.persistenza.CarrelloDAOImplTest]/[method:salvaCarrello_e_ottieniCarrelloPerEmail_funzionano()]
replaced return value with null for com/popx/persistenza/CarrelloDAOImpl::ottieniCarrelloPerEmail → KILLED

159

1.1
Location : clearCartByUserEmail
Killed by : none
removed call to java/sql/Connection::setAutoCommit → SURVIVED

164

1.1
Location : clearCartByUserEmail
Killed by : com.popx.integration.persistenza.CarrelloDAOImplTest.[engine:junit-jupiter]/[class:com.popx.integration.persistenza.CarrelloDAOImplTest]/[method:clearCartByUserEmail_rimuove_carrello_e_prodotti()]
removed call to java/sql/PreparedStatement::setString → KILLED

167

1.1
Location : clearCartByUserEmail
Killed by : com.popx.integration.persistenza.CarrelloDAOImplTest.[engine:junit-jupiter]/[class:com.popx.integration.persistenza.CarrelloDAOImplTest]/[method:clearCartByUserEmail_rimuove_carrello_e_prodotti()]
removed call to java/sql/PreparedStatement::setString → KILLED

170

1.1
Location : clearCartByUserEmail
Killed by : none
removed call to java/sql/Connection::commit → SURVIVED

Active mutators

Tests examined


Report generated by PIT 1.15.2