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:
BIN
dist/Programa Caja.jar
vendored
BIN
dist/Programa Caja.jar
vendored
Binary file not shown.
@@ -26,6 +26,7 @@ package danielcortes.xyz.models.calculo_fondo;
|
|||||||
|
|
||||||
import danielcortes.xyz.models.caja.Caja;
|
import danielcortes.xyz.models.caja.Caja;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface CalculoFondoDAO {
|
public interface CalculoFondoDAO {
|
||||||
|
|
||||||
@@ -33,13 +34,13 @@ public interface CalculoFondoDAO {
|
|||||||
|
|
||||||
List<CalculoFondo> findByCaja(Caja caja);
|
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);
|
int getTotalCalculoFondo(Caja caja);
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -56,8 +57,18 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
|||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
LOGGER.log(Level.FINE, "QUERY: {0}", query);
|
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 (
|
} catch (
|
||||||
@@ -70,7 +81,6 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
|||||||
@Override
|
@Override
|
||||||
public List<CalculoFondo> findByCaja(Caja caja) {
|
public List<CalculoFondo> findByCaja(Caja caja) {
|
||||||
List<CalculoFondo> calculoFondoList = new ArrayList<>();
|
List<CalculoFondo> calculoFondoList = new ArrayList<>();
|
||||||
|
|
||||||
if(Caja.EMPTY == caja){
|
if(Caja.EMPTY == caja){
|
||||||
return calculoFondoList;
|
return calculoFondoList;
|
||||||
}
|
}
|
||||||
@@ -82,7 +92,16 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
|||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()});
|
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) {
|
} catch (SQLException e) {
|
||||||
@@ -92,7 +111,7 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CalculoFondo findById(int id) {
|
public Optional<CalculoFondo> findById(int id) {
|
||||||
CalculoFondo calculoFondo = null;
|
CalculoFondo calculoFondo = null;
|
||||||
String query = "select * from calculo_fondo where id = ?";
|
String query = "select * from calculo_fondo where id = ?";
|
||||||
try (Connection conn = connectionHolder.getConnection()) {
|
try (Connection conn = connectionHolder.getConnection()) {
|
||||||
@@ -101,29 +120,33 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
|||||||
try (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});
|
||||||
|
|
||||||
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) {
|
} catch (SQLException e) {
|
||||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||||
}
|
}
|
||||||
return calculoFondo;
|
return Optional.ofNullable(calculoFondo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean insertCalculoFondo(CalculoFondo calculoFondo) {
|
public void insertCalculoFondo(CalculoFondo calculoFondo) {
|
||||||
int updates;
|
|
||||||
String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)";
|
String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)";
|
||||||
try (Connection conn = connectionHolder.getConnection()) {
|
try (Connection conn = connectionHolder.getConnection()) {
|
||||||
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
||||||
ps.setInt(1, calculoFondo.getValor());
|
ps.setInt(1, calculoFondo.getValor());
|
||||||
ps.setString(2, calculoFondo.getDescripcion());
|
ps.setString(2, calculoFondo.getDescripcion());
|
||||||
ps.setInt(3, calculoFondo.getCaja().getId());
|
ps.setInt(3, calculoFondo.getCaja().getId());
|
||||||
|
ps.executeUpdate();
|
||||||
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});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try (PreparedStatement ps = conn.prepareStatement("select last_insert_rowid()")) {
|
try (PreparedStatement ps = conn.prepareStatement("select last_insert_rowid()")) {
|
||||||
@@ -136,14 +159,11 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
|||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return updates > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean updateCalculoFondo(CalculoFondo calculoFondo) {
|
public void updateCalculoFondo(CalculoFondo calculoFondo) {
|
||||||
int updates;
|
|
||||||
String query = "update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?";
|
String query = "update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?";
|
||||||
try (Connection conn = connectionHolder.getConnection()) {
|
try (Connection conn = connectionHolder.getConnection()) {
|
||||||
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
||||||
@@ -152,35 +172,25 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
|||||||
ps.setInt(3, calculoFondo.getCaja().getId());
|
ps.setInt(3, calculoFondo.getCaja().getId());
|
||||||
ps.setInt(4, calculoFondo.getId());
|
ps.setInt(4, calculoFondo.getId());
|
||||||
|
|
||||||
updates = ps.executeUpdate();
|
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});
|
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return updates > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteCalculoFondo(CalculoFondo calculoFondo) {
|
public void deleteCalculoFondo(CalculoFondo calculoFondo) {
|
||||||
int updates;
|
|
||||||
String query = "delete from calculo_fondo where id = ?";
|
String query = "delete from calculo_fondo where id = ?";
|
||||||
try (Connection conn = connectionHolder.getConnection()) {
|
try (Connection conn = connectionHolder.getConnection()) {
|
||||||
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
||||||
ps.setInt(1, calculoFondo.getId());
|
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) {
|
} catch (SQLException e) {
|
||||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return updates > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -198,31 +208,16 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO {
|
|||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()});
|
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()});
|
||||||
|
|
||||||
rs.next();
|
if (rs.next()) {
|
||||||
sum = rs.getInt(1);
|
sum = rs.getInt(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sum;
|
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