Eliminado el metodo calculoFondoFromResultSet
No me agradaba tener ese metodo para solo 3 funciones que lo usaban, y de todas formas, estas funciones necesitaban los datos de manera distinta por lo que no era tan util en realidad
This commit is contained in:
@@ -26,6 +26,7 @@ package danielcortes.xyz.models.calculo_fondo;
|
||||
|
||||
import danielcortes.xyz.models.caja.Caja;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CalculoFondoDAO {
|
||||
|
||||
@@ -33,13 +34,13 @@ public interface CalculoFondoDAO {
|
||||
|
||||
List<CalculoFondo> findByCaja(Caja caja);
|
||||
|
||||
CalculoFondo findById(int id);
|
||||
Optional<CalculoFondo> findById(int id);
|
||||
|
||||
boolean insertCalculoFondo(CalculoFondo calculoFondo);
|
||||
void insertCalculoFondo(CalculoFondo calculoFondo);
|
||||
|
||||
boolean updateCalculoFondo(CalculoFondo calculoFondo);
|
||||
void updateCalculoFondo(CalculoFondo calculoFondo);
|
||||
|
||||
boolean deleteCalculoFondo(CalculoFondo calculoFondo);
|
||||
void deleteCalculoFondo(CalculoFondo calculoFondo);
|
||||
|
||||
int getTotalCalculoFondo(Caja caja);
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -56,8 +57,18 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
LOGGER.log(Level.FINE, "QUERY: {0}", query);
|
||||
|
||||
while (rs.next()) {
|
||||
//Es seguro que exista una caja con ese id, lo dice la base de datos.
|
||||
@SuppressWarnings("OptionalGetWithoutIsPresent")
|
||||
Caja caja = DAOManager.getCajaDAO().getById(rs.getInt("caja_id")).get();
|
||||
CalculoFondo calculoFondo = new CalculoFondo();
|
||||
|
||||
calculoFondoList = this.calculoFondoFromResultSet(rs);
|
||||
calculoFondo.setId(rs.getInt("id"));
|
||||
calculoFondo.setValor(rs.getInt("valor"));
|
||||
calculoFondo.setDescripcion(rs.getString("descripcion"));
|
||||
calculoFondo.setCaja(caja);
|
||||
calculoFondoList.add(calculoFondo);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (
|
||||
@@ -70,7 +81,6 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
||||
@Override
|
||||
public List<CalculoFondo> findByCaja(Caja caja) {
|
||||
List<CalculoFondo> calculoFondoList = new ArrayList<>();
|
||||
|
||||
if(Caja.EMPTY == caja){
|
||||
return calculoFondoList;
|
||||
}
|
||||
@@ -82,7 +92,16 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()});
|
||||
|
||||
calculoFondoList = this.calculoFondoFromResultSet(rs);
|
||||
if (rs.next()) {
|
||||
CalculoFondo calculoFondo = new CalculoFondo();
|
||||
|
||||
calculoFondo.setId(rs.getInt("id"));
|
||||
calculoFondo.setValor(rs.getInt("valor"));
|
||||
calculoFondo.setDescripcion(rs.getString("descripcion"));
|
||||
calculoFondo.setCaja(caja);
|
||||
|
||||
calculoFondoList.add(calculoFondo);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
@@ -92,7 +111,7 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculoFondo findById(int id) {
|
||||
public Optional<CalculoFondo> findById(int id) {
|
||||
CalculoFondo calculoFondo = null;
|
||||
String query = "select * from calculo_fondo where id = ?";
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
@@ -101,29 +120,33 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, id});
|
||||
|
||||
calculoFondo = this.calculoFondoFromResultSet(rs).get(0);
|
||||
if (rs.next()) {
|
||||
//Es seguro que exista una caja con ese id, confio en la base de datos.
|
||||
@SuppressWarnings("OptionalGetWithoutIsPresent")
|
||||
Caja caja = DAOManager.getCajaDAO().getById(rs.getInt("caja_id")).get();
|
||||
calculoFondo = new CalculoFondo();
|
||||
calculoFondo.setId(rs.getInt("id"));
|
||||
calculoFondo.setValor(rs.getInt("valor"));
|
||||
calculoFondo.setDescripcion(rs.getString("descripcion"));
|
||||
calculoFondo.setCaja(caja);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||
}
|
||||
return calculoFondo;
|
||||
return Optional.ofNullable(calculoFondo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean insertCalculoFondo(CalculoFondo calculoFondo) {
|
||||
int updates;
|
||||
public void insertCalculoFondo(CalculoFondo calculoFondo) {
|
||||
String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)";
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
try (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, "QUERY {0} | values: [{1}, {2}, {3}] | updates: {4}",
|
||||
new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(),
|
||||
calculoFondo.getCaja().getId(), updates});
|
||||
ps.executeUpdate();
|
||||
}
|
||||
|
||||
try (PreparedStatement ps = conn.prepareStatement("select last_insert_rowid()")) {
|
||||
@@ -136,14 +159,11 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||
return false;
|
||||
}
|
||||
return updates > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateCalculoFondo(CalculoFondo calculoFondo) {
|
||||
int updates;
|
||||
public void updateCalculoFondo(CalculoFondo calculoFondo) {
|
||||
String query = "update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?";
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
||||
@@ -152,42 +172,32 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
||||
ps.setInt(3, calculoFondo.getCaja().getId());
|
||||
ps.setInt(4, calculoFondo.getId());
|
||||
|
||||
updates = ps.executeUpdate();
|
||||
LOGGER.log(Level.FINE, "QUERY {0} | values: [{1}, {2}, {3}, {4}] | updates: {5}",
|
||||
new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(),
|
||||
calculoFondo.getCaja().getId(), updates});
|
||||
ps.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||
return false;
|
||||
}
|
||||
return updates > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteCalculoFondo(CalculoFondo calculoFondo) {
|
||||
int updates;
|
||||
public void deleteCalculoFondo(CalculoFondo calculoFondo) {
|
||||
String query = "delete from calculo_fondo where id = ?";
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
||||
ps.setInt(1, calculoFondo.getId());
|
||||
updates = ps.executeUpdate();
|
||||
ps.executeUpdate();
|
||||
|
||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1} | updates: {2}",
|
||||
new Object[]{query, calculoFondo.getId(), updates});
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||
return false;
|
||||
}
|
||||
return updates > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalCalculoFondo(Caja caja) {
|
||||
int sum = 0;
|
||||
|
||||
if(Caja.EMPTY == caja){
|
||||
if (Caja.EMPTY == caja) {
|
||||
return sum;
|
||||
}
|
||||
|
||||
@@ -198,31 +208,16 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()});
|
||||
|
||||
rs.next();
|
||||
sum = rs.getInt(1);
|
||||
if (rs.next()) {
|
||||
sum = rs.getInt(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
protected List<CalculoFondo> calculoFondoFromResultSet(ResultSet rs) throws SQLException {
|
||||
List<CalculoFondo> calculoFondoList = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
int caja_id = rs.getInt("caja_id");
|
||||
Caja caja = DAOManager.getCajaDAO().getById(caja_id).get();
|
||||
CalculoFondo calculoFondo = new CalculoFondo();
|
||||
calculoFondo.setId(rs.getInt("id"));
|
||||
calculoFondo.setValor(rs.getInt("valor"));
|
||||
calculoFondo.setDescripcion(rs.getString("descripcion"));
|
||||
calculoFondo.setCaja(caja);
|
||||
calculoFondoList.add(calculoFondo);
|
||||
|
||||
LOGGER.log(Level.FINER, "Se a creo: {0}", calculoFondo);
|
||||
|
||||
}
|
||||
return calculoFondoList;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user