ConfigurazioneDiagnosticaDatasourceChange.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.Configurazione;
  31. import org.openspcoop2.core.config.MessaggiDiagnostici;
  32. import org.openspcoop2.core.config.OpenspcoopSorgenteDati;
  33. import org.openspcoop2.utils.TipiDatabase;
  34. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  35. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  36. import org.openspcoop2.web.lib.mvc.DataElement;
  37. import org.openspcoop2.web.lib.mvc.ForwardParams;
  38. import org.openspcoop2.web.lib.mvc.GeneralData;
  39. import org.openspcoop2.web.lib.mvc.PageData;
  40. import org.openspcoop2.web.lib.mvc.Parameter;
  41. import org.openspcoop2.web.lib.mvc.ServletUtils;
  42. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  43. /**
  44.  * diagnosticaDatasourceChange
  45.  *
  46.  * @author Andrea Poli (apoli@link.it)
  47.  * @author Stefano Corallo (corallo@link.it)
  48.  * @author Sandra Giangrandi (sandra@link.it)
  49.  * @author $Author$
  50.  * @version $Rev$, $Date$
  51.  *
  52.  */
  53. public final class ConfigurazioneDiagnosticaDatasourceChange extends Action {

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

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

  57.         // Inizializzo PageData
  58.         PageData pd = new PageData();

  59.         GeneralHelper generalHelper = new GeneralHelper(session);

  60.         // Inizializzo GeneralData
  61.         GeneralData gd = generalHelper.initGeneralData(request);

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

  63.         try {
  64.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);

  65.             String id = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ID);
  66.             int idInt = Integer.parseInt(id);
  67.             String nome = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_NOME);
  68.             String nomeJndi = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_NOME_JNDI);
  69.             String tipoDatabase = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_TIPO_DATABASE);

  70.             String[] tipiDb = TipiDatabase.toStringArray();
  71.             String[] tipoDbList = new String[tipiDb.length-1];
  72.             int k = 0;
  73.             for (int i = 0; i < tipiDb.length; i++) {
  74.                 if (!tipiDb[i].equals(ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_TIPO_DATABASE_DEFAULT)) {
  75.                     tipoDbList[k] = tipiDb[i];
  76.                     k++;
  77.                 }
  78.             }

  79.             ConfigurazioneCore confCore = new ConfigurazioneCore();

  80.             // Preparo il menu
  81.             confHelper.makeMenu();

  82.             // Prendo l'appender
  83.             Configurazione newConfigurazione = confCore.getConfigurazioneGenerale();
  84.             MessaggiDiagnostici md = newConfigurazione.getMessaggiDiagnostici();
  85.             List<OpenspcoopSorgenteDati> lista = md.getOpenspcoopSorgenteDatiList();
  86.             OpenspcoopSorgenteDati od = null;
  87.             for (int j = 0; j < md.sizeOpenspcoopSorgenteDatiList(); j++) {
  88.                 od = lista.get(j);
  89.                 if (idInt == od.getId().intValue()) {
  90.                     break;
  91.                 }
  92.             }

  93.             // Se idhid = null, devo visualizzare la pagina per la
  94.             // modifica dati
  95.             if (confHelper.isEditModeInProgress()) {
  96.                 // setto la barra del titolo
  97.                 List<Parameter> lstParam = new ArrayList<>();

  98.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_TRACCIAMENTO,
  99.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_TRACCIAMENTO_TRANSAZIONI));
  100.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_ELENCO_SORGENTI_DATI_MESSAGGI_DIAGNOSTICI,
  101.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_DIAGNOSTICA_DATASOURCE_LIST));
  102.                 lstParam.add(new Parameter(od.getNome(), null));

  103.                 ServletUtils.setPageDataTitle(pd, lstParam);

  104.                 // Prendo i dati dal db
  105.                 nome = od.getNome();
  106.                 nomeJndi = od.getNomeJndi();
  107.                 tipoDatabase = od.getTipoDatabase();

  108.                 // preparo i campi
  109.                 List<DataElement> dati = new ArrayList<>();
  110.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

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

  112.                 dati = confHelper.addDiagnosticaDatasourceToDati(TipoOperazione.CHANGE, od.getNome(), od.getNomeJndi(),
  113.                         od.getTipoDatabase(), tipoDbList, dati, od.getId()+"", od.sizePropertyList());

  114.                 pd.setDati(dati);

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

  116.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  117.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_DIAGNOSTICA_DATASOURCE,
  118.                         ForwardParams.CHANGE());
  119.             }

  120.             // Controlli sui campi immessi
  121.             boolean isOk = confHelper.diagnosticaDatasourceCheckData(TipoOperazione.CHANGE);
  122.             if (!isOk) {
  123.                 // setto la barra del titolo
  124.                 List<Parameter> lstParam = new ArrayList<>();

  125.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE,
  126.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  127.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_ELENCO_SORGENTI_DATI_MESSAGGI_DIAGNOSTICI,
  128.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_DIAGNOSTICA_DATASOURCE_LIST));
  129.                 lstParam.add(new Parameter(od.getNome(), null));

  130.                 ServletUtils.setPageDataTitle(pd, lstParam);


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

  133.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  134.                
  135.                 dati = confHelper.addHiddenFieldsToDati(TipoOperazione.CHANGE, id, null, null, dati);

  136.                 dati = confHelper.addDiagnosticaDatasourceToDati(TipoOperazione.CHANGE, od.getNome(), od.getNomeJndi(),
  137.                         od.getTipoDatabase(), tipoDbList, dati, od.getId()+"", od.sizePropertyList());

  138.                 pd.setDati(dati);

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

  140.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  141.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_DIAGNOSTICA_DATASOURCE,
  142.                         ForwardParams.CHANGE());
  143.             }

  144.             // Modifico i dati dell'appender nel db
  145.             for (int i = 0; i < md.sizeOpenspcoopSorgenteDatiList(); i++) {
  146.                 OpenspcoopSorgenteDati tmpOd = lista.get(i);
  147.                 if (idInt == tmpOd.getId().intValue()) {
  148.                     md.removeOpenspcoopSorgenteDati(i);
  149.                     break;
  150.                 }
  151.             }

  152.             OpenspcoopSorgenteDati newOd = new OpenspcoopSorgenteDati();
  153.             newOd.setNome(nome);
  154.             newOd.setNomeJndi(nomeJndi);
  155.             newOd.setTipoDatabase(tipoDatabase);
  156.             md.addOpenspcoopSorgenteDati(newOd);
  157.             newConfigurazione.setMessaggiDiagnostici(md);

  158.             confCore.performUpdateOperation(userLogin, confHelper.smista(), newConfigurazione);

  159.             // Preparo la lista
  160.             newConfigurazione = confCore.getConfigurazioneGenerale();
  161.             md = newConfigurazione.getMessaggiDiagnostici();
  162.             List<OpenspcoopSorgenteDati> lista1 = md.getOpenspcoopSorgenteDatiList();

  163.             confHelper.prepareDiagnosticaDatasourceList(lista1);

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

  165.             return ServletUtils.getStrutsForwardEditModeFinished(mapping,
  166.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_DIAGNOSTICA_DATASOURCE,
  167.                     ForwardParams.CHANGE());
  168.         } catch (Exception e) {
  169.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  170.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_DIAGNOSTICA_DATASOURCE, ForwardParams.CHANGE());
  171.         }
  172.     }
  173. }