DBOggettiInUsoUtils_accordiCooperazione.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.IDAccordoCooperazione;
  32. import org.openspcoop2.core.id.IDSoggetto;
  33. import org.openspcoop2.core.registry.driver.IDAccordoCooperazioneFactory;
  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.SQLObjectFactory;

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

  48.     protected static boolean isAccordoCooperazioneInUso(Connection con, String tipoDB, IDAccordoCooperazione idAccordo,
  49.             Map<ErrorsHandlerCostant,List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  50.         String nomeMetodo = "isAccordoCooperazioneInUso";

  51.         PreparedStatement stmt = null;
  52.         ResultSet risultato = null;
  53.         PreparedStatement stmt2 = null;
  54.         ResultSet risultato2 = null;
  55.         String queryString;
  56.         try {
  57.             boolean isInUso = false;


  58.             long idAccordoServizioParteComune = DBUtils.getIdAccordoCooperazione(idAccordo, con, tipoDB);
  59.             if(idAccordoServizioParteComune<=0){
  60.                 throw new UtilsException("Accordi di Cooperazione con id ["+idAccordo.toString()+"] non trovato");
  61.             }

  62.             List<String> accordi_list = whereIsInUso.get(ErrorsHandlerCostant.IN_USO_IN_ACCORDI);

  63.             if (accordi_list == null) {
  64.                 accordi_list = new ArrayList<>();
  65.                 whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_ACCORDI, accordi_list);
  66.             }

  67.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  68.             sqlQueryObject.addFromTable(CostantiDB.ACCORDI_SERVIZI_COMPOSTO);
  69.             sqlQueryObject.addFromTable(CostantiDB.ACCORDI);
  70.             sqlQueryObject.addSelectField("*");
  71.             sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_SERVIZI_COMPOSTO+".id_accordo_cooperazione = ?");
  72.             sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_SERVIZI_COMPOSTO+".id_accordo = "+CostantiDB.ACCORDI+".id");
  73.             sqlQueryObject.setANDLogicOperator(true);
  74.             queryString = sqlQueryObject.createSQLQuery();
  75.             stmt = con.prepareStatement(queryString);
  76.             stmt.setLong(1, idAccordoServizioParteComune);
  77.             risultato = stmt.executeQuery();
  78.             while (risultato.next()){
  79.                 isInUso=true;
  80.                
  81.                 String nomeAccordo = risultato.getString("nome");
  82.                 int versione = risultato.getInt("versione");
  83.                 long idReferente = risultato.getLong("id_referente");
  84.                 IDSoggetto idReferenteObject = null;
  85.                
  86.                 if(idReferente>0){

  87.                     ISQLQueryObject sqlQueryObjectReferente = SQLObjectFactory.createSQLQueryObject(tipoDB);
  88.                     sqlQueryObjectReferente.addFromTable(CostantiDB.SOGGETTI);
  89.                     sqlQueryObjectReferente.addSelectField("*");
  90.                     sqlQueryObjectReferente.addWhereCondition("id=?");
  91.                     sqlQueryObjectReferente.setANDLogicOperator(true);
  92.                     String queryStringReferente = sqlQueryObjectReferente.createSQLQuery();
  93.                     stmt2 = con.prepareStatement(queryStringReferente);
  94.                     stmt2.setLong(1, idReferente);
  95.                     risultato2 = stmt2.executeQuery();
  96.                     if(risultato2.next()){
  97.                         idReferenteObject = new IDSoggetto();
  98.                         idReferenteObject.setTipo(risultato2.getString("tipo_soggetto"));
  99.                         idReferenteObject.setNome(risultato2.getString("nome_soggetto"));
  100.                     }
  101.                     risultato2.close(); risultato2=null;
  102.                     stmt2.close(); stmt2=null;

  103.                 }
  104.                
  105.                 if(normalizeObjectIds && idReferenteObject!=null) {
  106.                     String protocollo = ProtocolFactoryManager.getInstance().getProtocolByOrganizationType(idReferenteObject.getTipo());
  107.                     IDAccordoCooperazione idAccordoCooperazione = IDAccordoCooperazioneFactory.getInstance().getIDAccordoFromValues(nomeAccordo, idReferenteObject, versione);
  108.                     accordi_list.add(DBOggettiInUsoUtils.getProtocolPrefix(protocollo)+NamingUtils.getLabelAccordoCooperazione(protocollo, idAccordoCooperazione));
  109.                 }
  110.                 else {

  111.                     StringBuilder bf = new StringBuilder();

  112.                     bf.append(idReferenteObject!=null ? idReferenteObject.getTipo() : "?");
  113.                     bf.append("/");
  114.                     bf.append(idReferenteObject!=null ? idReferenteObject.getNome() : "?");
  115.                     bf.append(":");
  116.                    
  117.                     bf.append(nomeAccordo);
  118.    
  119.                     if(idReferente>0){
  120.                         bf.append(":");
  121.                         bf.append(versione);
  122.                     }
  123.    
  124.                     accordi_list.add(bf.toString());
  125.                 }
  126.                
  127.             }
  128.             risultato.close();
  129.             stmt.close();


  130.             return isInUso;

  131.         } catch (Exception se) {

  132.             throw new UtilsException("[DBOggettiInUsoUtils::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  133.         } finally {
  134.             //Chiudo statement and resultset
  135.             JDBCUtilities.closeResources(risultato, stmt);
  136.             JDBCUtilities.closeResources(risultato2, stmt2);
  137.         }
  138.     }

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

  140.         StringBuilder bf = new StringBuilder();
  141.         if(normalizeObjectIds && idAccordo.getSoggettoReferente()!=null) {
  142.             try {
  143.                 String protocollo = ProtocolFactoryManager.getInstance().getProtocolByOrganizationType(idAccordo.getSoggettoReferente().getTipo());
  144.                 String labelAccordo = DBOggettiInUsoUtils.getProtocolPrefix(protocollo)+NamingUtils.getLabelAccordoCooperazione(protocollo, idAccordo);
  145.                 bf.append(labelAccordo);
  146.             }catch(Exception e) {
  147.                 bf.append(idAccordo.toString());
  148.             }
  149.         }
  150.         else {
  151.             bf.append(idAccordo.toString());
  152.         }

  153.         Set<ErrorsHandlerCostant> keys = whereIsInUso.keySet();
  154.         String msg = "Accordo Cooperazione '"+bf.toString() + "' non eliminabile perch&egrave; :"+separator;
  155.         if(prefix==false){
  156.             msg = "";
  157.         }
  158.         String separatorCategorie = "";
  159.         if(whereIsInUso.size()>1) {
  160.             separatorCategorie = separator;
  161.         }
  162.         for (ErrorsHandlerCostant key : keys) {
  163.             List<String> messages = whereIsInUso.get(key);

  164.             if ( messages!=null && messages.size() > 0) {
  165.                 msg += separatorCategorie;
  166.             }
  167.            
  168.             switch (key) {
  169.             case IN_USO_IN_ACCORDI:
  170.                 if ( messages!=null && messages.size() > 0) {
  171.                     msg += "riferito da API (Servizi Composti): " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  172.                 }
  173.                 break;
  174.             default:
  175.                 msg += "utilizzato in oggetto non codificato ("+key+")"+separator;
  176.                 break;
  177.             }

  178.         }// chiudo for

  179.         return msg;
  180.     }

  181. }