package controleur; import Metier.Livre; import Metier.LivreService; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class SupressionLivreExemplaire extends HttpServlet { public static final String VUE_SUPRESSION = "/privateAcces/SupressionLivreExemplaire.jsp"; public static final String VUE_RECHERCHE = "/Recherche.jsp"; public static final String VUE_ACCUEIL = "/Accueil.jsp"; public static final String PARAM_ID = "id"; public static final String PARAM_ACTION = "action"; public static final String PARAM_EXMPLAIRE = "exemplaire"; public static final String ATT_LIVRE = "livre"; public static final String ATT_RESULTAT = "resultat"; private String id; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.getServletContext().getRequestDispatcher(this.VUE_ACCUEIL).forward( request, response ); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.id = request.getParameter(this.PARAM_ID); Livre livre = Metier.MetierFactory.getLivreService().getById(this.id); request.setAttribute(this.ATT_LIVRE, livre); this.getServletContext().getRequestDispatcher(this.VUE_SUPRESSION).forward( request, response ); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); ServletContext context = getServletConfig().getServletContext(); List<Livre> listelivre = new ArrayList(); String resultat = null; int index = 0; String action = request.getParameter(this.PARAM_ACTION); Livre livre = Metier.MetierFactory.getLivreService().getById(this.id); LivreService livreSrv = Metier.MetierFactory.getLivreService(); if(action.equals("livre")){ livreSrv.remove(livre); listelivre = (List<Livre>) session.getAttribute("lastRecherche"); for(int i=0; i<listelivre.size(); i++) { if(listelivre.get(i).getId().equals(this.id)){ index = i; } } listelivre.remove(index); session.setAttribute("lastRecherche", listelivre); if(context.getAttribute("nbLivres") != null){ int nbLivres = (int) context.getAttribute("nbLivres"); nbLivres = nbLivres - livre.getExemplaire(); context.setAttribute("nbLivres", nbLivres); } resultat = "<div class=\"alert alert-success alert-dismissable\">\n" + " <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button>\n" + " Le livre <strong>"+ livre.getTitre() +"</strong> a été supprimé.\n" + "</div>"; } else if(action.equals("exemplaire")){ int exemplaire = Integer.parseInt(request.getParameter(this.PARAM_EXMPLAIRE)); livre.setExemplaire(livre.getExemplaire() - exemplaire); livreSrv.update(livre); if(context.getAttribute("nbLivres") != null){ int nbLivres = (int) context.getAttribute("nbLivres"); nbLivres = nbLivres - exemplaire; context.setAttribute("nbLivres", nbLivres); } resultat = "<div class=\"alert alert-warning alert-dismissable\">\n" + " <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button>\n" + " Le livre <strong>"+ livre.getTitre() +"</strong> a maintenant <strong>"+ livre.getExemplaire() +"</strong> exemplaires.\n" + "</div>"; } request.setAttribute(this.ATT_RESULTAT, resultat); this.getServletContext().getRequestDispatcher(this.VUE_RECHERCHE).forward( request, response ); } @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }