ConfigurazioneHandlersServizioChange.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.struts.action.Action;
  28. import org.apache.struts.action.ActionForm;
  29. import org.apache.struts.action.ActionForward;
  30. import org.apache.struts.action.ActionMapping;
  31. import org.openspcoop2.core.commons.Liste;
  32. import org.openspcoop2.core.config.Configurazione;
  33. import org.openspcoop2.core.config.ConfigurazioneGeneraleHandler;
  34. import org.openspcoop2.core.config.ConfigurazioneHandler;
  35. import org.openspcoop2.core.config.ConfigurazioneServiceHandlers;
  36. import org.openspcoop2.core.config.constants.FaseServiceHandler;
  37. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  38. import org.openspcoop2.core.constants.TipoPdD;
  39. import org.openspcoop2.core.plugins.Plugin;
  40. import org.openspcoop2.core.plugins.utils.handlers.ConfigurazioneHandlerBean;
  41. import org.openspcoop2.message.constants.ServiceBinding;
  42. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  43. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  44. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  45. import org.openspcoop2.web.lib.mvc.Costanti;
  46. import org.openspcoop2.web.lib.mvc.DataElement;
  47. import org.openspcoop2.web.lib.mvc.ForwardParams;
  48. import org.openspcoop2.web.lib.mvc.GeneralData;
  49. import org.openspcoop2.web.lib.mvc.PageData;
  50. import org.openspcoop2.web.lib.mvc.Parameter;
  51. import org.openspcoop2.web.lib.mvc.ServletUtils;
  52. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  53. /**    
  54.  * ConfigurazioneHandlersServizioChange
  55.  *
  56.  * @author Pintori Giuliano (pintori@link.it)
  57.  * @author $Author$
  58.  * @version $Rev$, $Date$
  59.  */
  60. public class ConfigurazioneHandlersServizioChange extends Action {

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

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

  64.         // Inizializzo PageData
  65.         PageData pd = new PageData();

  66.         GeneralHelper generalHelper = new GeneralHelper(session);

  67.         // Inizializzo GeneralData
  68.         GeneralData gd = generalHelper.initGeneralData(request);

  69.         String userLogin = ServletUtils.getUserLoginFromSession(session);  
  70.        
  71.         TipoOperazione tipoOperazione = TipoOperazione.CHANGE;
  72.        
  73.         try {
  74.            
  75.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);
  76.            
  77.             // controllo primo accesso
  78.             boolean first = confHelper.isFirstTimeFromHttpParameters(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FIRST_TIME);
  79.            
  80.             String idHandlerS = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_ID_HANDLER);
  81.            
  82.             TipoPdD ruoloPorta = null;
  83.             String idPortaS = null;
  84.             Long idPorta = null;
  85.             ServiceBinding serviceBinding = null;
  86.             String nomePlugin = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_PLUGIN);
  87.             String fase = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FASE);
  88.             String stato = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_STATO);
  89.            
  90.             FaseServiceHandler faseSH = FaseServiceHandler.toEnumConstant(fase);        
  91.            
  92.             ConfigurazioneCore confCore = new ConfigurazioneCore();

  93.             Long idHandler = Long.parseLong(idHandlerS);
  94.            
  95.             ConfigurazioneHandlerBean handler = confCore.getHandlerServizio(fase, idHandler);
  96.            
  97.             Plugin plugin = handler.getPlugin();
  98.             String tipo = plugin.getTipo();
  99.             List<String> tipiPluginGiaUtilizzati = null;
  100.            
  101.             // Preparo il menu
  102.             confHelper.makeMenu();
  103.            
  104.             List<Parameter> lstParamSession = new ArrayList<>();

  105.             Parameter parTipologia = null;
  106.             if(fase!=null) {
  107.                 parTipologia = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FASE, fase);
  108.                 lstParamSession.add(parTipologia);
  109.             }
  110.            
  111.             String labelHandler = confHelper.getLabelTipologiaFromFaseMessageHandler(fase,true);
  112.             String servletListURL = ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_HANDLERS_SERVIZIO_LIST;
  113.             String messaggioValoriNonDisponibili = MessageFormat.format(ConfigurazioneCostanti.LABEL_HANDLER_PLUGIN_NON_DISPONIBILI_PER_LA_FASE, labelHandler);
  114.             String messaggioHandlerServizioDuplicato = null;

  115.             // setto la barra del titolo
  116.             List<Parameter> lstParam = null;
  117.             lstParam  = new ArrayList<>();
  118.            
  119.             if(!lstParamSession.isEmpty()) {
  120.                 lstParam.add(new Parameter(labelHandler, servletListURL, lstParamSession.toArray(new Parameter[lstParamSession.size()])));
  121.             } else {
  122.                 lstParam.add(new Parameter(labelHandler, servletListURL));
  123.             }
  124.             lstParam.add(new Parameter(plugin.getLabel(), null));
  125.             lstParam.add(0,new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  126.            
  127.             // Se tipo = null, devo visualizzare la pagina per l'inserimento
  128.             // dati
  129.             if (confHelper.isEditModeInProgress()) {
  130.                 ServletUtils.setPageDataTitle(pd, lstParam);
  131.                
  132.                 if(first) {
  133.                     nomePlugin = plugin.getTipo();
  134.                     stato = handler.getStato().getValue();
  135.                 }
  136.                
  137.                 // preparo i campi
  138.                 List<DataElement> dati = new ArrayList<>();
  139.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  140.                
  141.                 confHelper.addHandlerServizioToDati(dati, tipoOperazione, idHandlerS, nomePlugin, stato, ruoloPorta, idPortaS, serviceBinding, fase, tipiPluginGiaUtilizzati, messaggioValoriNonDisponibili);
  142.                
  143.                 // Set First is false
  144.                 confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_FIRST_TIME);
  145.                
  146.                 pd.setDati(dati);

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

  148.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_SERVIZIO, ForwardParams.CHANGE());
  149.             }
  150.                
  151.             // Controlli sui campi immessi
  152.             boolean isOk = confHelper.handlerServizioCheckData(tipoOperazione, handler, nomePlugin, stato, ruoloPorta, idPorta, fase, tipo, messaggioHandlerServizioDuplicato);
  153.            
  154.             if (!isOk) {
  155.                 ServletUtils.setPageDataTitle(pd, lstParam);
  156.                
  157.                 // preparo i campi
  158.                 List<DataElement> dati = new ArrayList<>();
  159.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  160.                
  161.                 confHelper.addHandlerServizioToDati(dati, tipoOperazione, idHandlerS, nomePlugin, stato, ruoloPorta, idPortaS, serviceBinding, fase, tipiPluginGiaUtilizzati, messaggioValoriNonDisponibili);
  162.                
  163.                 // Set First is false
  164.                 confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_FIRST_TIME);
  165.                
  166.                 pd.setDati(dati);

  167.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  168.                
  169.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  170.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_SERVIZIO,
  171.                         ForwardParams.CHANGE());
  172.             }
  173.                
  174.             handler.setStato(StatoFunzionalita.toEnumConstant(stato));
  175.            
  176.             List<Object> oggettiDaAggiornare = new ArrayList<>();
  177.             List<ConfigurazioneHandler> listaDaAggiornare = null;
  178.             Configurazione configurazione = confCore.getConfigurazioneGenerale();
  179.            
  180.             if(configurazione.getConfigurazioneHandler() == null)
  181.                 configurazione.setConfigurazioneHandler(new ConfigurazioneGeneraleHandler());
  182.            
  183.             if(configurazione.getConfigurazioneHandler().getService() == null)
  184.                 configurazione.getConfigurazioneHandler().setService(new ConfigurazioneServiceHandlers());
  185.            
  186.             switch (faseSH) {
  187.             case EXIT:
  188.                 listaDaAggiornare =configurazione.getConfigurazioneHandler().getService().getExitList();
  189.                 break;
  190.             case INIT:
  191.                 listaDaAggiornare =configurazione.getConfigurazioneHandler().getService().getInitList();
  192.                 break;
  193.             case INTEGRATION_MANAGER_REQUEST:
  194.                 listaDaAggiornare =configurazione.getConfigurazioneHandler().getService().getIntegrationManagerRequestList();
  195.                 break;
  196.             case INTEGRATION_MANAGER_RESPONSE:
  197.                 listaDaAggiornare =configurazione.getConfigurazioneHandler().getService().getIntegrationManagerResponseList();
  198.                 break;
  199.             }
  200.             oggettiDaAggiornare.add(configurazione);
  201.            
  202.             if(listaDaAggiornare!=null) {
  203.                 for (ConfigurazioneHandler handlerToCheck : listaDaAggiornare) {
  204.                     if(handlerToCheck.getTipo().equals(handler.getTipo())) {
  205.                         confHelper.updateHandler(handlerToCheck, handler, tipoOperazione);
  206.                     }
  207.                 }
  208.             }
  209.            
  210.             // update sul db
  211.             confCore.performUpdateOperation(userLogin, confHelper.smista(), oggettiDaAggiornare.toArray(new Object[oggettiDaAggiornare.size()]));
  212.            
  213.             // Preparo la lista
  214.             int idLista = Liste.CONFIGURAZIONE_HANDLERS_SERVIZIO;
  215.            
  216.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  217.            
  218.             ricerca = confHelper.checkSearchParameters(idLista, ricerca);

  219.             List<ConfigurazioneHandlerBean> lista = confCore.handlersServizioList(ricerca, fase);
  220.            
  221.             confHelper.prepareHandlersServizioList(ricerca, lista, ruoloPorta, idPortaS, serviceBinding, fase);
  222.            
  223.             if(ruoloPorta == null) {
  224.                 pd.setMessage(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_SERVICE_HANDLERS_MODIFICATA_CON_SUCCESSO, Costanti.MESSAGE_TYPE_INFO);
  225.             }
  226.            
  227.             // salvo l'oggetto ricerca nella sessione
  228.             ServletUtils.setSearchObjectIntoSession(request, session, ricerca);
  229.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  230.            
  231.             // Forward control to the specified success URI
  232.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_SERVIZIO, ForwardParams.CHANGE());
  233.         } catch (Exception e) {
  234.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  235.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_SERVIZIO, ForwardParams.CHANGE());
  236.         }  
  237.     }
  238. }