ConfigurazioneAllarmiChange.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.web.ctrlstat.servlet.config;

  21. import java.text.MessageFormat;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.List;

  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpServletResponse;
  27. import javax.servlet.http.HttpSession;

  28. import org.apache.struts.action.Action;
  29. import org.apache.struts.action.ActionForm;
  30. import org.apache.struts.action.ActionForward;
  31. import org.apache.struts.action.ActionMapping;
  32. import org.openspcoop2.core.allarmi.AllarmeHistory;
  33. import org.openspcoop2.core.allarmi.AllarmeParametro;
  34. import org.openspcoop2.core.allarmi.constants.RuoloPorta;
  35. import org.openspcoop2.core.allarmi.constants.StatoAllarme;
  36. import org.openspcoop2.core.allarmi.constants.TipoAllarme;
  37. import org.openspcoop2.core.allarmi.utils.AllarmiConverterUtils;
  38. import org.openspcoop2.core.commons.Filtri;
  39. import org.openspcoop2.core.commons.Liste;
  40. import org.openspcoop2.core.config.PortaApplicativa;
  41. import org.openspcoop2.core.config.PortaDelegata;
  42. import org.openspcoop2.core.id.IDPortaApplicativa;
  43. import org.openspcoop2.core.id.IDPortaDelegata;
  44. import org.openspcoop2.core.plugins.Plugin;
  45. import org.openspcoop2.core.plugins.constants.TipoPlugin;
  46. import org.openspcoop2.message.constants.ServiceBinding;
  47. import org.openspcoop2.monitor.engine.alarm.AlarmEngineConfig;
  48. import org.openspcoop2.monitor.engine.alarm.utils.AllarmiUtils;
  49. import org.openspcoop2.monitor.engine.alarm.wrapper.ConfigurazioneAllarmeBean;
  50. import org.openspcoop2.monitor.engine.dynamic.DynamicFactory;
  51. import org.openspcoop2.monitor.engine.dynamic.IDynamicLoader;
  52. import org.openspcoop2.monitor.sdk.condition.Context;
  53. import org.openspcoop2.monitor.sdk.plugins.IAlarmProcessing;
  54. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  55. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  56. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  57. import org.openspcoop2.web.ctrlstat.servlet.pa.PorteApplicativeCore;
  58. import org.openspcoop2.web.ctrlstat.servlet.pd.PorteDelegateCore;
  59. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  60. import org.openspcoop2.web.lib.mvc.DataElement;
  61. import org.openspcoop2.web.lib.mvc.ForwardParams;
  62. import org.openspcoop2.web.lib.mvc.GeneralData;
  63. import org.openspcoop2.web.lib.mvc.PageData;
  64. import org.openspcoop2.web.lib.mvc.Parameter;
  65. import org.openspcoop2.web.lib.mvc.ServletUtils;
  66. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  67. /**    
  68.  * ConfigurazioneAllarmiChange
  69.  *
  70.  * @author Pintori Giuliano (pintori@link.it)
  71.  * @author $Author$
  72.  * @version $Rev$, $Date$
  73.  */
  74. public class ConfigurazioneAllarmiChange extends Action {

  75.     @Override
  76.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

  77.         HttpSession session = request.getSession(true);

  78.         // Inizializzo PageData
  79.         PageData pd = new PageData();

  80.         GeneralHelper generalHelper = new GeneralHelper(session);

  81.         // Inizializzo GeneralData
  82.         GeneralData gd = generalHelper.initGeneralData(request);

  83.         String userLogin = ServletUtils.getUserLoginFromSession(session);  
  84.        
  85.         TipoOperazione tipoOperazione = TipoOperazione.CHANGE;
  86.        
  87.         String pluginSelectedExceptionMessage = null;

  88.         try {
  89.             StringBuilder sbParsingError = new StringBuilder();
  90.            
  91.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);
  92.            
  93.             // controllo primo accesso
  94.             boolean first = confHelper.isFirstTimeFromHttpParameters(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_FIRST_TIME);
  95.            
  96.             String idAllarmeS = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_ID_ALLARME);
  97.            
  98.             String ruoloPortaParam = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_RUOLO_PORTA);
  99.             RuoloPorta ruoloPorta = null;
  100.             if(ruoloPortaParam!=null) {
  101.                 ruoloPorta = RuoloPorta.toEnumConstant(ruoloPortaParam);
  102.             }
  103.             String nomePorta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_NOME_PORTA);
  104.             ServiceBinding serviceBinding = null;
  105.             String serviceBindingParam = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_SERVICE_BINDING);
  106.             if(serviceBindingParam!=null && !"".equals(serviceBindingParam)) {
  107.                 serviceBinding = ServiceBinding.valueOf(serviceBindingParam);
  108.             }
  109.             String nomePlugin = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_PLUGIN);
  110.            
  111.             ConfigurazioneCore confCore = new ConfigurazioneCore();
  112.             SoggettiCore soggettiCore = new SoggettiCore(confCore);
  113.             PorteDelegateCore pdCore = new PorteDelegateCore(confCore);
  114.             PorteApplicativeCore paCore = new PorteApplicativeCore(confCore);
  115.            
  116.             AlarmEngineConfig alarmEngineConfig = confCore.getAllarmiConfig();
  117.            
  118.             Long idAllarme = Long.parseLong(idAllarmeS);
  119.             ConfigurazioneAllarmeBean allarme = confCore.getAllarme(idAllarme);
  120.             Integer oldEnabled = allarme.getEnabled();
  121.             List<org.openspcoop2.monitor.sdk.parameters.Parameter<?>> parameters = null;
  122.             if(first) {
  123.                 confHelper.savePluginIntoSession(request, session, allarme.getPlugin());
  124.                 Context context = confHelper.createAlarmContext(allarme, parameters);
  125.                 parameters = confCore.getParameters(allarme, context);
  126.                 confHelper.saveParametriIntoSession(request, session, parameters);
  127.             }

  128.             parameters = confHelper.readParametriFromSession(request, session);
  129.            
  130.             String applicabilita = Filtri.FILTRO_APPLICABILITA_VALORE_CONFIGURAZIONE;
  131.             if(ruoloPorta!=null) {
  132.                 if(RuoloPorta.DELEGATA.equals(ruoloPorta)) {
  133.                     applicabilita = Filtri.FILTRO_APPLICABILITA_VALORE_FRUIZIONE;
  134.                 }
  135.                 else {
  136.                     applicabilita = Filtri.FILTRO_APPLICABILITA_VALORE_EROGAZIONE;
  137.                 }
  138.             }
  139.            
  140.             List<Plugin> listaPlugin = confCore.pluginsAllarmiList(applicabilita, true);
  141.             int numeroPluginRegistrati = listaPlugin.size();
  142.            
  143.             Plugin plugin = confHelper.readPluginFromSession(request, session);
  144.             allarme.setPlugin(plugin);
  145.            
  146.             // Dati Attivazione
  147.             String errorAttivazione = confHelper.readAllarmeFromRequest(tipoOperazione, first, allarme, alarmEngineConfig, plugin, parameters);
  148.             if(errorAttivazione!=null){
  149.                 confHelper.addParsingError(sbParsingError,errorAttivazione);
  150.             }
  151.            
  152.             if(ruoloPorta!=null) {
  153.                
  154.                 String protocollo = null;
  155.                 String tipoSoggettoProprietario = null;
  156.                 String nomeSoggettoProprietario = null;
  157.                 if(RuoloPorta.DELEGATA.equals(ruoloPorta)) {
  158.                     IDPortaDelegata idPD = new IDPortaDelegata();
  159.                     idPD.setNome(nomePorta);
  160.                     PortaDelegata porta = pdCore.getPortaDelegata(idPD);
  161.                     protocollo = soggettiCore.getProtocolloAssociatoTipoSoggetto(porta.getTipoSoggettoProprietario());
  162.                     // il tipo e nome serve per l'applicativo fruitore
  163.                     tipoSoggettoProprietario = porta.getTipoSoggettoProprietario();
  164.                     nomeSoggettoProprietario = porta.getNomeSoggettoProprietario();
  165.                 }
  166.                 else {
  167.                     IDPortaApplicativa idPA = new IDPortaApplicativa();
  168.                     idPA.setNome(nomePorta);
  169.                     PortaApplicativa porta = paCore.getPortaApplicativa(idPA);
  170.                     protocollo = soggettiCore.getProtocolloAssociatoTipoSoggetto(porta.getTipoSoggettoProprietario());
  171.                 }
  172.                
  173.                 allarme.getFiltro().setEnabled(true);
  174.                 allarme.getFiltro().setProtocollo(protocollo);
  175.                 allarme.getFiltro().setRuoloPorta(ruoloPorta);
  176.                 allarme.getFiltro().setNomePorta(nomePorta);
  177.                 if(RuoloPorta.DELEGATA.equals(ruoloPorta)) {
  178.                     allarme.getFiltro().setTipoFruitore(tipoSoggettoProprietario);
  179.                     allarme.getFiltro().setNomeFruitore(nomeSoggettoProprietario);
  180.                 }
  181.                
  182.             }
  183.            
  184.            
  185.             // Preparo il menu
  186.             confHelper.makeMenu();
  187.            
  188.             String postBackElementName = confHelper.getPostBackElementName();
  189.             if (postBackElementName != null) {
  190.                 // selezione del plugin
  191.                 if(postBackElementName.equals(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_PLUGIN)) {
  192.                     if(nomePlugin.equals(ConfigurazioneCostanti.DEFAULT_VALUE_NESSUNO)) {
  193.                         allarme.setNome(null);
  194.                         allarme.setTipoAllarme(null);
  195.                         allarme.setPlugin(null);
  196.                         parameters=null;
  197.                        
  198.                         confHelper.removeParametriFromSession(request, session);
  199.                         confHelper.removePluginFromSession(request, session);
  200.                     } else {
  201.                         for (Plugin pluginBean : listaPlugin) {
  202.                             String key = pluginBean.getLabel() + ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_PLUGIN_NOME_SEP + pluginBean.getClassName();
  203.                             if(key.equals(nomePlugin)) {
  204.                                 plugin = pluginBean;
  205.                                 break;
  206.                             }
  207.                         }
  208.                        
  209.                        
  210.                         allarme.setPlugin(plugin);
  211.                         parameters=null;
  212.                        
  213.                         if(allarme.getPlugin() != null) {
  214.                             allarme.setNome(allarme.getPlugin().getLabel());
  215.                             allarme.setTipo(allarme.getPlugin().getTipo());
  216.                             try{
  217.                                 Context context = confHelper.createAlarmContext(allarme, parameters);  
  218.                                 allarme.setNome(allarme.getNome());
  219.                                 IDynamicLoader dl = DynamicFactory.getInstance().newDynamicLoader(TipoPlugin.ALLARME, allarme.getPlugin().getTipo(), allarme.getPlugin().getClassName(), ControlStationCore.getLog());
  220.                                 IAlarmProcessing alarm = (IAlarmProcessing) dl.newInstance();
  221.                                 switch (alarm.getAlarmType()) {
  222.                                 case ACTIVE:
  223.                                     allarme.setTipoAllarme(TipoAllarme.ATTIVO);
  224.                                     break;
  225.                                 case PASSIVE:
  226.                                     allarme.setTipoAllarme(TipoAllarme.PASSIVO);
  227.                                     break;
  228.                                 }
  229.                                            
  230.                                 parameters = confCore.getParameters(allarme, context);
  231.                                                                
  232.                                 confHelper.saveParametriIntoSession(request, session, parameters);
  233.                                 confHelper.savePluginIntoSession(request, session, allarme.getPlugin());
  234.                             }catch(Exception e){
  235.                                 ControlStationCore.getLog().error(e.getMessage(), e);
  236.                                 allarme.setNome(null);
  237.                                 allarme.setTipoAllarme(null);
  238.                                 allarme.setPlugin(null);
  239.                                 allarme.setTipo(null);
  240.                                 parameters=null;
  241.                                 pluginSelectedExceptionMessage = e.getMessage();
  242.                                 confHelper.removeParametriFromSession(request, session);
  243.                                 confHelper.removePluginFromSession(request, session);
  244.                             }
  245.                         }
  246.                     }
  247.                 }
  248.             }
  249.            
  250.             List<Parameter> lstParamSession = new ArrayList<>();

  251.             Parameter parRuoloPorta = null;
  252.             if(ruoloPorta!=null) {
  253.                 parRuoloPorta = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_RUOLO_PORTA, ruoloPorta.getValue());
  254.                 lstParamSession.add(parRuoloPorta);
  255.             }
  256.             Parameter parNomePorta = null;
  257.             if(nomePorta!=null) {
  258.                 parNomePorta = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_NOME_PORTA, nomePorta);
  259.                 lstParamSession.add(parNomePorta);
  260.             }
  261.             Parameter parServiceBinding = null;
  262.             if(serviceBinding!=null) {
  263.                 parServiceBinding = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_SERVICE_BINDING, serviceBinding.name());
  264.                 lstParamSession.add(parServiceBinding);
  265.             }
  266.            
  267.             List<Parameter> lstParamPorta = null;
  268.             if(ruoloPorta!=null) {
  269.                 lstParamPorta = confHelper.getTitleListAllarmi(ruoloPorta, nomePorta, serviceBinding, allarme.getAlias());
  270.             }
  271.            
  272.             // setto la barra del titolo
  273.             List<Parameter> lstParam = null;
  274.             if(lstParamPorta!=null) {
  275.                 lstParam = lstParamPorta;
  276.             }
  277.             else {
  278.                 lstParam  = new ArrayList<>();
  279.                
  280.                 if(lstParamSession.size() > 0) {
  281.                     lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_ALLARMI,
  282.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_ALLARMI_LIST, lstParamSession.toArray(new Parameter[lstParamSession.size()])));
  283.                 } else {
  284.                     lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_ALLARMI,
  285.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_ALLARMI_LIST));
  286.                 }
  287.                 lstParam.add(new Parameter(allarme.getAlias(), null));
  288.             }
  289.            
  290.             // Se tipo = null, devo visualizzare la pagina per l'inserimento
  291.             // dati
  292.             if (confHelper.isEditModeInProgress()) {
  293.                 ServletUtils.setPageDataTitle(pd, lstParam);
  294.                
  295.                 // preparo i campi
  296.                 List<DataElement> dati = new ArrayList<>();
  297.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  298.                
  299.                 confHelper.addAllarmeToDati(dati, tipoOperazione, allarme, alarmEngineConfig, listaPlugin, parameters, ruoloPorta, nomePorta, serviceBinding);
  300.                
  301.                 // Set First is false
  302.                 confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_FIRST_TIME);
  303.                
  304.                 if(pluginSelectedExceptionMessage != null) {
  305.                     pd.setMessage(pluginSelectedExceptionMessage);
  306.                 }
  307.                
  308.                 pd.setDati(dati);

  309.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  310.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ALLARMI, ForwardParams.CHANGE());
  311.             }
  312.                
  313.             // Controlli sui campi immessi
  314.             boolean isOk = confHelper.allarmeCheckData(sbParsingError, tipoOperazione, null, allarme, numeroPluginRegistrati, parameters);
  315.             if (!isOk) {
  316.                 ServletUtils.setPageDataTitle(pd, lstParam);
  317.                
  318.                 // preparo i campi
  319.                 List<DataElement> dati = new ArrayList<>();
  320.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  321.                
  322.                 confHelper.addAllarmeToDati(dati, tipoOperazione, allarme, alarmEngineConfig, listaPlugin, parameters, ruoloPorta, nomePorta, serviceBinding);
  323.                
  324.                 // Set First is false
  325.                 confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_FIRST_TIME);
  326.                
  327.                 pd.setDati(dati);

  328.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  329.                
  330.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  331.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ALLARMI,
  332.                         ForwardParams.CHANGE());
  333.             }
  334.                
  335.             // salvataggio dei parametri
  336.             if(parameters!=null && !parameters.isEmpty()) {
  337.                 for (org.openspcoop2.monitor.sdk.parameters.Parameter<?> par : parameters) {
  338.                     boolean found = false;
  339.                     for (AllarmeParametro parDB : allarme.getAllarmeParametroList()) {
  340.                         if(parDB.getIdParametro().equals(par.getId())){
  341.                             parDB.setValore(par.getValueAsString());
  342.                             found = true;
  343.                             break;
  344.                         }
  345.                     }
  346.                     if(!found){
  347.                         AllarmeParametro parDB = new AllarmeParametro();
  348.                         parDB.setIdParametro(par.getId());
  349.                         parDB.setValore(par.getValueAsString());
  350.                         allarme.addAllarmeParametro(parDB);
  351.                     }
  352.                 }
  353.             }
  354.            
  355.             // data modifica
  356.             allarme.setLasttimestampUpdate(new Date());
  357.            
  358.             boolean modificatoInformazioniHistory = false;
  359.             boolean modificatoStato = false;
  360.             boolean modificatoAckwoldegment = false;
  361.             ConfigurazioneAllarmeBean oldConfigurazioneAllarme = confCore.getAllarme(idAllarme);
  362.             // se ho modificato l'abilitato devo registrare la modifica nella tabella history
  363.             if(allarme.getEnabled().intValue() != oldEnabled.intValue()) {
  364.                 modificatoInformazioniHistory = true;
  365.                
  366.                 if(allarme.getEnabled().intValue() == 0) {
  367.                     // se disabilito resetto lo stato per quando lo riabilito
  368.                     allarme.setStato(AllarmiConverterUtils.toIntegerValue(StatoAllarme.OK));
  369.                     allarme.setDettaglioStato(null);
  370.                 }
  371.             }
  372.            
  373.             // insert sul db
  374.             confCore.performUpdateOperation(userLogin, confHelper.smista(), allarme);
  375.            
  376.             if(modificatoInformazioniHistory && alarmEngineConfig.isHistoryEnabled()) {
  377.                 // registro la modifica
  378.                 AllarmeHistory history = ConfigurazioneUtilities.createAllarmeHistory(oldConfigurazioneAllarme, userLogin);
  379.                 confCore.performCreateOperation(userLogin, confHelper.smista(), history);
  380.             }
  381.            
  382.             /* ******** GESTIONE AVVIO THREAD NEL CASO DI ATTIVO *************** */
  383.             try {
  384.                 AllarmiUtils.notifyStateActiveThread(false, modificatoStato, modificatoAckwoldegment, oldConfigurazioneAllarme, allarme, ControlStationCore.getLog(), alarmEngineConfig);
  385.             } catch(Exception e) {
  386.                 String errorMsg = MessageFormat.format(ConfigurazioneCostanti.MESSAGGIO_ERRORE_ALLARME_SALVATO_NOTIFICA_FALLITA, allarme.getAlias(),e.getMessage());
  387.                 ControlStationCore.getLog().error(errorMsg, e);
  388.                 pd.setMessage(errorMsg);
  389.             }
  390.                
  391.             // Preparo la lista
  392.             int idLista = Liste.CONFIGURAZIONE_ALLARMI;
  393.            
  394.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  395.            
  396.             ricerca = confHelper.checkSearchParameters(idLista, ricerca);

  397.             List<ConfigurazioneAllarmeBean> lista = confCore.allarmiList(ricerca, ruoloPorta, nomePorta);
  398.            
  399.             confHelper.prepareAllarmiList(ricerca, lista, ruoloPorta, nomePorta, serviceBinding);
  400.            
  401.             // salvo l'oggetto ricerca nella sessione
  402.             ServletUtils.setSearchObjectIntoSession(request, session, ricerca);
  403.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  404.            
  405.             // Forward control to the specified success URI
  406.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ALLARMI, ForwardParams.CHANGE());
  407.         } catch (Exception e) {
  408.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  409.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ALLARMI, ForwardParams.CHANGE());
  410.         }  
  411.     }
  412. }