Controlador.ControladorLecturas.java Source code

Java tutorial

Introduction

Here is the source code for Controlador.ControladorLecturas.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlador;

import org.hibernate.*;
import Modelo.Lecturas;
import Modelo.NewHibernateUtil;
import java.awt.BorderLayout;
import java.util.List;
import java.util.Date;
import javax.swing.table.DefaultTableModel;
import org.hibernate.criterion.Restrictions;
import java.util.Vector;
import javax.swing.JInternalFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYSeries;

/**
 *
 * @author urias
 */
public class ControladorLecturas {

    List<Lecturas> lista;

    public ControladorLecturas(Date desde, Date hasta) {
        SessionFactory sesion = NewHibernateUtil.getSessionFactory();
        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();
        Criteria c = session.createCriteria(Lecturas.class);
        c.add(Restrictions.between("fecha", desde, hasta));
        lista = c.list();
        tx.commit();
        session.close();
    }

    public DefaultTableModel tablaTemperatura() {
        DefaultTableModel tabla = new DefaultTableModel(new Object[][] {}, new String[] { "Hora", "Temperatura" });
        for (Lecturas lectura : lista) {
            tabla.addRow(new Object[] { lectura.getHora(), lectura.getTemperatura() });
        }
        return tabla;
    }

    public Vector vectorTemperatura() {
        Vector data = new Vector(1, 1);
        for (Lecturas lectura : lista) {
            Vector row = new Vector(1, 1);
            row.addElement(lectura.getTemperatura());
            row.addElement(lectura.getHora());
            data.addElement(row);
        }
        return data;
    }

    public DefaultTableModel tablaHumedadAmbiental() {
        DefaultTableModel tabla = new DefaultTableModel(new Object[][] {},
                new String[] { "Hora", "Humedad Ambiental" });
        for (Lecturas lectura : lista) {
            tabla.addRow(new Object[] { lectura.getHora(), lectura.getHumedadambiente() });
        }
        return tabla;
    }

    public Vector vectorHumedadAmbiental() {
        Vector data = new Vector(1, 1);
        for (Lecturas lectura : lista) {
            Vector row = new Vector(1, 1);
            row.addElement(lectura.getHumedadambiente());
            row.addElement(lectura.getHora());
            data.addElement(row);
        }
        return data;
    }

    public DefaultTableModel tablaHumedadSuelo() {
        DefaultTableModel tabla = new DefaultTableModel(new Object[][] {},
                new String[] { "Hora", "Humedad de Suelo" });
        for (Lecturas lectura : lista) {
            tabla.addRow(new Object[] { lectura.getHora(), lectura.getHumedadsuelo() });
        }
        return tabla;
    }

    public Vector vectorHumedadSuelo() {
        Vector data = new Vector(1, 1);
        for (Lecturas lectura : lista) {
            Vector row = new Vector(1, 1);
            row.addElement(lectura.getHumedadsuelo());
            row.addElement(lectura.getHora());
            data.addElement(row);
        }
        return data;
    }

    public Lecturas ultimaLectura() {
        SessionFactory sesion = NewHibernateUtil.getSessionFactory();
        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();
        Criteria c = session.createCriteria(Lecturas.class);
        lista = c.list();
        tx.commit();
        session.close();
        return lista.get(lista.size() - 1);
    }

    public JInternalFrame graficoTemperatura() {
        DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();
        for (Object row : vectorTemperatura()) {
            int grados = Integer.parseInt(((Vector) row).elementAt(0).toString());
            String rowKey = "Sensor 1";
            String columnKey = ((Vector) row).elementAt(1).toString();
            defaultCategoryDataset.addValue(grados, rowKey, columnKey);
        }
        JFreeChart jFreeChart = ChartFactory.createLineChart(null, "Hora", "Grados", defaultCategoryDataset,
                PlotOrientation.VERTICAL, true, true, true);
        ChartPanel chartPanel = new ChartPanel(jFreeChart);
        JInternalFrame jInternalFrame = new JInternalFrame("Grafico de Temperatura Ambiental", true, true, true,
                true);
        jInternalFrame.add(chartPanel, BorderLayout.CENTER);
        jInternalFrame.pack();
        return jInternalFrame;
    }

    public JInternalFrame graficoHumedadAmbiental() {
        DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();
        for (Object row : vectorHumedadAmbiental()) {
            int porcentaje = Integer.parseInt(((Vector) row).elementAt(0).toString());
            String rowKey = "Sensor 1";
            String columnKey = ((Vector) row).elementAt(1).toString();
            defaultCategoryDataset.addValue(porcentaje, rowKey, columnKey);
        }
        JFreeChart jFreeChart = ChartFactory.createLineChart(null, "Hora", "Porcentaje Humedad",
                defaultCategoryDataset, PlotOrientation.VERTICAL, true, true, true);
        ChartPanel chartPanel = new ChartPanel(jFreeChart);
        JInternalFrame jInternalFrame = new JInternalFrame("Grafico de Humedad Ambiental", true, true, true, true);
        jInternalFrame.add(chartPanel, BorderLayout.CENTER);
        jInternalFrame.pack();
        return jInternalFrame;
    }

    public JInternalFrame graficoHumedadSuelo() {
        DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();
        for (Object row : vectorHumedadSuelo()) {
            int grados = Integer.parseInt(((Vector) row).elementAt(0).toString());
            String rowKey = "Sensor 1";
            String columnKey = ((Vector) row).elementAt(1).toString();
            defaultCategoryDataset.addValue(grados, rowKey, columnKey);
        }
        JFreeChart jFreeChart = ChartFactory.createLineChart(null, "Hora", "Humedad", defaultCategoryDataset,
                PlotOrientation.VERTICAL, true, true, true);
        ChartPanel chartPanel = new ChartPanel(jFreeChart);
        JInternalFrame jInternalFrame = new JInternalFrame("Grafico de Humedad del Suelo", true, true, true, true);
        jInternalFrame.add(chartPanel, BorderLayout.CENTER);
        jInternalFrame.pack();
        return jInternalFrame;
    }
}