DBOggettiInUsoUtils_scope.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.constants.CostantiDB;
  30. import org.openspcoop2.core.id.IDScope;
  31. import org.openspcoop2.utils.UtilsException;
  32. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  33. import org.openspcoop2.utils.sql.ISQLQueryObject;
  34. import org.openspcoop2.utils.sql.SQLObjectFactory;

  35. /**
  36.  * DBOggettiInUsoUtils_scope
  37.  *
  38.  * @author Andrea Poli (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  *
  42.  */
  43. public class DBOggettiInUsoUtils_scope {

  44.     protected static boolean isScopeConfigInUso(Connection con, String tipoDB, IDScope idScope, Map<ErrorsHandlerCostant,
  45.             List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  46.         return _isScopeInUso(con,tipoDB,idScope,false,true,whereIsInUso,normalizeObjectIds);
  47.     }
  48. //  protected static boolean isScopeRegistryInUso(Connection con, String tipoDB, IDScope idScope, Map<ErrorsHandlerCostant,
  49. //          List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  50. //      return _isScopeInUso(con,tipoDB,idScope,true,false,whereIsInUso,normalizeObjectIds);
  51. //  }
  52.     protected static boolean isScopeInUso(Connection con, String tipoDB, IDScope idScope, Map<ErrorsHandlerCostant,
  53.             List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  54.         return _isScopeInUso(con,tipoDB,idScope,true,true,whereIsInUso,normalizeObjectIds);
  55.     }
  56.     private static boolean _isScopeInUso(Connection con, String tipoDB, IDScope idScope, boolean registry, boolean config,
  57.             Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  58.         String nomeMetodo = "_isScopeInUso";

  59.         PreparedStatement stmt = null;
  60.         ResultSet risultato = null;
  61.         String queryString;

  62.         try {

  63.             //long idS = DBUtils.getIdScope(idScope, con, tipoDB);
  64.            
  65.             boolean isInUso = false;
  66.            
  67.             List<String> porte_applicative_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_PORTE_APPLICATIVE);
  68.             List<String> porte_delegate_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_PORTE_DELEGATE);
  69.             List<String> mappingErogazionePA_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_MAPPING_EROGAZIONE_PA);
  70.             List<String> mappingFruizionePD_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_MAPPING_FRUIZIONE_PD);
  71.            
  72.             if (porte_applicative_list == null) {
  73.                 porte_applicative_list = new ArrayList<>();
  74.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_PORTE_APPLICATIVE, porte_applicative_list);
  75.             }
  76.             if (porte_delegate_list == null) {
  77.                 porte_delegate_list = new ArrayList<>();
  78.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_PORTE_DELEGATE, porte_delegate_list);
  79.             }
  80.             if (mappingErogazionePA_list == null) {
  81.                 mappingErogazionePA_list = new ArrayList<>();
  82.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_MAPPING_EROGAZIONE_PA, mappingErogazionePA_list);
  83.             }
  84.             if (mappingFruizionePD_list == null) {
  85.                 mappingFruizionePD_list = new ArrayList<>();
  86.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_MAPPING_FRUIZIONE_PD, mappingFruizionePD_list);
  87.             }
  88.            

  89.             // Controllo che il scope non sia in uso nelle porte applicative
  90.             if(config){
  91.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  92.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE_SCOPE);
  93.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  94.                 sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  95.                 sqlQueryObject.addSelectField(CostantiDB.PORTE_APPLICATIVE+".nome_porta");
  96.                 sqlQueryObject.setANDLogicOperator(true);
  97.                 sqlQueryObject.setSelectDistinct(true);
  98.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE_SCOPE+".scope = ?");
  99.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE_SCOPE+".id_porta = "+CostantiDB.PORTE_APPLICATIVE+".id");
  100.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE+".id_soggetto = "+CostantiDB.SOGGETTI+".id");
  101.                 queryString = sqlQueryObject.createSQLQuery();
  102.                 stmt = con.prepareStatement(queryString);
  103.                 stmt.setString(1, idScope.getNome());
  104.                 risultato = stmt.executeQuery();
  105.                 while (risultato.next()) {
  106.                     String nome = risultato.getString("nome_porta");
  107.                     ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaApplicativa(nome, tipoDB, con, normalizeObjectIds);
  108.                     if(resultPorta.mapping) {
  109.                         mappingErogazionePA_list.add(resultPorta.label);
  110.                     }
  111.                     else {
  112.                         porte_applicative_list.add(resultPorta.label);
  113.                     }
  114.                     isInUso = true;
  115.                 }
  116.                 risultato.close();
  117.                 stmt.close();
  118.             }
  119.            
  120.             // Controllo che il scope non sia in uso nelle porte applicative
  121.             if(config){
  122.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  123.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE_SCOPE);
  124.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE);
  125.                 sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  126.                 sqlQueryObject.addSelectField(CostantiDB.PORTE_DELEGATE+".nome_porta");
  127.                 sqlQueryObject.setANDLogicOperator(true);
  128.                 sqlQueryObject.setSelectDistinct(true);
  129.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE_SCOPE+".scope = ?");
  130.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE_SCOPE+".id_porta = "+CostantiDB.PORTE_DELEGATE+".id");
  131.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE+".id_soggetto = "+CostantiDB.SOGGETTI+".id");
  132.                 queryString = sqlQueryObject.createSQLQuery();
  133.                 stmt = con.prepareStatement(queryString);
  134.                 stmt.setString(1, idScope.getNome());
  135.                 risultato = stmt.executeQuery();
  136.                 while (risultato.next()) {
  137.                     String nome = risultato.getString("nome_porta");
  138.                     ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaDelegata(nome, tipoDB, con, normalizeObjectIds);
  139.                     if(resultPorta.mapping) {
  140.                         mappingFruizionePD_list.add(resultPorta.label);
  141.                     }
  142.                     else {
  143.                         porte_delegate_list.add(resultPorta.label);
  144.                     }
  145.                     isInUso = true;
  146.                 }
  147.                 risultato.close();
  148.                 stmt.close();
  149.             }
  150.            
  151.             return isInUso;

  152.         } catch (Exception se) {
  153.             throw new UtilsException("[DBOggettiInUsoUtils::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  154.         } finally {
  155.             // Chiudo statement and resultset
  156.             JDBCUtilities.closeResources(risultato, stmt);
  157.         }
  158.     }


  159.     protected static String toString(IDScope idScope, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean prefix, String separator){
  160.         return toString(idScope, whereIsInUso, prefix, separator," non eliminabile perch&egrave; :");
  161.     }
  162.     protected static String toString(IDScope idScope, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean prefix, String separator, String intestazione){
  163.         Set<ErrorsHandlerCostant> keys = whereIsInUso.keySet();
  164.         String msg = "Scope '"+ idScope.getNome() + "'" + intestazione+separator;
  165.         if(prefix==false){
  166.             msg = "";
  167.         }
  168.         String separatorCategorie = "";
  169.         if(whereIsInUso.size()>1) {
  170.             separatorCategorie = separator;
  171.         }
  172.         for (ErrorsHandlerCostant key : keys) {
  173.             List<String> messages = whereIsInUso.get(key);

  174.             if ( messages!=null && messages.size() > 0) {
  175.                 msg += separatorCategorie;
  176.             }
  177.            
  178.             switch (key) {

  179.             case IN_USO_IN_PORTE_APPLICATIVE:
  180.                 if ( messages!=null && messages.size() > 0) {
  181.                     msg += "utilizzato nelle Porte Inbound (Controllo Accessi - Autorizzazione Token): " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  182.                 }
  183.                 break;
  184.             case IN_USO_IN_PORTE_DELEGATE:
  185.                 if ( messages!=null && messages.size() > 0) {
  186.                     msg += "utilizzato nelle Porte Outbound (Controllo Accessi - Autorizzazione Token): " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  187.                 }
  188.                 break;
  189.             case IN_USO_IN_MAPPING_EROGAZIONE_PA:
  190.                 if ( messages!=null && messages.size() > 0) {
  191.                     msg += "utilizzato nelle Erogazioni (Controllo Accessi - Autorizzazione Token): " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  192.                 }
  193.                 break;
  194.             case IN_USO_IN_MAPPING_FRUIZIONE_PD:
  195.                 if ( messages!=null && messages.size() > 0) {
  196.                     msg += "utilizzato nelle Fruizioni (Controllo Accessi - Autorizzazione Token): " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  197.                 }
  198.                 break;
  199.                
  200.             default:
  201.                 msg += "utilizzato in oggetto non codificato ("+key+")"+separator;
  202.                 break;
  203.             }

  204.         }// chiudo for

  205.         return msg;
  206.     }
  207.    
  208. }