ConfigurazioneHandlersRispostaChange.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.List;

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

  27. import org.apache.commons.lang.StringUtils;
  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.commons.Liste;
  33. import org.openspcoop2.core.config.Configurazione;
  34. import org.openspcoop2.core.config.ConfigurazioneGeneraleHandler;
  35. import org.openspcoop2.core.config.ConfigurazioneHandler;
  36. import org.openspcoop2.core.config.ConfigurazioneMessageHandlers;
  37. import org.openspcoop2.core.config.ConfigurazionePortaHandler;
  38. import org.openspcoop2.core.config.PortaApplicativa;
  39. import org.openspcoop2.core.config.PortaDelegata;
  40. import org.openspcoop2.core.config.constants.FaseMessageHandler;
  41. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  42. import org.openspcoop2.core.constants.TipoPdD;
  43. import org.openspcoop2.core.plugins.Plugin;
  44. import org.openspcoop2.core.plugins.utils.handlers.ConfigurazioneHandlerBean;
  45. import org.openspcoop2.message.constants.ServiceBinding;
  46. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  47. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  48. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  49. import org.openspcoop2.web.ctrlstat.servlet.pa.PorteApplicativeCore;
  50. import org.openspcoop2.web.ctrlstat.servlet.pd.PorteDelegateCore;
  51. import org.openspcoop2.web.lib.mvc.Costanti;
  52. import org.openspcoop2.web.lib.mvc.DataElement;
  53. import org.openspcoop2.web.lib.mvc.ForwardParams;
  54. import org.openspcoop2.web.lib.mvc.GeneralData;
  55. import org.openspcoop2.web.lib.mvc.PageData;
  56. import org.openspcoop2.web.lib.mvc.Parameter;
  57. import org.openspcoop2.web.lib.mvc.ServletUtils;
  58. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  59. /**    
  60.  * ConfigurazioneHandlersRispostaChange
  61.  *
  62.  * @author Pintori Giuliano (pintori@link.it)
  63.  * @author $Author$
  64.  * @version $Rev$, $Date$
  65.  */
  66. public class ConfigurazioneHandlersRispostaChange extends Action {

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

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

  70.         // Inizializzo PageData
  71.         PageData pd = new PageData();

  72.         GeneralHelper generalHelper = new GeneralHelper(session);

  73.         // Inizializzo GeneralData
  74.         GeneralData gd = generalHelper.initGeneralData(request);

  75.         String userLogin = ServletUtils.getUserLoginFromSession(session);  
  76.        
  77.         TipoOperazione tipoOperazione = TipoOperazione.CHANGE;
  78.        
  79.         try {
  80.            
  81.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);
  82.            
  83.             // controllo primo accesso
  84.             boolean first = confHelper.isFirstTimeFromHttpParameters(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FIRST_TIME);
  85.            
  86.             String idHandlerS = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_ID_HANDLER);
  87.            
  88.             String ruoloPortaParam = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_RUOLO_PORTA);
  89.             TipoPdD ruoloPorta = null;
  90.             if(ruoloPortaParam!=null) {
  91.                 ruoloPorta = TipoPdD.toTipoPdD(ruoloPortaParam);
  92.             }
  93.             String idPortaS = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_ID_PORTA);
  94.             Long idPorta = null;
  95.             if(StringUtils.isNotBlank(idPortaS)) {
  96.                 idPorta = Long.parseLong(idPortaS);
  97.             }
  98.            
  99.             ServiceBinding serviceBinding = null;
  100.             String serviceBindingParam = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_SERVICE_BINDING);
  101.             if(serviceBindingParam!=null && !"".equals(serviceBindingParam)) {
  102.                 serviceBinding = ServiceBinding.valueOf(serviceBindingParam);
  103.             }
  104.             String nomePlugin = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_PLUGIN);
  105.             String fase = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FASE);
  106.             String stato = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_STATO);
  107.            
  108.             FaseMessageHandler faseMH = FaseMessageHandler.toEnumConstant(fase);
  109.            
  110.             ConfigurazioneCore confCore = new ConfigurazioneCore();

  111.             Long idHandler = Long.parseLong(idHandlerS);
  112.            
  113.             ConfigurazioneHandlerBean handler = confCore.getHandlerRisposta(fase, ruoloPorta, idPorta, idHandler);
  114.            
  115.             Plugin plugin = handler.getPlugin();
  116.             String tipo = plugin.getTipo();
  117.             List<String> tipiPluginGiaUtilizzati = null;
  118.            
  119.             // Preparo il menu
  120.             confHelper.makeMenu();
  121.            
  122.             List<Parameter> lstParamSession = new ArrayList<>();

  123.             Parameter parRuoloPorta = null;
  124.             if(ruoloPorta!=null) {
  125.                 parRuoloPorta = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_RUOLO_PORTA, ruoloPorta.getTipo());
  126.                 lstParamSession.add(parRuoloPorta);
  127.             }
  128.             Parameter parIdPorta = null;
  129.             if(idPortaS!=null) {
  130.                 parIdPorta = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_ID_PORTA, idPortaS);
  131.                 lstParamSession.add(parIdPorta);
  132.             }
  133.             Parameter parServiceBinding = null;
  134.             if(serviceBinding!=null) {
  135.                 parServiceBinding = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_SERVICE_BINDING, serviceBinding.name());
  136.                 lstParamSession.add(parServiceBinding);
  137.             }
  138.             Parameter parTipologia = null;
  139.             if(fase!=null) {
  140.                 parTipologia = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FASE, fase);
  141.                 lstParamSession.add(parTipologia);
  142.             }
  143.            
  144.             String labelHandler = confHelper.getLabelTipologiaFromFaseMessageHandler(fase,true);
  145.             String labelHandlerDi = labelHandler + " di ";
  146.             String servletListURL = ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_HANDLERS_RISPOSTA_LIST;
  147.             String messaggioValoriNonDisponibili = MessageFormat.format(ConfigurazioneCostanti.LABEL_HANDLER_PLUGIN_NON_DISPONIBILI_PER_LA_FASE, labelHandler);
  148.             String messaggioHandlerRispostaDuplicato = null;

  149.             List<Parameter> lstParamPorta = null;
  150.             if(ruoloPorta!=null) {
  151.                 lstParamPorta = confHelper.getTitleListHandler(fase, ruoloPorta, idPortaS, serviceBinding, plugin.getLabel(),
  152.                         servletListURL, labelHandlerDi, labelHandler);
  153.             }
  154.            
  155.             // setto la barra del titolo
  156.             List<Parameter> lstParam = null;
  157.             if(lstParamPorta!=null) {
  158.                 lstParam = lstParamPorta;
  159.             }
  160.             else {
  161.                 lstParam  = new ArrayList<>();
  162.                
  163.                 if(!lstParamSession.isEmpty()) {
  164.                     lstParam.add(new Parameter(labelHandler, servletListURL, lstParamSession.toArray(new Parameter[lstParamSession.size()])));
  165.                 } else {
  166.                     lstParam.add(new Parameter(labelHandler, servletListURL));
  167.                 }
  168.                 lstParam.add(new Parameter(plugin.getLabel(), null));
  169.             }
  170.            
  171.             if(ruoloPorta == null) {
  172.                 lstParam.add(0,new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  173.             }
  174.            
  175.             // Se tipo = null, devo visualizzare la pagina per l'inserimento
  176.             // dati
  177.             if (confHelper.isEditModeInProgress()) {
  178.                 ServletUtils.setPageDataTitle(pd, lstParam);
  179.                
  180.                 if(first) {
  181.                     nomePlugin = plugin.getTipo();
  182.                     stato = handler.getStato().getValue();
  183.                 }
  184.                
  185.                 // preparo i campi
  186.                 List<DataElement> dati = new ArrayList<>();
  187.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  188.                
  189.                 confHelper.addHandlerRispostaToDati(dati, tipoOperazione, idHandlerS, nomePlugin, stato, ruoloPorta, idPortaS, serviceBinding, fase, tipiPluginGiaUtilizzati, messaggioValoriNonDisponibili);
  190.                
  191.                 // Set First is false
  192.                 confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_FIRST_TIME);
  193.                
  194.                 pd.setDati(dati);

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

  196.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_RISPOSTA, ForwardParams.CHANGE());
  197.             }
  198.                
  199.             // Controlli sui campi immessi
  200.             boolean isOk = confHelper.handlerRispostaCheckData(tipoOperazione, handler, nomePlugin, stato, ruoloPorta, idPorta, fase, tipo, messaggioHandlerRispostaDuplicato);
  201.            
  202.             if (!isOk) {
  203.                 ServletUtils.setPageDataTitle(pd, lstParam);
  204.                
  205.                 // preparo i campi
  206.                 List<DataElement> dati = new ArrayList<>();
  207.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  208.                
  209.                 confHelper.addHandlerRispostaToDati(dati, tipoOperazione, idHandlerS, nomePlugin, stato, ruoloPorta, idPortaS, serviceBinding, fase, tipiPluginGiaUtilizzati, messaggioValoriNonDisponibili);
  210.                
  211.                 // Set First is false
  212.                 confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_FIRST_TIME);
  213.                
  214.                 pd.setDati(dati);

  215.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  216.                
  217.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  218.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_RISPOSTA,
  219.                         ForwardParams.CHANGE());
  220.             }
  221.                
  222.             handler.setStato(StatoFunzionalita.toEnumConstant(stato));
  223.            
  224.             List<Object> oggettiDaAggiornare = new ArrayList<>();
  225.             List<ConfigurazioneHandler> listaDaAggiornare = null;
  226.             if(ruoloPorta !=null) {
  227.                 if(ruoloPorta.equals(TipoPdD.DELEGATA)) {
  228.                     PorteDelegateCore porteDelegateCore = new PorteDelegateCore(confCore);
  229.                     PortaDelegata portaDelegata = porteDelegateCore.getPortaDelegata(idPorta);
  230.                    
  231.                     if(portaDelegata.getConfigurazioneHandler() == null)
  232.                         portaDelegata.setConfigurazioneHandler(new ConfigurazionePortaHandler());
  233.                    
  234.                     if(portaDelegata.getConfigurazioneHandler().getResponse() == null)
  235.                         portaDelegata.getConfigurazioneHandler().setResponse(new ConfigurazioneMessageHandlers());
  236.                    
  237.                     switch (faseMH) {
  238.                     case IN:
  239.                         listaDaAggiornare = portaDelegata.getConfigurazioneHandler().getResponse().getInList();
  240.                         break;
  241.                     case IN_PROTOCOL_INFO:
  242.                         listaDaAggiornare = portaDelegata.getConfigurazioneHandler().getResponse().getInProtocolInfoList();
  243.                         break;
  244.                     case OUT:
  245.                         listaDaAggiornare = portaDelegata.getConfigurazioneHandler().getResponse().getOutList();
  246.                         break;
  247.                     case POST_OUT:
  248.                         listaDaAggiornare = portaDelegata.getConfigurazioneHandler().getResponse().getPostOutList();
  249.                         break;
  250.                     case PRE_IN:
  251.                         listaDaAggiornare = portaDelegata.getConfigurazioneHandler().getResponse().getPreInList();
  252.                         break;
  253.                     }
  254.                     oggettiDaAggiornare.add(portaDelegata);
  255.                 }
  256.                 else if(ruoloPorta.equals(TipoPdD.APPLICATIVA)) {
  257.                     PorteApplicativeCore porteApplicativeCore = new PorteApplicativeCore(confCore);
  258.                     PortaApplicativa portaApplicativa = porteApplicativeCore.getPortaApplicativa(idPorta);
  259.                    
  260.                     if(portaApplicativa.getConfigurazioneHandler() == null)
  261.                         portaApplicativa.setConfigurazioneHandler(new ConfigurazionePortaHandler());
  262.                    
  263.                     if(portaApplicativa.getConfigurazioneHandler().getResponse() == null)
  264.                         portaApplicativa.getConfigurazioneHandler().setResponse(new ConfigurazioneMessageHandlers());
  265.                    
  266.                     switch (faseMH) {
  267.                     case IN:
  268.                         listaDaAggiornare = portaApplicativa.getConfigurazioneHandler().getResponse().getInList();
  269.                         break;
  270.                     case IN_PROTOCOL_INFO:
  271.                         listaDaAggiornare = portaApplicativa.getConfigurazioneHandler().getResponse().getInProtocolInfoList();
  272.                         break;
  273.                     case OUT:
  274.                         listaDaAggiornare = portaApplicativa.getConfigurazioneHandler().getResponse().getOutList();
  275.                         break;
  276.                     case POST_OUT:
  277.                         listaDaAggiornare = portaApplicativa.getConfigurazioneHandler().getResponse().getPostOutList();
  278.                         break;
  279.                     case PRE_IN:
  280.                         listaDaAggiornare = portaApplicativa.getConfigurazioneHandler().getResponse().getPreInList();
  281.                         break;
  282.                     }  
  283.                     oggettiDaAggiornare.add(portaApplicativa);
  284.                 }
  285.             } else {
  286.                 Configurazione configurazione = confCore.getConfigurazioneGenerale();
  287.                
  288.                 if(configurazione.getConfigurazioneHandler() == null)
  289.                     configurazione.setConfigurazioneHandler(new ConfigurazioneGeneraleHandler());
  290.                
  291.                 if(configurazione.getConfigurazioneHandler().getResponse() == null)
  292.                     configurazione.getConfigurazioneHandler().setResponse(new ConfigurazioneMessageHandlers());
  293.                
  294.                 switch (faseMH) {
  295.                 case IN:
  296.                     listaDaAggiornare = configurazione.getConfigurazioneHandler().getResponse().getInList();
  297.                     break;
  298.                 case IN_PROTOCOL_INFO:
  299.                     listaDaAggiornare = configurazione.getConfigurazioneHandler().getResponse().getInProtocolInfoList();
  300.                     break;
  301.                 case OUT:
  302.                     listaDaAggiornare = configurazione.getConfigurazioneHandler().getResponse().getOutList();
  303.                     break;
  304.                 case POST_OUT:
  305.                     listaDaAggiornare = configurazione.getConfigurazioneHandler().getResponse().getPostOutList();
  306.                     break;
  307.                 case PRE_IN:
  308.                     listaDaAggiornare = configurazione.getConfigurazioneHandler().getResponse().getPreInList();
  309.                     break;
  310.                 }
  311.                 oggettiDaAggiornare.add(configurazione);
  312.             }
  313.            
  314.             if(listaDaAggiornare!=null) {
  315.                 for (ConfigurazioneHandler handlerToCheck : listaDaAggiornare) {
  316.                     if(handlerToCheck.getTipo().equals(handler.getTipo())) {
  317.                         confHelper.updateHandler(handlerToCheck, handler, tipoOperazione);
  318.                     }
  319.                 }
  320.             }
  321.            
  322.             // update sul db
  323.             confCore.performUpdateOperation(userLogin, confHelper.smista(), oggettiDaAggiornare.toArray(new Object[oggettiDaAggiornare.size()]));
  324.            
  325.             // Preparo la lista
  326.             int idLista = Liste.CONFIGURAZIONE_HANDLERS_RISPOSTA;
  327.            
  328.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  329.            
  330.             ricerca = confHelper.checkSearchParameters(idLista, ricerca);

  331.             List<ConfigurazioneHandlerBean> lista = confCore.handlersRispostaList(ricerca, fase, ruoloPorta, idPorta);
  332.            
  333.             confHelper.prepareHandlersRispostaList(ricerca, lista, ruoloPorta, idPortaS, serviceBinding, fase);
  334.            
  335.             if(ruoloPorta == null) {
  336.                 pd.setMessage(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_MESSAGE_HANDLERS_MODIFICATA_CON_SUCCESSO, Costanti.MESSAGE_TYPE_INFO);
  337.             }
  338.            
  339.             // salvo l'oggetto ricerca nella sessione
  340.             ServletUtils.setSearchObjectIntoSession(request, session, ricerca);
  341.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  342.            
  343.             // Forward control to the specified success URI
  344.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_RISPOSTA, ForwardParams.CHANGE());
  345.         } catch (Exception e) {
  346.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  347.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_RISPOSTA, ForwardParams.CHANGE());
  348.         }  
  349.     }
  350. }