diff --git a/Projet/src/main/java/projet/AfficherPage.java b/Projet/src/main/java/projet/AfficherPage.java index 1b426de..cb19d9a 100644 --- a/Projet/src/main/java/projet/AfficherPage.java +++ b/Projet/src/main/java/projet/AfficherPage.java @@ -10,6 +10,7 @@ import jakarta.servlet.http.HttpSession; import java.io.IOException; import java.util.ArrayList; +import java.util.List; import java.util.Map; @WebServlet("/AfficherPage") @@ -35,6 +36,10 @@ public class AfficherPage extends HttpServlet { request.setAttribute("listeMessages", listeMessages); request.setAttribute("listeUtilisateurs", listeUtilisateurs); + String menuHtml = genererMenuHTML(u.getId()); + request.setAttribute("menuHtml", menuHtml); + + if (idStr != null ) { try { int id = Integer.parseInt(idStr); @@ -66,5 +71,42 @@ public class AfficherPage extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } + + public static String genererMenuHTML(int idU) { + Map> arbre = Page.getArbreDesPages(idU); + StringBuilder html = new StringBuilder(); + html.append("\n"); + return html.toString(); + } + + private static void construireHTMLRecursif(Map> arbre, int parentId, StringBuilder html, int indentLevel) { + List enfants = arbre.get(parentId); + if (enfants == null || enfants.isEmpty()) return; + String indent = "\t".repeat(indentLevel); + + for (Page p : enfants) { + List sousPages = arbre.get(p.getId()); + boolean aSousPages = sousPages != null && !sousPages.isEmpty(); + + html.append(indent).append("
  • \n"); + + html.append(indent).append("\t
    \n"); + html.append(indent).append("\t\t") + .append(p.getTitre()).append("\n"); + html.append(indent).append("\t\t\n"); + html.append(indent).append("\t
    \n"); + + if (aSousPages) { + html.append(indent).append("\t
      \n"); + construireHTMLRecursif(arbre, p.getId(), html, indentLevel + 1); + html.append(indent).append("\t
    \n"); + } + + html.append(indent).append("
  • \n"); + } + } } diff --git a/Projet/src/main/java/projet/Page.java b/Projet/src/main/java/projet/Page.java index bdf61fa..47d6480 100644 --- a/Projet/src/main/java/projet/Page.java +++ b/Projet/src/main/java/projet/Page.java @@ -9,7 +9,9 @@ import java.sql.Statement; import java.time.LocalDate; import java.sql.Date; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import projet.Bloc.Type; @@ -283,4 +285,57 @@ public class Page extends ParamBD { e.printStackTrace(); } } + + public static Map> getArbreDesPages(int idU) { + Map> arbre = new HashMap<>(); + chargerSousPagesRecursivement(idU, arbre, null); + return arbre; + } + + public static void chargerSousPagesRecursivement(int idU, Map> arbre, Integer idParent) { + List enfants = new ArrayList<>(); + + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql; + PreparedStatement pst; + + if(idParent == null) { + sql = " SELECT id, titre" + + " FROM page " + + "WHERE auteur_id = ? AND page_parent_id IS NULL" + + ";"; + pst = connexion.prepareStatement(sql); + pst.setInt(1, idU); + } else { + sql = " SELECT id, titre" + + " FROM page " + + "WHERE auteur_id = ? AND page_parent_id = ?" + + ";"; + pst = connexion.prepareStatement(sql); + pst.setInt(1, idU); + pst.setInt(2, idParent); + } + + ResultSet rs = pst.executeQuery(); + while (rs.next()) { + int id = rs.getInt("id"); + String titre = rs.getString("titre"); + Page page = new Page(id, idU, titre); + enfants.add(page); + } + + rs.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + int key = (idParent == null) ? -1 : idParent; + arbre.put(key, enfants); + + for (Page enfant : enfants) { + chargerSousPagesRecursivement(idU, arbre, enfant.getId()); + } + } } diff --git a/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp b/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp index 1782bbb..917b41f 100644 --- a/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp +++ b/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp @@ -7,15 +7,15 @@ -
    +
    -
    + -
    +
    @@ -53,7 +53,7 @@
    -