Se limpio un poco el objeto CajaDAO
Primero se crearon bloques try/catch para todos los preparedstatement y resultsets Se eliminaron un par de metodos que no se utilizaban
This commit is contained in:
BIN
dist/Programa Caja.jar
vendored
BIN
dist/Programa Caja.jar
vendored
Binary file not shown.
@@ -25,7 +25,6 @@
|
|||||||
package danielcortes.xyz.models.caja;
|
package danielcortes.xyz.models.caja;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.YearMonth;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -38,11 +37,5 @@ public interface CajaDAO {
|
|||||||
Optional<Caja> getByFecha(LocalDate fecha);
|
Optional<Caja> getByFecha(LocalDate fecha);
|
||||||
|
|
||||||
void insert(Caja caja);
|
void insert(Caja caja);
|
||||||
|
|
||||||
void insert(List<Caja> cajas);
|
|
||||||
|
|
||||||
void update(Caja caja);
|
void update(Caja caja);
|
||||||
|
|
||||||
void createCajasForMonth(YearMonth month);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,16 +24,12 @@
|
|||||||
|
|
||||||
package danielcortes.xyz.models.caja;
|
package danielcortes.xyz.models.caja;
|
||||||
|
|
||||||
import danielcortes.xyz.data.DAOManager;
|
|
||||||
import danielcortes.xyz.data.SQLiteConnectionHolder;
|
import danielcortes.xyz.data.SQLiteConnectionHolder;
|
||||||
import danielcortes.xyz.models.documentos.Documentos;
|
|
||||||
import danielcortes.xyz.models.efectivo.Efectivo;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.YearMonth;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -55,9 +51,8 @@ public class SQLiteCajaDAO implements CajaDAO {
|
|||||||
List<Caja> cajaList = new ArrayList<>();
|
List<Caja> cajaList = new ArrayList<>();
|
||||||
try (Connection conn = connectionHolder.getConnection()) {
|
try (Connection conn = connectionHolder.getConnection()) {
|
||||||
String query = "select * from caja";
|
String query = "select * from caja";
|
||||||
PreparedStatement ps = conn.prepareStatement(query);
|
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
||||||
ResultSet rs = ps.executeQuery();
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
LOGGER.log(Level.FINE, "QUERY: {0}", new Object[]{query});
|
LOGGER.log(Level.FINE, "QUERY: {0}", new Object[]{query});
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
@@ -67,9 +62,8 @@ public class SQLiteCajaDAO implements CajaDAO {
|
|||||||
caja.setFondo(rs.getInt("fondo"));
|
caja.setFondo(rs.getInt("fondo"));
|
||||||
cajaList.add(caja);
|
cajaList.add(caja);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
rs.close();
|
}
|
||||||
ps.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||||
}
|
}
|
||||||
@@ -80,12 +74,11 @@ public class SQLiteCajaDAO implements CajaDAO {
|
|||||||
@Override
|
@Override
|
||||||
public Optional<Caja> getById(int id) {
|
public Optional<Caja> getById(int id) {
|
||||||
Caja caja = null;
|
Caja caja = null;
|
||||||
try (Connection conn = connectionHolder.getConnection()) {
|
|
||||||
String query = "select * from caja where id = ?";
|
String query = "select * from caja where id = ?";
|
||||||
PreparedStatement ps = conn.prepareStatement(query);
|
try (Connection conn = connectionHolder.getConnection()) {
|
||||||
|
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
||||||
ps.setInt(1, id);
|
ps.setInt(1, id);
|
||||||
ResultSet rs = ps.executeQuery();
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, id});
|
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, id});
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
@@ -94,9 +87,8 @@ public class SQLiteCajaDAO implements CajaDAO {
|
|||||||
caja.setFecha(LocalDate.parse(rs.getString("fecha")));
|
caja.setFecha(LocalDate.parse(rs.getString("fecha")));
|
||||||
caja.setFondo(rs.getInt("fondo"));
|
caja.setFondo(rs.getInt("fondo"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
rs.close();
|
}
|
||||||
ps.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||||
}
|
}
|
||||||
@@ -106,12 +98,11 @@ public class SQLiteCajaDAO implements CajaDAO {
|
|||||||
@Override
|
@Override
|
||||||
public Optional<Caja> getByFecha(LocalDate fecha) {
|
public Optional<Caja> getByFecha(LocalDate fecha) {
|
||||||
Caja caja = null;
|
Caja caja = null;
|
||||||
try (Connection conn = connectionHolder.getConnection()) {
|
|
||||||
String query = "select * from caja where fecha = ?";
|
String query = "select * from caja where fecha = ?";
|
||||||
|
try (Connection conn = connectionHolder.getConnection()) {
|
||||||
PreparedStatement ps = conn.prepareStatement(query);
|
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
||||||
ps.setString(1, fecha.toString());
|
ps.setString(1, fecha.toString());
|
||||||
ResultSet rs = ps.executeQuery();
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, fecha});
|
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, fecha});
|
||||||
|
|
||||||
@@ -121,9 +112,8 @@ public class SQLiteCajaDAO implements CajaDAO {
|
|||||||
caja.setFecha(LocalDate.parse(rs.getString("fecha")));
|
caja.setFecha(LocalDate.parse(rs.getString("fecha")));
|
||||||
caja.setFondo(rs.getInt("fondo"));
|
caja.setFondo(rs.getInt("fondo"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
rs.close();
|
}
|
||||||
ps.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||||
}
|
}
|
||||||
@@ -132,45 +122,27 @@ public class SQLiteCajaDAO implements CajaDAO {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void insert(Caja caja) {
|
public void insert(Caja caja) {
|
||||||
try (Connection conn = connectionHolder.getConnection()) {
|
|
||||||
String query = "insert into caja (fecha, fondo) values (?, ?)";
|
String query = "insert into caja (fecha, fondo) values (?, ?)";
|
||||||
PreparedStatement ps = conn.prepareStatement(query);
|
try (Connection conn = connectionHolder.getConnection()) {
|
||||||
|
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
||||||
ps.setString(1, caja.getFecha().toString());
|
ps.setString(1, caja.getFecha().toString());
|
||||||
ps.setInt(2, caja.getFondo());
|
ps.setInt(2, caja.getFondo());
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
|
|
||||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}, {2}",
|
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}, {2}",
|
||||||
new Object[]{query, caja.getFecha(), caja.getFondo()});
|
new Object[]{query, caja.getFecha(), caja.getFondo()});
|
||||||
|
}
|
||||||
ps.close();
|
|
||||||
|
|
||||||
query = "select last_insert_rowid()";
|
query = "select last_insert_rowid()";
|
||||||
ps = conn.prepareStatement("select last_insert_rowid()");
|
try (PreparedStatement ps = conn.prepareStatement("select last_insert_rowid()")) {
|
||||||
ResultSet rs = ps.executeQuery();
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
LOGGER.log(Level.FINE, "QUERY: {0}", new Object[]{query});
|
LOGGER.log(Level.FINE, "QUERY: {0}", new Object[]{query});
|
||||||
|
|
||||||
rs.next();
|
rs.next();
|
||||||
caja.setId(rs.getInt(1));
|
caja.setId(rs.getInt(1));
|
||||||
|
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void insert(List<Caja> cajas) {
|
|
||||||
String query = "insert into caja (fecha, fondo) values (?, ?)";
|
|
||||||
try (Connection conn = connectionHolder.getConnection(); PreparedStatement ps = conn
|
|
||||||
.prepareStatement(query)) {
|
|
||||||
for (Caja caja : cajas) {
|
|
||||||
ps.setString(1, caja.getFecha().toString());
|
|
||||||
ps.setInt(2, caja.getFondo());
|
|
||||||
ps.addBatch();
|
|
||||||
}
|
|
||||||
ps.executeBatch();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||||
}
|
}
|
||||||
@@ -178,9 +150,9 @@ public class SQLiteCajaDAO implements CajaDAO {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Caja caja) {
|
public void update(Caja caja) {
|
||||||
try (Connection conn = connectionHolder.getConnection()) {
|
|
||||||
String query = "update caja set fecha = ?, fondo = ? where id = ?";
|
String query = "update caja set fecha = ?, fondo = ? where id = ?";
|
||||||
PreparedStatement ps = conn.prepareStatement(query);
|
try (Connection conn = connectionHolder.getConnection()) {
|
||||||
|
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
||||||
ps.setString(1, caja.getFecha().toString());
|
ps.setString(1, caja.getFecha().toString());
|
||||||
ps.setInt(2, caja.getFondo());
|
ps.setInt(2, caja.getFondo());
|
||||||
ps.setInt(3, caja.getId());
|
ps.setInt(3, caja.getId());
|
||||||
@@ -189,41 +161,9 @@ public class SQLiteCajaDAO implements CajaDAO {
|
|||||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}, {2}, {3}",
|
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}, {2}, {3}",
|
||||||
new Object[]{query, caja.getFecha(), caja.getFondo(), caja.getId()});
|
new Object[]{query, caja.getFecha(), caja.getFondo(), caja.getId()});
|
||||||
|
|
||||||
ps.close();
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void createCajasForMonth(YearMonth mes) {
|
|
||||||
LocalDate startDate = mes.atDay(1);
|
|
||||||
LocalDate endDatePlusOne = mes.atEndOfMonth().plusDays(1);
|
|
||||||
List<Caja> cajas = new ArrayList<>();
|
|
||||||
|
|
||||||
while (startDate.isBefore(endDatePlusOne)) {
|
|
||||||
if (this.getByFecha(startDate).isPresent()) {
|
|
||||||
startDate = startDate.plusDays(1);
|
|
||||||
} else {
|
|
||||||
Caja caja = new Caja();
|
|
||||||
caja.setFecha(startDate);
|
|
||||||
cajas.add(caja);
|
|
||||||
|
|
||||||
startDate = startDate.plusDays(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.insert(cajas);
|
|
||||||
|
|
||||||
for (Caja caja : cajas) {
|
|
||||||
Efectivo efectivo = new Efectivo();
|
|
||||||
efectivo.setCaja(caja);
|
|
||||||
DAOManager.getEfectivoDAO().insertDefaultEfectivo(efectivo);
|
|
||||||
|
|
||||||
Documentos documentos = new Documentos();
|
|
||||||
documentos.setCaja(caja);
|
|
||||||
DAOManager.getDocumentosDAO().insertDefaultDocumentos(documentos);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user