AccordoServizioParteComuneInUsoCore.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.sql.Connection;
  22. import java.util.EnumMap;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.core.commons.ErrorsHandlerCostant;
  26. import org.openspcoop2.core.id.IDAccordo;
  27. import org.openspcoop2.core.id.IDPortType;
  28. import org.openspcoop2.core.id.IDPortTypeAzione;
  29. import org.openspcoop2.core.id.IDResource;
  30. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  31. import org.openspcoop2.core.registry.beans.AccordoServizioParteComuneSintetico;
  32. import org.openspcoop2.core.registry.beans.ResourceSintetica;
  33. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  34. import org.openspcoop2.core.registry.driver.DriverRegistroServiziNotFound;
  35. import org.openspcoop2.protocol.engine.utils.DBOggettiInUsoUtils;
  36. import org.openspcoop2.protocol.engine.utils.NamingUtils;
  37. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  38. import org.openspcoop2.web.ctrlstat.driver.DriverControlStationDB;
  39. import org.openspcoop2.web.ctrlstat.driver.DriverControlStationException;
  40. import org.openspcoop2.web.ctrlstat.servlet.apc.api.ApiCostanti;

  41. /**
  42.  * AccordoServizioParteComuneInUsoCore
  43.  *
  44.  * @author Poli Andrea (apoli@link.it)
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47.  */
  48. public class AccordoServizioParteComuneInUsoCore extends ControlStationCore {

  49.     private AccordoServizioParteComuneSinteticoCore sinteticoCore;
  50.    
  51.     protected AccordoServizioParteComuneInUsoCore(ControlStationCore core) throws DriverControlStationException {
  52.         super(core);
  53.         this.sinteticoCore = new AccordoServizioParteComuneSinteticoCore(core);
  54.     }
  55.    
  56.     public boolean isAccordoInUso(IDAccordo idAccordo, Map<ErrorsHandlerCostant,List<String>> whereIsInUso, boolean normalizeObjectIds) throws DriverRegistroServiziException {
  57.         Connection con = null;
  58.         String nomeMetodo = "isAccordoInUso";
  59.         DriverControlStationDB driver = null;

  60.         try {
  61.             // prendo una connessione
  62.             con = ControlStationCore.dbM.getConnection();
  63.             // istanzio il driver
  64.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  65.             return driver.isAccordoInUso(idAccordo, whereIsInUso, normalizeObjectIds);

  66.         } catch (Exception e) {
  67.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  68.             throw new DriverRegistroServiziException(getPrefixError(nomeMetodo,  e), e);
  69.         } finally {
  70.             ControlStationCore.dbM.releaseConnection(con);
  71.         }

  72.     }
  73.    
  74.     public boolean isAccordoInUso(AccordoServizioParteComune as, Map<ErrorsHandlerCostant,List<String>> whereIsInUso, boolean normalizeObjectIds) throws DriverRegistroServiziException {
  75.         Connection con = null;
  76.         String nomeMetodo = "isAccordoInUso";
  77.         DriverControlStationDB driver = null;

  78.         try {
  79.             // prendo una connessione
  80.             con = ControlStationCore.dbM.getConnection();
  81.             // istanzio il driver
  82.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  83.             return driver.isAccordoInUso(as, whereIsInUso, normalizeObjectIds);

  84.         } catch (Exception e) {
  85.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  86.             throw new DriverRegistroServiziException(getPrefixError(nomeMetodo,  e), e);
  87.         } finally {
  88.             ControlStationCore.dbM.releaseConnection(con);
  89.         }

  90.     }
  91.    
  92.     public String getDettagliAccordoInUso(IDAccordo idAccordo) throws DriverRegistroServiziException {
  93.         EnumMap<ErrorsHandlerCostant, List<String>> whereIsInUso = new EnumMap<>(ErrorsHandlerCostant.class);
  94.         boolean normalizeObjectIds = true;
  95.         boolean apcInUso  = this.isAccordoInUso(idAccordo, whereIsInUso, normalizeObjectIds );
  96.        
  97.         StringBuilder inUsoMessage = new StringBuilder();
  98.         if(apcInUso) {
  99.             String s = DBOggettiInUsoUtils.toString(idAccordo, whereIsInUso, false, "\n", normalizeObjectIds);
  100.             if(s!=null && s.startsWith("\n") && s.length()>1) {
  101.                 s = s.substring(1);
  102.             }
  103.             inUsoMessage.append(s);
  104.             inUsoMessage.append("\n");
  105.         } else {
  106.             inUsoMessage.append(ApiCostanti.LABEL_IN_USO_BODY_HEADER_NESSUN_RISULTATO);
  107.         }
  108.        
  109.         return inUsoMessage.toString();
  110.     }
  111.    
  112.     public boolean isRisorsaInUso(IDResource idRisorsa, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean normalizeObjectIds) throws DriverRegistroServiziException {
  113.         Connection con = null;
  114.         String nomeMetodo = "isRisorsaInUso";
  115.         DriverControlStationDB driver = null;

  116.         try {
  117.             // prendo una connessione
  118.             con = ControlStationCore.dbM.getConnection();
  119.             // istanzio il driver
  120.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  121.             return driver.isRisorsaInUso(idRisorsa, whereIsInUso, normalizeObjectIds);

  122.         } catch (Exception e) {
  123.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  124.             throw new DriverRegistroServiziException(getPrefixError(nomeMetodo,  e),e);
  125.         } finally {
  126.             ControlStationCore.dbM.releaseConnection(con);
  127.         }

  128.     }
  129.    
  130.     public String getDettagliRisorsaInUso(IDResource idResource) throws DriverRegistroServiziNotFound, DriverRegistroServiziException {
  131.         EnumMap<ErrorsHandlerCostant, List<String>> whereIsInUso = new EnumMap<>(ErrorsHandlerCostant.class);
  132.         boolean normalizeObjectIds = true;
  133.         boolean risorsaInUso  = this.isRisorsaInUso(idResource, whereIsInUso, normalizeObjectIds );
  134.        
  135.         StringBuilder inUsoMessage = new StringBuilder();
  136.         if(risorsaInUso) {
  137.            
  138.             // traduco nomeRisorsa in path
  139.             AccordoServizioParteComuneSintetico as = this.sinteticoCore.getAccordoServizioSintetico(idResource.getIdAccordo());        
  140.             String methodPath = getMethodPath(idResource, as);
  141.            
  142.             String s = DBOggettiInUsoUtils.toString(idResource, methodPath, whereIsInUso, false, "\n");
  143.             if(s!=null && s.startsWith("\n") && s.length()>1) {
  144.                 s = s.substring(1);
  145.             }
  146.             inUsoMessage.append(s);
  147.             inUsoMessage.append("\n");
  148.         } else {
  149.             inUsoMessage.append(AccordiServizioParteComuneCostanti.LABEL_IN_USO_ACCORDO_RISORSA_BODY_HEADER_NESSUN_RISULTATO);
  150.         }
  151.        
  152.         return inUsoMessage.toString();
  153.     }
  154.     private String getMethodPath(IDResource idResource, AccordoServizioParteComuneSintetico as) throws DriverRegistroServiziException {
  155.         String methodPath = null;
  156.         if(as.getResource()!=null) {
  157.             for (int j = 0; j < as.getResource().size(); j++) {
  158.                 ResourceSintetica risorsa = as.getResource().get(j);
  159.                 if (idResource.getNome().equals(risorsa.getNome())) {
  160.                     try {
  161.                         methodPath = NamingUtils.getLabelResource(risorsa);
  162.                     }catch(Exception e) {
  163.                         throw new DriverRegistroServiziException(e.getMessage(),e);
  164.                     }
  165.                     break;
  166.                 }
  167.             }
  168.         }
  169.         if(methodPath==null) {
  170.             methodPath = idResource.getNome();
  171.         }
  172.         return methodPath;
  173.     }
  174.    
  175.     public boolean isPortTypeInUso(IDPortType idPT, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean normalizeObjectIds) throws DriverRegistroServiziException {
  176.         Connection con = null;
  177.         String nomeMetodo = "isPortTypeInUso";
  178.         DriverControlStationDB driver = null;

  179.         try {
  180.             // prendo una connessione
  181.             con = ControlStationCore.dbM.getConnection();
  182.             // istanzio il driver
  183.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  184.             return driver.isPortTypeInUso(idPT, whereIsInUso, normalizeObjectIds);

  185.         } catch (Exception e) {
  186.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  187.             throw new DriverRegistroServiziException(getPrefixError(nomeMetodo,  e),e);
  188.         } finally {
  189.             ControlStationCore.dbM.releaseConnection(con);
  190.         }

  191.     }
  192.    
  193.     public String getDettagliPortTypeInUso(IDPortType idPT) throws DriverRegistroServiziException {
  194.         EnumMap<ErrorsHandlerCostant, List<String>> whereIsInUso = new EnumMap<>(ErrorsHandlerCostant.class);
  195.         boolean normalizeObjectIds = true;
  196.         boolean ptInUso  = this.isPortTypeInUso(idPT, whereIsInUso, normalizeObjectIds );
  197.        
  198.         StringBuilder inUsoMessage = new StringBuilder();
  199.         if(ptInUso) {
  200.             String s = DBOggettiInUsoUtils.toString(idPT, whereIsInUso, false, "\n");
  201.             if(s!=null && s.startsWith("\n") && s.length()>1) {
  202.                 s = s.substring(1);
  203.             }
  204.             inUsoMessage.append(s);
  205.             inUsoMessage.append("\n");
  206.         } else {
  207.             inUsoMessage.append(AccordiServizioParteComuneCostanti.LABEL_IN_USO_ACCORDO_PORT_TYPE_BODY_HEADER_NESSUN_RISULTATO);
  208.         }
  209.        
  210.         return inUsoMessage.toString();
  211.     }
  212.    
  213.     public boolean isOperazioneInUso(IDPortTypeAzione idOperazione, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean normalizeObjectIds) throws DriverRegistroServiziException {
  214.         Connection con = null;
  215.         String nomeMetodo = "isOperazioneInUso";
  216.         DriverControlStationDB driver = null;

  217.         try {
  218.             // prendo una connessione
  219.             con = ControlStationCore.dbM.getConnection();
  220.             // istanzio il driver
  221.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  222.             return driver.isOperazioneInUso(idOperazione, whereIsInUso, normalizeObjectIds);

  223.         } catch (Exception e) {
  224.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  225.             throw new DriverRegistroServiziException(getPrefixError(nomeMetodo,  e),e);
  226.         } finally {
  227.             ControlStationCore.dbM.releaseConnection(con);
  228.         }

  229.     }
  230.    
  231.     public String getDettagliOperazioneInUso(IDPortTypeAzione idOperazione) throws DriverRegistroServiziException {
  232.         EnumMap<ErrorsHandlerCostant, List<String>> whereIsInUso = new EnumMap<>(ErrorsHandlerCostant.class);
  233.         boolean normalizeObjectIds = true;
  234.         boolean risorsaInUso  = this.isOperazioneInUso(idOperazione, whereIsInUso, normalizeObjectIds );
  235.        
  236.         StringBuilder inUsoMessage = new StringBuilder();
  237.         if(risorsaInUso) {
  238.             String s = DBOggettiInUsoUtils.toString(idOperazione, whereIsInUso, false, "\n");
  239.             if(s!=null && s.startsWith("\n") && s.length()>1) {
  240.                 s = s.substring(1);
  241.             }
  242.             inUsoMessage.append(s);
  243.             inUsoMessage.append("\n");
  244.         } else {
  245.             inUsoMessage.append(AccordiServizioParteComuneCostanti.LABEL_IN_USO_ACCORDO_OPERAZIONE_BODY_HEADER_NESSUN_RISULTATO);
  246.         }
  247.        
  248.         return inUsoMessage.toString();
  249.     }
  250. }