Package dao.implementation.mysql.charge

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

package dao.implementation.mysql.charge;


import modele.charge.ChargeVoiture;
import modele.charge.FichePaiementFacture;
import dao.IDao;
import dao.implementation.mysql.AccessMySql;
import gui.MainWindow;
import gui.util.TaxiGuiUtil;

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

/**
* Class MySqlFichePaiementFactureDao
* @author Kasandra
*
*/
public class MySqlFichePaiementFactureDao implements IDao<FichePaiementFacture> {

  private static final String INSERT_FICHEPAIE_FACTURE= "insert into TFichePaiementFacture ( datePaieFact, libellePaieFact, montPaieFact, idChrgVoit) values (?,?,?,?)";
  private static final String UPDATE_FICHEPAIE_FACTURE  = "update TFichePaiementFacture set datePaieFact = ? , libellePaieFact = ? , montPaieFact = ? , idChrgVoit =  ? where idPaieFact = ?";
  private static final String DELETE_FICHEPAIE_FACTURE  = "delete from TFichePaiementFacture where idPaieFact = ?";
  private static final String GET_ALL_FICHEPAIE_FACTURE  = "select idPaieFact, datePaieFact, libellePaieFact, montPaieFact, tchargevoiture.idChrgVoit,  tchargevoiture.libelleChrgVoit from TFichePaiementFacture, tchargeVoiture where TFichePaiementFacture.idChrgVoit = tchargevoiture.idChrgVoit";
  private static final String GET_ALL_FICHEPAIE_FACTURE_BY_CHARGE = "select idPaieFact, datePaieFact, libellePaieFact, montPaieFact from TFichePaiementFacture where idChrgVoit =  ?";
  private static final String GET_FICHEPAIE_FACTURE_BY_ID = GET_ALL_FICHEPAIE_FACTURE  + " and idPaieFact = ?";

  /**
   * Efface une facture
   */
  public void delete(final FichePaiementFacture fichePaiementFacture) {
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          try {
            smt = connection.prepareStatement(DELETE_FICHEPAIE_FACTURE);
            smt.setInt(1, fichePaiementFacture.getId());
            smt.executeUpdate();
          } finally {
            if (smt != null) {
              smt.close();
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
         
        }
      }
    });
  }

  /**
   * Cherche une facture
   */
  public FichePaiementFacture find(final int id) {
    final FichePaiementFacture[] result = new FichePaiementFacture[1];
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          ResultSet res = null;
          try {
            smt = connection.prepareStatement(GET_FICHEPAIE_FACTURE_BY_ID);
            smt.setInt(1, id);
            res = smt.executeQuery();
            if (res.next()) {
              result[0] = new FichePaiementFacture(res.getInt(1));

              Date datePaieFacture = new Date(res.getDate(2).getTime());
              result[0].setDatePaieFacture(datePaieFacture);

              result[0].setLibellePaieFacture(res.getString(3));
              result[0].setMontantPaieFacture(res.getBigDecimal(4));

              ChargeVoiture chargeVoiture = new ChargeVoiture(res.getInt(5));
              chargeVoiture.setLibelle(res.getString(6));
              result[0].setChargeVoiture(chargeVoiture);
            }
          } finally {
            try {
              if (res != null) {
                res.close();
              }
            } finally {
              if (smt != null) {
                smt.close();
              }
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
        }
      }
    });
    return result[0];
  }

  /**
   * Cherche toutes les factures pay�es et l'affiche dans la liste
   */
  public ArrayList<FichePaiementFacture> findAll() {
    final ArrayList<FichePaiementFacture> listeFichePaiementFacture = new ArrayList<FichePaiementFacture>();
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          ResultSet res = null;
          try {
            smt = connection.prepareStatement(GET_ALL_FICHEPAIE_FACTURE);
            res = smt.executeQuery();
            while (res.next()) {
              FichePaiementFacture fichePaiementFacture = new FichePaiementFacture(res.getInt(1));

              Date datePaieFacture = new Date(res.getDate(2).getTime());
              fichePaiementFacture.setDatePaieFacture(datePaieFacture);

              fichePaiementFacture.setLibellePaieFacture(res.getString(3));
              fichePaiementFacture.setMontantPaieFacture(res.getBigDecimal(4));


              ChargeVoiture chargeVoiture = new ChargeVoiture(res.getInt(5));
              chargeVoiture.setLibelle(res.getString(6));
              fichePaiementFacture.setChargeVoiture(chargeVoiture);

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

  /**
   * Insert une facture pay�e
   */
  public FichePaiementFacture insert(final FichePaiementFacture fichePaiementFacture) {
    final FichePaiementFacture[] result = new FichePaiementFacture[1];
    result[0] = null;
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement psmt = null;
          try {

            psmt = connection.prepareStatement(INSERT_FICHEPAIE_FACTURE);

            psmt.setDate(1,new java.sql.Date(fichePaiementFacture.getDatePaieFacture().getTime()));
            psmt.setString(2,fichePaiementFacture.getLibellePaieFcature());
            psmt.setBigDecimal(3,fichePaiementFacture.getMontantPaieFacture());
            psmt.setInt(4,fichePaiementFacture.getChargeVoiture().getId());


            psmt.executeUpdate();
          } finally {
            if (psmt != null) {
              psmt.close();
            }
          }
          Statement smt = null;
          ResultSet res = null;
          try {
            smt = connection.createStatement();
            res = smt.executeQuery("select idPaieFact, datePaieFact, libellePaieFact, montPaieFact, tchargevoiture.idChrgVoit, tchargevoiture.libelleChrgVoit from TFichePaiementFacture, tchargeVoiture where TFichePaiementFacture.idChrgVoit = tchargevoiture.idChrgVoit and idPaieFact = LAST_INSERT_ID()");
            if (res.next()) {
              result[0] = new FichePaiementFacture(res.getInt(1));

              Date datePaieFact = new Date(res.getDate(2).getTime());
              fichePaiementFacture.setDatePaieFacture(datePaieFact);

              result[0].setLibellePaieFacture(res.getString(3));
              result[0].setMontantPaieFacture(res.getBigDecimal(4));

              ChargeVoiture chargeVoiture = new ChargeVoiture(res.getInt(5));
              chargeVoiture.setLibelle(res.getString(6));
              result[0].setChargeVoiture(chargeVoiture);
            }
          } finally {
            try {
              if (res != null) {
                res.close();
              }
            } finally {
              if (smt != null) {
                smt.close();
              }
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
       
        }
      }
    });
    return result[0];
  }

  /**
   * Mise � jour fiche paie facture
   */
  public FichePaiementFacture update(final FichePaiementFacture fichePaiementFacture) {
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          try {
            smt = connection.prepareStatement(UPDATE_FICHEPAIE_FACTURE);

            smt.setDate(1, new java.sql.Date(fichePaiementFacture.getDatePaieFacture().getTime()));

            smt.setString(2, fichePaiementFacture.getLibellePaieFcature());
            smt.setBigDecimal(3, fichePaiementFacture.getMontantPaieFacture());

            smt.setInt(4, fichePaiementFacture.getChargeVoiture().getId());
            smt.setInt(5, fichePaiementFacture.getId());
            smt.executeUpdate();
          }
          finally {
            if (smt != null) {
              smt.close();
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
         
        }
      }
    });
    return fichePaiementFacture;
  }

  public Collection<FichePaiementFacture> findByChargeVoiture(final ChargeVoiture chargeVoiture) {
    final ArrayList<FichePaiementFacture> listeFichePaiementFacture = new ArrayList<FichePaiementFacture>();
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          ResultSet res = null;
          try {
            smt = connection.prepareStatement(GET_ALL_FICHEPAIE_FACTURE_BY_CHARGE);
            smt.setInt(1, chargeVoiture.getId());
            res = smt.executeQuery();
            while (res.next()) {
              FichePaiementFacture fichePaiementFacture = new FichePaiementFacture(res.getInt(1));

              java.util.Date datePaieFacture = new java.util.Date(res.getDate(2).getTime());
              fichePaiementFacture.setDatePaieFacture(datePaieFacture);

              fichePaiementFacture.setLibellePaieFacture(res.getString(3));
              fichePaiementFacture.setMontantPaieFacture(res.getBigDecimal(4));

              fichePaiementFacture.setChargeVoiture(chargeVoiture);

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

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

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.