DBOggettiInUsoUtils_serviziApplicativi.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.protocol.engine.utils;

  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;

  28. import org.openspcoop2.core.commons.DBUtils;
  29. import org.openspcoop2.core.commons.ErrorsHandlerCostant;
  30. import org.openspcoop2.core.config.constants.RuoloTipoMatch;
  31. import org.openspcoop2.core.constants.CostantiDB;
  32. import org.openspcoop2.core.constants.ProprietariProtocolProperty;
  33. import org.openspcoop2.core.id.IDPortaApplicativa;
  34. import org.openspcoop2.core.id.IDServizio;
  35. import org.openspcoop2.core.id.IDServizioApplicativo;
  36. import org.openspcoop2.core.mapping.DBMappingUtils;
  37. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  38. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  39. import org.openspcoop2.protocol.engine.constants.Costanti;
  40. import org.openspcoop2.utils.UtilsException;
  41. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  42. import org.openspcoop2.utils.sql.ISQLQueryObject;
  43. import org.openspcoop2.utils.sql.SQLObjectFactory;

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


  53.     protected static boolean isServizioApplicativoInUso(Connection con, String tipoDB, IDServizioApplicativo idServizioApplicativo,
  54.             Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean isRegistroServiziLocale, boolean normalizeObjectIds,
  55.             boolean verificaRuoli) throws UtilsException {

  56.         if(isRegistroServiziLocale) {
  57.             // nop
  58.         }
  59.        
  60.         String nomeMetodo = "isServizioApplicativoInUso";

  61.         PreparedStatement stmt = null;
  62.         ResultSet risultato = null;
  63.         PreparedStatement stmt2 = null;
  64.         ResultSet risultato2 = null;
  65.         String queryString;
  66.         try {
  67.             boolean isInUso = false;

  68.             long idServizioApplicativoLong = DBUtils.getIdServizioApplicativo(idServizioApplicativo.getNome(),
  69.                     idServizioApplicativo.getIdSoggettoProprietario().getTipo(),
  70.                     idServizioApplicativo.getIdSoggettoProprietario().getNome(),
  71.                     con, tipoDB);
  72.             if(idServizioApplicativoLong<=0){
  73.                 throw new UtilsException("Servizio Applicativo con id ["+idServizioApplicativo+"] non trovato");
  74.             }


  75.             List<String> autorizzazionePDMappingList = whereIsInUso.get(ErrorsHandlerCostant.AUTORIZZAZIONE_MAPPING);
  76.             List<String> autorizzazionePDList = whereIsInUso.get(ErrorsHandlerCostant.AUTORIZZAZIONE);
  77.             List<String> autorizzazionePAMappingList = whereIsInUso.get(ErrorsHandlerCostant.AUTORIZZAZIONE_MAPPING_PA);
  78.             List<String> autorizzazionePAList = whereIsInUso.get(ErrorsHandlerCostant.AUTORIZZAZIONE_PA);
  79.             List<String> autorizzazionePAMappingModiList = whereIsInUso.get(ErrorsHandlerCostant.AUTORIZZAZIONE_MAPPING_PA_MODI);
  80.             List<String> autorizzazionePAModiList = whereIsInUso.get(ErrorsHandlerCostant.AUTORIZZAZIONE_PA_MODI);
  81.             List<String> autorizzazioneTokenPDMappingList = whereIsInUso.get(ErrorsHandlerCostant.AUTORIZZAZIONE_TOKEN_MAPPING_PD);
  82.             List<String> autorizzazioneTokenPDList = whereIsInUso.get(ErrorsHandlerCostant.AUTORIZZAZIONE_TOKEN_PD);
  83.             List<String> autorizzazioneTokenPAMappingList = whereIsInUso.get(ErrorsHandlerCostant.AUTORIZZAZIONE_TOKEN_MAPPING_PA);
  84.             List<String> autorizzazioneTokenPAList = whereIsInUso.get(ErrorsHandlerCostant.AUTORIZZAZIONE_TOKEN_PA);
  85.             List<String> ruoliPDMappingList = whereIsInUso.get(ErrorsHandlerCostant.RUOLI_MAPPING);
  86.             List<String> ruoliPDList = whereIsInUso.get(ErrorsHandlerCostant.RUOLI);
  87.             List<String> ruoliPAMappingList = whereIsInUso.get(ErrorsHandlerCostant.RUOLI_MAPPING_PA);
  88.             List<String> ruoliPAList = whereIsInUso.get(ErrorsHandlerCostant.RUOLI_PA);
  89.             List<String> ruoliTokenPDMappingList = whereIsInUso.get(ErrorsHandlerCostant.RUOLI_TOKEN_MAPPING_PD);
  90.             List<String> ruoliTokenPDList = whereIsInUso.get(ErrorsHandlerCostant.RUOLI_TOKEN_PD);
  91.             List<String> ruoliTokenPAMappingList = whereIsInUso.get(ErrorsHandlerCostant.RUOLI_TOKEN_MAPPING_PA);
  92.             List<String> ruoliTokenPAList = whereIsInUso.get(ErrorsHandlerCostant.RUOLI_TOKEN_PA);
  93.             List<String> ruoliTokenPAMappingModiList = whereIsInUso.get(ErrorsHandlerCostant.RUOLI_TOKEN_MAPPING_PA_MODI);
  94.             List<String> ruoliTokenPAModiList = whereIsInUso.get(ErrorsHandlerCostant.RUOLI_TOKEN_PA_MODI);
  95.             List<String> porteApplicativeList = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_PORTE_APPLICATIVE);
  96.             List<String> porteApplicativeMappingList = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_MAPPING_EROGAZIONE_PA);
  97.             List<String> ctList = whereIsInUso.get(ErrorsHandlerCostant.CONTROLLO_TRAFFICO);
  98.             List<String> allarmeList = whereIsInUso.get(ErrorsHandlerCostant.ALLARMI);
  99.             List<String> trasformazionePDMappingList = whereIsInUso.get(ErrorsHandlerCostant.TRASFORMAZIONE_MAPPING_PD);
  100.             List<String> trasformazionePDList = whereIsInUso.get(ErrorsHandlerCostant.TRASFORMAZIONE_PD);
  101.             List<String> trasformazionePAMappingList = whereIsInUso.get(ErrorsHandlerCostant.TRASFORMAZIONE_MAPPING_PA);
  102.             List<String> trasformazionePAList = whereIsInUso.get(ErrorsHandlerCostant.TRASFORMAZIONE_PA);
  103.             List<String> modiSignalHubList = whereIsInUso.get(ErrorsHandlerCostant.IS_RIFERITA_MODI_SIGNAL_HUB);
  104.            
  105.             if (autorizzazionePDMappingList == null) {
  106.                 autorizzazionePDMappingList = new ArrayList<>();
  107.                 whereIsInUso.put(ErrorsHandlerCostant.AUTORIZZAZIONE_MAPPING, autorizzazionePDMappingList);
  108.             }
  109.             if (autorizzazionePDList == null) {
  110.                 autorizzazionePDList = new ArrayList<>();
  111.                 whereIsInUso.put(ErrorsHandlerCostant.AUTORIZZAZIONE, autorizzazionePDList);
  112.             }
  113.             if (autorizzazionePAMappingList == null) {
  114.                 autorizzazionePAMappingList = new ArrayList<>();
  115.                 whereIsInUso.put(ErrorsHandlerCostant.AUTORIZZAZIONE_MAPPING_PA, autorizzazionePAMappingList);
  116.             }
  117.             if (autorizzazionePAList == null) {
  118.                 autorizzazionePAList = new ArrayList<>();
  119.                 whereIsInUso.put(ErrorsHandlerCostant.AUTORIZZAZIONE_PA, autorizzazionePAList);
  120.             }
  121.             if (autorizzazionePAMappingModiList == null) {
  122.                 autorizzazionePAMappingModiList = new ArrayList<>();
  123.                 whereIsInUso.put(ErrorsHandlerCostant.AUTORIZZAZIONE_MAPPING_PA_MODI, autorizzazionePAMappingModiList);
  124.             }
  125.             if (autorizzazionePAModiList == null) {
  126.                 autorizzazionePAModiList = new ArrayList<>();
  127.                 whereIsInUso.put(ErrorsHandlerCostant.AUTORIZZAZIONE_PA_MODI, autorizzazionePAModiList);
  128.             }
  129.            
  130.             if (autorizzazioneTokenPDMappingList == null) {
  131.                 autorizzazioneTokenPDMappingList = new ArrayList<>();
  132.                 whereIsInUso.put(ErrorsHandlerCostant.AUTORIZZAZIONE_TOKEN_MAPPING_PD, autorizzazioneTokenPDMappingList);
  133.             }
  134.             if (autorizzazioneTokenPDList == null) {
  135.                 autorizzazioneTokenPDList = new ArrayList<>();
  136.                 whereIsInUso.put(ErrorsHandlerCostant.AUTORIZZAZIONE_TOKEN_PD, autorizzazioneTokenPDList);
  137.             }
  138.             if (autorizzazioneTokenPAMappingList == null) {
  139.                 autorizzazioneTokenPAMappingList = new ArrayList<>();
  140.                 whereIsInUso.put(ErrorsHandlerCostant.AUTORIZZAZIONE_TOKEN_MAPPING_PA, autorizzazioneTokenPAMappingList);
  141.             }
  142.             if (autorizzazioneTokenPAList == null) {
  143.                 autorizzazioneTokenPAList = new ArrayList<>();
  144.                 whereIsInUso.put(ErrorsHandlerCostant.AUTORIZZAZIONE_TOKEN_PA, autorizzazioneTokenPAList);
  145.             }
  146.            
  147.             if (ruoliPDMappingList == null) {
  148.                 ruoliPDMappingList = new ArrayList<>();
  149.                 whereIsInUso.put(ErrorsHandlerCostant.RUOLI_MAPPING, ruoliPDMappingList);
  150.             }
  151.             if (ruoliPDList == null) {
  152.                 ruoliPDList = new ArrayList<>();
  153.                 whereIsInUso.put(ErrorsHandlerCostant.RUOLI, ruoliPDList);
  154.             }
  155.             if (ruoliPAMappingList == null) {
  156.                 ruoliPAMappingList = new ArrayList<>();
  157.                 whereIsInUso.put(ErrorsHandlerCostant.RUOLI_MAPPING_PA, ruoliPAMappingList);
  158.             }
  159.             if (ruoliPAList == null) {
  160.                 ruoliPAList = new ArrayList<>();
  161.                 whereIsInUso.put(ErrorsHandlerCostant.RUOLI_PA, ruoliPAList);
  162.             }
  163.            
  164.             if (ruoliTokenPDMappingList == null) {
  165.                 ruoliTokenPDMappingList = new ArrayList<>();
  166.                 whereIsInUso.put(ErrorsHandlerCostant.RUOLI_TOKEN_MAPPING_PD, ruoliPDMappingList);
  167.             }
  168.             if (ruoliTokenPDList == null) {
  169.                 ruoliTokenPDList = new ArrayList<>();
  170.                 whereIsInUso.put(ErrorsHandlerCostant.RUOLI_TOKEN_PD, ruoliTokenPDList);
  171.             }
  172.             if (ruoliTokenPAMappingList == null) {
  173.                 ruoliTokenPAMappingList = new ArrayList<>();
  174.                 whereIsInUso.put(ErrorsHandlerCostant.RUOLI_TOKEN_MAPPING_PA, ruoliTokenPAMappingList);
  175.             }
  176.             if (ruoliTokenPAList == null) {
  177.                 ruoliTokenPAList = new ArrayList<>();
  178.                 whereIsInUso.put(ErrorsHandlerCostant.RUOLI_TOKEN_PA, ruoliTokenPAList);
  179.             }
  180.             if (ruoliTokenPAMappingModiList == null) {
  181.                 ruoliTokenPAMappingModiList = new ArrayList<>();
  182.                 whereIsInUso.put(ErrorsHandlerCostant.RUOLI_TOKEN_MAPPING_PA_MODI, ruoliTokenPAMappingModiList);
  183.             }
  184.             if (ruoliTokenPAModiList == null) {
  185.                 ruoliTokenPAModiList = new ArrayList<>();
  186.                 whereIsInUso.put(ErrorsHandlerCostant.RUOLI_TOKEN_PA_MODI, ruoliTokenPAModiList);
  187.             }
  188.            
  189.             if (porteApplicativeList == null) {
  190.                 porteApplicativeList = new ArrayList<>();
  191.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_PORTE_APPLICATIVE, porteApplicativeList);
  192.             }
  193.             if (porteApplicativeMappingList == null) {
  194.                 porteApplicativeMappingList = new ArrayList<>();
  195.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_MAPPING_EROGAZIONE_PA, porteApplicativeMappingList);
  196.             }
  197.             if (ctList == null) {
  198.                 ctList = new ArrayList<>();
  199.                 whereIsInUso.put(ErrorsHandlerCostant.CONTROLLO_TRAFFICO, ctList);
  200.             }
  201.             if (allarmeList == null) {
  202.                 allarmeList = new ArrayList<>();
  203.                 whereIsInUso.put(ErrorsHandlerCostant.ALLARMI, allarmeList);
  204.             }
  205.             if (trasformazionePDMappingList == null) {
  206.                 trasformazionePDMappingList = new ArrayList<>();
  207.                 whereIsInUso.put(ErrorsHandlerCostant.TRASFORMAZIONE_MAPPING_PD, trasformazionePDMappingList);
  208.             }
  209.             if (trasformazionePDList == null) {
  210.                 trasformazionePDList = new ArrayList<>();
  211.                 whereIsInUso.put(ErrorsHandlerCostant.TRASFORMAZIONE_PD, trasformazionePDList);
  212.             }
  213.             if (trasformazionePAMappingList == null) {
  214.                 trasformazionePAMappingList = new ArrayList<>();
  215.                 whereIsInUso.put(ErrorsHandlerCostant.TRASFORMAZIONE_MAPPING_PA, trasformazionePAMappingList);
  216.             }
  217.             if (trasformazionePAList == null) {
  218.                 trasformazionePAList = new ArrayList<>();
  219.                 whereIsInUso.put(ErrorsHandlerCostant.TRASFORMAZIONE_PA, trasformazionePAList);
  220.             }
  221.            
  222.             if (modiSignalHubList == null) {
  223.                 modiSignalHubList = new ArrayList<>();
  224.                 whereIsInUso.put(ErrorsHandlerCostant.IS_RIFERITA_MODI_SIGNAL_HUB, modiSignalHubList);
  225.             }

  226.             // Porte delegate, autorizzazione
  227.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  228.             sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE_SA);
  229.             sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE);
  230.             sqlQueryObject.addSelectField("nome_porta");
  231.             sqlQueryObject.setANDLogicOperator(true);
  232.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE_SA+".id_porta="+CostantiDB.PORTE_DELEGATE+".id");
  233.             sqlQueryObject.addWhereCondition("id_servizio_applicativo=?");
  234.             queryString = sqlQueryObject.createSQLQuery();
  235.             stmt = con.prepareStatement(queryString);
  236.             stmt.setLong(1, idServizioApplicativoLong);
  237.             risultato = stmt.executeQuery();
  238.             while (risultato.next()){
  239.                 String nome = risultato.getString("nome_porta");
  240.                 ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaDelegata(nome, tipoDB, con, normalizeObjectIds);
  241.                 if(resultPorta.mapping) {
  242.                     autorizzazionePDMappingList.add(resultPorta.label);
  243.                 }
  244.                 else {
  245.                     autorizzazionePDList.add(resultPorta.label);
  246.                 }
  247.                 isInUso = true;
  248.             }
  249.             risultato.close();
  250.             stmt.close();
  251.            
  252.            
  253.             // Porte delegate, autorizzazione (token)
  254.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  255.             sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE_TOKEN_SA);
  256.             sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE);
  257.             sqlQueryObject.addSelectField("nome_porta");
  258.             sqlQueryObject.setANDLogicOperator(true);
  259.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE_TOKEN_SA+".id_porta="+CostantiDB.PORTE_DELEGATE+".id");
  260.             sqlQueryObject.addWhereCondition("id_servizio_applicativo=?");
  261.             queryString = sqlQueryObject.createSQLQuery();
  262.             stmt = con.prepareStatement(queryString);
  263.             stmt.setLong(1, idServizioApplicativoLong);
  264.             risultato = stmt.executeQuery();
  265.             while (risultato.next()){
  266.                 String nome = risultato.getString("nome_porta");
  267.                 ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaDelegata(nome, tipoDB, con, normalizeObjectIds);
  268.                 if(resultPorta.mapping) {
  269.                     autorizzazioneTokenPDMappingList.add(resultPorta.label);
  270.                 }
  271.                 else {
  272.                     autorizzazioneTokenPDList.add(resultPorta.label);
  273.                 }
  274.                 isInUso = true;
  275.             }
  276.             risultato.close();
  277.             stmt.close();
  278.            
  279.            
  280.             // Porte applicative, autorizzazione
  281.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  282.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE_SA_AUTORIZZATI);
  283.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  284.             sqlQueryObject.addSelectField("nome_porta");
  285.             sqlQueryObject.setANDLogicOperator(true);
  286.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE_SA_AUTORIZZATI+".id_porta="+CostantiDB.PORTE_APPLICATIVE+".id");
  287.             sqlQueryObject.addWhereCondition("id_servizio_applicativo=?");
  288.             queryString = sqlQueryObject.createSQLQuery();
  289.             stmt = con.prepareStatement(queryString);
  290.             stmt.setLong(1, idServizioApplicativoLong);
  291.             risultato = stmt.executeQuery();
  292.             while (risultato.next()){
  293.                 String nome = risultato.getString("nome_porta");
  294.                 ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaApplicativa(nome, tipoDB, con, normalizeObjectIds);
  295.                 if(resultPorta.mapping) {
  296.                     if(resultPorta.erogazioneModi)
  297.                         autorizzazionePAMappingModiList.add(resultPorta.label);
  298.                     else
  299.                         autorizzazionePAMappingList.add(resultPorta.label);
  300.                 }
  301.                 else {
  302.                     if(resultPorta.erogazioneModi)
  303.                         autorizzazionePAModiList.add(resultPorta.label);
  304.                     else
  305.                         autorizzazionePAList.add(resultPorta.label);
  306.                 }
  307.                 isInUso = true;
  308.             }
  309.             risultato.close();
  310.             stmt.close();
  311.            
  312.            
  313.             // Porte applicative, autorizzazione (token)
  314.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  315.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE_TOKEN_SA);
  316.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  317.             sqlQueryObject.addSelectField("nome_porta");
  318.             sqlQueryObject.setANDLogicOperator(true);
  319.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE_TOKEN_SA+".id_porta="+CostantiDB.PORTE_APPLICATIVE+".id");
  320.             sqlQueryObject.addWhereCondition("id_servizio_applicativo=?");
  321.             queryString = sqlQueryObject.createSQLQuery();
  322.             stmt = con.prepareStatement(queryString);
  323.             stmt.setLong(1, idServizioApplicativoLong);
  324.             risultato = stmt.executeQuery();
  325.             while (risultato.next()){
  326.                 String nome = risultato.getString("nome_porta");
  327.                 ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaApplicativa(nome, tipoDB, con, normalizeObjectIds);
  328.                 if(resultPorta.mapping) {
  329.                     autorizzazioneTokenPAMappingList.add(resultPorta.label);
  330.                 }
  331.                 else {
  332.                     autorizzazioneTokenPAList.add(resultPorta.label);
  333.                 }
  334.                 isInUso = true;
  335.             }
  336.             risultato.close();
  337.             stmt.close();

  338.            
  339.            
  340.             if(verificaRuoli) {
  341.                
  342.                 // Raccolgo prima i ruoli
  343.                 List<String> listRuoliSA = new ArrayList<>();
  344.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  345.                 sqlQueryObject.addFromTable(CostantiDB.SERVIZI_APPLICATIVI);
  346.                 sqlQueryObject.addFromTable(CostantiDB.SERVIZI_APPLICATIVI_RUOLI);
  347.                 sqlQueryObject.addSelectField("ruolo");
  348.                 sqlQueryObject.setANDLogicOperator(true);
  349.                 sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI_APPLICATIVI+".id=?");
  350.                 sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI_APPLICATIVI_RUOLI+".id_servizio_applicativo="+CostantiDB.SERVIZI_APPLICATIVI+".id");
  351.                 queryString = sqlQueryObject.createSQLQuery();
  352.                 stmt = con.prepareStatement(queryString);
  353.                 stmt.setLong(1, idServizioApplicativoLong);
  354.                 risultato = stmt.executeQuery();
  355.                 while (risultato.next()){
  356.                     String ruolo = risultato.getString("ruolo");
  357.                     listRuoliSA.add(ruolo);
  358.                 }
  359.                 risultato.close();
  360.                 stmt.close();
  361.                
  362.                 if(!listRuoliSA.isEmpty()) {
  363.                
  364.                     checkServizioApplicativoRuoloInUsoInPorteDelegate(con, tipoDB, normalizeObjectIds,
  365.                             CostantiDB.PORTE_DELEGATE_RUOLI,
  366.                             listRuoliSA,
  367.                             ruoliPDMappingList, ruoliPDList);
  368.                    
  369.                     checkServizioApplicativoRuoloInUsoInPorteDelegate(con, tipoDB, normalizeObjectIds,
  370.                             CostantiDB.PORTE_DELEGATE_TOKEN_RUOLI,
  371.                             listRuoliSA,
  372.                             ruoliTokenPDMappingList, ruoliTokenPDList);
  373.                                    
  374.                     boolean isInUsoRuoli = checkServizioApplicativoRuoloInUsoInPorteApplicative(con, tipoDB, normalizeObjectIds,
  375.                             CostantiDB.PORTE_APPLICATIVE_RUOLI,
  376.                             listRuoliSA,
  377.                             ruoliPAMappingList, ruoliPAList,
  378.                             ruoliPAMappingList, ruoliPAList);
  379.                     if(isInUsoRuoli) {
  380.                         isInUso = true;
  381.                     }
  382.                    
  383.                     boolean isInUsoRuoliToken = checkServizioApplicativoRuoloInUsoInPorteApplicative(con, tipoDB, normalizeObjectIds,
  384.                             CostantiDB.PORTE_APPLICATIVE_TOKEN_RUOLI,
  385.                             listRuoliSA,
  386.                             ruoliTokenPAMappingList, ruoliTokenPAList,
  387.                             ruoliTokenPAMappingModiList, ruoliTokenPAModiList);
  388.                     if(isInUsoRuoliToken) {
  389.                         isInUso = true;
  390.                     }
  391.                 }
  392.             }
  393.            


  394.             // Porte applicative
  395.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  396.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE_SA);
  397.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  398.             sqlQueryObject.addSelectField("*");
  399.             sqlQueryObject.setANDLogicOperator(true);
  400.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE_SA+".id_porta="+CostantiDB.PORTE_APPLICATIVE+".id");
  401.             sqlQueryObject.addWhereCondition("id_servizio_applicativo=?");
  402.             queryString = sqlQueryObject.createSQLQuery();
  403.             stmt = con.prepareStatement(queryString);
  404.             stmt.setLong(1, idServizioApplicativoLong);
  405.             risultato = stmt.executeQuery();
  406.             while (risultato.next()){
  407.                 String nome = risultato.getString("nome_porta");
  408.                 ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaApplicativa(nome, tipoDB, con, normalizeObjectIds);
  409.                 if(resultPorta.mapping) {
  410.                     porteApplicativeMappingList.add(resultPorta.label);
  411.                 }
  412.                 else {
  413.                     porteApplicativeList.add(resultPorta.label);
  414.                 }
  415.                 isInUso = true;
  416.             }
  417.             risultato.close();
  418.             stmt.close();

  419.            
  420.             // Controllo che il servizio aplicativo non sia associato a policy di controllo del traffico o allarmi
  421.            
  422.             int max = 2;
  423.             if(!CostantiDB.isAllarmiEnabled()) {
  424.                 max=1;
  425.             }
  426.             for (int i = 0; i < max; i++) {
  427.                
  428.                 String tabella = CostantiDB.CONTROLLO_TRAFFICO_ACTIVE_POLICY;
  429.                 String identificativo_column = "active_policy_id";
  430.                 String alias_column = "policy_alias";
  431.                 List<String> list = ctList;
  432.                 String oggetto = "Policy";
  433.                 boolean allarmi = false;
  434.                 if(i==1) {
  435.                     tabella = CostantiDB.ALLARMI;
  436.                     identificativo_column = "nome";
  437.                     alias_column = "alias";
  438.                     list = allarmeList;
  439.                     oggetto = "Allarme";
  440.                     allarmi=true;
  441.                 }
  442.                
  443.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  444.                 sqlQueryObject.addFromTable(tabella);
  445.                 sqlQueryObject.addSelectField(identificativo_column);
  446.                 sqlQueryObject.addSelectField(alias_column);
  447.                 sqlQueryObject.addSelectField("filtro_ruolo");
  448.                 sqlQueryObject.addSelectField("filtro_porta");
  449.                 sqlQueryObject.setANDLogicOperator(false); // OR
  450.                 sqlQueryObject.addWhereCondition(true,
  451.                         tabella+".filtro_tipo_fruitore = ?",
  452.                         tabella+".filtro_nome_fruitore = ?",
  453.                         tabella+".filtro_sa_fruitore = ?");
  454.                 if(!allarmi) {
  455.                     sqlQueryObject.addWhereCondition(true,
  456.                             tabella+".filtro_tipo_erogatore = ?",
  457.                             tabella+".filtro_nome_erogatore = ?",
  458.                             tabella+".filtro_sa_erogatore = ?");
  459.                 }
  460.                 sqlQueryObject.addOrderBy("filtro_ruolo");
  461.                 sqlQueryObject.addOrderBy("filtro_porta");
  462.                 queryString = sqlQueryObject.createSQLQuery();
  463.                 stmt = con.prepareStatement(queryString);
  464.                 int index = 1;
  465.                 stmt.setString(index++, idServizioApplicativo.getIdSoggettoProprietario().getTipo());
  466.                 stmt.setString(index++, idServizioApplicativo.getIdSoggettoProprietario().getNome());
  467.                 stmt.setString(index++, idServizioApplicativo.getNome());
  468.                 if(!allarmi) {
  469.                     stmt.setString(index++, idServizioApplicativo.getIdSoggettoProprietario().getTipo());
  470.                     stmt.setString(index++, idServizioApplicativo.getIdSoggettoProprietario().getNome());
  471.                     stmt.setString(index++, idServizioApplicativo.getNome());
  472.                 }
  473.                 risultato = stmt.executeQuery();
  474.                 while (risultato.next()) {
  475.                    
  476.                     String alias = risultato.getString(alias_column);
  477.                     if(alias== null || "".equals(alias)) {
  478.                         alias = risultato.getString(identificativo_column);
  479.                     }
  480.                    
  481.                     String nomePorta = risultato.getString("filtro_porta");
  482.                     String filtro_ruolo = risultato.getString("filtro_ruolo");
  483.                     if(nomePorta!=null) {
  484.                         String tipo = null;
  485.                         String label = null;
  486.                         if("delegata".equals(filtro_ruolo)) {
  487.                             try {
  488.                                 ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaDelegata(nomePorta, tipoDB, con, normalizeObjectIds);
  489.                                 if(resultPorta.mapping) {
  490.                                     label = "Fruizione di Servizio "+ resultPorta.label;
  491.                                 }
  492.                             }catch(Exception e) {
  493.                                 tipo = "Outbound";
  494.                             }
  495.                         }
  496.                         else if("applicativa".equals(filtro_ruolo)) {
  497.                             try {
  498.                                 ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaApplicativa(nomePorta, tipoDB, con, normalizeObjectIds);
  499.                                 if(resultPorta.mapping) {
  500.                                     label = "Erogazione di Servizio "+ resultPorta.label;
  501.                                 }
  502.                             }catch(Exception e) {
  503.                                 tipo = "Inbound";
  504.                             }
  505.                         }
  506.                         else {
  507.                             tipo = filtro_ruolo;
  508.                         }
  509.                         if(label==null) {
  510.                             list.add(oggetto+" '"+alias+"' attiva nella porta '"+tipo+"' '"+nomePorta+"' ");
  511.                         }
  512.                         else {
  513.                             list.add(oggetto+" '"+alias+"' attiva nella "+label);
  514.                         }
  515.                     }
  516.                     else {
  517.                         list.add(oggetto+" '"+alias+"'");
  518.                     }
  519.    
  520.                     isInUso = true;
  521.                 }
  522.                 risultato.close();
  523.                 stmt.close();
  524.             }
  525.            
  526.            
  527.            
  528.             // Porte delegate, trasformazioni
  529.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  530.             sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE_TRASFORMAZIONI_SA);
  531.             sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE_TRASFORMAZIONI);
  532.             sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE);
  533.             sqlQueryObject.addSelectField("nome_porta");
  534.             sqlQueryObject.setANDLogicOperator(true);
  535.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE_TRASFORMAZIONI+".id_porta="+CostantiDB.PORTE_DELEGATE+".id");
  536.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE_TRASFORMAZIONI_SA+".id_trasformazione="+CostantiDB.PORTE_DELEGATE_TRASFORMAZIONI+".id");
  537.             sqlQueryObject.addWhereCondition("id_servizio_applicativo=?");
  538.             queryString = sqlQueryObject.createSQLQuery();
  539.             stmt = con.prepareStatement(queryString);
  540.             stmt.setLong(1, idServizioApplicativoLong);
  541.             risultato = stmt.executeQuery();
  542.             while (risultato.next()){
  543.                 String nome = risultato.getString("nome_porta");
  544.                 ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaDelegata(nome, tipoDB, con, normalizeObjectIds);
  545.                 if(resultPorta.mapping) {
  546.                     trasformazionePDMappingList.add(resultPorta.label);
  547.                 }
  548.                 else {
  549.                     trasformazionePDList.add(resultPorta.label);
  550.                 }
  551.                 isInUso = true;
  552.             }
  553.             risultato.close();
  554.             stmt.close();
  555.            
  556.            
  557.            
  558.             // Porte applicative, trasformazioni
  559.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  560.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE_TRASFORMAZIONI_SA);
  561.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE_TRASFORMAZIONI);
  562.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  563.             sqlQueryObject.addSelectField("nome_porta");
  564.             sqlQueryObject.setANDLogicOperator(true);
  565.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE_TRASFORMAZIONI+".id_porta="+CostantiDB.PORTE_APPLICATIVE+".id");
  566.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE_TRASFORMAZIONI_SA+".id_trasformazione="+CostantiDB.PORTE_APPLICATIVE_TRASFORMAZIONI+".id");
  567.             sqlQueryObject.addWhereCondition("id_servizio_applicativo=?");
  568.             queryString = sqlQueryObject.createSQLQuery();
  569.             stmt = con.prepareStatement(queryString);
  570.             stmt.setLong(1, idServizioApplicativoLong);
  571.             risultato = stmt.executeQuery();
  572.             while (risultato.next()){
  573.                 String nome = risultato.getString("nome_porta");
  574.                 ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaApplicativa(nome, tipoDB, con, normalizeObjectIds);
  575.                 if(resultPorta.mapping) {
  576.                     trasformazionePAMappingList.add(resultPorta.label);
  577.                 }
  578.                 else {
  579.                     trasformazionePAList.add(resultPorta.label);
  580.                 }
  581.                 isInUso = true;
  582.             }
  583.             risultato.close();
  584.             stmt.close();
  585.            
  586.            
  587.            
  588.            
  589.             // ** Controllo che non sia riferito dalla configurazione SignalHub **      
  590.            
  591.            
  592.             if(Costanti.MODIPA_PROTOCOL_NAME.equals(idServizioApplicativo.getIdSoggettoProprietario().getTipo())) {
  593.            
  594.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  595.                 sqlQueryObject.addFromTable(CostantiDB.SERVIZI);
  596.                 sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  597.                 sqlQueryObject.addFromTable(CostantiDB.PROTOCOL_PROPERTIES);
  598.                 sqlQueryObject.addSelectAliasField(CostantiDB.SERVIZI,"tipo_servizio","tipoServizio");
  599.                 sqlQueryObject.addSelectAliasField(CostantiDB.SERVIZI,"nome_servizio","nomeServizio");
  600.                 sqlQueryObject.addSelectAliasField(CostantiDB.SERVIZI,"versione_servizio","versioneServizio");
  601.                 sqlQueryObject.addSelectAliasField(CostantiDB.SOGGETTI,"tipo_soggetto","tipoSoggetto");
  602.                 sqlQueryObject.addSelectAliasField(CostantiDB.SOGGETTI,"nome_soggetto","nomeSoggetto");
  603.                 sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI + ".id_soggetto = " + CostantiDB.SOGGETTI + ".id");
  604.                 sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI + ".id = " + CostantiDB.PROTOCOL_PROPERTIES + ".id_proprietario");
  605.                 sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".tipo_proprietario=?");
  606.                 sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".name=?");
  607.                 sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".value_string=?");
  608.                 sqlQueryObject.setANDLogicOperator(true);
  609.                 queryString = sqlQueryObject.createSQLQuery();
  610.                 stmt = con.prepareStatement(queryString);
  611.                 int index = 1;
  612.                 stmt.setString(index++, ProprietariProtocolProperty.ACCORDO_SERVIZIO_PARTE_SPECIFICA.name());
  613.                 stmt.setString(index++, CostantiDB.MODIPA_API_IMPL_INFO_SIGNAL_HUB_PUBLISHER_SA_ID);
  614.                 stmt.setString(index++, idServizioApplicativo.getNome());
  615.                 risultato = stmt.executeQuery();
  616.                 while(risultato.next()) {
  617.                    
  618.                     IDServizio idS = IDServizioFactory.getInstance().getIDServizioFromValues(risultato.getString("tipoServizio"), risultato.getString("nomeServizio"),
  619.                             risultato.getString("tipoSoggetto"), risultato.getString("nomeSoggetto"),
  620.                             risultato.getInt("versioneServizio"));
  621.                    
  622.                     // trovata
  623.                     IDPortaApplicativa idPA = DBMappingUtils.getIDPortaApplicativaAssociataDefault(idS, con, tipoDB);
  624.                     if(idPA!=null) {
  625.                         ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaApplicativa(idPA.getNome(), tipoDB, con, normalizeObjectIds);
  626.                         modiSignalHubList.add(resultPorta.label);
  627.                     }
  628.                     else {
  629.                         modiSignalHubList.add(idS.toString());
  630.                     }
  631.                    
  632.                     isInUso = true;
  633.                    
  634.                 }
  635.                 risultato.close();
  636.                 stmt.close();
  637.                    
  638.             }
  639.            
  640.            
  641.             return isInUso;

  642.         } catch (Exception se) {
  643.             throw new UtilsException("[DBOggettiInUsoUtils::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  644.         } finally {
  645.             // Chiudo statement and resultset
  646.             JDBCUtilities.closeResources(risultato2, stmt2);
  647.             JDBCUtilities.closeResources(risultato, stmt);
  648.         }
  649.     }
  650.    
  651.     private static boolean checkServizioApplicativoRuoloInUsoInPorteDelegate(Connection con, String tipoDB, boolean normalizeObjectIds,
  652.             String nomeTabella,
  653.             List<String> listRuoliSA,
  654.             List<String> ruoliPDmappingList, List<String> ruoliPDlist) throws Exception {
  655.         boolean isInUso = false;
  656.         PreparedStatement stmt = null;
  657.         ResultSet risultato = null;
  658.         try {
  659.            
  660.             List<String> distinctPorteDelegate = new ArrayList<>();
  661.             List<String> verificaPorteDelegateAll = new ArrayList<>();
  662.            
  663.             for (String ruolo : listRuoliSA) {
  664.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  665.                 sqlQueryObject.addFromTable(nomeTabella);
  666.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE);
  667.                 sqlQueryObject.addSelectField("nome_porta");
  668.                 sqlQueryObject.addSelectField("ruoli_match");
  669.                 sqlQueryObject.setANDLogicOperator(true);
  670.                 sqlQueryObject.addWhereCondition(nomeTabella+".id_porta="+CostantiDB.PORTE_DELEGATE+".id");
  671.                 sqlQueryObject.addWhereCondition(nomeTabella+".ruolo=?");
  672.                 String queryString = sqlQueryObject.createSQLQuery();
  673.                 stmt = con.prepareStatement(queryString);
  674.                 stmt.setString(1, ruolo);
  675.                 risultato = stmt.executeQuery();
  676.                 while (risultato.next()){
  677.                     String nome = risultato.getString("nome_porta");
  678.                     String ruoloMatch = risultato.getString("ruoli_match");
  679.                     if(RuoloTipoMatch.ANY.getValue().equals(ruoloMatch)) {
  680.                         if(distinctPorteDelegate.contains(nome)) {
  681.                             continue;
  682.                         }
  683.                         else {
  684.                             distinctPorteDelegate.add(nome);
  685.                         }
  686.                         ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaDelegata(nome, tipoDB, con, normalizeObjectIds);
  687.                         if(resultPorta.mapping) {
  688.                             ruoliPDmappingList.add(resultPorta.label);
  689.                         }
  690.                         else {
  691.                             ruoliPDlist.add(resultPorta.label);
  692.                         }
  693.                         isInUso = true;
  694.                     }
  695.                     else {
  696.                         // devo verificare tutti i ruoli richiesti
  697.                         if(verificaPorteDelegateAll.contains(nome)) {
  698.                             continue;
  699.                         }
  700.                         else {
  701.                             verificaPorteDelegateAll.add(nome);
  702.                         }
  703.                     }
  704.                 }
  705.                 risultato.close();
  706.                 stmt.close();  
  707.             }
  708.            
  709.             // autorizzazione 'all'
  710.             if(!verificaPorteDelegateAll.isEmpty()) {
  711.                 for (String nome : verificaPorteDelegateAll) {
  712.                    
  713.                     // Raccolgo prima i ruoli della porta
  714.                     List<String> listRuoliPorta = new ArrayList<>();
  715.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  716.                     sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE);
  717.                     sqlQueryObject.addFromTable(nomeTabella);
  718.                     sqlQueryObject.addSelectField("ruolo");
  719.                     sqlQueryObject.setANDLogicOperator(true);
  720.                     sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE+".nome_porta=?");
  721.                     sqlQueryObject.addWhereCondition(nomeTabella+".id_porta="+CostantiDB.PORTE_DELEGATE+".id");
  722.                     String queryString = sqlQueryObject.createSQLQuery();
  723.                     stmt = con.prepareStatement(queryString);
  724.                     stmt.setString(1, nome);
  725.                     risultato = stmt.executeQuery();
  726.                     while (risultato.next()){
  727.                         String ruolo = risultato.getString("ruolo");
  728.                         listRuoliPorta.add(ruolo);
  729.                     }
  730.                     risultato.close();
  731.                     stmt.close();
  732.                    
  733.                     if(!listRuoliPorta.isEmpty()) {
  734.                         boolean match = true;
  735.                         for (String ruoloPorta : listRuoliPorta) {
  736.                             if(!listRuoliSA.contains(ruoloPorta)) {
  737.                                 match = false;
  738.                                 break;
  739.                             }
  740.                         }
  741.                         if(match) {
  742.                             if(distinctPorteDelegate.contains(nome)) {
  743.                                 continue;
  744.                             }
  745.                             else {
  746.                                 distinctPorteDelegate.add(nome);
  747.                             }
  748.                             ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaDelegata(nome, tipoDB, con, normalizeObjectIds);
  749.                             if(resultPorta.mapping) {
  750.                                 ruoliPDmappingList.add(resultPorta.label);
  751.                             }
  752.                             else {
  753.                                 ruoliPDlist.add(resultPorta.label);
  754.                             }
  755.                             isInUso = true;
  756.                         }
  757.                     }
  758.                    
  759.                 }
  760.             }
  761.            
  762.            
  763.            
  764.         } finally {
  765.             // Chiudo statement and resultset
  766.             try {
  767.                 if (risultato != null) {
  768.                     risultato.close();
  769.                 }
  770.             } catch (Exception e) {
  771.                 // ignore
  772.             }
  773.             try {
  774.                 if (stmt != null) {
  775.                     stmt.close();
  776.                 }
  777.             } catch (Exception e) {
  778.                 // ignore
  779.             }
  780.         }
  781.         return isInUso;
  782.     }
  783.    
  784.     private static boolean checkServizioApplicativoRuoloInUsoInPorteApplicative(Connection con, String tipoDB, boolean normalizeObjectIds,
  785.             String nomeTabella,
  786.             List<String> listRuoliSA,
  787.             List<String> ruoliPAmappingList, List<String> ruoliPAlist,
  788.             List<String> ruoliPAmappingModiList, List<String> ruoliPAmodiList) throws Exception {
  789.         boolean isInUso = false;
  790.         PreparedStatement stmt = null;
  791.         ResultSet risultato = null;
  792.         try {
  793.            
  794.             List<String> distinctPorteApplicative = new ArrayList<>();
  795.             List<String> verificaPorteApplicativeAll = new ArrayList<>();
  796.            
  797.             for (String ruolo : listRuoliSA) {
  798.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  799.                 sqlQueryObject.addFromTable(nomeTabella);
  800.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  801.                 sqlQueryObject.addSelectField("nome_porta");
  802.                 sqlQueryObject.addSelectField("ruoli_match");
  803.                 sqlQueryObject.setANDLogicOperator(true);
  804.                 sqlQueryObject.addWhereCondition(nomeTabella+".id_porta="+CostantiDB.PORTE_APPLICATIVE+".id");
  805.                 sqlQueryObject.addWhereCondition(nomeTabella+".ruolo=?");
  806.                 String queryString = sqlQueryObject.createSQLQuery();
  807.                 stmt = con.prepareStatement(queryString);
  808.                 stmt.setString(1, ruolo);
  809.                 risultato = stmt.executeQuery();
  810.                 while (risultato.next()){
  811.                     String nome = risultato.getString("nome_porta");
  812.                     String ruoloMatch = risultato.getString("ruoli_match");
  813.                     if(RuoloTipoMatch.ANY.getValue().equals(ruoloMatch)) {
  814.                         if(distinctPorteApplicative.contains(nome)) {
  815.                             continue;
  816.                         }
  817.                         else {
  818.                             distinctPorteApplicative.add(nome);
  819.                         }
  820.                         ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaApplicativa(nome, tipoDB, con, normalizeObjectIds);
  821.                         if(resultPorta.mapping) {
  822.                             if(resultPorta.erogazioneModi)
  823.                                 ruoliPAmappingModiList.add(resultPorta.label);
  824.                             else
  825.                                 ruoliPAmappingList.add(resultPorta.label);
  826.                         }
  827.                         else {
  828.                             if(resultPorta.erogazioneModi)
  829.                                 ruoliPAmodiList.add(resultPorta.label);
  830.                             else
  831.                                 ruoliPAlist.add(resultPorta.label);
  832.                         }
  833.                         isInUso = true;
  834.                     }
  835.                     else {
  836.                         // devo verificare tutti i ruoli richiesti
  837.                         if(verificaPorteApplicativeAll.contains(nome)) {
  838.                             continue;
  839.                         }
  840.                         else {
  841.                             verificaPorteApplicativeAll.add(nome);
  842.                         }
  843.                     }
  844.                 }
  845.                 risultato.close();
  846.                 stmt.close();  
  847.             }
  848.            
  849.             // autorizzazione 'all'
  850.             if(!verificaPorteApplicativeAll.isEmpty()) {
  851.                 for (String nome : verificaPorteApplicativeAll) {
  852.                    
  853.                     // Raccolgo prima i ruoli della porta
  854.                     List<String> listRuoliPorta = new ArrayList<>();
  855.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  856.                     sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  857.                     sqlQueryObject.addFromTable(nomeTabella);
  858.                     sqlQueryObject.addSelectField("ruolo");
  859.                     sqlQueryObject.setANDLogicOperator(true);
  860.                     sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE+".nome_porta=?");
  861.                     sqlQueryObject.addWhereCondition(nomeTabella+".id_porta="+CostantiDB.PORTE_APPLICATIVE+".id");
  862.                     String queryString = sqlQueryObject.createSQLQuery();
  863.                     stmt = con.prepareStatement(queryString);
  864.                     stmt.setString(1, nome);
  865.                     risultato = stmt.executeQuery();
  866.                     while (risultato.next()){
  867.                         String ruolo = risultato.getString("ruolo");
  868.                         listRuoliPorta.add(ruolo);
  869.                     }
  870.                     risultato.close();
  871.                     stmt.close();
  872.                    
  873.                     if(!listRuoliPorta.isEmpty()) {
  874.                         boolean match = true;
  875.                         for (String ruoloPorta : listRuoliPorta) {
  876.                             if(!listRuoliSA.contains(ruoloPorta)) {
  877.                                 match = false;
  878.                                 break;
  879.                             }
  880.                         }
  881.                         if(match) {
  882.                             if(distinctPorteApplicative.contains(nome)) {
  883.                                 continue;
  884.                             }
  885.                             else {
  886.                                 distinctPorteApplicative.add(nome);
  887.                             }
  888.                             ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaApplicativa(nome, tipoDB, con, normalizeObjectIds);
  889.                             if(resultPorta.mapping) {
  890.                                 if(resultPorta.erogazioneModi)
  891.                                     ruoliPAmappingModiList.add(resultPorta.label);
  892.                                 else
  893.                                     ruoliPAmappingList.add(resultPorta.label);
  894.                             }
  895.                             else {
  896.                                 if(resultPorta.erogazioneModi)
  897.                                     ruoliPAmodiList.add(resultPorta.label);
  898.                                 else
  899.                                     ruoliPAlist.add(resultPorta.label);
  900.                             }
  901.                             isInUso = true;
  902.                         }
  903.                     }
  904.                    
  905.                 }
  906.             }
  907.         } finally {
  908.             // Chiudo statement and resultset
  909.             try {
  910.                 if (risultato != null) {
  911.                     risultato.close();
  912.                 }
  913.             } catch (Exception e) {
  914.                 // ignore
  915.             }
  916.             try {
  917.                 if (stmt != null) {
  918.                     stmt.close();
  919.                 }
  920.             } catch (Exception e) {
  921.                 // ignore
  922.             }
  923.         }
  924.         return isInUso;
  925.     }

  926.     protected static String toString(IDServizioApplicativo idServizioApplicativo, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean prefix, String separator, boolean normalizeObjectIds){
  927.        
  928.         StringBuilder bf = new StringBuilder();
  929.         if(normalizeObjectIds) {
  930.             try {
  931.                 String protocollo = ProtocolFactoryManager.getInstance().getProtocolByOrganizationType(idServizioApplicativo.getIdSoggettoProprietario().getTipo());
  932.                 String labelSA = DBOggettiInUsoUtils.getProtocolPrefix(protocollo)+idServizioApplicativo.getNome()+DBOggettiInUsoUtils.getSubjectSuffix(protocollo, idServizioApplicativo.getIdSoggettoProprietario());
  933.                 bf.append(labelSA);
  934.             }catch(Exception e) {
  935.                 bf.append(idServizioApplicativo.getIdSoggettoProprietario().toString()+"_"+idServizioApplicativo.getNome());
  936.             }
  937.         }
  938.         else {
  939.             bf.append(idServizioApplicativo.getIdSoggettoProprietario().toString()+"_"+idServizioApplicativo.getNome());
  940.         }
  941.        
  942.         Set<ErrorsHandlerCostant> keys = whereIsInUso.keySet();
  943.         StringBuilder msgBuilder = new StringBuilder("Applicativo '" +bf.toString() +"' non eliminabile perch&egrave; :"+separator);
  944.         if(!prefix){
  945.             msgBuilder = new StringBuilder("");
  946.         }
  947.         String separatorCategorie = "";
  948.         if(whereIsInUso.size()>1) {
  949.             separatorCategorie = separator;
  950.         }
  951.         for (ErrorsHandlerCostant key : keys) {
  952.             List<String> messages = whereIsInUso.get(key);

  953.             if ( messages!=null && !messages.isEmpty()) {
  954.                 msgBuilder.append(separatorCategorie);
  955.             }
  956.            
  957.             switch (key) {
  958.             case AUTORIZZAZIONE_MAPPING:
  959.                 if ( messages!=null && !messages.isEmpty()) {
  960.                     msgBuilder.append("utilizzato nel Controllo degli Accessi (Autorizzazione Trasporto - Richiedenti Autorizzati) delle Fruizioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  961.                 }
  962.                 break;
  963.             case AUTORIZZAZIONE:
  964.                 if ( messages!=null && !messages.isEmpty()) {
  965.                     msgBuilder.append("utilizzato nelle Porte Outbound (Controllo degli Accessi - Autorizzazione Trasporto -  Richiedenti Autorizzati): " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  966.                 }
  967.                 break;
  968.             case AUTORIZZAZIONE_MAPPING_PA:
  969.                 if ( messages!=null && !messages.isEmpty()) {
  970.                     msgBuilder.append("utilizzato nel Controllo degli Accessi (Autorizzazione Trasporto - Richiedenti Autorizzati) delle Erogazioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  971.                 }
  972.                 break;
  973.             case AUTORIZZAZIONE_PA:
  974.                 if ( messages!=null && !messages.isEmpty()) {
  975.                     msgBuilder.append("utilizzato nelle Porte Inbound (Controllo degli Accessi - Autorizzazione Trasporto - Richiedenti Autorizzati): " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  976.                 }
  977.                 break;
  978.             case AUTORIZZAZIONE_MAPPING_PA_MODI:
  979.                 if ( messages!=null && !messages.isEmpty()) {
  980.                     msgBuilder.append("utilizzato nel Controllo degli Accessi (Autorizzazione Messaggio - Richiedenti Autorizzati) delle Erogazioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  981.                 }
  982.                 break;
  983.             case AUTORIZZAZIONE_PA_MODI:
  984.                 if ( messages!=null && !messages.isEmpty()) {
  985.                     msgBuilder.append("utilizzato nelle Porte Inbound (Controllo degli Accessi - Autorizzazione Messaggio - Richiedenti Autorizzati): " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  986.                 }
  987.                 break;
  988.                
  989.             case AUTORIZZAZIONE_TOKEN_MAPPING_PD:
  990.                 if ( messages!=null && !messages.isEmpty()) {
  991.                     msgBuilder.append("utilizzato nel Controllo degli Accessi (Autorizzazione Token - Richiedenti Autorizzati) delle Fruizioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  992.                 }
  993.                 break;
  994.             case AUTORIZZAZIONE_TOKEN_PD:
  995.                 if ( messages!=null && !messages.isEmpty()) {
  996.                     msgBuilder.append("utilizzato nelle Porte Outbound (Controllo degli Accessi - Autorizzazione Token - Richiedenti Autorizzati): " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  997.                 }
  998.                 break;
  999.             case AUTORIZZAZIONE_TOKEN_MAPPING_PA:
  1000.                 if ( messages!=null && !messages.isEmpty()) {
  1001.                     msgBuilder.append("utilizzato nel Controllo degli Accessi (Autorizzazione Token - Richiedenti Autorizzati) delle Erogazioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1002.                 }
  1003.                 break;
  1004.             case AUTORIZZAZIONE_TOKEN_PA:
  1005.                 if ( messages!=null && !messages.isEmpty()) {
  1006.                     msgBuilder.append("utilizzato nelle Porte Inbound (Controllo degli Accessi - Autorizzazione Token - Richiedenti Autorizzati): " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1007.                 }
  1008.                 break;
  1009.                
  1010.             case RUOLI_MAPPING:
  1011.                 if ( messages!=null && !messages.isEmpty()) {
  1012.                     msgBuilder.append("compatibile con l'Autorizzazione Trasporto per Ruoli indicata nel Controllo degli Accessi delle Fruizioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1013.                 }
  1014.                 break;
  1015.             case RUOLI:
  1016.                 if ( messages!=null && !messages.isEmpty()) {
  1017.                     msgBuilder.append("compatibile con l'Autorizzazione Trasporto per Ruoli indicata nel Controllo degli Accessi delle Porte Outbound: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1018.                 }
  1019.                 break;
  1020.             case RUOLI_MAPPING_PA:
  1021.                 if ( messages!=null && !messages.isEmpty()) {
  1022.                     msgBuilder.append("compatibile con l'Autorizzazione Trasporto per Ruoli indicata nel Controllo degli Accessi delle Erogazioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1023.                 }
  1024.                 break;
  1025.             case RUOLI_PA:
  1026.                 if ( messages!=null && !messages.isEmpty()) {
  1027.                     msgBuilder.append("compatibile con l'Autorizzazione Trasporto per Ruoli indicata nel Controllo degli Accessi delle Porte Inbound: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1028.                 }
  1029.                 break;
  1030.                
  1031.             case RUOLI_TOKEN_MAPPING_PD:
  1032.                 if ( messages!=null && !messages.isEmpty()) {
  1033.                     msgBuilder.append("compatibile con l'Autorizzazione Token per Ruoli indicata nel Controllo degli Accessi delle Fruizioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1034.                 }
  1035.                 break;
  1036.             case RUOLI_TOKEN_PD:
  1037.                 if ( messages!=null && !messages.isEmpty()) {
  1038.                     msgBuilder.append("compatibile con l'Autorizzazione Token per Ruoli indicata nel Controllo degli Accessi delle Porte Outbound: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1039.                 }
  1040.                 break;
  1041.             case RUOLI_TOKEN_MAPPING_PA:
  1042.                 if ( messages!=null && !messages.isEmpty()) {
  1043.                     msgBuilder.append("compatibile con l'Autorizzazione Token per Ruoli indicata nel Controllo degli Accessi delle Erogazioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1044.                 }
  1045.                 break;
  1046.             case RUOLI_TOKEN_PA:
  1047.                 if ( messages!=null && !messages.isEmpty()) {
  1048.                     msgBuilder.append("compatibile con l'Autorizzazione Token per Ruoli indicata nel Controllo degli Accessi delle Porte Inbound: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1049.                 }
  1050.                 break;
  1051.             case RUOLI_TOKEN_MAPPING_PA_MODI:
  1052.                 if ( messages!=null && !messages.isEmpty()) {
  1053.                     msgBuilder.append("compatibile con l'Autorizzazione Messaggio per Ruoli indicata nel Controllo degli Accessi delle Erogazioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1054.                 }
  1055.                 break;
  1056.             case RUOLI_TOKEN_PA_MODI:
  1057.                 if ( messages!=null && !messages.isEmpty()) {
  1058.                     msgBuilder.append("compatibile con l'Autorizzazione Messaggio per Ruoli indicata nel Controllo degli Accessi delle Porte Inbound: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1059.                 }
  1060.                 break;
  1061.                
  1062.             case IN_USO_IN_MAPPING_EROGAZIONE_PA:
  1063.                 if ( messages!=null && !messages.isEmpty()) {
  1064.                     msgBuilder.append("utilizzato come applicativo server nei connettori delle Erogazioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1065.                 }
  1066.                 break;
  1067.             case IN_USO_IN_PORTE_APPLICATIVE:
  1068.                 if ( messages!=null && !messages.isEmpty()) {
  1069.                     msgBuilder.append("in uso in Porte Inbound: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1070.                 }
  1071.                 break;
  1072.             case CONTROLLO_TRAFFICO:
  1073.                 if ( messages!=null && !messages.isEmpty()) {
  1074.                     msgBuilder.append("utilizzato in policy di Rate Limiting: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1075.                 }
  1076.                 break;
  1077.             case ALLARMI:
  1078.                 if ( messages!=null && !messages.isEmpty()) {
  1079.                     msgBuilder.append("utilizzato in Allarmi: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1080.                 }
  1081.                 break;
  1082.             case TRASFORMAZIONE_MAPPING_PD:
  1083.                 if ( messages!=null && !messages.isEmpty()) {
  1084.                     msgBuilder.append("utilizzato nel criterio di applicabilità della Trasformazione (Applicativi) per le Fruizioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1085.                 }
  1086.                 break;
  1087.             case TRASFORMAZIONE_PD:
  1088.                 if ( messages!=null && !messages.isEmpty()) {
  1089.                     msgBuilder.append("utilizzato nelle Porte Outbound (Criterio di applicabilità della Trasformazione - Applicativi): " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1090.                 }
  1091.                 break;
  1092.             case TRASFORMAZIONE_MAPPING_PA:
  1093.                 if ( messages!=null && !messages.isEmpty()) {
  1094.                     msgBuilder.append("utilizzato nel criterio di applicabilità della Trasformazione (Applicativi) per le Erogazioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1095.                 }
  1096.                 break;
  1097.             case TRASFORMAZIONE_PA:
  1098.                 if ( messages!=null && !messages.isEmpty()) {
  1099.                     msgBuilder.append("utilizzato nelle Porte Inbound (Criterio di applicabilità della Trasformazione - Applicativi): " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1100.                 }
  1101.                 break;
  1102.                
  1103.             case IS_RIFERITA_MODI_SIGNAL_HUB:
  1104.                 if ( messages!=null && !messages.isEmpty() ) {
  1105.                     msgBuilder.append("riferito nella configurazione 'SignalHub' dell'erogazione: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator);
  1106.                 }
  1107.                 break;              
  1108.                
  1109.             default:
  1110.                 msgBuilder.append("utilizzato in oggetto non codificato ("+key+")"+separator);
  1111.                 break;
  1112.             }

  1113.         }// chiudo for

  1114.         return msgBuilder.toString();
  1115.     }
  1116.    
  1117. }