diff --git a/Projet/projet.sql b/Projet/projet.sql index e146c12..9243318 100644 --- a/Projet/projet.sql +++ b/Projet/projet.sql @@ -1,6 +1,7 @@ DROP TABLE IF EXISTS bloc; +DROP TABLE IF EXISTS partage; DROP TABLE IF EXISTS page; -DROP TABLE IF EXISTS messages; +DROP TABLE IF EXISTS message; DROP TABLE IF EXISTS utilisateur; CREATE TABLE utilisateur ( @@ -14,7 +15,7 @@ CREATE TABLE page ( id INT PRIMARY KEY AUTO_INCREMENT, titre VARCHAR(200) NOT NULL, date_creation DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - date_modification DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + date_modification DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, auteur_id INT NOT NULL, droits ENUM('LECTURE', 'ECRITURE', 'ADMIN') NOT NULL, FOREIGN KEY (auteur_id) REFERENCES utilisateur(id) ON DELETE CASCADE @@ -22,10 +23,10 @@ CREATE TABLE page ( CREATE TABLE bloc ( id INT PRIMARY KEY AUTO_INCREMENT, - type ENUM('TEXTE', 'LISTE', 'TITRE', 'CODE', 'PAGE') NOT NULL, + type ENUM('TEXTE', 'LISTE', 'TITRE', 'CODE', 'PAGE') NOT NULL DEFAULT 'TEXTE', contenu TEXT, date_creation DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - date_modification DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + date_modification DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, page_id INT NOT NULL, ordre INT NOT NULL, auteur_id INT NOT NULL, @@ -34,33 +35,26 @@ CREATE TABLE bloc ( FOREIGN KEY (auteur_id) REFERENCES utilisateur(id) ON DELETE CASCADE ); -CREATE TABLE messages ( +CREATE TABLE message ( id INT AUTO_INCREMENT PRIMARY KEY, login VARCHAR(100) NOT NULL, contenu TEXT, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP ); -DELIMITER // +CREATE TABLE partage ( + id INT AUTO_INCREMENT PRIMARY KEY, + page_id INT NOT NULL, + utilisateur_id INT NOT NULL, + droits ENUM('LECTURE', 'ECRITURE') NOT NULL DEFAULT 'LECTURE', + date_partage DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (page_id) REFERENCES page(id) ON DELETE CASCADE, + FOREIGN KEY (utilisateur_id) REFERENCES utilisateur(id) ON DELETE CASCADE, + UNIQUE(page_id, utilisateur_id) -- Empêche les doublons de partage +); -CREATE TRIGGER trg_bloc_maj -BEFORE UPDATE ON bloc -FOR EACH ROW -BEGIN - SET NEW.date_modification = NOW(); -END; -// -DELIMITER ; +CREATE INDEX idx_page_auteur ON page(auteur_id); +CREATE INDEX idx_bloc_page ON bloc(page_id); +CREATE INDEX idx_partages_user ON partage(utilisateur_id); -DELIMITER // - -CREATE TRIGGER trg_page_maj -BEFORE UPDATE ON page -FOR EACH ROW -BEGIN - SET NEW.date_modification = NOW(); -END; -// - -DELIMITER ; diff --git a/Projet/src/main/java/projet/AfficherPage.java b/Projet/src/main/java/projet/AfficherPage.java index 076e20f..0def7cd 100644 --- a/Projet/src/main/java/projet/AfficherPage.java +++ b/Projet/src/main/java/projet/AfficherPage.java @@ -24,11 +24,16 @@ public class AfficherPage extends HttpServlet { if(u != null) { u.chargerPages(); + u.chargerPagesPartagees(); ArrayList listePages = u.getListePages(); + ArrayList listePagesPartagees = u.getListePagesPartagees(); ArrayList listeMessages = Message.getListeMessages(); + ArrayList listeUtilisateurs = Utilisateur.getListeUtilisateurs(); request.setAttribute("listePages", listePages); + request.setAttribute("listePagesPartagees", listePagesPartagees); request.setAttribute("listeMessages", listeMessages); + request.setAttribute("listeUtilisateurs", listeUtilisateurs); if (idStr != null ) { try { diff --git a/Projet/src/main/java/projet/Bloc.java b/Projet/src/main/java/projet/Bloc.java index 8678b8d..74e1918 100644 --- a/Projet/src/main/java/projet/Bloc.java +++ b/Projet/src/main/java/projet/Bloc.java @@ -4,7 +4,9 @@ import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.time.LocalDate; import java.util.HashMap; import java.util.Map; @@ -31,9 +33,6 @@ public class Bloc extends ParamBD { } public Bloc(int id, Type type, String contenu, int ordre) { - if (contenu == null || contenu.isEmpty()) { - throw new IllegalArgumentException("Le contenu ne peut pas être vide."); - } this.id = id; this.type = type; this.contenu = contenu; @@ -103,13 +102,14 @@ public class Bloc extends ParamBD { return "Bloc{id=" + id + ", type=" + type + ", contenu='" + contenu + "', dateCreation=" + dateCreation + ", dateModification=" + dateModification + ", ordre=" + ordre + "}"; } - protected static void ajouterBloc(int idU, int idP, String contenu, int ordre, Type type, LocalDate dl, Map metadata) { + protected static int ajouterBloc(int idU, int idP, String contenu, int ordre, Type type, LocalDate dl, Map metadata) { + int idGenere = -1 ; try { Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); String sql = " INSERT INTO bloc(type, contenu, date_creation, date_modification, page_id, ordre, auteur_id, metadata)" + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)" + ";"; - PreparedStatement pst = connexion.prepareStatement(sql); + PreparedStatement pst = connexion.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); pst.setString(1, type.name()); pst.setString(2, contenu); pst.setDate(3, Date.valueOf(dl)); @@ -119,14 +119,46 @@ public class Bloc extends ParamBD { pst.setInt(7, idU); pst.setString(8, metadata.toString()); pst.executeUpdate(); - + + ResultSet rs = pst.getGeneratedKeys(); + if (rs.next()) { + idGenere = rs.getInt(1); + } + + rs.close(); pst.close(); connexion.close(); } catch (SQLException e) { e.printStackTrace(); } + + return idGenere; } + protected static void ajouterBlocVide(int idU, int idP) { + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = "INSERT INTO bloc(type, page_id, ordre, auteur_id, contenu, metadata)" + + " VALUES (?, ?, ?, ?, '', '{}');"; + PreparedStatement pst = connexion.prepareStatement(sql); + + Type type = Type.TEXTE; + int ordre = 0; + + pst.setString(1, type.name()); + pst.setInt(2, idP); + pst.setInt(3, ordre); + pst.setInt(4, idU); + + pst.executeUpdate(); + pst.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public static void updateBloc(int idBloc, String nouveauContenu) { try { Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); diff --git a/Projet/src/main/java/projet/Message.java b/Projet/src/main/java/projet/Message.java index cc99a58..a1bebae 100644 --- a/Projet/src/main/java/projet/Message.java +++ b/Projet/src/main/java/projet/Message.java @@ -38,7 +38,7 @@ public class Message extends ParamBD{ protected static void ajouterMessage(String login, String contenu) { try { Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); - String sql = " INSERT INTO messages(contenu, login)" + String sql = " INSERT INTO message(contenu, login)" + " VALUES (?, ?)" + ";"; PreparedStatement pst = connexion.prepareStatement(sql); @@ -59,7 +59,7 @@ public class Message extends ParamBD{ try { Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); String sql = " SELECT login, contenu" - + " FROM messages" + + " FROM message" + ";"; PreparedStatement pst = connexion.prepareStatement(sql); ResultSet rs = pst.executeQuery(); @@ -79,4 +79,19 @@ public class Message extends ParamBD{ return messages; } + + protected static void effacerMessage() { + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " DELETE FROM message" + + ";"; + PreparedStatement pst = connexion.prepareStatement(sql); + pst.executeUpdate(); + + pst.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } } \ No newline at end of file diff --git a/Projet/src/main/java/projet/NouveauBloc.java b/Projet/src/main/java/projet/NouveauBloc.java index 26eb109..6ea9a1a 100644 --- a/Projet/src/main/java/projet/NouveauBloc.java +++ b/Projet/src/main/java/projet/NouveauBloc.java @@ -1,6 +1,5 @@ package projet; -import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; @@ -26,8 +25,7 @@ public class NouveauBloc extends HttpServlet { Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); if(u != null) { - RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp"); - rd.forward(request, response); + response.sendRedirect("AfficherPage"); }else { response.sendRedirect("/Projet/"); } @@ -45,18 +43,18 @@ public class NouveauBloc extends HttpServlet { String ordreStr = request.getParameter("ordre"); String pageIdStr = request.getParameter("pageId"); + + int ordre = Integer.parseInt(ordreStr); int pageId = Integer.parseInt(pageIdStr); Bloc.Type type = Bloc.Type.valueOf(typeStr); Map metadata = new HashMap<>(); - - if (contenu == null || contenu.isEmpty()) { - response.sendRedirect("AfficherPage"); - } else { - Bloc.ajouterBloc(u.getId(), pageId, contenu, ordre, type, LocalDate.now(), metadata); - response.sendRedirect("AfficherPage"); - } + int idGenere = Bloc.ajouterBloc(u.getId(), pageId, contenu, ordre, type, LocalDate.now(), metadata); + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + response.getWriter().write("{\"idGenere\": " + idGenere + "}"); + } else { response.sendRedirect("/Projet/"); } diff --git a/Projet/src/main/java/projet/NouvellePage.java b/Projet/src/main/java/projet/NouvellePage.java index 06212eb..47aa579 100644 --- a/Projet/src/main/java/projet/NouvellePage.java +++ b/Projet/src/main/java/projet/NouvellePage.java @@ -1,6 +1,5 @@ package projet; -import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; @@ -24,8 +23,7 @@ public class NouvellePage extends HttpServlet { Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); if(u != null) { - RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp"); - rd.forward(request, response); + response.sendRedirect("AfficherPage"); }else { response.sendRedirect("/Projet/"); } @@ -44,7 +42,9 @@ public class NouvellePage extends HttpServlet { response.sendRedirect("AfficherPage"); } else { int id = Page.ajouterPage(u.getId(), titre, LocalDate.now()); - if (id !=-1) response.sendRedirect("AfficherPage?id=" + id); + if (id !=-1) { + response.sendRedirect("AfficherPage?id=" + id); + } else response.sendRedirect("AfficherPage"); } } else { diff --git a/Projet/src/main/java/projet/Page.java b/Projet/src/main/java/projet/Page.java index d3a42e4..072124b 100644 --- a/Projet/src/main/java/projet/Page.java +++ b/Projet/src/main/java/projet/Page.java @@ -15,6 +15,7 @@ import projet.Bloc.Type; public class Page extends ParamBD { private int id; + private int auteur_id; private String titre; private LocalDate dateCreation; private LocalDate dateModification; @@ -31,19 +32,21 @@ public class Page extends ParamBD { } - public Page(int id, String titre, LocalDate dateCreation, LocalDate dateModification, Droit droits) { + public Page(int id, int auteur_id, String titre, LocalDate dateCreation, LocalDate dateModification, Droit droits) { if (titre == null || titre.isEmpty()) { throw new IllegalArgumentException("Le contenu ne peut pas être vide."); } this.id = id; + this.auteur_id = auteur_id; this.titre = titre; this.dateCreation = dateCreation; this.dateModification = dateModification; this.droits = droits; } - public Page(int id, String titre) { - this(id, titre, null, null, Droit.ADMIN); // Appelle le constructeur avec droits = null + public Page(int id, int auteur_id, String titre) { + this.id = id; + this.titre = titre; } public int getId() { @@ -53,6 +56,14 @@ public class Page extends ParamBD { public void setId(int id) { this.id = id; } + + public int getAuteur_id() { + return auteur_id; + } + + public void setAuteur_id(int auteur_id) { + this.auteur_id = auteur_id; + } public String getTitre() { return titre; @@ -118,7 +129,7 @@ public class Page extends ParamBD { if (rs.next()) { idGenere = rs.getInt(1); } - + rs.close(); pst.close(); connexion.close(); @@ -126,6 +137,10 @@ public class Page extends ParamBD { e.printStackTrace(); } + if (idGenere != -1) { + Bloc.ajouterBlocVide(idU, idGenere); + } + return idGenere; } @@ -140,14 +155,21 @@ public class Page extends ParamBD { sql = " SELECT titre" + " FROM page" + " WHERE id=? AND auteur_id = ?" + + " UNION" + + " SELECT titre" + + " FROM partage" + + " JOIN page ON partage.page_id = page.id" + + " WHERE partage.utilisateur_id = ?" + ";"; pst = connexion.prepareStatement(sql); pst.setInt(1, id); pst.setInt(2, idU); + pst.setInt(3, idU); rs = pst.executeQuery(); while(rs.next()) { titre = rs.getString("titre"); page.titre = titre; + page.id = id; } if(titre != null) { sql = " SELECT id, contenu, type, ordre" @@ -170,6 +192,9 @@ public class Page extends ParamBD { page.listeBlocs.add(bloc); } } + rs.close(); + pst.close(); + connexion.close(); } catch (SQLException e) { e.printStackTrace(); } @@ -189,8 +214,31 @@ public class Page extends ParamBD { pst.setInt(2, idU); pst.executeUpdate(); + pst.close(); + connexion.close(); } catch (SQLException e) { e.printStackTrace(); } } + + public static void partagerPage(int idPage, int idU, int idP) { + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " INSERT INTO partage (page_id, utilisateur_id)" + + " SELECT ?, ?" + + " FROM page" + + " WHERE id = ? AND auteur_id = ?" + + ";"; + PreparedStatement pst = connexion.prepareStatement(sql); + + pst.setInt(1, idPage); + pst.setInt(2, idP); + pst.setInt(3, idPage); + pst.setInt(4, idU); + pst.executeUpdate(); + + } catch (SQLException e) { + e.printStackTrace(); + } + } } diff --git a/Projet/src/main/java/projet/SupprimerBloc.java b/Projet/src/main/java/projet/SupprimerBloc.java index e307f0f..232b0b9 100644 --- a/Projet/src/main/java/projet/SupprimerBloc.java +++ b/Projet/src/main/java/projet/SupprimerBloc.java @@ -23,8 +23,7 @@ public class SupprimerBloc extends HttpServlet { Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); if(u != null) { - RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp"); - rd.forward(request, response); + response.sendRedirect("AfficherPage"); }else { response.sendRedirect("/Projet/"); } diff --git a/Projet/src/main/java/projet/SupprimerPage.java b/Projet/src/main/java/projet/SupprimerPage.java index 283e340..261e6fb 100644 --- a/Projet/src/main/java/projet/SupprimerPage.java +++ b/Projet/src/main/java/projet/SupprimerPage.java @@ -23,8 +23,7 @@ public class SupprimerPage extends HttpServlet { Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); if(u != null) { - RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp"); - rd.forward(request, response); + response.sendRedirect("AfficherPage"); }else { response.sendRedirect("/Projet/"); } diff --git a/Projet/src/main/java/projet/Tchat.java b/Projet/src/main/java/projet/Tchat.java index ef4f84a..bda71c6 100644 --- a/Projet/src/main/java/projet/Tchat.java +++ b/Projet/src/main/java/projet/Tchat.java @@ -18,11 +18,7 @@ public class Tchat { @OnMessage public void onMessage(String message, Session senderSession) throws IOException { - System.out.println(message); String[] parts = message.split(" : ", 2); - System.out.println(parts[0].trim()); - System.out.println(parts[1].trim()); - System.out.println(""); if (parts.length == 2) { String login = parts[0].trim(); String contenu = parts[1].trim(); diff --git a/Projet/src/main/java/projet/Utilisateur.java b/Projet/src/main/java/projet/Utilisateur.java index 652e9b4..784b016 100644 --- a/Projet/src/main/java/projet/Utilisateur.java +++ b/Projet/src/main/java/projet/Utilisateur.java @@ -18,6 +18,7 @@ public class Utilisateur extends ParamBD { private String login; private Privilege privilege; private ArrayList listePages; + private ArrayList listePagesPartagees; public enum Privilege { ADMIN, @@ -25,6 +26,14 @@ public class Utilisateur extends ParamBD { GUEST } + public Utilisateur(int id, String login) { + if (login == null || login.isEmpty()) { + throw new IllegalArgumentException("Le pseudo ne peut pas être vide."); + } + this.id = id; + this.login = login; + } + public Utilisateur(int id, String login, Privilege privilege) { if (login == null || login.isEmpty()) { throw new IllegalArgumentException("Le pseudo ne peut pas être vide."); @@ -41,14 +50,6 @@ public class Utilisateur extends ParamBD { public void setId(int id) { this.id = id; } - - public Privilege getprivilege() { - return privilege; - } - - public void setprivilege(Privilege privilege) { - this.privilege = privilege; - } public String getLogin() { return login; @@ -58,8 +59,8 @@ public class Utilisateur extends ParamBD { this.login = login; } - public Privilege getPrivilege() { - return privilege; + public String getPrivilege() { + return privilege.name(); } public void setPrivilege(Privilege privilege) { @@ -78,10 +79,18 @@ public class Utilisateur extends ParamBD { return "Utilisateur{id=" + id + ", login='" + login + "', privilege=" + privilege + "}"; } + public ArrayList getListePagesPartagees() { + return listePagesPartagees; + } + + public void setListePagesPartagees(ArrayList listePagesPartagees) { + this.listePagesPartagees = listePagesPartagees; + } + protected void chargerPages(){ try { Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); - String sql = " SELECT id, titre, date_creation, date_modification, droits" + String sql = " SELECT id, auteur_id, titre, date_creation, date_modification, droits" + " FROM page" + " WHERE auteur_id=" + this.id @@ -92,13 +101,14 @@ public class Utilisateur extends ParamBD { listePages = new ArrayList(); while(rs.next()) { int idPage = rs.getInt("id"); + int auteur_id = rs.getInt("auteur_id"); String titre = rs.getString("titre"); LocalDate date_creation = rs.getDate("date_creation").toLocalDate(); LocalDate date_modification = rs.getDate("date_modification").toLocalDate(); String droits = rs.getString("droits"); Droit droitsEnum = Droit.valueOf(droits.toUpperCase()); - listePages.add(new Page(idPage, titre, date_creation, date_modification, droitsEnum)); + listePages.add(new Page(idPage, auteur_id, titre, date_creation, date_modification, droitsEnum)); } rs.close(); st.close(); @@ -108,6 +118,33 @@ public class Utilisateur extends ParamBD { } } + protected void chargerPagesPartagees(){ + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " SELECT page.id, titre" + + " FROM partage" + + " JOIN page ON partage.page_id = page.id" + + " WHERE utilisateur_id=" + + this.id + + ";"; + Statement st = connexion.createStatement(); + ResultSet rs = st.executeQuery(sql); + + listePagesPartagees = new ArrayList(); + while(rs.next()) { + int idPage = rs.getInt("id"); + String titre = rs.getString("titre"); + + listePagesPartagees.add(new Page(idPage, this.id, titre)); + } + rs.close(); + st.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + protected static Utilisateur authentifier(String login, String mdp) { Utilisateur u = null; try { @@ -159,8 +196,8 @@ public class Utilisateur extends ParamBD { return u; } - sql = "INSERT INTO utilisateur (login, mot_de_passe) " - + " VALUES (?, ?)" + sql = "INSERT INTO utilisateur (login, mot_de_passe, privilege)" + + " VALUES (?, ?, 'USER')" + ";"; pst = connexion.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); pst.setString(1, login); @@ -182,4 +219,30 @@ public class Utilisateur extends ParamBD { } return u; } + + protected static ArrayList getListeUtilisateurs() { + ArrayList listeUtilisateur = new ArrayList<>(); + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " SELECT id, login" + + " FROM utilisateur" + + ";"; + Statement st = connexion.createStatement(); + ResultSet rs = st.executeQuery(sql); + + while(rs.next()) { + int id = rs.getInt("id"); + String login = rs.getString("login"); + + Utilisateur u = new Utilisateur(id, login); + listeUtilisateur.add(u); + } + rs.close(); + st.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return listeUtilisateur; + } } diff --git a/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp b/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp index c60fd92..06583a0 100644 --- a/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp +++ b/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp @@ -18,40 +18,46 @@
-

${page.titre}

+
+

${page.titre}

+ +
-
+
- + >${bloc.contenu}
- -
-
- -
-
- -
-
@@ -61,197 +67,10 @@
-
+
- diff --git a/Projet/src/main/webapp/WEB-INF/Footer.jsp b/Projet/src/main/webapp/WEB-INF/Footer.jsp index e9c7d88..4993f3a 100644 --- a/Projet/src/main/webapp/WEB-INF/Footer.jsp +++ b/Projet/src/main/webapp/WEB-INF/Footer.jsp @@ -1,12 +1,13 @@ - - + + + \ No newline at end of file diff --git a/Projet/src/main/webapp/WEB-INF/Header.jsp b/Projet/src/main/webapp/WEB-INF/Header.jsp index 1898956..eb4c0d0 100644 --- a/Projet/src/main/webapp/WEB-INF/Header.jsp +++ b/Projet/src/main/webapp/WEB-INF/Header.jsp @@ -6,33 +6,8 @@ ${param.titre} - + +