ConfigurazionePdD_allarmi.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.pdd.config;

  21. import java.sql.Connection;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.openspcoop2.core.allarmi.Allarme;
  25. import org.openspcoop2.core.allarmi.AllarmeParametro;
  26. import org.openspcoop2.core.allarmi.IdAllarme;
  27. import org.openspcoop2.core.allarmi.constants.RuoloPorta;
  28. import org.openspcoop2.core.allarmi.constants.StatoAllarme;
  29. import org.openspcoop2.core.allarmi.dao.IAllarmeServiceSearch;
  30. import org.openspcoop2.core.allarmi.utils.AllarmiConverterUtils;
  31. import org.openspcoop2.core.allarmi.utils.FiltroRicercaAllarmi;
  32. import org.openspcoop2.core.commons.dao.DAOFactory;
  33. import org.openspcoop2.core.config.constants.CostantiConfigurazione;
  34. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  35. import org.openspcoop2.core.config.driver.DriverConfigurazioneNotFound;
  36. import org.openspcoop2.core.config.driver.db.DriverConfigurazioneDB;
  37. import org.openspcoop2.generic_project.exception.NotFoundException;
  38. import org.openspcoop2.generic_project.expression.IPaginatedExpression;
  39. import org.openspcoop2.generic_project.expression.LikeMode;
  40. import org.openspcoop2.generic_project.expression.SortOrder;
  41. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  42. import org.openspcoop2.monitor.engine.alarm.AlarmImpl;
  43. import org.openspcoop2.monitor.engine.alarm.AlarmManager;
  44. import org.openspcoop2.monitor.sdk.alarm.AlarmStatus;
  45. import org.openspcoop2.monitor.sdk.alarm.IAlarm;
  46. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;

  47. /**    
  48.  * ConfigurazionePdD_controlloTraffico
  49.  *
  50.  * @author Poli Andrea (poli@link.it)
  51.  * @author $Author$
  52.  * @version $Rev$, $Date$
  53.  */
  54. public class ConfigurazionePdD_allarmi extends AbstractConfigurazionePdDConnectionResourceManager {

  55.     private ServiceManagerProperties smp;
  56.    
  57.     public ConfigurazionePdD_allarmi(OpenSPCoop2Properties openspcoopProperties, DriverConfigurazioneDB driver, boolean useConnectionPdD) {
  58.         super(openspcoopProperties, driver, useConnectionPdD, OpenSPCoop2Logger.getLoggerOpenSPCoopAllarmiSql(openspcoopProperties.isAllarmiDebug()));
  59.            
  60.         this.smp = new ServiceManagerProperties();
  61.         this.smp.setShowSql(this.openspcoopProperties.isAllarmiDebug());
  62.         this.smp.setDatabaseType(this.driver.getTipoDB());
  63.     }
  64.    
  65.     public Allarme getAllarme(Connection connectionPdD, String nomeAllarme) throws DriverConfigurazioneException, DriverConfigurazioneNotFound {
  66.        
  67.         Allarme allarme = null;
  68.        
  69.         ConfigurazionePdDConnectionResource cr = null;
  70.         try{
  71.             cr = this.getConnection(connectionPdD, "Allarmi.searchAllarmi");
  72.             org.openspcoop2.core.allarmi.dao.IServiceManager sm =
  73.                     (org.openspcoop2.core.allarmi.dao.IServiceManager) DAOFactory.getInstance(this.log).
  74.                     getServiceManager(org.openspcoop2.core.allarmi.utils.ProjectInfo.getInstance(),
  75.                             cr.connectionDB,this.smp,this.log);
  76.        
  77.             IdAllarme id = new IdAllarme();
  78.             id.setNome(nomeAllarme);
  79.             allarme = sm.getAllarmeServiceSearch().get(id);
  80.                        
  81.         }
  82.         catch(NotFoundException e){
  83.             String errorMsg = "Allarme '"+nomeAllarme+"' non trovato";
  84.             this.log.debug(errorMsg,e);
  85.             throw new DriverConfigurazioneNotFound(errorMsg,e);
  86.         }
  87.         catch(Exception e){
  88.             String errorMsg = "Errore durante la ricerca dell'allarme '"+nomeAllarme+"': "+e.getMessage();
  89.             this.log.error(errorMsg,e);
  90.             throw new DriverConfigurazioneException(errorMsg,e);
  91.         }
  92.         finally {
  93.             this.releaseConnection(cr);
  94.         }

  95.         if(allarme!=null) {
  96.             return allarme;
  97.         }
  98.         throw new DriverConfigurazioneNotFound("Allarme '"+nomeAllarme+"' non trovato");
  99.     }
  100.    
  101.    
  102.     public List<Allarme> searchAllarmi(Connection connectionPdD, FiltroRicercaAllarmi filtroRicerca) throws DriverConfigurazioneException, DriverConfigurazioneNotFound {
  103.        
  104.         List<Allarme> list = new ArrayList<Allarme>();
  105.                
  106.         ConfigurazionePdDConnectionResource cr = null;
  107.         try{
  108.             cr = this.getConnection(connectionPdD, "Allarmi.searchAllarmi");
  109.             org.openspcoop2.core.allarmi.dao.IServiceManager sm =
  110.                     (org.openspcoop2.core.allarmi.dao.IServiceManager) DAOFactory.getInstance(this.log).
  111.                     getServiceManager(org.openspcoop2.core.allarmi.utils.ProjectInfo.getInstance(),
  112.                             cr.connectionDB,this.smp,this.log);
  113.            
  114.             String tipo = filtroRicerca.getTipo();
  115.             if(tipo==null || "".equals(tipo)) {
  116.                 throw new Exception("Tipo allarme non fornito");
  117.             }
  118.            
  119.             String idParametroCluster = filtroRicerca.getIdParametroCluster();
  120.             String idCluster = filtroRicerca.getIdCluster();
  121.             boolean idClusterOpzionale = filtroRicerca.isIdClusterOpzionale();
  122.            
  123.             String idParametro = filtroRicerca.getIdParametro();
  124.             String valoreParametro = filtroRicerca.getValoreParametro();
  125.            
  126.             boolean recuperaSoloAllarmiInStatoDiversoDaOk = filtroRicerca.isRecuperaSoloAllarmiInStatoDiversoDaOk();
  127.        
  128.             Boolean globale = filtroRicerca.getGlobale();
  129.             RuoloPorta ruoloPorta = null;
  130.             String nomePorta = null;
  131.             if(globale!=null && !globale) {
  132.                 ruoloPorta = filtroRicerca.getRuoloPorta();
  133.                 nomePorta = filtroRicerca.getNomePorta();
  134.             }
  135.            
  136.             IAllarmeServiceSearch allarmeServiceSearch = sm.getAllarmeServiceSearch();
  137.        
  138.             IPaginatedExpression expr = allarmeServiceSearch.newPaginatedExpression();
  139.             expr.limit(1000); // numero eccessivo
  140.             expr.addOrder(Allarme.model().NOME, SortOrder.ASC);
  141.             expr.and();
  142.             expr.equals(Allarme.model().TIPO, tipo);
  143.             expr.equals(Allarme.model().ENABLED, 1);
  144.            
  145.             if(globale!=null) {
  146.                 if(globale) {
  147.                     expr.isNull(Allarme.model().FILTRO.NOME_PORTA);
  148.                 }
  149.                 else {
  150.                     expr.equals(Allarme.model().FILTRO.RUOLO_PORTA, ruoloPorta.getValue()).and().equals(Allarme.model().FILTRO.NOME_PORTA, nomePorta);
  151.                 }
  152.             }
  153.            
  154.             if(recuperaSoloAllarmiInStatoDiversoDaOk) {
  155.                 expr.notEquals(Allarme.model().STATO, AllarmiConverterUtils.toIntegerValue(StatoAllarme.OK));
  156.             }
  157.            
  158.             // AVENDO DUE PARAMETRI IN GIOCO, LA QUERY VIENE SBAGLIATA. DOVREI GENERARE DUE JOIN CON DIFFERENTE ALIAS
  159.             // IL CONTROLLO DEL CLUSTER LO FACCIO APPLICATIVO (tanto sono un numero limitato)
  160.     //      if(idParametroCluster!=null) {
  161.     //          expr.equals(ConfigurazioneAllarme.model().CONFIGURAZIONE_ALLARME_PARAMETRO.ID_PARAMETRO, idParametroCluster);
  162.     //          if(idCluster!=null) {
  163.     //              expr.like(ConfigurazioneAllarme.model().CONFIGURAZIONE_ALLARME_PARAMETRO.VALORE, idCluster,LikeMode.EXACT);
  164.     //          }
  165.     //          else {
  166.     //              expr.isNull(ConfigurazioneAllarme.model().CONFIGURAZIONE_ALLARME_PARAMETRO.VALORE);
  167.     //          }
  168.     //      }
  169.             if(idParametro!=null) {
  170.                 expr.equals(Allarme.model().ALLARME_PARAMETRO.ID_PARAMETRO, idParametro);
  171.                 if(valoreParametro!=null) {
  172.                     expr.like(Allarme.model().ALLARME_PARAMETRO.VALORE, valoreParametro,LikeMode.EXACT);
  173.                 }
  174.                 else {
  175.                     expr.isNull(Allarme.model().ALLARME_PARAMETRO.VALORE);
  176.                 }
  177.             }
  178.            
  179.             List<Allarme> l = allarmeServiceSearch.findAll(expr);
  180.             if(l!=null && l.size()>0){
  181.                 for (Allarme configurazioneAllarme : l) {
  182.                     boolean add = true;
  183.                     if(idParametroCluster!=null) {
  184.                         for (AllarmeParametro configurazioneAllarmeParametro : configurazioneAllarme.getAllarmeParametroList()) {
  185.                             if(idParametroCluster.equals(configurazioneAllarmeParametro.getIdParametro())) {
  186.                                 if(idCluster!=null) {
  187.                                     if(idCluster.equals(configurazioneAllarmeParametro.getValore())==false) {
  188.                                         if(idClusterOpzionale) {
  189.                                             if(configurazioneAllarmeParametro.getValore()!=null &&
  190.                                                     !"".equals(configurazioneAllarmeParametro.getValore()) &&
  191.                                                     !CostantiConfigurazione.CLUSTER_ID_NON_DEFINITO.equals(configurazioneAllarmeParametro.getValore())) {
  192.                                                 add=false;
  193.                                             }
  194.                                         }
  195.                                         else {
  196.                                             add=false;
  197.                                         }
  198.                                     }
  199.                                 }
  200.                                 else {
  201.                                     if(idClusterOpzionale) {
  202.                                         if(configurazioneAllarmeParametro.getValore()!=null &&
  203.                                                 !"".equals(configurazioneAllarmeParametro.getValore()) &&
  204.                                                 !CostantiConfigurazione.CLUSTER_ID_NON_DEFINITO.equals(configurazioneAllarmeParametro.getValore())) {
  205.                                             add=false;
  206.                                         }
  207.                                     }
  208.                                     else {
  209.                                         add=false;
  210.                                     }
  211.                                 }
  212.                                 break;
  213.                             }
  214.                         }
  215.                     }
  216.                     if(add) {
  217.                         list.add(configurazioneAllarme);
  218.                     }
  219.                 }
  220.             }
  221.         }
  222.         catch(Exception e){
  223.             String errorMsg = "Errore durante la ricerca degli allarmi: "+e.getMessage();
  224.             this.log.error(errorMsg,e);
  225.             throw new DriverConfigurazioneException(errorMsg,e);
  226.         }
  227.         finally {
  228.             this.releaseConnection(cr);
  229.         }

  230.         if(list!=null && !list.isEmpty()) {
  231.             return list;
  232.         }
  233.         throw new DriverConfigurazioneNotFound("Allarmi non trovati");
  234.     }

  235.    
  236.     public List<IAlarm> instanceAllarmi(Connection connectionPdD, List<Allarme> listAllarmi) throws DriverConfigurazioneException {
  237.        
  238.         ConfigurazionePdDConnectionResource cr = null;
  239.         try{
  240.             cr = this.getConnection(connectionPdD, "Allarmi.instanceAllarmi");
  241.             DAOFactory daoFactory = DAOFactory.getInstance(this.log);
  242.             org.openspcoop2.core.plugins.dao.IServiceManager smPlugins =
  243.                     (org.openspcoop2.core.plugins.dao.IServiceManager) daoFactory.
  244.                     getServiceManager(org.openspcoop2.core.plugins.utils.ProjectInfo.getInstance(),
  245.                             cr.connectionDB,this.smp,this.log);
  246.            
  247.             if(listAllarmi==null || listAllarmi.isEmpty()) {
  248.                 throw new Exception("Non sono stati forniti allarmi da istanziare");
  249.             }
  250.            
  251.             List<IAlarm> list = new ArrayList<IAlarm>();
  252.             for (Allarme allarme : listAllarmi) {
  253.                 list.add(AlarmManager.getAlarm(allarme, this.log, daoFactory, smPlugins));
  254.             }
  255.            
  256.             return list;
  257.         }
  258.         catch(Exception e){
  259.             String errorMsg = "Errore durante l'istanziazione degli allarmi: "+e.getMessage();
  260.             this.log.error(errorMsg,e);
  261.             throw new DriverConfigurazioneException(errorMsg,e);
  262.         }
  263.         finally {
  264.             this.releaseConnection(cr);
  265.         }

  266.     }
  267.    
  268.     public boolean changeStatus(Connection connectionPdD,  AlarmImpl alarm, AlarmStatus nuovoStatoAllarme) throws DriverConfigurazioneException {
  269.         return _changeStatus(connectionPdD, alarm, nuovoStatoAllarme);
  270.     }
  271.     public boolean changeStatus(Connection connectionPdD,  IAlarm alarm, AlarmStatus nuovoStatoAllarme) throws DriverConfigurazioneException {
  272.         return _changeStatus(connectionPdD, alarm, nuovoStatoAllarme);
  273.     }
  274.     private boolean _changeStatus(Connection connectionPdD,  IAlarm alarm, AlarmStatus nuovoStatoAllarme) throws DriverConfigurazioneException {
  275.         ConfigurazionePdDConnectionResource cr = null;
  276.         try{
  277.             cr = this.getConnection(connectionPdD, "Allarmi.changeStato");
  278.                
  279.             if(alarm instanceof AlarmImpl) {
  280.                 ((AlarmImpl)alarm).changeStatus(this.log, cr.connectionDB, this.smp,nuovoStatoAllarme);
  281.             }
  282.             else {
  283.                 alarm.changeStatus(nuovoStatoAllarme);
  284.             }
  285.             return true; // serve solo per passare dai metodi "cache" della configurazione; il valore true/false non viene usato
  286.         }
  287.         catch(Exception e){
  288.             String errorMsg = "Errore durante update stato degli allarmi: "+e.getMessage();
  289.             this.log.error(errorMsg,e);
  290.             throw new DriverConfigurazioneException(errorMsg,e);
  291.         }
  292.         finally {
  293.             this.releaseConnection(cr);
  294.         }
  295.     }
  296.    
  297.    
  298. }