/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); HttpSession sess = request.getSession(); Utilisateur util = (Utilisateur) sess.getAttribute("user"); ArrayList<Integer> arrayRemove = new ArrayList<Integer>(); try { if (util.getHasmMapPanier().isEmpty()) { throw new ChangeQuantityException("cartEmpty"); } else { for (Entry<Integer, AlbumCart> entry : util.getHasmMapPanier().entrySet()) { Integer idAlbum = entry.getKey(); int qte = Integer.parseInt(request.getParameter("quantity" + idAlbum.toString())); if (qte < 0 || qte > 100) { throw new ChangeQuantityException("qteInvalid"); } if (qte == 0) { arrayRemove.add(idAlbum); } else { AlbumCart alb = entry.getValue(); alb.setQte(qte); entry.setValue(alb); } } for (int i = 0; i < arrayRemove.size(); i++) { util.getHasmMapPanier().remove(arrayRemove.get(i)); } RequestDispatcher rd = request.getRequestDispatcher("cart.jsp"); rd.forward(request, response); } } catch (NumberFormatException e) { RequestDispatcher rd = request.getRequestDispatcher("cart.jsp"); request.setAttribute("message", "errorNumber"); rd.forward(request, response); } catch (ChangeQuantityException e) { RequestDispatcher rd = request.getRequestDispatcher("cart.jsp"); request.setAttribute("message", e); rd.forward(request, response); } }
public void ConfirmerCommande(Utilisateur util) throws CommandeException { try { for (Iterator iter = util.getHasmMapPanier().entrySet().iterator(); iter.hasNext(); ) // Vérification des quantités dans la hashmap { Map.Entry data = (Map.Entry) iter.next(); AlbumCart album = (AlbumCart) data.getValue(); if (album.getQte() < 1) { throw new CommandeException("qteInvalid"); } } Context cont = new InitialContext(); DataSource source = (DataSource) cont.lookup("jdbc/MusicStore"); connexion = source.getConnection(); String requeteSQL = "INSERT INTO COMMANDE (IDUTILISATEUR,DATE) VALUES(?,CURRENT DATE)"; PreparedStatement prepStat = connexion.prepareStatement(requeteSQL); prepStat.setInt(1, util.getIdUtilisateur()); prepStat.executeUpdate(); requeteSQL = "SELECT IDCommande, IDUtilisateur from Commande where IDUTILISATEUR=? AND IDCommande=(SELECT MAX(IDCommande) from Commande)"; prepStat = connexion.prepareStatement(requeteSQL); prepStat.setInt(1, util.getIdUtilisateur()); ResultSet donnees = prepStat.executeQuery(); donnees.next(); Integer idCommande = donnees.getInt(1); if (donnees.getInt(2) == util.getIdUtilisateur()) { for (Iterator iter = util.getHasmMapPanier().entrySet().iterator(); iter.hasNext(); ) // Vérification des quantités dans la hashmap { Map.Entry data = (Map.Entry) iter.next(); AlbumCart album = (AlbumCart) data.getValue(); requeteSQL = "INSERT INTO LIGNECOMMANDE (IDALBUM,IDCOMMANDE,QUANTITE,PRIX) VALUES (?,?,?,?)"; prepStat = connexion.prepareStatement(requeteSQL); prepStat.setInt(1, album.getIdAlbum()); prepStat.setInt(2, idCommande); prepStat.setInt(3, album.getQte()); if (album.getPromo()) prepStat.setDouble(4, album.getPrixPromo()); else prepStat.setDouble(4, album.getPrix()); prepStat.executeUpdate(); } } } catch (SQLException ex) { throw new CommandeException("sqlException"); } catch (NamingException ex) { throw new CommandeException("errorNaming"); } catch (CommandeException ex) { throw new CommandeException(ex.toString()); } finally { try { connexion.close(); } catch (SQLException e) { throw new CommandeException("sqlException"); } } }