simplification repartition class et constructeur et modif API
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
package fr.iutfbleau.projetIHM2022FI2.ETU.Model;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import fr.iutfbleau.projetIHM2022FI2.API.AbstractChangementFactory;
|
||||
import fr.iutfbleau.projetIHM2022FI2.API.AbstractGroupeFactory;
|
||||
import fr.iutfbleau.projetIHM2022FI2.API.Etudiant;
|
||||
@@ -16,10 +10,8 @@ import fr.iutfbleau.projetIHM2022FI2.API.Groupe;
|
||||
import fr.iutfbleau.projetIHM2022FI2.API.TypeGroupe;
|
||||
import fr.iutfbleau.projetIHM2022FI2.MNP.AbstractChangementFactoryNP;
|
||||
import fr.iutfbleau.projetIHM2022FI2.MNP.AbstractGroupeFactoryNP;
|
||||
import fr.iutfbleau.projetIHM2022FI2.MNP.GroupeNP;
|
||||
import fr.iutfbleau.projetIHM2022FI2.Permanent.View.Chargement;
|
||||
import fr.iutfbleau.projetIHM2022FI2.ETU.Controller.ObservateurFenetre;
|
||||
import fr.iutfbleau.projetIHM2022FI2.ETU.Util.BD;
|
||||
import fr.iutfbleau.projetIHM2022FI2.ETU.View.Chargement;
|
||||
import fr.iutfbleau.projetIHM2022FI2.ETU.View.FenetreEtudiant;
|
||||
import fr.iutfbleau.projetIHM2022FI2.ETU.View.FenetreGroupe;
|
||||
|
||||
@@ -36,7 +28,6 @@ public class Model{
|
||||
private JFrame fenetre;
|
||||
private Etudiant Selected;
|
||||
|
||||
private BD bd;
|
||||
public Model(){
|
||||
this.fenetre=new JFrame();
|
||||
this.fenetre.setSize(1200, 720);
|
||||
@@ -45,9 +36,11 @@ public class Model{
|
||||
this.fenetre.setLayout(new GridLayout(1,2));
|
||||
this.fenetre.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
||||
this.fenetre.setMinimumSize(this.fenetre.getSize());
|
||||
this.bd=new BD(this.fenetre);
|
||||
Chargement ch=new Chargement(this.fenetre);
|
||||
this.promo=this.getPromo(ch);
|
||||
this.promo=new AbstractGroupeFactoryNP(this.fenetre);
|
||||
if(this.promo.getPromotion()==null){
|
||||
this.promo=null;
|
||||
}
|
||||
ch.dispose();
|
||||
this.fenetre.setVisible(true);
|
||||
this.initEtu(null);
|
||||
@@ -68,13 +61,14 @@ public class Model{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fonction pour refresh/changer de groupe d'affichage
|
||||
* @param g le groupe a afficher
|
||||
*/
|
||||
public void showGroupe(Groupe g){
|
||||
if(g!=null)
|
||||
g=this.bd.refreshALL(g);
|
||||
g=this.promo .refreshALL(g);
|
||||
this.panGroupe.removeAll();
|
||||
this.fenGr=new FenetreGroupe(g, this, this.promo.getGroupesOfEtudiant(this.Selected));
|
||||
this.fenEtu=new FenetreEtudiant(g, this.Selected, this);
|
||||
@@ -94,58 +88,9 @@ public void showGroupe(Groupe g){
|
||||
public JFrame getFenetre() {
|
||||
return fenetre;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fonction permettant d'initialiser l'AbstractFactory de la promo
|
||||
* avec la Base de donné
|
||||
* @param Chargement: pour indiquer la progression du chargement
|
||||
*/
|
||||
private AbstractGroupeFactory getPromo(Chargement chargement){
|
||||
AbstractGroupeFactory agf=null;
|
||||
Connection cnx=this.bd.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();
|
||||
rs.last();
|
||||
int nbgrp=rs.getRow();
|
||||
rs=pst.executeQuery();
|
||||
try{
|
||||
//Si il existe bien une promotion
|
||||
if(rs.next()){
|
||||
//On créer le groupe de promo
|
||||
Groupe groupe=new GroupeNP(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4), TypeGroupe.getType(rs.getString(5)), null);
|
||||
//On lui ajoute tout ses sous-groupe
|
||||
this.init(groupe, (100/nbgrp), chargement);
|
||||
//On créer la Factory
|
||||
agf=new AbstractGroupeFactoryNP(groupe, this.fenetre);
|
||||
}else{
|
||||
//Si aucune ligne et donc pas de promo:
|
||||
this.promo=null;
|
||||
}
|
||||
}catch(SQLException e){
|
||||
}
|
||||
rs.close();
|
||||
pst.close();
|
||||
}catch(SQLException e){
|
||||
}
|
||||
this.bd.close(cnx);
|
||||
return agf;
|
||||
}
|
||||
|
||||
public Set<Etudiant> getEtudiant(){
|
||||
return this.bd.getEtudiant();
|
||||
}
|
||||
|
||||
private void init(Groupe g, int pourcentage, Chargement ch){
|
||||
this.bd.refreshALL(g);
|
||||
ch.addPourcent(pourcentage+1);
|
||||
for(Groupe gr:g.getSousGroupes()){
|
||||
this.init(gr, pourcentage, ch);
|
||||
}
|
||||
return this.promo.getPromotion().getEtudiants();
|
||||
}
|
||||
|
||||
private void initEtu(String err){
|
||||
@@ -179,208 +124,13 @@ public void showGroupe(Groupe g){
|
||||
this.initEtu("Etudiant introuvable");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ***********************
|
||||
// FONCTION POUR SUPRIMER UN ETUDIANT
|
||||
// ***********************
|
||||
|
||||
|
||||
public boolean deleteEtu(Etudiant e){
|
||||
if(this.deleteEtutoChildren(e, this.fenGr.getG())){
|
||||
this.promo.dropFromGroupe(this.fenGr.getG(), e);
|
||||
this.showGroupe(this.fenGr.getG());
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean deleteEtutoChildren(Etudiant e, Groupe g){
|
||||
if(g.getMax()<g.getEtudiants().size()-1)
|
||||
return false;
|
||||
for(Groupe sous: g.getSousGroupes()){
|
||||
if(this.deleteEtutoChildren(e, sous)==false){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// ***********************
|
||||
// FONCTION POUR Ajouter UN ETUDIANT
|
||||
// ***********************
|
||||
|
||||
/**
|
||||
* Fonction pour ajouter un étudiant au groupe
|
||||
* @param g
|
||||
* @param e
|
||||
* @return boolean pour savoir si il a pus être ajouter
|
||||
*/
|
||||
public boolean addEtudiant(Groupe g, Etudiant e){
|
||||
//Si on a la place
|
||||
if(g!=g.getPointPoint())
|
||||
if(this.addEtuToParent(g.getPointPoint(), e)==false)
|
||||
return false;
|
||||
//On induqe a la BD de sauvegarder cette modification
|
||||
this.promo.addToGroupe(g, e);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction recursive pour ajouter l'etudiant a ses parents si il ne l'ont pas
|
||||
* @param g groupe
|
||||
* @param e etudiant
|
||||
* @return true si possible
|
||||
*/
|
||||
private boolean addEtuToParent(Groupe g, Etudiant e){
|
||||
if(g.getMax()==g.getSize())
|
||||
return false;
|
||||
for(Etudiant et:g.getEtudiants()){
|
||||
if(et.getId()==e.getId()){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if(g.getPointPoint()!=g){
|
||||
if(this.addEtuToParent(g.getPointPoint(), e)==false){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
this.promo.addToGroupe(g, e);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// ***********************
|
||||
// FONCTION POUR SUPRIMER UN GROUPE
|
||||
// ***********************
|
||||
|
||||
/**
|
||||
* Fonction pour supprimer un groupe
|
||||
* @param g le groupe a supprimer
|
||||
*/
|
||||
public void delete(Groupe g){
|
||||
//On ne peut pas supprimer la promo normallement
|
||||
if(g==this.promo.getPromotion()){
|
||||
if(JOptionPane.showConfirmDialog(this.fenetre, "Attention ête vous sûr de vouloir supprimer la promo", "Attention", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
|
||||
this.promo.deleteGroupe(g);
|
||||
this.promo=null;
|
||||
this.changement=null;
|
||||
this.showGroupe(null);
|
||||
}else{
|
||||
return;
|
||||
}
|
||||
//JOptionPane.showMessageDialog(this.fenetre, "impossible de supprimer la promotion", "alerte", JOptionPane.ERROR_MESSAGE);
|
||||
}else{
|
||||
//on suprime le groupe
|
||||
this.promo.deleteGroupe(g);
|
||||
this.showGroupe(g.getPointPoint());
|
||||
}
|
||||
}
|
||||
|
||||
// ***********************
|
||||
// FONCTION POUR RENOMER UN GROUPE
|
||||
// ***********************
|
||||
|
||||
/**
|
||||
* Fonction pour renomer un groupe
|
||||
* @param name
|
||||
* @param g
|
||||
*/
|
||||
public void rename(String name, Groupe g){
|
||||
this.promo.changeNameGroupe(g, name);
|
||||
this.showGroupe(g);
|
||||
}
|
||||
|
||||
|
||||
// **********************
|
||||
// FONCTION POUR CREER UN GROUPE
|
||||
// **********************
|
||||
|
||||
|
||||
/**
|
||||
* Fonction permetant de créer une partition d'un groupe
|
||||
* @param g le groupe a partitionner
|
||||
* @param n le nombre de partition
|
||||
* @param name le nom des partition
|
||||
*/
|
||||
public void partition(Groupe g, int n, String name){
|
||||
Chargement ch=new Chargement(this.fenetre);
|
||||
this.fenetre.setVisible(false);
|
||||
this.promo.createPartition(g, name, n);
|
||||
ch.dispose();
|
||||
this.fenetre.setVisible(true);
|
||||
this.showGroupe(g);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction permettant de créer un sous-groupe d'un groupe
|
||||
* @param g le groupe parent
|
||||
* @param name le nom du sous-groupe
|
||||
* @param min le nombre min d'etudiant
|
||||
* @param max le nombre max d'etudiant
|
||||
* @param ajout la liste des étudiants a ajouter au groupe
|
||||
*/
|
||||
public void free(Groupe g, String name, int min, int max, Set<Etudiant> ajout){
|
||||
Chargement ch=new Chargement(this.fenetre);
|
||||
this.fenetre.setVisible(false);
|
||||
//on creer le groupe
|
||||
this.promo.createGroupe(g, name, min, max);
|
||||
Groupe creer=null;
|
||||
//on le recupere
|
||||
for(Groupe gr:g.getSousGroupes()){
|
||||
if(gr.getName()==name){
|
||||
creer=gr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//on y ajoute les étudiant
|
||||
int n=0;
|
||||
for(Etudiant e:ajout){
|
||||
if(this.addEtudiant(creer, e)==false)
|
||||
n++;
|
||||
}
|
||||
creer=this.bd.refreshALL(creer);
|
||||
ch.dispose();
|
||||
this.fenetre.setVisible(true);
|
||||
if(n>0)
|
||||
JOptionPane.showMessageDialog(this.fenetre, "erreur impossible d'ajouter "+n+" etudiants par manque de place dans le groupe de ses parents", "erreur", JOptionPane.ERROR_MESSAGE);
|
||||
//Si finalement avec les etudiants qui n'ont pas pus être ajouter le min n'est pas atteint
|
||||
if(creer.getEtudiants().size()+1<=creer.getMin()){
|
||||
//On le supprime
|
||||
this.promo.deleteGroupe(creer);
|
||||
JOptionPane.showMessageDialog(this.fenetre, "en raison du manque d'etudiant pour le nombre min le groupe n'a pas pus être créer", "errer", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
this.showGroupe(g);
|
||||
}
|
||||
|
||||
public void addPromo(int min, int max, String name, Set<Etudiant> ajout){
|
||||
Chargement ch=new Chargement(this.fenetre);
|
||||
this.promo=new AbstractGroupeFactoryNP(name, min, max, this.fenetre);
|
||||
this.changement=new AbstractChangementFactoryNP(promo, this.fenetre);
|
||||
this.fenetre.setVisible(false);
|
||||
ch.addPourcent(20);
|
||||
int pourcent=85/ajout.size();
|
||||
for(Etudiant e:ajout){
|
||||
this.addEtudiant(this.promo.getPromotion(), e);
|
||||
ch.addPourcent(pourcent);
|
||||
}
|
||||
ch.dispose();
|
||||
this.fenetre.setVisible(true);
|
||||
this.showGroupe(this.promo.getPromotion());
|
||||
}
|
||||
|
||||
|
||||
// **************************
|
||||
// FONCTION POUR LES CHANGEMENTS
|
||||
// ******************************
|
||||
public void changeGroupe(Etudiant e, Groupe b){
|
||||
if(b==null)
|
||||
return;
|
||||
b=this.bd.refreshGroupe(b);
|
||||
b=this.promo.refreshALL(b);
|
||||
if(b.getEtudiants()!=null && b.getMax()>b.getEtudiants().size()+1){
|
||||
this.changement.createChangement(this.fenGr.getG(), e, b);
|
||||
}else{
|
||||
@@ -390,7 +140,7 @@ public void showGroupe(Groupe g){
|
||||
}
|
||||
|
||||
public Set<Groupe> getGroupePartition(){
|
||||
this.bd.refreshGroupe(this.fenGr.getG().getPointPoint());
|
||||
this.promo.refreshALL(this.fenGr.getG().getPointPoint());
|
||||
Set<Groupe> retour=new HashSet<>();
|
||||
if(this.fenGr.getG().getPointPoint().getType()!=TypeGroupe.PARTITION)
|
||||
throw new IllegalStateException("impossible de changer un étudiant d'un groupe ne provenant pas d'une partition");
|
||||
|
Reference in New Issue
Block a user