PorteDelegateTrasformazioniAdd.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.pd;

  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.PortaDelegata;
  35. import org.openspcoop2.core.config.TrasformazioneRegola;
  36. import org.openspcoop2.core.config.TrasformazioneRegolaApplicabilitaRichiesta;
  37. import org.openspcoop2.core.config.TrasformazioneRegolaRichiesta;
  38. import org.openspcoop2.core.config.Trasformazioni;
  39. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  40. import org.openspcoop2.core.config.driver.db.DriverConfigurazioneDBLib;
  41. import org.openspcoop2.core.id.IDServizio;
  42. import org.openspcoop2.core.id.IDSoggetto;
  43. import org.openspcoop2.core.mapping.MappingFruizionePortaDelegata;
  44. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  45. import org.openspcoop2.core.registry.beans.AccordoServizioParteComuneSintetico;
  46. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  47. import org.openspcoop2.message.constants.ServiceBinding;
  48. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  49. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  50. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  51. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  52. import org.openspcoop2.web.ctrlstat.servlet.apc.AccordiServizioParteComuneCore;
  53. import org.openspcoop2.web.ctrlstat.servlet.aps.AccordiServizioParteSpecificaCore;
  54. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  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.  * PorteDelegateTrasformazioniAdd
  64.  *
  65.  * @author Giuliano Pintori (pintori@link.it)
  66.  * @author $Author$
  67.  * @version $Rev$, $Date$
  68.  *
  69.  */
  70. public class PorteDelegateTrasformazioniAdd 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.             PorteDelegateHelper porteDelegateHelper = new PorteDelegateHelper(request, pd, session);
  82.             // prelevo il flag che mi dice da quale pagina ho acceduto la sezione delle porte delegate
  83.             Integer parentPD = ServletUtils.getIntegerAttributeFromSession(PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT, session, request);
  84.             if(parentPD == null) parentPD = PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT_NONE;
  85.             String id = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID);
  86.             int idInt = Integer.parseInt(id);
  87.             String idsogg = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO);
  88.            
  89.             String idAsps = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_ASPS);
  90.             if(idAsps == null)
  91.                 idAsps = "";
  92.            
  93.             String idFruizione = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE);
  94.             if(idFruizione == null)
  95.                 idFruizione = "";
  96.             Long idFru = Long.parseLong(idFruizione);
  97.            
  98.             String nome = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_NOME);
  99.             String stato = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_STATO);
  100.             if(stato==null || "".equals(stato)) {
  101.                 stato = StatoFunzionalita.DISABILITATO.getValue();
  102.             }
  103.             String azioniAllTmp = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_APPLICABILITA_AZIONI_ALL);
  104.             boolean azioniAll = azioniAllTmp==null || "".equals(azioniAllTmp) || CostantiControlStation.PARAMETRO_CONFIGURAZIONE_TRASFORMAZIONI_APPLICABILITA_AZIONI_ALL_VALUE_TRUE.equals(azioniAllTmp);
  105.             String [] azioni = porteDelegateHelper.getParameterValues(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_APPLICABILITA_AZIONI);
  106.             String pattern = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_APPLICABILITA_PATTERN);
  107.             String contentType = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_APPLICABILITA_CT);
  108.            
  109.            
  110.             Parameter pId = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID, id);
  111.             Parameter pIdSoggetto = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO, idsogg);
  112.             Parameter pIdAsps = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_ASPS, idAsps);
  113.             Parameter pIdFruizione = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE, idFruizione);

  114.             PorteDelegateCore porteDelegateCore = new PorteDelegateCore();
  115.             AccordiServizioParteSpecificaCore apsCore = new AccordiServizioParteSpecificaCore(porteDelegateCore);
  116.             AccordiServizioParteComuneCore apcCore = new AccordiServizioParteComuneCore(porteDelegateCore);
  117.             SoggettiCore soggettiCore = new SoggettiCore(porteDelegateCore);
  118.             // Preparo il menu
  119.             porteDelegateHelper.makeMenu();

  120.             // Prendo nome della porta applicativa
  121.             PortaDelegata portaDelegata = porteDelegateCore.getPortaDelegata(idInt);
  122.             String nomePorta = portaDelegata.getNome();
  123.            
  124.            
  125.             MappingFruizionePortaDelegata mappingAssociatoPorta = porteDelegateCore.getMappingFruizionePortaDelegata(portaDelegata);
  126.            
  127.             String[] azioniDisponibiliList = null;
  128.             String[] azioniDisponibiliLabelList = null;
  129.            
  130.            
  131.            
  132.            
  133.             List<String> azioniAssociatePorta = new ArrayList<>();
  134.             if(portaDelegata.getAzione() != null && portaDelegata.getAzione().getAzioneDelegataList() != null)
  135.                 azioniAssociatePorta.addAll(portaDelegata.getAzione().getAzioneDelegataList());

  136.             AccordoServizioParteSpecifica asps = apsCore.getAccordoServizioParteSpecifica(Integer.parseInt(idAsps));
  137.             AccordoServizioParteComuneSintetico apc = apcCore.getAccordoServizioSintetico(asps.getIdAccordo());
  138.             ServiceBinding serviceBinding = apcCore.toMessageServiceBinding(apc.getServiceBinding());
  139.             Map<String,String> azioniAccordo = porteDelegateCore.getAzioniConLabel(asps, apc, false, true, new ArrayList<>());
  140.            
  141.             if(azioniAccordo!=null && azioniAccordo.size()>0) {
  142.                 // porte ridefinite
  143.                 if(!mappingAssociatoPorta.isDefault()) {
  144.                    
  145.                     azioniDisponibiliList = new String[azioniAssociatePorta.size()];
  146.                     azioniDisponibiliLabelList = new String[azioniAssociatePorta.size()];
  147.                     int i = 0;
  148.                     for (String string : azioniAccordo.keySet()) {
  149.                         if(azioniAssociatePorta.contains(string)) {
  150.                             azioniDisponibiliList[i] = string;
  151.                             azioniDisponibiliLabelList[i] = azioniAccordo.get(string);
  152.                             i++;
  153.                         }
  154.                     }
  155.                 } else { // elenco azioni non associate a nessun mapping
  156.                     IDServizio idServizioFromAccordo = IDServizioFactory.getInstance().getIDServizioFromAccordo(asps);
  157.                     IDSoggetto idSoggettoFruitore = new IDSoggetto();
  158.                     String tipoSoggettoFruitore = null;
  159.                     String nomeSoggettoFruitore = null;
  160.                     if(apsCore.isRegistroServiziLocale()){
  161.                         org.openspcoop2.core.registry.Soggetto soggettoFruitore = soggettiCore.getSoggettoRegistro(Integer.parseInt(idsogg));
  162.                         tipoSoggettoFruitore = soggettoFruitore.getTipo();
  163.                         nomeSoggettoFruitore = soggettoFruitore.getNome();
  164.                     }else{
  165.                         org.openspcoop2.core.config.Soggetto soggettoFruitore = soggettiCore.getSoggetto(Integer.parseInt(idsogg));
  166.                         tipoSoggettoFruitore = soggettoFruitore.getTipo();
  167.                         nomeSoggettoFruitore = soggettoFruitore.getNome();
  168.                     }
  169.                     idSoggettoFruitore.setTipo(tipoSoggettoFruitore);
  170.                     idSoggettoFruitore.setNome(nomeSoggettoFruitore);
  171.                    
  172.                     List<MappingFruizionePortaDelegata> listaMappingFruizione = apsCore.serviziFruitoriMappingList(idFru, idSoggettoFruitore , idServizioFromAccordo, null);
  173.                     List<String> azioniOccupate = new ArrayList<>();
  174.                     int listaMappingErogazioneSize = listaMappingFruizione != null ? listaMappingFruizione.size() : 0;
  175.                     if(listaMappingErogazioneSize > 0) {
  176.                         for (int i = 0; i < listaMappingFruizione.size(); i++) {
  177.                             MappingFruizionePortaDelegata mappingErogazionePortaDelegata = listaMappingFruizione.get(i);
  178.                             // colleziono le azioni gia' configurate
  179.                             PortaDelegata portaDelegataTmp = porteDelegateCore.getPortaDelegata(mappingErogazionePortaDelegata.getIdPortaDelegata());
  180.                             if(portaDelegataTmp.getAzione() != null && portaDelegataTmp.getAzione().getAzioneDelegataList() != null)
  181.                                 azioniOccupate.addAll(portaDelegataTmp.getAzione().getAzioneDelegataList());
  182.                         }
  183.                     }
  184.                     Map<String,String> azioniAccordoDisponibili = porteDelegateCore.getAzioniConLabel(asps, apc, false, true, azioniOccupate);
  185.                    
  186.                     azioniDisponibiliList = new String[azioniAccordoDisponibili.size()];
  187.                     azioniDisponibiliLabelList = new String[azioniAccordoDisponibili.size()];
  188.                     int i = 0;
  189.                     for (String string : azioniAccordoDisponibili.keySet()) {
  190.                         azioniDisponibiliList[i] = string;
  191.                         azioniDisponibiliLabelList[i] = azioniAccordoDisponibili.get(string);
  192.                         i++;
  193.                     }
  194.                 }
  195.             }
  196.            
  197.             // setto il titolo
  198.             List<Parameter> lstParam = porteDelegateHelper.getTitoloPD(parentPD, idsogg, idAsps, idFruizione);
  199.            
  200.             String labelPerPorta = null;
  201.             if(parentPD!=null && (parentPD.intValue() == PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT_CONFIGURAZIONE)) {
  202.                 labelPerPorta = porteDelegateCore.getLabelRegolaMappingFruizionePortaDelegata(
  203.                         PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_DI,
  204.                         PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI,
  205.                         portaDelegata);
  206.             }
  207.             else {
  208.                 labelPerPorta = PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_DI+nomePorta;
  209.             }

  210.             lstParam.add(new Parameter(labelPerPorta, PorteDelegateCostanti.SERVLET_NAME_PORTE_DELEGATE_TRASFORMAZIONI_LIST, pId, pIdSoggetto, pIdAsps, pIdFruizione));
  211.            
  212.             lstParam.add(ServletUtils.getParameterAggiungi());

  213.             ServletUtils.setPageDataTitle(pd, lstParam);

  214.             // Se nomehid = null, devo visualizzare la pagina per l'inserimento
  215.             // dati
  216.             if (porteDelegateHelper.isEditModeInProgress()) {
  217.                 // preparo i campi
  218.                 List<DataElement> dati = new ArrayList<>();
  219.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  220.                
  221.                 dati = porteDelegateHelper.addTrasformazioneToDatiOpAdd(dati, portaDelegata, nome,
  222.                         stato, azioniAll, azioniDisponibiliList, azioniDisponibiliLabelList, azioni, pattern, contentType,
  223.                         null, null, null,
  224.                         apc.getServiceBinding(),true);
  225.                
  226.                 dati = porteDelegateHelper.addHiddenFieldsToDati(TipoOperazione.ADD, id, idsogg, null, idAsps,
  227.                         idFruizione, portaDelegata.getTipoSoggettoProprietario(), portaDelegata.getNomeSoggettoProprietario(), dati);
  228.                
  229.                 pd.setDati(dati);

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

  231.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_TRASFORMAZIONI, ForwardParams.ADD());
  232.             }
  233.            
  234.             // quando un parametro viene inviato come vuoto, sul db viene messo null, gestisco il caso
  235.             String azioniAsString = azioni != null ? StringUtils.join(Arrays.asList(azioni), ",") : "";
  236.             String patternDBCheck = StringUtils.isNotEmpty(pattern) ? pattern : null;
  237.             String contentTypeDBCheck = StringUtils.isNotEmpty(contentType) ? contentType : null;
  238.             String azioniDBCheck = StringUtils.isNotEmpty(azioniAsString) ? azioniAsString : null;
  239.             TrasformazioneRegola trasformazioneDBCheckCriteri = porteDelegateCore.getTrasformazione(Long.parseLong(id), azioniDBCheck, patternDBCheck, contentTypeDBCheck, null,
  240.                     null);
  241.             TrasformazioneRegola trasformazioneDBCheckNome = porteDelegateCore.getTrasformazione(Long.parseLong(id), nome);
  242.            
  243.             boolean isOk = porteDelegateHelper.trasformazioniCheckData(TipoOperazione.ADD, Long.parseLong(id), nome, trasformazioneDBCheckCriteri, trasformazioneDBCheckNome, null,
  244.                     serviceBinding);
  245.             if (!isOk) {

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

  248.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  249.                
  250.                 dati = porteDelegateHelper.addTrasformazioneToDatiOpAdd(dati, portaDelegata, nome,
  251.                         stato, azioniAll, azioniDisponibiliList, azioniDisponibiliLabelList, azioni, pattern, contentType,
  252.                         null, null, null,
  253.                         apc.getServiceBinding(),true);
  254.                
  255.                 dati = porteDelegateHelper.addHiddenFieldsToDati(TipoOperazione.ADD, id, idsogg, null, idAsps,
  256.                         idFruizione, portaDelegata.getTipoSoggettoProprietario(), portaDelegata.getNomeSoggettoProprietario(), dati);
  257.                
  258.                 pd.setDati(dati);

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

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

  314.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_TRASFORMAZIONI, ForwardParams.ADD());


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