Package factories

Source Code of factories.FClients

package factories;

import interfaces.exceptions.MetierException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import base.Client;
import factories.exceptions.FactoriesException;


/**
*
* @author brahim
*
*/
public class FClients {

  private List cacheClients;

  /* Les requêtes SQL qui seront executées */
  private String selectClient = " select * from client where idClient = ? "
  private String selectClients = " select idClient from client ";
  private String selectCommandesClient = " select * from commande where idClient = ? ";
  private String insererClient = " insert into client(idClient, nom, prenom, adresse, mdp) "+
    " values(?,?,?,?,?) ";
  private String updateAdresse = "UPDATE CLIENT SET " +
      "ADRESSE = ? WHERE IDCLIENT = ?";
 
 
  /* Préparation des requêtes */
  private PreparedStatement pSelectClient,pSelectCommandesClient,
    pSelectClients,pInsererClient, pUpdateAdresse;// pSelectLignesCommandes;

 
 
  /**
   * Constructeur privé : Pattern Singleton
   *
   */
  private FClients() {
    cacheClients = new ArrayList<Client>();
  }
 
  /*
   * Connexion JDBC
   */
  private Connection c;

  /*
   * Traitement du singleton de la fabrique
   */
  private static FClients instance;


  /**
   * Recuperation du singleton de la fabrique
   *
   * @return un singleton de la fabrique
   */
  public static FClients getInstance() {
    if (instance == null)
      instance = new FClients();
    return instance;
  }
 
  /**
   * Preparation des statements
   *
   */
  private void init() throws SQLException {
    //Cr�ation des statements
    pSelectClient = c.prepareStatement(selectClient);
    pSelectCommandesClient = c.prepareStatement(selectCommandesClient);
    pSelectClients = c.prepareStatement(selectClients);
    pInsererClient = c.prepareStatement(insererClient);
    pUpdateAdresse = c.prepareStatement(updateAdresse);
    //pSelectLignesCommandes = c.prepareStatement(selectLignesCommandes);
  }

  /**
   * Mise en place d'une connexion JDBC au singleton
   *
   * @param c
   * @throws SQLException
   */
  public void setConnection(Connection c) throws SQLException {
    this.c = c;
    this.init();
  }
 
 
  /**
   * Recherche d'un client par son identifiant
   * @param idClient Identifiant du client a rechercher
   * @return les informations client restituees de la base
   * @throws SQLException Lors d'un probleme de connexion a la base de donnees
   * @throws FactoriesException
   */
  public Client rechercherClient(String idClient)
    throws SQLException, FactoriesException{
    /*if(!this.clientExists(idClient)){     
      System.out.println("Arret premier niveau");
      throw new FactoriesException(MetierException.CLIENT_EXISTE_PAS);
    }*/
     
   
   
    pSelectClient.clearParameters();
    pSelectClient.setString(1,idClient);
   
    ResultSet rsClient = pSelectClient.executeQuery();
   
    Client c = null;
   
    if(rsClient.next()){
     
      String nom = rsClient.getString(2);
      String prenom = rsClient.getString(3);
      String adresse = rsClient.getString(4);
      String mdp = rsClient.getString(5);
      c = new Client(idClient,nom,prenom,adresse,mdp);   
     
      pSelectCommandesClient.clearParameters();
      pSelectCommandesClient.setString(1,c.getId());
      ResultSet rsCommandesClient = pSelectCommandesClient.executeQuery();
      while(rsCommandesClient.next()){
        String idCommande = rsCommandesClient.getString(1);       
        c.ajouterCommande(idCommande);       
      }
     
     
    }else{
      // on ne retourne pas null
      throw new FactoriesException(MetierException.CLIENT_EXISTE_PAS);
     
    }
     
    return c;
  }
 
 
  /**
   * Création d'un nouveau client dans la base de donnée
   * TODO: verification si un client existe deja (son IDENTIFIANT)
   * @param c
   * @return
   * @throws SQLException
   */
  public synchronized int creerClient(Client c) throws SQLException, FactoriesException{
   
    if(this.clientExists(c.getId())){
      throw new FactoriesException(MetierException.CLIENT_EXISTE_DEJA);
    }
   
    pInsererClient.clearParameters();   
   
    pInsererClient.setString(1, c.getId());
    pInsererClient.setString(2, c.getNom());
    pInsererClient.setString(3, c.getPrenom());
    pInsererClient.setString(4, c.getAdresse());
    pInsererClient.setString(5, c.getMdp());
   
    int res = pInsererClient.executeUpdate();
   
    return res;
  }
 
  /**
   * Renvoie la liste des clients existant (Renvoie seulement les identifiants)
   *
   * @return
   * @throws SQLException
   */
  public List<String> getListeClients() throws SQLException{
    pSelectClients.clearParameters();
    ResultSet rsClients = pSelectClients.executeQuery();
    List<String> listeIdClients = new ArrayList<String>();
    while(rsClients.next()){
      listeIdClients.add(rsClients.getString(1));
    }
    return new ArrayList<String>();
  }

  /**
   * Verifier si la connexion a la base de donnees existe
   * @return Confirmation d'existence ou pas d'une connexion.
   */
  public boolean hasConnection(){
    return (this.c != null);
  }
 
 
 
  /**
   * Mise a jour de l'adresse d'un client
   * @param clt Client dont l'adresse on souhaite mettre a jour
   * dans la base de donnees.
   * @return resultat de la requete
   * @throws SQLException Lors d'un probleme de connexion a la base de donnees.
   * @throws FactoriesException Erreur lorsque le client n'existe pas
   * dans la base de donnees.
   */
  public synchronized int updateAdresse(Client clt)
    throws SQLException, FactoriesException{
    // verification rapide de l'existance du client
    /*if(!this.clientExists(clt.getId()))
      throw new FactoriesException(MetierException.CLIENT_EXISTE_PAS);*/
   
    pUpdateAdresse.clearParameters();
   
    pUpdateAdresse.setString(1, clt.getAdresse());
    pUpdateAdresse.setString(2, clt.getId());
   
    return pUpdateAdresse.executeUpdate();
  }
 
 
  /**
   * Verification de l'existence d'un client d'apres son identifiant.
   * @param id Identifiant du client
   * @return vrai si le client existe dans la base, faux sinon.
   * @throws SQLException Lors d'un probleme de connexion a la base de donnees.
   */
  private boolean clientExists(String id) throws SQLException{
    // verification de l'existence d'un client du meme identifiant
    List<String> clients = this.getListeClients();
    System.out.println("Affichage de la liste des clients : ");
    Iterator it = clients.iterator();
    while(it.hasNext()){
      String idClient = (String)it.next();
      System.out.println("ID => "+idClient);
    }
    return clients.contains(id);
  }
 
 
  public ArrayList<String> getCommandesClient(String id) throws SQLException{
    ArrayList<String> refsCdes = new ArrayList<String>();
   
    pSelectCommandesClient.clearParameters();
    pSelectCommandesClient.setString(1,id);
    ResultSet rsCommandesClient = pSelectCommandesClient.executeQuery();
    while(rsCommandesClient.next()){
      String idCommande = rsCommandesClient.getString(1);       
      refsCdes.add(idCommande);       
    }
   
    return refsCdes;
  }
 
 
 
 

}
TOP

Related Classes of factories.FClients

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.