SondaFactory.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */


  20. package org.openspcoop2.utils.sonde;

  21. import java.lang.reflect.Constructor;
  22. import java.lang.reflect.InvocationTargetException;
  23. import java.sql.Connection;
  24. import java.sql.PreparedStatement;
  25. import java.sql.ResultSet;
  26. import java.sql.SQLException;
  27. import java.sql.Types;
  28. import java.util.ArrayList;
  29. import java.util.Date;
  30. import java.util.List;

  31. import org.openspcoop2.utils.TipiDatabase;
  32. import org.openspcoop2.utils.sql.ISQLQueryObject;
  33. import org.openspcoop2.utils.sql.SQLObjectFactory;
  34. import org.openspcoop2.utils.sql.SQLQueryObjectException;

  35. /**
  36.  * Classe di implementazione della Factory per le Sonde
  37.  *
  38.  *
  39.  * @author Bussu Giovanni (bussu@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */

  43. public class SondaFactory {

  44.     /**
  45.      * Aggiorna la configurazione della sonda identificata dal parametro nome
  46.      * @param nome nome della sonda da aggiornare
  47.      * @param warn soglia di warning da impostare
  48.      * @param err soglia di error da impostare
  49.      * @param connection connessione al database
  50.      * @param tipoDatabase tipo di database
  51.      * @throws Exception
  52.      */
  53.     public static void updateConfSonda(String nome, long warn, long err, Connection connection, TipiDatabase tipoDatabase) throws Exception {
  54.         ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);

  55.         sqlQueryObject.addUpdateTable("sonde");
  56.         sqlQueryObject.addUpdateField("soglia_warn", "?");
  57.         sqlQueryObject.addUpdateField("soglia_error", "?");
  58.         sqlQueryObject.addWhereCondition("nome = ?");
  59.         String sql = sqlQueryObject.createSQLUpdate();

  60.         PreparedStatement ps = connection.prepareStatement(sql);
  61.         try {
  62.             ps.setLong(1, warn);
  63.             ps.setLong(2, err);
  64.             ps.setString(3, nome);
  65.             ps.executeUpdate();
  66.         }finally {
  67.             try {
  68.                 if(ps!=null) {
  69.                     ps.close();
  70.                 }
  71.             }catch(Throwable t) {
  72.                 // ignore
  73.             }
  74.         }
  75.     }

  76.     /**
  77.      * Aggiorna lo stato della sonda identificata dal parametro nome
  78.      * @param nome nome della sonda da aggiornare
  79.      * @param sonda parametri con cui aggiornare la sonda
  80.      * @param connection connessione al database
  81.      * @param tipoDatabase tipo di database
  82.      * @throws SondaException
  83.      */
  84.     public static void updateStatoSonda(String nome, Sonda sonda, Connection connection, TipiDatabase tipoDatabase) throws SondaException {
  85.         try {
  86.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);

  87.             sqlQueryObject.addUpdateTable("sonde");
  88.             sqlQueryObject.addUpdateField("data_ok", "?");
  89.             sqlQueryObject.addUpdateField("data_warn", "?");
  90.             sqlQueryObject.addUpdateField("data_error", "?");
  91.             sqlQueryObject.addUpdateField("data_ultimo_check", "?");
  92.             sqlQueryObject.addUpdateField("dati_check", "?");
  93.             sqlQueryObject.addUpdateField("stato_ultimo_check", "?");
  94.             sqlQueryObject.addWhereCondition("nome = ?");
  95.             String sql = sqlQueryObject.createSQLUpdate();

  96.             PreparedStatement ps = null;
  97.             try {
  98.                 ps = connection.prepareStatement(sql);
  99.                 int i = 1;
  100.                 if(sonda.getParam().getDataOk()!= null) {
  101.                     ps.setTimestamp(i++, new java.sql.Timestamp(sonda.getParam().getDataOk().getTime()));
  102.                 } else {
  103.                     ps.setNull(i++, Types.TIMESTAMP);
  104.                 }
  105.                 if(sonda.getParam().getDataWarn()!= null) {
  106.                     ps.setTimestamp(i++, new java.sql.Timestamp(sonda.getParam().getDataWarn().getTime()));
  107.                 } else {
  108.                     ps.setNull(i++, Types.TIMESTAMP);
  109.                 }
  110.                 if(sonda.getParam().getDataError()!= null) {
  111.                     ps.setTimestamp(i++, new java.sql.Timestamp(sonda.getParam().getDataError().getTime()));
  112.                 } else {
  113.                     ps.setNull(i++, Types.TIMESTAMP);
  114.                 }
  115.                 ps.setTimestamp(i++, new java.sql.Timestamp(sonda.getParam().getDataUltimoCheck().getTime()));
  116.                 ps.setString(i++, sonda.getParam().marshallDatiCheck());
  117.                 ps.setInt(i++, sonda.getParam().getStatoUltimoCheck());
  118.                 ps.setString(i++, nome);
  119.                 ps.executeUpdate();
  120.             } finally {
  121.                 if(ps != null) {
  122.                     try {ps.close();} catch(Exception e) {}
  123.                 }
  124.             }
  125.         } catch(Exception e) {
  126.             throw new SondaException(e);
  127.         }
  128.     }

  129.     /**
  130.      * @param nome nome della sonda da recuperare
  131.      * @param connection connessione al database
  132.      * @param tipoDatabase tipo di database
  133.      * @return la sonda identificata dal parametro nome
  134.      * @throws SondaException
  135.      */
  136.     public static Sonda get(String nome, Connection connection, TipiDatabase tipoDatabase) throws SondaException {
  137.         try {
  138.             ISQLQueryObject sqlQueryObject = getSqlQueryObjectForGetSonda(tipoDatabase);

  139.             sqlQueryObject.addWhereCondition("nome = ?");
  140.             String sql = sqlQueryObject.createSQLQuery();

  141.             PreparedStatement ps = null;
  142.             ResultSet rs = null;
  143.             try {
  144.                 ps = connection.prepareStatement(sql);
  145.                 ps.setString(1, nome);
  146.                 rs = ps.executeQuery();

  147.                 if(rs.next()) {
  148.                     return getSonda(rs);
  149.                 }
  150.             } finally {
  151.                 if(rs != null) {
  152.                     try {rs.close();} catch(Exception e) {}
  153.                 }
  154.                 if(ps != null) {
  155.                     try {ps.close();} catch(Exception e) {}
  156.                 }
  157.             }
  158.             return null;
  159.         } catch(Exception e) {
  160.             throw new SondaException(e);
  161.         }
  162.     }

  163.     /**
  164.      * @param connection connessione al database
  165.      * @param tipoDatabase tipo di database
  166.      * @return la lista di tutte le sonde presenti nel db
  167.      * @throws SondaException
  168.      */
  169.     public static List<Sonda> findAll(Connection connection, TipiDatabase tipoDatabase) throws SondaException {
  170.         try {
  171.             List<Sonda> sondaLst = new ArrayList<Sonda>();

  172.             ISQLQueryObject sqlQueryObject = getSqlQueryObjectForGetSonda(tipoDatabase);

  173.             String sql = sqlQueryObject.createSQLQuery();

  174.             PreparedStatement ps = null;
  175.             ResultSet rs = null;
  176.             try {
  177.                 ps = connection.prepareStatement(sql);
  178.                 rs = ps.executeQuery();
  179.                 while(rs.next()) {
  180.                     Sonda sonda = getSonda(rs);
  181.                     sondaLst.add(sonda);
  182.                 }
  183.             } finally {
  184.                 if(rs != null) {
  185.                     try {rs.close();} catch(Exception e) {}
  186.                 }
  187.                 if(ps != null) {
  188.                     try {ps.close();} catch(Exception e) {}
  189.                 }
  190.             }
  191.             return sondaLst;
  192.         } catch(Exception e) {
  193.             throw new SondaException(e);
  194.         }
  195.     }

  196.     private static Sonda getSonda(ResultSet rs) throws SQLException,
  197.     NoSuchMethodException, SondaException,
  198.     InstantiationException, IllegalAccessException,
  199.     InvocationTargetException {
  200.         String nome = rs.getString("nome");
  201.         String classe = rs.getString("classe");
  202.         Long soglia_warn = rs.getLong("soglia_warn");
  203.         Long soglia_error = rs.getLong("soglia_error");
  204.         Date data_ok = rs.getTimestamp("data_ok");
  205.         Date data_warn = rs.getTimestamp("data_warn");
  206.         Date data_error = rs.getTimestamp("data_error");
  207.         Date data_ultimo_check = rs.getTimestamp("data_ultimo_check");
  208.         String dati_check = rs.getString("dati_check");
  209.         Integer stato_ultimo_check = rs.getInt("stato_ultimo_check");
  210.         Class<?> className = null;
  211.         try {
  212.             className = (Class<?>) Class.forName(classe);
  213.         } catch(ClassNotFoundException e) {
  214.             throw new SondaException("Classe di definizione della sonda ["+classe+"] non trovata");
  215.         }
  216.         if(!Sonda.class.isAssignableFrom(className)) {
  217.             throw new SondaException("Classe di definizione della sonda ["+classe+"] deve essere un'estensione della classe ["+Sonda.class.getName()+"]");
  218.         }
  219.         Constructor<?> ctor = className.getDeclaredConstructor(ParametriSonda.class);
  220.         ctor.setAccessible(true);
  221.         ParametriSonda param = new ParametriSonda();

  222.         param.setNome(nome);

  223.         param.setDataUltimoCheck(data_ultimo_check);

  224.         param.setDataError(data_error);
  225.         param.setDataWarn(data_warn);
  226.         param.setDataOk(data_ok);

  227.         param.unmarshallDatiCheck(dati_check);

  228.         param.setSogliaWarn(soglia_warn);
  229.         param.setSogliaError(soglia_error);

  230.         param.setStatoUltimoCheck(stato_ultimo_check);

  231.         Sonda sonda = (Sonda)ctor.newInstance(param);
  232.         return sonda;
  233.     }

  234.     private static ISQLQueryObject getSqlQueryObjectForGetSonda(TipiDatabase tipoDatabase) throws SQLQueryObjectException {
  235.         ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);

  236.         sqlQueryObject.addFromTable("sonde");
  237.         sqlQueryObject.addSelectField("nome");
  238.         sqlQueryObject.addSelectField("classe");
  239.         sqlQueryObject.addSelectField("soglia_warn");
  240.         sqlQueryObject.addSelectField("soglia_error");
  241.         sqlQueryObject.addSelectField("data_ok");
  242.         sqlQueryObject.addSelectField("data_warn");
  243.         sqlQueryObject.addSelectField("data_error");
  244.         sqlQueryObject.addSelectField("data_ultimo_check");
  245.         sqlQueryObject.addSelectField("dati_check");
  246.         sqlQueryObject.addSelectField("stato_ultimo_check");
  247.         return sqlQueryObject;
  248.     }

  249. }