2022-11-15 15:08:13 +01:00
package fr.iutfbleau.projetIHM2022FI2.Graphic.Model ;
import java.sql.PreparedStatement ;
import java.sql.SQLException ;
import java.util.Set ;
import java.sql.Connection ;
import java.sql.ResultSet ;
2022-11-17 10:34:29 +01:00
2022-11-15 15:08:13 +01:00
import javax.swing.* ;
import java.awt.* ;
import fr.iutfbleau.projetIHM2022FI2.API.AbstractGroupeFactory ;
import fr.iutfbleau.projetIHM2022FI2.API.Etudiant ;
import fr.iutfbleau.projetIHM2022FI2.API.Groupe ;
import fr.iutfbleau.projetIHM2022FI2.API.TypeGroupe ;
import fr.iutfbleau.projetIHM2022FI2.Graphic.Controller.ObservateurFenetre ;
import fr.iutfbleau.projetIHM2022FI2.Graphic.Util.BD ;
import fr.iutfbleau.projetIHM2022FI2.Graphic.View.Chargement ;
2022-11-17 13:46:28 +01:00
import fr.iutfbleau.projetIHM2022FI2.Graphic.View.DefaultMutableTreeNodeGroupe ;
2022-11-15 15:08:13 +01:00
import fr.iutfbleau.projetIHM2022FI2.Graphic.View.FenetreEtudiant ;
import fr.iutfbleau.projetIHM2022FI2.Graphic.View.FenetreGroupe ;
2022-11-17 13:13:43 +01:00
import fr.iutfbleau.projetIHM2022FI2.Graphic.View.JTreeGroupe ;
2022-11-15 15:08:13 +01:00
import fr.iutfbleau.projetIHM2022FI2.MNP.AbstractGroupeFactoryNP ;
import fr.iutfbleau.projetIHM2022FI2.MNP.GroupeNP ;
/ * *
* Le Model de L ' IHM
* /
public class Model {
2022-11-17 14:45:42 +01:00
2022-11-17 13:46:28 +01:00
private JPanel panGroupe ;
2022-11-15 15:08:13 +01:00
private FenetreGroupe fenGr ;
private FenetreEtudiant fenEtu ;
private AbstractGroupeFactory promo ;
private JFrame fenetre ;
2022-11-17 13:46:28 +01:00
private JTree tree ;
2022-11-15 15:08:13 +01:00
private BD bd ;
2022-11-17 14:45:42 +01:00
2022-11-15 15:08:13 +01:00
public Model ( ) {
2022-11-17 13:13:43 +01:00
this . fenetre = new JFrame ( ) ;
this . fenetre . setSize ( 1200 , 720 ) ;
2022-11-17 13:46:28 +01:00
this . fenetre . setLocation ( 100 , 100 ) ;
this . fenetre . addWindowListener ( new ObservateurFenetre ( ) ) ;
this . fenetre . setLayout ( new GridLayout ( 1 , 2 ) ) ;
this . fenetre . setDefaultCloseOperation ( JFrame . DO_NOTHING_ON_CLOSE ) ;
this . fenetre . setMinimumSize ( this . fenetre . getSize ( ) ) ;
2022-11-17 14:45:42 +01:00
2022-11-15 15:08:13 +01:00
this . bd = new BD ( this . fenetre ) ;
2022-11-17 13:13:43 +01:00
Chargement ch = new Chargement ( this . fenetre . getSize ( ) ) ;
2022-11-15 15:08:13 +01:00
this . promo = this . getPromo ( ch ) ;
2022-11-16 14:59:56 +01:00
ch . dispose ( ) ;
2022-11-17 14:45:42 +01:00
this . fenetre . setVisible ( true ) ;
2022-11-15 15:08:13 +01:00
if ( this . promo = = null ) {
this . fenGr = new FenetreGroupe ( null , this ) ;
2022-11-17 13:13:43 +01:00
this . fenEtu = new FenetreEtudiant ( null , this ) ;
2022-11-15 15:08:13 +01:00
} else {
this . fenGr = new FenetreGroupe ( this . promo . getPromotion ( ) , this ) ;
2022-11-17 13:13:43 +01:00
this . fenEtu = new FenetreEtudiant ( this . promo . getPromotion ( ) , this ) ;
2022-11-15 15:08:13 +01:00
}
2022-11-17 13:46:28 +01:00
this . panGroupe = new JPanel ( new GridLayout ( 2 , 1 ) ) ;
2022-11-17 14:45:42 +01:00
this . initJtree ( this . promo . getPromotion ( ) ) ;
2022-11-17 13:46:28 +01:00
if ( this . promo ! = null ) {
2022-11-16 11:14:29 +01:00
this . showGroupe ( this . promo . getPromotion ( ) ) ;
2022-11-17 13:46:28 +01:00
} else {
this . showGroupe ( null ) ;
}
2022-11-15 15:08:13 +01:00
}
/ * *
* Fonction pour refresh / changer de groupe d ' affichage
* @param g le groupe a afficher
* /
public void showGroupe ( Groupe g ) {
2022-11-16 14:59:56 +01:00
if ( g ! = null )
g = this . bd . refreshALL ( g ) ;
2022-11-17 13:46:28 +01:00
2022-11-17 14:45:42 +01:00
//this.initJtree(this.promo.getPromotion());
2022-11-17 13:46:28 +01:00
this . panGroupe . removeAll ( ) ;
2022-11-15 15:08:13 +01:00
this . fenGr = new FenetreGroupe ( g , this ) ;
2022-11-17 13:13:43 +01:00
this . fenEtu = new FenetreEtudiant ( g , this ) ;
2022-11-15 15:08:13 +01:00
this . fenetre . getContentPane ( ) . removeAll ( ) ;
2022-11-17 13:46:28 +01:00
this . panGroupe . add ( this . tree ) ;
this . panGroupe . add ( this . fenGr ) ;
2022-11-17 14:45:42 +01:00
this . panGroupe . revalidate ( ) ;
2022-11-17 13:46:28 +01:00
this . fenetre . add ( this . panGroupe ) ;
2022-11-15 15:08:13 +01:00
JScrollPane scroll = new JScrollPane ( this . fenEtu ) ;
scroll . getVerticalScrollBar ( ) . setUnitIncrement ( 15 ) ;
this . fenetre . add ( scroll ) ;
this . fenetre . revalidate ( ) ;
}
/ * *
* getteur de la fenetre
* @return JFrame la fenetre
* /
public JFrame getFenetre ( ) {
return fenetre ;
}
/ * *
* Fonction pour supprimer un groupe
* @param g le groupe a supprimer
* /
public void delete ( Groupe g ) {
2022-11-16 17:03:05 +01:00
//On ne peut pas supprimer la promo normallement
2022-11-15 15:08:13 +01:00
if ( g = = this . promo . getPromotion ( ) ) {
2022-11-16 17:03:05 +01:00
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 = null ;
this . bd . suprGroupe ( g ) ;
this . showGroupe ( null ) ;
} else {
return ;
}
2022-11-16 14:59:56 +01:00
//JOptionPane.showMessageDialog(this.fenetre, "impossible de supprimer la promotion", "alerte", JOptionPane.ERROR_MESSAGE);
2022-11-16 11:14:29 +01:00
} else {
//autrement on récupere les groupe a supprimer par ordre avec une fonction recursive
//elle contiendra les sous-groupe remontant j'usqau groupe a supprimer
//On initialise la liste
this . bd . suprGroupe ( g ) ;
this . showGroupe ( g . getPointPoint ( ) ) ;
2022-11-15 15:08:13 +01:00
}
}
2022-11-16 11:14:29 +01:00
private int getTailleGroupe ( Groupe g ) {
int i = 0 ;
for ( Groupe gr : g . getSousGroupes ( ) ) {
i + = this . getTailleGroupe ( gr ) ;
2022-11-15 15:08:13 +01:00
}
2022-11-16 11:14:29 +01:00
return 1 + i ;
2022-11-15 15:08:13 +01:00
}
/ * *
* 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 ) {
2022-11-17 13:13:43 +01:00
Chargement ch = new Chargement ( this . fenetre . getSize ( ) ) ;
2022-11-15 15:08:13 +01:00
this . promo . createPartition ( g , name , n ) ;
//On recherche le groupe Partitionner pour le sauvegarder dans la BD
2022-11-16 11:14:29 +01:00
2022-11-15 15:08:13 +01:00
for ( Groupe gr : g . getSousGroupes ( ) ) {
2022-11-16 11:14:29 +01:00
if ( gr . getSousGroupes ( ) . size ( ) > 0 & & gr . getSousGroupes ( ) . iterator ( ) . next ( ) . getName ( ) . contains ( name ) ) {
this . fenetre . setVisible ( false ) ;
this . bd . saveGroupe ( gr , 100 / this . getTailleGroupe ( gr ) + 1 , ch ) ;
2022-11-16 14:59:56 +01:00
ch . dispose ( ) ;
2022-11-16 11:14:29 +01:00
this . fenetre . setVisible ( true ) ;
2022-11-15 15:08:13 +01:00
break ;
}
}
//On dis a la BD de sauvegarder ce groupe et tous ces sous Groupe
2022-11-16 11:14:29 +01:00
this . showGroupe ( g ) ;
2022-11-15 15:08:13 +01:00
}
/ * *
* 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 ) {
//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 ;
}
}
2022-11-16 11:14:29 +01:00
//on crééer le groupe
this . bd . createGroupe ( creer ) ;
2022-11-15 15:08:13 +01:00
//on y ajoute les étudiant
int n = 0 ;
for ( Etudiant e : ajout ) {
if ( this . addEtudiant ( creer , e ) = = false )
n + + ;
}
2022-11-16 11:14:29 +01:00
creer = this . bd . refreshALL ( creer ) ;
2022-11-15 15:08:13 +01:00
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
2022-11-16 11:14:29 +01:00
if ( creer . getEtudiants ( ) . size ( ) < = creer . getMin ( ) ) {
2022-11-15 15:08:13 +01:00
//On le supprime
this . promo . deleteGroupe ( creer ) ;
2022-11-16 11:14:29 +01:00
//this.bd.suprGroupe(creer);
2022-11-15 15:08:13 +01:00
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 ) ;
}
/ * *
* Fonction pour renomer un groupe
* @param name
* @param g
* /
public void rename ( String name , Groupe g ) {
this . bd . changeNameGroupe ( g , name ) ;
g . setName ( name ) ;
this . showGroupe ( g ) ;
}
2022-11-16 17:03:05 +01:00
public void addPromo ( int min , int max , String name , Set < Etudiant > ajout ) {
2022-11-17 13:13:43 +01:00
Chargement ch = new Chargement ( this . fenetre . getSize ( ) ) ;
2022-11-15 15:08:13 +01:00
this . promo = new AbstractGroupeFactoryNP ( name , min , max ) ;
2022-11-16 11:14:29 +01:00
this . fenetre . setVisible ( false ) ;
this . bd . saveGroupe ( this . promo . getPromotion ( ) , this . getTailleGroupe ( this . promo . getPromotion ( ) ) , ch ) ;
2022-11-16 17:03:05 +01:00
ch . addPourcent ( 20 ) ;
int pourcent = 85 / ajout . size ( ) ;
for ( Etudiant e : ajout ) {
this . addEtudiant ( this . promo . getPromotion ( ) , e ) ;
ch . addPourcent ( pourcent ) ;
}
2022-11-16 14:59:56 +01:00
ch . dispose ( ) ;
2022-11-16 11:14:29 +01:00
this . fenetre . setVisible ( true ) ;
2022-11-16 17:03:05 +01:00
this . showGroupe ( this . promo . getPromotion ( ) ) ;
2022-11-15 15:08:13 +01:00
}
/ * *
* 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
2022-11-16 17:03:05 +01:00
if ( g ! = g . getPointPoint ( ) )
if ( this . addEtuToParent ( g . getPointPoint ( ) , e ) = = false )
return false ;
2022-11-15 15:08:13 +01:00
//On induqe a la BD de sauvegarder cette modification
this . bd . saveEtu ( e , g ) ;
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 ( ) ) {
2022-11-16 17:03:05 +01:00
if ( et . getId ( ) = = e . getId ( ) ) {
2022-11-15 15:08:13 +01:00
return true ;
}
}
2022-11-16 17:03:05 +01:00
if ( g . getPointPoint ( ) ! = g ) {
if ( this . addEtuToParent ( g . getPointPoint ( ) , e ) = = false ) {
return false ;
}
}
this . bd . saveEtu ( e , g ) ;
2022-11-15 15:08:13 +01:00
return true ;
}
/ * *
* 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 (
2022-11-16 11:14:29 +01:00
" SELECT `id`, `nom`, `min`, `max`, `value`, `id-parent` FROM `Groupe` join `TYPE` on Groupe.Type=TYPE.name ORDER BY Groupe.id ASC; " ) ;
2022-11-15 15:08:13 +01:00
ResultSet rs = pst . executeQuery ( ) ;
rs = pst . executeQuery ( ) ;
2022-11-16 11:14:29 +01:00
rs . last ( ) ;
int nbgrp = rs . getRow ( ) ;
rs = pst . executeQuery ( ) ;
2022-11-15 15:08:13 +01:00
try {
//Si il existe bien une promotion
2022-11-16 11:14:29 +01:00
if ( rs . next ( ) ) {
2022-11-15 15:08:13 +01:00
//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
2022-11-16 11:14:29 +01:00
this . init ( groupe , ( 100 / nbgrp ) , chargement ) ;
2022-11-15 15:08:13 +01:00
//On créer la Factory
agf = new AbstractGroupeFactoryNP ( groupe ) ;
} 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 ;
}
2022-11-16 11:14:29 +01:00
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 ) ;
}
}
2022-11-17 13:13:43 +01:00
public boolean deleteEtu ( Etudiant e ) {
if ( this . deleteEtutoChildren ( e , this . fenGr . getG ( ) ) ) {
this . bd . deleteEtu ( e , this . fenGr . getG ( ) ) ;
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 ;
}
2022-11-17 14:45:42 +01:00
private void initJtree ( Groupe g ) {
DefaultMutableTreeNodeGroupe mut ;
if ( g ! = null ) {
mut = new DefaultMutableTreeNodeGroupe ( g , g . getName ( ) ) ;
for ( Groupe sous : g . getSousGroupes ( ) ) {
this . treeRecursif ( sous , mut ) ;
}
} else {
mut = new DefaultMutableTreeNodeGroupe ( g , null ) ;
}
this . tree = new JTreeGroupe ( this , mut ) ;
}
private void treeRecursif ( Groupe g , DefaultMutableTreeNodeGroupe m ) {
DefaultMutableTreeNodeGroupe mut = new DefaultMutableTreeNodeGroupe ( g , g . getName ( ) ) ;
m . add ( mut ) ;
for ( Groupe sous : g . getSousGroupes ( ) ) {
this . treeRecursif ( sous , mut ) ;
}
}
private void refreshTree ( Groupe g ) {
}
2022-11-15 15:08:13 +01:00
}