Se comienza a incluir logs en el programa, va a hacer falta en el momento que se haga deploy serio

This commit is contained in:
Daniel Cortes
2019-01-20 05:29:33 -03:00
parent 446f10e9f2
commit 6904f55c20
8 changed files with 279 additions and 217 deletions

View File

@@ -24,6 +24,7 @@
package danielcortes.xyz.models.caja;
import danielcortes.xyz.data.Configuration;
import danielcortes.xyz.data.SQLiteConnectionHolder;
import danielcortes.xyz.models.documentos.Documentos;
import danielcortes.xyz.models.documentos.DocumentosDAO;
@@ -39,8 +40,12 @@ import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SQLiteCajaDAO extends CajaDAO {
private static final Logger LOGGER = Logger.getLogger( Configuration.class.getName() );
public SQLiteCajaDAO() {
this.connectionHolder = new SQLiteConnectionHolder();
}
@@ -49,15 +54,18 @@ public class SQLiteCajaDAO extends CajaDAO {
public List<Caja> findAll() {
List<Caja> cajaList = new ArrayList<>();
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from caja";
PreparedStatement ps = conn.prepareStatement("select * from caja");
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "Se ejecuta query: {0}", query);
cajaList = this.cajasFromResultSet(rs);
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return cajaList;
@@ -67,18 +75,19 @@ public class SQLiteCajaDAO extends CajaDAO {
public Caja findById(int id) {
Caja caja = null;
try (Connection conn = connectionHolder.getConnection()) {
PreparedStatement ps = conn.prepareStatement("select * from caja where id = ?");
String query = "select * from caja where id = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "Se ejecuta query: {0}, con id = {1}", new Object[]{query, id});
caja = this.cajasFromResultSet(rs).get(0);
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return caja;
}
@@ -87,12 +96,14 @@ public class SQLiteCajaDAO extends CajaDAO {
public Caja findByFecha(LocalDate fecha) {
Caja caja = null;
try (Connection conn = connectionHolder.getConnection()) {
PreparedStatement ps = conn.prepareStatement("select * from caja where fecha = ?");
String query = "select * from caja where fecha = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, fecha.toString());
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "Se ejecuta query: {0}, con fecha = {1}", new Object[]{query, fecha});
List<Caja> cajaList = this.cajasFromResultSet(rs);
if (cajaList.size() > 0) {
@@ -102,7 +113,7 @@ public class SQLiteCajaDAO extends CajaDAO {
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return caja;
}
@@ -111,22 +122,30 @@ public class SQLiteCajaDAO extends CajaDAO {
public boolean insertCaja(Caja caja) {
int updates;
try (Connection conn = connectionHolder.getConnection()) {
PreparedStatement ps = conn.prepareStatement("insert into caja (fecha) values (?)");
String query = "insert into caja (fecha) values (?)";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, caja.getFecha().toString());
updates = ps.executeUpdate();
LOGGER.log(Level.FINE, "Se ejecuta query: {0}, con fecha = {1}", new Object[]{query, caja.getFecha().toString()});
ps.close();
query = "select last_insert_rowid()";
ps = conn.prepareStatement("select last_insert_rowid()");
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "Se ejecuta query: {0}", new Object[]{query});
rs.next();
caja.setId(rs.getInt(1));
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.toString(), e);
return false;
}
return updates > 0;
@@ -136,15 +155,17 @@ public class SQLiteCajaDAO extends CajaDAO {
public boolean updateCaja(Caja caja) {
int updates;
try (Connection conn = connectionHolder.getConnection()) {
PreparedStatement ps = conn.prepareStatement("update caja set fecha = ? where id = ?");
String query = "update caja set fecha = ? where id = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, caja.getFecha().toString());
ps.setInt(2, caja.getId());
updates = ps.executeUpdate();
LOGGER.log(Level.FINE, "Se ejecuta query: {0}, con fecha = {1} y id = {2}", new Object[]{query, caja.getFecha(), caja.getId()});
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
return false;
}
return updates > 0;
@@ -155,6 +176,8 @@ public class SQLiteCajaDAO extends CajaDAO {
LocalDate date = month.withDayOfMonth(1);
LocalDate endDate = date.withDayOfMonth(date.lengthOfMonth()).plusDays(1);
LOGGER.log(Level.FINE, "Se intentara crear las cajas para un mes para las fechas entre {0} y {1}", new Object[]{date, endDate});
while (date.isBefore(endDate)) {
if (this.findByFecha(date) != null) {
date = date.plusDays(1);
@@ -177,6 +200,5 @@ public class SQLiteCajaDAO extends CajaDAO {
date = date.plusDays(1);
}
}
}