ConfigurazioneHandlersServizioAdd.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.constants.TipoPlugin;
  41. import org.openspcoop2.core.plugins.utils.handlers.ConfigurazioneHandlerBean;
  42. import org.openspcoop2.message.constants.ServiceBinding;
  43. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  44. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  45. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  46. import org.openspcoop2.web.lib.mvc.Costanti;
  47. import org.openspcoop2.web.lib.mvc.DataElement;
  48. import org.openspcoop2.web.lib.mvc.ForwardParams;
  49. import org.openspcoop2.web.lib.mvc.GeneralData;
  50. import org.openspcoop2.web.lib.mvc.PageData;
  51. import org.openspcoop2.web.lib.mvc.Parameter;
  52. import org.openspcoop2.web.lib.mvc.ServletUtils;
  53. import org.openspcoop2.web.lib.mvc.TipoOperazione;

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

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

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

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

  67.         GeneralHelper generalHelper = new GeneralHelper(session);

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

  70.         String userLogin = ServletUtils.getUserLoginFromSession(session);  
  71.        
  72.         TipoOperazione tipoOperazione = TipoOperazione.ADD;
  73.        
  74.         try {
  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.             TipoPdD ruoloPorta = null;
  81.             String idPortaS = null;
  82.             Long idPorta = null;
  83.             ServiceBinding serviceBinding = null;
  84.             String nomePlugin = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_PLUGIN);
  85.             String fase = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FASE);
  86.             String stato = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_STATO);
  87.            
  88.             FaseServiceHandler faseSH = FaseServiceHandler.toEnumConstant(fase);            
  89.            
  90.             ConfigurazioneCore confCore = new ConfigurazioneCore();
  91.            
  92.             List<String> tipiPluginGiaUtilizzati = null;
  93.             ConsoleSearch ricercaPluginUilizzati = new ConsoleSearch(true);
  94.             List<ConfigurazioneHandlerBean> listaEsistenti = confCore.handlersServizioList(ricercaPluginUilizzati, fase);
  95.             if(listaEsistenti != null && !listaEsistenti.isEmpty()) {
  96.                 tipiPluginGiaUtilizzati = new ArrayList<>();
  97.                
  98.                 for (ConfigurazioneHandlerBean configurazioneHandlerBean : listaEsistenti) {
  99.                     tipiPluginGiaUtilizzati.add(configurazioneHandlerBean.getPlugin().getTipo());
  100.                 }
  101.             }
  102.            
  103.            
  104.             // Preparo il menu
  105.             confHelper.makeMenu();
  106.                        
  107.             List<Parameter> lstParamSession = new ArrayList<>();

  108.             Parameter parTipologia = null;
  109.             if(fase!=null) {
  110.                 parTipologia = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FASE, fase);
  111.                 lstParamSession.add(parTipologia);
  112.             }
  113.            
  114.             String labelHandler = confHelper.getLabelTipologiaFromFaseServiceHandler(fase);
  115.             String servletListURL = ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_HANDLERS_SERVIZIO_LIST;
  116.             String messaggioValoriNonDisponibili = MessageFormat.format(ConfigurazioneCostanti.LABEL_HANDLER_PLUGIN_NON_DISPONIBILI_PER_LA_FASE, labelHandler);
  117.             String messaggioHandlerServizioDuplicato = null;
  118.             String tipo = null;
  119.             Plugin plugin = null;
  120.            
  121.             if(nomePlugin != null) {
  122.                 plugin = confCore.getPlugin(TipoPlugin.SERVICE_HANDLER, nomePlugin, false);
  123.             }
  124.            
  125.             if(plugin != null) {
  126.                 messaggioHandlerServizioDuplicato = MessageFormat.format(ConfigurazioneCostanti.MESSAGGIO_HANDLER_SERVIZIO_DUPLICATO, labelHandler, plugin.getLabel());
  127.                 tipo = plugin.getTipo();
  128.             }
  129.            
  130.             // setto la barra del titolo
  131.             List<Parameter> lstParam = null;
  132.             lstParam = new ArrayList<>();
  133.            
  134.             if(!lstParamSession.isEmpty()) {
  135.                 lstParam.add(new Parameter(labelHandler, servletListURL, lstParamSession.toArray(new Parameter[lstParamSession.size()])));
  136.             } else {
  137.                 lstParam.add(new Parameter(labelHandler, servletListURL));
  138.             }
  139.             lstParam.add(ServletUtils.getParameterAggiungi());
  140.             lstParam.add(0,new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  141.            
  142.             // Se tipo = null, devo visualizzare la pagina per l'inserimento
  143.             // dati
  144.             if (confHelper.isEditModeInProgress()) {
  145.                 ServletUtils.setPageDataTitle(pd, lstParam);
  146.                
  147.                 if(first) {
  148.                     nomePlugin = "";
  149.                     stato = StatoFunzionalita.ABILITATO.getValue();
  150.                 }
  151.                
  152.                 // preparo i campi
  153.                 List<DataElement> dati = new ArrayList<>();
  154.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  155.                
  156.                 confHelper.addHandlerServizioToDati(dati, tipoOperazione, null, nomePlugin, stato, ruoloPorta, idPortaS, serviceBinding, fase, tipiPluginGiaUtilizzati, messaggioValoriNonDisponibili);
  157.                
  158.                 if(pd.getMessage() != null && pd.isDisableEditMode()) {
  159.                     dati = new ArrayList<>();
  160.                 }
  161.                
  162.                 // Set First is false
  163.                 confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FIRST_TIME);
  164.                
  165.                 pd.setDati(dati);

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

  167.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_SERVIZIO, ForwardParams.ADD());
  168.             }
  169.            
  170.             // Controlli sui campi immessi
  171.             boolean isOk = confHelper.handlerServizioCheckData(TipoOperazione.ADD, null, nomePlugin, stato, ruoloPorta, idPorta, fase, tipo, messaggioHandlerServizioDuplicato);
  172.             if (!isOk) {
  173.                 ServletUtils.setPageDataTitle(pd, lstParam);
  174.                
  175.                 // preparo i campi
  176.                 List<DataElement> dati = new ArrayList<>();
  177.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  178.                
  179.                 confHelper.addHandlerServizioToDati(dati, tipoOperazione, null, nomePlugin, stato, ruoloPorta, idPortaS, serviceBinding, fase, tipiPluginGiaUtilizzati, messaggioValoriNonDisponibili);
  180.                
  181.                 // Set First is false
  182.                 confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FIRST_TIME);
  183.                
  184.                 pd.setDati(dati);

  185.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  186.                
  187.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  188.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_SERVIZIO,
  189.                         ForwardParams.ADD());
  190.             }
  191.            
  192.             ConfigurazioneHandler handler = new ConfigurazioneHandler();
  193.            
  194.             handler.setTipo(plugin.getTipo());
  195.             handler.setStato(StatoFunzionalita.toEnumConstant(stato));
  196.             int posizione = confCore.getMaxPosizioneHandlersServizio(fase) + 1;
  197.             handler.setPosizione(posizione);
  198.            
  199.             List<Object> oggettiDaAggiornare = new ArrayList<>();
  200.             Configurazione configurazione = confCore.getConfigurazioneGenerale();
  201.            
  202.             if(configurazione.getConfigurazioneHandler() == null)
  203.                 configurazione.setConfigurazioneHandler(new ConfigurazioneGeneraleHandler());
  204.            
  205.             if(configurazione.getConfigurazioneHandler().getService() == null)
  206.                 configurazione.getConfigurazioneHandler().setService(new ConfigurazioneServiceHandlers());
  207.            
  208.             switch (faseSH) {
  209.             case EXIT:
  210.                 configurazione.getConfigurazioneHandler().getService().addExit(handler);
  211.                 break;
  212.             case INIT:
  213.                 configurazione.getConfigurazioneHandler().getService().addInit(handler);
  214.                 break;
  215.             case INTEGRATION_MANAGER_REQUEST:
  216.                 configurazione.getConfigurazioneHandler().getService().addIntegrationManagerRequest(handler);
  217.                 break;
  218.             case INTEGRATION_MANAGER_RESPONSE:
  219.                 configurazione.getConfigurazioneHandler().getService().addIntegrationManagerResponse(handler);
  220.                 break;
  221.             }
  222.             oggettiDaAggiornare.add(configurazione);
  223.            
  224.             // insert sul db
  225.             confCore.performUpdateOperation(userLogin, confHelper.smista(), oggettiDaAggiornare.toArray(new Object[oggettiDaAggiornare.size()]));
  226.            
  227.             // Preparo la lista
  228.             int idLista = Liste.CONFIGURAZIONE_HANDLERS_SERVIZIO;
  229.            
  230.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  231.            
  232.             ricerca = confHelper.checkSearchParameters(idLista, ricerca);

  233.             List<ConfigurazioneHandlerBean> lista = confCore.handlersServizioList(ricerca, fase);
  234.            
  235.             confHelper.prepareHandlersServizioList(ricerca, lista, ruoloPorta, idPortaS, serviceBinding, fase);
  236.            
  237.             if(ruoloPorta == null) {
  238.                 pd.setMessage(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_SERVICE_HANDLERS_MODIFICATA_CON_SUCCESSO, Costanti.MESSAGE_TYPE_INFO);
  239.             }
  240.            
  241.             // salvo l'oggetto ricerca nella sessione
  242.             ServletUtils.setSearchObjectIntoSession(request, session, ricerca);
  243.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  244.            
  245.             // Forward control to the specified success URI
  246.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_SERVIZIO, ForwardParams.ADD());
  247.         } catch (Exception e) {
  248.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  249.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_SERVIZIO, ForwardParams.ADD());
  250.         }  
  251.     }
  252. }