PorteApplicativeTrasformazioniRispostaAdd.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 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.PortaApplicativa;
  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.  * PorteApplicativeTrasformazioniRispostaAdd
  57.  *
  58.  * @author Giuliano Pintori (pintori@link.it)
  59.  * @author $Author$
  60.  * @version $Rev$, $Date$
  61.  *
  62.  */
  63. public class PorteApplicativeTrasformazioniRispostaAdd 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.             PorteApplicativeHelper porteApplicativeHelper = new PorteApplicativeHelper(request, pd, session);
  75.             // prelevo il flag che mi dice da quale pagina ho acceduto la sezione delle porte applicative
  76.             Integer parentPA = ServletUtils.getIntegerAttributeFromSession(PorteApplicativeCostanti.ATTRIBUTO_PORTE_APPLICATIVE_PARENT, session, request);
  77.             if(parentPA == null) parentPA = PorteApplicativeCostanti.ATTRIBUTO_PORTE_APPLICATIVE_PARENT_NONE;
  78.            
  79.             String idPorta = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID);
  80.             int idInt = Integer.parseInt(idPorta);
  81.             String idsogg = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO);
  82.             String idAsps = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS);
  83.             if(idAsps == null)
  84.                 idAsps = "";
  85.             String idTrasformazioneS = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_TRASFORMAZIONE);
  86.             long idTrasformazione = Long.parseLong(idTrasformazioneS);
  87.            
  88.             String nomeRisposta = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTA_NOME);
  89.            
  90.             String returnCode = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTA_APPLICABILITA_STATUS);
  91.             if(returnCode == null)
  92.                 returnCode = CostantiControlStation.VALUE_PARAMETRO_CONFIGURAZIONE_RETURN_CODE_QUALSIASI;
  93.            
  94.             String statusMin = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTA_APPLICABILITA_STATUS_MIN);
  95.             if(statusMin == null)
  96.                 statusMin = "";
  97.             String statusMax = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTA_APPLICABILITA_STATUS_MAX);
  98.             if(statusMax == null)
  99.                 statusMax = "";
  100.            
  101.             String pattern = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTA_APPLICABILITA_PATTERN);
  102.             String contentType = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTA_APPLICABILITA_CT);
  103.            
  104.            

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

  112.             // Prendo nome della porta applicativa
  113.             PortaApplicativa pa = porteApplicativeCore.getPortaApplicativa(idInt);
  114.             String nomePorta = pa.getNome();
  115.             String protocollo = soggettiCore.getProtocolloAssociatoTipoSoggetto(pa.getTipoSoggettoProprietario());
  116.            
  117.             Trasformazioni trasformazioni = pa.getTrasformazioni();
  118.             TrasformazioneRegola regola = null;
  119.             for (int j = 0; j < trasformazioni.sizeRegolaList(); j++) {
  120.                 TrasformazioneRegola regolaTmp = trasformazioni.getRegola(j);
  121.                 if (regolaTmp.getId().longValue() == idTrasformazione) {
  122.                     regola = regolaTmp;
  123.                     break;
  124.                 }
  125.             }
  126.             if(regola==null) {
  127.                 throw new Exception("TrasformazioneRegola con id '"+idTrasformazione+"' non trovata");
  128.             }
  129.            
  130.             AccordoServizioParteSpecifica asps = apsCore.getAccordoServizioParteSpecifica(Integer.parseInt(idAsps));
  131.             AccordoServizioParteComuneSintetico apc = apcCore.getAccordoServizioSintetico(asps.getIdAccordo());
  132.             ServiceBinding serviceBinding = apcCore.toMessageServiceBinding(apc.getServiceBinding());
  133.            
  134.             String nomeTrasformazione = regola.getNome();
  135.             Parameter pIdTrasformazione = new Parameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_TRASFORMAZIONE, idTrasformazione+"");
  136.            
  137.             String postBackElementName = porteApplicativeHelper.getPostBackElementName();
  138.            
  139.             // se ho modificato il soggetto ricalcolo il servizio e il service binding
  140.             if (postBackElementName != null &&
  141.                 postBackElementName.equals(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTA_APPLICABILITA_STATUS)) {
  142.                 statusMin = "";
  143.                 statusMax = "";
  144.             }

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

  146.             String labelPerPorta = null;
  147.             if(parentPA!=null && (parentPA.intValue() == PorteApplicativeCostanti.ATTRIBUTO_PORTE_APPLICATIVE_PARENT_CONFIGURAZIONE)) {
  148.                 labelPerPorta = porteApplicativeCore.getLabelRegolaMappingErogazionePortaApplicativa(
  149.                         PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_DI,
  150.                         PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI,
  151.                         pa);
  152.             }
  153.             else {
  154.                 labelPerPorta = PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_DI+nomePorta;
  155.             }

  156.             lstParam.add(new Parameter(labelPerPorta,
  157.                     PorteApplicativeCostanti.SERVLET_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_LIST,
  158.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID, idPorta),
  159.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO, idsogg),
  160.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS, idAsps)
  161.                     ));
  162.            
  163.             lstParam.add(new Parameter(nomeTrasformazione, PorteApplicativeCostanti.SERVLET_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_CHANGE,
  164.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID, idPorta),
  165.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO, idsogg),
  166.                     new Parameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS, idAsps), pIdTrasformazione));
  167.            
  168.             String labelPag = PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTE;
  169.            
  170.             List<Parameter> parametriInvocazioneServletTrasformazioniRisposta = new ArrayList<>();
  171.             parametriInvocazioneServletTrasformazioniRisposta.add(new Parameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID, idPorta));
  172.             parametriInvocazioneServletTrasformazioniRisposta.add(new Parameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO, idsogg));
  173.             parametriInvocazioneServletTrasformazioniRisposta.add(new Parameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS, idAsps));
  174.             parametriInvocazioneServletTrasformazioniRisposta.add(pIdTrasformazione);
  175.            
  176.             lstParam.add(new Parameter(labelPag,PorteApplicativeCostanti.SERVLET_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTA_LIST,parametriInvocazioneServletTrasformazioniRisposta));
  177.            
  178.             lstParam.add(ServletUtils.getParameterAggiungi());

  179.             ServletUtils.setPageDataTitle(pd, lstParam);

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

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

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

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

  227.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  228.                
  229.                 dati = porteApplicativeHelper.addTrasformazioneRispostaToDatiOpAdd(protocollo, dati, idTrasformazioneS,
  230.                         apc.getServiceBinding(),
  231.                         nomeRisposta, returnCode, statusMin, statusMax, pattern, contentType);
  232.                
  233.                 dati = porteApplicativeHelper.addHiddenFieldsToDati(TipoOperazione.ADD, idPorta, idsogg, idPorta,idAsps,  dati);
  234.                
  235.                 pd.setDati(dati);

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

  237.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTA, ForwardParams.ADD());
  238.             }
  239.            
  240.             // salvataggio nuova regola
  241.             for (int j = 0; j < trasformazioni.sizeRegolaList(); j++) {
  242.                 TrasformazioneRegola regolaTmp = trasformazioni.getRegola(j);
  243.                 if (regolaTmp.getId().longValue() == idTrasformazione) {
  244.                     regola = regolaTmp;
  245.                     break;
  246.                 }
  247.             }
  248.            
  249.             TrasformazioneRegolaRisposta risposta = new TrasformazioneRegolaRisposta();

  250.             // calcolo prossima posizione
  251.             int posizione = 1;
  252.             for (TrasformazioneRegolaRisposta check : regola.getRispostaList()) {
  253.                 if(check.getPosizione()>=posizione) {
  254.                     posizione = check.getPosizione()+1;
  255.                 }
  256.             }
  257.            
  258.             risposta.setNome(nomeRisposta);
  259.             risposta.setPosizione(posizione);
  260.            
  261.             TrasformazioneRegolaApplicabilitaRisposta applicabilita = new TrasformazioneRegolaApplicabilitaRisposta();
  262.            
  263.             if(returnCode.equals(CostantiControlStation.VALUE_PARAMETRO_CONFIGURAZIONE_RETURN_CODE_QUALSIASI)) {
  264.                 applicabilita.setReturnCodeMin(null);
  265.                 applicabilita.setReturnCodeMax(null);
  266.             } else if(returnCode.equals(CostantiControlStation.VALUE_PARAMETRO_CONFIGURAZIONE_RETURN_CODE_ESATTO)) {
  267.                 applicabilita.setReturnCodeMin(StringUtils.isNotEmpty(statusMin) ? Integer.parseInt(statusMin) : null);
  268.                 applicabilita.setReturnCodeMax(StringUtils.isNotEmpty(statusMin) ? Integer.parseInt(statusMin) : null);
  269.             } else { // intervallo
  270.                 applicabilita.setReturnCodeMin(StringUtils.isNotEmpty(statusMin) ? Integer.parseInt(statusMin) : null);
  271.                 applicabilita.setReturnCodeMax(StringUtils.isNotEmpty(statusMax) ? Integer.parseInt(statusMax) : null);
  272.             }
  273.            
  274.             applicabilita.setPattern(pattern);
  275.             if(contentType != null) {
  276.                 applicabilita.getContentTypeList().addAll(Arrays.asList(contentType.split(",")));
  277.             }
  278.            
  279.            
  280.             risposta.setApplicabilita(applicabilita);
  281.             regola.addRisposta(risposta );
  282.             porteApplicativeCore.performUpdateOperation(userLogin, porteApplicativeHelper.smista(), pa);
  283.            
  284.             // ricaricare id trasformazione
  285.             pa = porteApplicativeCore.getPortaApplicativa(Long.parseLong(idPorta));

  286.             TrasformazioneRegola trasformazioneAggiornata = porteApplicativeCore.getTrasformazione(pa.getId(), regola.getNome());
  287.            
  288.             // Preparo la lista
  289.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  290.            
  291.             int idLista = Liste.PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTE;
  292.            
  293.             ricerca = porteApplicativeHelper.checkSearchParameters(idLista, ricerca);

  294.             List<TrasformazioneRegolaRisposta> lista = porteApplicativeCore.porteAppTrasformazioniRispostaList(Long.parseLong(idPorta), trasformazioneAggiornata.getId(), ricerca);
  295.            
  296.             porteApplicativeHelper.preparePorteAppTrasformazioniRispostaList(nomePorta, trasformazioneAggiornata.getId(), ricerca, lista);
  297.                        
  298.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  299.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTA, ForwardParams.ADD());


  300.         } catch (Exception e) {
  301.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_RISPOSTA, ForwardParams.ADD());
  302.         }
  303.     }
  304. }