Package dao.implementation.mysql.charge

Source Code of dao.implementation.mysql.charge.MySqlChargeVoitureDao

package dao.implementation.mysql.charge;


import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import modele.charge.ChargeVoiture;
import modele.charge.FichePaiementFacture;
import modele.charge.TypeCharge;
import modele.commun.Etat;
import modele.voiture.Voiture;
import dao.IChargeVoitureDao;
import dao.implementation.mysql.AccessMySql;

/**
* Class MySqlChargeVoitureDao
* @author Kasandra
*
*/
public class MySqlChargeVoitureDao implements IChargeVoitureDao {


  private static final String INSERT_CHARGEVOITURE = "insert into tchargevoiture (ReferenceChrgVoit, libelleChrgVoit, dateEmmissionChrgVoit, dateEcheanceChrgVoit, montIndicatifChrgVoit, idTypeChrgVoit, idVoit, idEtat) values (?,?, ?,?, ?,?, ?,?)";
  private static final String UPDATE_CHARGEVOITURE = "update tchargevoiture set ReferenceChrgVoit = ?, libelleChrgVoit = ?, dateEmmissionChrgVoit = ?, dateEcheanceChrgVoit = ?, montIndicatifChrgVoit = ?, idTypeChrgVoit =?, idVoit = ?, idEtat = ?  where idChrgVoit = ?";
  private static final String DELETE_CHARGEVOITURE = "delete from tchargevoiture where idChrgVoit = ?";
  private static final String GET_ALL_CHARGEVOITURE = "select idChrgVoit, ReferenceChrgVoit, libelleChrgVoit, dateEmmissionChrgVoit, dateEcheanceChrgVoit, montIndicatifChrgVoit, ttypeChargeVoiture.idTypeChrgVoit, ttypeChargeVoiture.descripTypeChrgVoit, tvoiture.idVoit, tvoiture.numIdentifAggloVoit, tetat.idEtat, tetat.descripEtat from tchargevoiture, ttypechargevoiture, tvoiture, tetat where tchargevoiture.idTypeChrgVoit = ttypechargevoiture.idTypeChrgVoit and tchargevoiture.idVoit= tvoiture.idVoit and tchargevoiture.idEtat = tetat.idEtat ";
  private static final String GET_ALL_COMPTE_USER_ACTIF  = "select idChrgVoit, ReferenceChrgVoit, libelleChrgVoit, dateEmmissionChrgVoit, dateEcheanceChrgVoit, montIndicatifChrgVoit, ttypeChargeVoiture.idTypeChrgVoit, ttypeChargeVoiture.descripTypeChrgVoit, tvoiture.idVoit, tvoiture.numIdentifAggloVoit, tetat.idEtat, tetat.descripEtat from tchargevoiture, ttypechargevoiture, tvoiture, tetat where tchargevoiture.idTypeChrgVoit = ttypechargevoiture.idTypeChrgVoit and tchargevoiture.idVoit= tvoiture.idVoit  and tchargevoiture.idEtat = tetat.idEtat and TEtat.descripEtat != 'pay�' ";
 
 
  private static final String GET_CHARGEVOITURE_BY_ID = GET_ALL_CHARGEVOITURE + " where idChrgVoit = ?";
  private static final String GET_ALL_CHARGEVOITURE_PAS_ENTIEREMENT_PAYE_A_TEMPS = "SELECT * FROM tchargevoiture a LEFT JOIN ( SELECT idChrgVoit, sum( montPaieFact ) montantPaye FROM tfichepaiementfacture GROUP BY idChrgVoit )b ON ( a.idChrgVoit = b.idChrgVoit ) WHERE montIndicatifChrgVoit > IFNULL( montantPaye, 0) and dateEcheanceChrgVoit < ?";
 
  public void delete(final ChargeVoiture chargeVoiture) {
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          try {
            smt = connection.prepareStatement(DELETE_CHARGEVOITURE);
            smt.setInt(1, chargeVoiture.getId());
            smt.executeUpdate();
          } finally {
            if (smt != null) {
              smt.close();
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
         
        }
      }
    });
  }

  public Collection<ChargeVoiture> findPasEnti�rementPay�ATemps(final Date dateR�f�rence) {
    final ArrayList<ChargeVoiture> listeChargeVoiture = new ArrayList<ChargeVoiture>();

    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          ResultSet res = null;
          try {
            smt = connection.prepareStatement(GET_ALL_CHARGEVOITURE_PAS_ENTIEREMENT_PAYE_A_TEMPS);
            smt.setDate(1, new java.sql.Date(dateR�f�rence.getTime()));
            res = smt.executeQuery();

            while (res.next()) {
              ChargeVoiture chargeVoiture = new ChargeVoiture(res.getInt(1));
              chargeVoiture.setReference(res.getString(2));
              chargeVoiture.setLibelle(res.getString(3));

              Date dateEmmissionCharge = new  Date(res.getDate(4).getTime());
              chargeVoiture.setDateEmmissionCharge(dateEmmissionCharge);
             
              Date dateEcheance = new Date(res.getDate(5).getTime());
              chargeVoiture.setDateEcheanceCharge(dateEcheance);

              chargeVoiture.setMontantIndicatif(new BigDecimal(res.getString(6)));

              TypeCharge typeCharge = new TypeCharge(res.getInt(7));
              typeCharge.setLibelle(res.getString(8));/////////////////////
              chargeVoiture.setTypeCharge(typeCharge);

              Voiture voiture = new Voiture(res.getInt(9));
              voiture.setNumeroAgglomeration(res.getInt(10));/////////////////////
              chargeVoiture.setVoiture(voiture);
             
              MySqlFichePaiementFactureDao fichePaiementFactureDao = new MySqlFichePaiementFactureDao();
              Collection<FichePaiementFacture> paiements = fichePaiementFactureDao.findByChargeVoiture(chargeVoiture);
              chargeVoiture.setPaiements(paiements);

              listeChargeVoiture.add(chargeVoiture);
            }
          } finally {
            try {
              if (res != null) {
                res.close();
              }
            } finally {
              if (smt != null) {
                smt.close();
              }
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
         
        }
      }
    });
    return listeChargeVoiture;
  }

  public ChargeVoiture find(final int id) {
    // � l'int�rieur de la fonction runWithConnection, on ne peut acc�der qu'aux variables "final".
    // n�anmoins, on a besoin de changer cette valeur.  Utiliser un tableau permet de tromper le compilateur.
    final ChargeVoiture[] result = new ChargeVoiture[1];
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          ResultSet res = null;
          try {
            smt = connection.prepareStatement(GET_CHARGEVOITURE_BY_ID);
            smt.setInt(1, id);
            res = smt.executeQuery();
            if (res.next()) {
              result[0] = new ChargeVoiture(res.getInt(1));
              ChargeVoiture chargeVoiture = result[0];
              chargeVoiture.setLibelle(res.getString(2));

              TypeCharge typeCharge = new TypeCharge(res.getInt(3));
              typeCharge.setLibelle(res.getString(4));
              chargeVoiture.setTypeCharge(typeCharge);


              Voiture voiture = new Voiture(res.getInt(5));
              voiture.setNumeroAgglomeration(Integer.parseInt(res.getString(6)));
              chargeVoiture.setVoiture(voiture);

              Etat etat = new Etat(res.getInt(7));
              etat.setDescrip(res.getString(8));
              chargeVoiture.setEtat(etat);
             
              MySqlFichePaiementFactureDao fichePaiementFactureDao = new MySqlFichePaiementFactureDao();
              chargeVoiture.setPaiements(fichePaiementFactureDao.findByChargeVoiture(chargeVoiture));
            }
          } finally {
            try {
              if (res != null) {
                res.close();
              }
            } finally {
              if (smt != null) {
                smt.close();
              }
            }
          }
        } catch (SQLException e) {
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
    });
    return result[0];
  }



  public ArrayList<ChargeVoiture> findAll() {

    final ArrayList<ChargeVoiture> listeChargeVoiture = new ArrayList<ChargeVoiture>();

    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          ResultSet res = null;
          try {
            smt = connection.prepareStatement(GET_ALL_CHARGEVOITURE);
            res = smt.executeQuery();

            while (res.next()) {
              ChargeVoiture chargeVoiture = new ChargeVoiture(res.getInt(1));
              chargeVoiture.setReference(res.getString(2));
              chargeVoiture.setLibelle(res.getString(3));

              Date dateEmmissionCharge = new  Date(res.getDate(4).getTime());
              chargeVoiture.setDateEmmissionCharge(dateEmmissionCharge);
             
             
              Date dateEcheance = new Date(res.getDate(5).getTime());
              chargeVoiture.setDateEcheanceCharge(dateEcheance);
             
             

              chargeVoiture.setMontantIndicatif(new BigDecimal(res.getString(6)));

              TypeCharge typeCharge = new TypeCharge(res.getInt(7));
              typeCharge.setLibelle(res.getString(8));/////////////////////
              chargeVoiture.setTypeCharge(typeCharge);

              Voiture voiture = new Voiture(res.getInt(9));
              voiture.setNumeroAgglomeration(res.getInt(10));/////////////////////
              chargeVoiture.setVoiture(voiture);

              Etat etat = new Etat(res.getInt(11));
              etat.setDescrip(res.getString(12));
              chargeVoiture.setEtat(etat);

              listeChargeVoiture.add(chargeVoiture);
            }
          } finally {
            try {
              if (res != null) {
                res.close();
              }
            } finally {
              if (smt != null) {
                smt.close();
              }
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
         
        }
      }
    });
    return listeChargeVoiture;
  }

  public ChargeVoiture insert(final ChargeVoiture chargeVoiture) {
    final ChargeVoiture[] result = new ChargeVoiture[1];
    result[0] = null;
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement psmt = null;
          try {

            psmt = connection.prepareStatement(INSERT_CHARGEVOITURE);

            psmt.setString(1,chargeVoiture.getReference());
            psmt.setString(2,chargeVoiture.getLibelle());
            psmt.setDate(3,new java.sql.Date(chargeVoiture.getDateEmmissionCharge().getTime()));
           
            psmt.setDate(4,new java.sql.Date(chargeVoiture.getDateEcheanceCharge().getTime()));
            psmt.setBigDecimal(5,chargeVoiture.getMontantIndicatif());


            psmt.setInt(6,chargeVoiture.getTypeCharge().getId());
            psmt.setInt(7,chargeVoiture.getVoiture().getId());
            psmt.setInt(8,chargeVoiture.getEtat().getCdeEtat());

            psmt.executeUpdate();
          }
          finally {
            if (psmt != null) {
              psmt.close();
            }
          }
          Statement smt = null;
          ResultSet res = null;
          try {
            smt = connection.createStatement();
            res = smt.executeQuery("select idChrgVoit, ReferenceChrgVoit, libelleChrgVoit, dateEmmissionChrgVoit, dateEcheanceChrgVoit, montIndicatifChrgVoit, ttypeChargeVoiture.idTypeChrgVoit, ttypeChargeVoiture.descripTypeChrgVoit, tvoiture.idVoit, tvoiture.numIdentifAggloVoit, tetat.idEtat, tetat.descripEtat from tchargevoiture, ttypechargevoiture, tvoiture, tetat where tchargevoiture.idTypeChrgVoit = ttypechargevoiture.idTypeChrgVoit and tchargevoiture.idVoit= tvoiture.idVoit and tchargevoiture.idEtat = tetat.idEtat and idChrgVoit = LAST_INSERT_ID()");
            if (res.next()) {
              result[0] = new ChargeVoiture(res.getInt(1));
              result[0].setReference(res.getString(2));
              result[0].setLibelle(res.getString(3));

              Date dateEmmission = new Date(res.getDate(4).getTime());
              result[0].setDateEmmissionCharge(dateEmmission);

              Date dateEcheance = new Date(res.getDate(5).getTime());
              result[0].setDateEcheanceCharge(dateEcheance);

              result[0].setMontantIndicatif( res.getBigDecimal(6));


              TypeCharge typeCharge = new TypeCharge(res.getInt(7));
              typeCharge.setLibelle(res.getString(8));
              result[0].setTypeCharge(typeCharge);

              Voiture voiture = new Voiture(res.getInt(9));
              voiture.setNumeroAgglomeration(res.getInt(10));
              result[0].setVoiture(voiture);

              Etat etat = new Etat(res.getInt(11));
              etat.setDescrip(res.getString(12));
              result[0].setEtat(etat);
            }
          } finally {
            try {
              if (res != null) {
                res.close();
              }
            } finally {
              if (smt != null) {
                smt.close();
              }
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
         
        }
      }
    });
    return result[0];
  }

  public ChargeVoiture update(final ChargeVoiture chargeVoiture) {
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          try {
            smt = connection.prepareStatement(UPDATE_CHARGEVOITURE);
            smt.setString(1, chargeVoiture.getReference());
            smt.setString(2, chargeVoiture.getLibelle());
            smt.setDate(3, new java.sql.Date(chargeVoiture.getDateEmmissionCharge().getTime()));
            smt.setDate(4, new java.sql.Date(chargeVoiture.getDateEcheanceCharge().getTime()));
            smt.setBigDecimal(5, chargeVoiture.getMontantIndicatif());

            smt.setInt(6, chargeVoiture.getTypeCharge().getId());
            smt.setInt(7, chargeVoiture.getVoiture().getId());
            smt.setInt(8, chargeVoiture.getEtat().getCdeEtat());

            smt.setInt(9,chargeVoiture.getId());

            smt.executeUpdate();
          }
          finally {
            if (smt != null) {
              smt.close();
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
         
        }
      }
    });
    return chargeVoiture;
  }

  @Override
  public ArrayList<ChargeVoiture> findAllActif() {
    final ArrayList<ChargeVoiture> listeChargeVoiture = new ArrayList<ChargeVoiture>();

    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          ResultSet res = null;
          try {
            smt = connection.prepareStatement(GET_ALL_COMPTE_USER_ACTIF);
            res = smt.executeQuery();

            while (res.next()) {
              ChargeVoiture chargeVoiture = new ChargeVoiture(res.getInt(1));
              chargeVoiture.setReference(res.getString(2));
              chargeVoiture.setLibelle(res.getString(3));

              Date dateEmmissionCharge = new  Date(res.getDate(4).getTime());
              chargeVoiture.setDateEmmissionCharge(dateEmmissionCharge);
             
             
              Date dateEcheance = new Date(res.getDate(5).getTime());
              chargeVoiture.setDateEcheanceCharge(dateEcheance);
             
             

              chargeVoiture.setMontantIndicatif(new BigDecimal(res.getString(6)));

              TypeCharge typeCharge = new TypeCharge(res.getInt(7));
              typeCharge.setLibelle(res.getString(8));/////////////////////
              chargeVoiture.setTypeCharge(typeCharge);

              Voiture voiture = new Voiture(res.getInt(9));
              voiture.setNumeroAgglomeration(res.getInt(10));/////////////////////
              chargeVoiture.setVoiture(voiture);

              Etat etat = new Etat(res.getInt(11));
              etat.setDescrip(res.getString(12));
              chargeVoiture.setEtat(etat);

              listeChargeVoiture.add(chargeVoiture);
            }
          } finally {
            try {
              if (res != null) {
                res.close();
              }
            } finally {
              if (smt != null) {
                smt.close();
              }
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
         
        }
      }
    });
    return listeChargeVoiture;
  }
}
TOP

Related Classes of dao.implementation.mysql.charge.MySqlChargeVoitureDao

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.