AccordiServizioParteComuneResourcesParametersAdd.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.Resource;
  33. import org.openspcoop2.core.registry.ResourceParameter;
  34. import org.openspcoop2.core.registry.ResourceRequest;
  35. import org.openspcoop2.core.registry.ResourceResponse;
  36. import org.openspcoop2.core.registry.constants.ParameterType;
  37. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  38. import org.openspcoop2.protocol.engine.utils.NamingUtils;
  39. import org.openspcoop2.utils.rest.api.ApiResponse;
  40. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  41. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  42. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  43. import org.openspcoop2.web.ctrlstat.servlet.apc.api.ApiCostanti;
  44. import org.openspcoop2.web.lib.mvc.Costanti;
  45. import org.openspcoop2.web.lib.mvc.DataElement;
  46. import org.openspcoop2.web.lib.mvc.ForwardParams;
  47. import org.openspcoop2.web.lib.mvc.GeneralData;
  48. import org.openspcoop2.web.lib.mvc.PageData;
  49. import org.openspcoop2.web.lib.mvc.Parameter;
  50. import org.openspcoop2.web.lib.mvc.ServletUtils;
  51. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  52. /**
  53.  * AccordiServizioParteComuneResourcesParametersAdd
  54.  *
  55.  * @author Andrea Poli (apoli@link.it)
  56.  * @author $Author$
  57.  * @version $Rev$, $Date$
  58.  *
  59.  */
  60. public final class AccordiServizioParteComuneResourcesParametersAdd extends Action {

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

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

  64.         // Inizializzo PageData
  65.         PageData pd = new PageData();

  66.         GeneralHelper generalHelper = new GeneralHelper(session);

  67.         // Inizializzo GeneralData
  68.         GeneralData gd = generalHelper.initGeneralData(request);

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

  70.         IDAccordoFactory idAccordoFactory = IDAccordoFactory.getInstance();

  71.         // Parametri relativi al tipo operazione
  72.         TipoOperazione tipoOp = TipoOperazione.ADD;


  73.         try {
  74.             AccordiServizioParteComuneCore apcCore = new AccordiServizioParteComuneCore();
  75.             AccordiServizioParteComuneHelper apcHelper = new AccordiServizioParteComuneHelper(request, pd, session);

  76.             String editMode = apcHelper.getParameter(Costanti.DATA_ELEMENT_EDIT_MODE_NAME);
  77.            
  78.             String id = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ID);
  79.             long idAccordoLong = Long.parseLong(id);
  80.             String nomeRisorsa = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_NOME);
  81.             if (nomeRisorsa == null) {
  82.                 nomeRisorsa = "";
  83.             }
  84.             String statusS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_RESPONSE_STATUS);
  85.             Integer status = null;
  86.             try {
  87.                 if(statusS!=null)
  88.                     status = Integer.parseInt(statusS);
  89.             } catch(Exception e) {
  90.                 // ignore
  91.             }
  92.             String isReq = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCE_REQUEST);
  93.             boolean isRequest = ServletUtils.isCheckBoxEnabled(isReq);
  94.            
  95.             String descr = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_PARAMETER_DESCRIZIONE);
  96.             if (descr == null) {
  97.                 descr = "";
  98.             }
  99.            
  100.             String tipoAccordo = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_TIPO_ACCORDO);
  101.             if("".equals(tipoAccordo))
  102.                 tipoAccordo = null;
  103.             String nome = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_PARAMETER_NOME);
  104.             String tipo = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_PARAMETER_TIPO);
  105.             String restrizioni = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_PARAMETER_RESTRIZIONI);
  106.             String tipoParametroS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_PARAMETER_TIPO_PARAMETRO);
  107.             ParameterType tipoParametro =  StringUtils.isNotEmpty(tipoParametroS) ? ParameterType.toEnumConstant(tipoParametroS) : null;
  108.             String requiredS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_PARAMETER_REQUIRED);
  109.             boolean required = ServletUtils.isCheckBoxEnabled(requiredS);
  110.            
  111.             // Preparo il menu
  112.             apcHelper.makeMenu();

  113.             // Prendo il nome
  114.             AccordoServizioParteComune as = apcCore.getAccordoServizioFull(idAccordoLong);
  115.             String uriAS = idAccordoFactory.getUriFromAccordo(as);
  116.             String labelASTitle = apcHelper.getLabelIdAccordo(as);

  117.             Resource risorsa = null;
  118.             for (int j = 0; j < as.sizeResourceList(); j++) {
  119.                 risorsa = as.getResource(j);
  120.                 if (nomeRisorsa.equals(risorsa.getNome())) {
  121.                     break;
  122.                 }
  123.             }
  124.            
  125.             if(risorsa==null) {
  126.                 throw new Exception("Risorsa con nome '"+nomeRisorsa+"' non trovata nell'accordo con id '"+idAccordoLong+"'");
  127.             }
  128.            
  129.             Long idResponse = null;
  130.             Long idResource = null;
  131.             ResourceRequest resourceRequest = null;
  132.             ResourceResponse resourceResponse = null;
  133.             if(isRequest) {
  134.                 resourceRequest = risorsa.getRequest();
  135.                 idResource = risorsa.getId();
  136.             } else {
  137.                 if(risorsa.getResponseList() != null) {
  138.                     for (int i = 0; i < risorsa.getResponseList().size(); i++) {
  139.                         resourceResponse = risorsa.getResponse(i);
  140.                         if (resourceResponse.getStatus() == status) {
  141.                             idResponse = resourceResponse.getId();
  142.                             break;
  143.                         }
  144.                     }
  145.                 }
  146.             }
  147.            
  148.             Parameter pTipoAccordo = AccordiServizioParteComuneUtilities.getParametroAccordoServizio(tipoAccordo);
  149.             Parameter pIdAccordo = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ID, id);
  150.             Parameter pNomeAccordo = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_NOME, uriAS);
  151.             Parameter pNomeRisorsa = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_NOME, risorsa.getNome());
  152.             Parameter pIsRequest = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCE_REQUEST, isRequest+"");
  153.             Parameter pIdRisorsa = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_ID, risorsa.getId()+"");
  154.             Parameter pResponseStatus = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_RESPONSE_STATUS, statusS);
  155.             Boolean isModalitaVistaApiCustom = ServletUtils.getBooleanAttributeFromSession(ApiCostanti.SESSION_ATTRIBUTE_VISTA_APC_API, session, request, false).getValue();
  156.             List<Parameter> listaParams = apcHelper.getTitoloApc(TipoOperazione.ADD, as, tipoAccordo, labelASTitle, null, false);
  157.            
  158.             String labelRisorse = (isModalitaVistaApiCustom!=null && isModalitaVistaApiCustom.booleanValue()) ? AccordiServizioParteComuneCostanti.LABEL_RISORSE : AccordiServizioParteComuneCostanti.LABEL_RISORSE + " di " + labelASTitle;
  159.            
  160.             listaParams.add(new Parameter(labelRisorse, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo));
  161.            
  162.             String labelRisorsa = NamingUtils.getLabelResource(risorsa);
  163.             listaParams.add(new Parameter(labelRisorsa, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_CHANGE, pIdAccordo, pNomeAccordo, pTipoAccordo,pIdRisorsa));
  164.            
  165.             if(!isRequest) {
  166.                 String labelResponse = AccordiServizioParteComuneCostanti.LABEL_RISPOSTE;
  167.                 listaParams.add(new Parameter(labelResponse, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_RISPOSTE_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo,pNomeRisorsa));
  168.                
  169.                 String labelRisposta = AccordiServizioParteComuneCostanti.LABEL_PARAMETRO_APC_RESOURCES_RESPONSE_STATUS +" "+
  170.                         (ApiResponse.isDefaultHttpReturnCode(status)? AccordiServizioParteComuneCostanti.LABEL_PARAMETRO_APC_RESOURCES_RESPONSE_STATUS_DEFAULT : statusS);
  171.                
  172.                 listaParams.add(new Parameter(labelRisposta, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_RISPOSTE_CHANGE, pIdAccordo, pTipoAccordo, pNomeAccordo, pNomeRisorsa,pResponseStatus));
  173.             }
  174.            
  175.             String labelParameters = AccordiServizioParteComuneCostanti.LABEL_PARAMETERS;
  176.            
  177.             listaParams.add(new Parameter(labelParameters, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_PARAMETERS_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo,pNomeRisorsa,pIsRequest, pResponseStatus));
  178.             listaParams.add(new Parameter(Costanti.PAGE_DATA_TITLE_LABEL_AGGIUNGI, null));
  179.            
  180.             // Se idhid = null, devo visualizzare la pagina per l'inserimento
  181.             // dati
  182.             if(ServletUtils.isEditModeInProgress(editMode)){

  183.                 // setto la barra del titolo
  184.                 ServletUtils.setPageDataTitle(pd, listaParams);
  185.                
  186.                 // init
  187.                 if(nome == null) {
  188.                     nome = "";
  189.                     descr = "";
  190.                     required = false;
  191.                     tipoParametro = ParameterType.QUERY;
  192.                     tipo = "";
  193.                     restrizioni = null;
  194.                 }

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

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

  198.                 dati = apcHelper.addAccordiResourceParameterToDati(tipoOp, dati, id, as.getStatoPackage(),tipoAccordo,
  199.                              nomeRisorsa, isRequest, statusS, null, nome, descr,  tipoParametro, tipo, restrizioni, required);

  200.                 pd.setDati(dati);

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

  202.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_PARAMETERS, ForwardParams.ADD());
  203.             }

  204.             // Controlli sui campi immessi
  205.             boolean isOk = apcHelper.accordiResourceParameterCheckData(tipoOp, id, nomeRisorsa, isRequest, statusS, nome, descr, tipoParametro,
  206.                     tipo, restrizioni, required, idResource,idResponse,null,null);

  207.             if (!isOk) {

  208.                 // setto la barra del titolo
  209.                 ServletUtils.setPageDataTitle(pd, listaParams);

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

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

  213.                 dati = apcHelper.addAccordiResourceParameterToDati(tipoOp, dati, id, as.getStatoPackage(),tipoAccordo,
  214.                          nomeRisorsa, isRequest, statusS, null, nome, descr,  tipoParametro, tipo, restrizioni, required);
  215.                
  216.                 pd.setDati(dati);

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

  218.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_PARAMETERS, ForwardParams.ADD());
  219.             }

  220.             // Inserisco il parametro nel db
  221.             ResourceParameter newParameter = new ResourceParameter();
  222.             newParameter.setParameterType(tipoParametro);
  223.             newParameter.setNome(nome);
  224.             newParameter.setTipo(tipo);
  225.             if(restrizioni!=null && !"".equals(restrizioni)) {
  226.                 newParameter.setRestrizioni(restrizioni);
  227.             }
  228.             newParameter.setDescrizione(descr);
  229.             newParameter.setRequired(required);
  230.            
  231.            
  232.             if(isRequest) {
  233.                 if(resourceRequest == null)
  234.                     resourceRequest = new ResourceRequest();
  235.                 resourceRequest.addParameter(newParameter);
  236.             } else {
  237.                 resourceResponse.addParameter(newParameter);
  238.             }
  239.            
  240.             // effettuo le operazioni
  241.             apcCore.performUpdateOperation(userLogin, apcHelper.smista(), as);
  242.            
  243.             // Preparo la lista
  244.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  245.            
  246.             // Devo rileggere l'accordo dal db, perche' altrimenti
  247.             // manca l'id delle risorse
  248.             as = apcCore.getAccordoServizioFull(idAccordoLong);
  249.            
  250.             risorsa = null;
  251.             for (int j = 0; j < as.sizeResourceList(); j++) {
  252.                 risorsa = as.getResource(j);
  253.                 if (nomeRisorsa.equals(risorsa.getNome())) {
  254.                     break;
  255.                 }
  256.             }
  257.            
  258.             if(risorsa==null) {
  259.                 throw new Exception("Risorsa con nome '"+nomeRisorsa+"' non trovata nell'accordo con id '"+idAccordoLong+"'");
  260.             }
  261.            
  262.             idResponse = null;
  263.             idResource = null;
  264.             resourceRequest = null;
  265.             resourceResponse = null;
  266.             if(isRequest) {
  267.                 resourceRequest = risorsa.getRequest();
  268.                 idResource = risorsa.getId();
  269.             } else {
  270.                 if(risorsa.getResponseList() != null) {
  271.                     for (int i = 0; i < risorsa.getResponseList().size(); i++) {
  272.                         resourceResponse = risorsa.getResponse(i);
  273.                         if (resourceResponse.getStatus() == status) {
  274.                             idResponse = resourceResponse.getId();
  275.                             break;
  276.                         }
  277.                     }
  278.                 }
  279.             }
  280.                        
  281.             List<ResourceParameter> lista = apcCore.accordiResourceParametersList(idResource, isRequest, idResponse, ricerca);

  282.             apcHelper.prepareAccordiResourcesParametersList(id, as, lista, ricerca, tipoAccordo, isRequest, risorsa, resourceRequest, resourceResponse);

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

  284.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_PARAMETERS, ForwardParams.ADD());

  285.         } catch (Exception e) {
  286.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  287.                     AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_PARAMETERS, ForwardParams.ADD());
  288.         }
  289.     }
  290. }