package dao.implementation.mysql.alerte;
import modele.alerte.AlerteVoiture;
import modele.alerte.AlerteVoiture.TypeAlerte;
import modele.chauffeur.Pays;
import modele.chauffeur.Ville;
import modele.voiture.Voiture;
import dao.GestionnaireDeStockage;
import dao.IDao;
import dao.implementation.mysql.AccessMySql;
import gui.MainWindow;
import gui.util.TaxiGuiUtil;
import gui.voiture.gestionvoiture.VoitureAffichable;
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.Date;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
/**
* class MySqlAlerteVoitureDao
* @author Kasandra
*
*/
public class MySqlAlerteVoitureDao implements IDao<AlerteVoiture> {
private static final String INSERT_ALERTE_VOITURE = "insert into talertevoiture (idVoit,typeEntret, dateAlerte) values (?,?, ?)";
private static final String GET_ALERTE_VOITURE_BY_ID = "select id, idVoit, typeEntret, dateAlerte from talertevoiture where id = ?";
private static final String GET_ALL_ALERTEVOITURE = "select id, idVoit, typeEntret, dateAlerte from Talertevoiture";
private static final String DELETE_ALERTEVOITURE = "delete from talertevoiture where id = ?";
/**
* Efface l'alerte voiture
*/
public void delete(final AlerteVoiture alerteVoiture) {
AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
public void runWithconnection(Connection connection) {
try {
PreparedStatement smt = null;
try {
smt = connection.prepareStatement(DELETE_ALERTEVOITURE);
smt.setInt(1, alerteVoiture.getId());
smt.executeUpdate();
} finally {
if (smt != null) {
smt.close();
}
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
});
}
public AlerteVoiture find(final int id) {
final AlerteVoiture[] result = new AlerteVoiture[1];
final IDao<Voiture> voitureDao = getVoitureDao();
AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
public void runWithconnection(Connection connection) {
try {
PreparedStatement smt = null;
ResultSet res = null;
try {
smt = connection.prepareStatement(GET_ALERTE_VOITURE_BY_ID);
smt.setInt(1, id);
res = smt.executeQuery();
if (res.next()) {
int idAlerte = res.getInt(1);
Voiture voiture = voitureDao.find(res.getInt(2));
TypeAlerte typeAlerte = TypeAlerte.valueOf(res.getString(3));
Date dateAlerte = new java.sql.Timestamp(res.getTimestamp(4).getTime());
//Date dateAlerte = new Date(res.getDate(4).getTime());
result[0] = new AlerteVoiture(idAlerte,voiture,typeAlerte, dateAlerte);
}
}
finally {
try {
if (res != null) {
res.close();
}
}
finally {
if (smt != null) {
smt.close();
}
}
}
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
return result[0];
}
public ArrayList<AlerteVoiture> findAll() {
final ArrayList<AlerteVoiture> alertesVoiture = new ArrayList<AlerteVoiture>();
final IDao<Voiture> voitureDao = getVoitureDao();
AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
public void runWithconnection(Connection connection) {
try {
PreparedStatement psmt = null;
ResultSet res = null;
try {
psmt = connection.prepareStatement(GET_ALL_ALERTEVOITURE);
res = psmt.executeQuery();
while (res.next()) {
int idAlerte = res.getInt(1);
Voiture voiture = voitureDao.find(res.getInt(2));
TypeAlerte typeAlerte = TypeAlerte.valueOf(res.getString(3));
Date dateAlerte = new java.sql.Timestamp(res.getTimestamp(4).getTime());
//Date dateAlerte = new Date(res.getDate(4).getTime());
alertesVoiture.add(new AlerteVoiture(idAlerte,voiture,typeAlerte, dateAlerte));
}
}
finally {
try {
if (res != null) {
res.close();
}
}
finally {
if (psmt != null) {
psmt.close();
}
}
}
} catch(SQLException e) {
throw new RuntimeException(e);
}
}
});
return alertesVoiture;
}
public AlerteVoiture insert(final AlerteVoiture alerteVoiture) {
final AlerteVoiture[] result = new AlerteVoiture[1];
result[0] = null;
AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
public void runWithconnection(Connection connection) {
try {
PreparedStatement psmt = null;
try {
psmt = connection.prepareStatement(INSERT_ALERTE_VOITURE);
psmt.setInt(1,alerteVoiture.getVoiture().getId());
psmt.setString(2,alerteVoiture.getTypeAlerte().name());
psmt.setTimestamp(3, new java.sql.Timestamp(alerteVoiture.getDateAlerte().getTime()));
//psmt.setDate(3, new java.sql.Date(alerteVoiture.getDateAlerte().getTime()));
psmt.executeUpdate();
}
finally {
if (psmt != null) {
psmt.close();
}
}
Statement smt = null;
ResultSet res = null;
try {
smt = connection.createStatement();
res = smt.executeQuery("SELECT LAST_INSERT_ID()");
if (res.next()) {
int id = res.getInt(1);
result[0] = find(id);
}
}
finally {
try {
if (res != null) {
res.close();
}
}
finally {
if (smt != null) {
smt.close();
}
}
}
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
return result[0];
}
public AlerteVoiture update(AlerteVoiture obj) {
// TODO Auto-generated method stub
return null;
}
/**
* retourne
* @return IDao<Voiture>
*/
private IDao<Voiture> getVoitureDao() {
try {
return GestionnaireDeStockage.getInstance().getVoitureDao();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}