Files
sistema-caja/src/danielcortes/xyz/models/efectivo/SQLiteEfectivoDAO.java
Daniel Cortes eeea52b525 Se elimino el sistema de logging de java
Planeo hacerlo nuevamente con una libreria
2019-03-07 21:38:57 -03:00

255 lines
9.3 KiB
Java

/*
* MIT License
*
* Copyright (c) 2018-2019 Daniel Cortes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package danielcortes.xyz.models.efectivo;
import danielcortes.xyz.data.ConnectionHolder;
import danielcortes.xyz.data.DAOManager;
import danielcortes.xyz.data.SQLiteConnectionHolder;
import danielcortes.xyz.models.caja.Caja;
import danielcortes.xyz.models.caja.CajaDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class SQLiteEfectivoDAO implements EfectivoDAO {
private ConnectionHolder connectionHolder;
public SQLiteEfectivoDAO() {
this.connectionHolder = new SQLiteConnectionHolder();
}
@Override
public List<Efectivo> getAll() {
List<Efectivo> efectivoList = new ArrayList<>();
String query = "select * from efectivos";
try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
try (ResultSet rs = ps.executeQuery()) {
CajaDAO cajaDAO = DAOManager.getCajaDAO();
while (rs.next()) {
//Confio en que la base de datos me entregara un id existente.
@SuppressWarnings("OptionalGetWithoutIsPresent")
Caja caja = cajaDAO.getById(rs.getInt("caja_id")).get();
Efectivo efectivo = new Efectivo();
efectivo.setCaja(caja);
efectivo.setId(rs.getInt("id"));
efectivo.setVeinteMil(rs.getInt("veinte_mil"));
efectivo.setDiezMil(rs.getInt("diez_mil"));
efectivo.setCincoMil(rs.getInt("cinco_mil"));
efectivo.setDosMil(rs.getInt("dos_mil"));
efectivo.setMil(rs.getInt("mil"));
efectivo.setQuinientos(rs.getInt("quinientos"));
efectivo.setCien(rs.getInt("cien"));
efectivo.setCincuenta(rs.getInt("cincuenta"));
efectivo.setDiez(rs.getInt("diez"));
efectivoList.add(efectivo);
}
}
}
} catch (SQLException e) {
}
return efectivoList;
}
@Override
public Optional<Efectivo> getById(int id) {
Efectivo efectivo = null;
String query = "select * from efectivos where id = ?";
try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, id);
try (ResultSet rs = ps.executeQuery()) {
CajaDAO cajaDAO = DAOManager.getCajaDAO();
if (rs.next()) {
//Confio en que la base de datos me entregara un id existente.
@SuppressWarnings("OptionalGetWithoutIsPresent")
Caja caja = cajaDAO.getById(rs.getInt("caja_id")).get();
efectivo = new Efectivo();
efectivo.setCaja(caja);
efectivo.setId(rs.getInt("id"));
efectivo.setVeinteMil(rs.getInt("veinte_mil"));
efectivo.setDiezMil(rs.getInt("diez_mil"));
efectivo.setCincoMil(rs.getInt("cinco_mil"));
efectivo.setDosMil(rs.getInt("dos_mil"));
efectivo.setMil(rs.getInt("mil"));
efectivo.setQuinientos(rs.getInt("quinientos"));
efectivo.setCien(rs.getInt("cien"));
efectivo.setCincuenta(rs.getInt("cincuenta"));
efectivo.setDiez(rs.getInt("diez"));
}
}
}
} catch (SQLException e) {
}
return Optional.ofNullable(efectivo);
}
@Override
public Optional<Efectivo> getByCaja(Caja caja) {
Efectivo efectivo = null;
if (Caja.EMPTY == caja) {
return Optional.ofNullable(efectivo);
}
String query = "select * from efectivos where caja_id = ?";
try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, caja.getId());
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
efectivo = new Efectivo();
efectivo.setCaja(caja);
efectivo.setId(rs.getInt("id"));
efectivo.setVeinteMil(rs.getInt("veinte_mil"));
efectivo.setDiezMil(rs.getInt("diez_mil"));
efectivo.setCincoMil(rs.getInt("cinco_mil"));
efectivo.setDosMil(rs.getInt("dos_mil"));
efectivo.setMil(rs.getInt("mil"));
efectivo.setQuinientos(rs.getInt("quinientos"));
efectivo.setCien(rs.getInt("cien"));
efectivo.setCincuenta(rs.getInt("cincuenta"));
efectivo.setDiez(rs.getInt("diez"));
}
}
}
} catch (SQLException e) {
}
return Optional.ofNullable(efectivo);
}
@Override
public int getTotalEfectivo(Caja caja) {
int total = 0;
if (Caja.EMPTY == caja) {
return total;
}
String query = "select veinte_mil + diez_mil + cinco_mil + dos_mil + mil + quinientos + cien + cincuenta + diez from efectivos where caja_id = ?";
try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, caja.getId());
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
total = rs.getInt(1);
}
}
}
} catch (SQLException e) {
}
return total;
}
@Override
public void insertEfectivo(Efectivo efectivo) {
String query = "insert into efectivos (veinte_mil, diez_mil, cinco_mil, dos_mil, mil, quinientos, cien, cincuenta, diez, caja_id) values (?,?,?,?,?,?,?,?,?,?)";
try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, efectivo.getVeinteMil());
ps.setInt(2, efectivo.getDiezMil());
ps.setInt(3, efectivo.getCincoMil());
ps.setInt(4, efectivo.getDosMil());
ps.setInt(5, efectivo.getMil());
ps.setInt(6, efectivo.getQuinientos());
ps.setInt(7, efectivo.getCien());
ps.setInt(8, efectivo.getCincuenta());
ps.setInt(9, efectivo.getDiez());
ps.setInt(10, efectivo.getCaja().getId());
ps.executeUpdate();
}
try (PreparedStatement ps = conn.prepareStatement("select last_insert_rowid()")) {
try (ResultSet rs = ps.executeQuery()) {
rs.next();
efectivo.setId(rs.getInt(1));
}
}
} catch (SQLException e) {
}
}
@Override
public void insertDefaultEfectivo(Efectivo efectivo) {
String query = "insert into efectivos (veinte_mil, diez_mil, cinco_mil, dos_mil, mil, quinientos, cien, cincuenta, diez, caja_id) values (0,0,0,0,0,0,0,0,0,?)";
try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, efectivo.getCaja().getId());
ps.executeUpdate();
}
try (PreparedStatement ps = conn.prepareStatement("select last_insert_rowid()")) {
try (ResultSet rs = ps.executeQuery()) {
rs.next();
efectivo.setId(rs.getInt(1));
}
}
} catch (SQLException e) {
}
}
@Override
public void updateEfectivo(Efectivo efectivo) {
String query = "update efectivos set veinte_mil = ?, diez_mil = ?, cinco_mil = ?, dos_mil = ?, mil = ?, quinientos = ?, cien = ?, cincuenta = ?, diez = ?, caja_id = ? where id = ?";
try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, efectivo.getVeinteMil());
ps.setInt(2, efectivo.getDiezMil());
ps.setInt(3, efectivo.getCincoMil());
ps.setInt(4, efectivo.getDosMil());
ps.setInt(5, efectivo.getMil());
ps.setInt(6, efectivo.getQuinientos());
ps.setInt(7, efectivo.getCien());
ps.setInt(8, efectivo.getCincuenta());
ps.setInt(9, efectivo.getDiez());
ps.setInt(10, efectivo.getCaja().getId());
ps.setInt(11, efectivo.getId());
ps.executeUpdate();
}
} catch (SQLException e) {
}
}
@Override
public void deleteEfectivo(Efectivo efectivo) {
String query = "delete from efectivos where id = ?";
try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, efectivo.getId());
ps.executeUpdate();
}
} catch (SQLException e) {
}
}
}