ConfigurazioneRouteAdd.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.util.ArrayList;
  22. import java.util.List;

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

  26. import org.apache.struts.action.Action;
  27. import org.apache.struts.action.ActionForm;
  28. import org.apache.struts.action.ActionForward;
  29. import org.apache.struts.action.ActionMapping;
  30. import org.openspcoop2.core.config.AccessoRegistroRegistro;
  31. import org.openspcoop2.core.config.Route;
  32. import org.openspcoop2.core.config.RouteGateway;
  33. import org.openspcoop2.core.config.RouteRegistro;
  34. import org.openspcoop2.core.config.RoutingTable;
  35. import org.openspcoop2.core.config.RoutingTableDestinazione;
  36. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  37. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  38. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  39. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  40. import org.openspcoop2.web.lib.mvc.Costanti;
  41. import org.openspcoop2.web.lib.mvc.DataElement;
  42. import org.openspcoop2.web.lib.mvc.ForwardParams;
  43. import org.openspcoop2.web.lib.mvc.GeneralData;
  44. import org.openspcoop2.web.lib.mvc.PageData;
  45. import org.openspcoop2.web.lib.mvc.Parameter;
  46. import org.openspcoop2.web.lib.mvc.ServletUtils;
  47. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  48. /**
  49.  * routingAdd
  50.  *
  51.  * @author Andrea Poli (apoli@link.it)
  52.  * @author Stefano Corallo (corallo@link.it)
  53.  * @author Sandra Giangrandi (sandra@link.it)
  54.  * @author $Author$
  55.  * @version $Rev$, $Date$
  56.  *
  57.  */
  58. public final class ConfigurazioneRouteAdd extends Action {

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

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

  62.         // Inizializzo PageData
  63.         PageData pd = new PageData();

  64.         GeneralHelper generalHelper = new GeneralHelper(session);

  65.         // Inizializzo GeneralData
  66.         GeneralData gd = generalHelper.initGeneralData(request);

  67.         String userLogin = ServletUtils.getUserLoginFromSession(session);  

  68.         try {
  69.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);

  70.             String nome = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_NOME);
  71.             String tipo = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_TIPO);
  72.             String tiporotta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA);
  73.             if (tiporotta == null) {
  74.                 tiporotta = ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA_REGISTRO;
  75.             }
  76.             String tiposoggrotta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_TIPO_SOGGETTO_ROTTA);
  77.             String nomesoggrotta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_NOME_SOGGETTO_ROTTA);
  78.             String registrorotta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_REGISTRO_ROTTA);

  79.             ConfigurazioneCore confCore = new ConfigurazioneCore();
  80.             SoggettiCore soggettiCore = new SoggettiCore(confCore);

  81.             // Soggetti
  82.             List<String> tipiSoggetti = new ArrayList<>();
  83.             tipiSoggetti.add("-");
  84.             tipiSoggetti.addAll(soggettiCore.getTipiSoggettiGestiti());
  85.             String[] tipiSoggettiLabel = tipiSoggetti.toArray(new String[1]);
  86.            
  87.             String[] tipiSoggettiLabelPerProtocollo = tipiSoggettiLabel;
  88.             if(tipo!=null && !"".equals(tipo) && !"-".equals(tipo)){
  89.                 List<String> tipiSoggettiPerRotta = new ArrayList<>();
  90.                 tipiSoggettiPerRotta.add("-");
  91.                 tipiSoggettiPerRotta.addAll(soggettiCore.getTipiSoggettiGestitiProtocollo(soggettiCore.getProtocolloAssociatoTipoSoggetto(tipo)));
  92.                 tipiSoggettiLabelPerProtocollo = tipiSoggettiPerRotta.toArray(new String[1]);
  93.             }
  94.            
  95.             // Preparo il menu
  96.             confHelper.makeMenu();

  97.             // Prendo la lista di registri e la metto in un array
  98.             // aggiungendo il campo "all"
  99.             List<AccessoRegistroRegistro> list = confCore.registriList(new ConsoleSearch(true));
  100.             String[] registriList = new String[list.size() + 1];
  101.             String[] registriListLabel = new String[list.size() + 1];
  102.             registriList[0] = "0";
  103.             registriListLabel[0] = ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_REGISTRO_ROTTA_ALL;
  104.             int i = 1;
  105.             for (AccessoRegistroRegistro arr : list) {
  106.                 registriList[i] = arr.getNome();
  107.                 registriListLabel[i] = arr.getNome();
  108.                 i++;
  109.             }

  110.             // Se nomehid = null, devo visualizzare la pagina per l'inserimento
  111.             // dati
  112.             if (confHelper.isEditModeInProgress()) {
  113.                 // setto la barra del titolo
  114.                 List<Parameter> lstParam = new ArrayList<>();

  115.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  116.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_TABELLA_DI_ROUTING,
  117.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_ROUTING));
  118.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_DESTINAZIONI,
  119.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_ROTTE_ROUTING_LIST));
  120.                 lstParam.add(ServletUtils.getParameterAggiungi());

  121.                 ServletUtils.setPageDataTitle(pd, lstParam);

  122.                 // preparo i campi
  123.                 List<DataElement> dati = new ArrayList<>();
  124.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  125.                 dati = confHelper.addValoriRottaToDati(TipoOperazione.ADD,  nome, tipo, tiporotta,
  126.                         ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_REGISTRO_ROTTA_ALL,
  127.                          registriList, registriListLabel, dati, "","",
  128.                          tipiSoggettiLabel,tipiSoggettiLabelPerProtocollo);

  129.                 pd.setDati(dati);

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

  131.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  132.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ROTTE_ROUTING,
  133.                         ForwardParams.ADD());
  134.             }

  135.             // Controlli sui campi immessi
  136.             boolean isOk = confHelper.routingListCheckData(TipoOperazione.ADD, registriList);
  137.             if (!isOk) {
  138.                 // setto la barra del titolo
  139.                 List<Parameter> lstParam = new ArrayList<>();

  140.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  141.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_TABELLA_DI_ROUTING,
  142.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_ROUTING));
  143.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_DESTINAZIONI,
  144.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_ROTTE_ROUTING_LIST));
  145.                 lstParam.add(ServletUtils.getParameterAggiungi());

  146.                 ServletUtils.setPageDataTitle(pd, lstParam);

  147.                 // preparo i campi
  148.                 List<DataElement> dati = new ArrayList<>();

  149.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  150.                
  151.                 dati =  confHelper.addValoriRottaToDati(TipoOperazione.ADD, nome, tipo, tiporotta,
  152.                         registrorotta,  registriList,
  153.                         registriListLabel, dati, "","",
  154.                         tipiSoggettiLabel,tipiSoggettiLabelPerProtocollo);

  155.                 pd.setDati(dati);

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

  157.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  158.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ROTTE_ROUTING,
  159.                         ForwardParams.ADD());
  160.             }

  161.             // Inserisco il routing nel db
  162.             RoutingTable rt = confCore.getRoutingTable();
  163.             RoutingTableDestinazione rtd = new RoutingTableDestinazione();
  164.             rtd.setNome(nome);
  165.             rtd.setTipo(tipo);
  166.             Route tmpR = new Route();
  167.             if (tiporotta.equals(ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA_GATEWAY)) {
  168.                 RouteGateway rg = new RouteGateway();
  169.                 rg.setTipo(tiposoggrotta);
  170.                 rg.setNome(nomesoggrotta);
  171.                 tmpR.setGateway(rg);
  172.             } else {
  173.                 RouteRegistro rr = new RouteRegistro();
  174.                 rr.setNome(registrorotta);
  175.                 tmpR.setRegistro(rr);
  176.             }
  177.             rtd.addRoute(tmpR);
  178.             rt.addDestinazione(rtd);

  179.             confCore.performUpdateOperation(userLogin, confHelper.smista(), rt);

  180.             // Preparo la lista
  181.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);

  182.             List<RoutingTableDestinazione> lista = confCore.routingList(ricerca);

  183.             confHelper.prepareRoutingList(ricerca, lista);

  184.             pd.setMessage(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_TABELLA_ROUTING_MODIFICATA_CON_SUCCESSO, Costanti.MESSAGE_TYPE_INFO);
  185.            
  186.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  187.             return ServletUtils.getStrutsForwardEditModeFinished(mapping,
  188.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ROTTE_ROUTING,
  189.                     ForwardParams.ADD());
  190.         } catch (Exception e) {
  191.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  192.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ROTTE_ROUTING, ForwardParams.ADD());
  193.         }
  194.     }


  195. }