Conflicts: src/java/bdd/gardeMySQL.java src/java/bean/Gardes.java src/java/servlet/GardesServlet.java web/WEB-INF/jspf/header.jspf
201 lines
7.1 KiB
Java
201 lines
7.1 KiB
Java
/*
|
|
* 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 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 {
|
|
private final Connection theConnection = Connexion.getConnect("localhost",
|
|
"sdis29",
|
|
"adminBDsdis",
|
|
"mdpBDsdis");
|
|
public ArrayList<Gardes> getLesDisponibilite(Calendar[] lesDates, int[] lesPeriodes) {
|
|
ArrayList<Gardes> 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<Pompier> 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<Gardes> 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;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|