Ultimas piezas de documentacion en los modelos, ademas toquetee un poco el codigo de actualizacion de version al ver que no era tan robusto como me gustaria, ahora me agrada mas

This commit is contained in:
Daniel Cortés
2019-03-21 23:28:21 -03:00
parent ad5c050991
commit ea4aadf31b
5 changed files with 96 additions and 42 deletions

BIN
dist/Programa Caja.jar vendored

Binary file not shown.

View File

@@ -91,7 +91,6 @@ public class DAOManager {
return egresoDAO; return egresoDAO;
} }
public static IngresoDAO getIngresoDAO() { public static IngresoDAO getIngresoDAO() {
return ingresoDAO; return ingresoDAO;
} }

View File

@@ -33,16 +33,30 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Objeto DAO que realiza las querys y mapeos necesarios del objeto TipoIngreso. En especifico esta
* implementacion se comunica con la base de datos SQLite
*/
public class SQLiteTipoIngresoDAO implements TipoIngresoDAO { public class SQLiteTipoIngresoDAO implements TipoIngresoDAO {
private static final Logger log = LogManager.getLogger(SQLiteTipoIngresoDAO.class);
private ConnectionHolder connectionHolder; private ConnectionHolder connectionHolder;
public SQLiteTipoIngresoDAO() { public SQLiteTipoIngresoDAO() {
this.connectionHolder = new SQLiteConnectionHolder(); this.connectionHolder = new SQLiteConnectionHolder();
} }
/**
* Obtiene una lista todos los TipoIngreso existentes
*
* @return Una lista de TipoIngreso
*/
@Override @Override
public List<TipoIngreso> getAll() { public List<TipoIngreso> getAll() {
log.debug("Se intentara conseguir todos los TipoIngreso");
List<TipoIngreso> tiposIngresoList = new ArrayList<>(); List<TipoIngreso> tiposIngresoList = new ArrayList<>();
String query = "select * from tipos_ingreso"; String query = "select * from tipos_ingreso";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
@@ -57,13 +71,22 @@ public class SQLiteTipoIngresoDAO implements TipoIngresoDAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); log.error("Error al intentar conseguir todos los TipoIngreso", e);
} }
log.debug("Se consiguieron " + tiposIngresoList.size() + " TiposIngreso");
return tiposIngresoList; return tiposIngresoList;
} }
/**
* Obtiene un TipoIngreso por su id
*
* @param id Id unico del TipoIngreso
* @return Un optional que puede contener TipoIngreso o puede estar vacio, dado que no es seguro
* que exista un TipoIngreso con el id indicado
*/
@Override @Override
public Optional<TipoIngreso> getById(int id) { public Optional<TipoIngreso> getById(int id) {
log.debug("Se intentara conseguir un TipoIngreso con id " + id);
TipoIngreso tipoIngreso = null; TipoIngreso tipoIngreso = null;
String query = "select * from tipos_ingreso where id = ?"; String query = "select * from tipos_ingreso where id = ?";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
@@ -78,13 +101,22 @@ public class SQLiteTipoIngresoDAO implements TipoIngresoDAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); log.error("Error al intentar conseguir un TipoIngreso con id " + id);
} }
log.debug("Se consiguio el TipoIngreso " + tipoIngreso);
return Optional.ofNullable(tipoIngreso); return Optional.ofNullable(tipoIngreso);
} }
/**
* Obtiene un TipoIngreso dado su nombre
*
* @param nombre Nombre del TipoIngreso
* @return Un optional que puede contenter un TipoIngreso o puede estar vacio, ya que no es seguro
* que exista un TipoIngreso para dado nombre
*/
@Override @Override
public Optional<TipoIngreso> getByNombre(String nombre) { public Optional<TipoIngreso> getByNombre(String nombre) {
log.debug("Se intentara conseguir un TipoIngreso con nombre " + nombre);
TipoIngreso tipoIngreso = null; TipoIngreso tipoIngreso = null;
String query = "select * from tipos_ingreso where nombre = ?"; String query = "select * from tipos_ingreso where nombre = ?";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
@@ -99,8 +131,9 @@ public class SQLiteTipoIngresoDAO implements TipoIngresoDAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); log.error("Error al intentar conseguir un TipoIngreso con nombre " + nombre);
} }
log.debug("Se consiguio el TipoIngreso " + tipoIngreso);
return Optional.ofNullable(tipoIngreso); return Optional.ofNullable(tipoIngreso);
} }

View File

@@ -1,5 +1,6 @@
package danielcortes.xyz.models.version; package danielcortes.xyz.models.version;
import danielcortes.xyz.data.ConnectionHolder;
import danielcortes.xyz.data.SQLiteConnectionHolder; import danielcortes.xyz.data.SQLiteConnectionHolder;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@@ -12,10 +13,16 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class SQLiteVersionDAO extends VersionDAO { public class SQLiteVersionDAO implements VersionDAO {
private static final Logger log = LogManager.getLogger(SQLiteVersionDAO.class);
private ConnectionHolder connectionHolder;
public SQLiteVersionDAO() { public SQLiteVersionDAO() {
this.connectionHolder = new SQLiteConnectionHolder(); this.connectionHolder = new SQLiteConnectionHolder();
@@ -24,7 +31,7 @@ public class SQLiteVersionDAO extends VersionDAO {
private boolean tableVersionsExists() { private boolean tableVersionsExists() {
try { try {
DatabaseMetaData md = this.connectionHolder.getConnection().getMetaData(); DatabaseMetaData md = this.connectionHolder.getConnection().getMetaData();
try(ResultSet rs = md.getTables(null, null, "version", null)) { try (ResultSet rs = md.getTables(null, null, "version", null)) {
return rs.next(); return rs.next();
} }
} catch (SQLException e) { } catch (SQLException e) {
@@ -33,7 +40,7 @@ public class SQLiteVersionDAO extends VersionDAO {
} }
} }
private int getCurrentVersion() { private int getCurrentVersion() throws SQLException{
if (tableVersionsExists()) { if (tableVersionsExists()) {
String query = "SELECT version FROM version LIMIT 1"; String query = "SELECT version FROM version LIMIT 1";
try (Connection conn = this.connectionHolder.getConnection()) { try (Connection conn = this.connectionHolder.getConnection()) {
@@ -44,52 +51,72 @@ public class SQLiteVersionDAO extends VersionDAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
log.error("Error al obtener la version desde la base de datos, se enviara una exepcion");
throw e;
} }
}else{
return 0;
} }
return 0;
} }
private InputStream getVersionScript(int version) { private InputStream unzipVersionScript(int version) throws IOException {
try { ZipFile zipFile = new ZipFile(new File("data/version_scripts"));
ZipFile zipFile = new ZipFile(new File("data/version_scripts")); ZipEntry zipEntry = zipFile.getEntry(version + ".sql");
ZipEntry zipEntry = zipFile.getEntry(version + ".sql"); return zipFile.getInputStream(zipEntry);
return zipFile.getInputStream(zipEntry); }
} catch (IOException e) {
e.printStackTrace(); private ArrayList<String> getVersionScript(int version) throws IOException {
} ArrayList<String> script = new ArrayList<>();
return new InputStream() { try (BufferedReader reader = new BufferedReader(
@Override new InputStreamReader(unzipVersionScript(version)))) {
public int read() { String line;
return 0; while ((line = reader.readLine()) != null) {
if(line.trim().length() > 0) {
script.add(line);
}
} }
}; }
return script;
} }
private void executeVersionScript(int version) { private void executeVersionScript(int version) {
try (Connection conn = this.connectionHolder.getConnection()) { try (Connection conn = this.connectionHolder.getConnection()) {
try(Statement statement = conn.createStatement()) { conn.setAutoCommit(false);
InputStream inputStream = getVersionScript(version); try (Statement statement = conn.createStatement()) {
try(BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { try{
String line; ArrayList<String> script = getVersionScript(version);
while ((line = reader.readLine()) != null) { for (String query : script) {
statement.addBatch(line); log.debug("Se agrega query '" + query + "' size " + query.length());
statement.addBatch(query);
} }
statement.executeBatch();
} catch (IOException e) {
log.error("Error al leer al obtener el script", e);
} }
statement.executeBatch(); conn.commit();
} catch (SQLException e) {
log.error("Error al ejecutar el script, se pudo realizar un rollback", e);
conn.rollback();
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); log.error("Error de sql relacionado con la conexion, no se puede realizar rollback", e);
} catch (IOException e) {
e.printStackTrace();
} }
} }
@Override @Override
public void updateTo(int version) { public void updateTo(int version) {
int currentVersion = this.getCurrentVersion(); try {
while (currentVersion < version) { log.warn("Se intentara actualizar la base de datos a la version " + version);
currentVersion++; int currentVersion = this.getCurrentVersion();
executeVersionScript(currentVersion); log.warn("La version actual es " + currentVersion);
while (currentVersion < version) {
currentVersion++;
log.warn("Actualizando a version: " + currentVersion);
executeVersionScript(currentVersion);
}
log.warn("Se termino de actualizar a la version " + version);
} catch (SQLException e){
log.error("Error al intentar realizar una actualizacion", e);
} }
} }
} }

View File

@@ -1,10 +1,5 @@
package danielcortes.xyz.models.version; package danielcortes.xyz.models.version;
import danielcortes.xyz.data.ConnectionHolder; public interface VersionDAO {
void updateTo(int version);
public abstract class VersionDAO {
protected ConnectionHolder connectionHolder;
public abstract void updateTo(int version);
} }