PorteApplicativeTrasformazioniAdd.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.pa;

  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.Map;

  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpServletResponse;
  27. import javax.servlet.http.HttpSession;

  28. import org.apache.commons.lang.StringUtils;
  29. import org.apache.struts.action.Action;
  30. import org.apache.struts.action.ActionForm;
  31. import org.apache.struts.action.ActionForward;
  32. import org.apache.struts.action.ActionMapping;
  33. import org.openspcoop2.core.commons.Liste;
  34. import org.openspcoop2.core.config.PortaApplicativa;
  35. import org.openspcoop2.core.config.PortaApplicativaServizioApplicativo;
  36. import org.openspcoop2.core.config.TrasformazioneRegola;
  37. import org.openspcoop2.core.config.TrasformazioneRegolaApplicabilitaRichiesta;
  38. import org.openspcoop2.core.config.TrasformazioneRegolaRichiesta;
  39. import org.openspcoop2.core.config.Trasformazioni;
  40. import org.openspcoop2.core.config.constants.CostantiConfigurazione;
  41. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  42. import org.openspcoop2.core.config.driver.db.DriverConfigurazioneDBLib;
  43. import org.openspcoop2.core.id.IDServizio;
  44. import org.openspcoop2.core.mapping.MappingErogazionePortaApplicativa;
  45. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  46. import org.openspcoop2.core.registry.beans.AccordoServizioParteComuneSintetico;
  47. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  48. import org.openspcoop2.message.constants.ServiceBinding;
  49. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  50. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  51. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  52. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  53. import org.openspcoop2.web.ctrlstat.servlet.apc.AccordiServizioParteComuneCore;
  54. import org.openspcoop2.web.ctrlstat.servlet.aps.AccordiServizioParteSpecificaCore;
  55. import org.openspcoop2.web.lib.mvc.DataElement;
  56. import org.openspcoop2.web.lib.mvc.ForwardParams;
  57. import org.openspcoop2.web.lib.mvc.GeneralData;
  58. import org.openspcoop2.web.lib.mvc.PageData;
  59. import org.openspcoop2.web.lib.mvc.Parameter;
  60. import org.openspcoop2.web.lib.mvc.ServletUtils;
  61. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  62. /**
  63.  * PorteApplicativeTrasformazioniAdd
  64.  *
  65.  * @author Giuliano Pintori (pintori@link.it)
  66.  * @author $Author$
  67.  * @version $Rev$, $Date$
  68.  *
  69.  */
  70. public class PorteApplicativeTrasformazioniAdd extends Action {

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

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

  74.         // Inizializzo PageData
  75.         PageData pd = new PageData();

  76.         GeneralHelper generalHelper = new GeneralHelper(session);

  77.         String userLogin = ServletUtils.getUserLoginFromSession(session);  
  78.         // Inizializzo GeneralData
  79.         GeneralData gd = generalHelper.initGeneralData(request);

  80.         try {
  81.             PorteApplicativeHelper porteApplicativeHelper = new PorteApplicativeHelper(request, pd, session);
  82.             // prelevo il flag che mi dice da quale pagina ho acceduto la sezione delle porte applicative
  83.             Integer parentPA = ServletUtils.getIntegerAttributeFromSession(PorteApplicativeCostanti.ATTRIBUTO_PORTE_APPLICATIVE_PARENT, session, request);
  84.             if(parentPA == null) parentPA = PorteApplicativeCostanti.ATTRIBUTO_PORTE_APPLICATIVE_PARENT_NONE;
  85.             String idPorta = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID);
  86.             int idInt = Integer.parseInt(idPorta);
  87.             String idsogg = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO);
  88.             String idAsps = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS);
  89.             if(idAsps == null)
  90.                 idAsps = "";
  91.            
  92.            
  93.             String nome = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_NOME);
  94.             String stato = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_STATO);
  95.             if(stato==null || "".equals(stato)) {
  96.                 stato = StatoFunzionalita.DISABILITATO.getValue();
  97.             }
  98.             String azioniAllTmp = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_APPLICABILITA_AZIONI_ALL);
  99.             boolean azioniAll = azioniAllTmp==null || "".equals(azioniAllTmp) || CostantiControlStation.PARAMETRO_CONFIGURAZIONE_TRASFORMAZIONI_APPLICABILITA_AZIONI_ALL_VALUE_TRUE.equals(azioniAllTmp);
  100.             String [] azioni = porteApplicativeHelper.getParameterValues(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_APPLICABILITA_AZIONI);
  101.             String pattern = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_APPLICABILITA_PATTERN);
  102.             String contentType = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_APPLICABILITA_CT);
  103.             String [] connettori = porteApplicativeHelper.getParameterValues(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_APPLICABILITA_CONNETTORI);
  104.            
  105.            

  106.             PorteApplicativeCore porteApplicativeCore = new PorteApplicativeCore();
  107.             AccordiServizioParteSpecificaCore apsCore = new AccordiServizioParteSpecificaCore(porteApplicativeCore);
  108.             AccordiServizioParteComuneCore apcCore = new AccordiServizioParteComuneCore(porteApplicativeCore);
  109.             // Preparo il menu
  110.             porteApplicativeHelper.makeMenu();

  111.             // Prendo nome della porta applicativa
  112.             PortaApplicativa pa = porteApplicativeCore.getPortaApplicativa(idInt);
  113.             if(pa==null) {
  114.                 throw new Exception("PortaApplicativa con id '"+idInt+"' non trovata");
  115.             }
  116.             String nomePorta = pa.getNome();
  117.            
  118.            
  119.             MappingErogazionePortaApplicativa mappingAssociatoPorta = porteApplicativeCore.getMappingErogazionePortaApplicativa(pa);
  120.            
  121.             String[] azioniDisponibiliList = null;
  122.             String[] azioniDisponibiliLabelList = null;
  123.                        
  124.             List<String> azioniAssociatePorta = new ArrayList<>();
  125.             if(pa.getAzione() != null && pa.getAzione().getAzioneDelegataList() != null)
  126.                 azioniAssociatePorta.addAll(pa.getAzione().getAzioneDelegataList());

  127.             AccordoServizioParteSpecifica asps = apsCore.getAccordoServizioParteSpecifica(Integer.parseInt(idAsps));
  128.             AccordoServizioParteComuneSintetico apc = apcCore.getAccordoServizioSintetico(asps.getIdAccordo());
  129.             ServiceBinding serviceBinding = apcCore.toMessageServiceBinding(apc.getServiceBinding());
  130.             Map<String,String> azioniAccordo = porteApplicativeCore.getAzioniConLabel(asps, apc, false, true, new ArrayList<>());
  131.            
  132.             if(azioniAccordo!=null && azioniAccordo.size()>0) {
  133.                 // porte ridefinite
  134.                 if(!mappingAssociatoPorta.isDefault()) {
  135.                    
  136.                     azioniDisponibiliList = new String[azioniAssociatePorta.size()];
  137.                     azioniDisponibiliLabelList = new String[azioniAssociatePorta.size()];
  138.                     int i = 0;
  139.                     for (String string : azioniAccordo.keySet()) {
  140.                         if(azioniAssociatePorta.contains(string)) {
  141.                             azioniDisponibiliList[i] = string;
  142.                             azioniDisponibiliLabelList[i] = azioniAccordo.get(string);
  143.                             i++;
  144.                         }
  145.                     }
  146.                 } else { // elenco azioni non associate a nessun mapping
  147.                     IDServizio idServizio2 = IDServizioFactory.getInstance().getIDServizioFromAccordo(asps);
  148.                     List<MappingErogazionePortaApplicativa> listaMappingErogazione = apsCore.mappingServiziPorteAppList(idServizio2,asps.getId(), null);
  149.                     List<String> azioniOccupate = new ArrayList<>();
  150.                     int listaMappingErogazioneSize = listaMappingErogazione != null ? listaMappingErogazione.size() : 0;
  151.                     if(listaMappingErogazioneSize > 0) {
  152.                         for (int i = 0; i < listaMappingErogazione.size(); i++) {
  153.                             MappingErogazionePortaApplicativa mappingErogazionePortaApplicativa = listaMappingErogazione.get(i);
  154.                             // colleziono le azioni gia' configurate
  155.                             PortaApplicativa portaApplicativa = porteApplicativeCore.getPortaApplicativa(mappingErogazionePortaApplicativa.getIdPortaApplicativa());
  156.                             if(portaApplicativa.getAzione() != null && portaApplicativa.getAzione().getAzioneDelegataList() != null)
  157.                                 azioniOccupate.addAll(portaApplicativa.getAzione().getAzioneDelegataList());
  158.                         }
  159.                     }
  160.                     Map<String,String> azioniAccordoDisponibili = porteApplicativeCore.getAzioniConLabel(asps, apc, false, true, azioniOccupate);
  161.                    
  162.                     azioniDisponibiliList = new String[azioniAccordoDisponibili.size()];
  163.                     azioniDisponibiliLabelList = new String[azioniAccordoDisponibili.size()];
  164.                     int i = 0;
  165.                     for (String string : azioniAccordoDisponibili.keySet()) {
  166.                         azioniDisponibiliList[i] = string;
  167.                         azioniDisponibiliLabelList[i] = azioniAccordoDisponibili.get(string);
  168.                         i++;
  169.                     }
  170.                 }
  171.             }
  172.            
  173.            
  174.             String[] connettoriDisponibiliList = null;
  175.             String[] connettoriDisponibiliLabelList = null;
  176.             if(pa!=null && pa.getBehaviour()!=null && pa.sizeServizioApplicativoList()>0) {
  177.                 connettoriDisponibiliList = new String[pa.sizeServizioApplicativoList()];
  178.                 connettoriDisponibiliLabelList = new String[pa.sizeServizioApplicativoList()];
  179.                 int index = 0;
  180.                 for (PortaApplicativaServizioApplicativo pasa : pa.getServizioApplicativoList()) {
  181.                     String nomeServizioApplicativoErogatoreConnettoreMultiplo = pasa.getNome();
  182.                     String nomeConnettoreMultiplo = pasa.getDatiConnettore()!= null ? pasa.getDatiConnettore().getNome() : null;
  183.                     if(nomeConnettoreMultiplo==null) {
  184.                         nomeConnettoreMultiplo=CostantiConfigurazione.NOME_CONNETTORE_DEFAULT;
  185.                     }
  186.                     connettoriDisponibiliList[index] = nomeServizioApplicativoErogatoreConnettoreMultiplo;
  187.                     connettoriDisponibiliLabelList[index] = nomeConnettoreMultiplo;
  188.                     index++;
  189.                 }
  190.             }
  191.            

  192.             List<Parameter> lstParam = porteApplicativeHelper.getTitoloPA(parentPA, idsogg, idAsps);

  193.             String labelPerPorta = null;
  194.             if(parentPA!=null && (parentPA.intValue() == PorteApplicativeCostanti.ATTRIBUTO_PORTE_APPLICATIVE_PARENT_CONFIGURAZIONE)) {
  195.                 labelPerPorta = porteApplicativeCore.getLabelRegolaMappingErogazionePortaApplicativa(
  196.                         PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_DI,
  197.                         PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI,
  198.                         pa);
  199.             }
  200.             else {
  201.                 labelPerPorta = PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_DI+nomePorta;
  202.             }

  203.             lstParam.add(new Parameter(labelPerPorta,
  204.                     PorteApplicativeCostanti.SERVLET_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_LIST,
  205.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID, idPorta),
  206.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO, idsogg),
  207.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS, idAsps)
  208.                     ));
  209.             lstParam.add(ServletUtils.getParameterAggiungi());

  210.             ServletUtils.setPageDataTitle(pd, lstParam);

  211.             // Se nomehid = null, devo visualizzare la pagina per l'inserimento
  212.             // dati
  213.             if (porteApplicativeHelper.isEditModeInProgress()) {
  214.                 // preparo i campi
  215.                 List<DataElement> dati = new ArrayList<>();
  216.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  217.                
  218.                 dati = porteApplicativeHelper.addTrasformazioneToDatiOpAdd(dati, pa, nome,
  219.                         stato, azioniAll, azioniDisponibiliList, azioniDisponibiliLabelList, azioni, pattern, contentType,
  220.                         connettoriDisponibiliList, connettoriDisponibiliLabelList, connettori,
  221.                         apc.getServiceBinding(),false);
  222.                
  223.                 dati = porteApplicativeHelper.addHiddenFieldsToDati(TipoOperazione.ADD, idPorta, idsogg, idPorta,idAsps,  dati);
  224.                
  225.                 pd.setDati(dati);

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

  227.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI,  ForwardParams.ADD());
  228.             }
  229.            
  230.             // quando un parametro viene inviato come vuoto, sul db viene messo null, gestisco il caso
  231.             String azioniAsString = azioni != null ? StringUtils.join(Arrays.asList(azioni), ",") : "";
  232.             String azioniDBCheck = StringUtils.isNotEmpty(azioniAsString) ? azioniAsString : null;
  233.             String patternDBCheck = StringUtils.isNotEmpty(pattern) ? pattern : null;
  234.             String contentTypeDBCheck = StringUtils.isNotEmpty(contentType) ? contentType : null;
  235.             String connettoriAsString = connettori != null ? StringUtils.join(Arrays.asList(connettori), ",") : "";
  236.             String connettoriDBCheck = StringUtils.isNotEmpty(connettoriAsString) ? connettoriAsString : null;
  237.             TrasformazioneRegola trasformazioneDBCheckCriteri = porteApplicativeCore.getTrasformazione(Long.parseLong(idPorta), azioniDBCheck, patternDBCheck, contentTypeDBCheck, connettoriDBCheck,
  238.                     null, null, true);
  239.             TrasformazioneRegola trasformazioneDBCheckNome = porteApplicativeCore.getTrasformazione(Long.parseLong(idPorta), nome);
  240.            
  241.             boolean isOk = porteApplicativeHelper.trasformazioniCheckData(TipoOperazione.ADD, Long.parseLong(idPorta), nome, trasformazioneDBCheckCriteri, trasformazioneDBCheckNome, null,
  242.                     serviceBinding);
  243.             if (!isOk) {

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

  246.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  247.                
  248.                 dati = porteApplicativeHelper.addTrasformazioneToDatiOpAdd(dati, pa, nome,
  249.                         stato, azioniAll, azioniDisponibiliList, azioniDisponibiliLabelList, azioni, pattern, contentType,
  250.                         connettoriDisponibiliList, connettoriDisponibiliLabelList, connettori,
  251.                         apc.getServiceBinding(),false);
  252.                
  253.                 dati = porteApplicativeHelper.addHiddenFieldsToDati(TipoOperazione.ADD, idPorta, idsogg, idPorta,idAsps,  dati);
  254.                
  255.                 pd.setDati(dati);

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

  257.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI,  ForwardParams.ADD());
  258.             }
  259.            
  260.             // salvataggio nuova regola
  261.            
  262.             if(pa.getTrasformazioni() == null) {
  263.                 pa.setTrasformazioni(new Trasformazioni());
  264.             }
  265.            
  266.             // calcolo prossima posizione
  267.             int posizione = 1;
  268.             for (TrasformazioneRegola check : pa.getTrasformazioni().getRegolaList()) {
  269.                 if(check.getPosizione()>=posizione) {
  270.                     posizione = check.getPosizione()+1;
  271.                 }
  272.             }
  273.            
  274.             TrasformazioneRegola regola = new TrasformazioneRegola();
  275.             regola.setPosizione(posizione);
  276.             regola.setNome(nome);
  277.             regola.setStato(DriverConfigurazioneDBLib.getEnumStatoFunzionalita(stato));
  278.             TrasformazioneRegolaApplicabilitaRichiesta applicabilita = new TrasformazioneRegolaApplicabilitaRichiesta();
  279.            
  280.             applicabilita.setPattern(pattern);
  281.             if(contentType != null) {
  282.                 applicabilita.getContentTypeList().addAll(Arrays.asList(contentType.split(",")));
  283.             }
  284.            
  285.             if(azioni != null && azioni.length > 0) {
  286.                 for (String azione : azioni) {
  287.                     applicabilita.addAzione(azione);
  288.                 }
  289.             }
  290.            
  291.             if(connettori != null && connettori.length > 0) {
  292.                 for (String connettore : connettori) {
  293.                     applicabilita.addConnettore(connettore);
  294.                 }
  295.             }
  296.            
  297.             regola.setApplicabilita(applicabilita);
  298.             regola.setRichiesta(new TrasformazioneRegolaRichiesta());
  299.             pa.getTrasformazioni().addRegola(regola);
  300.            
  301.             porteApplicativeCore.performUpdateOperation(userLogin, porteApplicativeHelper.smista(), pa);
  302.            
  303.             // Preparo la lista
  304.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  305.            
  306.             int idLista = Liste.PORTE_APPLICATIVE_TRASFORMAZIONI;
  307.            
  308.             ricerca = porteApplicativeHelper.checkSearchParameters(idLista, ricerca);
  309.            
  310.             List<TrasformazioneRegola> lista = porteApplicativeCore.porteAppTrasformazioniList(Long.parseLong(idPorta), ricerca);
  311.            
  312.             porteApplicativeHelper.preparePorteAppTrasformazioniRegolaList(nomePorta, ricerca, lista);
  313.                        
  314.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  315.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI, ForwardParams.ADD());


  316.         } catch (Exception e) {
  317.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI, ForwardParams.ADD());
  318.         }
  319.     }
  320. }