ConfigurazioneDiagnosticaDatasourceAdd.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.  * diagnosticaDatasourceAdd
  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 ConfigurazioneDiagnosticaDatasourceAdd 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.            
  66.             String nome = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_NOME);
  67.             String nomeJndi = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_NOME_JNDI);
  68.             String tipoDatabase = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_TIPO_DATABASE);

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

  78.             ConfigurazioneCore confCore = new ConfigurazioneCore();

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

  81.             // Se nome = null, devo visualizzare la pagina per l'inserimento
  82.             // dati
  83.             if (confHelper.isEditModeInProgress()) {
  84.                 // setto la barra del titolo
  85.                 List<Parameter> lstParam = new ArrayList<>();

  86.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_TRACCIAMENTO,
  87.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_TRACCIAMENTO_TRANSAZIONI));
  88.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_ELENCO_SORGENTI_DATI_MESSAGGI_DIAGNOSTICI,
  89.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_DIAGNOSTICA_DATASOURCE_LIST));
  90.                 lstParam.add(ServletUtils.getParameterAggiungi());

  91.                 ServletUtils.setPageDataTitle(pd, lstParam);

  92.                 // preparo i campi
  93.                 List<DataElement> dati = new ArrayList<>();
  94.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  95.                 dati = confHelper.addDiagnosticaDatasourceToDati(TipoOperazione.ADD, "", "", null,
  96.                         tipoDbList, dati, null, 0);
  97.                
  98.                 pd.setDati(dati);

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

  100.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  101.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_DIAGNOSTICA_DATASOURCE,
  102.                         ForwardParams.ADD());
  103.             }

  104.             // Controlli sui campi immessi
  105.             boolean isOk = confHelper.diagnosticaDatasourceCheckData(TipoOperazione.ADD);
  106.             if (!isOk) {
  107.                 // setto la barra del titolo
  108.                 List<Parameter> lstParam = new ArrayList<>();

  109.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE,
  110.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  111.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_ELENCO_SORGENTI_DATI_MESSAGGI_DIAGNOSTICI,
  112.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_DIAGNOSTICA_DATASOURCE_LIST));
  113.                 lstParam.add(ServletUtils.getParameterAggiungi());

  114.                 ServletUtils.setPageDataTitle(pd, lstParam);

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

  117.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  118.                
  119.                 dati = confHelper. addDiagnosticaDatasourceToDati(TipoOperazione.ADD, nome, nomeJndi, tipoDatabase,
  120.                         tipoDbList, dati, null,0);

  121.                 pd.setDati(dati);

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

  123.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  124.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_DIAGNOSTICA_DATASOURCE,
  125.                         ForwardParams.ADD());
  126.             }

  127.             // Inserisco l'appender nel db
  128.             Configurazione newConfigurazione = confCore.getConfigurazioneGenerale();
  129.             MessaggiDiagnostici md = newConfigurazione.getMessaggiDiagnostici();
  130.             if (md == null)
  131.                 md = new MessaggiDiagnostici();
  132.             OpenspcoopSorgenteDati od = new OpenspcoopSorgenteDati();
  133.             od.setNome(nome);
  134.             od.setNomeJndi(nomeJndi);
  135.             od.setTipoDatabase(tipoDatabase);
  136.             md.addOpenspcoopSorgenteDati(od);
  137.             newConfigurazione.setMessaggiDiagnostici(md);

  138.             confCore.performUpdateOperation(userLogin, confHelper.smista(), newConfigurazione);
  139.            
  140.             // Preparo la lista
  141.             newConfigurazione = confCore.getConfigurazioneGenerale();
  142.             md = newConfigurazione.getMessaggiDiagnostici();
  143.             List<OpenspcoopSorgenteDati> lista = md.getOpenspcoopSorgenteDatiList();

  144.             confHelper.prepareDiagnosticaDatasourceList(lista);

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

  146.             return ServletUtils.getStrutsForwardEditModeFinished(mapping,
  147.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_DIAGNOSTICA_DATASOURCE,
  148.                     ForwardParams.ADD());
  149.         } catch (Exception e) {
  150.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  151.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_DIAGNOSTICA_DATASOURCE, ForwardParams.ADD());
  152.         }
  153.     }
  154. }