From 785442caf6c645d525c0db3b18c3446e253accce Mon Sep 17 00:00:00 2001 From: "clementine.desrucques" Date: Mon, 13 Dec 2021 10:22:45 +0100 Subject: [PATCH] --- src/java/bdd/DispoMySql.java | 24 ++++-- src/java/form/DispoForm.java | 51 ++++++++++++ src/java/servlet/DispoServlet.java | 129 +++++++++++++++++++++++++++++ src/java/util/TrmtDate.java | 129 +++++++++++++++++++++++++++++ web/WEB-INF/web.xml | 8 ++ 5 files changed, 336 insertions(+), 5 deletions(-) create mode 100644 src/java/form/DispoForm.java create mode 100644 src/java/servlet/DispoServlet.java create mode 100644 src/java/util/TrmtDate.java diff --git a/src/java/bdd/DispoMySql.java b/src/java/bdd/DispoMySql.java index 5d32f53..4b94071 100644 --- a/src/java/bdd/DispoMySql.java +++ b/src/java/bdd/DispoMySql.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.logging.Level; import java.util.logging.Logger; +import util.TrmtDate; /** * @@ -40,8 +41,8 @@ public class DispoMySql { for (Pompier unP : lesPom) { for (Calendar uneDate : lesDates) { for (String unePeriode : lesPeriodes) { - unePomp = new Pompier(uneDate, unePeriode, unP); - lesP.add(unePomp); + uneDispo = new Dispo(uneDate, unePeriode, unP); + lesP.add(uneDispo); } } } @@ -66,14 +67,27 @@ public class DispoMySql { prepStmt.setDate(2, TrmtDate.getSQLDate(d2)); ResultSet resultat = prepStmt.executeQuery(); while (resultat.next()) { - leP = new Pompier(resultat.getInt("id"), + leP = new Pompier( + resultat.getCaserne("laCaserne"), + resultat.getInt("id"), resultat.getString("nom"), resultat.getString("prenom"), - resultat.getString("fonction")); + resultat.getParametre("leStatut"), + resultat.getParametre("leType"), + resultat.getString("mail"), + resultat.getString("login"), + resultat.getString("mdp"), + resultat.getString("adresse"), + resultat.getString("cp"), + resultat.getString("ville"), + resultat.getString("bip"), + resultat.getParametre("leGrade"), + resultat.getString("commentaire") + ); uneDispo = new Dispo(TrmtDate.getCalDate(resultat.getDate("jourVentil")), resultat.getString("periode"), leP); - // Recherche si uneVentil existe dans les Ventil + // Recherche si unpompier existe dans les Pompier int indice = lesDispo.indexOf(uneDispo); if (indice > -1) { laDispoTrouvee = lesDispo.get(indice); diff --git a/src/java/form/DispoForm.java b/src/java/form/DispoForm.java new file mode 100644 index 0000000..87037da --- /dev/null +++ b/src/java/form/DispoForm.java @@ -0,0 +1,51 @@ +/* + * 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 form; + +import bdd.DispoMySql; +import bean.Dispo; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; +import java.util.ArrayList; +import java.util.Map; + +/** + * + * @author clementine.desrucques + */ +public class DispoForm { + HttpServletRequest request; + + public void verifDispo(HttpServletRequest request){ + HttpSession maS = request.getSession(); + ArrayList lesDispo = (ArrayList) maS.getAttribute("lesVentilInit"); + + DispoMySql vm = new DispoMySql(); + Map map = request.getParameterMap(); + String[] lesNvVentil = map.get("lesAct"); + int i = 0; + for(Dispo uneDispo : lesDispo){ + int oldAct = uneDispo.getActivite(); + int newAct = Integer.parseInt(lesNvVentil[i]); + if(oldAct != newAct){ + if(uneDispo.isIsInBD()){ + if(newAct == 0){ + vm.delete(uneDispo); + uneDispo.setIsInBD(false); + } else { + uneDispo.setActivite(newAct); + vm.update(uneDispo); + } + } else { + uneDispo.setActivite(newAct); + vm.create(uneDispo); + uneDispo.setIsInBD(true); + } + } + i++; + } + } +} diff --git a/src/java/servlet/DispoServlet.java b/src/java/servlet/DispoServlet.java new file mode 100644 index 0000000..335404b --- /dev/null +++ b/src/java/servlet/DispoServlet.java @@ -0,0 +1,129 @@ +/* + * 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 servlet; + +import bdd.DispoMySql; +import bean.Dispo; +import form.DispoForm; +import jakarta.servlet.http.HttpServlet; +import java.io.IOException; +import java.io.PrintWriter; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; +import java.util.ArrayList; +import java.util.Calendar; +import util.TrmtDate; + +/** + * + * @author clementine.desrucques + */ +public class DispoServlet extends HttpServlet { + + /** + * Processes requests for both HTTP GET and POST + * 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"); + try ( PrintWriter out = response.getWriter()) { + /* TODO output your page here. You may use following sample code. */ + out.println(""); + out.println(""); + out.println(""); + out.println("Servlet DispoServlet"); + out.println(""); + out.println(""); + out.println("

Servlet DispoServlet at " + request.getContextPath() + "

"); + out.println(""); + out.println(""); + } + } + + // + /** + * Handles the HTTP GET method. + * + * @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet-specific error occurs + * @throws IOException if an I/O error occurs + */ + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { +// processRequest(request, response); + HttpSession maSession = request.getSession(); + if (maSession.getAttribute("lesVentilInit") == null) { + + /* Initialisations des dates de la semaine */ + Calendar[] lesDates = new Calendar[5]; + String[] lesDatesEnString = new String[5]; + lesDates[0] = TrmtDate.getDateDebutSemaine(); + lesDatesEnString[0] = TrmtDate.getDateAAfficher(lesDates[0]); + for (int i = 1; i < lesDates.length; i++) { + lesDates[i] = TrmtDate.addDays(lesDates[0], i); + lesDatesEnString[i] = TrmtDate.getDateAAfficher(lesDates[i]); + } + + maSession.setAttribute("lesDates", lesDates); + maSession.setAttribute("lesDatesEnString", lesDatesEnString); + //System.out.println("lesDates "+ lesDates[4].getTime()); + + /* Initialisation des périodes */ + String[] lesPeriodes = {"AM", "PM"}; + maSession.setAttribute("lesPeriodes", lesPeriodes); + + /* Recherche des ventilations */ + DispoMySql vm = new DispoMySql(); + ArrayList lesVentilInit = vm.getLesDispo(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/gardeJSP.jsp").forward(request, response); + } + + /** + * Handles the HTTP POST method. + * + * @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet-specific error occurs + * @throws IOException if an I/O error occurs + */ + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { +// processRequest(request, response); + DispoForm vf = new DispoForm(); + vf.verifDispo(request); + getServletContext().getRequestDispatcher("/WEB-INF/pompierJSP.jsp").forward(request, response); + } + + /** + * Returns a short description of the servlet. + * + * @return a String containing servlet description + */ + @Override + public String getServletInfo() { + return "Short description"; + }// + +} diff --git a/src/java/util/TrmtDate.java b/src/java/util/TrmtDate.java new file mode 100644 index 0000000..d477a89 --- /dev/null +++ b/src/java/util/TrmtDate.java @@ -0,0 +1,129 @@ +package util; +/* + * Classe abstraite facilitant le traitement des dates + * Toutes les méthodes sont statiques, appel : TrmtDate.nomMéthode + */ + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.Locale; +import java.util.TimeZone; + +/** + * + * @author Dominique_2 + */ +public abstract class TrmtDate { + static Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Paris")); + + /** + * Recherche de la date du début de la semaine courante + * @return date du lundi de la semaine, format Calendar + */ + public static Calendar getDateDebutSemaine() { + return getDateDebutSemaine(getDateDuJour()); + } + + /** + * Recherche de la date du début de semaine à partir de la date passée en paramètre + * @param c Calendar, date de recherche + * @return Calendar, début semaine à partir de c + */ + public static Calendar getDateDebutSemaine(Calendar c) { + Calendar leCal = getClone(c); + leCal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); + //System.out.println("Date du lundi correspondant : " + nvCal.getTime()); + return leCal; + } + /** + * Retourne la date passée en paramètre sous la forme : lun.05/10 par exemple + * @param d date à transformer + * @return chaine de car., forme lun.05/10 par exemple + */ + public static String getDateAAfficher(Calendar d) { + Locale locale = Locale.getDefault(); + String dateAAfficher = d.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, locale); + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM"); + String jrMois = sdf.format(d.getTime()); + dateAAfficher += jrMois; + return dateAAfficher; + } + + /** + * Retourne une date égale à la date passée en paramètre plus le nb jours indiqués + * @param dateInit date initiale, format Calendar + * @param days nb jours à ajouter + * @return dateInit + days, format Calendar + */ + public static Calendar addDays(Calendar dateInit, int days) { + Calendar leCal = getClone(dateInit); + leCal.setTime(dateInit.getTime()); + leCal.add(Calendar.DAY_OF_MONTH, days); + return leCal; + } + + /** + * Retourne une instance de Calendar correspondant à la date au format SQl + * passée en paramètre + * @param sqlDate : date au format java.sql.Date + * @return instance de Calendar + */ + public static Calendar getCalDate(java.sql.Date sqlDate) { + cal.setTime(new java.util.Date(sqlDate.getTime())); + return cal; + } + + /** + * Retourne une date sql à partir d'une instance de Calendar + * @param unCal + * @return date sql + */ + public static java.sql.Date getSQLDate(Calendar unCal) { + return new java.sql.Date(unCal.getTimeInMillis()); + } + + /** + * Retourne une instance de Calendar correspondant à la date + * passée en paramètre + * @param utilDate : date au format java.util.Date + * @return instance de Calendar + */ + public static Calendar getCalDate(java.util.Date utilDate) { + cal.setTime(utilDate); + return cal; + } + + /** + * Retourne la date du jour au format SQL + * @return Date au format sql + */ + public static java.sql.Date getSQLDateDuJour() { + return java.sql.Date.valueOf(java.time.LocalDate.now()); + } + + /** + * Retourne la date du jour au format Calendar + * @return Calendar + */ + public static Calendar getDateDuJour() { + Calendar nvCal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Paris")); + nvCal.set(Calendar.HOUR_OF_DAY, 0); + nvCal.set(Calendar.MINUTE, 0); + nvCal.set(Calendar.SECOND, 0); + nvCal.set(Calendar.MILLISECOND, 0); + //System.out.println("Date du jour : " + nvCal.getTime()); + return nvCal; + } + + /** + * Permet d'obtenir une autre instance de Calendar identique à celle passée en paramètre + * @param c Calendar à cloner + * @return nouvelle instance de Calendar + */ + public static Calendar getClone(Calendar c) { + Calendar leCal = Calendar.getInstance(); + leCal.setTime(c.getTime()); + return leCal; + } +} diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml index a5dfda0..3cec7eb 100644 --- a/web/WEB-INF/web.xml +++ b/web/WEB-INF/web.xml @@ -16,6 +16,10 @@ PompierServlet servlet.PompierServlet + + DispoServlet + servlet.DispoServlet + AuthentifServlet /authentification @@ -24,6 +28,10 @@ PompierServlet /pompier + + DispoServlet + /DispoServlet + 30