DBOggettiInUsoUtils_accordi.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.constants.ProprietariProtocolProperty;
  32. import org.openspcoop2.core.id.IDAccordo;
  33. import org.openspcoop2.core.id.IDPortType;
  34. import org.openspcoop2.core.id.IDPortTypeAzione;
  35. import org.openspcoop2.core.id.IDServizio;
  36. import org.openspcoop2.core.id.IDSoggetto;
  37. import org.openspcoop2.core.mapping.DBMappingUtils;
  38. import org.openspcoop2.core.mapping.MappingErogazionePortaApplicativa;
  39. import org.openspcoop2.core.mapping.MappingFruizionePortaDelegata;
  40. import org.openspcoop2.core.registry.constants.ServiceBinding;
  41. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  42. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  43. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  44. import org.openspcoop2.protocol.engine.constants.Costanti;
  45. import org.openspcoop2.utils.UtilsException;
  46. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  47. import org.openspcoop2.utils.sql.ISQLQueryObject;
  48. import org.openspcoop2.utils.sql.SQLObjectFactory;

  49. /**
  50.  * DBOggettiInUsoUtils_accordi
  51.  *
  52.  * @author Andrea Poli (apoli@link.it)
  53.  * @author $Author$
  54.  * @version $Rev$, $Date$
  55.  *
  56.  */
  57. public class DBOggettiInUsoUtils_accordi {

  58.     protected static boolean isAccordoServizioParteComuneInUso(Connection con, String tipoDB, IDAccordo idAccordo,
  59.             Map<ErrorsHandlerCostant,List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  60.         String nomeMetodo = "isAccordoServizioParteComuneInUso";

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


  66.             long idAccordoServizioParteComune = DBUtils.getIdAccordoServizioParteComune(idAccordo, con, tipoDB);
  67.             if(idAccordoServizioParteComune<=0){
  68.                 throw new UtilsException("Accordi di Servizio Parte Comune con id ["+idAccordo.toString()+"] non trovato");
  69.             }

  70.             List<String> servizi_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_SERVIZI);
  71.             List<String> mappingErogazionePA_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_MAPPING_EROGAZIONE_PA);
  72.             List<String> mappingFruizionePD_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_MAPPING_FRUIZIONE_PD);
  73.            
  74.             List<String> correlazione_list = whereIsInUso.get(ErrorsHandlerCostant.IS_CORRELATA);
  75.            
  76.             if (servizi_list == null) {
  77.                 servizi_list = new ArrayList<>();
  78.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_SERVIZI, servizi_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.             if (correlazione_list == null) {
  90.                 correlazione_list = new ArrayList<>();
  91.                 whereIsInUso.put(ErrorsHandlerCostant.IS_CORRELATA, correlazione_list);
  92.             }
  93.            

  94.             //controllo se in uso in servizi
  95.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  96.             sqlQueryObject.addFromTable(CostantiDB.SERVIZI);
  97.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  98.             sqlQueryObject.addSelectField("*");
  99.             sqlQueryObject.addWhereCondition("id_accordo = ?");
  100.             sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI+".id_soggetto = "+CostantiDB.SOGGETTI+".id");
  101.             sqlQueryObject.setANDLogicOperator(true);
  102.             queryString = sqlQueryObject.createSQLQuery();
  103.             stmt = con.prepareStatement(queryString);
  104.             stmt.setLong(1, idAccordoServizioParteComune);
  105.             risultato = stmt.executeQuery();
  106.             List<IDServizio> listIDServizio = new ArrayList<>();
  107.             while (risultato.next()){
  108.                 isInUso=true;
  109.                
  110.                 String tipoSoggettoErogatore = risultato.getString("tipo_soggetto");
  111.                 String nomeSoggettoErogatore = risultato.getString("nome_soggetto");
  112.                 String tipoServizio = risultato.getString("tipo_servizio");
  113.                 String nomeServizio = risultato.getString("nome_servizio");
  114.                 int versioneServizio = risultato.getInt("versione_servizio");
  115.                 IDServizio idServizio = IDServizioFactory.getInstance().getIDServizioFromValues(tipoServizio, nomeServizio,
  116.                         tipoSoggettoErogatore, nomeSoggettoErogatore, versioneServizio);
  117.                 listIDServizio.add(idServizio);
  118.             }
  119.             risultato.close();
  120.             stmt.close();
  121.            
  122.             if(!listIDServizio.isEmpty()) {
  123.                
  124.                 for (IDServizio idServizio : listIDServizio) {
  125.                    
  126.                     String protocollo = ProtocolFactoryManager.getInstance().getProtocolByOrganizationType(idServizio.getSoggettoErogatore().getTipo());
  127.                     boolean found = false;
  128.                    
  129.                     // check PA
  130.                     List<MappingErogazionePortaApplicativa> listPA = null;
  131.                     try {
  132.                         listPA = DBMappingUtils.mappingErogazionePortaApplicativaList(con, tipoDB, idServizio, true);
  133.                     }catch(Exception e) {
  134.                         // ignore
  135.                     }
  136.                     if(listPA!=null && !listPA.isEmpty()) {
  137.                         found=true;
  138.                         for (MappingErogazionePortaApplicativa mappingPA : listPA) {
  139.                             String suffixGruppo = "";
  140.                             if(!mappingPA.isDefault()) {
  141.                                 suffixGruppo = " (Gruppo: "+mappingPA.getDescrizione()+")";
  142.                             }
  143.                             String servizio = DBOggettiInUsoUtils.getProtocolPrefix(protocollo)+
  144.                                     NamingUtils.getLabelAccordoServizioParteSpecifica(protocollo, mappingPA.getIdServizio())+
  145.                                     suffixGruppo;
  146.                             mappingErogazionePA_list.add(servizio);
  147.                         }
  148.                     }
  149.                
  150.                     // check PD
  151.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  152.                     sqlQueryObject.addFromTable(CostantiDB.SERVIZI);
  153.                     sqlQueryObject.addFromTable(CostantiDB.SERVIZI_FRUITORI);
  154.                     sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  155.                     sqlQueryObject.addSelectField("tipo_soggetto");
  156.                     sqlQueryObject.addSelectField("nome_soggetto");
  157.                     sqlQueryObject.addWhereCondition("tipo_servizio = ?");
  158.                     sqlQueryObject.addWhereCondition("nome_servizio = ?");
  159.                     sqlQueryObject.addWhereCondition("versione_servizio = ?");
  160.                     sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI+".id_soggetto = ?");
  161.                     sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI_FRUITORI+".id_servizio = "+CostantiDB.SERVIZI+".id");
  162.                     sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI_FRUITORI+".id_soggetto = "+CostantiDB.SOGGETTI+".id");
  163.                     sqlQueryObject.setANDLogicOperator(true);
  164.                     queryString = sqlQueryObject.createSQLQuery();
  165.                     stmt = con.prepareStatement(queryString);
  166.                     stmt.setString(1, idServizio.getTipo());
  167.                     stmt.setString(2, idServizio.getNome());
  168.                     stmt.setInt(3, idServizio.getVersione());
  169.                     stmt.setLong(4, DBUtils.getIdSoggetto(idServizio.getSoggettoErogatore().getNome(), idServizio.getSoggettoErogatore().getTipo(), con, tipoDB));
  170.                     risultato = stmt.executeQuery();
  171.                     while (risultato.next()){                  
  172.                         String tipoSoggettoFruitore = risultato.getString("tipo_soggetto");
  173.                         String nomeSoggettoFruitore = risultato.getString("nome_soggetto");
  174.                         IDSoggetto idSoggettoFruitore = new IDSoggetto(tipoSoggettoFruitore, nomeSoggettoFruitore);
  175.                         List<MappingFruizionePortaDelegata> listPD = null;
  176.                         try {
  177.                             listPD = DBMappingUtils.mappingFruizionePortaDelegataList(con, tipoDB, idSoggettoFruitore, idServizio, true);
  178.                         }catch(Exception e) {
  179.                             // ignore
  180.                         }
  181.                         if(listPD!=null && !listPD.isEmpty()) {
  182.                             found=true;
  183.                             for (MappingFruizionePortaDelegata mappingPD : listPD) {
  184.                                 String suffixGruppo = "";
  185.                                 if(!mappingPD.isDefault()) {
  186.                                     suffixGruppo = " (Gruppo: "+mappingPD.getDescrizione()+")";
  187.                                 }
  188.                                 String servizio = DBOggettiInUsoUtils.getProtocolPrefix(protocollo)+
  189.                                         NamingUtils.getLabelAccordoServizioParteSpecifica(protocollo, mappingPD.getIdServizio())+
  190.                                         " (Fruitore:"+NamingUtils.getLabelSoggetto(protocollo, idSoggettoFruitore)+")"+
  191.                                         suffixGruppo;
  192.                                 mappingFruizionePD_list.add(servizio);
  193.                             }
  194.                         }
  195.                     }
  196.                     risultato.close();
  197.                     stmt.close();
  198.                    
  199.                     // servizio
  200.                     if(!found) {
  201.                         servizi_list.add(idServizio.toString());
  202.                     }
  203.                 }
  204.                
  205.             }

  206.            
  207.            
  208.            
  209.             // Controllo che qualche azione o risorsa non sia stata correlata da un'altra azione tramite ModI
  210.             if(Costanti.MODIPA_PROTOCOL_NAME.equals(idAccordo.getSoggettoReferente().getTipo())) {
  211.            
  212.                 // Recupero tipo REST/SOAP
  213.                
  214.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  215.                 sqlQueryObject.addFromTable(CostantiDB.ACCORDI);
  216.                 sqlQueryObject.addSelectField("service_binding");
  217.                 sqlQueryObject.addWhereCondition("id = ?");
  218.                 sqlQueryObject.setANDLogicOperator(true);
  219.                 queryString = sqlQueryObject.createSQLQuery();
  220.                 stmt = con.prepareStatement(queryString);
  221.                 stmt.setLong(1, idAccordoServizioParteComune);
  222.                 risultato = stmt.executeQuery();
  223.                 ServiceBinding tipoAccordo = null;
  224.                 if (risultato.next()){
  225.                     tipoAccordo = ServiceBinding.toEnumConstant(risultato.getString("service_binding"), false);
  226.                 }
  227.                 risultato.close();
  228.                 stmt.close();
  229.                
  230.                 if(ServiceBinding.REST.equals(tipoAccordo)) {
  231.                    
  232.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  233.                     sqlQueryObject.addFromTable(CostantiDB.ACCORDI);
  234.                     sqlQueryObject.addFromTable(CostantiDB.API_RESOURCES);
  235.                     sqlQueryObject.addSelectAliasField(CostantiDB.API_RESOURCES,"nome","nomeRisorsa");
  236.                     sqlQueryObject.addSelectAliasField(CostantiDB.API_RESOURCES,"http_method","httpMethodRisorsa");
  237.                     sqlQueryObject.addSelectAliasField(CostantiDB.API_RESOURCES,"path","pathRisorsa");
  238.                     sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI + ".id = ?");
  239.                     sqlQueryObject.addWhereCondition(CostantiDB.API_RESOURCES + ".id_accordo = " + CostantiDB.ACCORDI + ".id");
  240.                     sqlQueryObject.setANDLogicOperator(true);
  241.                     sqlQueryObject.addOrderBy("pathRisorsa");
  242.                     sqlQueryObject.addOrderBy("httpMethodRisorsa");
  243.                     queryString = sqlQueryObject.createSQLQuery();
  244.                     stmt = con.prepareStatement(queryString);
  245.                     int index = 1;
  246.                     stmt.setLong(index++, idAccordoServizioParteComune);
  247.                     risultato = stmt.executeQuery();
  248.                     List<String> risorse = new ArrayList<>();
  249.                     while(risultato.next()){
  250.                         String nomeRisorsa = risultato.getString("nomeRisorsa");
  251.                         risorse.add(nomeRisorsa);
  252.                     }
  253.                     risultato.close();
  254.                     stmt.close();
  255.                    
  256.                     if(!risorse.isEmpty()) {
  257.                        
  258.                         for (String nomeRisorsa : risorse) {
  259.                            
  260.                             ISQLQueryObject sqlQueryObjectApiExists = SQLObjectFactory.createSQLQueryObject(tipoDB);
  261.                             sqlQueryObjectApiExists.setANDLogicOperator(true);
  262.                             sqlQueryObjectApiExists.addFromTable(CostantiDB.PROTOCOL_PROPERTIES);
  263.                             sqlQueryObjectApiExists.addSelectField(CostantiDB.PROTOCOL_PROPERTIES, "name");
  264.                             sqlQueryObjectApiExists.addWhereCondition(CostantiDB.API_RESOURCES + ".id = " + CostantiDB.PROTOCOL_PROPERTIES + ".id_proprietario");
  265.                             sqlQueryObjectApiExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".tipo_proprietario=?");
  266.                             sqlQueryObjectApiExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".name=?");
  267.                             sqlQueryObjectApiExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".value_string=?");
  268.                
  269.                             ISQLQueryObject sqlQueryObjectOperationExists = SQLObjectFactory.createSQLQueryObject(tipoDB);
  270.                             sqlQueryObjectOperationExists.setANDLogicOperator(true);
  271.                             sqlQueryObjectOperationExists.addFromTable(CostantiDB.PROTOCOL_PROPERTIES);
  272.                             sqlQueryObjectOperationExists.addSelectField(CostantiDB.PROTOCOL_PROPERTIES, "name");
  273.                             sqlQueryObjectOperationExists.addWhereCondition(CostantiDB.API_RESOURCES + ".id = " + CostantiDB.PROTOCOL_PROPERTIES + ".id_proprietario");
  274.                             sqlQueryObjectOperationExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".tipo_proprietario=?");
  275.                             sqlQueryObjectOperationExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".name=?");
  276.                             sqlQueryObjectOperationExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".value_string=?");
  277.                            
  278.                            
  279.                             // Verifico correlazione PUSH verso altri accordi
  280.                            
  281.                             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  282.                             sqlQueryObject.addFromTable(CostantiDB.ACCORDI);
  283.                             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  284.                             sqlQueryObject.addFromTable(CostantiDB.API_RESOURCES);
  285.                             sqlQueryObject.addSelectAliasField(CostantiDB.API_RESOURCES,"nome","nomeRisorsaCorrelata");
  286.                             sqlQueryObject.addSelectAliasField(CostantiDB.API_RESOURCES,"http_method","httpMethodRisorsaCorrelata");
  287.                             sqlQueryObject.addSelectAliasField(CostantiDB.API_RESOURCES,"path","pathRisorsaCorrelata");
  288.                             sqlQueryObject.addSelectAliasField(CostantiDB.ACCORDI,"nome","nomeApi");
  289.                             sqlQueryObject.addSelectAliasField(CostantiDB.ACCORDI,"versione","versioneApi");
  290.                             sqlQueryObject.addSelectAliasField(CostantiDB.SOGGETTI,"tipo_soggetto","tipoReferenteApi");
  291.                             sqlQueryObject.addSelectAliasField(CostantiDB.SOGGETTI,"nome_soggetto","nomeReferenteApi");
  292.                             sqlQueryObject.addWhereCondition(CostantiDB.API_RESOURCES + ".id_accordo = " + CostantiDB.ACCORDI + ".id");
  293.                             sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI + ".id = " + CostantiDB.ACCORDI + ".id_referente");
  294.                             sqlQueryObject.addWhereExistsCondition(false, sqlQueryObjectApiExists);
  295.                             sqlQueryObject.addWhereExistsCondition(false, sqlQueryObjectOperationExists);
  296.                             sqlQueryObject.setANDLogicOperator(true);
  297.                             sqlQueryObject.addOrderBy("nomeApi");
  298.                             sqlQueryObject.addOrderBy("versioneApi");
  299.                             sqlQueryObject.addOrderBy("nomeReferenteApi");
  300.                             sqlQueryObject.addOrderBy("tipoReferenteApi");
  301.                             sqlQueryObject.addOrderBy("pathRisorsaCorrelata");
  302.                             sqlQueryObject.addOrderBy("httpMethodRisorsaCorrelata");
  303.                             sqlQueryObject.setSortType(true);
  304.                             queryString = sqlQueryObject.createSQLQuery();
  305.                             stmt = con.prepareStatement(queryString);
  306.                             index = 1;
  307.                             // sqlQueryObjectApiExists
  308.                             stmt.setString(index++, ProprietariProtocolProperty.RESOURCE.name());
  309.                             stmt.setString(index++, Costanti.MODIPA_PROFILO_INTERAZIONE_ASINCRONA_API_RICHIESTA_CORRELATA);
  310.                             stmt.setString(index++, IDAccordoFactory.getInstance().getUriFromIDAccordo(idAccordo));
  311.                             // sqlQueryObjectOperationExists
  312.                             stmt.setString(index++, ProprietariProtocolProperty.RESOURCE.name());
  313.                             stmt.setString(index++, Costanti.MODIPA_PROFILO_INTERAZIONE_ASINCRONA_AZIONE_RICHIESTA_CORRELATA);
  314.                             stmt.setString(index++, nomeRisorsa);
  315.                             risultato = stmt.executeQuery();
  316.                             IDAccordoFactory idAccordoFactory = IDAccordoFactory.getInstance();
  317.                             while (risultato.next()){
  318.                                
  319.                                 String nomeApi = risultato.getString("nomeApi");
  320.                                 int versioneApi = risultato.getInt("versioneApi");
  321.                                 String tipoReferenteApi = risultato.getString("tipoReferenteApi");
  322.                                 String nomeReferenteApi = risultato.getString("nomeReferenteApi");
  323.                                 IDAccordo idAPI = idAccordoFactory.getIDAccordoFromValues(nomeApi, tipoReferenteApi, nomeReferenteApi, versioneApi);
  324.                                
  325.                                 if(idAPI.equals(idAccordo)) {
  326.                                     continue; // non devo tenere traccia di correlazioni interne allo stesso accordo. Devo poter eliminare l'accordo nella sua interezza
  327.                                 }
  328.                                
  329.                                 @SuppressWarnings("unused")
  330.                                 String nomeRisorsaCorrelata = risultato.getString("nomeRisorsaCorrelata");
  331.                                 String httpMethodRisorsaCorrelata = risultato.getString("httpMethodRisorsaCorrelata");
  332.                                 String pathRisorsaCorrelata = risultato.getString("pathRisorsaCorrelata");
  333.                                
  334.                                 String path = null;
  335.                                 if(pathRisorsaCorrelata==null || "".equals(pathRisorsaCorrelata)) {
  336.                                     path = "*";
  337.                                 }
  338.                                 else {
  339.                                     path = pathRisorsaCorrelata;
  340.                                 }
  341.                                
  342.                                 String method = null;
  343.                                 if(httpMethodRisorsaCorrelata==null || "".equals(httpMethodRisorsaCorrelata)) {
  344.                                     method = "Qualsiasi";
  345.                                 }
  346.                                 else {
  347.                                     method = httpMethodRisorsaCorrelata;
  348.                                 }
  349.                                
  350.                                 correlazione_list.add("Risorsa "+method+" "+path+" dell'API '"+idAccordoFactory.getUriFromIDAccordo(idAPI)+"' (interazione: NonBloccante-Push)");
  351.                                 isInUso = true;
  352.                             }
  353.                             risultato.close();
  354.                             stmt.close();
  355.                         }
  356.                     }
  357.                 }
  358.                 else {
  359.                    
  360.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  361.                     sqlQueryObject.addFromTable(CostantiDB.ACCORDI);
  362.                     sqlQueryObject.addFromTable(CostantiDB.PORT_TYPE);
  363.                     sqlQueryObject.addFromTable(CostantiDB.PORT_TYPE_AZIONI);
  364.                     sqlQueryObject.addSelectAliasField(CostantiDB.PORT_TYPE_AZIONI,"nome","nomeAzione");
  365.                     sqlQueryObject.addSelectAliasField(CostantiDB.PORT_TYPE,"nome","nomePT");
  366.                     sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI + ".id = ?");
  367.                     sqlQueryObject.addWhereCondition(CostantiDB.PORT_TYPE + ".id_accordo = " + CostantiDB.ACCORDI + ".id");
  368.                     sqlQueryObject.addWhereCondition(CostantiDB.PORT_TYPE_AZIONI + ".id_port_type = " + CostantiDB.PORT_TYPE + ".id");
  369.                     sqlQueryObject.setANDLogicOperator(true);
  370.                     sqlQueryObject.addOrderBy("nomePT");
  371.                     sqlQueryObject.addOrderBy("nomeAzione");
  372.                     queryString = sqlQueryObject.createSQLQuery();
  373.                     stmt = con.prepareStatement(queryString);
  374.                     int index = 1;
  375.                     stmt.setLong(index++, idAccordoServizioParteComune);
  376.                     risultato = stmt.executeQuery();
  377.                     List<IDPortTypeAzione> azioni = new ArrayList<IDPortTypeAzione>();
  378.                     while(risultato.next()){
  379.                        
  380.                         IDPortType idPT = new IDPortType();
  381.                         idPT.setIdAccordo(idAccordo);
  382.                         idPT.setNome(risultato.getString("nomePT"));
  383.                        
  384.                         IDPortTypeAzione id = new IDPortTypeAzione();
  385.                         id.setIdPortType(idPT);
  386.                         id.setNome(risultato.getString("nomeAzione"));
  387.                        
  388.                         azioni.add(id);
  389.                     }
  390.                     risultato.close();
  391.                     stmt.close();
  392.                    
  393.                     if(!azioni.isEmpty()) {
  394.                        
  395.                         for (IDPortTypeAzione idAzione : azioni) {
  396.                            
  397.                             ISQLQueryObject sqlQueryObjectApiExists = SQLObjectFactory.createSQLQueryObject(tipoDB);
  398.                             sqlQueryObjectApiExists.setANDLogicOperator(true);
  399.                             sqlQueryObjectApiExists.addFromTable(CostantiDB.PROTOCOL_PROPERTIES);
  400.                             sqlQueryObjectApiExists.addSelectField(CostantiDB.PROTOCOL_PROPERTIES, "name");
  401.                             sqlQueryObjectApiExists.addWhereCondition(CostantiDB.PORT_TYPE_AZIONI + ".id = " + CostantiDB.PROTOCOL_PROPERTIES + ".id_proprietario");
  402.                             sqlQueryObjectApiExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".tipo_proprietario=?");
  403.                             sqlQueryObjectApiExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".name=?");
  404.                             sqlQueryObjectApiExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".value_string=?");
  405.                            
  406.                             ISQLQueryObject sqlQueryObjectPortTypeExists = SQLObjectFactory.createSQLQueryObject(tipoDB);
  407.                             sqlQueryObjectPortTypeExists.setANDLogicOperator(true);
  408.                             sqlQueryObjectPortTypeExists.addFromTable(CostantiDB.PROTOCOL_PROPERTIES);
  409.                             sqlQueryObjectPortTypeExists.addSelectField(CostantiDB.PROTOCOL_PROPERTIES, "name");
  410.                             sqlQueryObjectPortTypeExists.addWhereCondition(CostantiDB.PORT_TYPE_AZIONI + ".id = " + CostantiDB.PROTOCOL_PROPERTIES + ".id_proprietario");
  411.                             sqlQueryObjectPortTypeExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".tipo_proprietario=?");
  412.                             sqlQueryObjectPortTypeExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".name=?");
  413.                             sqlQueryObjectPortTypeExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".value_string=?");
  414.                            
  415.                             ISQLQueryObject sqlQueryObjectOperationExists = SQLObjectFactory.createSQLQueryObject(tipoDB);
  416.                             sqlQueryObjectOperationExists.setANDLogicOperator(true);
  417.                             sqlQueryObjectOperationExists.addFromTable(CostantiDB.PROTOCOL_PROPERTIES);
  418.                             sqlQueryObjectOperationExists.addSelectField(CostantiDB.PROTOCOL_PROPERTIES, "name");
  419.                             sqlQueryObjectOperationExists.addWhereCondition(CostantiDB.PORT_TYPE_AZIONI + ".id = " + CostantiDB.PROTOCOL_PROPERTIES + ".id_proprietario");
  420.                             sqlQueryObjectOperationExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".tipo_proprietario=?");
  421.                             sqlQueryObjectOperationExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".name=?");
  422.                             sqlQueryObjectOperationExists.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES + ".value_string=?");
  423.                            
  424.                            
  425.                             // Verifico correlazione PUSH verso altri accordi
  426.                            
  427.                             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  428.                             sqlQueryObject.addFromTable(CostantiDB.ACCORDI);
  429.                             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  430.                             sqlQueryObject.addFromTable(CostantiDB.PORT_TYPE);
  431.                             sqlQueryObject.addFromTable(CostantiDB.PORT_TYPE_AZIONI);
  432.                             sqlQueryObject.addSelectAliasField(CostantiDB.PORT_TYPE_AZIONI,"nome","nomeAzioneCorrelata");
  433.                             sqlQueryObject.addSelectAliasField(CostantiDB.PORT_TYPE,"nome","nomePTCorrelato");
  434.                             sqlQueryObject.addSelectAliasField(CostantiDB.ACCORDI,"nome","nomeApi");
  435.                             sqlQueryObject.addSelectAliasField(CostantiDB.ACCORDI,"versione","versioneApi");
  436.                             sqlQueryObject.addSelectAliasField(CostantiDB.SOGGETTI,"tipo_soggetto","tipoReferenteApi");
  437.                             sqlQueryObject.addSelectAliasField(CostantiDB.SOGGETTI,"nome_soggetto","nomeReferenteApi");
  438.                             sqlQueryObject.addWhereCondition(CostantiDB.PORT_TYPE + ".id_accordo = " + CostantiDB.ACCORDI + ".id");
  439.                             sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI + ".id = " + CostantiDB.ACCORDI + ".id_referente");
  440.                             sqlQueryObject.addWhereCondition(CostantiDB.PORT_TYPE_AZIONI + ".id_port_type = " + CostantiDB.PORT_TYPE + ".id");
  441.                             sqlQueryObject.addWhereExistsCondition(false, sqlQueryObjectApiExists);
  442.                             sqlQueryObject.addWhereExistsCondition(false, sqlQueryObjectPortTypeExists);
  443.                             sqlQueryObject.addWhereExistsCondition(false, sqlQueryObjectOperationExists);
  444.                             sqlQueryObject.setANDLogicOperator(true);
  445.                             sqlQueryObject.addOrderBy("nomeApi");
  446.                             sqlQueryObject.addOrderBy("versioneApi");
  447.                             sqlQueryObject.addOrderBy("nomeReferenteApi");
  448.                             sqlQueryObject.addOrderBy("tipoReferenteApi");
  449.                             sqlQueryObject.addOrderBy("nomePTCorrelato");
  450.                             sqlQueryObject.addOrderBy("nomeAzioneCorrelata");
  451.                             sqlQueryObject.setSortType(true);
  452.                             queryString = sqlQueryObject.createSQLQuery();
  453.                             stmt = con.prepareStatement(queryString);
  454.                             index = 1;
  455.                             // sqlQueryObjectApiExists
  456.                             stmt.setString(index++, ProprietariProtocolProperty.OPERATION.name());
  457.                             stmt.setString(index++, Costanti.MODIPA_PROFILO_INTERAZIONE_ASINCRONA_API_RICHIESTA_CORRELATA);
  458.                             stmt.setString(index++, IDAccordoFactory.getInstance().getUriFromIDAccordo(idAzione.getIdPortType().getIdAccordo()));
  459.                             // sqlQueryObjectPortTypeExists
  460.                             stmt.setString(index++, ProprietariProtocolProperty.OPERATION.name());
  461.                             stmt.setString(index++, Costanti.MODIPA_PROFILO_INTERAZIONE_ASINCRONA_SERVIZIO_RICHIESTA_CORRELATA);
  462.                             stmt.setString(index++, idAzione.getIdPortType().getNome());
  463.                             // sqlQueryObjectOperationExists
  464.                             stmt.setString(index++, ProprietariProtocolProperty.OPERATION.name());
  465.                             stmt.setString(index++, Costanti.MODIPA_PROFILO_INTERAZIONE_ASINCRONA_AZIONE_RICHIESTA_CORRELATA);
  466.                             stmt.setString(index++, idAzione.getNome());
  467.                             risultato = stmt.executeQuery();
  468.                             IDAccordoFactory idAccordoFactory = IDAccordoFactory.getInstance();
  469.                             while (risultato.next()){
  470.                                 String nomeAzioneCorrelata = risultato.getString("nomeAzioneCorrelata");
  471.                                 String nomePTCorrelato = risultato.getString("nomePTCorrelato");
  472.                                 String nomeApi = risultato.getString("nomeApi");
  473.                                 int versioneApi = risultato.getInt("versioneApi");
  474.                                 String tipoReferenteApi = risultato.getString("tipoReferenteApi");
  475.                                 String nomeReferenteApi = risultato.getString("nomeReferenteApi");
  476.                                 IDAccordo idAPI = idAccordoFactory.getIDAccordoFromValues(nomeApi, tipoReferenteApi, nomeReferenteApi, versioneApi);
  477.                                
  478.                                 if(idAPI.equals(idAccordo)) {
  479.                                     continue; // non devo tenere traccia di correlazioni interne allo stesso accordo. Devo poter eliminare l'accordo nella sua interezza
  480.                                 }
  481.                                
  482.                                 correlazione_list.add("Azione "+nomeAzioneCorrelata+" del Servizio "+nomePTCorrelato+" dell'API '"+idAccordoFactory.getUriFromIDAccordo(idAPI)+"' (interazione: NonBloccante-Push)");
  483.                                 isInUso = true;
  484.                             }
  485.                             risultato.close();
  486.                             stmt.close();
  487.                            
  488.                         }
  489.                        
  490.                     }
  491.                    
  492.                 }

  493.             }
  494.            
  495.            
  496.             return isInUso;

  497.         } catch (Exception se) {

  498.             throw new UtilsException("[DBOggettiInUsoUtils::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  499.         } finally {
  500.             //Chiudo statement and resultset
  501.             JDBCUtilities.closeResources(risultato, stmt);
  502.         }
  503.     }


  504.     protected static String toString(IDAccordo idAccordo, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean prefix, String separator, boolean normalizeObjectIds){

  505.         StringBuilder bf = new StringBuilder();
  506.         if(normalizeObjectIds && idAccordo.getSoggettoReferente()!=null) {
  507.             try {
  508.                 String protocollo = ProtocolFactoryManager.getInstance().getProtocolByOrganizationType(idAccordo.getSoggettoReferente().getTipo());
  509.                 String labelAccordo = DBOggettiInUsoUtils.getProtocolPrefix(protocollo)+NamingUtils.getLabelAccordoServizioParteComune(protocollo, idAccordo);
  510.                 bf.append(labelAccordo);
  511.             }catch(Exception e) {
  512.                 bf.append(idAccordo.toString());
  513.             }
  514.         }
  515.         else {
  516.             bf.append(idAccordo.toString());
  517.         }

  518.         Set<ErrorsHandlerCostant> keys = whereIsInUso.keySet();
  519.         String msg = "API '"+bf.toString() + "' non eliminabile perch&egrave; :"+separator;
  520.         if(prefix==false){
  521.             msg = "";
  522.         }
  523.         String separatorCategorie = "";
  524.         if(whereIsInUso.size()>1) {
  525.             separatorCategorie = separator;
  526.         }
  527.         for (ErrorsHandlerCostant key : keys) {
  528.             List<String> messages = whereIsInUso.get(key);

  529.             if ( messages!=null && messages.size() > 0) {
  530.                 msg += separatorCategorie;
  531.             }
  532.            
  533.             switch (key) {
  534.             case IN_USO_IN_SERVIZI:
  535.                 if ( messages!=null && messages.size() > 0) {
  536.                     msg += "implementata dai Servizi: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  537.                 }
  538.                 break;
  539.             case IN_USO_IN_MAPPING_EROGAZIONE_PA:
  540.                 if ( messages!=null && messages.size() > 0) {
  541.                     msg += "implementata nelle Erogazioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  542.                 }
  543.                 break;
  544.             case IN_USO_IN_MAPPING_FRUIZIONE_PD:
  545.                 if ( messages!=null && messages.size() > 0) {
  546.                     msg += "implementata nelle Fruizioni: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  547.                 }
  548.                 break;
  549.                
  550.             case IS_CORRELATA:
  551.                 if ( messages!=null && messages.size() > 0) {
  552.                     msg += "correlata ad operazioni di altre API: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  553.                 }
  554.                 break;
  555.                
  556.             default:
  557.                 msg += "utilizzata in oggetto non codificato ("+key+")"+separator;
  558.                 break;
  559.             }

  560.         }// chiudo for

  561.         return msg;
  562.     }
  563.    
  564. }