diff --git a/Projet/projet.sql b/Projet/projet.sql index dcee547..e146c12 100644 --- a/Projet/projet.sql +++ b/Projet/projet.sql @@ -1,5 +1,6 @@ DROP TABLE IF EXISTS bloc; DROP TABLE IF EXISTS page; +DROP TABLE IF EXISTS messages; DROP TABLE IF EXISTS utilisateur; CREATE TABLE utilisateur ( @@ -12,24 +13,54 @@ CREATE TABLE utilisateur ( CREATE TABLE page ( id INT PRIMARY KEY AUTO_INCREMENT, titre VARCHAR(200) NOT NULL, - date_creation DATETIME NOT NULL, - date_modification DATETIME NOT NULL, + date_creation DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + date_modification DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, auteur_id INT NOT NULL, droits ENUM('LECTURE', 'ECRITURE', 'ADMIN') NOT NULL, - FOREIGN KEY (auteur_id) REFERENCES utilisateur(id) + FOREIGN KEY (auteur_id) REFERENCES utilisateur(id) ON DELETE CASCADE ); CREATE TABLE bloc ( id INT PRIMARY KEY AUTO_INCREMENT, type ENUM('TEXTE', 'LISTE', 'TITRE', 'CODE', 'PAGE') NOT NULL, contenu TEXT, - date_creation DATETIME NOT NULL, - date_modification DATETIME NOT NULL, + date_creation DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + date_modification DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, page_id INT NOT NULL, ordre INT NOT NULL, auteur_id INT NOT NULL, metadata JSON, - FOREIGN KEY (page_id) REFERENCES page(id), - FOREIGN KEY (auteur_id) REFERENCES utilisateur(id) + FOREIGN KEY (page_id) REFERENCES page(id) ON DELETE CASCADE, + FOREIGN KEY (auteur_id) REFERENCES utilisateur(id) ON DELETE CASCADE ); +CREATE TABLE messages ( + id INT AUTO_INCREMENT PRIMARY KEY, + login VARCHAR(100) NOT NULL, + contenu TEXT, + timestamp DATETIME DEFAULT CURRENT_TIMESTAMP +); + +DELIMITER // + +CREATE TRIGGER trg_bloc_maj +BEFORE UPDATE ON bloc +FOR EACH ROW +BEGIN + SET NEW.date_modification = NOW(); +END; +// + +DELIMITER ; + +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 a69f6bd..076e20f 100644 --- a/Projet/src/main/java/projet/AfficherPage.java +++ b/Projet/src/main/java/projet/AfficherPage.java @@ -25,13 +25,17 @@ public class AfficherPage extends HttpServlet { if(u != null) { u.chargerPages(); ArrayList listePages = u.getListePages(); + ArrayList listeMessages = Message.getListeMessages(); request.setAttribute("listePages", listePages); + request.setAttribute("listeMessages", listeMessages); if (idStr != null ) { try { int id = Integer.parseInt(idStr); Page page = Page.getPageById(u.getId(), id); + + if (page != null) { request.setAttribute("page", page); request.getRequestDispatcher("/WEB-INF/AfficherPage.jsp").forward(request, response); diff --git a/Projet/src/main/java/projet/Message.java b/Projet/src/main/java/projet/Message.java new file mode 100644 index 0000000..cc99a58 --- /dev/null +++ b/Projet/src/main/java/projet/Message.java @@ -0,0 +1,82 @@ +package projet; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class Message extends ParamBD{ + private String login; + private String contenu; + + public Message() { + } + + public Message(String login, String contenu) { + this.login = login; + this.contenu = contenu; + } + + public String getLogin() { + return login; + } + + public void setLogin(String auteur) { + this.login = auteur; + } + + public String getContenu() { + return contenu; + } + + public void setContenu(String contenu) { + this.contenu = contenu; + } + + protected static void ajouterMessage(String login, String contenu) { + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " INSERT INTO messages(contenu, login)" + + " VALUES (?, ?)" + + ";"; + PreparedStatement pst = connexion.prepareStatement(sql); + pst.setString(1, contenu); + pst.setString(2, login); + pst.executeUpdate(); + + pst.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + protected static ArrayList getListeMessages() { + ArrayList messages = new ArrayList<>(); + + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " SELECT login, contenu" + + " FROM messages" + + ";"; + PreparedStatement pst = connexion.prepareStatement(sql); + ResultSet rs = pst.executeQuery(); + while(rs.next()) { + String contenu = rs.getString("contenu"); + String auteur = rs.getString("login"); + + Message message = new Message(auteur, contenu); + messages.add(message); + } + rs.close(); + pst.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + return messages; + } +} \ No newline at end of file diff --git a/Projet/src/main/java/projet/NouvellePage.java b/Projet/src/main/java/projet/NouvellePage.java index 064b6fc..06212eb 100644 --- a/Projet/src/main/java/projet/NouvellePage.java +++ b/Projet/src/main/java/projet/NouvellePage.java @@ -43,8 +43,9 @@ public class NouvellePage extends HttpServlet { if (titre == null || titre.isEmpty()) { response.sendRedirect("AfficherPage"); } else { - Page.ajouterPage(u.getId(), titre, LocalDate.now()); - response.sendRedirect("AfficherPage"); + int id = Page.ajouterPage(u.getId(), titre, LocalDate.now()); + if (id !=-1) response.sendRedirect("AfficherPage?id=" + id); + else response.sendRedirect("AfficherPage"); } } else { response.sendRedirect("/Projet/"); diff --git a/Projet/src/main/java/projet/Page.java b/Projet/src/main/java/projet/Page.java index ff3b54e..d3a42e4 100644 --- a/Projet/src/main/java/projet/Page.java +++ b/Projet/src/main/java/projet/Page.java @@ -5,6 +5,7 @@ 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.sql.Date; import java.util.ArrayList; @@ -97,25 +98,35 @@ public class Page extends ParamBD { return "Page{id=" + id + ", titre='" + titre + "', dateCreation=" + dateCreation + ", dateModification=" + dateModification + ", droits=" + droits + "}"; } - protected static void ajouterPage(int idU, String t, LocalDate dl) { + protected static int ajouterPage(int idU, String t, LocalDate dl) { + int idGenere = -1; + try { Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); String sql = " INSERT INTO page(titre, date_creation, date_modification, auteur_id, droits)" + " VALUES (?, ?, ?, ?, ?)" + ";"; - PreparedStatement pst = connexion.prepareStatement(sql); + PreparedStatement pst = connexion.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); pst.setString(1, t); pst.setDate(2, Date.valueOf(dl)); pst.setDate(3, Date.valueOf(dl)); pst.setInt(4, idU); pst.setString(5, "ADMIN"); 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 Page getPageById(int idU, int id) { diff --git a/Projet/src/main/java/projet/Tchat.java b/Projet/src/main/java/projet/Tchat.java new file mode 100644 index 0000000..ef4f84a --- /dev/null +++ b/Projet/src/main/java/projet/Tchat.java @@ -0,0 +1,52 @@ +package projet; + +import jakarta.websocket.*; +import jakarta.websocket.server.ServerEndpoint; +import java.io.IOException; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +@ServerEndpoint("/ws/tchat") +public class Tchat { + private static final Set sessions = Collections.synchronizedSet(new HashSet<>()); + + @OnOpen + public void onOpen(Session session) { + sessions.add(session); + } + + @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(); + + Message.ajouterMessage(login, contenu); + + synchronized (sessions) { + for (Session session : sessions) { + if (session.isOpen()) { + session.getBasicRemote().sendText(message); + } + } + } + } + } + + @OnClose + public void onClose(Session session) { + sessions.remove(session); + } + + @OnError + public void onError(Session session, Throwable throwable) { + sessions.remove(session); + throwable.printStackTrace(); + } +} diff --git a/Projet/src/main/java/projet/Utilisateur.java b/Projet/src/main/java/projet/Utilisateur.java index 5d9be4e..652e9b4 100644 --- a/Projet/src/main/java/projet/Utilisateur.java +++ b/Projet/src/main/java/projet/Utilisateur.java @@ -9,7 +9,6 @@ import java.sql.Statement; import java.time.LocalDate; import java.util.ArrayList; -import projet.Bloc.Type; import projet.Page.Droit; diff --git a/Projet/src/main/webapp/WEB-INF/Accueil.jsp b/Projet/src/main/webapp/WEB-INF/Accueil.jsp index 776bcc1..a1c1ec2 100644 --- a/Projet/src/main/webapp/WEB-INF/Accueil.jsp +++ b/Projet/src/main/webapp/WEB-INF/Accueil.jsp @@ -73,6 +73,5 @@ - \ No newline at end of file diff --git a/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp b/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp index e2533d1..c60fd92 100644 --- a/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp +++ b/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp @@ -8,10 +8,14 @@
+ +
-
+ + +

${page.titre}

@@ -41,7 +45,8 @@ data-id="${bloc.id}" data-ordre="${bloc.ordre}" data-type="${bloc.type}" - >${bloc.contenu} + placeholder = "Tapez ici..."; + >
@@ -54,12 +59,22 @@
-
+ + +
diff --git a/Projet/src/main/webapp/WEB-INF/Footer.jsp b/Projet/src/main/webapp/WEB-INF/Footer.jsp index 64cafbc..e9c7d88 100644 --- a/Projet/src/main/webapp/WEB-INF/Footer.jsp +++ b/Projet/src/main/webapp/WEB-INF/Footer.jsp @@ -1,12 +1,12 @@ - - - Made with Bulma - - + \ 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 db8da4a..1898956 100644 --- a/Projet/src/main/webapp/WEB-INF/Header.jsp +++ b/Projet/src/main/webapp/WEB-INF/Header.jsp @@ -13,6 +13,25 @@ border: none; outline: none; } + .tchat-container { + flex-grow: 1; + max-height: 100%; + height: 100%; + display: flex; + flex-direction: column; + } + .messages-container { + flex-grow: 1; + overflow-y: auto; + margin-bottom: 0.5rem; + padding: 0.5rem; + border: 1px solid #ddd; + border-radius: 4px; + background: #f9f9f9; + } + .tchat-container input[type="text"] { + margin-top: auto; + } @@ -26,7 +45,7 @@ \ No newline at end of file