/** * 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 Utilisateur connexion(String login, String pass) throws ConnexionException { Utilisateur user = new Utilisateur(); user.setMail(""); try { Context ctx = new InitialContext(); DataSource source = (DataSource) ctx.lookup("jdbc/MusicStore"); connexion = source.getConnection(); String requeteSQL = "SELECT motdepasse, prenom, IDUTILISATEUR FROM utilisateur WHERE mail = LCASE(?)"; PreparedStatement prepStat = connexion.prepareStatement(requeteSQL); prepStat.setString(1, login); ResultSet donnees = prepStat.executeQuery(); while (donnees.next()) { String passTest = donnees.getString(1); if (passTest.equals(pass) != true) { throw new ConnexionException("wrongPass"); } else { user.setMail(login); user.setPrenom(donnees.getString(2)); user.setIdUtilisateur(donnees.getInt(3)); } } } catch (SQLException e) { throw new ConnexionException("sqlConnexionError"); } catch (NamingException e) { throw new ConnexionException("errorNaming"); } finally { try { connexion.close(); } catch (SQLException e) { throw new ConnexionException("sqlConnexionError"); } } return user; }
public void ajoutUtilisateur(Utilisateur util) throws InscriptionException { try { Context cont = new InitialContext(); DataSource source = (DataSource) cont.lookup("jdbc/MusicStore"); connexion = source.getConnection(); String requeteSQL = "INSERT INTO UTILISATEUR" + "(NOM, PRENOM, ADR_RUE, ADR_NUMERO, ADR_BOITE, ADR_CODEPOSTAL, ADR_LOCALITE,MAIL,MOTDEPASSE,NUMTEL)" + "VALUES(?,?,?,?,?,?,?,LCASE(?),?,?)"; PreparedStatement prepStat = connexion.prepareStatement(requeteSQL); prepStat.setString(1, util.getNom()); prepStat.setString(2, util.getPrenom()); prepStat.setString(3, util.getRue()); prepStat.setInt(4, util.getNumero()); prepStat.setString(5, util.getBoite()); prepStat.setInt(6, util.getCodepostal()); prepStat.setString(7, util.getLocalite()); prepStat.setString(8, util.getMail()); prepStat.setString(9, util.getPassword()); prepStat.setString(10, util.getNumTel()); prepStat.executeUpdate(); } catch (SQLIntegrityConstraintViolationException ex) { throw new InscriptionException("errorMailUsed"); } catch (SQLException ex) { throw new InscriptionException("sqlException"); } catch (NamingException ex) { throw new InscriptionException("errorNaming"); } finally { try { connexion.close(); } catch (SQLException e) { throw new InscriptionException("sqlException"); } } }
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"); } } }