AccordiServizioParteComuneResourcesRisposteChange.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.struts.action.Action;
  27. import org.apache.struts.action.ActionForm;
  28. import org.apache.struts.action.ActionForward;
  29. import org.apache.struts.action.ActionMapping;
  30. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  31. import org.openspcoop2.core.registry.Resource;
  32. import org.openspcoop2.core.registry.ResourceResponse;
  33. import org.openspcoop2.core.registry.constants.StatiAccordo;
  34. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  35. import org.openspcoop2.protocol.engine.utils.NamingUtils;
  36. import org.openspcoop2.utils.rest.api.ApiResponse;
  37. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  38. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  39. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  40. import org.openspcoop2.web.ctrlstat.servlet.apc.api.ApiCostanti;
  41. import org.openspcoop2.web.lib.mvc.Costanti;
  42. import org.openspcoop2.web.lib.mvc.DataElement;
  43. import org.openspcoop2.web.lib.mvc.ForwardParams;
  44. import org.openspcoop2.web.lib.mvc.GeneralData;
  45. import org.openspcoop2.web.lib.mvc.PageData;
  46. import org.openspcoop2.web.lib.mvc.Parameter;
  47. import org.openspcoop2.web.lib.mvc.ServletUtils;
  48. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  49. /**
  50.  * AccordiServizioParteComuneResourcesRisposteChange
  51.  *
  52.  * @author Andrea Poli (apoli@link.it)
  53.  * @author $Author$
  54.  * @version $Rev$, $Date$
  55.  *
  56.  */
  57. public final class AccordiServizioParteComuneResourcesRisposteChange extends Action {

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

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

  61.         // Inizializzo PageData
  62.         PageData pd = new PageData();

  63.         GeneralHelper generalHelper = new GeneralHelper(session);

  64.         // Inizializzo GeneralData
  65.         GeneralData gd = generalHelper.initGeneralData(request);

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

  67.         // Parametri relativi al tipo operazione
  68.         TipoOperazione tipoOp = TipoOperazione.CHANGE;

  69.         try {
  70.             AccordiServizioParteComuneCore apcCore = new AccordiServizioParteComuneCore();
  71.             AccordiServizioParteComuneHelper apcHelper = new AccordiServizioParteComuneHelper(request, pd, session);

  72.             String editMode = apcHelper.getParameter(Costanti.DATA_ELEMENT_EDIT_MODE_NAME);
  73.             String id = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ID);
  74.             long idAccordoLong = Long.parseLong(id);
  75.             String tipoAccordo = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_TIPO_ACCORDO);
  76.             if("".equals(tipoAccordo))
  77.                 tipoAccordo = null;
  78.             String nomeAccordo = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_NOME);
  79.             String nomeRisorsa = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_NOME);
  80.             String descr = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_RESPONSE_DESCRIZIONE);
  81.             if (descr == null) {
  82.                 descr = "";
  83.             }
  84.            
  85.             String statusS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_RESPONSE_STATUS);
  86.             int status = -1;
  87.             if(statusS==null || "".equals(statusS)) {
  88.                 status = ApiResponse.getDefaultHttpReturnCode();
  89.             }
  90.             else {
  91.                 try {
  92.                     status = Integer.parseInt(statusS);
  93.                 } catch(Exception e) {
  94.                     // ignore
  95.                 }
  96.             }
  97.            
  98.             // Preparo il menu
  99.             apcHelper.makeMenu();

  100.             // Prendo il nome
  101.             AccordoServizioParteComune as = apcCore.getAccordoServizioFull(idAccordoLong);
  102.             String labelASTitle = apcHelper.getLabelIdAccordo(as);

  103.             ResourceResponse resourceResponseOLD = null;
  104.             Resource risorsa = null;
  105.             for (int i = 0; i < as.sizeResourceList(); i++) {
  106.                 Resource res = as.getResource(i);
  107.                 if (nomeRisorsa.equals(res.getNome())) {
  108.                     risorsa = res;
  109.                     break;
  110.                 }
  111.             }
  112.             if(risorsa==null) {
  113.                 throw new Exception("Risorsa '"+nomeRisorsa+"' non trovata");
  114.             }
  115.            
  116.             if(risorsa.getResponseList() != null) {
  117.                 for (int i = 0; i < risorsa.getResponseList().size(); i++) {
  118.                     ResourceResponse rr = risorsa.getResponse(i);
  119.                     if(rr.getStatus() == status) {
  120.                         resourceResponseOLD = rr;
  121.                         break;
  122.                     }
  123.                 }
  124.             }
  125.            
  126.             IDAccordoFactory idAccordoFactory = IDAccordoFactory.getInstance();
  127.             String uriAS = idAccordoFactory.getUriFromAccordo(as);
  128.             Parameter pTipoAccordo = AccordiServizioParteComuneUtilities.getParametroAccordoServizio(tipoAccordo);
  129.             Parameter pIdAccordo = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ID, id);
  130.             Parameter pNomeAccordo = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_NOME, uriAS);
  131.             Parameter pIdRisorsa = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_ID, risorsa.getId()+"");
  132.             Parameter pNomeRisorsa = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_NOME, nomeRisorsa);
  133.            
  134.             Boolean isModalitaVistaApiCustom = ServletUtils.getBooleanAttributeFromSession(ApiCostanti.SESSION_ATTRIBUTE_VISTA_APC_API, session, request, false).getValue();
  135.             List<Parameter> listaParams = apcHelper.getTitoloApc(TipoOperazione.LIST, as, tipoAccordo, labelASTitle, null, false);
  136.            
  137.             String labelRisorse = (isModalitaVistaApiCustom!=null && isModalitaVistaApiCustom.booleanValue()) ? AccordiServizioParteComuneCostanti.LABEL_RISORSE : AccordiServizioParteComuneCostanti.LABEL_RISORSE + " di " + labelASTitle;
  138.            
  139.             listaParams.add(new Parameter(labelRisorse, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo));
  140.            
  141.             String labelRisorsa = NamingUtils.getLabelResource(risorsa);
  142.             listaParams.add(new Parameter(labelRisorsa, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_CHANGE, pIdAccordo, pNomeAccordo, pTipoAccordo,pIdRisorsa));
  143.            
  144.             String labelResponse = AccordiServizioParteComuneCostanti.LABEL_RISPOSTE ;
  145.             listaParams.add(new Parameter(labelResponse, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_RISPOSTE_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo,pNomeRisorsa));
  146.            
  147.             String labelRisposta = AccordiServizioParteComuneCostanti.LABEL_PARAMETRO_APC_RESOURCES_RESPONSE_STATUS+" "+
  148.                     (ApiResponse.isDefaultHttpReturnCode(status)? AccordiServizioParteComuneCostanti.LABEL_PARAMETRO_APC_RESOURCES_RESPONSE_STATUS_DEFAULT : statusS);
  149.             listaParams.add(new Parameter(labelRisposta, null));

  150.             // Se idhid = null, devo visualizzare la pagina per la
  151.             // modifica dati
  152.             if(ServletUtils.isEditModeInProgress(editMode)){

  153.                 // setto la barra del titolo
  154.                 ServletUtils.setPageDataTitle(pd, listaParams);

  155.                 // Prendo i dati dell'accordo
  156.                 if(resourceResponseOLD != null){
  157.                     descr = resourceResponseOLD.getDescrizione();
  158.                 }

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

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

  162.                 dati = apcHelper.addAccordiResourceResponseToDati(tipoOp, dati, id, nomeAccordo, tipoAccordo, as.getStatoPackage(), nomeRisorsa, descr, statusS);

  163.                 pd.setDati(dati);

  164.                 if(  apcHelper.isModalitaStandard() || (apcHelper.isShowGestioneWorkflowStatoDocumenti() && StatiAccordo.finale.toString().equals(as.getStatoPackage())) ){
  165.                     pd.disableEditMode();
  166.                 }

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

  168.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_RISPOSTE, ForwardParams.CHANGE());

  169.             }

  170.             // Controlli sui campi immessi
  171.             boolean isOk = apcHelper.accordiResourceResponseCheckData(tipoOp, id, risorsa.getId().intValue(), nomeRisorsa, statusS, descr);

  172.             if (!isOk) {

  173.                 // setto la barra del titolo
  174.                 ServletUtils.setPageDataTitle(pd, listaParams);

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

  177.                 dati = apcHelper.addAccordiResourceResponseToDati(tipoOp, dati, id, nomeAccordo, tipoAccordo, as.getStatoPackage(), nomeRisorsa, descr, statusS);

  178.                 pd.setDati(dati);

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

  180.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_RISPOSTE, ForwardParams.CHANGE());
  181.             }

  182.             // Modifico i dati del port-type nel db
  183.             Resource res = null;
  184.             for (int i = 0; i < as.sizeResourceList(); i++) {
  185.                 res = as.getResource(i);
  186.                 if (nomeRisorsa.equals(res.getNome())) {
  187.                     break;
  188.                 }
  189.             }
  190.             if(res==null) {
  191.                 throw new Exception("Risorsa '"+nomeRisorsa+"' non trovata");
  192.             }
  193.            
  194.             ResourceResponse resourceResponse = null;
  195.            
  196.             if(res.getResponseList() != null) {
  197.                 for (int i = 0; i < res.getResponseList().size(); i++) {
  198.                     ResourceResponse rr = res.getResponse(i);
  199.                     if(rr.getStatus() == status) {
  200.                         resourceResponse = rr;
  201.                         res.removeResponse(i);
  202.                         break;
  203.                     }
  204.                 }
  205.             }
  206.            
  207.             ResourceResponse newResponse = new ResourceResponse();
  208.             newResponse.setStatus(status);
  209.             newResponse.setDescrizione(descr);
  210.             if(resourceResponse!=null) {
  211.                 newResponse.setParameterList(resourceResponse.getParameterList());
  212.                 newResponse.setRepresentationList(resourceResponse.getRepresentationList());
  213.             }
  214.                        
  215.             res.addResponse(newResponse);

  216.             // effettuo le operazioni
  217.             apcCore.performUpdateOperation(userLogin, apcHelper.smista(), as);

  218.             // Preparo la lista
  219.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);

  220.             // Devo rileggere l'accordo dal db, perche' altrimenti
  221.             // manca l'id dei nuovi port-type
  222.             as = apcCore.getAccordoServizioFull(idAccordoLong);
  223.            
  224.             risorsa = null;
  225.             for (int j = 0; j < as.sizeResourceList(); j++) {
  226.                 risorsa = as.getResource(j);
  227.                 if (nomeRisorsa.equals(risorsa.getNome())) {
  228.                     break;
  229.                 }
  230.             }
  231.            
  232.             if(risorsa==null) {
  233.                 throw new Exception("Risorsa '"+nomeRisorsa+"' non trovata");
  234.             }
  235.            
  236.             List<ResourceResponse> lista = apcCore.accordiResourceResponseList(risorsa.getId(), ricerca);

  237.             apcHelper.prepareAccordiResourcesResponseList(ricerca, lista, id, as, tipoAccordo, risorsa);

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

  239.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_RISPOSTE, ForwardParams.CHANGE());

  240.         } catch (Exception e) {
  241.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  242.                     AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_RISPOSTE, ForwardParams.CHANGE());
  243.         }
  244.     }
  245. }