package controleur; import Metier.Categorie; import Metier.CategorieService; import Metier.Livre; import Metier.LivreService; import java.io.IOException; 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 AddLivre extends HttpServlet { public static final String VUE_RECHERCHE = "/Recherche.jsp"; public static final String CHAMP_TITRE = "titre"; public static final String CHAMP_AUTEUR = "auteur"; public static final String CHAMP_TOME = "tome"; public static final String CHAMP_EXEMPLAIRE = "exemplaire"; public static final String CHAMP_ORIGINE = "origine"; public static final String CHAMP_ANNEE = "annee"; public static final String CHAMP_PRIX = "prix"; public static final String CHAMP_CATEGORIE = "categorie"; public static final String ATT_RESULTAT = "resultat"; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* Affichage de la page de connection */ this.getServletContext().getRequestDispatcher(this.VUE_RECHERCHE).forward( request, response ); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = getServletConfig().getServletContext(); HttpSession session = request.getSession(); /* Récupération des champs du formulaire. */ String titre = request.getParameter(this.CHAMP_TITRE); String auteur = request.getParameter(this.CHAMP_AUTEUR); String tome = request.getParameter(this.CHAMP_TOME); int exemplaire = Integer.parseInt(request.getParameter(this.CHAMP_EXEMPLAIRE)); String origine = request.getParameter(this.CHAMP_ORIGINE); String annee = request.getParameter(this.CHAMP_ANNEE); String prix = request.getParameter(this.CHAMP_PRIX); String categorie = request.getParameter(this.CHAMP_CATEGORIE); String localisation=""; Livre l = new Livre(auteur, titre, localisation, tome, exemplaire, annee, prix, origine); LivreService livreSrv = Metier.MetierFactory.getLivreService(); CategorieService categorieSrv = Metier.MetierFactory.getCategorieService(); Categorie c = categorieSrv.getByNom(categorie); l.setCatégorie(c); livreSrv.add(l); List<Livre> livres = (List<Livre>) session.getAttribute("lastRecherche"); livres.add(l); session.setAttribute("lastRecherche", livres); if(context.getAttribute("nbLivres") != null){ int nbLivres = (int) context.getAttribute("nbLivres"); nbLivres = nbLivres + l.getExemplaire(); context.setAttribute("nbLivres", nbLivres); } String 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>"+ l.getTitre() +"</strong> a été ajouté.\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> }