ConfigurazioneRouteChange.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.  * routingChange
  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 ConfigurazioneRouteChange 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 id = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ID);
  71.             int idInt = Integer.parseInt(id);
  72.             String nome = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_NOME);
  73.             String tipo = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_TIPO);
  74.             String tiporotta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA);
  75.             String tiposoggrotta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_TIPO_SOGGETTO_ROTTA);
  76.             String nomesoggrotta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_NOME_SOGGETTO_ROTTA);
  77.             String registrorotta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_REGISTRO_ROTTA);

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

  96.             // Prendo nome e tipo del routing
  97.             RoutingTable rt = confCore.getRoutingTable();
  98.             RoutingTableDestinazione rtd = null;
  99.             for (int i = 0; i < rt.sizeDestinazioneList(); i++) {
  100.                 rtd = rt.getDestinazione(i);
  101.                 if (idInt == rtd.getId().intValue()) {
  102.                     break;
  103.                 }
  104.             }
  105.             // Quando arrivo qui, in rtd ho il mio routing
  106.             String oldNome = rtd.getNome();
  107.             String oldTipo = rtd.getTipo();
  108.             String titolo = oldTipo + "/" + oldNome;
  109.            
  110.             if(nome==null || "".equals(nome)){
  111.                 nome=rtd.getNome();
  112.             }
  113.             if(tipo==null || "".equals(tipo)){
  114.                 tipo=rtd.getTipo();
  115.             }

  116.             // Prendo la lista di registri e la metto in un array
  117.             // aggiungendo il campo "all"
  118.             List<AccessoRegistroRegistro> list = confCore.registriList(new ConsoleSearch(true));
  119.             String[] registriList = new String[list.size() + 1];
  120.             String[] registriListLabel = new String[list.size() + 1];
  121.             registriList[0] = "0";
  122.             registriListLabel[0] = ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_REGISTRO_ROTTA_ALL;
  123.             int i = 1;
  124.             for (AccessoRegistroRegistro arr : list) {
  125.                 registriList[i] = arr.getNome();
  126.                 registriListLabel[i] = arr.getNome();
  127.                 i++;
  128.             }

  129.             // Se nomehid = null, devo visualizzare la pagina per la
  130.             // modifica dati
  131.             if (confHelper.isEditModeInProgress()) {
  132.                 // setto la barra del titolo
  133.                 List<Parameter> lstParam = new ArrayList<>();

  134.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  135.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_TABELLA_DI_ROUTING,
  136.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_ROUTING));
  137.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_DESTINAZIONI,
  138.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_ROTTE_ROUTING_LIST));
  139.                 lstParam.add(new Parameter(titolo, null));

  140.                 ServletUtils.setPageDataTitle(pd, lstParam);

  141.                 Route r = rtd.getRoute(0);
  142.                 RouteGateway rg = r.getGateway();
  143.                 RouteRegistro rr = r.getRegistro();
  144.                 if (tiporotta == null) {
  145.                     if (rg != null) {
  146.                         tiporotta = ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA_GATEWAY;
  147.                     } else {
  148.                         tiporotta = ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA_REGISTRO;
  149.                     }
  150.                 }
  151.                 if (tiposoggrotta == null) {
  152.                     if (rg != null) {
  153.                         tiposoggrotta = rg.getTipo();
  154.                         nomesoggrotta = rg.getNome();
  155.                     } else {
  156.                         tiposoggrotta = "";
  157.                         nomesoggrotta = "";
  158.                     }
  159.                 }
  160.                 if (registrorotta == null) {
  161.                     if (rr != null) {
  162.                         registrorotta = rr.getNome();
  163.                     } else {
  164.                         registrorotta = "0";
  165.                     }
  166.                 }

  167.                 // preparo i campi
  168.                 List<DataElement> dati = new ArrayList<>();
  169.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  170.                 dati = confHelper.addHiddenFieldsToDati(TipoOperazione.CHANGE, id, null, null, dati);

  171.                 dati = confHelper.addValoriRottaToDati(TipoOperazione.CHANGE, nome, tipo, tiporotta, registrorotta,
  172.                         registriList, registriListLabel, dati, tiposoggrotta, nomesoggrotta,
  173.                         tipiSoggettiLabel,tipiSoggettiLabelPerProtocollo);

  174.                 pd.setDati(dati);

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

  176.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  177.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ROTTE_ROUTING,
  178.                         ForwardParams.CHANGE());
  179.             }

  180.             // Controlli sui campi immessi
  181.             boolean isOk = confHelper.routingListCheckData(TipoOperazione.CHANGE, registriList);
  182.             if (!isOk) {
  183.                 // setto la barra del titolo
  184.                 List<Parameter> lstParam = new ArrayList<>();

  185.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  186.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_TABELLA_DI_ROUTING,
  187.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_ROUTING));
  188.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_DESTINAZIONI,
  189.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_ROTTE_ROUTING_LIST));
  190.                 lstParam.add(new Parameter(titolo, null));

  191.                 ServletUtils.setPageDataTitle(pd, lstParam);

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

  194.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  195.                 dati = confHelper.addHiddenFieldsToDati(TipoOperazione.CHANGE, id, null, null, dati);

  196.                 dati = confHelper.addValoriRottaToDati(TipoOperazione.CHANGE, nome, tipo, tiporotta, registrorotta,
  197.                         registriList, registriListLabel, dati, tiposoggrotta, nomesoggrotta,
  198.                         tipiSoggettiLabel,tipiSoggettiLabelPerProtocollo);


  199.                 pd.setDati(dati);

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

  201.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  202.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ROTTE_ROUTING,
  203.                         ForwardParams.CHANGE());
  204.             }

  205.             // Modifico i dati del routing nel db
  206.             RoutingTableDestinazione rtdToUpdate = new RoutingTableDestinazione();
  207.             for (i = 0; i < rt.sizeDestinazioneList(); i++) {
  208.                 RoutingTableDestinazione tmpRtd = rt.getDestinazione(i);
  209.                 if (idInt == tmpRtd.getRoute(0).getId().intValue()) {
  210.                     rt.removeDestinazione(i);
  211.                     break;
  212.                 }
  213.             }

  214.             rtdToUpdate.setNome(nome);
  215.             rtdToUpdate.setTipo(tipo);

  216.             Route tmpR = new Route();
  217.             if (tiporotta.equals(ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA_GATEWAY)) {
  218.                 RouteGateway rg = new RouteGateway();
  219.                 rg.setTipo(tiposoggrotta);
  220.                 rg.setNome(nomesoggrotta);
  221.                 tmpR.setGateway(rg);
  222.             } else {
  223.                 RouteRegistro rr = new RouteRegistro();
  224.                 rr.setNome(registrorotta);
  225.                 tmpR.setRegistro(rr);
  226.             }
  227.             rtdToUpdate.addRoute(tmpR);
  228.             rt.addDestinazione(rtdToUpdate);

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

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

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

  233.             confHelper.prepareRoutingList(ricerca, lista);

  234.             pd.setMessage(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_TABELLA_ROUTING_MODIFICATA_CON_SUCCESSO, Costanti.MESSAGE_TYPE_INFO);
  235.            
  236.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  237.             return ServletUtils.getStrutsForwardEditModeFinished(mapping,
  238.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ROTTE_ROUTING,
  239.                     ForwardParams.CHANGE());
  240.         } catch (Exception e) {
  241.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  242.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ROTTE_ROUTING, ForwardParams.CHANGE());
  243.         }
  244.     }
  245. }