package es.madrid.redmetro.rmr.service; import es.madrid.redmetro.rmr.dao.ICochera; import es.madrid.redmetro.rmr.dao.impl.CocheraJDBC; import es.madrid.redmetro.rmr.exception.RedMetroException; import es.madrid.redmetro.rmr.model.Cochera; public class CocheraService { private ICochera cocheraDAO = new CocheraJDBC(); // en caso de que haya relación de fk: el service necesita también el dao de tren // private ITren trenDAO = new TrenJDBC(); public void insertarCochera(Cochera cochera) throws RedMetroException { Cochera cocheraEnBD = cocheraDAO.obtenerCocheraPorCodigo(cochera.getCodigoCochera()); if (cocheraEnBD == null) { cocheraDAO.insertarCochera(cochera); System.out.println("Cochera " + cochera.getCodigoCochera() + " insertada c orrectamente."); } else { System.out.println("Cochera " + cochera.getCodigoCochera() + " ya existe."); } } public void actualizarCochera(Cochera cochera) throws RedMetroException { cocheraDAO.actualizarCochera(cochera); System.out.println("Cocchera " + cochera.getCodigoCochera() + " actualizada"); } public void borrarCochera (int codigoCochera) throws RedMetroException { cocheraDAO.borrarCochera(codigoCochera); System.out.println("Cochera " + codigoCochera + " borrada."); } // en caso de haber relación de fk: borra los trenesy la cochera en una transacción // public void borrarCocheraYSusTrenes(int codigoCochera) throws RedMetroException { // Connection conexion = null; // try { // conexion = GestorConexionBD.getConexion(); // conexion.setAutoCommit(false); // trenDAO.borrarTrenesDeCochera(conexion, codigoCochera); // cocheraDAO.borrarCochera(conexion, codigoCochera); // conexion.commit(); // System.out.println("Cochera " + codigoCochera + " y sus trenes borrados."); // } catch (SQLException | RedMetroException e) { // try { // if (conexion != null) conexion.rollback(); // } catch (SQLException errorRollback) { // System.out.println("Error al deshacer: " + errorRollback.getMessage()); // } // throw new RedMetroException("Error al borrar la cochera " + codigoCochera, e); // } finally { // try { // if (conexion != null) conexion.close(); // } catch (SQLException errorCierre) { // System.out.println("Error al cerrar: " + errorCierre.getMessage()); // } // } // } }