Ajout Modele Non persisitant

This commit is contained in:
2022-12-03 19:38:26 +01:00
parent 8dd7db7396
commit e628cdfa6f
63 changed files with 5214 additions and 906 deletions

View File

@@ -1,13 +1,5 @@
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 changements.
@@ -18,28 +10,15 @@ public class AbstractChangementFactoryNP implements AbstractChangementFactory {
// l'usine à groupe travaillant en tandem avec cette usine.
private AbstractGroupeFactory agf;
private JFrame fenetre;
// 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;
public AbstractChangementFactoryNP(AbstractGroupeFactory agf, JFrame fenetre){
public AbstractChangementFactoryNP(AbstractGroupeFactory agf){
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.getChange();
}
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();
this.brain=new HashMap<Integer,Changement>();
}
@@ -56,11 +35,10 @@ public class AbstractChangementFactoryNP implements AbstractChangementFactory {
* @return l'ensemble de tous les changements en attente
*/
public Set<Changement> getAllChangements(){
this.refresh();
// 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.
Set<Changement> out = new HashSet<Changement>(this.brain.values());
Set<Changement> out = new HashSet(this.brain.values());
return out;
}
@@ -86,12 +64,7 @@ public class AbstractChangementFactoryNP implements AbstractChangementFactory {
agf.dropFromGroupe(a,e);
agf.addToGroupe(b,e);
// En cas de succès, on enlève le changement du cerveau
this.deleteChangement(c);
for(Changement ch: this.getAllChangements()){
if(ch.getB().getSize()+1==ch.getB().getMax()){
this.deleteChangement(ch);
}
}
this.brain.remove(Integer.valueOf(c.getId()));
}
@@ -103,22 +76,9 @@ public class AbstractChangementFactoryNP implements AbstractChangementFactory {
*/
public void deleteChangement(Changement c){
Objects.requireNonNull(c,"On ne peut pas demander la suppression d'un changement qui est null");
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());
this.brain.remove(Integer.valueOf(c.getId()));
}
/**
@@ -136,177 +96,21 @@ public class AbstractChangementFactoryNP implements AbstractChangementFactory {
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);
this.brain.put(Integer.valueOf(c.getId()),c);
Connection cnx=this.cnx();
try{
PreparedStatement pst=cnx.prepareStatement("SELECT * FROM `Changement` where `idGroupeA`=? AND `idGroupeB`=? AND `idEtudiant`=?; ");
pst.setInt(1, A.getId());
pst.setInt(2, B.getId());
pst.setInt(3, e.getId());
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);
}
}catch(SQLException er){
System.out.println(er.toString());
if(this.erreurSQL()){
this.createChangement(A, e, B);
}else{
return;
}
}
this.close(cnx);
this.brain.put(Integer.valueOf(c.getId()),c);
}
@Override
/**
* permet d'ajouter un nouveau changement. de type 2
*
* @param A groupe actuel
* @param B groupe demandé
* @param e étudiant concerné par le changement
* @param raison la raison du changement de type 2
* @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, String raison){
public void createChangement(Groupe A, Etudiant e, Groupe B, String raison) {
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");
Objects.requireNonNull(raison,"La raison ne peut pas être null");
Changement c = new ChangementNP(A,e,B, raison);
this.brain.put(Integer.valueOf(c.getId()),c);
Connection cnx=this.cnx();
try{
PreparedStatement pst=cnx.prepareStatement("SELECT * FROM `Changement` where `idGroupeA`=? AND `idGroupeB`=? AND `idEtudiant`=?; ");
pst.setInt(1, A.getId());
pst.setInt(2, B.getId());
pst.setInt(3, e.getId());
if(!pst.executeQuery().next()){
pst.close();
pst=cnx.prepareStatement("INSERT INTO `Changement` (`id`, `idGroupeA`, `idGroupeB`, `idEtudiant`, `Raison`) VALUES (?, ?, ?, ?, ?);");
pst.setInt(1, c.getId());
pst.setInt(2, A.getId());
pst.setInt(3, B.getId());
pst.setInt(4, e.getId());
pst.setString(5, raison);
pst.executeUpdate();
pst.close();
}else{
pst.close();
JOptionPane.showMessageDialog(this.fenetre, "Vous Avez deja demander a Chnager dans ce Groupe", "erreur", JOptionPane.ERROR_MESSAGE);
}
}catch(SQLException er){
System.out.println(er.toString());
if(this.erreurSQL()){
this.createChangement(A, e, B, raison);
}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;
}
}
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(6), rs.getString(7), rs.getInt(4));
if(rs.getString(5)!=null){
this.brain.put(rs.getInt(1), new ChangementNP(ab[0], e, ab[1], rs.getInt(1), rs.getString(5)));
}else{
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);
}
}
private void refresh(){
this.brain=new HashMap<Integer,Changement>();
this.getChange();
}
}