PorteDelegateAzioneAdd.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.List;
  23. import java.util.Map;

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

  27. import org.apache.struts.action.Action;
  28. import org.apache.struts.action.ActionForm;
  29. import org.apache.struts.action.ActionForward;
  30. import org.apache.struts.action.ActionMapping;
  31. import org.openspcoop2.core.commons.Liste;
  32. import org.openspcoop2.core.config.PortaDelegata;
  33. import org.openspcoop2.core.config.PortaDelegataServizio;
  34. import org.openspcoop2.core.config.PortaDelegataSoggettoErogatore;
  35. import org.openspcoop2.core.id.IDPortaDelegata;
  36. import org.openspcoop2.core.id.IDServizio;
  37. import org.openspcoop2.core.id.IDSoggetto;
  38. import org.openspcoop2.core.mapping.MappingFruizionePortaDelegata;
  39. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  40. import org.openspcoop2.core.registry.ConfigurazioneServizioAzione;
  41. import org.openspcoop2.core.registry.Fruitore;
  42. import org.openspcoop2.core.registry.beans.AccordoServizioParteComuneSintetico;
  43. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  44. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  45. import org.openspcoop2.message.constants.ServiceBinding;
  46. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  47. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  48. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  49. import org.openspcoop2.web.ctrlstat.servlet.apc.AccordiServizioParteComuneCore;
  50. import org.openspcoop2.web.ctrlstat.servlet.aps.AccordiServizioParteSpecificaCore;
  51. import org.openspcoop2.web.ctrlstat.servlet.aps.AccordiServizioParteSpecificaCostanti;
  52. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  53. import org.openspcoop2.web.lib.mvc.Costanti;
  54. import org.openspcoop2.web.lib.mvc.DataElement;
  55. import org.openspcoop2.web.lib.mvc.ForwardParams;
  56. import org.openspcoop2.web.lib.mvc.GeneralData;
  57. import org.openspcoop2.web.lib.mvc.PageData;
  58. import org.openspcoop2.web.lib.mvc.Parameter;
  59. import org.openspcoop2.web.lib.mvc.ServletUtils;
  60. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  61. /**
  62.  * PorteDelegateAzioneAdd
  63.  *
  64.  * @author Andrea Poli (apoli@link.it)
  65.  * @author Giuliano Pintori (pintori@link.it)
  66.  * @author $Author$
  67.  * @version $Rev$, $Date$
  68.  *
  69.  */
  70. public final class PorteDelegateAzioneAdd 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.         // Inizializzo GeneralData
  78.         GeneralData gd = generalHelper.initGeneralData(request);

  79.         try {
  80.             PorteDelegateHelper porteDelegateHelper = new PorteDelegateHelper(request, pd, session);
  81.             // prelevo il flag che mi dice da quale pagina ho acceduto la sezione delle porte delegate
  82.             Integer parentPD = ServletUtils.getIntegerAttributeFromSession(PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT, session, request);
  83.             if(parentPD == null) parentPD = PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT_NONE;
  84.             String idPorta = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID);
  85.             int idInt = Integer.parseInt(idPorta);
  86.             String idsogg = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO);
  87.             int soggInt = Integer.parseInt(idsogg);
  88.             String idAsps = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_ASPS);
  89.             if(idAsps == null)
  90.                 idAsps = "";
  91.             String idFruizione = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE);
  92.             if(idFruizione == null)
  93.                 idFruizione = "";
  94.             int idFruizioneInt = Integer.parseInt(idFruizione);
  95.             PorteDelegateCore porteDelegateCore = new PorteDelegateCore();
  96.             SoggettiCore soggettiCore = new SoggettiCore(porteDelegateCore);
  97.             AccordiServizioParteSpecificaCore apsCore = new AccordiServizioParteSpecificaCore(porteDelegateCore);
  98.             AccordiServizioParteComuneCore apcCore = new AccordiServizioParteComuneCore(porteDelegateCore);
  99.             // multiselect
  100.             String[] azionis = porteDelegateHelper.getParameterValues(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_AZIONI);
  101.            
  102.             // Preparo il menu
  103.             porteDelegateHelper.makeMenu();

  104.             // Prendo nome della porta applicativa
  105.             PortaDelegata portaDelegata = porteDelegateCore.getPortaDelegata(idInt);
  106.             String nomePorta = portaDelegata.getNome();
  107.            
  108.             int idServizio = -1;
  109.             AccordoServizioParteSpecifica asps = null;
  110.             if(!idAsps.equals("")) {
  111.                 idServizio = Integer.parseInt(idAsps);
  112.                 asps = apsCore.getAccordoServizioParteSpecifica(idServizio);
  113.             } else {
  114.                 PortaDelegataServizio pds = portaDelegata.getServizio();
  115.                   idServizio = -1;
  116.                 String tipoServizio = null;
  117.                 String nomeServizio = null;
  118.                 Integer versioneServizio = null;
  119.                 if (pds != null) {
  120.                     idServizio = pds.getId().intValue();
  121.                     tipoServizio = pds.getTipo();
  122.                     nomeServizio = pds.getNome();
  123.                     versioneServizio = pds.getVersione();
  124.                 }
  125.                
  126.                 if (idServizio <= 0) {
  127.                 PortaDelegataSoggettoErogatore soggettoErogatore = portaDelegata.getSoggettoErogatore();
  128.                 idServizio = (int) apsCore.getIdAccordoServizioParteSpecifica(nomeServizio, tipoServizio, versioneServizio, soggettoErogatore.getNome(), soggettoErogatore.getTipo());
  129.                 }
  130.                 asps = apsCore.getAccordoServizioParteSpecifica(idServizio);
  131.             }
  132.            
  133.             AccordoServizioParteComuneSintetico aspc = apcCore.getAccordoServizioSintetico(IDAccordoFactory.getInstance().getIDAccordoFromUri(asps.getAccordoServizioParteComune()));
  134.             ServiceBinding serviceBinding = apcCore.toMessageServiceBinding(aspc.getServiceBinding());
  135.             IDSoggetto idSoggettoFruitore = soggettiCore.getIdSoggettoRegistro(soggInt);
  136.            
  137.             IDServizio idServizio2 = IDServizioFactory.getInstance().getIDServizioFromAccordo(asps);
  138.             List<MappingFruizionePortaDelegata> listaMappingFruizione = apsCore.serviziFruitoriMappingList((long)idFruizioneInt, idSoggettoFruitore, idServizio2, null);
  139.             List<String> azioniOccupate = new ArrayList<>();
  140.             int listaMappingFruizioneSize = listaMappingFruizione != null ? listaMappingFruizione.size() : 0;
  141.             if(listaMappingFruizioneSize > 0) {
  142.                 for (int i = 0; i < listaMappingFruizione.size(); i++) {
  143.                     MappingFruizionePortaDelegata mappingFruizionePortaDelegata = listaMappingFruizione.get(i);
  144.                     // colleziono le azioni gia' configurate
  145.                     PortaDelegata portaDelegataTmp = porteDelegateCore.getPortaDelegata(mappingFruizionePortaDelegata.getIdPortaDelegata());
  146.                     if(portaDelegataTmp.getAzione() != null && portaDelegataTmp.getAzione().getAzioneDelegataList() != null)
  147.                         azioniOccupate.addAll(portaDelegataTmp.getAzione().getAzioneDelegataList());
  148.                 }
  149.             }
  150.            
  151.             // Prendo le azioni  disponibili
  152.             boolean addTrattinoSelezioneNonEffettuata = false;
  153.             int sogliaAzioni = addTrattinoSelezioneNonEffettuata ? 1 : 0;
  154.             Map<String,String> azioni = porteDelegateCore.getAzioniConLabel(asps, aspc, addTrattinoSelezioneNonEffettuata, true, azioniOccupate);
  155.             String[] azioniDisponibiliList = null;
  156.             String[] azioniDisponibiliLabelList = null;
  157.             if(azioni!=null && azioni.size()>0) {
  158.                 azioniDisponibiliList = new String[azioni.size()];
  159.                 azioniDisponibiliLabelList = new String[azioni.size()];
  160.                 int i = 0;
  161.                 for (String string : azioni.keySet()) {
  162.                     azioniDisponibiliList[i] = string;
  163.                     azioniDisponibiliLabelList[i] = azioni.get(string);
  164.                     i++;
  165.                 }
  166.             }

  167.  
  168.             List<Parameter> lstParam = porteDelegateHelper.getTitoloPD(parentPD, idsogg, idAsps, idFruizione);
  169.            
  170.             String labelPerPorta = null;
  171.             if(parentPD!=null && (parentPD.intValue() == PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT_CONFIGURAZIONE)) {
  172.                 labelPerPorta = porteDelegateCore.getLabelRegolaMappingFruizionePortaDelegata(
  173.                         porteDelegateHelper.getLabelAzioniDi(serviceBinding),
  174.                         porteDelegateHelper.getLabelAzioni(serviceBinding),
  175.                         portaDelegata);
  176.             }
  177.             else {
  178.                 labelPerPorta = porteDelegateHelper.getLabelAzioniDi(serviceBinding)+nomePorta;
  179.             }
  180.            
  181.             lstParam.add(new Parameter(labelPerPorta,
  182.                     PorteDelegateCostanti.SERVLET_NAME_PORTE_DELEGATE_AZIONE_LIST,
  183.                     new Parameter( PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID, idPorta),
  184.                     new Parameter( PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO, idsogg),
  185.                     new Parameter( PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_ASPS, idAsps),
  186.                     new Parameter( PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE, idFruizione)            
  187.                     ));
  188.             lstParam.add(ServletUtils.getParameterAggiungi());
  189.            
  190.             // Se servizioApplicativohid = null, devo visualizzare la pagina per
  191.             // l'inserimento dati
  192.             if (porteDelegateHelper.isEditModeInProgress()) {
  193.                
  194.                 // setto la barra del titolo
  195.                 ServletUtils.setPageDataTitle(pd, lstParam);

  196.                 // preparo i campi
  197.                 List<DataElement> dati = new ArrayList<>();
  198.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  199.                
  200.                 if(azioniDisponibiliList==null || azioniDisponibiliList.length <= sogliaAzioni) {
  201.                     // si controlla 1 poiche' c'e' il trattino nelle azioni disponibili
  202.                     pd.setMessage(porteDelegateHelper.getLabelAllAzioniConfigurate(serviceBinding), Costanti.MESSAGE_TYPE_INFO);
  203.                     pd.disableEditMode();
  204.                 }
  205.                 else {
  206.                     dati = porteDelegateHelper.addPorteAzioneToDati(TipoOperazione.ADD,dati, "", azioniDisponibiliList,azioniDisponibiliLabelList, azionis, serviceBinding);
  207.                     dati = porteDelegateHelper.addHiddenFieldsToDati(TipoOperazione.ADD, idPorta, idsogg, idPorta, idAsps,
  208.                             idFruizione, portaDelegata.getTipoSoggettoProprietario(), portaDelegata.getNomeSoggettoProprietario(), dati);
  209.                 }
  210.                 pd.setDati(dati);

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

  212.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_AZIONE,
  213.                         ForwardParams.ADD());
  214.             }

  215.             // Controlli sui campi immessi
  216.             boolean isOk = porteDelegateHelper.porteDelAzioneCheckData(TipoOperazione.ADD,azioniOccupate,listaMappingFruizione);
  217.             if (!isOk) {
  218.                 // setto la barra del titolo
  219.                 ServletUtils.setPageDataTitle(pd, lstParam);

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

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

  223.                 dati = porteDelegateHelper.addPorteAzioneToDati(TipoOperazione.ADD,dati, "", azioniDisponibiliList,azioniDisponibiliLabelList, azionis, serviceBinding);
  224.                 dati = porteDelegateHelper.addHiddenFieldsToDati(TipoOperazione.ADD, idPorta, idsogg, idPorta, idAsps,
  225.                         idFruizione, portaDelegata.getTipoSoggettoProprietario(), portaDelegata.getNomeSoggettoProprietario(), dati);

  226.                 pd.setDati(dati);

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

  228.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_AZIONE,
  229.                         ForwardParams.ADD());
  230.             }

  231.             List<Object> listaOggettiDaModificare = new ArrayList<>();
  232.            
  233.             String azioneGiaEsistente = portaDelegata.getAzione().getAzioneDelegata(0); // prendo la prima
  234.            
  235.             // aggiungo azione nel db
  236.             for(String azione: azionis) {
  237.                 portaDelegata.getAzione().addAzioneDelegata(azione);
  238.             }
  239.             listaOggettiDaModificare.add(portaDelegata);

  240.             boolean updateASPS = false;
  241.             Fruitore fruitore = null;
  242.             for (Fruitore fruitoreCheck : asps.getFruitoreList()) {
  243.                 if(fruitoreCheck.getTipo().equals(portaDelegata.getTipoSoggettoProprietario()) && fruitoreCheck.getNome().equals(portaDelegata.getNomeSoggettoProprietario())) {
  244.                     fruitore = fruitoreCheck;
  245.                     break;
  246.                 }
  247.             }
  248.             if(fruitore==null) {
  249.                 throw new Exception("Fruitore con id '"+portaDelegata.getTipoSoggettoProprietario()+"/"+portaDelegata.getNomeSoggettoProprietario()+"' non trovato");
  250.             }
  251.             for (int j = 0; j < fruitore.sizeConfigurazioneAzioneList(); j++) {
  252.                 ConfigurazioneServizioAzione config = fruitore.getConfigurazioneAzione(j);
  253.                 if(config!=null &&
  254.                         config.getAzioneList().contains(azioneGiaEsistente)) {
  255.                     for(String azione: azionis) {
  256.                         config.addAzione(azione);
  257.                     }
  258.                     updateASPS = true;
  259.                     break;
  260.                 }
  261.             }
  262.             if(updateASPS) {
  263.                 listaOggettiDaModificare.add(asps);
  264.             }
  265.            
  266.             String userLogin = ServletUtils.getUserLoginFromSession(session);

  267.             porteDelegateCore.performUpdateOperation(userLogin, porteDelegateHelper.smista(), listaOggettiDaModificare.toArray());

  268.             // ricarico la pd
  269.            
  270.             IDPortaDelegata idPD = new IDPortaDelegata();
  271.             idPD.setNome(nomePorta);
  272.             portaDelegata = porteDelegateCore.getPortaDelegata(idPD );
  273.             List<String> listaAzioni = portaDelegata.getAzione().getAzioneDelegataList();
  274.            
  275.             // Preparo la lista
  276.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  277.             int idLista = Liste.PORTE_DELEGATE_AZIONI;
  278.             ricerca = porteDelegateHelper.checkSearchParameters(idLista, ricerca);
  279.            
  280.             List<Parameter> listaParametriSessione = new ArrayList<>();
  281.             listaParametriSessione.add(new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID, idPorta));
  282.             listaParametriSessione.add(new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO, idsogg));
  283.             listaParametriSessione.add(new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_ASPS, idAsps));
  284.             listaParametriSessione.add(new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE, idFruizione));
  285.             listaParametriSessione.add(new Parameter(AccordiServizioParteSpecificaCostanti.PARAMETRO_APS_TIPO_SOGGETTO_FRUITORE, portaDelegata.getTipoSoggettoProprietario()));
  286.             listaParametriSessione.add(new Parameter(AccordiServizioParteSpecificaCostanti.PARAMETRO_APS_NOME_SOGGETTO_FRUITORE, portaDelegata.getNomeSoggettoProprietario()));
  287.             lstParam =  porteDelegateHelper.getTitoloPD(parentPD, idsogg, idAsps, idFruizione);
  288.            
  289.             // imposto menu' contestuale
  290.             porteDelegateHelper.impostaComandiMenuContestualePD(idsogg, idAsps, idFruizione);
  291.            
  292.             porteDelegateHelper.preparePorteAzioneList(ricerca,
  293.                     listaAzioni, idPorta, parentPD, lstParam, nomePorta, PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_AZIONE,
  294.                     listaParametriSessione, labelPerPorta, serviceBinding, aspc);
  295.            

  296.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  297.             // Forward control to the specified success URI
  298.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_AZIONE,
  299.                     ForwardParams.ADD());
  300.         } catch (Exception e) {
  301.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  302.                     PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_AZIONE,
  303.                     ForwardParams.ADD());
  304.         }
  305.     }
  306. }