simplification repartition class et constructeur et modif API

This commit is contained in:
2022-12-01 19:58:40 +01:00
parent b926dc4d32
commit 35696d3b14
16 changed files with 449 additions and 1217 deletions

View File

@@ -27,7 +27,8 @@ public class AbstractChangementFactoryNP implements AbstractChangementFactory {
Objects.requireNonNull(agf,"On ne peut pas créer une usine à changement dont l'usine à groupe parternaire est null");
this.agf=agf;
this.fenetre=fenetre;
this.brain=new HashMap<Integer,Changement>();
this.brain=new HashMap<Integer,Changement>();
this.getChange();
}
public AbstractChangementFactoryNP(AbstractGroupeFactory agf, JFrame fenetre, Set<Changement> liste){
@@ -37,7 +38,8 @@ public class AbstractChangementFactoryNP implements AbstractChangementFactory {
this.brain=new HashMap<Integer,Changement>();
for(Changement ch:liste){
this.brain.put(ch.getId(), ch);
}
}
this.getChange();
}
@@ -207,4 +209,40 @@ public class AbstractChangementFactoryNP implements AbstractChangementFactory {
this.close(clos);
}
}
private void getChange(){
Connection cnx=this.cnx();
try{
PreparedStatement pst=cnx.prepareStatement("SELECT * FROM `Changement` NATURAL JOIN Etudiant; ");
ResultSet rs=pst.executeQuery();
while(rs.next()){
Groupe[] ab=new Groupe[2];
this.getGroupe(rs.getInt(2), rs.getInt(3), ab, this.agf.getPromotion());
Etudiant e=new EtudiantNP(rs.getString(5), rs.getString(6), rs.getInt(4));
this.brain.put(rs.getInt(1), new ChangementNP(ab[0], e, ab[1], rs.getInt(1)));
}
pst.close();
}catch(SQLException e){
if(this.erreurCO())
this.getChange();
return;
}
this.close(cnx);
}
private void getGroupe(int idA, int idB, Groupe[] retour, Groupe tofind){
if(retour[0]!=null && retour[1]!=null)
return;
if(tofind.getId()==idA){
retour[0]=tofind;
}
if(tofind.getId()==idB){
retour[1]=tofind;
}
for(Groupe sous:tofind.getSousGroupes()){
this.getGroupe(idA, idB, retour, sous);
}
}
}

View File

@@ -29,11 +29,10 @@ 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, JFrame fenetre){
this.promo=promo;
public AbstractGroupeFactoryNP(JFrame fenetre){
this.fenetre=fenetre;
this.brain=new HashMap<Integer,Groupe>();
this.addSousGroupe(this.promo);
this.init();
}
/**
@@ -49,18 +48,6 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
this.saveGroupe(promo);
}
/**
* permet d'ajouter les sous groupe lors de l'initialisation de la promo
* avec un groupe déja créer
*
* @param g le groupe (methode récursive)
*/
private void addSousGroupe(Groupe g){
this.brain.put(g.getId(), g);
for(Groupe s:g.getSousGroupes()){
this.addSousGroupe(s);
}
}
/**
* Test plutôt optimiste. Si la clé est identique alors on fait comme si c'était le bon groupe.
@@ -118,10 +105,9 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
*/
public void deleteGroupe(Groupe g){
Objects.requireNonNull(g,"On ne peut pas enlever un groupe null car null n'est pas un groupe autorisé");
//if (!this.knows(g)){
//throw new IllegalArgumentException("Impossible d'enlever un groupe inconnu");
//possible maintenant
//}
if (!this.knows(g)){
throw new IllegalArgumentException("Impossible d'enlever un groupe inconnu");
}
g.getPointPoint().removeSousGroupe(g);
this.brain.remove(Integer.valueOf(g.getId()));
this.suprGroupe(g);
@@ -141,10 +127,9 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
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");
//Possible maintenant
//}
if (!this.knows(pere)){
throw new IllegalArgumentException("Interdit d'ajouter un fils à un groupe inconnu");
}
if (pere.getType().equals(TypeGroupe.PARTITION)){
throw new IllegalArgumentException("Impossible d'ajouter un groupe à une parition. Il faut utiliser createPartition pour créer une partition");
}
@@ -177,10 +162,9 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
public void createPartition(Groupe pere, String name, int n){
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("Impossible de partitionner ce groupe inconnu");
//possible maintenant
//}
if (!this.knows(pere)){
throw new IllegalArgumentException("Impossible de partitionner ce groupe inconnu");
}
if (pere.getType().equals(TypeGroupe.PARTITION)){
throw new IllegalArgumentException("Impossible de créer une partition à ce niveau. Il faut soit repartitionner le groupe au dessus, soit partitionner une partition en dessous.");
}
@@ -224,10 +208,9 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
public void addToGroupe(Groupe g, Etudiant e){
Objects.requireNonNull(g,"Le groupe ne peut pas être null");
Objects.requireNonNull(e,"L'étudiant ne peut pas être null");
//if (!this.knows(g)){
//throw new IllegalArgumentException("Impossible d'ajouter l'étudiant car le est groupe inconnu");
//rendu possible maintenant par la syncronisation en temps réel
//}
if (!this.knows(g)){
throw new IllegalArgumentException("Impossible d'ajouter l'étudiant car le est groupe inconnu");
}
g.addEtudiant(e);
this.saveEtu(e, g);
}
@@ -245,10 +228,9 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
public void dropFromGroupe(Groupe g, Etudiant e){
Objects.requireNonNull(g,"Le groupe ne peut pas être null");
Objects.requireNonNull(e,"L'étudiant ne peut pas être null");
//if (!this.knows(g)){
//throw new IllegalArgumentException("Impossible de suprimer l'étudiant car le est groupe inconnu");
//Possible maintenant
//}
if (!this.knows(g)){
throw new IllegalArgumentException("Impossible de suprimer l'étudiant car le est groupe inconnu");
}
g.removeEtudiant(e);
this.deleteEtu(e, g);
}
@@ -268,7 +250,7 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
public Set<Etudiant> getEtudiants(String nomEtu){
Set<Etudiant> s=new LinkedHashSet<>();
for(Etudiant e:this.getPromotion().getEtudiants()){
if((e.getNom()).equals(nomEtu.toString())){
if((e.getNom()).contains(nomEtu)){
s.add(e);
}
}
@@ -284,11 +266,13 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
* @throws java.lang.NullPointerException si le String est null.
*/
public Set<Groupe> getGroupesOfEtudiant(Etudiant etu){
if(etu==null)
throw new NullPointerException();
Collection<Groupe> s= this.brain.values();
Set<Groupe> ret=new LinkedHashSet<Groupe>();
for(Groupe g: s){
for(Etudiant e: g.getEtudiants()){
if(e==etu){
if(etu==e){
ret.add(g);
break;
}
@@ -476,4 +460,191 @@ private boolean saveEtu(Etudiant etudiant, Groupe g){
this.close(cnx);
return true;
}
/*****************************
* FONCTION POUR INITIALISER LA FACTORY AVEC LA BD
* ********************************
*/
private void init(){
Connection cnx=this.cnx();
//on récupère le Groupe de la BD n'ayant pas de Parent (La promo donc)
try{
PreparedStatement pst = cnx.prepareStatement(
"SELECT `id`, `nom`, `min`, `max`, `value`, `id-parent` FROM `Groupe` join `TYPE` on Groupe.Type=TYPE.name ORDER BY Groupe.id ASC;");
ResultSet rs = pst.executeQuery();
rs=pst.executeQuery();
try{
//Si il existe bien une promotion
if(rs.next()){
//On créer le groupe de promo
this.promo=new GroupeNP(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4), TypeGroupe.getType(rs.getString(5)), null);
//on ajoute au cerveau
this.brain.put(this.promo.getId(), this.promo);
//On lui ajoute tout ses sous-groupe
this.addSousGroupe(this.promo, cnx);
//On ajoute les étudiants:
for(Groupe sous: this.brain.values()){
this.addBDEtudiant(sous, cnx);
}
}else{
//Si aucune ligne et donc pas de promo:
this.promo=null;
}
}catch(SQLException e){
}
rs.close();
pst.close();
}catch(SQLException e){
}
this.close(cnx);
}
/**
* Fonction recursive permettant de récuperrer les sous groupe a partir de la BD
* @param g le groupe
* @param cnx la connection a la BD (evite de la surcharger)
* @param pourcent le pourcentage de ce groupe dans le chargement
*/
private void addSousGroupe(Groupe g, Connection cnx){
try{
//On récupere les Groupe qui ont le parent :g
PreparedStatement pst= cnx.prepareStatement(
"SELECT `id`, `nom`, `min`, `max`, `value`, `id-parent` FROM `Groupe` join `TYPE` on Groupe.Type=TYPE.name where Groupe.`id-parent`=? and Groupe.id!=Groupe.`id-parent`;");
pst.setString(1, String.valueOf(g.getId()));
ResultSet rs=pst.executeQuery();
rs=pst.executeQuery();
//autrement si le groupe as des sous groupe
while(rs.next()){
//on les ajoute
Groupe nouveau=new GroupeNP(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4), TypeGroupe.getType(rs.getString(5)), g);
//Si one le connait pas
if(this.brain.get(nouveau.getId())==null){
this.brain.put(nouveau.getId(), nouveau);
}
g.addSousGroupe(nouveau);
this.addSousGroupe(nouveau, cnx);
//on ajoute les sous groupe des sous-groupe
}
rs.close();
}catch(SQLException e){
if(this.erreurCO())
this.addSousGroupe(g, cnx);
}
}
/**
* Fonction recursive ajoutant les étudiant aux groupe de la promo
* @param g le groupe pour qui ajouter les Etudiant
* @param cnx la connection (evite de surcharger la BD)
*/
private void addBDEtudiant(Groupe g, Connection cnx){
try{
//On récupère les etudiants contenue du groupe
PreparedStatement pst;
//Si c'est la promo
pst= cnx.prepareStatement("SELECT Etudiant.nom, Etudiant.prenom, Etudiant.id FROM `CONTIENT` JOIN Etudiant on CONTIENT.idEt=Etudiant.id WHERE CONTIENT.idGroupe=? ORDER BY Etudiant.id ASC");
pst.setInt(1, g.getId());
ResultSet rs=pst.executeQuery();
//Pour tous les étudiants
while(rs.next()){
boolean exist=false;
//autrement on recupere l'etudiant
for(Etudiant e: g.getPointPoint().getEtudiants()){
if(e.getId()==rs.getInt(3)){
exist=true;
g.addEtudiant(e);
break;
}
}
if(exist==false){
g.addEtudiant(new EtudiantNP(rs.getString(1), rs.getString(2), rs.getInt(3)));
}
}
rs.close();
pst.close();
}catch(SQLException e){
if(this.erreurCO())
this.addBDEtudiant(g, cnx);
}
}
/*******************
* FONCTION POUR REFRESH
* *******************
*/
/**
* Refresh le groupe donnée pour le mettre a jour avec la Base de donnée
* Si le groupe n'existe plus on retourne le groupe parent le plus proche existant encore
* @param g le groupe a refresh
* @return le groupe refresh
*/
private Groupe refreshGroupe(Groupe g, Connection cnx){
try{
PreparedStatement pst = cnx.prepareStatement(
"SELECT `id`, `nom`, `min`, `max`, `value`, `id-parent` FROM `Groupe` join `TYPE` on Groupe.Type=TYPE.name where Groupe.`id`=? OR Groupe.`id-parent`=? ORDER BY Groupe.id ASC;");
pst.setString(1, String.valueOf(g.getId()));
pst.setString(2, String.valueOf(g.getId()));
ResultSet rs=pst.executeQuery();
if(rs.next()){
if(rs.getString(2)!=g.getName()){
g.setName(rs.getString(2));
}
Set<Groupe> it=new LinkedHashSet<>();
for(Groupe gr:g.getSousGroupes()){
it.add(gr);
}
for(Groupe gr:it){
g.removeSousGroupe(gr);
}
Set<Etudiant> et=new LinkedHashSet<>();
for(Etudiant gr:g.getEtudiants()){
et.add(gr);
}
for(Etudiant gr:et){
g.removeEtudiant(gr);
}
this.addSousGroupe(g, cnx);
}else{
if(g==g.getPointPoint()){
g=null;
}else{
g=this.refreshGroupe(g.getPointPoint(), cnx);
}
}
rs.close();
}catch(SQLException e){
if(this.erreurCO())
this.refreshGroupe(g, cnx);
}
return g;
}
/**
* refresh le Groupe ainsi que ses etudiants
* @param g le groupe
* @return le groupe refresh/un parent si il a été refresh
*/
public Groupe refreshALL(Groupe g){
Connection cnx = this.cnx();
g=this.refreshGroupe(g, cnx);
Set<Etudiant> et=new LinkedHashSet<>();
for(Etudiant gr:g.getEtudiants()){
et.add(gr);
}
for(Etudiant gr:et){
g.removeEtudiant(gr);
}
this.addBDEtudiant(g, cnx);
this.close(cnx);
return g;
}
}