Premier commit

This commit is contained in:
Lensors 2025-04-12 08:23:51 +02:00
commit d644c8efef
36 changed files with 1537 additions and 0 deletions

17
Projet/.classpath Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v11.0">
<attributes>
<attribute name="owner.project.facets" value="jst.web"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>

1
Projet/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build/

31
Projet/.project Normal file
View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Projet</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/node_modules/*|**/*.min.js|**/bower_components/*" kind="src" path="src/main/webapp"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>

View File

@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

View File

@ -0,0 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=21
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=21

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="Projet">
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<property name="context-root" value="Projet"/>
<property name="java-output-path" value="/Projet/build/classes"/>
</wb-module>
</project-modules>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="Apache Tomcat v11.0"/>
<fixed facet="java"/>
<fixed facet="jst.web"/>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="21"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="jst.web" version="6.0"/>
</faceted-project>

View File

@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.baseBrowserLibrary

View File

@ -0,0 +1 @@
Window

42
Projet/BD.md Normal file
View File

@ -0,0 +1,42 @@
```
DROP TABLE IF EXISTS utilisateur;
DROP TABLE IF EXISTS page;
DROP TABLE IF EXISTS bloc;
```
```
CREATE TABLE utilisateur (
id INT PRIMARY KEY AUTO_INCREMENT,
login VARCHAR(100) NOT NULL,
date_arrivee DATE NOT NULL,
privileges ENUM('ADMIN', 'USER', 'GUEST') NOT NULL
);
```
```
CREATE TABLE page (
id INT PRIMARY KEY AUTO_INCREMENT,
titre VARCHAR(200) NOT NULL,
date_creation DATETIME NOT NULL,
date_modification DATETIME NOT NULL,
auteur_id INT NOT NULL,
droits ENUM('LECTURE', 'ECRITURE', 'ADMIN') NOT NULL
FOREIGN KEY (auteur_id) REFERENCES utilisateur(id)
);
```
```
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,
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)
);
```

35
Projet/projet.sql Normal file
View File

@ -0,0 +1,35 @@
DROP TABLE IF EXISTS bloc;
DROP TABLE IF EXISTS page;
DROP TABLE IF EXISTS utilisateur;
CREATE TABLE utilisateur (
id INT PRIMARY KEY AUTO_INCREMENT,
login VARCHAR(100) NOT NULL,
mot_de_passe VARCHAR(255) NOT NULL,
privilege ENUM('ADMIN', 'USER', 'GUEST') NOT NULL
);
CREATE TABLE page (
id INT PRIMARY KEY AUTO_INCREMENT,
titre VARCHAR(200) NOT NULL,
date_creation DATETIME NOT NULL,
date_modification DATETIME NOT NULL,
auteur_id INT NOT NULL,
droits ENUM('LECTURE', 'ECRITURE', 'ADMIN') NOT NULL,
FOREIGN KEY (auteur_id) REFERENCES utilisateur(id)
);
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,
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)
);

View File

@ -0,0 +1,63 @@
package projet;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("")
public class Accueil extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() {
ParamBD.init(getServletContext());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
if(u == null) {
request.setAttribute("erreur", 0);
RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Accueil.jsp");
rd.forward(request, response);
} else {
response.sendRedirect("AfficherPage");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String login = request.getParameter("login");
String mdp = request.getParameter("mdp");
String action = request.getParameter("action");
if(action.equals("connexion")) {
Utilisateur u = Utilisateur.authentifier(login, mdp);
if(u != null) {
HttpSession session = request.getSession();
session.setAttribute("utilisateur", u);
response.sendRedirect("AfficherPage");
}else {
request.setAttribute("erreur", 1);
RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Accueil.jsp");
rd.forward(request, response);
}
} else if (action.equals("inscription")){
Utilisateur u = Utilisateur.inscrire(login, mdp);
if(u != null) {
HttpSession session = request.getSession();
session.setAttribute("utilisateur", u);
response.sendRedirect("AfficherPage");
}else {
request.setAttribute("erreur", 2);
RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Accueil.jsp");
rd.forward(request, response);
}
}
}
}

View File

@ -0,0 +1,58 @@
package projet;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet("/AfficherPage")
public class AfficherPage extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
String idStr = request.getParameter("id");
if(u != null) {
u.chargerPages();
ArrayList<Page> listePages = u.getListePages();
request.setAttribute("listePages", listePages);
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);
return;
} else {
request.getRequestDispatcher("/WEB-INF/AfficherPage.jsp").forward(request, response);
return;
}
} catch (NumberFormatException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "ID invalide");
return;
}
}
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/AfficherPage.jsp");
rd.forward(request, response);
} else {
response.sendRedirect("/Projet/");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}

View File

@ -0,0 +1,167 @@
package projet;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
public class Bloc extends ParamBD {
private int id;
private Type type;
private String contenu;
private LocalDate dateCreation;
private LocalDate dateModification;
private int ordre;
private Map<String, Object> metadata;
public enum Type {
TEXTE,
LISTE,
TITRE,
CODE,
PAGE
}
public Bloc() {
}
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;
this.ordre = ordre;
this.dateCreation = LocalDate.now();
this.dateModification = LocalDate.now();
metadata = new HashMap<>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public String getContenu() {
return contenu;
}
public void setContenu(String contenu) {
this.contenu = contenu;
}
public LocalDate getDateCreation() {
return dateCreation;
}
public void setDateCreation(LocalDate dateCreation) {
this.dateCreation = dateCreation;
}
public LocalDate getDateModification() {
return dateModification;
}
public void setDateModification(LocalDate dateModification) {
this.dateModification = dateModification;
}
public int getOrdre() {
return ordre;
}
public void setOrdre(int ordre) {
this.ordre = ordre;
}
public Map<String, Object> getMetadata() {
return metadata;
}
public void setMetadata(Map<String, Object> metadata) {
this.metadata = metadata;
}
public String toString() {
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<String, Object> metadata) {
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);
pst.setString(1, type.name());
pst.setString(2, contenu);
pst.setDate(3, Date.valueOf(dl));
pst.setDate(4, Date.valueOf(dl));
pst.setInt(5, idP);
pst.setInt(6, ordre);
pst.setInt(7, idU);
pst.setString(8, metadata.toString());
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);
String sql = " UPDATE bloc"
+ " SET contenu = ?"
+ ", date_modification = ?"
+ " WHERE id = ?"
+ ";";
PreparedStatement pst = connexion.prepareStatement(sql);
pst.setString(1, nouveauContenu);
pst.setDate(2, Date.valueOf(LocalDate.now()));
pst.setInt(3, idBloc);
pst.executeUpdate();
pst.close();
connexion.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void supprimerBloc(int idBloc, int idU) {
try {
Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword);
String sql = " DELETE FROM bloc"
+ " WHERE id = ? and auteur_id = ?"
+ ";";
PreparedStatement pst = connexion.prepareStatement(sql);
pst.setInt(1, idBloc);
pst.setInt(2, idU);
pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,26 @@
package projet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/Deconnexion")
public class Deconnexion extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();
response.sendRedirect("/Projet/");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}

View File

@ -0,0 +1,47 @@
package projet;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/ModifBloc")
public class ModifBloc extends HttpServlet {
private static final long serialVersionUID = 1L;
public ModifBloc() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
if(u != null) {
RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp");
rd.forward(request, response);
}else {
response.sendRedirect("/Projet/");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
if(u != null) {
String contenu = request.getParameter("contenu");
int blocId = Integer.parseInt(request.getParameter("blocId"));
Bloc.updateBloc(blocId, contenu);
response.sendRedirect("AfficherPage");
} else {
response.sendRedirect("/Projet/");
}
}
}

View File

@ -0,0 +1,65 @@
package projet;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/NouveauBloc")
public class NouveauBloc extends HttpServlet {
private static final long serialVersionUID = 1L;
public NouveauBloc() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
if(u != null) {
RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp");
rd.forward(request, response);
}else {
response.sendRedirect("/Projet/");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
if(u != null) {
request.setCharacterEncoding("UTF-8");
String contenu = request.getParameter("contenu");
String typeStr = request.getParameter("type");
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<String, Object> 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");
}
} else {
response.sendRedirect("/Projet/");
}
}
}

View File

@ -0,0 +1,53 @@
package projet;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDate;
@WebServlet("/NouvellePage")
public class NouvellePage extends HttpServlet {
private static final long serialVersionUID = 1L;
public NouvellePage() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
if(u != null) {
RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp");
rd.forward(request, response);
}else {
response.sendRedirect("/Projet/");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
if(u != null) {
request.setCharacterEncoding("UTF-8");
String titre = request.getParameter("titre");
if (titre == null || titre.isEmpty()) {
response.sendRedirect("AfficherPage");
} else {
Page.ajouterPage(u.getId(), titre, LocalDate.now());
response.sendRedirect("AfficherPage");
}
} else {
response.sendRedirect("/Projet/");
}
}
}

View File

@ -0,0 +1,185 @@
package projet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import projet.Bloc.Type;
public class Page extends ParamBD {
private int id;
private String titre;
private LocalDate dateCreation;
private LocalDate dateModification;
private Droit droits;
private List<Bloc> listeBlocs;
public enum Droit {
LECTURE,
ECRITURE,
ADMIN
}
public Page() {
}
public Page(int 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.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 int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public LocalDate getDateCreation() {
return dateCreation;
}
public void setDateCreation(LocalDate dateCreation) {
this.dateCreation = dateCreation;
}
public LocalDate getDateModification() {
return dateModification;
}
public void setDateModification(LocalDate dateModification) {
this.dateModification = dateModification;
}
public Droit getDroits() {
return droits;
}
public void setDroits(Droit droits) {
this.droits = droits;
}
public List<Bloc> getListeBlocs() {
return listeBlocs;
}
public void setListeBlocs(List<Bloc> listeBlocs) {
this.listeBlocs = listeBlocs;
}
public String toString() {
return "Page{id=" + id + ", titre='" + titre + "', dateCreation=" + dateCreation + ", dateModification=" + dateModification + ", droits=" + droits + "}";
}
protected static void ajouterPage(int idU, String t, LocalDate dl) {
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);
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();
pst.close();
connexion.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
protected static Page getPageById(int idU, int id) {
Page page = new Page();
String titre = null;
String sql;
PreparedStatement pst;
ResultSet rs;
try {
Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword);
sql = " SELECT titre"
+ " FROM page"
+ " WHERE id=? AND auteur_id = ?"
+ ";";
pst = connexion.prepareStatement(sql);
pst.setInt(1, id);
pst.setInt(2, idU);
rs = pst.executeQuery();
while(rs.next()) {
titre = rs.getString("titre");
page.titre = titre;
}
if(titre != null) {
sql = " SELECT id, contenu, type, ordre"
+ " FROM bloc"
+ " WHERE page_id=?"
+ " ORDER BY ordre"
+ ";";
pst = connexion.prepareStatement(sql);
pst.setInt(1, id);
rs = pst.executeQuery();
page.listeBlocs = new ArrayList<>();
while(rs.next()) {
int idB = rs.getInt("id");
String contenu = rs.getString("contenu");
String type = rs.getString("type");
Type typeEnum = Type.valueOf(type.toUpperCase());
int ordre = rs.getInt("ordre");
Bloc bloc = new Bloc(idB, typeEnum, contenu, ordre);
page.listeBlocs.add(bloc);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return page;
}
public static void supprimerPage(int idPage, int idU) {
try {
Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword);
String sql = " DELETE FROM page"
+ " WHERE id = ? and auteur_id = ?"
+ ";";
PreparedStatement pst = connexion.prepareStatement(sql);
pst.setInt(1, idPage);
pst.setInt(2, idU);
pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,20 @@
package projet;
import jakarta.servlet.ServletContext;
public class ParamBD {
protected static String bdURL;
protected static String bdLogin;
protected static String bdPassword;
protected static void init(ServletContext context) {
try {
Class.forName(context.getInitParameter("JDBC_DRIVER"));
bdURL = context.getInitParameter("JDBC_URL");
bdLogin = context.getInitParameter("JDBC_LOGIN");
bdPassword = context.getInitParameter("JDBC_PASSWORD");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,52 @@
package projet;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/SupprimerBloc")
public class SupprimerBloc extends HttpServlet {
private static final long serialVersionUID = 1L;
public SupprimerBloc() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
if(u != null) {
RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp");
rd.forward(request, response);
}else {
response.sendRedirect("/Projet/");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
if(u != null) {
String idBlocStr = request.getParameter("blocId");
if(idBlocStr == null || idBlocStr.isEmpty()) {
response.sendRedirect("AfficherPage");
} else {
int idBloc = Integer.parseInt(idBlocStr);
Bloc.supprimerBloc(idBloc, u.getId());
response.sendRedirect("AfficherPage");
}
} else {
response.sendRedirect("/Projet/");
}
}
}

View File

@ -0,0 +1,49 @@
package projet;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/SupprimerPage")
public class SupprimerPage extends HttpServlet {
private static final long serialVersionUID = 1L;
public SupprimerPage() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
if(u != null) {
RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp");
rd.forward(request, response);
}else {
response.sendRedirect("/Projet/");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
if(u != null) {
String idPageStr = request.getParameter("pageId");
int idPage = Integer.parseInt(idPageStr);
Page.supprimerPage(idPage, u.getId());
response.sendRedirect("AfficherPage");
} else {
response.sendRedirect("/Projet/");
}
}
}

View File

@ -0,0 +1,186 @@
package projet;
import java.sql.Connection;
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.ArrayList;
import projet.Bloc.Type;
import projet.Page.Droit;
public class Utilisateur extends ParamBD {
private int id;
private String login;
private Privilege privilege;
private ArrayList<Page> listePages;
public enum Privilege {
ADMIN,
USER,
GUEST
}
public Utilisateur(int id, String login, Privilege privilege) {
if (login == null || login.isEmpty()) {
throw new IllegalArgumentException("Le pseudo ne peut pas être vide.");
}
this.id = id;
this.login = login;
this.privilege = privilege;
}
public int getId() {
return id;
}
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;
}
public void setLogin(String login) {
this.login = login;
}
public Privilege getPrivilege() {
return privilege;
}
public void setPrivilege(Privilege privilege) {
this.privilege = privilege;
}
public ArrayList<Page> getListePages() {
return listePages;
}
public void setListePages(ArrayList<Page> listePages) {
this.listePages = listePages;
}
public String toString() {
return "Utilisateur{id=" + id + ", login='" + login + "', privilege=" + privilege + "}";
}
protected void chargerPages(){
try {
Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword);
String sql = " SELECT id, titre, date_creation, date_modification, droits"
+ " FROM page"
+ " WHERE auteur_id="
+ this.id
+ ";";
Statement st = connexion.createStatement();
ResultSet rs = st.executeQuery(sql);
listePages = new ArrayList<Page>();
while(rs.next()) {
int idPage = rs.getInt("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));
}
rs.close();
st.close();
connexion.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
protected static Utilisateur authentifier(String login, String mdp) {
Utilisateur u = null;
try {
Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword);
String sql = " SELECT id, privilege"
+ " FROM utilisateur"
+ " WHERE login=? AND mot_de_passe = ?"
+ ";";
PreparedStatement pst = connexion.prepareStatement(sql);
pst.setString(1, login);
pst.setString(2, mdp);
ResultSet rs = pst.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
String privilegeSQL = rs.getString("privilege");
Privilege privilege = null;
if (privilegeSQL.equals("ADMIN")) privilege=Privilege.ADMIN;
else if (privilegeSQL.equals("USER")) privilege=Privilege.USER;
else if (privilegeSQL.equals("GUEST")) privilege=Privilege.GUEST;
if(id != 0) {
u = new Utilisateur(id, login, privilege);
}
}
rs.close();
pst.close();
connexion.close();
} catch (SQLException e) {
e.printStackTrace();
}
return u;
}
public static Utilisateur inscrire(String login, String mdp) {
Utilisateur u = null;
try {
Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword);
String sql = "SELECT id "
+ " FROM utilisateur "
+ " WHERE login = ?"
+ ";";
PreparedStatement pst = connexion.prepareStatement(sql);
pst.setString(1, login);
ResultSet rs = pst.executeQuery();
while(rs.next()) {
rs.close();
pst.close();
connexion.close();
return u;
}
sql = "INSERT INTO utilisateur (login, mot_de_passe) "
+ " VALUES (?, ?)"
+ ";";
pst = connexion.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
pst.setString(1, login);
pst.setString(2, mdp);
int ri = pst.executeUpdate();
if (ri > 0) {
rs = pst.getGeneratedKeys();
while(rs.next()) {
int id = rs.getInt(1);
u = new Utilisateur(id, login, Privilege.USER);
}
}
rs.close();
pst.close();
connexion.close();
} catch (SQLException e) {
e.printStackTrace();
}
return u;
}
}

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

View File

@ -0,0 +1,78 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:include page="Header.jsp">
<jsp:param name="titre" value="Accueil" />
</jsp:include>
<c:set var="erreur" value="${erreur}" />
<c:if test="${erreur == 1}">
<div class="columns is-centered">
<div class="column is-half">
<article class="message is-danger">
<div class="message-header">
<p>Erreur</p>
</div>
<div class="message-body">
<p>Mauvais login ou mot de passe.</p>
</div>
</article>
</div>
</div>
</c:if>
<c:if test="${erreur == 2}">
<div class="columns is-centered">
<div class="column is-half">
<article class="message is-danger">
<div class="message-header">
<p>Erreur</p>
</div>
<div class="message-body">
<p>Login déjà utilisé.</p>
</div>
</article>
</div>
</div>
</c:if>
<div class="columns is-centered">
<div class="column is-half">
<form class="box" method="post" action=".">
<div class="field">
<label class="label" for="login">Login</label>
<div class="control">
<input
class="input"
type="text"
name="login"
id="login"
value="${erreur == 1 ? param.login : ''}"
placeholder="Entrez votre login">
</div>
</div>
<div class="field">
<label class="label" for="mdp">Mot de passe</label>
<div class="control">
<input
class="input"
type="password"
name="mdp"
id="mdp"
placeholder="Entrez votre mot de passe" >
</div>
</div>
<button class="button is-primary" name="action" value="connexion">Se connecter</button>
<button class="button is-primary is-light" name="action" value="inscription">S'inscrire</button>
</form>
</div>
</div>
</main>
<jsp:include page="Footer.jsp" />

View File

@ -0,0 +1,191 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:include page="Header.jsp">
<jsp:param name="titre" value="Page" />
</jsp:include>
<div class="columns">
<div class="column is-one-fifth">
<jsp:include page="MenuPages.jsp" />
</div>
<div class="column">
<c:choose>
<c:when test="${not empty page.titre}">
<h2 class="block">${page.titre}</h2>
<div class="block" id="md">
<c:forEach var="bloc" items="${page.listeBlocs}">
<div class="field is-grouped is-align-items-flex-start bloc-container" data-id="${bloc.id}">
<div class="control is-expanded">
<textarea
class="textarea is-primary"
rows="1"
data-id="${bloc.id}"
data-ordre="${bloc.ordre}"
data-type="${bloc.type}"
>${bloc.contenu}</textarea>
</div>
<div class="control">
<button class="delete is-danger delete-bloc-btn" data-id="${bloc.id}"></button>
</div>
</div>
</c:forEach>
<div class="field is-grouped is-align-items-flex-start bloc-container" data-id="${bloc.id}">
<div class="control is-expanded">
<textarea
class="textarea is-primary"
rows="1"
data-id="${bloc.id}"
data-ordre="${bloc.ordre}"
data-type="${bloc.type}"
>${bloc.contenu}</textarea>
</div>
<div class="control">
<button class="delete is-danger delete-bloc-btn" data-id="${bloc.id}"></button>
</div>
</div>
</div>
</c:when>
<c:otherwise>
<p>Pas encore de page choisie.</p>
</c:otherwise>
</c:choose>
</div>
<div class="column is-one-quarter">
<jsp:include page="Tchat.jsp" />
</div>
</div>
<script>
// Fonction pour ajouter un nouvel événement à chaque textarea
function addTextareaEvent(textarea) {
textarea.addEventListener('keydown', function(event) {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault(); // Empêcher le saut de ligne par défaut
const allTextareas = document.querySelectorAll('#md textarea');
const lastTextarea = allTextareas[allTextareas.length - 1];
if (lastTextarea.value.trim() !== "") {
// Envoi d'un nouveau bloc au serveur
const params = new URLSearchParams();
params.append("contenu", lastTextarea.value);
params.append("type", "TEXTE"); // Tu peux le changer dynamiquement
params.append("ordre", allTextareas.length);
params.append("pageId", new URLSearchParams(window.location.search).get("id"));
fetch("/Projet/NouveauBloc", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: params
}).then(response => {
if (!response.ok) {
console.error("Erreur lors de l'ajout du bloc.");
}
});
// Créer un nouveau textarea seulement si le dernier n'est pas vide
const newTextarea = document.createElement('textarea');
newTextarea.classList.add('textarea', 'is-primary');
newTextarea.placeholder = "Tapez ici...";
newTextarea.rows = "1";
newTextarea.dataset.type = "TEXTE";
document.getElementById('md').appendChild(newTextarea);
addTextareaEvent(newTextarea);
autoResize(newTextarea);
newTextarea.focus();
} else {
// Envoi d'un update de bloc au serveur
event.preventDefault();
const currentTextarea = event.target;
const blocId = currentTextarea.getAttribute('data-id'); // Récupère l'ID du bloc
const params = new URLSearchParams();
params.append("contenu", currentTextarea.value);
params.append("blocId", blocId);
fetch("/Projet/ModifBloc", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: params
}).then(response => {
if (!response.ok) {
console.error("Erreur lors de la mise à jour du bloc.");
}
});
lastTextarea.focus();
}
} else if (event.key === 'Enter' && event.shiftKey) { // Si Shift + Entrée est pressé
event.preventDefault();
const cursorPosition = textarea.selectionStart;
const textBefore = textarea.value.substring(0, cursorPosition);
const textAfter = textarea.value.substring(cursorPosition);
textarea.value = textBefore + "\n" + textAfter;
textarea.selectionStart = textarea.selectionEnd = cursorPosition + 1;
autoResize(textarea);
textarea.focus();
}
});
textarea.addEventListener('input', function () {
autoResize(textarea);
});
}
function autoResize(textarea) {
textarea.style.height = 'auto';
textarea.style.height = textarea.scrollHeight + 'px';
}
document.querySelectorAll('#md textarea').forEach(t => {
addTextareaEvent(t);
autoResize(t)
});
function addDeleteEvent(button) {
button.addEventListener('click', function () {
const blocId = button.dataset.id;
const blocContainer = button.closest('.bloc-container');
const textarea = blocContainer.querySelector('textarea');
if (textarea.value.trim() === "") {
return; // Empêche la suppression si le textarea est vide
}
if (confirm("Voulez-vous vraiment supprimer ce bloc ?")) {
const params = new URLSearchParams();
params.append("blocId", blocId);
fetch("/Projet/SupprimerBloc", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: params
}).then(response => {
if (response.ok) {
blocContainer.remove(); // Supprime visuellement le bloc
} else {
console.error("Erreur lors de la suppression du bloc.");
}
});
}
});
}
document.querySelectorAll('.delete-bloc-btn').forEach(t => {
addDeleteEvent(t);
});
</script>
<jsp:include page="Footer.jsp" />

View File

@ -0,0 +1,12 @@
<a href="https://bulma.io">
<img
src="https://bulma.io/assets/images/made-with-bulma.png"
alt="Made with Bulma"
width="128"
height="24">
</a>
</main>
</body>
</html>

View File

@ -0,0 +1,45 @@
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="fr" data-theme="light">
<head>
<meta charset="UTF-8">
<title>${param.titre}</title>
<link href="bulma.css" rel="stylesheet">
<style>
.textarea {
resize: none; /* Empêche le redimensionnement par l'utilisateur */
overflow-y: hidden; /* Masquer la barre de défilement verticale */
border: none;
outline: none;
}
</style>
</head>
<body>
<nav class="navbar has-shadow is-white" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" href="/Projet/">
<h1 class="title is-2">Prise de notes collaborative</h1>
</a>
</div>
<div class="navbar-menu">
<div class="navbar-end">
<c:if test="${not empty utilisateur}">
<div class="navbar-item">
<p>${utilisateur.login}</p>
</div>
</c:if>
<c:choose>
<c:when test="${not empty sessionScope.utilisateur}">
<a class="navbar-item" href="Deconnexion">Se déconnecter</a>
</c:when>
<c:otherwise>
<a class="navbar-item" href="">Se connecter</a>
<a class="navbar-item" href="">S'inscrire</a>
</c:otherwise>
</c:choose>
</div>
</div>
</nav>
<main class="section">

View File

@ -0,0 +1,27 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<h2 class="block">Menu des pages</h2>
<aside class="menu">
<ul class="menu-list" id="menuPages">
<c:forEach var="page" items="${listePages}">
<li>
<a href="AfficherPage?id=${page.id}">${page.titre}</a>
<button class="delete is-small delete-page-btn is-primary" data-id="${page.id}"></button>
</li>
</c:forEach>
<li>
<form action="NouvellePage" method="POST">
<input
class="input is-primary is-small"
type="text"
name="titre"
id="titrePage"
placeholder="Nom de votre nouvelle page"
onkeydown="if(event.key === 'Enter') this.form.submit();"
>
</form>
</li>
</ul>
</aside>

View File

@ -0,0 +1,10 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<h2 class="block">Tchat</h2>
<div class="block">
<input
class="input is-primary is-small"
type="text"
placeholder="Ecrivez ici">
</div>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" version="6.0">
<display-name>Projet</display-name>
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
<context-param>
<param-name>JDBC_DRIVER</param-name>
<param-value>com.mysql.cj.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>JDBC_URL</param-name>
<param-value>jdbc:mysql://localhost:3306/projet</param-value>
</context-param>
<context-param>
<param-name>JDBC_LOGIN</param-name>
<param-value>webuser</param-value>
</context-param>
<context-param>
<param-name>JDBC_PASSWORD</param-name>
<param-value>multipass</param-value>
</context-param>
</web-app>

2
Projet/src/main/webapp/bulma.css vendored Normal file
View File

@ -0,0 +1,2 @@
@charset "UTF-8";
@import "https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/bulma.min.css";