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:
@@ -91,7 +91,6 @@ public class DAOManager {
|
||||
return egresoDAO;
|
||||
}
|
||||
|
||||
|
||||
public static IngresoDAO getIngresoDAO() {
|
||||
return ingresoDAO;
|
||||
}
|
||||
|
||||
@@ -33,16 +33,30 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
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 {
|
||||
|
||||
private static final Logger log = LogManager.getLogger(SQLiteTipoIngresoDAO.class);
|
||||
private ConnectionHolder connectionHolder;
|
||||
|
||||
public SQLiteTipoIngresoDAO() {
|
||||
this.connectionHolder = new SQLiteConnectionHolder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene una lista todos los TipoIngreso existentes
|
||||
*
|
||||
* @return Una lista de TipoIngreso
|
||||
*/
|
||||
@Override
|
||||
public List<TipoIngreso> getAll() {
|
||||
log.debug("Se intentara conseguir todos los TipoIngreso");
|
||||
List<TipoIngreso> tiposIngresoList = new ArrayList<>();
|
||||
String query = "select * from tipos_ingreso";
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
@@ -57,13 +71,22 @@ public class SQLiteTipoIngresoDAO implements TipoIngresoDAO {
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
log.error("Error al intentar conseguir todos los TipoIngreso", e);
|
||||
}
|
||||
log.debug("Se consiguieron " + tiposIngresoList.size() + " TiposIngreso");
|
||||
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
|
||||
public Optional<TipoIngreso> getById(int id) {
|
||||
log.debug("Se intentara conseguir un TipoIngreso con id " + id);
|
||||
TipoIngreso tipoIngreso = null;
|
||||
String query = "select * from tipos_ingreso where id = ?";
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
@@ -78,13 +101,22 @@ public class SQLiteTipoIngresoDAO implements TipoIngresoDAO {
|
||||
}
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
public Optional<TipoIngreso> getByNombre(String nombre) {
|
||||
log.debug("Se intentara conseguir un TipoIngreso con nombre " + nombre);
|
||||
TipoIngreso tipoIngreso = null;
|
||||
String query = "select * from tipos_ingreso where nombre = ?";
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
@@ -99,8 +131,9 @@ public class SQLiteTipoIngresoDAO implements TipoIngresoDAO {
|
||||
}
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package danielcortes.xyz.models.version;
|
||||
|
||||
import danielcortes.xyz.data.ConnectionHolder;
|
||||
import danielcortes.xyz.data.SQLiteConnectionHolder;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
@@ -12,10 +13,16 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.zip.ZipEntry;
|
||||
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() {
|
||||
this.connectionHolder = new SQLiteConnectionHolder();
|
||||
@@ -24,7 +31,7 @@ public class SQLiteVersionDAO extends VersionDAO {
|
||||
private boolean tableVersionsExists() {
|
||||
try {
|
||||
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();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
@@ -33,7 +40,7 @@ public class SQLiteVersionDAO extends VersionDAO {
|
||||
}
|
||||
}
|
||||
|
||||
private int getCurrentVersion() {
|
||||
private int getCurrentVersion() throws SQLException{
|
||||
if (tableVersionsExists()) {
|
||||
String query = "SELECT version FROM version LIMIT 1";
|
||||
try (Connection conn = this.connectionHolder.getConnection()) {
|
||||
@@ -44,52 +51,72 @@ public class SQLiteVersionDAO extends VersionDAO {
|
||||
}
|
||||
}
|
||||
} 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) {
|
||||
try {
|
||||
ZipFile zipFile = new ZipFile(new File("data/version_scripts"));
|
||||
ZipEntry zipEntry = zipFile.getEntry(version + ".sql");
|
||||
return zipFile.getInputStream(zipEntry);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new InputStream() {
|
||||
@Override
|
||||
public int read() {
|
||||
return 0;
|
||||
private InputStream unzipVersionScript(int version) throws IOException {
|
||||
ZipFile zipFile = new ZipFile(new File("data/version_scripts"));
|
||||
ZipEntry zipEntry = zipFile.getEntry(version + ".sql");
|
||||
return zipFile.getInputStream(zipEntry);
|
||||
}
|
||||
|
||||
private ArrayList<String> getVersionScript(int version) throws IOException {
|
||||
ArrayList<String> script = new ArrayList<>();
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(unzipVersionScript(version)))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if(line.trim().length() > 0) {
|
||||
script.add(line);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
return script;
|
||||
}
|
||||
|
||||
private void executeVersionScript(int version) {
|
||||
try (Connection conn = this.connectionHolder.getConnection()) {
|
||||
try(Statement statement = conn.createStatement()) {
|
||||
InputStream inputStream = getVersionScript(version);
|
||||
try(BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
statement.addBatch(line);
|
||||
conn.setAutoCommit(false);
|
||||
try (Statement statement = conn.createStatement()) {
|
||||
try{
|
||||
ArrayList<String> script = getVersionScript(version);
|
||||
for (String query : script) {
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.error("Error de sql relacionado con la conexion, no se puede realizar rollback", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTo(int version) {
|
||||
int currentVersion = this.getCurrentVersion();
|
||||
while (currentVersion < version) {
|
||||
currentVersion++;
|
||||
executeVersionScript(currentVersion);
|
||||
try {
|
||||
log.warn("Se intentara actualizar la base de datos a la version " + version);
|
||||
int currentVersion = this.getCurrentVersion();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
package danielcortes.xyz.models.version;
|
||||
|
||||
import danielcortes.xyz.data.ConnectionHolder;
|
||||
|
||||
public abstract class VersionDAO {
|
||||
|
||||
protected ConnectionHolder connectionHolder;
|
||||
|
||||
public abstract void updateTo(int version);
|
||||
public interface VersionDAO {
|
||||
void updateTo(int version);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user