AccordiServizioParteComuneResourcesRisposteAdd.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.driver.IDAccordoFactory;
  34. import org.openspcoop2.protocol.engine.utils.NamingUtils;
  35. import org.openspcoop2.utils.rest.api.ApiResponse;
  36. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  37. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  38. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  39. import org.openspcoop2.web.ctrlstat.servlet.apc.api.ApiCostanti;
  40. import org.openspcoop2.web.lib.mvc.Costanti;
  41. import org.openspcoop2.web.lib.mvc.DataElement;
  42. import org.openspcoop2.web.lib.mvc.ForwardParams;
  43. import org.openspcoop2.web.lib.mvc.GeneralData;
  44. import org.openspcoop2.web.lib.mvc.PageData;
  45. import org.openspcoop2.web.lib.mvc.Parameter;
  46. import org.openspcoop2.web.lib.mvc.ServletUtils;
  47. import org.openspcoop2.web.lib.mvc.TipoOperazione;

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

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

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

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

  62.         GeneralHelper generalHelper = new GeneralHelper(session);

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

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

  66.         // Parametri relativi al tipo operazione
  67.         TipoOperazione tipoOp = TipoOperazione.ADD;

  68.         try {
  69.             AccordiServizioParteComuneCore apcCore = new AccordiServizioParteComuneCore();

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

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

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

  96.             // Preparo il menu
  97.             apcHelper.makeMenu();

  98.             // Prendo il nome
  99.             AccordoServizioParteComune as = apcCore.getAccordoServizioFull(idAccordoLong);
  100.             String labelASTitle = apcHelper.getLabelIdAccordo(as);
  101.            
  102.             Resource risorsa = null;
  103.             for (int j = 0; j < as.sizeResourceList(); j++) {
  104.                 risorsa = as.getResource(j);
  105.                 if (nomeRisorsa.equals(risorsa.getNome())) {
  106.                     break;
  107.                 }
  108.             }
  109.            
  110.             IDAccordoFactory idAccordoFactory = IDAccordoFactory.getInstance();
  111.             String uriAS = idAccordoFactory.getUriFromAccordo(as);
  112.             Parameter pTipoAccordo = AccordiServizioParteComuneUtilities.getParametroAccordoServizio(tipoAccordo);
  113.             Parameter pIdAccordo = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ID, id);
  114.             Parameter pNomeAccordo = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_NOME, uriAS);
  115.             Parameter pIdRisorsa = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_ID, risorsa.getId()+"");
  116.             Parameter pNomeRisorsa = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_NOME, nomeRisorsa);
  117.            
  118.             Boolean isModalitaVistaApiCustom = ServletUtils.getBooleanAttributeFromSession(ApiCostanti.SESSION_ATTRIBUTE_VISTA_APC_API, session, request, false).getValue();
  119.             List<Parameter> listaParams = apcHelper.getTitoloApc(TipoOperazione.LIST, as, tipoAccordo, labelASTitle, null, false);
  120.            
  121.             String labelRisorse = (isModalitaVistaApiCustom!=null && isModalitaVistaApiCustom.booleanValue()) ? AccordiServizioParteComuneCostanti.LABEL_RISORSE : AccordiServizioParteComuneCostanti.LABEL_RISORSE + " di " + labelASTitle;
  122.            
  123.             listaParams.add(new Parameter(labelRisorse, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo));
  124.            
  125.             String labelRisorsa = NamingUtils.getLabelResource(risorsa);
  126.             listaParams.add(new Parameter(labelRisorsa, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_CHANGE, pIdAccordo, pNomeAccordo, pTipoAccordo,pIdRisorsa));
  127.            
  128.             String labelResponse = AccordiServizioParteComuneCostanti.LABEL_RISPOSTE ;
  129.            
  130.             listaParams.add(new Parameter(labelResponse, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_RESOURCES_RISPOSTE_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo,pNomeRisorsa));
  131.             listaParams.add(new Parameter(Costanti.PAGE_DATA_TITLE_LABEL_AGGIUNGI, null));

  132.             // Se idhid = null, devo visualizzare la pagina per l'inserimento
  133.             // dati
  134.             if(ServletUtils.isEditModeInProgress(editMode)){

  135.                 // setto la barra del titolo
  136.                 ServletUtils.setPageDataTitle(pd, listaParams);

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

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

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

  141.                 pd.setDati(dati);

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

  143.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_RISPOSTE, ForwardParams.ADD());
  144.             }
  145.            
  146.             // Controlli sui campi immessi
  147.             boolean isOk = apcHelper.accordiResourceResponseCheckData(tipoOp, id, risorsa.getId().intValue(), nomeRisorsa, statusS, descr);

  148.             if (!isOk) {

  149.                 // setto la barra del titolo
  150.                 ServletUtils.setPageDataTitle(pd, listaParams);

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

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

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

  155.                 pd.setDati(dati);

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

  157.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_RISPOSTE, ForwardParams.ADD());
  158.             }

  159.             // Inserisco la risorsa nel db

  160.             ResourceResponse newResponse = new ResourceResponse();
  161.             newResponse.setStatus(status);
  162.             newResponse.setDescrizione(descr);
  163.        
  164.             risorsa.addResponse(newResponse);
  165.            
  166.             // effettuo le operazioni
  167.             apcCore.performUpdateOperation(userLogin, apcHelper.smista(), as);
  168.            
  169.             // Preparo la lista
  170.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);

  171.             // Devo rileggere l'accordo dal db, perche' altrimenti
  172.             // manca l'id delle risorse
  173.             as = apcCore.getAccordoServizioFull(idAccordoLong);
  174.            
  175.             risorsa = null;
  176.             for (int j = 0; j < as.sizeResourceList(); j++) {
  177.                 risorsa = as.getResource(j);
  178.                 if (nomeRisorsa.equals(risorsa.getNome())) {
  179.                     break;
  180.                 }
  181.             }
  182.            
  183.             if(risorsa==null) {
  184.                 throw new Exception("Risorsa con nome '"+nomeRisorsa+"' non trovata nell'accordo con id '"+idAccordoLong+"'");
  185.             }
  186.            
  187.             List<ResourceResponse> lista = apcCore.accordiResourceResponseList(risorsa.getId(), ricerca);

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

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

  190.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_RISPOSTE, ForwardParams.ADD());

  191.         } catch (Exception e) {
  192.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  193.                     AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_RISPOSTE, ForwardParams.ADD());
  194.         }
  195.     }
  196. }