AccordiServizioParteComuneResourcesRepresentationAdd.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.apc;

  21. import java.util.ArrayList;
  22. import java.util.List;

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

  26. import org.apache.commons.lang.StringUtils;
  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.registry.AccordoServizioParteComune;
  32. import org.openspcoop2.core.registry.IdSoggetto;
  33. import org.openspcoop2.core.registry.Resource;
  34. import org.openspcoop2.core.registry.ResourceRepresentation;
  35. import org.openspcoop2.core.registry.ResourceRepresentationJson;
  36. import org.openspcoop2.core.registry.ResourceRepresentationXml;
  37. import org.openspcoop2.core.registry.ResourceRequest;
  38. import org.openspcoop2.core.registry.ResourceResponse;
  39. import org.openspcoop2.core.registry.constants.RepresentationType;
  40. import org.openspcoop2.core.registry.constants.RepresentationXmlType;
  41. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  42. import org.openspcoop2.message.constants.MessageType;
  43. import org.openspcoop2.message.constants.ServiceBinding;
  44. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  45. import org.openspcoop2.protocol.engine.utils.NamingUtils;
  46. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  47. import org.openspcoop2.utils.rest.api.ApiResponse;
  48. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  49. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  50. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  51. import org.openspcoop2.web.ctrlstat.servlet.apc.api.ApiCostanti;
  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.  * AccordiServizioParteComuneResourcesRepresentationAdd
  63.  *
  64.  * @author Andrea Poli (apoli@link.it)
  65.  * @author $Author$
  66.  * @version $Rev$, $Date$
  67.  *
  68.  */
  69. public final class AccordiServizioParteComuneResourcesRepresentationAdd extends Action {

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

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

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

  75.         GeneralHelper generalHelper = new GeneralHelper(session);

  76.         // Inizializzo GeneralData
  77.         GeneralData gd = generalHelper.initGeneralData(request);

  78.         String userLogin = ServletUtils.getUserLoginFromSession(session);

  79.         IDAccordoFactory idAccordoFactory = IDAccordoFactory.getInstance();

  80.         // Parametri relativi al tipo operazione
  81.         TipoOperazione tipoOp = TipoOperazione.ADD;

  82.         try {
  83.             AccordiServizioParteComuneCore apcCore = new AccordiServizioParteComuneCore();
  84.             SoggettiCore soggettiCore = new SoggettiCore(apcCore);

  85.             AccordiServizioParteComuneHelper apcHelper = new AccordiServizioParteComuneHelper(request, pd, session);

  86.             String editMode = apcHelper.getParameter(Costanti.DATA_ELEMENT_EDIT_MODE_NAME);

  87.             String id = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ID);
  88.             long idAccordoLong = Long.parseLong(id);
  89.             String nomeRisorsa = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_NOME);
  90.             if (nomeRisorsa == null) {
  91.                 nomeRisorsa = "";
  92.             }
  93.             String statusS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_RESPONSE_STATUS);
  94.             Integer status = null;
  95.             try {
  96.                 if(statusS!=null)
  97.                     status = Integer.parseInt(statusS);
  98.             } catch(Exception e) {
  99.                 // ignore
  100.             }
  101.             String isReq = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCE_REQUEST);
  102.             boolean isRequest = ServletUtils.isCheckBoxEnabled(isReq);
  103.            
  104.             String descr = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_DESCRIZIONE);
  105.             if (descr == null) {
  106.                 descr = "";
  107.             }

  108.             String messageProcessorS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_MESSAGE_TYPE);
  109.             MessageType messageType = (StringUtils.isNotEmpty(messageProcessorS) && !messageProcessorS.equals(AccordiServizioParteComuneCostanti.DEFAULT_VALUE_PARAMETRO_APC_MESSAGE_TYPE_DEFAULT)) ? MessageType.valueOf(messageProcessorS) : null;
  110.             String mediaType = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_MEDIA_TYPE);
  111.             String tipoS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_TIPO);
  112.             RepresentationType tipo = (StringUtils.isNotEmpty(tipoS) && !messageProcessorS.equals(AccordiServizioParteComuneCostanti.DEFAULT_VALUE_PARAMETRO_APC_RESOURCES_REPRESENTATION_TIPO_NON_DEFINITO)) ? RepresentationType.toEnumConstant(tipoS) : null;
  113.             String nome = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_NOME);
  114.             String tipoJson = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_JSON_TYPE);
  115.             String namespaceXml = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_XML_NAMESPACE);
  116.             String nomeXml = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_XML_NAME);
  117.             String xmlTypeS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_REPRESENTATION_XML_TIPO);
  118.             RepresentationXmlType xmlType =  StringUtils.isNotEmpty(xmlTypeS) ? RepresentationXmlType.toEnumConstant(xmlTypeS) : null;
  119.             String tipoAccordo = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_TIPO_ACCORDO);
  120.             if("".equals(tipoAccordo))
  121.                 tipoAccordo = null;

  122.             // Preparo il menu
  123.             apcHelper.makeMenu();

  124.             // Prendo il nome
  125.             AccordoServizioParteComune as = apcCore.getAccordoServizioFull(idAccordoLong);
  126.             String uriAS = idAccordoFactory.getUriFromAccordo(as);
  127.             String labelASTitle = apcHelper.getLabelIdAccordo(as);
  128.             String protocollo = null;
  129.             //calcolo del protocollo implementato dall'accordo
  130.             IdSoggetto soggettoReferente = as.getSoggettoReferente();
  131.             String tipoSoggettoReferente = soggettoReferente.getTipo();
  132.             protocollo = soggettiCore.getProtocolloAssociatoTipoSoggetto(tipoSoggettoReferente);
  133.            
  134.             IProtocolFactory<?> protocolFactory = ProtocolFactoryManager.getInstance().getProtocolFactoryByName(protocollo);
  135.            
  136.             ServiceBinding serviceBinding = null;
  137.             //calcolo del serviceBinding dall'accordo
  138.             serviceBinding = apcCore.toMessageServiceBinding(as.getServiceBinding());

  139.             Resource risorsa = null;
  140.             for (int j = 0; j < as.sizeResourceList(); j++) {
  141.                 risorsa = as.getResource(j);
  142.                 if (nomeRisorsa.equals(risorsa.getNome())) {
  143.                     break;
  144.                 }
  145.             }
  146.            
  147.             if(risorsa==null) {
  148.                 throw new Exception("Risorsa con nome '"+nomeRisorsa+"' non trovata nell'accordo con id '"+idAccordoLong+"'");
  149.             }
  150.            
  151.             Long idResponse = null;
  152.             Long idResource = null;
  153.             ResourceRequest resourceRequest = null;
  154.             ResourceResponse resourceResponse = null;
  155.             if(isRequest) {
  156.                 resourceRequest = risorsa.getRequest();
  157.                 idResource = risorsa.getId();
  158.             } else {
  159.                 if(risorsa.getResponseList() != null) {
  160.                     for (int i = 0; i < risorsa.getResponseList().size(); i++) {
  161.                         resourceResponse = risorsa.getResponse(i);
  162.                         if (resourceResponse.getStatus() == status) {
  163.                             idResponse = resourceResponse.getId();
  164.                             break;
  165.                         }
  166.                     }
  167.                 }
  168.             }
  169.            
  170.             Parameter pTipoAccordo = AccordiServizioParteComuneUtilities.getParametroAccordoServizio(tipoAccordo);
  171.             Parameter pIdAccordo = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ID, id);
  172.             Parameter pNomeAccordo = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_NOME, uriAS);
  173.             Parameter pNomeRisorsa = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_NOME, risorsa.getNome());
  174.             Parameter pIsRequest = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCE_REQUEST, isRequest+"");
  175.             Parameter pIdRisorsa = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_ID, risorsa.getId()+"");
  176.             Parameter pResponseStatus = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_RESPONSE_STATUS, statusS);
  177.             Boolean isModalitaVistaApiCustom = ServletUtils.getBooleanAttributeFromSession(ApiCostanti.SESSION_ATTRIBUTE_VISTA_APC_API, session, request, false).getValue();
  178.             List<Parameter> listaParams = apcHelper.getTitoloApc(TipoOperazione.ADD, as, tipoAccordo, labelASTitle, null, false);
  179.            
  180.             String labelRisorse = (isModalitaVistaApiCustom!=null && isModalitaVistaApiCustom.booleanValue()) ? AccordiServizioParteComuneCostanti.LABEL_RISORSE : AccordiServizioParteComuneCostanti.LABEL_RISORSE + " di " + labelASTitle;
  181.            
  182.             listaParams.add(new Parameter(labelRisorse, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo));
  183.            
  184.             String labelRisorsa = NamingUtils.getLabelResource(risorsa);
  185.             listaParams.add(new Parameter(labelRisorsa, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_CHANGE, pIdAccordo, pNomeAccordo, pTipoAccordo,pIdRisorsa));
  186.            
  187.             if(!isRequest) {
  188.                 String labelResponse = AccordiServizioParteComuneCostanti.LABEL_RISPOSTE;
  189.                 listaParams.add(new Parameter(labelResponse, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_RISPOSTE_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo,pNomeRisorsa));
  190.                
  191.                 String labelRisposta = AccordiServizioParteComuneCostanti.LABEL_PARAMETRO_APC_RESOURCES_RESPONSE_STATUS +" "+
  192.                         (ApiResponse.isDefaultHttpReturnCode(status)? AccordiServizioParteComuneCostanti.LABEL_PARAMETRO_APC_RESOURCES_RESPONSE_STATUS_DEFAULT : statusS);
  193.                
  194.                 listaParams.add(new Parameter(labelRisposta, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_RISPOSTE_CHANGE, pIdAccordo, pTipoAccordo, pNomeAccordo, pNomeRisorsa,pResponseStatus));
  195.             }
  196.            
  197.             String labelRepresentation = AccordiServizioParteComuneCostanti.LABEL_REPRESENTATION;
  198.            
  199.             listaParams.add(new Parameter(labelRepresentation, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_REPRESENTATIONS_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo,pNomeRisorsa,pIsRequest, pResponseStatus));
  200.             listaParams.add(new Parameter(Costanti.PAGE_DATA_TITLE_LABEL_AGGIUNGI, null));
  201.            
  202.             // Se idhid = null, devo visualizzare la pagina per l'inserimento
  203.             // dati
  204.             if(ServletUtils.isEditModeInProgress(editMode)){
  205.                 // setto la barra del titolo
  206.                 ServletUtils.setPageDataTitle(pd,listaParams);
  207.                
  208.                 if(mediaType == null) {
  209.                     nome = "";
  210.                     descr = "";
  211.                     tipoJson = "";
  212.                     namespaceXml = "";
  213.                     nomeXml = "";
  214.                     xmlType = RepresentationXmlType.ELEMENT;
  215.                     // default non definito
  216.                     tipo = null;
  217.                 }

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

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

  221.                 dati = apcHelper.addAccordiResourceRepresentationToDati(tipoOp, dati, id, as.getStatoPackage(),tipoAccordo,protocollo,
  222.                         protocolFactory,serviceBinding, nomeRisorsa, isRequest, statusS, null, mediaType, nome, descr, messageType, tipo, tipoJson, nomeXml, namespaceXml, xmlType);
  223.                
  224.                 pd.setDati(dati);

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

  226.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_REPRESENTATIONS, ForwardParams.ADD());
  227.             }
  228.            
  229.             // Controlli sui campi immessi
  230.             boolean isOk = apcHelper.accordiResourceRepresentationCheckData(tipoOp, id, nomeRisorsa, isRequest, statusS, mediaType, nome, descr, messageType, tipo, tipoJson, nomeXml, namespaceXml, xmlType, idResource,idResponse,null);

  231.             if (!isOk) {

  232.                 // setto la barra del titolo
  233.                 ServletUtils.setPageDataTitle(pd, listaParams);

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

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

  237.                 dati = apcHelper.addAccordiResourceRepresentationToDati(tipoOp, dati, id, as.getStatoPackage(),tipoAccordo,protocollo,
  238.                         protocolFactory,serviceBinding, nomeRisorsa, isRequest, statusS, null, mediaType, nome, descr,  messageType, tipo, tipoJson, nomeXml, namespaceXml, xmlType);
  239.                 pd.setDati(dati);

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

  241.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_REPRESENTATIONS, ForwardParams.ADD());
  242.             }

  243.             // Inserisco la risorsa nel db
  244.             ResourceRepresentation newRepresentation = new ResourceRepresentation();
  245.             newRepresentation.setMessageType(apcCore.fromMessageMessageType(messageType));
  246.             newRepresentation.setDescrizione(descr);

  247.             newRepresentation.setNome(nome);
  248.             newRepresentation.setMediaType(mediaType);
  249.             newRepresentation.setRepresentationType(tipo);
  250.             if(tipo !=null) {
  251.                 switch (tipo) {
  252.                 case JSON:
  253.                     ResourceRepresentationJson json = new ResourceRepresentationJson();
  254.                     json.setTipo(tipoJson);
  255.                     newRepresentation.setJson(json);
  256.                     break;
  257.                 case XML:
  258.                     ResourceRepresentationXml xml = new ResourceRepresentationXml();
  259.                     xml.setXmlType(xmlType);
  260.                     xml.setNome(nomeXml);
  261.                     xml.setNamespace(namespaceXml);
  262.                     newRepresentation.setXml(xml);
  263.                     break;
  264.                 default:
  265.                     break;
  266.                 }
  267.             }
  268.            
  269.             if(isRequest) {
  270.                 if(resourceRequest == null)
  271.                     resourceRequest = new ResourceRequest();
  272.                
  273.                 resourceRequest.addRepresentation(newRepresentation);
  274.             } else {
  275.                 resourceResponse.addRepresentation(newRepresentation);
  276.             }
  277.            
  278.             // effettuo le operazioni
  279.             apcCore.performUpdateOperation(userLogin, apcHelper.smista(), as);
  280.            
  281.             // Preparo la lista
  282.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  283.            
  284.             // Devo rileggere l'accordo dal db, perche' altrimenti
  285.             // manca l'id delle risorse
  286.             as = apcCore.getAccordoServizioFull(idAccordoLong);
  287.            
  288.             risorsa = null;
  289.             for (int j = 0; j < as.sizeResourceList(); j++) {
  290.                 risorsa = as.getResource(j);
  291.                 if (nomeRisorsa.equals(risorsa.getNome())) {
  292.                     break;
  293.                 }
  294.             }
  295.            
  296.             if(risorsa==null) {
  297.                 throw new Exception("Risorsa con nome '"+nomeRisorsa+"' non trovata nell'accordo con id '"+idAccordoLong+"'");
  298.             }
  299.            
  300.             idResponse = null;
  301.             idResource = null;
  302.             resourceRequest = null;
  303.             resourceResponse = null;
  304.             if(isRequest) {
  305.                 resourceRequest = risorsa.getRequest();
  306.                 idResource = risorsa.getId();
  307.             } else {
  308.                 if(risorsa.getResponseList() != null) {
  309.                     for (int i = 0; i < risorsa.getResponseList().size(); i++) {
  310.                         resourceResponse = risorsa.getResponse(i);
  311.                         if (resourceResponse.getStatus() == status) {
  312.                             idResponse = resourceResponse.getId();
  313.                             break;
  314.                         }
  315.                     }
  316.                 }
  317.             }

  318.             List<ResourceRepresentation> lista = apcCore.accordiResourceRepresentationsList(idResource, isRequest, idResponse, ricerca);

  319.             apcHelper.prepareAccordiResourcesRepresentationsList(id, as, lista, ricerca, tipoAccordo, isRequest, risorsa, resourceRequest, resourceResponse);

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

  321.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_REPRESENTATIONS, ForwardParams.ADD());

  322.         } catch (Exception e) {
  323.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  324.                     AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_REPRESENTATIONS, ForwardParams.ADD());
  325.         }
  326.     }
  327. }