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

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

  49.     // Lascio i metodi se servissero in futuro
  50.     protected static boolean isGruppoConfigInUso(Connection con, String tipoDB, IDGruppo idGruppo, Map<ErrorsHandlerCostant,
  51.             List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  52.         return _isGruppoInUso(con,tipoDB,idGruppo,false,true,whereIsInUso,normalizeObjectIds);
  53.     }
  54.     protected static boolean isGruppoRegistryInUso(Connection con, String tipoDB, IDGruppo idGruppo, Map<ErrorsHandlerCostant,
  55.             List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  56.         return _isGruppoInUso(con,tipoDB,idGruppo,true,false,whereIsInUso,normalizeObjectIds);
  57.     }
  58.     protected static boolean isGruppoInUso(Connection con, String tipoDB, IDGruppo idGruppo, Map<ErrorsHandlerCostant,
  59.             List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  60.         return _isGruppoInUso(con,tipoDB,idGruppo,true,true,whereIsInUso,normalizeObjectIds);
  61.     }
  62.     private static boolean _isGruppoInUso(Connection con, String tipoDB, IDGruppo idGruppo, boolean registry, boolean config, Map<ErrorsHandlerCostant,
  63.             List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  64.         String nomeMetodo = "_isGruppoInUso";

  65.         PreparedStatement stmt = null;
  66.         ResultSet risultato = null;
  67.         PreparedStatement stmt2 = null;
  68.         ResultSet risultato2 = null;
  69.         String queryString;

  70.         try {

  71.             long idG = DBUtils.getIdGruppo(idGruppo, con, tipoDB);

  72.             boolean isInUso = false;

  73.             List<String> accordi_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_ACCORDI);
  74.             List<String> ct_list = whereIsInUso.get(ErrorsHandlerCostant.CONTROLLO_TRAFFICO);
  75.             List<String> allarme_list = whereIsInUso.get(ErrorsHandlerCostant.ALLARMI);

  76.             if (accordi_list == null) {
  77.                 accordi_list = new ArrayList<>();
  78.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_ACCORDI, accordi_list);
  79.             }
  80.             if (ct_list == null) {
  81.                 ct_list = new ArrayList<>();
  82.                 whereIsInUso.put(ErrorsHandlerCostant.CONTROLLO_TRAFFICO, ct_list);
  83.             }
  84.             if (allarme_list == null) {
  85.                 allarme_list = new ArrayList<>();
  86.                 whereIsInUso.put(ErrorsHandlerCostant.ALLARMI, allarme_list);
  87.             }


  88.             // Controllo che il gruppo non sia in uso negli accordi
  89.             if(registry){

  90.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  91.                 sqlQueryObject.addFromTable(CostantiDB.ACCORDI_GRUPPI);
  92.                 sqlQueryObject.addFromTable(CostantiDB.ACCORDI);
  93.                 sqlQueryObject.addSelectField("*");
  94.                 sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_GRUPPI+".id_gruppo = ?");
  95.                 sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_GRUPPI+".id_accordo = "+CostantiDB.ACCORDI+".id");
  96.                 sqlQueryObject.setANDLogicOperator(true);
  97.                 queryString = sqlQueryObject.createSQLQuery();
  98.                 stmt = con.prepareStatement(queryString);
  99.                 stmt.setLong(1, idG);
  100.                 risultato = stmt.executeQuery();
  101.                 while (risultato.next()){
  102.                     isInUso=true;

  103.                     String nomeAccordo = risultato.getString("nome");
  104.                     int versione = risultato.getInt("versione");
  105.                     long idReferente = risultato.getLong("id_referente");
  106.                     IDSoggetto idReferenteObject = null;

  107.                     if(idReferente>0){

  108.                         ISQLQueryObject sqlQueryObjectReferente = SQLObjectFactory.createSQLQueryObject(tipoDB);
  109.                         sqlQueryObjectReferente.addFromTable(CostantiDB.SOGGETTI);
  110.                         sqlQueryObjectReferente.addSelectField("*");
  111.                         sqlQueryObjectReferente.addWhereCondition("id=?");
  112.                         sqlQueryObjectReferente.setANDLogicOperator(true);
  113.                         String queryStringReferente = sqlQueryObjectReferente.createSQLQuery();
  114.                         stmt2 = con.prepareStatement(queryStringReferente);
  115.                         stmt2.setLong(1, idReferente);
  116.                         risultato2 = stmt2.executeQuery();
  117.                         if(risultato2.next()){
  118.                             idReferenteObject = new IDSoggetto();
  119.                             idReferenteObject.setTipo(risultato2.getString("tipo_soggetto"));
  120.                             idReferenteObject.setNome(risultato2.getString("nome_soggetto"));
  121.                         }
  122.                         risultato2.close(); risultato2=null;
  123.                         stmt2.close(); stmt2=null;

  124.                     }

  125.                     if(normalizeObjectIds && idReferenteObject!=null) {
  126.                         String protocollo = ProtocolFactoryManager.getInstance().getProtocolByOrganizationType(idReferenteObject.getTipo());
  127.                         IDAccordo idAccordo = IDAccordoFactory.getInstance().getIDAccordoFromValues(nomeAccordo, idReferenteObject, versione);
  128.                         accordi_list.add(DBOggettiInUsoUtils.getProtocolPrefix(protocollo)+NamingUtils.getLabelAccordoServizioParteComune(protocollo, idAccordo));
  129.                     }
  130.                     else {

  131.                         StringBuilder bf = new StringBuilder();

  132.                         bf.append(idReferenteObject!=null ? idReferenteObject.getTipo() : "?");
  133.                         bf.append("/");
  134.                         bf.append(idReferenteObject!=null ? idReferenteObject.getNome() : "?");
  135.                         bf.append(":");

  136.                         bf.append(nomeAccordo);

  137.                         if(idReferente>0){
  138.                             bf.append(":");
  139.                             bf.append(versione);
  140.                         }

  141.                         accordi_list.add(bf.toString());
  142.                     }

  143.                 }
  144.                 risultato.close();
  145.                 stmt.close();
  146.             }

  147.             // Controllo che il soggetto non sia associato a policy di controllo del traffico o allarmi
  148.             if(config){

  149.                 int max = 2;
  150.                 if(!CostantiDB.isAllarmiEnabled()) {
  151.                     max=1;
  152.                 }

  153.                 for (int i = 0; i < max; i++) {

  154.                     String tabella = CostantiDB.CONTROLLO_TRAFFICO_ACTIVE_POLICY;
  155.                     String identificativo_column = "active_policy_id";
  156.                     String alias_column = "policy_alias";
  157.                     List<String> list = ct_list;
  158.                     String oggetto = "Policy";
  159.                     if(i==1) {
  160.                         tabella = CostantiDB.ALLARMI;
  161.                         identificativo_column = "nome";
  162.                         alias_column = "alias";
  163.                         list = allarme_list;
  164.                         oggetto = "Allarme";
  165.                     }

  166.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  167.                     sqlQueryObject.addFromTable(tabella);
  168.                     sqlQueryObject.addSelectField(identificativo_column);
  169.                     sqlQueryObject.addSelectField(alias_column);
  170.                     sqlQueryObject.addSelectField("filtro_ruolo");
  171.                     sqlQueryObject.addSelectField("filtro_porta");
  172.                     sqlQueryObject.setANDLogicOperator(false); // OR
  173.                     sqlQueryObject.addWhereCondition(true,
  174.                             tabella+".filtro_tag = ?");
  175.                     queryString = sqlQueryObject.createSQLQuery();
  176.                     sqlQueryObject.addOrderBy("filtro_ruolo");
  177.                     sqlQueryObject.addOrderBy("filtro_porta");
  178.                     stmt = con.prepareStatement(queryString);
  179.                     int index = 1;
  180.                     stmt.setString(index++, idGruppo.getNome());
  181.                     risultato = stmt.executeQuery();
  182.                     while (risultato.next()) {

  183.                         String alias = risultato.getString(alias_column);
  184.                         if(alias== null || "".equals(alias)) {
  185.                             alias = risultato.getString(identificativo_column);
  186.                         }

  187.                         String nomePorta = risultato.getString("filtro_porta");
  188.                         String filtro_ruolo = risultato.getString("filtro_ruolo");
  189.                         if(nomePorta!=null) {
  190.                             String tipo = null;
  191.                             String label = null;
  192.                             if("delegata".equals(filtro_ruolo)) {
  193.                                 try {
  194.                                     ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaDelegata(nomePorta, tipoDB, con, normalizeObjectIds);
  195.                                     if(resultPorta.mapping) {
  196.                                         label = "Fruizione di Servizio "+ resultPorta.label;
  197.                                     }
  198.                                 }catch(Exception e) {
  199.                                     tipo = "Outbound";
  200.                                 }
  201.                             }
  202.                             else if("applicativa".equals(filtro_ruolo)) {
  203.                                 try {
  204.                                     ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaApplicativa(nomePorta, tipoDB, con, normalizeObjectIds);
  205.                                     if(resultPorta.mapping) {
  206.                                         label = "Erogazione di Servizio "+ resultPorta.label;
  207.                                     }
  208.                                 }catch(Exception e) {
  209.                                     tipo = "Inbound";
  210.                                 }
  211.                             }
  212.                             else {
  213.                                 tipo = filtro_ruolo;
  214.                             }
  215.                             if(label==null) {
  216.                                 list.add(oggetto+" '"+alias+"' attiva nella porta '"+tipo+"' '"+nomePorta+"' ");
  217.                             }
  218.                             else {
  219.                                 list.add(oggetto+" '"+alias+"' attiva nella "+label);
  220.                             }
  221.                         }
  222.                         else {
  223.                             list.add(oggetto+" '"+alias+"'");
  224.                         }

  225.                         isInUso = true;
  226.                     }
  227.                     risultato.close();
  228.                     stmt.close();

  229.                 }
  230.             }

  231.             return isInUso;

  232.         } catch (Exception se) {
  233.             throw new UtilsException("[DBOggettiInUsoUtils::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  234.         } finally {
  235.             // Chiudo statement and resultset
  236.             JDBCUtilities.closeResources(risultato2, stmt2);
  237.             JDBCUtilities.closeResources(risultato, stmt);
  238.         }
  239.     }


  240.     protected static String toString(IDGruppo idGruppo, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean prefix, String separator){
  241.         return toString(idGruppo, whereIsInUso, prefix, separator," non eliminabile perch&egrave; :");
  242.     }
  243.     protected static String toString(IDGruppo idGruppo, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean prefix, String separator, String intestazione){
  244.         Set<ErrorsHandlerCostant> keys = whereIsInUso.keySet();
  245.         String msg = "Gruppo '"+idGruppo.getNome()+"'" + intestazione+separator;
  246.         if(prefix==false){
  247.             msg = "";
  248.         }
  249.         String separatorCategorie = "";
  250.         if(whereIsInUso.size()>1) {
  251.             separatorCategorie = separator;
  252.         }
  253.         for (ErrorsHandlerCostant key : keys) {
  254.             List<String> messages = whereIsInUso.get(key);

  255.             if ( messages!=null && messages.size() > 0) {
  256.                 msg += separatorCategorie;
  257.             }

  258.             switch (key) {
  259.             case IN_USO_IN_ACCORDI:
  260.                 if ( messages!=null && messages.size() > 0 ) {
  261.                     msg += "associato all'API: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  262.                 }
  263.                 break;
  264.             case CONTROLLO_TRAFFICO:
  265.                 if ( messages!=null && messages.size() > 0 ) {
  266.                     msg += "utilizzato in Policy di Rate Limiting: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  267.                 }
  268.                 break;
  269.             case ALLARMI:
  270.                 if ( messages!=null && messages.size() > 0 ) {
  271.                     msg += "utilizzato in Allarmi: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  272.                 }
  273.                 break;
  274.             default:
  275.                 msg += "utilizzato in oggetto non codificato ("+key+")"+separator;
  276.                 break;
  277.             }

  278.         }// chiudo for

  279.         return msg;
  280.     }

  281. }