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

@@ -29,7 +29,9 @@ import danielcortes.xyz.data.Configuration;
import danielcortes.xyz.views.ManagerView;
import javax.swing.*;
import java.util.Arrays;
import java.util.Locale;
import java.util.logging.*;
public class Main {
public static void main(String[] args) {
@@ -37,7 +39,7 @@ public class Main {
}
private static void run() {
setSystemProperties();
setUpSystemProperties();
ManagerView view = new ManagerView();
ManagerController managerController = new ManagerController(view);
@@ -56,7 +58,7 @@ public class Main {
}
private static void setSystemProperties() {
private static void setUpSystemProperties() {
System.setProperty("awt.useSystemAAFontSettings", "on");
System.setProperty("swing.aatext", "true");
@@ -67,8 +69,6 @@ public class Main {
}
Locale.setDefault(new Locale("es"));
}
}

View File

@@ -27,21 +27,29 @@ package danielcortes.xyz.data;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Configuration {
private static final Logger LOGGER = Logger.getLogger( Configuration.class.getName() );
private static final Properties config;
static {
config = new Properties();
try {
LOGGER.log(Level.INFO, "Leyendo y llenando el objeto de properties");
FileInputStream in = new FileInputStream("conf.properties");
config.load(in);
in.close();
} catch (IOException e) {
System.err.println("Couldn't load properties! The application will close");
e.printStackTrace();
LOGGER.log(Level.INFO, "El objeto de properties se a llenado correctamente");
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
System.exit(1);
}
}

View File

@@ -27,8 +27,13 @@ package danielcortes.xyz.data;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SQLiteConnectionHolder implements ConnectionHolder {
private static final Logger LOGGER = Logger.getLogger( Configuration.class.getName() );
private String databaseURI;
public SQLiteConnectionHolder() {
@@ -42,8 +47,9 @@ public class SQLiteConnectionHolder implements ConnectionHolder {
try {
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection(databaseURI);
LOGGER.log(Level.FINER, "Creada conexion a base de datos SQLITE");
} catch (ClassNotFoundException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.toString(), e);
System.exit(133);
}

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);
}
}
}

View File

@@ -24,6 +24,7 @@
package danielcortes.xyz.models.calculo_fondo;
import danielcortes.xyz.data.Configuration;
import danielcortes.xyz.data.SQLiteConnectionHolder;
import danielcortes.xyz.models.caja.Caja;
@@ -33,8 +34,12 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
private static final Logger LOGGER = Logger.getLogger(Configuration.class.getName());
public SQLiteCalculoFondoDAO() {
this.connectionHolder = new SQLiteConnectionHolder();
}
@@ -43,15 +48,18 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
public List<CalculoFondo> findAll() {
List<CalculoFondo> calculoFondoList = new ArrayList<>();
try (Connection conn = connectionHolder.getConnection()) {
PreparedStatement ps = conn.prepareStatement("select * from calculo_fondo");
String query = "select * from calculo_fondo";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "Se ejecuta query: {0}", query);
calculoFondoList = this.cajasFromResultSet(rs);
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return calculoFondoList;
}
@@ -60,17 +68,19 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
public List<CalculoFondo> findByCaja(Caja caja) {
List<CalculoFondo> calculoFondoList = new ArrayList<>();
try (Connection conn = connectionHolder.getConnection()) {
PreparedStatement ps = conn.prepareStatement("select * from calculo_fondo where caja_id = ?");
String query = "select * from calculo_fondo where caja_id = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, caja.getId());
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "Se ejecuta query: {0} con caja_id = {1}", new Object[]{query, caja.getId()});
calculoFondoList = this.cajasFromResultSet(rs);
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return calculoFondoList;
}
@@ -79,16 +89,19 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
public CalculoFondo findById(int id) {
CalculoFondo calculoFondo = null;
try (Connection conn = connectionHolder.getConnection()) {
PreparedStatement ps = conn.prepareStatement("select * from calculo_fondo where id = ?");
String query = "select * from calculo_fondo 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});
calculoFondo = this.cajasFromResultSet(rs).get(0);
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return calculoFondo;
}
@@ -97,23 +110,28 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
public boolean insertCalculoFondo(CalculoFondo calculoFondo) {
int updates;
try (Connection conn = connectionHolder.getConnection()) {
PreparedStatement ps = conn.prepareStatement("insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)");
String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, calculoFondo.getValor());
ps.setString(2, calculoFondo.getDescripcion());
ps.setInt(3, calculoFondo.getCaja().getId());
updates = ps.executeUpdate();
LOGGER.log(Level.FINE, "Se ejecuta query: {0} con valor = {1}, descripcion = {2}, caja_id = {3}. se realizaron {4} updates", new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(), calculoFondo.getCaja().getId(), updates});
ps.close();
ps = conn.prepareStatement("select last_insert_rowid()");
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "Se ejecuta query: {0}", query);
rs.next();
calculoFondo.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;
@@ -123,17 +141,19 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
public boolean updateCalculoFondo(CalculoFondo calculoFondo) {
int updates;
try (Connection conn = connectionHolder.getConnection()) {
PreparedStatement ps = conn.prepareStatement("update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?");
String query = "update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, calculoFondo.getValor());
ps.setString(2, calculoFondo.getDescripcion());
ps.setInt(3, calculoFondo.getCaja().getId());
ps.setInt(4, calculoFondo.getId());
updates = ps.executeUpdate();
LOGGER.log(Level.FINE, "Se ejecuta query: {0} con valor = {1}, descripcion = {2}, caja_id = {3}, id = {4}. se realizaron {5} updates", new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(), calculoFondo.getCaja().getId(), calculoFondo.getId(), updates});
ps.close();
} catch (SQLException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.toString(), e);
return false;
}
return updates > 0;
@@ -143,14 +163,16 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
public boolean deleteCalculoFondo(CalculoFondo calculoFondo) {
int updates;
try (Connection conn = connectionHolder.getConnection()) {
PreparedStatement ps = conn.prepareStatement("delete from calculo_fondo where id = ?");
String query = "delete from calculo_fondo where id = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, calculoFondo.getId());
updates = ps.executeUpdate();
LOGGER.log(Level.FINE, "Se ejecuta query: {0} con id = {1}. Se realizaron {2} updates", new Object[]{query, calculoFondo.getId(), updates});
ps.close();
} catch (SQLException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.toString(), e);
return false;
}
return updates > 0;
@@ -160,16 +182,19 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
public int getTotalCalculoFondo(Caja caja) {
int sum = 0;
try (Connection conn = connectionHolder.getConnection()) {
PreparedStatement ps = conn.prepareStatement("select sum(valor) from calculo_fondo where caja_id = ?");
String query = "select sum(valor) from calculo_fondo where caja_id = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, caja.getId());
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "Se ejecuta query: {0} con caja_id = {1}", new Object[]{query, caja.getId()});
rs.next();
sum = rs.getInt(1);
ps.close();
} catch (SQLException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return sum;
}