Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ipartek.formacion.modelo.daos;
- import java.io.IOException;
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Types;
- import java.util.ArrayList;
- import java.util.Properties;
- import org.apache.log4j.Logger;
- import com.ipartek.formacion.modelo.cm.ConnectionManager;
- import com.ipartek.formacion.modelo.pojo.Estadistica;
- public class EstadisticasDAO {
- private final static Logger LOG = Logger.getLogger(EstadisticasDAO.class);
- private static EstadisticasDAO INSTANCE = null;
- private static final String SQL_OBJETIVO_ANUAL = "SELECT objetivo FROM objetivo where Anio=?;";
- private static final String SQL_DEVUELVE_TOTALES = "{call pa_multa_devuelveTotales(?,?,?,?,?)}";
- private static final String SQL_TOTAL_MESES_ANIO = "SELECT ROUND(SUM(importe), 2), MONTH(fecha_alta) FROM multa WHERE id_agente = ? AND YEAR(fecha_alta) = ? AND fecha_baja IS NULL GROUP BY MONTH(fecha_alta) ORDER BY MONTH(fecha_alta);";
- private ArrayList<Estadistica> estadisticas;
- private EstadisticasDAO() {
- super();
- }
- public synchronized static EstadisticasDAO getInstance() {
- if (INSTANCE == null) {
- INSTANCE = new EstadisticasDAO();
- }
- return INSTANCE;
- }
- public Integer getObjetivoAnual(int Anio) {
- Integer objetivo = null;
- try (Connection conn = ConnectionManager.getConnection();
- PreparedStatement pst = conn.prepareStatement(SQL_OBJETIVO_ANUAL)) {
- pst.setLong(1, Anio);
- try (ResultSet rs = pst.executeQuery()) {
- while (rs.next()) {
- objetivo = rs.getInt("Objetivo");
- }
- } catch (Exception e) {
- LOG.error(e);
- }
- } catch (Exception e) {
- LOG.error(e);
- }
- return objetivo;
- }
- public Float getTotales(String opcion, Long idAgente, int Mes, int Anio) throws SQLException {
- Float recaudacion = null;
- try (Connection conn = ConnectionManager.getConnection();
- CallableStatement cs = conn.prepareCall(SQL_DEVUELVE_TOTALES);) {
- cs.setString(1, opcion);
- cs.setLong(2, idAgente);
- cs.setInt(3, Anio);
- cs.setInt(4, Mes);
- cs.registerOutParameter(5, Types.FLOAT);
- int affectedRows = cs.executeUpdate();
- if (affectedRows == 1) {
- recaudacion = cs.getFloat(5);
- }
- }
- return recaudacion;
- }
- public ArrayList<Estadistica> getArrayCompleto(Long idagente, int anyo) throws IOException {
- Properties prop = new Properties();
- prop.load(ConnectionManager.class.getClassLoader().getResourceAsStream("meses.properties"));
- estadisticas = new ArrayList<Estadistica>();
- try (Connection conn = ConnectionManager.getConnection(); PreparedStatement pst = conn.prepareStatement(SQL_TOTAL_MESES_ANIO)) {
- pst.setFloat(1, idagente);
- pst.setInt(2, anyo);
- try (ResultSet rs = pst.executeQuery()) {
- rs.next();
- for(int i = 1; i <= 12; i++) { // i = posiciones de las filas. 1 = Enero, 2 = Febrero, etc.
- try {
- if (i == rs.getInt(2)) {
- // TODO redondear Floats a 2 decimales
- estadisticas.add(new Estadistica(prop.getProperty(String.valueOf(i)), rs.getFloat(1)));
- rs.next();
- }
- else {
- estadisticas.add(new Estadistica(prop.getProperty(String.valueOf(i))));
- }
- }
- catch (Exception e) {
- estadisticas.add(new Estadistica(prop.getProperty(String.valueOf(i))));
- }
- }
- }
- catch (Exception e) {
- LOG.error(e);
- }
- } catch (Exception e) {
- LOG.error(e);
- }
- finally {
- }
- return estadisticas;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement