Files
FIprojetIHM2022/java/APIGroupe/src/fr/iutfbleau/projetIHM2022FI2/MNP/AbstractChangementFactoryNP.java

249 lines
9.3 KiB
Java
Raw Normal View History

2022-10-31 12:53:56 +01:00
package fr.iutfbleau.projetIHM2022FI2.MNP;
import fr.iutfbleau.projetIHM2022FI2.API.*;
2022-11-29 15:09:41 +01:00
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;
2022-10-31 12:53:56 +01:00
import java.util.*;
/**
* Usine abstraite gérant l'ensemble des changements.
*
*/
public class AbstractChangementFactoryNP implements AbstractChangementFactory {
// l'usine à groupe travaillant en tandem avec cette usine.
private AbstractGroupeFactory agf;
2022-11-29 15:09:41 +01:00
private JFrame fenetre;
2022-10-31 12:53:56 +01:00
// On utilise une table de hachage pour retrouver facilement un changement (à partir de son id).
// Si il y a beaucoup de changements c'est plus rapide que de parcourir toute une liste.
private HashMap<Integer,Changement> brain;
2022-11-29 15:09:41 +01:00
public AbstractChangementFactoryNP(AbstractGroupeFactory agf, JFrame fenetre){
2022-10-31 12:53:56 +01:00
Objects.requireNonNull(agf,"On ne peut pas créer une usine à changement dont l'usine à groupe parternaire est null");
this.agf=agf;
2022-11-29 15:09:41 +01:00
this.fenetre=fenetre;
this.brain=new HashMap<Integer,Changement>();
this.getChange();
2022-10-31 12:53:56 +01:00
}
2022-11-29 15:34:17 +01:00
public AbstractChangementFactoryNP(AbstractGroupeFactory agf, JFrame fenetre, Set<Changement> liste){
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>();
for(Changement ch:liste){
this.brain.put(ch.getId(), ch);
}
this.getChange();
2022-11-29 15:34:17 +01:00
}
2022-10-31 12:53:56 +01:00
/**
* permet de récupérer l'usine abstraite pour les groupes qui fonctionne en tandem avec cette usine abstraite
* @return cette usine abstraite pour les groupes
*/
public AbstractGroupeFactory getGroupeFactory(){
return this.agf;
}
/**
* permet de récupérer les changements
* @return l'ensemble de tous les changements en attente
*/
public Set<Changement> getAllChangements(){
// la méthode value() d'un hashmap retourne la collection des valeurs.
// Il faut transformer la collection en Set.
// Un constructeur de HashSet permet de faire cette opération.
2022-11-17 10:34:29 +01:00
Set<Changement> out = new HashSet<Changement>(this.brain.values());
2022-10-31 12:53:56 +01:00
return out;
}
/**
* permet de mettre en oeuvre un changement connu de l'usine abstraite.
*
* @throws java.lang.NullPointerException si un argument est null
* @throws java.lang.IllegalStateException si le changement n'a pas de sens en l'état actuel (e.g. étudiant pas dans le groupe de départ a, groupe b inconnu, groupe a inconnu, etc).
* @throws java.lang.IllegalArgumentException si inconnu de l'usine abstraite
*/
public void applyChangement(Changement c){
Objects.requireNonNull(c,"On ne peut pas appliquer un changement qui est null");
Etudiant e = c.getEtu();
Groupe a = c.getA();
Groupe b = c.getB();
if (!agf.knows(a)) throw new IllegalStateException("Le groupe de départ du changement est inconnu. Impossible à mettre en oeuvre.");
if (!agf.knows(b)) throw new IllegalStateException("Le groupe d'arrivée du changement est inconnu. Impossible à mettre en oeuvre.");
// pas encore implanté.
// if(!agf.getGroupesOfEtudiant(e).contains(a)) throw new IllegalStateException("Le groupe de départ ne contient pas l'étudiant. Impossible à mettre en oeuvre.");
agf.dropFromGroupe(a,e);
agf.addToGroupe(b,e);
// En cas de succès, on enlève le changement du cerveau
this.brain.remove(Integer.valueOf(c.getId()));
}
/**
* permet de supprimer un changement connu de l'usine abstraite.
*
* @throws java.lang.NullPointerException si un argument est null
* @throws java.lang.IllegalArgumentException si inconnu de l'usine abstraite
*/
public void deleteChangement(Changement c){
Objects.requireNonNull(c,"On ne peut pas demander la suppression d'un changement qui est null");
2022-11-29 15:34:17 +01:00
Connection cnx=this.cnx();
try{
PreparedStatement pst=cnx.prepareStatement("DELETE FROM `Changement` where `id`=?");
pst.setInt(1, c.getId());
pst.executeUpdate();
pst.close();
}catch(SQLException er){
System.out.println(er.toString());
if(this.erreurSQL()){
this.deleteChangement(c);
}else{
return;
}
}
this.close(cnx);
this.brain.remove(c.getId());
2022-10-31 12:53:56 +01:00
}
/**
* permet d'ajouter un nouveau changement.
*
* @param A groupe actuel
* @param B groupe demandé
* @param e étudiant concerné par le changement
*
* @throws java.lang.NullPointerException si un argument est null
* @throws java.lang.IllegalArgumentException si les groupes ou l'étudiant ne sont pas connus de la factory partenaire, ou e n'appartient pas à A ou A et B ne sont pas frères dans l'arbre des groupes.
*
*/
public void createChangement(Groupe A, Etudiant e, Groupe B){
Objects.requireNonNull(A,"Le groupe d'origine ne peut pas être null");
Objects.requireNonNull(B,"Le groupe d'arrivée ne peut pas être null");
Objects.requireNonNull(e,"L'étudiant ne peut pas être null");
Changement c = new ChangementNP(A,e,B);
2022-11-29 15:09:41 +01:00
this.brain.put(Integer.valueOf(c.getId()),c);
Connection cnx=this.cnx();
try{
2022-11-29 15:34:17 +01:00
PreparedStatement pst=cnx.prepareStatement("SELECT * FROM `Changement` where `idGroupeA`=? AND `idGroupeB`=? AND `idEtudiant`=?; ");
2022-11-29 15:09:41 +01:00
pst.setInt(1, A.getId());
pst.setInt(2, B.getId());
pst.setInt(3, e.getId());
2022-11-29 15:34:17 +01:00
if(!pst.executeQuery().next()){
pst.close();
pst=cnx.prepareStatement("INSERT INTO `Changement` (`idGroupeA`, `idGroupeB`, `idEtudiant`, `id`) VALUES (?, ?, ?, ?);");
pst.setInt(1, A.getId());
pst.setInt(2, B.getId());
pst.setInt(3, e.getId());
pst.setInt(4, c.getId());
pst.executeUpdate();
pst.close();
}else{
pst.close();
JOptionPane.showMessageDialog(this.fenetre, "Vous Avez deja demander a Chnager dans ce Groupe", "erreur", JOptionPane.ERROR_MESSAGE);
}
2022-11-29 15:09:41 +01:00
}catch(SQLException er){
2022-11-29 15:34:17 +01:00
System.out.println(er.toString());
2022-11-29 15:09:41 +01:00
if(this.erreurSQL()){
this.createChangement(A, e, B);
}else{
return;
}
}
this.close(cnx);
}
// **********************
// 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;
}
2022-10-31 12:53:56 +01:00
}
2022-11-29 15:09:41 +01:00
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 close(AutoCloseable clos){
try{
clos.close();
}catch(Exception e){
if(this.erreurCO()==true)
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);
}
}
2022-10-31 12:53:56 +01:00
}