DBOggettiInUsoUtils_canali.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.ErrorsHandlerCostant;
  29. import org.openspcoop2.core.config.CanaleConfigurazione;
  30. import org.openspcoop2.core.constants.CostantiDB;
  31. import org.openspcoop2.core.id.IDAccordo;
  32. import org.openspcoop2.core.id.IDSoggetto;
  33. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  34. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  35. import org.openspcoop2.utils.UtilsException;
  36. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  37. import org.openspcoop2.utils.sql.ISQLQueryObject;
  38. import org.openspcoop2.utils.sql.LikeConfig;
  39. import org.openspcoop2.utils.sql.SQLObjectFactory;

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

  49.     protected static boolean isCanaleInUsoRegistro(Connection con, String tipoDB, CanaleConfigurazione canale, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  50.         return isCanaleInUso(con, tipoDB, canale, whereIsInUso, normalizeObjectIds, true, true, false);
  51.     }
  52.     protected static boolean isCanaleInUso(Connection con, String tipoDB, CanaleConfigurazione canale, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  53.         return isCanaleInUso(con, tipoDB, canale, whereIsInUso, normalizeObjectIds, true, true, true);
  54.     }
  55.     protected static boolean isCanaleInUso(Connection con, String tipoDB, CanaleConfigurazione canale, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean normalizeObjectIds, boolean registry, boolean config, boolean nodi) throws UtilsException {
  56.         String nomeMetodo = "_isCanaleInUso";

  57.         PreparedStatement stmt = null;
  58.         ResultSet risultato = null;
  59.         PreparedStatement stmt2 = null;
  60.         ResultSet risultato2 = null;
  61.         String queryString;

  62.         try {
  63.             String nomeCanale = canale.getNome();
  64.            
  65.             boolean isInUso = false;
  66.            
  67.             List<String> accordi_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_ACCORDI);
  68.             List<String> porte_applicative_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_PORTE_APPLICATIVE);
  69.             List<String> porte_delegate_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_PORTE_DELEGATE);
  70.             List<String> mappingErogazionePA_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_MAPPING_EROGAZIONE_PA);
  71.             List<String> mappingFruizionePD_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_MAPPING_FRUIZIONE_PD);
  72.             List<String> nodi_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_CANALE_NODO);
  73.            
  74.             if (accordi_list == null) {
  75.                 accordi_list = new ArrayList<>();
  76.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_ACCORDI, accordi_list);
  77.             }
  78.             if (porte_applicative_list == null) {
  79.                 porte_applicative_list = new ArrayList<>();
  80.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_PORTE_APPLICATIVE, porte_applicative_list);
  81.             }
  82.             if (porte_delegate_list == null) {
  83.                 porte_delegate_list = new ArrayList<>();
  84.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_PORTE_DELEGATE, porte_delegate_list);
  85.             }
  86.             if (mappingErogazionePA_list == null) {
  87.                 mappingErogazionePA_list = new ArrayList<>();
  88.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_MAPPING_EROGAZIONE_PA, mappingErogazionePA_list);
  89.             }
  90.             if (mappingFruizionePD_list == null) {
  91.                 mappingFruizionePD_list = new ArrayList<>();
  92.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_MAPPING_FRUIZIONE_PD, mappingFruizionePD_list);
  93.             }
  94.             if (nodi_list == null) {
  95.                 nodi_list = new ArrayList<>();
  96.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_CANALE_NODO, nodi_list);
  97.             }
  98.            
  99.             // Controllo che il gruppo non sia in uso negli accordi
  100.             if(registry) {
  101.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  102.                 sqlQueryObject.addFromTable(CostantiDB.ACCORDI);
  103.                 sqlQueryObject.addSelectField("*");
  104.                 sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI+".canale = ?");
  105.                 sqlQueryObject.setANDLogicOperator(true);
  106.                 queryString = sqlQueryObject.createSQLQuery();
  107.                 stmt = con.prepareStatement(queryString);
  108.                 stmt.setString(1, nomeCanale);
  109.                 risultato = stmt.executeQuery();
  110.                 while (risultato.next()){
  111.                     isInUso=true;
  112.                    
  113.                     String nomeAccordo = risultato.getString("nome");
  114.                     int versione = risultato.getInt("versione");
  115.                     long idReferente = risultato.getLong("id_referente");
  116.                     IDSoggetto idReferenteObject = null;
  117.                    
  118.                     if(idReferente>0){
  119.    
  120.                         ISQLQueryObject sqlQueryObjectReferente = SQLObjectFactory.createSQLQueryObject(tipoDB);
  121.                         sqlQueryObjectReferente.addFromTable(CostantiDB.SOGGETTI);
  122.                         sqlQueryObjectReferente.addSelectField("*");
  123.                         sqlQueryObjectReferente.addWhereCondition("id=?");
  124.                         sqlQueryObjectReferente.setANDLogicOperator(true);
  125.                         String queryStringReferente = sqlQueryObjectReferente.createSQLQuery();
  126.                         stmt2 = con.prepareStatement(queryStringReferente);
  127.                         stmt2.setLong(1, idReferente);
  128.                         risultato2 = stmt2.executeQuery();
  129.                         if(risultato2.next()){
  130.                             idReferenteObject = new IDSoggetto();
  131.                             idReferenteObject.setTipo(risultato2.getString("tipo_soggetto"));
  132.                             idReferenteObject.setNome(risultato2.getString("nome_soggetto"));
  133.                         }
  134.                         risultato2.close(); risultato2=null;
  135.                         stmt2.close(); stmt2=null;
  136.    
  137.                     }
  138.                    
  139.                     if(normalizeObjectIds && idReferenteObject!=null) {
  140.                         String protocollo = ProtocolFactoryManager.getInstance().getProtocolByOrganizationType(idReferenteObject.getTipo());
  141.                         IDAccordo idAccordo = IDAccordoFactory.getInstance().getIDAccordoFromValues(nomeAccordo, idReferenteObject, versione);
  142.                         accordi_list.add(DBOggettiInUsoUtils.getProtocolPrefix(protocollo)+NamingUtils.getLabelAccordoServizioParteComune(protocollo, idAccordo));
  143.                     }
  144.                     else {
  145.    
  146.                         StringBuilder bf = new StringBuilder();
  147.    
  148.                         bf.append(idReferenteObject!=null ? idReferenteObject.getTipo() : "?");
  149.                         bf.append("/");
  150.                         bf.append(idReferenteObject!=null ? idReferenteObject.getNome() : "?");
  151.                         bf.append(":");
  152.                        
  153.                         bf.append(nomeAccordo);
  154.        
  155.                         if(idReferente>0){
  156.                             bf.append(":");
  157.                             bf.append(versione);
  158.                         }
  159.        
  160.                         accordi_list.add(bf.toString());
  161.                     }
  162.                    
  163.                 }
  164.                 risultato.close();
  165.                 stmt.close();
  166.             }
  167.            
  168.             // Controllo che il canale non sia in uso nelle porte applicative
  169.             if(config){
  170.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  171.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  172.                 sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  173.                 sqlQueryObject.addSelectField(CostantiDB.PORTE_APPLICATIVE+".nome_porta");
  174.                 sqlQueryObject.setANDLogicOperator(true);
  175.                 sqlQueryObject.setSelectDistinct(true);
  176.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE+".canale = ?");
  177.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE+".id_soggetto = "+CostantiDB.SOGGETTI+".id");
  178.                 queryString = sqlQueryObject.createSQLQuery();
  179.                 stmt = con.prepareStatement(queryString);
  180.                 stmt.setString(1, nomeCanale);
  181.                 risultato = stmt.executeQuery();
  182.                 while (risultato.next()) {
  183.                     String nome = risultato.getString("nome_porta");
  184.                     ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaApplicativa(nome, tipoDB, con, normalizeObjectIds);
  185.                     if(resultPorta.mapping) {
  186.                         mappingErogazionePA_list.add(resultPorta.label);
  187.                     }
  188.                     else {
  189.                         porte_applicative_list.add(resultPorta.label);
  190.                     }
  191.                     isInUso = true;
  192.                 }
  193.                 risultato.close();
  194.                 stmt.close();
  195.             }
  196.            
  197.             // Controllo che il canale non sia in uso nelle porte applicative
  198.             if(config){
  199.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  200.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE);
  201.                 sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  202.                 sqlQueryObject.addSelectField(CostantiDB.PORTE_DELEGATE+".nome_porta");
  203.                 sqlQueryObject.setANDLogicOperator(true);
  204.                 sqlQueryObject.setSelectDistinct(true);
  205.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE+".canale = ?");
  206.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE+".id_soggetto = "+CostantiDB.SOGGETTI+".id");
  207.                 queryString = sqlQueryObject.createSQLQuery();
  208.                 stmt = con.prepareStatement(queryString);
  209.                 stmt.setString(1, nomeCanale);
  210.                 risultato = stmt.executeQuery();
  211.                 while (risultato.next()) {
  212.                     String nome = risultato.getString("nome_porta");
  213.                     ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaDelegata(nome, tipoDB, con, normalizeObjectIds);
  214.                     if(resultPorta.mapping) {
  215.                         mappingFruizionePD_list.add(resultPorta.label);
  216.                     }
  217.                     else {
  218.                         porte_delegate_list.add(resultPorta.label);
  219.                     }
  220.                     isInUso = true;
  221.                 }
  222.                 risultato.close();
  223.                 stmt.close();
  224.             }
  225.            
  226.             // Controllo che il canale non sia in uso in un nodo
  227.             if(nodi){
  228.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  229.                 sqlQueryObject.addFromTable(CostantiDB.CONFIGURAZIONE_CANALI_NODI);
  230.                 sqlQueryObject.addSelectField(CostantiDB.CONFIGURAZIONE_CANALI_NODI+".nome");
  231.                 sqlQueryObject.setANDLogicOperator(false);
  232.                 sqlQueryObject.setSelectDistinct(true);
  233.                 // condizione di controllo
  234.                 // (canali == 'NOME') OR (canali like 'CANALE,%') OR (canali like '%,CANALE') OR (canali like '%,CANALE,%')
  235.                 sqlQueryObject.addWhereCondition(CostantiDB.CONFIGURAZIONE_CANALI_NODI+".canali = ?");
  236.                 // fix: devo generare io il % e non farlo fare a sql query object poiche' mi serve lo start e l'end
  237.                 sqlQueryObject.addWhereLikeCondition(CostantiDB.CONFIGURAZIONE_CANALI_NODI+".canali", nomeCanale+",", LikeConfig.startsWith(false));
  238.                 sqlQueryObject.addWhereLikeCondition(CostantiDB.CONFIGURAZIONE_CANALI_NODI+".canali", ","+nomeCanale, LikeConfig.endsWith(false));
  239.                 sqlQueryObject.addWhereLikeCondition(CostantiDB.CONFIGURAZIONE_CANALI_NODI+".canali", ","+nomeCanale+",", true , false);
  240.                 queryString = sqlQueryObject.createSQLQuery();
  241.                 stmt = con.prepareStatement(queryString);
  242.                 stmt.setString(1, nomeCanale);
  243.                 risultato = stmt.executeQuery();
  244.                 while (risultato.next()) {
  245.                     String nome = risultato.getString("nome");
  246.                     nodi_list.add(nome);
  247.                     isInUso = true;
  248.                 }
  249.                 risultato.close();
  250.                 stmt.close();
  251.             }

  252.             return isInUso;

  253.         } catch (Exception se) {
  254.             throw new UtilsException("[DBOggettiInUsoUtils::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  255.         } finally {
  256.             // Chiudo statement and resultset
  257.             JDBCUtilities.closeResources(risultato2, stmt2);
  258.             JDBCUtilities.closeResources(risultato, stmt);
  259.         }
  260.     }

  261.     protected static String toString(CanaleConfigurazione canale, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean prefix, String separator){
  262.         return toString(canale, whereIsInUso, prefix, separator," non eliminabile perch&egrave; :");
  263.     }
  264.     protected static String toString(CanaleConfigurazione canale, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean prefix, String separator, String intestazione){
  265.         Set<ErrorsHandlerCostant> keys = whereIsInUso.keySet();
  266.         String msg = "Canale '"+canale.getNome()+"'" + intestazione+separator;
  267.         if(prefix==false){
  268.             msg = "";
  269.         }
  270.         String separatorCategorie = "";
  271.         if(whereIsInUso.size()>1) {
  272.             separatorCategorie = separator;
  273.         }
  274.         for (ErrorsHandlerCostant key : keys) {
  275.             List<String> messages = whereIsInUso.get(key);

  276.             if ( messages!=null && messages.size() > 0) {
  277.                 msg += separatorCategorie;
  278.             }
  279.            
  280.             switch (key) {
  281.             case IN_USO_IN_ACCORDI:
  282.                 if ( messages!=null && messages.size() > 0 ) {
  283.                     msg += "associato all'API: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  284.                 }
  285.                 break;
  286.             case IN_USO_IN_MAPPING_EROGAZIONE_PA:
  287.                 if ( messages!=null && messages.size() > 0) {
  288.                     msg += "utilizzato nelle Erogazioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  289.                 }
  290.                 break;
  291.             case IN_USO_IN_MAPPING_FRUIZIONE_PD:
  292.                 if ( messages!=null && messages.size() > 0) {
  293.                     msg += "utilizzato nelle Fruizioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  294.                 }
  295.                 break;
  296.             case IN_USO_IN_PORTE_DELEGATE:
  297.                 if ( messages!=null && messages.size() > 0 ) {
  298.                     msg += "in uso in Porte Delegate: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  299.                 }
  300.                 break;
  301.             case IN_USO_IN_PORTE_APPLICATIVE:
  302.                 if ( messages!=null && messages.size() > 0 ) {
  303.                     msg += "in uso in Porte Applicative: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  304.                 }
  305.                 break;
  306.             case IN_USO_IN_CANALE_NODO:
  307.                 if ( messages!=null && messages.size() > 0) {
  308.                     msg += "utilizzato nei Nodi: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  309.                 }
  310.                 break;
  311.             default:
  312.                 msg += "utilizzato in oggetto non codificato ("+key+")"+separator;
  313.                 break;
  314.             }

  315.         }// chiudo for

  316.         return msg;
  317.     }
  318. }