ProjetDevWeb2/Projet/src/main/java/projet/Page.java

342 lines
8.7 KiB
Java

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.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
private Droit droits;
private List<Bloc> listeBlocs;
public enum Droit {
LECTURE,
ECRITURE,
ADMIN
}
public Page() {
}
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, int auteur_id, String titre) {
this.id = id;
this.titre = titre;
}
public int getId() {
return id;
}
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;
}
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 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, 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();
}
if (idGenere != -1) {
Bloc.ajouterBlocVide(idU, idGenere);
}
return idGenere;
}
protected static int ajouterPage(int idU, String t, LocalDate dl, int idParent) {
int idGenere = -1;
try {
Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword);
String sql = " INSERT INTO page(titre, date_creation, date_modification, auteur_id, droits, page_parent_id)"
+ " VALUES (?, ?, ?, ?, ?, ?)"
+ ";";
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.setInt(6, idParent);
pst.executeUpdate();
ResultSet rs = pst.getGeneratedKeys();
if (rs.next()) {
idGenere = rs.getInt(1);
}
rs.close();
pst.close();
connexion.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (idGenere != -1) {
Bloc.ajouterBlocVide(idU, idGenere);
}
return idGenere;
}
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, auteur_id"
+ " FROM page"
+ " WHERE id=? AND auteur_id = ?"
+ " UNION"
+ " SELECT titre, NULL AS auteur_id"
+ " FROM partage"
+ " JOIN page ON partage.page_id = page.id"
+ " WHERE partage.utilisateur_id = ?"
+ " AND page.id = ?"
+ ";";
pst = connexion.prepareStatement(sql);
pst.setInt(1, id);
pst.setInt(2, idU);
pst.setInt(3, idU);
pst.setInt(4, id);
rs = pst.executeQuery();
while(rs.next()) {
titre = rs.getString("titre");
page.titre = titre;
page.id = id;
page.auteur_id = rs.getInt("auteur_id");
}
if(titre != null) {
sql = " SELECT id, contenu, type, ordre, metadata"
+ " 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");
String metadata = rs.getString("metadata");
Type typeEnum = Type.valueOf(type.toUpperCase());
int ordre = rs.getInt("ordre");
Bloc bloc = new Bloc(idB, typeEnum, contenu, ordre, metadata);
page.listeBlocs.add(bloc);
}
}
rs.close();
pst.close();
connexion.close();
} 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();
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();
pst.close();
connexion.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Map<Integer, List<Page>> getArbreDesPages(int idU) {
Map<Integer, List<Page>> arbre = new HashMap<>();
chargerSousPagesRecursivement(idU, arbre, null);
return arbre;
}
public static void chargerSousPagesRecursivement(int idU, Map<Integer, List<Page>> arbre, Integer idParent) {
List<Page> 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());
}
}
}