PorteDelegateTrasformazioniRispostaAdd.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 javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26. import javax.servlet.http.HttpSession;

  27. import org.apache.commons.lang.StringUtils;
  28. import org.apache.struts.action.Action;
  29. import org.apache.struts.action.ActionForm;
  30. import org.apache.struts.action.ActionForward;
  31. import org.apache.struts.action.ActionMapping;
  32. import org.openspcoop2.core.commons.Liste;
  33. import org.openspcoop2.core.config.PortaDelegata;
  34. import org.openspcoop2.core.config.TrasformazioneRegola;
  35. import org.openspcoop2.core.config.TrasformazioneRegolaApplicabilitaRisposta;
  36. import org.openspcoop2.core.config.TrasformazioneRegolaRisposta;
  37. import org.openspcoop2.core.config.Trasformazioni;
  38. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  39. import org.openspcoop2.core.registry.beans.AccordoServizioParteComuneSintetico;
  40. import org.openspcoop2.message.constants.ServiceBinding;
  41. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  42. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  43. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  44. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  45. import org.openspcoop2.web.ctrlstat.servlet.apc.AccordiServizioParteComuneCore;
  46. import org.openspcoop2.web.ctrlstat.servlet.aps.AccordiServizioParteSpecificaCore;
  47. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  48. import org.openspcoop2.web.lib.mvc.DataElement;
  49. import org.openspcoop2.web.lib.mvc.ForwardParams;
  50. import org.openspcoop2.web.lib.mvc.GeneralData;
  51. import org.openspcoop2.web.lib.mvc.PageData;
  52. import org.openspcoop2.web.lib.mvc.Parameter;
  53. import org.openspcoop2.web.lib.mvc.ServletUtils;
  54. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  55. /**
  56.  * PorteDelegateTrasformazioniRispostaAdd
  57.  *
  58.  * @author Giuliano Pintori (pintori@link.it)
  59.  * @author $Author$
  60.  * @version $Rev$, $Date$
  61.  *
  62.  */
  63. public class PorteDelegateTrasformazioniRispostaAdd extends Action {

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

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

  67.         // Inizializzo PageData
  68.         PageData pd = new PageData();

  69.         GeneralHelper generalHelper = new GeneralHelper(session);

  70.         String userLogin = ServletUtils.getUserLoginFromSession(session);  
  71.         // Inizializzo GeneralData
  72.         GeneralData gd = generalHelper.initGeneralData(request);

  73.         try {
  74.             PorteDelegateHelper porteDelegateHelper = new PorteDelegateHelper(request, pd, session);
  75.             // prelevo il flag che mi dice da quale pagina ho acceduto la sezione delle porte delegate
  76.             Integer parentPD = ServletUtils.getIntegerAttributeFromSession(PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT, session, request);
  77.             if(parentPD == null) parentPD = PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT_NONE;
  78.             String id = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID);
  79.             int idInt = Integer.parseInt(id);
  80.             String idsogg = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO);
  81.            
  82.             String idAsps = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_ASPS);
  83.             if(idAsps == null)
  84.                 idAsps = "";
  85.            
  86.             String idFruizione = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE);
  87.             if(idFruizione == null)
  88.                 idFruizione = "";
  89.            
  90.             String idTrasformazioneS = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_TRASFORMAZIONE);
  91.             long idTrasformazione = Long.parseLong(idTrasformazioneS);
  92.            
  93.             String nomeRisposta = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTA_NOME);
  94.                        
  95.             String returnCode = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTA_APPLICABILITA_STATUS);
  96.             if(returnCode == null)
  97.                 returnCode = CostantiControlStation.VALUE_PARAMETRO_CONFIGURAZIONE_RETURN_CODE_QUALSIASI;
  98.            
  99.             String statusMin = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTA_APPLICABILITA_STATUS_MIN);
  100.             if(statusMin == null)
  101.                 statusMin = "";
  102.             String statusMax = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTA_APPLICABILITA_STATUS_MAX);
  103.             if(statusMax == null)
  104.                 statusMax = "";
  105.            
  106.             String pattern = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTA_APPLICABILITA_PATTERN);
  107.             String contentType = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTA_APPLICABILITA_CT);
  108.            
  109.             Parameter pId = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID, id);
  110.             Parameter pIdSoggetto = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO, idsogg);
  111.             Parameter pIdAsps = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_ASPS, idAsps);
  112.             Parameter pIdFruizione = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE, idFruizione);

  113.             PorteDelegateCore porteDelegateCore = new PorteDelegateCore();
  114.             AccordiServizioParteSpecificaCore apsCore = new AccordiServizioParteSpecificaCore(porteDelegateCore);
  115.             AccordiServizioParteComuneCore apcCore = new AccordiServizioParteComuneCore(porteDelegateCore);
  116.             SoggettiCore soggettiCore = new SoggettiCore(porteDelegateCore);
  117.                        
  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.             String protocollo = soggettiCore.getProtocolloAssociatoTipoSoggetto(portaDelegata.getTipoSoggettoProprietario());
  124.            
  125.             Trasformazioni trasformazioni = portaDelegata.getTrasformazioni();
  126.             TrasformazioneRegola regola = null;
  127.             for (int j = 0; j < trasformazioni.sizeRegolaList(); j++) {
  128.                 TrasformazioneRegola regolaTmp = trasformazioni.getRegola(j);
  129.                 if (regolaTmp.getId().longValue() == idTrasformazione) {
  130.                     regola = regolaTmp;
  131.                     break;
  132.                 }
  133.             }
  134.             if(regola==null) {
  135.                 throw new Exception("TrasformazioneRegola con id '"+idTrasformazione+"' non trovata");
  136.             }
  137.            
  138.             AccordoServizioParteSpecifica asps = apsCore.getAccordoServizioParteSpecifica(Integer.parseInt(idAsps));
  139.             AccordoServizioParteComuneSintetico apc = apcCore.getAccordoServizioSintetico(asps.getIdAccordo());
  140.             ServiceBinding serviceBinding = apcCore.toMessageServiceBinding(apc.getServiceBinding());
  141.            
  142.             String nomeTrasformazione = regola.getNome();
  143.             Parameter pIdTrasformazione = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_TRASFORMAZIONE, idTrasformazione+"");
  144.            
  145.             String postBackElementName = porteDelegateHelper.getPostBackElementName();
  146.            
  147.             // se ho modificato il soggetto ricalcolo il servizio e il service binding
  148.             if (postBackElementName != null &&
  149.                 postBackElementName.equals(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTA_APPLICABILITA_STATUS)) {
  150.                 statusMin = "";
  151.                 statusMax = "";
  152.             }

  153.             List<Parameter> lstParam = porteDelegateHelper.getTitoloPD(parentPD, idsogg, idAsps, idFruizione);
  154.            
  155.             String labelPerPorta = null;
  156.             if(parentPD!=null && (parentPD.intValue() == PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT_CONFIGURAZIONE)) {
  157.                 labelPerPorta = porteDelegateCore.getLabelRegolaMappingFruizionePortaDelegata(
  158.                         PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_DI,
  159.                         PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI,
  160.                         portaDelegata);
  161.             }
  162.             else {
  163.                 labelPerPorta = PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_DI+nomePorta;
  164.             }

  165.             lstParam.add(new Parameter(labelPerPorta, PorteDelegateCostanti.SERVLET_NAME_PORTE_DELEGATE_TRASFORMAZIONI_LIST, pId, pIdSoggetto, pIdAsps, pIdFruizione));
  166.            
  167.             lstParam.add(new Parameter(nomeTrasformazione, PorteDelegateCostanti.SERVLET_NAME_PORTE_DELEGATE_TRASFORMAZIONI_CHANGE,
  168.                     pId, pIdSoggetto, pIdAsps, pIdFruizione, pIdTrasformazione));
  169.            
  170.             String labelPag = PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTE;
  171.            
  172.             List<Parameter> parametriInvocazioneServletTrasformazioniRisposta = new ArrayList<>();
  173.             parametriInvocazioneServletTrasformazioniRisposta.add(pId);
  174.             parametriInvocazioneServletTrasformazioniRisposta.add(pIdSoggetto);
  175.             parametriInvocazioneServletTrasformazioniRisposta.add(pIdAsps);
  176.             parametriInvocazioneServletTrasformazioniRisposta.add(pIdFruizione);
  177.             parametriInvocazioneServletTrasformazioniRisposta.add(pIdTrasformazione);
  178.            
  179.             lstParam.add(new Parameter(labelPag,PorteDelegateCostanti.SERVLET_NAME_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTA_LIST,parametriInvocazioneServletTrasformazioniRisposta));
  180.            
  181.             lstParam.add(ServletUtils.getParameterAggiungi());

  182.             ServletUtils.setPageDataTitle(pd, lstParam);

  183.             // Se nomehid = null, devo visualizzare la pagina per l'inserimento
  184.             // dati
  185.             if (porteDelegateHelper.isEditModeInProgress()) {
  186.                 // preparo i campi
  187.                 List<DataElement> dati = new ArrayList<>();
  188.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  189.                
  190.                 dati = porteDelegateHelper.addTrasformazioneRispostaToDatiOpAdd(protocollo, dati, idTrasformazioneS,
  191.                         apc.getServiceBinding(),
  192.                         nomeRisposta, returnCode, statusMin, statusMax, pattern, contentType);
  193.                
  194.                 dati = porteDelegateHelper.addHiddenFieldsToDati(TipoOperazione.ADD, id, idsogg, null, idAsps,
  195.                         idFruizione, portaDelegata.getTipoSoggettoProprietario(), portaDelegata.getNomeSoggettoProprietario(), dati);
  196.                
  197.                 pd.setDati(dati);

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

  199.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTA,   ForwardParams.ADD());
  200.             }
  201.            
  202.            
  203.             boolean isOk = porteDelegateHelper.trasformazioniRispostaCheckData(TipoOperazione.ADD, regola, null,
  204.                     serviceBinding);
  205.            
  206.             if(isOk) {
  207.                 // quando un parametro viene inviato come vuoto, sul db viene messo null, gestisco il caso
  208.                 Integer statusMinDBCheck = StringUtils.isNotEmpty(statusMin) ? Integer.parseInt(statusMin) : null;
  209.                 Integer statusMaxDBCheck = StringUtils.isNotEmpty(statusMax) ? Integer.parseInt(statusMax) : null;
  210.                 if(returnCode.equals(CostantiControlStation.VALUE_PARAMETRO_CONFIGURAZIONE_RETURN_CODE_ESATTO))
  211.                     statusMaxDBCheck = statusMinDBCheck;
  212.                 String patternDBCheck = StringUtils.isNotEmpty(pattern) ? pattern : null;
  213.                 String contentTypeDBCheck = StringUtils.isNotEmpty(contentType) ? contentType : null;
  214.                 boolean giaRegistrato = porteDelegateCore.existsTrasformazioneRisposta(Long.parseLong(id), idTrasformazione, statusMinDBCheck, statusMaxDBCheck, patternDBCheck, contentTypeDBCheck);
  215.                 if (giaRegistrato) {
  216.                     pd.setMessage(CostantiControlStation.MESSAGGIO_ERRORE_REGOLA_TRASFORMAZIONE_APPLICABILITA_DUPLICATA);
  217.                     isOk = false;
  218.                 }
  219.                 if (isOk) {
  220.                     giaRegistrato = porteDelegateCore.existsTrasformazioneRisposta(Long.parseLong(id), idTrasformazione, nomeRisposta);
  221.                     if (giaRegistrato) {
  222.                         pd.setMessage(CostantiControlStation.MESSAGGIO_ERRORE_REGOLA_TRASFORMAZIONE_APPLICABILITA_NOME);
  223.                         isOk = false;
  224.                     }
  225.                 }
  226.             }
  227.            
  228.             if (!isOk) {

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

  231.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  232.                
  233.                 dati = porteDelegateHelper.addTrasformazioneRispostaToDatiOpAdd(protocollo, dati, idTrasformazioneS,
  234.                         apc.getServiceBinding(),
  235.                         nomeRisposta, returnCode, statusMin, statusMax, pattern, contentType);
  236.                
  237.                 dati = porteDelegateHelper.addHiddenFieldsToDati(TipoOperazione.ADD, id, idsogg, null, idAsps,
  238.                         idFruizione, portaDelegata.getTipoSoggettoProprietario(), portaDelegata.getNomeSoggettoProprietario(), dati);
  239.                
  240.                 pd.setDati(dati);

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

  242.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTA,   ForwardParams.ADD());
  243.             }
  244.            
  245.             // salvataggio nuova regola
  246.             for (int j = 0; j < trasformazioni.sizeRegolaList(); j++) {
  247.                 TrasformazioneRegola regolaTmp = trasformazioni.getRegola(j);
  248.                 if (regolaTmp.getId().longValue() == idTrasformazione) {
  249.                     regola = regolaTmp;
  250.                     break;
  251.                 }
  252.             }
  253.            
  254.             TrasformazioneRegolaRisposta risposta = new TrasformazioneRegolaRisposta();
  255.            
  256.             // calcolo prossima posizione
  257.             int posizione = 1;
  258.             for (TrasformazioneRegolaRisposta check : regola.getRispostaList()) {
  259.                 if(check.getPosizione()>=posizione) {
  260.                     posizione = check.getPosizione()+1;
  261.                 }
  262.             }
  263.            
  264.             risposta.setNome(nomeRisposta);
  265.             risposta.setPosizione(posizione);
  266.            
  267.             TrasformazioneRegolaApplicabilitaRisposta applicabilita = new TrasformazioneRegolaApplicabilitaRisposta();
  268.            
  269.             if(returnCode.equals(CostantiControlStation.VALUE_PARAMETRO_CONFIGURAZIONE_RETURN_CODE_QUALSIASI)) {
  270.                 applicabilita.setReturnCodeMin(null);
  271.                 applicabilita.setReturnCodeMax(null);
  272.             } else if(returnCode.equals(CostantiControlStation.VALUE_PARAMETRO_CONFIGURAZIONE_RETURN_CODE_ESATTO)) {
  273.                 applicabilita.setReturnCodeMin(StringUtils.isNotEmpty(statusMin) ? Integer.parseInt(statusMin) : null);
  274.                 applicabilita.setReturnCodeMax(StringUtils.isNotEmpty(statusMin) ? Integer.parseInt(statusMin) : null);
  275.             } else { // intervallo
  276.                 applicabilita.setReturnCodeMin(StringUtils.isNotEmpty(statusMin) ? Integer.parseInt(statusMin) : null);
  277.                 applicabilita.setReturnCodeMax(StringUtils.isNotEmpty(statusMax) ? Integer.parseInt(statusMax) : null);
  278.             }
  279.            
  280.             applicabilita.setPattern(pattern);
  281.             if(contentType != null) {
  282.                 applicabilita.getContentTypeList().addAll(Arrays.asList(contentType.split(",")));
  283.             }
  284.            
  285.            
  286.             risposta.setApplicabilita(applicabilita);
  287.             regola.addRisposta(risposta );
  288.             porteDelegateCore.performUpdateOperation(userLogin, porteDelegateHelper.smista(), portaDelegata);
  289.            
  290.             // ricaricare id trasformazione
  291.             portaDelegata = porteDelegateCore.getPortaDelegata(Long.parseLong(id));

  292.             TrasformazioneRegola trasformazioneAggiornata = porteDelegateCore.getTrasformazione(portaDelegata.getId(), regola.getNome());
  293.            
  294.             // Preparo la lista
  295.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  296.            
  297.             int idLista = Liste.PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTE;
  298.            
  299.             ricerca = porteDelegateHelper.checkSearchParameters(idLista, ricerca);

  300.             List<TrasformazioneRegolaRisposta> lista = porteDelegateCore.porteDelegateTrasformazioniRispostaList(Long.parseLong(id), trasformazioneAggiornata.getId(), ricerca);
  301.            
  302.             porteDelegateHelper.preparePorteDelegateTrasformazioniRispostaList(nomePorta, trasformazioneAggiornata.getId(), ricerca, lista);
  303.                        
  304.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  305.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTA, ForwardParams.ADD());


  306.         } catch (Exception e) {
  307.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping, PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_TRASFORMAZIONI_RISPOSTA, ForwardParams.ADD());
  308.         }
  309.     }
  310. }