From 93022f82e3d8b116be16eacb6bdd8fc90a3b4926 Mon Sep 17 00:00:00 2001 From: "funcha.ahamadi" Date: Mon, 13 Dec 2021 11:33:34 +0100 Subject: [PATCH] Merge origin/master Conflicts: src/java/bdd/PompierMySql.java src/java/com/test/servlets/authentifServlet.java web/WEB-INF/CreaPompier.jsp --- src/java/bdd/GardeMySQL.java | 199 ++++++++++++++++++++++++++++ src/java/bdd/gardeMySQL.java | 98 -------------- src/java/bean/Gardes.java | 10 +- src/java/servlet/GardesServlet.java | 2 +- web/WEB-INF/jspf/header.jspf | 17 ++- 5 files changed, 221 insertions(+), 105 deletions(-) create mode 100644 src/java/bdd/GardeMySQL.java delete mode 100644 src/java/bdd/gardeMySQL.java diff --git a/src/java/bdd/GardeMySQL.java b/src/java/bdd/GardeMySQL.java new file mode 100644 index 0000000..fe1bab9 --- /dev/null +++ b/src/java/bdd/GardeMySQL.java @@ -0,0 +1,199 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package bdd; + +import bean.Caserne; +import bean.Gardes; +import bean.Pompier; +import com.sdis29a.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; + +/** + * + * @author funcha.ahamadi + */ +public class GardeMySQL { + private final Connection theConnection = Connexion.getConnect("localhost", + "sdis29", + "adminBDsdis", + "mdpBDsdis"); + public ArrayList getLesDisponibilite(Calendar[] lesDates, int[] lesPeriodes) { + ArrayList lesDispo = new ArrayList<>(); + Gardes uneDispo; + + /* a. Initialisation de la collection lesVentil pour la période, + création d'une ventil par salarié, par date et période + */ + int indiceDateFin = lesDates.length - 1; + Pompier lePompier; + PompierMySQL pm = new PompierMySQL(); + + ArrayList lesPompiers = new ArrayList<>(); + for (Pompier unPompier : lesPompiers) { + for (Calendar uneDate : lesDates) { + for (int unePeriode : lesPeriodes) { + uneDispo = new Gardes(uneDate, unePeriode, unPompier); + lesDispo.add(uneDispo); + } + } + } + + /* b) Maj de la collection lesVentil + */ + readAll(lesDispo, lesDates[0], lesDates[indiceDateFin]); + //System.out.println("lesDispo : " + lesDispo); + return lesDispo; + } + public void readAll(ArrayList lesDispo, Calendar d1, Calendar d2) { + Pompier lePompier; + Gardes uneDispo; + Gardes laDispoTrouvee; + String sql = "SELECT * FROM gardes " + + "INNER JOIN pompier on idPompier = id " + + "WHERE jourGarde BETWEEN ? and ?;"; + try { + PreparedStatement prepStmt = theConnection.prepareStatement(sql); + prepStmt.setDate(1, TrmtDate.getSQLDate(d1)); + prepStmt.setDate(2, TrmtDate.getSQLDate(d2)); + ResultSet resultat = prepStmt.executeQuery(); + 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")); + uneDispo = new Gardes(TrmtDate.getCalDate(resultat.getDate("jourGarde")), + resultat.getInt("periode") + ,lePompier); + // Recherche si uneDispo existe dans les Dispos + int indice = lesDispo.indexOf(uneDispo); + if (indice > -1) { + laDispoTrouvee = lesDispo.get(indice); + laDispoTrouvee.setDisponibilite(resultat.getInt("disponibilite")); + laDispoTrouvee.setIsInBDD(true); + } + } + + prepStmt.close(); + resultat.close(); + } catch (SQLException ex) { + System.out.println("SQLExeption : " + ex.getMessage()); + System.out.println("SQLState : " + ex.getSQLState()); + System.out.println("Code erreur : " + ex.getErrorCode()); + } + } + /** + * Suppression de la garde passé en paramètre + * + * @param v ventilation à supprimer + * @return nb ligne a supprimer + */ + public int delete(Gardes g) { + int nbMaj = -1; + String sql = "DELETE * FROM gardes " + + "WHERE idPompiere=?" + + "AND periode=?" + + "AND jourGarde= ?;"; + try { + PreparedStatement prepStmt = theConnection.prepareStatement(sql); + 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; + } + /** + * 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 (?, ?, ?,?)"; + + 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 garde " + + "SET disponibilite = ? " + + "WHERE idPompier = ? " + + "AND jourGarde = ? " + + "AND periode = ?"; + try { + PreparedStatement prepStmt = theConnection.prepareStatement(sql); + 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); + } + return nbMAJ; + } + + + + + + + + +} diff --git a/src/java/bdd/gardeMySQL.java b/src/java/bdd/gardeMySQL.java deleted file mode 100644 index 442c108..0000000 --- a/src/java/bdd/gardeMySQL.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package bdd; - -import bean.Gardes; -import bean.Pompier; -import com.sdis29a.util.TrmtDate; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Calendar; - -/** - * - * @author funcha.ahamadi - */ -public class gardeMySQL { - private final Connection theConnection = Connexion.getConnect("localhost", - "sdis29", - "adminBDsdis", - "mdpBDsdis"); - public ArrayList getLesDisponibilite(Calendar[] lesDates, int[] lesPeriodes) { - ArrayList lesDispo = new ArrayList<>(); - Gardes uneDispo; - - /* a. Initialisation de la collection lesVentil pour la période, - création d'une ventil par salarié, par date et période - */ - int indiceDateFin = lesDates.length - 1; - Pompier lePompier; - PompierMySQL pm = new PompierMySQL(); - ArrayList lesPompiers = pm.readLesPompiersCaserne(idCaserne); - for (Pompier unPompier : lesPompiers) { - for (Calendar uneDate : lesDates) { - for (int unePeriode : lesPeriodes) { - uneDispo = new Gardes(uneDate, unePeriode, unPompier); - lesDispo.add(uneDispo); - } - } - } - - /* b) Maj de la collection lesVentil - */ - readAll(lesDispo, lesDates[0], lesDates[indiceDateFin]); - //System.out.println("lesDispo : " + lesDispo); - return lesDispo; - } - public void readAll(ArrayList lesDispo, Calendar d1, Calendar d2) { - Pompier lePompier; - Gardes uneDispo; - Gardes laDispoTrouvee; - String sql = "SELECT * FROM gardes " - + "INNER JOIN pompier on idPompier = id " - + "WHERE jourGarde BETWEEN ? and ?;"; - try { - PreparedStatement prepStmt = theConnection.prepareStatement(sql); - 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"), - resultat.getString("nom"), - resultat.getString("prenom"); - uneDispo = new Gardes(TrmtDate.getCalDate(resultat.getDate("jourGarde")), - resultat.getInt("periode") - ,lePompier); - // Recherche si uneDispo existe dans les Dispos - int indice = lesDispo.indexOf(uneDispo); - if (indice > -1) { - laDispoTrouvee = lesDispo.get(indice); - laDispoTrouvee.setActivite(resultat.getInt("disponibilite")); - laDispoTrouvee.setIsInDB(true); - } - - prepStmt.close(); - resultat.close(); - } catch (SQLException ex) { - System.out.println("SQLExeption : " + ex.getMessage()); - System.out.println("SQLState : " + ex.getSQLState()); - System.out.println("Code erreur : " + ex.getErrorCode()); - } - } - - - - - - - - - -} diff --git a/src/java/bean/Gardes.java b/src/java/bean/Gardes.java index f8c6e06..8c19eb1 100644 --- a/src/java/bean/Gardes.java +++ b/src/java/bean/Gardes.java @@ -13,11 +13,11 @@ import java.util.Objects; * @author funcha.ahamadi */ public class Gardes { - private Calendar jourGarde; - private int periode; - private Pompier pompier; - private int disponibilite; - private boolean isInBDD; + public Calendar jourGarde; + public int periode; + public Pompier pompier; + public int disponibilite; + public boolean isInBDD; public Gardes(Calendar jourGarde, int periode, Pompier pompier, int disponibilite, boolean isInBDD) { this.jourGarde = jourGarde; diff --git a/src/java/servlet/GardesServlet.java b/src/java/servlet/GardesServlet.java index bb30347..2cd14ec 100644 --- a/src/java/servlet/GardesServlet.java +++ b/src/java/servlet/GardesServlet.java @@ -81,7 +81,7 @@ public class GardesServlet extends HttpServlet { /* Affichage de la page de ventilation */ - getServletContext().getRequestDispatcher("/WEB-INF/ventilJSP.jsp").forward(request, response); + getServletContext().getRequestDispatcher("/WEB-INF/feuilleGardes.jsp").forward(request, response); } /** diff --git a/web/WEB-INF/jspf/header.jspf b/web/WEB-INF/jspf/header.jspf index 472da67..f000f40 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()}) - +