This commit is contained in:
clementine.desrucques 2021-12-13 10:22:45 +01:00
parent 5729a4a7eb
commit 785442caf6
5 changed files with 336 additions and 5 deletions

View File

@ -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);

View File

@ -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 <Dispo> lesDispo = (ArrayList<Dispo>) maS.getAttribute("lesVentilInit");
DispoMySql vm = new DispoMySql();
Map<String, String[]> 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++;
}
}
}

View File

@ -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 <code>GET</code> and <code>POST</code>
* 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("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet DispoServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet DispoServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> 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<Dispo> 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 <code>POST</code> 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";
}// </editor-fold>
}

129
src/java/util/TrmtDate.java Normal file
View File

@ -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;
}
}

View File

@ -16,6 +16,10 @@
<servlet-name>PompierServlet</servlet-name>
<servlet-class>servlet.PompierServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DispoServlet</servlet-name>
<servlet-class>servlet.DispoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AuthentifServlet</servlet-name>
<url-pattern>/authentification</url-pattern>
@ -24,6 +28,10 @@
<servlet-name>PompierServlet</servlet-name>
<url-pattern>/pompier</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DispoServlet</servlet-name>
<url-pattern>/DispoServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30