Se aplico el estilo de codigo de google :3

https://github.com/google/styleguide
This commit is contained in:
Daniel Cortes
2019-03-01 23:28:43 -03:00
parent 412e128398
commit 95685b7f82
81 changed files with 8826 additions and 7757 deletions

View File

@@ -28,7 +28,6 @@ import danielcortes.xyz.data.DAOManager;
import danielcortes.xyz.data.SQLiteConnectionHolder;
import danielcortes.xyz.models.documentos.Documentos;
import danielcortes.xyz.models.efectivo.Efectivo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -42,186 +41,189 @@ import java.util.logging.Level;
import java.util.logging.Logger;
public class SQLiteCajaDAO implements CajaDAO {
private static final Logger LOGGER = Logger.getLogger(SQLiteCajaDAO.class.getName());
private SQLiteConnectionHolder connectionHolder;
private static final Logger LOGGER = Logger.getLogger(SQLiteCajaDAO.class.getName());
public SQLiteCajaDAO() {
this.connectionHolder = new SQLiteConnectionHolder();
private SQLiteConnectionHolder connectionHolder;
public SQLiteCajaDAO() {
this.connectionHolder = new SQLiteConnectionHolder();
}
@Override
public List<Caja> getAll() {
List<Caja> cajaList = new ArrayList<>();
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from caja";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "QUERY: {0}", new Object[]{query});
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();
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
}
@Override
public List<Caja> getAll() {
List<Caja> cajaList = new ArrayList<>();
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from caja";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
return cajaList;
}
LOGGER.log(Level.FINE, "QUERY: {0}", new Object[]{query});
@Override
public Optional<Caja> getById(int id) {
Caja caja = null;
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from caja where id = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
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);
}
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, id});
rs.close();
ps.close();
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
}
while (rs.next()) {
caja = new Caja();
caja.setId(rs.getInt("id"));
caja.setFecha(LocalDate.parse(rs.getString("fecha")));
caja.setFondo(rs.getInt("fondo"));
}
return cajaList;
rs.close();
ps.close();
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return Optional.ofNullable(caja);
}
@Override
public Optional<Caja> getByFecha(LocalDate fecha) {
Caja caja = null;
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from caja where fecha = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, fecha.toString());
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, fecha});
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 Optional.ofNullable(caja);
}
@Override
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());
ps.executeUpdate();
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}, {2}",
new Object[]{query, caja.getFecha(), caja.getFondo()});
ps.close();
query = "select last_insert_rowid()";
ps = conn.prepareStatement("select last_insert_rowid()");
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "QUERY: {0}", new Object[]{query});
rs.next();
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) {
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());
ps.executeUpdate();
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);
}
}
@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);
}
}
@Override
public Optional<Caja> getById(int id) {
Caja caja = null;
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from caja where id = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
this.insert(cajas);
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, id});
for (Caja caja : cajas) {
Efectivo efectivo = new Efectivo();
efectivo.setCaja(caja);
DAOManager.getEfectivoDAO().insertDefaultEfectivo(efectivo);
while (rs.next()) {
caja = new Caja();
caja.setId(rs.getInt("id"));
caja.setFecha(LocalDate.parse(rs.getString("fecha")));
caja.setFondo(rs.getInt("fondo"));
}
Documentos documentos = new Documentos();
documentos.setCaja(caja);
DAOManager.getDocumentosDAO().insertDefaultDocumentos(documentos);
rs.close();
ps.close();
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return Optional.ofNullable(caja);
}
@Override
public Optional<Caja> getByFecha(LocalDate fecha) {
Caja caja = null;
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from caja where fecha = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, fecha.toString());
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, fecha});
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 Optional.ofNullable(caja);
}
@Override
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());
ps.executeUpdate();
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}, {2}", new Object[]{query, caja.getFecha(), caja.getFondo()});
ps.close();
query = "select last_insert_rowid()";
ps = conn.prepareStatement("select last_insert_rowid()");
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "QUERY: {0}", new Object[]{query});
rs.next();
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) {
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());
ps.executeUpdate();
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);
}
}
@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);
}
}
}
}