diff --git a/src/java/bdd/gardeMySQL.java b/src/java/bdd/GardeMySQL.java similarity index 60% rename from src/java/bdd/gardeMySQL.java rename to src/java/bdd/GardeMySQL.java index c82114d..abf917f 100644 --- a/src/java/bdd/gardeMySQL.java +++ b/src/java/bdd/GardeMySQL.java @@ -5,23 +5,26 @@ */ package bdd; +import bean.Caserne; import bean.Gardes; import bean.Pompier; -import util.TrmtDate; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.util.ArrayList; import java.util.Calendar; import java.util.logging.Level; import java.util.logging.Logger; +import util.TrmtDate; /** * * @author funcha.ahamadi */ -public class gardeMySQL { +public class GardeMySQL { private final Connection theConnection = Connexion.getConnect("localhost", "sdis29", "adminBDsdis", @@ -36,7 +39,8 @@ public class gardeMySQL { int indiceDateFin = lesDates.length - 1; Pompier lePompier; PompierMySQL pm = new PompierMySQL(); - ArrayList lesPompiers = pm.readLesPomprangeiersCaserne(idCaserne); + + ArrayList lesPompiers = new ArrayList<>(); for (Pompier unPompier : lesPompiers) { for (Calendar uneDate : lesDates) { for (int unePeriode : lesPeriodes) { @@ -64,17 +68,17 @@ public class gardeMySQL { prepStmt.setDate(1, TrmtDate.getSQLDate(d1)); prepStmt.setDate(2, TrmtDate.getSQLDate(d2)); ResultSet resultat = prepStmt.executeQuery(); - while (resultat.next()) - // public Pompier(Caserne laCaserne, int id, String nom, String prenom) { - lePompier = new Pompier( - resultat.getCaserne("laCaserne"), + while (resultat.next()) { + CaserneMySQL laCaserneMySQL = new CaserneMySQL(); + int idCaserne = resultat.getInt("idCaserne"); + Caserne laCaserne = laCaserneMySQL.read(idCaserne); + lePompier = new Pompier(laCaserne, resultat.getInt("id"), resultat.getString("nom"), - resultat.getString("prenom") - ); + resultat.getString("prenom")); uneDispo = new Gardes(TrmtDate.getCalDate(resultat.getDate("jourGarde")), - resultat.getInt("periode"), - lePompier); + resultat.getInt("periode") + ,lePompier); // Recherche si uneDispo existe dans les Dispos int indice = lesDispo.indexOf(uneDispo); if (indice > -1) { @@ -82,6 +86,7 @@ public class gardeMySQL { laDispoTrouvee.setDisponibilite(resultat.getInt("disponibilite")); laDispoTrouvee.setIsInBDD(true); } + } prepStmt.close(); resultat.close(); @@ -91,93 +96,105 @@ public class gardeMySQL { System.out.println("Code erreur : " + ex.getErrorCode()); } } - /** - * Suppression de la ventilation passé en param - * @param v : ventilation - * @return : nb lignes supprimées + /** + * Suppression de la garde passé en paramètre + * + * @param v ventilation à supprimer + * @return nb ligne a supprimer */ - public int delete(Gardes v) { + public int delete(Gardes g) { int nbMaj = -1; - String sql = "DELETE FROM gardes " - + "WHERE idPompier = ? AND" - + "periode = ? AND" - + "jourGarde = ?;"; + String sql = "DELETE * FROM gardes " + + "WHERE idPompiere=?" + + "AND periode=?" + + "AND jourGarde= ?;"; try { PreparedStatement prepStmt = theConnection.prepareStatement(sql); - prepStmt.setInt(1, v.getPompier().getId()); - prepStmt.setInt(2, v.getPeriode()); - prepStmt.setDate(3, TrmtDate.getSQLDate(v.getJourGarde())); - nbMaj= prepStmt.executeUpdate(); - - } catch (SQLException ex) { + prepStmt.setInt(1, g.getPompier().getId()); + prepStmt.setInt(2, g.getPeriode()); + prepStmt.setInt(3, g.getPeriode()); + prepStmt.setDate(3, TrmtDate.getSQLDate(g.getJourGarde())); + nbMaj = prepStmt.executeUpdate(); + + } catch (SQLException ex) { System.out.println("SQLExeption : " + ex.getMessage()); System.out.println("SQLState : " + ex.getSQLState()); System.out.println("Code erreur : " + ex.getErrorCode()); } - - return nbMaj; - } - + return nbMaj; + } + /** + * Creation de la ventilation passé en paramètre ventilation à créer + * + * @return : identifiant de la ventilation créé, ou -1 si erreur + */ + public int create(Gardes g) { + int idCree = -1; + String sql = "INSERT into gardes " + + "(jourGarde, periode, idPompier, disponibilite) " + + "VALUES (?, ?, ?,?)"; - public int update(Gardes v) { + try { + PreparedStatement prepStmt = null; + prepStmt = theConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + prepStmt.setDate(1, TrmtDate.getSQLDate(g.getJourGarde())); + prepStmt.setInt(2, g.getPeriode()); + prepStmt.setInt(3, g.getPompier().getId()); + prepStmt.setInt(4, g.getDisponibilite()); + + System.out.println(prepStmt); + int nbLigne = prepStmt.executeUpdate(); + + // Recherche du no du pompier créé + if (nbLigne > 0) { + ResultSet result = prepStmt.getGeneratedKeys(); + if (result.next()) { + idCree = result.getInt(1); + + } + } + prepStmt.close(); + } catch (SQLException ex) { + System.out.println("SQLExeption : " + ex.getMessage()); + System.out.println("SQLState : " + ex.getSQLState()); + System.out.println("Code erreur : " + ex.getErrorCode()); + } + + return idCree; + } + /** + * Mise à jour de la ventilation passée en paramètre + * + * @param v : ventilation à mettre à jour + * @return : nb lignes mises à jour, -1 si erreur + */ + public int update(Gardes g) { int nbMAJ = -1; - String sql = "UPDATE gardes " - + "SET ventil = ? " - + "WHERE idPompier= ? " + String sql = "UPDATE garde " + + "SET disponibilite = ? " + + "WHERE idPompier = ? " + "AND jourGarde = ? " + "AND periode = ?"; try { PreparedStatement prepStmt = theConnection.prepareStatement(sql); - prepStmt.setInt(1, v.getDisponibilite()); - prepStmt.setInt(2, v.getPompier().getId()); - prepStmt.setDate(3, TrmtDate.getSQLDate(v.getJourGarde())); - prepStmt.setInt(4, v.getPeriode()); + prepStmt.setInt(1, g.getDisponibilite()); + prepStmt.setInt(2, g.getPompier().getId()); + prepStmt.setDate(3, TrmtDate.getSQLDate(g.getJourGarde())); + prepStmt.setInt(4, g.getPeriode()); System.out.println("update : " + prepStmt); nbMAJ = prepStmt.executeUpdate(); } catch (SQLException ex) { - Logger.getLogger(gardeMySQL.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(GardeMySQL.class.getName()).log(Level.SEVERE, null, ex); } return nbMAJ; } - /** - * Création d'un ventil - * @param v : ventil à créer - * @return : id du ventil - */ - public int create(Gardes v) { - int nbCre = -1; - String sql = "INSERT INTO gardes " - + "(jourGarde, periode, idPompier, ventil)" - + "VALUES (?, ?, ?, ?);"; - try { - PreparedStatement prepStmt = theConnection.prepareStatement(sql); - prepStmt.setDate(1, TrmtDate.getSQLDate(v.getJourGarde())); - prepStmt.setInt(2, v.getPeriode()); - prepStmt.setInt(3, v.getPompier().getId()); - prepStmt.setInt(4, v.getDisponibilite()); - int nbLigne= prepStmt.executeUpdate(); - if (nbLigne > 0) { - ResultSet result = prepStmt.getGeneratedKeys(); - if (result.next()) { - nbCre = result.getInt(1); - } - } - - } catch (SQLException ex) { - System.out.println("SQLExeption : " + ex.getMessage()); - System.out.println("SQLState : " + ex.getSQLState()); - System.out.println("Code erreur : " + ex.getErrorCode()); - } - - return nbCre; - } - - - + + + } diff --git a/src/java/form/DispoForm.java b/src/java/form/DispoForm.java index dc6f1c5..fc7022b 100644 --- a/src/java/form/DispoForm.java +++ b/src/java/form/DispoForm.java @@ -5,7 +5,8 @@ */ package form; -import bdd.gardeMySQL; +import bdd.GardeMySQL; + import bean.Gardes; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; @@ -23,7 +24,7 @@ public class DispoForm { HttpSession maS = request.getSession(); ArrayList lesG = (ArrayList) maS.getAttribute("lesVentilInit"); - gardeMySQL vm = new gardeMySQL(); + GardeMySQL vm = new GardeMySQL(); Map map = request.getParameterMap(); String[] lesNvVentil = map.get("lesAct"); int i = 0; diff --git a/src/java/servlet/GardesServlet.java b/src/java/servlet/GardesServlet.java index b491698..2e7300e 100644 --- a/src/java/servlet/GardesServlet.java +++ b/src/java/servlet/GardesServlet.java @@ -5,7 +5,8 @@ */ package servlet; -import bdd.gardeMySQL; +import bdd.GardeMySQL; + import bean.Gardes; import bean.Pompier; import form.DispoForm; @@ -66,6 +67,8 @@ public class GardesServlet extends HttpServlet { throws ServletException, IOException { //processRequest(request, response); HttpSession maSession = request.getSession(); + + if(maSession.getAttribute("lesVentilInit") == null){ ArrayList lesPompiers = (ArrayList) maSession.getAttribute("lesPompiers"); Calendar[] lesDates = new Calendar[7]; String[] lesDatesEnString = new String[7]; @@ -77,19 +80,25 @@ public class GardesServlet extends HttpServlet { lesDatesEnString[i] = TrmtDate.getDateAAfficher(lesDates[i]); } maSession.setAttribute("lesDates", lesDates); + maSession.setAttribute("lesDatesEnString", lesDatesEnString); + System.out.println(lesDatesEnString.toString()); /*Initialisation des periodes */ int[] lesPeriodes = {1,2,3,4}; maSession.setAttribute("lesPeriodes", lesPeriodes); + System.out.println(lesPeriodes.toString()); /* Recherche des ventilations */ - gardeMySQL vm = new gardeMySQL(); + + GardeMySQL vm = new GardeMySQL(); ArrayList lesVentilInit = vm.getLesDisponibilite(lesDates, lesPeriodes); //System.out.println("lesVentilInit : " + lesVentilInit); maSession.setAttribute("lesVentilInit", lesVentilInit); + + /* Initialisation des couleurs */ String[] lesCouleurs = {"blanc", "gris", "jaune"}; maSession.setAttribute("lesCouleurs", lesCouleurs); - + } /* Affichage de la page de ventilation */ getServletContext().getRequestDispatcher("/WEB-INF/feuilleGardes.jsp").forward(request, response); } diff --git a/src/java/servlet/PompierServlet.java b/src/java/servlet/PompierServlet.java index 2ebd905..946770c 100644 --- a/src/java/servlet/PompierServlet.java +++ b/src/java/servlet/PompierServlet.java @@ -36,7 +36,7 @@ public class PompierServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { + try ( PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println(""); out.println(""); @@ -74,7 +74,7 @@ public class PompierServlet extends HttpServlet { boolean isNumeric = StringUtils.isStrictlyNumeric(pageEnString); if (isNumeric) { pageDemandee = Integer.parseInt(pageEnString); - if (pageDemandee != 2 && pageDemandee != 3 && pageDemandee !=5) { // erreur + if (pageDemandee != 2 && pageDemandee != 3 && pageDemandee != 5) { // erreur erreur = true; } } else { @@ -101,16 +101,13 @@ public class PompierServlet extends HttpServlet { page = 3; } } - if(erreur==false && pageDemandee==5){ //page gardes - getServletContext().getRequestDispatcher("/WEB-INF/feuilleGardes.jsp").forward(request, response); - - } + if (erreur) { request.getRequestDispatcher("/authentification").forward(request, response); // redirection servlet } else { request.setAttribute("page", page); maSession.setAttribute("lePompier", lePompier); - getServletContext().getRequestDispatcher("/WEB-INF/" + jsp).forward(request, response); + getServletContext().getRequestDispatcher("/WEB-INF/" + jsp).forward(request, response); } } @@ -130,16 +127,16 @@ public class PompierServlet extends HttpServlet { depuis la validation du formulaire de choix du pompier à afficher (btAfficherPompier = "Afficher") depuis le bouton Ajouter du formulaire du choix du pompier (btAfficherPompier = "Ajouter") depuis le bouton de validation de mise à jour d'un pompier (btAfficherPompier = "ValiderMaj") - */ + */ int erreur = 0; String btValue = null; - ArrayList valeursBt = new ArrayList() ; - valeursBt.add("Afficher") ; - valeursBt.add("Ajouter") ; - valeursBt.add("ValiderMaj") ; - + ArrayList valeursBt = new ArrayList(); + valeursBt.add("Afficher"); + valeursBt.add("Ajouter"); + valeursBt.add("ValiderMaj"); + // Test du bouton btAfficherPompier - if (request.getParameter("btAfficherPompier")==null) { + if (request.getParameter("btAfficherPompier") == null) { erreur = 1; } else { btValue = request.getParameter("btAfficherPompier"); @@ -152,7 +149,7 @@ public class PompierServlet extends HttpServlet { //response.sendRedirect("/sdis29M1/authentification"); return; } - + HttpSession maSession = request.getSession(); ArrayList lesPompiers = (ArrayList) maSession.getAttribute("lesPompiers"); if (request.getParameter("btAfficherPompier").equals("Afficher") @@ -162,12 +159,12 @@ public class PompierServlet extends HttpServlet { maSession.setAttribute("lePompier", lesPompiers.get(noPompier)); request.setAttribute("page", 3); } - + if (request.getParameter("btAfficherPompier").equals("Ajouter")) { maSession.removeAttribute("lePompier"); } - - if (request.getParameter("btAfficherPompier").equals("ValiderMaj")) { + + if (request.getParameter("btAfficherPompier").equals("ValiderMaj")) { PompierForm pf = new PompierForm(request); boolean valid = pf.ctrlPompier(); // if (maSession.getAttribute("lePompier")==null) { @@ -182,8 +179,8 @@ public class PompierServlet extends HttpServlet { // } // } pf.majBDD(); - } - + } + getServletContext().getRequestDispatcher("/WEB-INF/pompierJSP.jsp").forward(request, response); } diff --git a/web/WEB-INF/feuilleGardes.jsp b/web/WEB-INF/feuilleGardes.jsp index 871d4e8..d5ab9ed 100644 --- a/web/WEB-INF/feuilleGardes.jsp +++ b/web/WEB-INF/feuilleGardes.jsp @@ -3,20 +3,9 @@ Created on : 6 déc. 2021, 15:07:45 Author : funcha.ahamadi --%> - <%@page contentType="text/html" pageEncoding="UTF-8"%> - - Feuille de gardes - -
- - - - - - - - +<%@include file= "jspf/debutJSP.jspf" %> + @@ -59,4 +48,35 @@
N° de BIPNom Prenom ${uneDate}
-
\ No newline at end of file +
+ + + Feuille de gardes + +
+ + + + + + + + + + + + + + + + + + +
NoNom Prenom ${uneDate}
${unePeriode}
+
+
+ + + + + diff --git a/web/WEB-INF/jspf/formulairePompier.jspf b/web/WEB-INF/jspf/formulairePompier.jspf index 77c8c47..70c1d7a 100644 --- a/web/WEB-INF/jspf/formulairePompier.jspf +++ b/web/WEB-INF/jspf/formulairePompier.jspf @@ -175,6 +175,9 @@ + + +
${message}
diff --git a/web/WEB-INF/jspf/header.jspf b/web/WEB-INF/jspf/header.jspf index b4a9737..9baa2c7 100644 --- a/web/WEB-INF/jspf/header.jspf +++ b/web/WEB-INF/jspf/header.jspf @@ -16,7 +16,7 @@ ${sessionScope.lePompierConnecte.getNom()} ( ${sessionScope.lePompierConnecte.getLeStatut().getValeur()}) - +