ConfigurazioneTracciamentoDatasourcePropertiesAdd.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.OpenspcoopSorgenteDati;
  32. import org.openspcoop2.core.config.Property;
  33. import org.openspcoop2.core.config.Tracciamento;
  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.DataElementType;
  38. import org.openspcoop2.web.lib.mvc.ForwardParams;
  39. import org.openspcoop2.web.lib.mvc.GeneralData;
  40. import org.openspcoop2.web.lib.mvc.PageData;
  41. import org.openspcoop2.web.lib.mvc.Parameter;
  42. import org.openspcoop2.web.lib.mvc.ServletUtils;
  43. import org.openspcoop2.web.lib.mvc.TipoOperazione;

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

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

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

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

  60.         GeneralHelper generalHelper = new GeneralHelper(session);

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

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

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

  66.             String id = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ID);
  67.             int idInt = Integer.parseInt(id);
  68.             String nome = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_NOME);
  69.             String valore = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_VALORE);

  70.             ConfigurazioneCore confCore = new ConfigurazioneCore();

  71.             // Preparo il menu
  72.             confHelper.makeMenu();

  73.             // Prendo l'appender
  74.             Configurazione newConfigurazione = confCore.getConfigurazioneGenerale();
  75.             Tracciamento t = newConfigurazione.getTracciamento();
  76.             List<OpenspcoopSorgenteDati> lista = t.getOpenspcoopSorgenteDatiList();
  77.             OpenspcoopSorgenteDati od = null;
  78.             for (int j = 0; j < t.sizeOpenspcoopSorgenteDatiList(); j++) {
  79.                 od = lista.get(j);
  80.                 if (idInt == od.getId().intValue()) {
  81.                     break;
  82.                 }
  83.             }

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

  89.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_TRACCIAMENTO,
  90.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_TRACCIAMENTO_TRANSAZIONI));
  91.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_ELENCO_SORGENTI_DATI_TRACCIAMENTO,
  92.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_TRACCIAMENTO_DATASOURCE_LIST));
  93.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_PROPRIETA + " di " + od.getNome(),
  94.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_TRACCIAMENTO_DATASOURCE_PROPERTIES_LIST,
  95.                         new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ID, od.getId()+"")
  96.                         ));
  97.                 lstParam.add(ServletUtils.getParameterAggiungi());

  98.                 ServletUtils.setPageDataTitle(pd, lstParam);

  99.                 // preparo i campi
  100.                 List<DataElement> dati = new ArrayList<>();
  101.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  102.                 DataElement dataElement = new DataElement();
  103.                 dataElement.setLabel(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_PROPRIETA);
  104.                 dataElement.setType(DataElementType.TITLE);
  105.                 dati.add(dataElement);
  106.                
  107.                 dati = confHelper.addNomeValoreToDati(TipoOperazione.ADD, dati, "", "", false);

  108.                 dati = confHelper.addHiddenFieldsToDati(TipoOperazione.ADD, id, null, null, dati);

  109.                 pd.setDati(dati);

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

  111.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  112.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_TRACCIAMENTO_DATASOURCE_PROPERTIES,
  113.                         ForwardParams.ADD());
  114.             }

  115.             // Controlli sui campi immessi
  116.             boolean isOk = confHelper.tracciamentoDatasourcePropCheckData(TipoOperazione.ADD);
  117.             if (!isOk) {
  118.                 // setto la barra del titolo
  119.                 List<Parameter> lstParam = new ArrayList<>();

  120.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE,
  121.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  122.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_ELENCO_SORGENTI_DATI_TRACCIAMENTO,
  123.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_TRACCIAMENTO_DATASOURCE_LIST));
  124.                 lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_PROPRIETA + " di " + od.getNome(),
  125.                         ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_TRACCIAMENTO_DATASOURCE_PROPERTIES_LIST,
  126.                         new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ID, od.getId()+"")
  127.                         ));
  128.                 lstParam.add(ServletUtils.getParameterAggiungi());

  129.                 ServletUtils.setPageDataTitle(pd, lstParam);

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

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

  133.                 DataElement dataElement = new DataElement();
  134.                 dataElement.setLabel(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_PROPRIETA);
  135.                 dataElement.setType(DataElementType.TITLE);
  136.                 dati.add(dataElement);
  137.                
  138.                 dati = confHelper.addNomeValoreToDati(TipoOperazione.ADD, dati, nome, valore,false);

  139.                 dati = confHelper.addHiddenFieldsToDati(TipoOperazione.ADD, id, null, null, dati);

  140.                 pd.setDati(dati);

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

  142.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  143.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_TRACCIAMENTO_DATASOURCE_PROPERTIES,
  144.                         ForwardParams.ADD());
  145.             }

  146.             // Inserisco la proprietà nel db
  147.             Property odp = new Property();
  148.             odp.setNome(nome);
  149.             odp.setValore(valore);
  150.             od.addProperty(odp);

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

  152.             // Preparo la lista
  153.             newConfigurazione = confCore.getConfigurazioneGenerale();
  154.             t = newConfigurazione.getTracciamento();
  155.             lista = t.getOpenspcoopSorgenteDatiList();
  156.             od = null;
  157.             for (int j = 0; j < t.sizeOpenspcoopSorgenteDatiList(); j++) {
  158.                 od = lista.get(j);
  159.                 if (idInt == od.getId().intValue()) {
  160.                     break;
  161.                 }
  162.             }
  163.            
  164.             if(od==null) {
  165.                 throw new Exception("Datasource non trovato");
  166.             }
  167.            
  168.             List<Property> lista1 = od.getPropertyList();

  169.             confHelper.prepareTracciamentoDatasourcePropList(od, lista1);

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

  171.             return ServletUtils.getStrutsForwardEditModeFinished(mapping,
  172.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_TRACCIAMENTO_DATASOURCE_PROPERTIES,
  173.                     ForwardParams.ADD());
  174.         } catch (Exception e) {
  175.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  176.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_TRACCIAMENTO_DATASOURCE_PROPERTIES, ForwardParams.ADD());
  177.         }
  178.     }
  179. }