Advertisement
Hector2244

DAO

Jan 17th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. package com.ipartek.formacion.modelo.daos;
  2.  
  3. import java.io.IOException;
  4. import java.sql.CallableStatement;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Types;
  10. import java.util.ArrayList;
  11. import java.util.Properties;
  12.  
  13. import org.apache.log4j.Logger;
  14.  
  15. import com.ipartek.formacion.modelo.cm.ConnectionManager;
  16. import com.ipartek.formacion.modelo.pojo.Estadistica;
  17.  
  18. public class EstadisticasDAO {
  19.     private final static Logger LOG = Logger.getLogger(EstadisticasDAO.class);
  20.     private static EstadisticasDAO INSTANCE = null;
  21.     private static final String SQL_OBJETIVO_ANUAL = "SELECT objetivo FROM objetivo where Anio=?;";
  22.     private static final String SQL_DEVUELVE_TOTALES = "{call pa_multa_devuelveTotales(?,?,?,?,?)}";
  23.     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);";
  24.     private ArrayList<Estadistica> estadisticas;
  25.  
  26.     private EstadisticasDAO() {
  27.         super();
  28.     }
  29.  
  30.     public synchronized static EstadisticasDAO getInstance() {
  31.  
  32.         if (INSTANCE == null) {
  33.             INSTANCE = new EstadisticasDAO();
  34.         }
  35.         return INSTANCE;
  36.     }
  37.  
  38.     public Integer getObjetivoAnual(int Anio) {
  39.         Integer objetivo = null;
  40.         try (Connection conn = ConnectionManager.getConnection();
  41.                 PreparedStatement pst = conn.prepareStatement(SQL_OBJETIVO_ANUAL)) {
  42.             pst.setLong(1, Anio);
  43.             try (ResultSet rs = pst.executeQuery()) {
  44.                 while (rs.next()) {
  45.                     objetivo = rs.getInt("Objetivo");
  46.                 }
  47.             } catch (Exception e) {
  48.                 LOG.error(e);
  49.             }
  50.         } catch (Exception e) {
  51.             LOG.error(e);
  52.         }
  53.         return objetivo;
  54.     }
  55.  
  56.     public Float getTotales(String opcion, Long idAgente, int Mes, int Anio) throws SQLException {
  57.         Float recaudacion = null;
  58.         try (Connection conn = ConnectionManager.getConnection();
  59.                 CallableStatement cs = conn.prepareCall(SQL_DEVUELVE_TOTALES);) {
  60.  
  61.             cs.setString(1, opcion);
  62.             cs.setLong(2, idAgente);
  63.             cs.setInt(3, Anio);
  64.             cs.setInt(4, Mes);
  65.             cs.registerOutParameter(5, Types.FLOAT);
  66.             int affectedRows = cs.executeUpdate();
  67.             if (affectedRows == 1) {
  68.                 recaudacion = cs.getFloat(5);
  69.             }
  70.  
  71.         }
  72.         return recaudacion;
  73.     }
  74.    
  75.     public ArrayList<Estadistica> getArrayCompleto(Long idagente, int anyo) throws IOException {
  76.         Properties prop = new Properties();        
  77.         prop.load(ConnectionManager.class.getClassLoader().getResourceAsStream("meses.properties"));
  78.        
  79.         estadisticas = new ArrayList<Estadistica>();
  80.        
  81.         try (Connection conn = ConnectionManager.getConnection(); PreparedStatement pst = conn.prepareStatement(SQL_TOTAL_MESES_ANIO)) {
  82.             pst.setFloat(1, idagente);
  83.             pst.setInt(2, anyo);
  84.             try (ResultSet rs = pst.executeQuery()) {
  85.                 rs.next();
  86.                 for(int i = 1; i <= 12; i++) { // i = posiciones de las filas. 1 = Enero, 2 = Febrero, etc.
  87.                     try {
  88.                         if (i == rs.getInt(2)) {  
  89.                             // TODO redondear Floats a 2 decimales
  90.                             estadisticas.add(new Estadistica(prop.getProperty(String.valueOf(i)), rs.getFloat(1)));
  91.                             rs.next();
  92.                         }
  93.                         else {
  94.                             estadisticas.add(new Estadistica(prop.getProperty(String.valueOf(i))));
  95.                         }
  96.                     }
  97.                     catch (Exception e) {
  98.                         estadisticas.add(new Estadistica(prop.getProperty(String.valueOf(i))));
  99.                     }
  100.                 }
  101.             }
  102.             catch (Exception e) {
  103.                 LOG.error(e);
  104.             }
  105.         } catch (Exception e) {
  106.             LOG.error(e);
  107.         }
  108.         finally {  
  109.         }
  110.         return estadisticas;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement