AccordiServizioParteComuneResourcesRepresentationDel.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.ResourceRepresentation;
  33. import org.openspcoop2.core.registry.ResourceRequest;
  34. import org.openspcoop2.core.registry.ResourceResponse;
  35. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  36. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  37. import org.openspcoop2.web.ctrlstat.core.Utilities;
  38. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  39. import org.openspcoop2.web.lib.mvc.Costanti;
  40. import org.openspcoop2.web.lib.mvc.ForwardParams;
  41. import org.openspcoop2.web.lib.mvc.GeneralData;
  42. import org.openspcoop2.web.lib.mvc.PageData;
  43. import org.openspcoop2.web.lib.mvc.ServletUtils;

  44. /**
  45.  * AccordiServizioParteComuneResourcesRepresentationDel
  46.  *
  47.  * @author Andrea Poli (apoli@link.it)
  48.  * @author $Author$
  49.  * @version $Rev$, $Date$
  50.  *
  51.  */
  52. public final class AccordiServizioParteComuneResourcesRepresentationDel extends Action {

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

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

  56.         // Inizializzo PageData
  57.         PageData pd = new PageData();

  58.         // Inizializzo GeneralData
  59.         GeneralHelper generalHelper = new GeneralHelper(session);

  60.         GeneralData gd = generalHelper.initGeneralData(request);

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

  62.         try {
  63.             AccordiServizioParteComuneCore apcCore = new AccordiServizioParteComuneCore();
  64.             AccordiServizioParteComuneHelper apcHelper = new AccordiServizioParteComuneHelper(request, pd, session);

  65.             String id = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ID);
  66.             long idAccordoLong = Long.valueOf(id);
  67.             String objToRemove = apcHelper.getParameter(Costanti.PARAMETER_NAME_OBJECTS_FOR_REMOVE);
  68.             String tipoAccordo = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_TIPO_ACCORDO);
  69.             if("".equals(tipoAccordo))
  70.                 tipoAccordo = null;
  71.            
  72.             String nomeRisorsa = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_NOME);
  73.             if (nomeRisorsa == null) {
  74.                 nomeRisorsa = "";
  75.             }
  76.             String statusS = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCES_RESPONSE_STATUS);
  77.             Integer status = null;
  78.             try {
  79.                 if(statusS!=null)
  80.                     status = Integer.parseInt(statusS);
  81.             } catch(Exception e) {
  82.                 // ignore
  83.             }
  84.             String isReq = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_RESOURCE_REQUEST);
  85.             boolean isRequest = ServletUtils.isCheckBoxEnabled(isReq);

  86.             // Preparo il menu
  87.             apcHelper.makeMenu();

  88.             ArrayList<String> resourcesToRemove = Utilities.parseIdsToRemove(objToRemove);
  89.             AccordoServizioParteComune as = apcCore.getAccordoServizioFull(idAccordoLong);

  90.             Resource risorsa = null;
  91.             for (int j = 0; j < as.sizeResourceList(); j++) {
  92.                 risorsa = as.getResource(j);
  93.                 if (nomeRisorsa.equals(risorsa.getNome())) {
  94.                     break;
  95.                 }
  96.             }
  97.            
  98.             if(risorsa==null) {
  99.                 throw new Exception("Risorsa con nome '"+nomeRisorsa+"' non trovata nell'accordo con id '"+idAccordoLong+"'");
  100.             }
  101.            
  102.             Long idResponse = null;
  103.             Long idResource = null;
  104.             ResourceRequest resourceRequest = null;
  105.             ResourceResponse resourceResponse = null;
  106.             List<ResourceRepresentation> representationList = null;
  107.             if(isRequest) {
  108.                 resourceRequest = risorsa.getRequest();
  109.                 idResource = risorsa.getId();
  110.                 if(resourceRequest != null)
  111.                     representationList = resourceRequest.getRepresentationList();
  112.             } else {
  113.                 if(risorsa.getResponseList() != null) {
  114.                     for (int i = 0; i < risorsa.getResponseList().size(); i++) {
  115.                         resourceResponse = risorsa.getResponse(i);
  116.                         if (resourceResponse.getStatus() == status) {
  117.                             idResponse = resourceResponse.getId();
  118.                             break;
  119.                         }
  120.                     }
  121.                     if(resourceResponse != null)
  122.                         representationList = resourceResponse.getRepresentationList();
  123.                 }
  124.             }
  125.            
  126.             String mediaType = "";
  127.             boolean modificaAS_effettuata = false;
  128.             StringBuilder errori = new StringBuilder();
  129.             for (int i = 0; i < resourcesToRemove.size(); i++) {
  130.                 mediaType = resourcesToRemove.get(i);
  131.                
  132.                 // Effettuo eliminazione
  133.                 int idx = -1;          
  134.                 if(representationList != null && representationList.size() > 0) {
  135.                     for (int j  = 0; j  < representationList.size(); j ++) {
  136.                         ResourceRepresentation resourceRepresentation = representationList.get(j );
  137.                         if(resourceRepresentation.getMediaType().equals(mediaType)) {
  138.                             idx = j ;
  139.                             break;
  140.                         }
  141.                     }
  142.                     if(idx > -1) {
  143.                         modificaAS_effettuata = true;
  144.                         representationList.remove(idx);
  145.                     }
  146.                 }
  147.             }
  148.            
  149.             // imposto msg di errore se presente
  150.             if(errori.length()>0)
  151.                 pd.setMessage(errori.toString());

  152.             // effettuo le operazioni
  153.             if(modificaAS_effettuata)
  154.                 apcCore.performUpdateOperation(userLogin, apcHelper.smista(), as);

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

  157.             // Devo rileggere l'accordo dal db, perche' altrimenti
  158.             // manca l'id dei port-type
  159.             as = apcCore.getAccordoServizioFull(idAccordoLong);
  160.            
  161.             risorsa = null;
  162.             for (int j = 0; j < as.sizeResourceList(); j++) {
  163.                 risorsa = as.getResource(j);
  164.                 if (nomeRisorsa.equals(risorsa.getNome())) {
  165.                     break;
  166.                 }
  167.             }
  168.            
  169.             if(risorsa==null) {
  170.                 throw new Exception("Risorsa con nome '"+nomeRisorsa+"' non trovata nell'accordo con id '"+idAccordoLong+"'");
  171.             }
  172.            
  173.             idResponse = null;
  174.             idResource = null;
  175.             resourceRequest = null;
  176.             resourceResponse = null;
  177.             if(isRequest) {
  178.                 resourceRequest = risorsa.getRequest();
  179.                 idResource = risorsa.getId();
  180.             } else {
  181.                 if(risorsa.getResponseList() != null) {
  182.                     for (int i = 0; i < risorsa.getResponseList().size(); i++) {
  183.                         resourceResponse = risorsa.getResponse(i);
  184.                         if (resourceResponse.getStatus() == status) {
  185.                             idResponse = resourceResponse.getId();
  186.                             break;
  187.                         }
  188.                     }
  189.                 }
  190.             }

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

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

  193.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  194.            
  195.             return ServletUtils.getStrutsForward(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_REPRESENTATIONS, ForwardParams.DEL());
  196.            
  197.         } catch (Exception e) {
  198.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  199.                     AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_RESOURCES_REPRESENTATIONS, ForwardParams.DEL());
  200.         }
  201.     }
  202. }