repartition ROOT, ETU et utilisation de l'API respecter, correction de bug et ajout fonction visite de groupe d'un etudiant seulement ceux que il connait

This commit is contained in:
2022-11-26 18:09:08 +01:00
parent 203fca24cd
commit 47742b1ddc
35 changed files with 1670 additions and 686 deletions

View File

@@ -1,5 +1,13 @@
package fr.iutfbleau.projetIHM2022FI2.MNP;
import fr.iutfbleau.projetIHM2022FI2.API.*;
import java.sql.Connection;
import org.mariadb.jdbc.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.*;
/**
* Usine abstraite gérant l'ensemble des groupes.
@@ -10,6 +18,9 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
// la racine (promotion)
private Groupe promo;
//la fentre pour les fenetre modale
private JFrame fenetre;
// On utilise une table de hachage pour retrouver facilement un groupe (à partir de son id).
// Si il y a beaucoup de groupes c'est plus rapide que de parcourir toute une liste.
@@ -18,11 +29,24 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
/**
* Le constructeur fabrique le groupe promotion déja plein (utilisé en Modèle persistant de donné).
*/
public AbstractGroupeFactoryNP(Groupe promo){
public AbstractGroupeFactoryNP(Groupe promo, JFrame fenetre){
this.promo=promo;
this.fenetre=fenetre;
this.brain=new HashMap<Integer,Groupe>();
this.addSousGroupe(this.promo);
}
/**
* Le constructeur fabrique le groupe promotion vide.
* Il faut ensuite y ajouter les étudiants.
*/
public AbstractGroupeFactoryNP(String name, int min, int max, JFrame fenetre){
Objects.requireNonNull(name,"On ne peut pas créer une promotion dont le nom est null");
this.promo=new GroupeNP(name,min,max);
this.fenetre=fenetre;
this.brain=new HashMap<Integer,Groupe>();
this.brain.put(Integer.valueOf(this.promo.getId()),this.promo);
this.addSousGroupe(this.promo);
this.saveGroupe(promo);
}
/**
@@ -37,17 +61,6 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
this.addSousGroupe(s);
}
}
/**
* Le constructeur fabrique le groupe promotion vide.
* Il faut ensuite y ajouter les étudiants.
*/
public AbstractGroupeFactoryNP(String name, int min, int max){
Objects.requireNonNull(name,"On ne peut pas créer une promotion dont le nom est null");
this.promo=new GroupeNP(name,min,max);
this.brain=new HashMap<Integer,Groupe>();
this.brain.put(Integer.valueOf(this.promo.getId()),this.promo);
}
/**
* Test plutôt optimiste. Si la clé est identique alors on fait comme si c'était le bon groupe.
@@ -58,8 +71,35 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
return this.brain.containsKey(Integer.valueOf(g.getId()));
}
public boolean changeNameGroupe(Groupe g, String name){
if(!this.knows(g)){
throw new IllegalArgumentException("Groupe inconu ");
}
Connection cnx=this.cnx();
try{
PreparedStatement pst=cnx.prepareStatement(
"UPDATE `Groupe` SET `nom` = ? WHERE `Groupe`.`id` = ? "
);
pst.setString(1, name);
pst.setInt(2, g.getId());
pst.executeUpdate();
pst.close();
}catch(SQLException e){
System.out.println(e.toString());
if(this.erreurSQL()){
this.changeNameGroupe(g, name);
}else{
return false;
}
}
this.close(cnx);
g.setName(name);
return true;
}
/**
* permet de récupérer le Groupe qui contient les étudiants de toute la promotion
* @return la promo.
@@ -81,14 +121,9 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
if (!this.knows(g)){
throw new IllegalArgumentException("Impossible d'enlever un groupe inconnu");
}
if (this.getPromotion().equals(g)){
throw new IllegalArgumentException("Impossible de détruire le groupe de toute la promotion");
}
if (g.getSousGroupes().size()>0){
throw new IllegalStateException("Impossible de détruire un groupe contenant un groupe");
}
g.getPointPoint().removeSousGroupe(g);
this.brain.remove(Integer.valueOf(g.getId()));
this.suprGroupe(g);
}
/**
@@ -104,6 +139,7 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
public void createGroupe(Groupe pere, String name, int min, int max){
Objects.requireNonNull(pere,"Le groupe pere ne peut pas être null");
Objects.requireNonNull(name,"Le nouveau groupe ne peut pas avoir null comme nom");
if (!this.knows(pere)){
throw new IllegalArgumentException("Interdit d'ajouter un fils à un groupe inconnu");
}
@@ -113,9 +149,11 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
if ( min <= 0 || max < min){
throw new IllegalArgumentException("Il faut que 0 < min <= max");
}
Groupe g = new GroupeNP(pere,name,min,max);
pere.addSousGroupe(g);
this.brain.put(Integer.valueOf(g.getId()),g);
this.saveGroupe(g);
}
/**
@@ -167,6 +205,7 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
groupes.get(i).addEtudiant(s);
i = (i+1) %n;
}
this.saveGroupe(copiePereRacinePartition);
}
/**
@@ -186,6 +225,7 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
throw new IllegalArgumentException("Impossible d'ajouter l'étudiant car le est groupe inconnu");
}
g.addEtudiant(e);
this.saveEtu(e, g);
}
/**
@@ -205,6 +245,7 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
throw new IllegalArgumentException("Impossible d'ajouter l'étudiant car le est groupe inconnu");
}
g.removeEtudiant(e);
this.deleteEtu(e, g);
}
/**
@@ -250,4 +291,180 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
}
return ret;
}
// **********************
// FONCTION POUR SIMPLIFIER LES Modification BD
// ***********************
private Connection cnx(){
//On se Connecte a la BD
try{
Class.forName("org.mariadb.jdbc.Driver");
Connection cnx = DriverManager.getConnection(
"jdbc:mariadb://dwarves.iut-fbleau.fr/chaignea",
"chaignea", "Chaigneauphpmyadmin");
return cnx;
}catch(Exception e){
if(this.erreurCO()==true){
return this.cnx();
}
}
return null;
}
private boolean erreurCO(){
if(JOptionPane.showConfirmDialog(this.fenetre, "erreur connection a la BD reassayer?", "erreur connection", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
return true;
}else{
this.fenetre.dispose();
return false;
}
}
private boolean erreurSQL(){
if(JOptionPane.showConfirmDialog(this.fenetre, "erreur lors de la modification, reasssayer?", "erreur SQL", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
return true;
}else{
return false;
}
}
private void deleteEtu(Etudiant et, Groupe g){
Connection cnx=this.cnx();
try{
PreparedStatement pst=cnx.prepareStatement(
"DELETE FROM CONTIENT WHERE idGroupe=? AND idEt=?; "
);
pst.setInt(2, et.getId());
this.deleteEtu(pst, cnx, g);
pst.close();
}catch(SQLException e){
System.out.println(e.toString());
if(this.erreurSQL()){
this.deleteEtu(et, g);
}
}
this.close(cnx);
}
private void deleteEtu(PreparedStatement pst, Connection cnx, Groupe g){
try{
pst.setInt(1, g.getId());
pst.executeUpdate();
for(Groupe sous: g.getSousGroupes()){
this.deleteEtu(pst, cnx, sous);
}
}catch(SQLException e){
System.out.println(e.toString());
if(this.erreurSQL()){
this.deleteEtu(pst, cnx, g);
}
}
}
private void close(AutoCloseable clos){
try{
clos.close();
}catch(Exception e){
if(this.erreurCO()==true)
this.close(clos);
}
}
private boolean saveEtu(Etudiant etudiant, Groupe g){
Connection cnx = this.cnx();
try{
PreparedStatement pst=cnx.prepareStatement(
"Select id from Etudiant where id=?; ");
pst.setString(1, String.valueOf(etudiant.getId()));
ResultSet rs=pst.executeQuery();
if(rs.next()){
//L'etudiant est déja connu de la BD
pst.close();
pst=cnx.prepareStatement(
"INSERT INTO `CONTIENT` (`idGroupe`, `idEt`) VALUES (?, ?);");
pst.setInt(2, etudiant.getId());
pst.setInt(1, g.getId());
pst.executeUpdate();
}else{
pst.close();
pst=cnx.prepareStatement(
"INSERT INTO `Etudiant` (`id`, `nom`, `prenom`) VALUES (?, ?, ?) ;");
pst.setInt(1, etudiant.getId());
pst.setString(2, etudiant.getNom());
pst.setString(3, etudiant.getPrenom());
pst.executeUpdate();
}
rs.close();
pst.close();
}catch(SQLException e){
System.out.println(e.toString());
if(this.erreurSQL()){
this.saveEtu(etudiant, g);
}else{
return false;
}
}
this.close(cnx);
return true;
}
private boolean saveGroupe(Groupe g){
Connection cnx=this.cnx();
try{
PreparedStatement pst=cnx.prepareStatement(
"INSERT INTO `Groupe` (`id`, `nom`, `min`, `max`, `Type`, `id-parent`) VALUES (?, ?, ?, ?, ?, ?);"
);
pst.setInt(1, g.getId());
pst.setString(2, g.getName());
pst.setInt(3, g.getMin());
pst.setInt(4, g.getMax());
pst.setString(5, g.getType().name());
pst.setInt(6, g.getPointPoint().getId());
pst.executeUpdate();
pst.close();
for(Etudiant e: g.getEtudiants()){
this.saveEtu(e, g);
}
for(Groupe sous:g.getSousGroupes()){
this.saveGroupe(sous);
}
}catch(SQLException e){
System.out.println(e.toString());
if(this.erreurSQL()){
this.saveGroupe(g);
}else{
return false;
}
}
this.close(cnx);
return true;
}
private boolean suprGroupe(Groupe g){
Connection cnx=this.cnx();
try{
PreparedStatement pst=cnx.prepareStatement("Delete FROM Groupe where id=?;");
pst.setInt(1, g.getId());
pst.executeUpdate();
pst.close();
pst=cnx.prepareStatement("Delete FROM CONTIENT where idGroupe=?;");
pst.setInt(1, g.getId());
pst.executeUpdate();
pst.close();
}catch(SQLException e){
System.out.println(e.toString());
if(this.erreurSQL()){
this.suprGroupe(g);
}else{
return false;
}
}
this.close(cnx);
return true;
}
}