Limpiado modelo de caja

Como consecuencia esto llevo a modificar otras clases que la utilizaban
ya que se decidieron cambiar un par de cosas en su API

- Quitar el que los metodos de update e insert retornaran un booleano,
realmente no era algo necesario y no se estaba utilizando.

- Remplazar el retornar una Caja por retornar un Optional<Caja> en los
metodos de getByFecha y getById, con el fin eliminar un poco los
chequeos de nulls

- El metodo de cajasFromResultSet fue eliminado ya que no lo veia
necesario ya que operaba generaba una List con las cajas obtenidas por
el ResultSet y los de los 2 metodos que la invocaban, solo uno de ellos
necesitaba una lista, el otro solo obtenia el primero de la lista y
continuaba.

- Cambie el necesitar un LocalDate en el metodo que genera las cajas
para un mes, por un objeto con logico para esta situacion, el cual es
YearMonth, esto tuvo como consecuencia la mayoria de los cambios fuera
de esta clase.

- Renombre los metodos para que tuvieran nombres mas agradables para mi,
esto es lo otro que hizo cambiar muchas otras clases.
This commit is contained in:
Daniel Cortes
2019-03-01 22:40:09 -03:00
parent 0045f9da5a
commit 412e128398
19 changed files with 137 additions and 161 deletions

View File

@@ -24,33 +24,34 @@
package danielcortes.xyz.models.caja;
import danielcortes.xyz.data.DAOManager;
import danielcortes.xyz.data.SQLiteConnectionHolder;
import danielcortes.xyz.models.documentos.Documentos;
import danielcortes.xyz.models.documentos.DocumentosDAO;
import danielcortes.xyz.models.documentos.SQLiteDocumentosDAO;
import danielcortes.xyz.models.efectivo.Efectivo;
import danielcortes.xyz.models.efectivo.EfectivoDAO;
import danielcortes.xyz.models.efectivo.SQLiteEfectivoDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SQLiteCajaDAO extends CajaDAO {
public class SQLiteCajaDAO implements CajaDAO {
private static final Logger LOGGER = Logger.getLogger(SQLiteCajaDAO.class.getName());
private SQLiteConnectionHolder connectionHolder;
public SQLiteCajaDAO() {
this.connectionHolder = new SQLiteConnectionHolder();
}
@Override
public List<Caja> findAll() {
public List<Caja> getAll() {
List<Caja> cajaList = new ArrayList<>();
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from caja";
@@ -59,7 +60,13 @@ public class SQLiteCajaDAO extends CajaDAO {
LOGGER.log(Level.FINE, "QUERY: {0}", new Object[]{query});
cajaList = this.cajasFromResultSet(rs);
while (rs.next()) {
Caja caja = new Caja();
caja.setId(rs.getInt("id"));
caja.setFecha(LocalDate.parse(rs.getString("fecha")));
caja.setFondo(rs.getInt("fondo"));
cajaList.add(caja);
}
rs.close();
ps.close();
@@ -71,7 +78,7 @@ public class SQLiteCajaDAO extends CajaDAO {
}
@Override
public Caja findById(int id) {
public Optional<Caja> getById(int id) {
Caja caja = null;
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from caja where id = ?";
@@ -81,18 +88,23 @@ public class SQLiteCajaDAO extends CajaDAO {
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, id});
caja = this.cajasFromResultSet(rs).get(0);
while (rs.next()) {
caja = new Caja();
caja.setId(rs.getInt("id"));
caja.setFecha(LocalDate.parse(rs.getString("fecha")));
caja.setFondo(rs.getInt("fondo"));
}
rs.close();
ps.close();
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return caja;
return Optional.ofNullable(caja);
}
@Override
public Caja findByFecha(LocalDate fecha) {
public Optional<Caja> getByFecha(LocalDate fecha) {
Caja caja = null;
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from caja where fecha = ?";
@@ -103,10 +115,11 @@ public class SQLiteCajaDAO extends CajaDAO {
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, fecha});
List<Caja> cajaList = this.cajasFromResultSet(rs);
if (cajaList.size() > 0) {
caja = cajaList.get(0);
while (rs.next()) {
caja = new Caja();
caja.setId(rs.getInt("id"));
caja.setFecha(LocalDate.parse(rs.getString("fecha")));
caja.setFondo(rs.getInt("fondo"));
}
rs.close();
@@ -114,20 +127,19 @@ public class SQLiteCajaDAO extends CajaDAO {
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return caja;
return Optional.ofNullable(caja);
}
@Override
public boolean insertCaja(Caja caja) {
int updates;
public void insert(Caja caja) {
try (Connection conn = connectionHolder.getConnection()) {
String query = "insert into caja (fecha, fondo) values (?, ?)";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, caja.getFecha().toString());
ps.setInt(2, caja.getFondo());
updates = ps.executeUpdate();
ps.executeUpdate();
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}, {2} | updates: {3}", new Object[]{query, caja.getFecha(), caja.getFondo(), updates});
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}, {2}", new Object[]{query, caja.getFecha(), caja.getFondo()});
ps.close();
@@ -144,65 +156,72 @@ public class SQLiteCajaDAO extends CajaDAO {
ps.close();
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
return false;
}
return updates > 0;
}
@Override
public boolean updateCaja(Caja caja) {
int updates;
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) {
LOGGER.log(Level.SEVERE, e.toString(), e);
}
}
@Override
public void update(Caja caja) {
try (Connection conn = connectionHolder.getConnection()) {
String query = "update caja set fecha = ?, fondo = ? where id = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, caja.getFecha().toString());
ps.setInt(2, caja.getFondo());
ps.setInt(3, caja.getId());
updates = ps.executeUpdate();
ps.executeUpdate();
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}, {2}, {3} | updates: {4}", new Object[]{query, caja.getFecha(), caja.getFondo(), caja.getId(), updates});
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}, {2}, {3}", new Object[]{query, caja.getFecha(), caja.getFondo(), caja.getId()});
ps.close();
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
return false;
}
return updates > 0;
}
@Override
public void createCajasForMonth(LocalDate month) {
LocalDate date = month.withDayOfMonth(1);
LocalDate endDate = date.withDayOfMonth(date.lengthOfMonth()).plusDays(1);
int count = 0;
public void createCajasForMonth(YearMonth mes) {
LocalDate startDate = mes.atDay(1);
LocalDate endDatePlusOne = mes.atEndOfMonth().plusDays(1);
List<Caja> cajas = new ArrayList<>();
LOGGER.log(Level.FINE, "Se intentara crear las cajas no existentes para las fechas entre {0} y {1}", new Object[]{date, endDate});
while (startDate.isBefore(endDatePlusOne)) {
if (this.getByFecha(startDate).isPresent()){
startDate = startDate.plusDays(1);
}else{
Caja caja = new Caja();
caja.setFecha(startDate);
cajas.add(caja);
while (date.isBefore(endDate)) {
if (this.findByFecha(date) != null) {
date = date.plusDays(1);
continue;
startDate = startDate.plusDays(1);
}
Caja caja = new Caja();
caja.setFecha(date);
this.insertCaja(caja);
Efectivo efectivo = new Efectivo();
EfectivoDAO efectivoDAO = new SQLiteEfectivoDAO();
efectivo.setCaja(caja);
efectivoDAO.insertDefaultEfectivo(efectivo);
Documentos documentos = new Documentos();
DocumentosDAO documentosDAO = new SQLiteDocumentosDAO();
documentos.setCaja(caja);
documentosDAO.insertDefaultDocumentos(documentos);
date = date.plusDays(1);
count++;
}
LOGGER.log(Level.FINE, "Terminado de intentar crear las cajas y se crearon {2}", new Object[]{date, endDate, count});
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);
}
}
}