DriverRegistroServiziDB_soggettiDriver.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.core.registry.driver.db;

  21. import java.sql.Connection;
  22. import java.sql.Date;
  23. import java.sql.PreparedStatement;
  24. import java.sql.ResultSet;
  25. import java.sql.SQLException;
  26. import java.sql.Timestamp;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Map;

  30. import org.apache.commons.lang.StringUtils;
  31. import org.openspcoop2.core.byok.IDriverBYOK;
  32. import org.openspcoop2.core.commons.DBUtils;
  33. import org.openspcoop2.core.commons.ErrorsHandlerCostant;
  34. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  35. import org.openspcoop2.core.constants.CostantiDB;
  36. import org.openspcoop2.core.constants.ProprietariProtocolProperty;
  37. import org.openspcoop2.core.id.IDSoggetto;
  38. import org.openspcoop2.core.registry.AccordoCooperazione;
  39. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  40. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  41. import org.openspcoop2.core.registry.Connettore;
  42. import org.openspcoop2.core.registry.CredenzialiSoggetto;
  43. import org.openspcoop2.core.registry.Proprieta;
  44. import org.openspcoop2.core.registry.ProtocolProperty;
  45. import org.openspcoop2.core.registry.RuoliSoggetto;
  46. import org.openspcoop2.core.registry.Ruolo;
  47. import org.openspcoop2.core.registry.RuoloSoggetto;
  48. import org.openspcoop2.core.registry.Soggetto;
  49. import org.openspcoop2.core.registry.constants.CostantiRegistroServizi;
  50. import org.openspcoop2.core.registry.constants.CredenzialeTipo;
  51. import org.openspcoop2.core.registry.constants.StatiAccordo;
  52. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  53. import org.openspcoop2.core.registry.driver.DriverRegistroServiziNotFound;
  54. import org.openspcoop2.core.registry.driver.FiltroRicercaSoggetti;
  55. import org.openspcoop2.utils.certificate.ArchiveLoader;
  56. import org.openspcoop2.utils.certificate.ArchiveType;
  57. import org.openspcoop2.utils.certificate.Certificate;
  58. import org.openspcoop2.utils.certificate.CertificateUtils;
  59. import org.openspcoop2.utils.certificate.PrincipalType;
  60. import org.openspcoop2.utils.crypt.CryptConfig;
  61. import org.openspcoop2.utils.crypt.CryptFactory;
  62. import org.openspcoop2.utils.crypt.ICrypt;
  63. import org.openspcoop2.utils.jdbc.IJDBCAdapter;
  64. import org.openspcoop2.utils.jdbc.JDBCAdapterFactory;
  65. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  66. import org.openspcoop2.utils.sql.ISQLQueryObject;
  67. import org.openspcoop2.utils.sql.SQLObjectFactory;

  68. /**
  69.  * DriverRegistroServiziDB_soggettiDriver
  70.  *
  71.  *
  72.  * @author Sandra Giangrandi (sandra@link.it)
  73.  * @author Stefano Corallo (corallo@link.it)
  74.  * @author $Author$
  75.  * @version $Rev$, $Date$
  76.  */
  77. public class DriverRegistroServiziDB_soggettiDriver {

  78.     private DriverRegistroServiziDB driver = null;
  79.     private DriverRegistroServiziDB_protocolPropertiesDriver protocolPropertiesDriver = null;
  80.    
  81.     protected DriverRegistroServiziDB_soggettiDriver(DriverRegistroServiziDB driver) {
  82.         this.driver = driver;
  83.         this.protocolPropertiesDriver = new DriverRegistroServiziDB_protocolPropertiesDriver(driver);
  84.     }
  85.    
  86.     protected org.openspcoop2.core.registry.Soggetto getSoggetto(IDSoggetto idSoggetto) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  87.         // conrollo consistenza
  88.         if (idSoggetto == null)
  89.             throw new DriverRegistroServiziException("[getSoggetto] Parametro idSoggetto is null");

  90.         String nomeSogg = idSoggetto.getNome();
  91.         String tipoSogg = idSoggetto.getTipo();

  92.         if (nomeSogg == null || nomeSogg.trim().equals("") || tipoSogg == null || tipoSogg.trim().equals(""))
  93.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getSoggetto] : soggetto non specificato.");

  94.         long idSoggettoLong;

  95.         Connection con = null;
  96.         PreparedStatement stm = null;
  97.         ResultSet rs = null;

  98.         if (this.driver.atomica) {
  99.             try {
  100.                 con = this.driver.getConnectionFromDatasource("getSoggetto(idSoggetto)");

  101.             } catch (Exception e) {
  102.                 throw new DriverRegistroServiziException("DriverRegistroServiziDB::getSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  103.             }

  104.         } else
  105.             con = this.driver.globalConnection;

  106.         this.driver.logDebug("operazione atomica = " + this.driver.atomica);

  107.         try {
  108.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  109.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  110.             sqlQueryObject.addSelectField("id");
  111.             sqlQueryObject.addWhereCondition("nome_soggetto = ?");
  112.             sqlQueryObject.addWhereCondition("tipo_soggetto = ?");
  113.             sqlQueryObject.setANDLogicOperator(true);
  114.             String sqlQuery = sqlQueryObject.createSQLQuery();

  115.             stm = con.prepareStatement(sqlQuery);

  116.             stm.setString(1, nomeSogg);
  117.             stm.setString(2, tipoSogg);

  118.             this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, nomeSogg, tipoSogg));
  119.             rs = stm.executeQuery();

  120.             if (rs.next()) {
  121.                 idSoggettoLong = rs.getLong("id");
  122.                
  123.             }else{
  124.                 throw new DriverRegistroServiziNotFound("Nessun soggetto trovato eseguendo: "+DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, nomeSogg, tipoSogg));
  125.             }
  126.            
  127.         }catch (DriverRegistroServiziNotFound e) {
  128.             throw e;
  129.         } catch (SQLException se) {

  130.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getSoggetto] SqlException: " + se.getMessage(),se);
  131.         }catch (Exception se) {

  132.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getSoggetto] Exception: " + se.getMessage(),se);
  133.         } finally {
  134.             JDBCUtilities.closeResources(rs, stm);
  135.             this.driver.closeConnection(con);
  136.         }
  137.        
  138.         return this.getSoggetto(idSoggettoLong);
  139.     }

  140.    
  141.     protected Soggetto getSoggetto(long idSoggetto) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  142.         return getSoggetto(idSoggetto,null);
  143.     }
  144.     protected Soggetto getSoggetto(long idSoggetto,Connection conParam) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {

  145.         // conrollo consistenza
  146.         if (idSoggetto <= 0)
  147.             return null;

  148.         Soggetto soggetto = null;

  149.         Connection con = null;
  150.         PreparedStatement stm = null;
  151.         ResultSet rs = null;

  152.         if(conParam!=null){
  153.             con = conParam;
  154.         }
  155.         else if (this.driver.atomica) {
  156.             try {
  157.                 con = this.driver.getConnectionFromDatasource("getSoggetto(longId)");

  158.             } catch (Exception e) {
  159.                 throw new DriverRegistroServiziException("DriverRegistroServiziDB::getSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  160.             }

  161.         } else
  162.             con = this.driver.globalConnection;

  163.         this.driver.logDebug("operazione atomica = " + this.driver.atomica);

  164.         try {
  165.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  166.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  167.             sqlQueryObject.addSelectField("*");
  168.             sqlQueryObject.addWhereCondition("id = ?");
  169.             String sqlQuery = sqlQueryObject.createSQLQuery();

  170.             stm = con.prepareStatement(sqlQuery);

  171.             stm.setLong(1, idSoggetto);

  172.             this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, idSoggetto));
  173.             rs = stm.executeQuery();

  174.             if (rs.next()) {
  175.                 soggetto = new Soggetto();

  176.                 soggetto.setId(rs.getLong("id"));

  177.                 String nomeSogg = rs.getString("nome_soggetto");
  178.                 soggetto.setNome(nomeSogg);

  179.                 String tipoSogg = rs.getString("tipo_soggetto");
  180.                 soggetto.setTipo(tipoSogg);

  181.                 String tmp = rs.getString("descrizione");
  182.                 soggetto.setDescrizione(((tmp == null || tmp.equals("")) ? null : tmp));

  183.                 tmp = rs.getString("identificativo_porta");
  184.                 soggetto.setIdentificativoPorta(((tmp == null || tmp.equals("")) ? null : tmp));

  185.                 tmp = rs.getString("server");
  186.                 soggetto.setPortaDominio(((tmp == null || tmp.equals("")) ? null : tmp));

  187.                 tmp = rs.getString("superuser");
  188.                 soggetto.setSuperUser(((tmp == null || tmp.equals("")) ? null : tmp));

  189.                 if(rs.getInt("privato")==1)
  190.                     soggetto.setPrivato(true);
  191.                 else
  192.                     soggetto.setPrivato(false);

  193.                 tmp = rs.getString("codice_ipa");
  194.                 soggetto.setCodiceIpa(((tmp == null || tmp.equals("")) ? null : tmp));

  195.                 //  Ora Registrazione
  196.                 if(rs.getTimestamp("ora_registrazione")!=null){
  197.                     soggetto.setOraRegistrazione(new Date(rs.getTimestamp("ora_registrazione").getTime()));
  198.                 }

  199.                 long idConnettore = rs.getLong("id_connettore");
  200.                 Connettore connettore = this.driver.getConnettore(idConnettore, con);

  201.                 String profilo = rs.getString("profilo");
  202.                 if(profilo!=null){
  203.                     profilo = profilo.trim();
  204.                     soggetto.setVersioneProtocollo(profilo);
  205.                 }

  206.                 String tipoAuth = rs.getString("tipoauth");
  207.                 if(tipoAuth != null && !tipoAuth.equals("")){
  208.                     CredenzialiSoggetto credenziali = new CredenzialiSoggetto();
  209.                     credenziali.setTipo(DriverRegistroServiziDB_LIB.getEnumCredenzialeTipo(tipoAuth));
  210.                    
  211.                     credenziali.setUser(rs.getString("utente"));        
  212.                     credenziali.setPassword(rs.getString("password"));
  213.                    
  214.                     if(org.openspcoop2.core.registry.constants.CredenzialeTipo.APIKEY.equals(credenziali.getTipo())) {
  215.                         credenziali.setAppId(CostantiDB.isAPPID(rs.getString("issuer")));
  216.                     }
  217.                     else {
  218.                         credenziali.setIssuer(rs.getString("issuer"));
  219.                     }
  220.                     credenziali.setSubject(rs.getString("subject"));
  221.                     credenziali.setCnSubject(rs.getString("cn_subject"));
  222.                     credenziali.setCnIssuer(rs.getString("cn_issuer"));
  223.                     IJDBCAdapter jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(this.driver.tipoDB);
  224.                     credenziali.setCertificate(jdbcAdapter.getBinaryData(rs, "certificate"));
  225.                     int strict = rs.getInt("cert_strict_verification");
  226.                     if(strict == CostantiDB.TRUE) {
  227.                         credenziali.setCertificateStrictVerification(true);
  228.                     }
  229.                     else if(strict == CostantiDB.FALSE) {
  230.                         credenziali.setCertificateStrictVerification(false);
  231.                     }
  232.                                
  233.                     soggetto.addCredenziali( credenziali );
  234.                 }
  235.                
  236.                 // aggiungo connettore
  237.                 soggetto.setConnettore(connettore);

  238.                 // Proprieta Oggetto
  239.                 soggetto.setProprietaOggetto(DriverRegistroServiziDB_utilsDriver.readProprietaOggetto(rs,false));
  240.                
  241.                 rs.close();
  242.                 stm.close();
  243.                
  244.                
  245.                 // RUOLI
  246.                
  247.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  248.                 sqlQueryObject.addFromTable(CostantiDB.SOGGETTI_RUOLI);
  249.                 sqlQueryObject.addSelectField("*");
  250.                 sqlQueryObject.addWhereCondition("id_soggetto = ?");
  251.                 sqlQueryObject.setANDLogicOperator(true);
  252.                 sqlQuery = sqlQueryObject.createSQLQuery();
  253.                 stm = con.prepareStatement(sqlQuery);
  254.                 stm.setLong(1, soggetto.getId());
  255.                 this.driver.logDebug("eseguo query ruoli: " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, soggetto.getId()));
  256.                 rs = stm.executeQuery();
  257.                 while (rs.next()) {
  258.                     if(soggetto.getRuoli()==null){
  259.                         soggetto.setRuoli(new RuoliSoggetto());
  260.                     }  
  261.                     RuoloSoggetto ruolo = new RuoloSoggetto();
  262.                     ruolo.setId(rs.getLong("id_ruolo"));
  263.                     soggetto.getRuoli().addRuolo(ruolo);
  264.                 }
  265.                 rs.close();
  266.                 stm.close();
  267.                
  268.                 if(soggetto.getRuoli()!=null && soggetto.getRuoli().sizeRuoloList()>0){
  269.                     for (int i = 0; i < soggetto.getRuoli().sizeRuoloList(); i++) {
  270.                         Ruolo ruolo = this.driver.getRuolo(con,soggetto.getRuoli().getRuolo(i).getId());
  271.                         soggetto.getRuoli().getRuolo(i).setNome(ruolo.getNome());
  272.                     }
  273.                 }
  274.                
  275.                
  276.                 // CREDENZIALI
  277.                
  278.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  279.                 sqlQueryObject.addFromTable(CostantiDB.SOGGETTI_CREDENZIALI);
  280.                 sqlQueryObject.addSelectField("*");
  281.                 sqlQueryObject.addWhereCondition("id_soggetto = ?");
  282.                 sqlQueryObject.setANDLogicOperator(true);
  283.                 sqlQueryObject.addOrderBy("id", true);
  284.                 sqlQuery = sqlQueryObject.createSQLQuery();
  285.                 stm = con.prepareStatement(sqlQuery);
  286.                 stm.setLong(1, soggetto.getId());
  287.                 this.driver.logDebug("eseguo query credenziali: " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, soggetto.getId()));
  288.                 rs = stm.executeQuery();
  289.                 while (rs.next()) {
  290.                     CredenzialiSoggetto credenziali = new CredenzialiSoggetto();
  291.                     credenziali.setTipo(DriverRegistroServiziDB_LIB.getEnumCredenzialeTipo(tipoAuth));
  292.                    
  293.                     credenziali.setIssuer(rs.getString("issuer"));
  294.                     credenziali.setSubject(rs.getString("subject"));
  295.                     credenziali.setCnSubject(rs.getString("cn_subject"));
  296.                     credenziali.setCnIssuer(rs.getString("cn_issuer"));
  297.                     IJDBCAdapter jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(this.driver.tipoDB);
  298.                     credenziali.setCertificate(jdbcAdapter.getBinaryData(rs, "certificate"));
  299.                     int strict = rs.getInt("cert_strict_verification");
  300.                     if(strict == CostantiDB.TRUE) {
  301.                         credenziali.setCertificateStrictVerification(true);
  302.                     }
  303.                     else if(strict == CostantiDB.FALSE) {
  304.                         credenziali.setCertificateStrictVerification(false);
  305.                     }
  306.                                
  307.                     soggetto.addCredenziali( credenziali );
  308.                 }
  309.                 rs.close();
  310.                 stm.close();
  311.                
  312.                
  313.                 // PROPERTIES
  314.                
  315.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  316.                 sqlQueryObject.addFromTable(CostantiDB.SOGGETTI_PROPS);
  317.                 sqlQueryObject.addSelectField("*");
  318.                 sqlQueryObject.addWhereCondition("id_soggetto = ?");
  319.                 sqlQueryObject.setANDLogicOperator(true);
  320.                 sqlQuery = sqlQueryObject.createSQLQuery();
  321.                 stm = con.prepareStatement(sqlQuery);
  322.                 stm.setLong(1, soggetto.getId());
  323.                 this.driver.logDebug("eseguo query ruoli: " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, soggetto.getId()));
  324.                 rs = stm.executeQuery();
  325.                 while (rs.next()) {
  326.                    
  327.                     Proprieta proprieta = new Proprieta();
  328.                     proprieta.setNome(rs.getString("nome"));
  329.                    
  330.                     String plainValue = rs.getString("valore");
  331.                     String encValue = rs.getString("enc_value");
  332.                     if(encValue!=null && StringUtils.isNotEmpty(encValue)) {
  333.                         IDriverBYOK driverBYOK = this.driver.getDriverUnwrapBYOK();
  334.                         if(driverBYOK!=null) {
  335.                             proprieta.setValore(driverBYOK.unwrapAsString(encValue));
  336.                         }
  337.                         else {
  338.                             proprieta.setValore(encValue);
  339.                         }
  340.                     }
  341.                     else {
  342.                         proprieta.setValore(plainValue);
  343.                     }
  344.                    
  345.                     soggetto.addProprieta(proprieta);

  346.                 }
  347.                 rs.close();
  348.                 stm.close();
  349.                
  350.                
  351.                
  352.             }else{
  353.                 throw new DriverRegistroServiziNotFound("Nessun risultato trovato eseguendo: "+DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, idSoggetto));
  354.             }

  355.             // Protocol Properties
  356.             try{
  357.                 List<ProtocolProperty> listPP = DriverRegistroServiziDB_LIB.getListaProtocolProperty(soggetto.getId(), ProprietariProtocolProperty.SOGGETTO, con,
  358.                         this.driver.tipoDB, this.driver.getDriverUnwrapBYOK());
  359.                 if(listPP!=null && !listPP.isEmpty()){
  360.                     for (ProtocolProperty protocolProperty : listPP) {
  361.                         soggetto.addProtocolProperty(protocolProperty);
  362.                     }
  363.                 }
  364.             }catch(DriverRegistroServiziNotFound dNotFound){
  365.                 // ignore
  366.             }
  367.            
  368.             return soggetto;
  369.         } catch (SQLException se) {
  370.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getSoggetto] SqlException: " + se.getMessage(),se);
  371.         }catch (DriverRegistroServiziNotFound se) {
  372.             throw se;
  373.         }catch (Exception se) {
  374.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getSoggetto] Exception: " + se.getMessage(),se);
  375.         } finally {

  376.             //Chiudo statement and resultset
  377.             JDBCUtilities.closeResources(rs, stm);

  378.             this.driver.closeConnection(conParam, con);
  379.         }
  380.     }
  381.    
  382.    
  383.    
  384.     protected List<IDSoggetto> getAllIdSoggetti(FiltroRicercaSoggetti filtroRicerca) throws DriverRegistroServiziException,DriverRegistroServiziNotFound{
  385.         Connection con = null;
  386.         PreparedStatement stm = null;
  387.         ResultSet rs = null;

  388.         this.driver.logDebug("getAllIdSoggettiRegistro...");

  389.         Certificate certificatoFiltro = null;
  390.         try {
  391.             this.driver.logDebug("operazione atomica = " + this.driver.atomica);
  392.             // prendo la connessione dal pool
  393.             if (this.driver.atomica)
  394.                 con = this.driver.getConnectionFromDatasource("getAllIdSoggetti");
  395.             else
  396.                 con = this.driver.globalConnection;

  397.             boolean testInChiaro = false;
  398.             ICrypt crypt = null;
  399.             if(filtroRicerca!=null && filtroRicerca.getCredenzialiSoggetto()!=null && filtroRicerca.getCredenzialiSoggetto().getPassword()!=null){
  400.                 CredenzialeTipo cTipo = filtroRicerca.getCredenzialiSoggetto().getTipo();
  401.                 if(CredenzialeTipo.BASIC.equals(cTipo)){
  402.                     CryptConfig config = filtroRicerca.getCryptConfig();
  403.                     if(config==null || config.isBackwardCompatibility()) {
  404.                         testInChiaro = true;
  405.                     }
  406.                     if(config!=null) {
  407.                         try {
  408.                             crypt = CryptFactory.getCrypt(this.driver.log, config);
  409.                         }catch(Exception e) {
  410.                             throw new DriverRegistroServiziException(e.getMessage(),e);
  411.                         }
  412.                     }
  413.                 }
  414.                 else if(CredenzialeTipo.APIKEY.equals(cTipo)){
  415.                     CryptConfig config = filtroRicerca.getCryptConfig();
  416.                     if(config!=null) {
  417.                         try {
  418.                             crypt = CryptFactory.getCrypt(this.driver.log, config);
  419.                         }catch(Exception e) {
  420.                             throw new DriverRegistroServiziException(e.getMessage(),e);
  421.                         }
  422.                     }
  423.                     else {
  424.                         testInChiaro = true;
  425.                     }
  426.                 }
  427.             }
  428.            
  429.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  430.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  431.             if(filtroRicerca!=null){
  432.                 if(filtroRicerca.getIdRuolo()!=null){
  433.                     sqlQueryObject.addFromTable(CostantiDB.SOGGETTI_RUOLI);
  434.                     sqlQueryObject.addFromTable(CostantiDB.RUOLI);
  435.                 }
  436.             }
  437.             sqlQueryObject.setSelectDistinct(true);
  438.             sqlQueryObject.addSelectField("tipo_soggetto");
  439.             sqlQueryObject.addSelectField("nome_soggetto");
  440.             sqlQueryObject.setSelectDistinct(true);
  441.             if(filtroRicerca!=null){
  442.                 // Filtro By Data
  443.                 if(filtroRicerca.getMinDate()!=null)
  444.                     sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".ora_registrazione > ?");
  445.                 if(filtroRicerca.getMaxDate()!=null)
  446.                     sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".ora_registrazione < ?");
  447.                 // Filtro By Tipo e Nome
  448.                 if(filtroRicerca.getTipo()!=null)
  449.                     sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".tipo_soggetto = ?");
  450.                 if(filtroRicerca.getNome()!=null)
  451.                     sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".nome_soggetto = ?");
  452.                 // Filtro By Pdd
  453.                 if(filtroRicerca.getNomePdd()!=null)
  454.                     sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".server = ?");
  455.                 // Filtro By Ruoli
  456.                 if(filtroRicerca.getIdRuolo()!=null){
  457.                     sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI_RUOLI+".id_soggetto="+CostantiDB.SOGGETTI+".id");
  458.                     sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI_RUOLI+".id_ruolo="+CostantiDB.RUOLI+".id");
  459.                     sqlQueryObject.addWhereCondition(CostantiDB.RUOLI+".nome = ?");
  460.                 }
  461.                 // Filtro By Credenziali (per ssl vengono cercate solo nelle credenziali principali)
  462.                 if(filtroRicerca.getCredenzialiSoggetto()!=null){
  463.                     if(filtroRicerca.getCredenzialiSoggetto().getTipo()!=null){
  464.                         sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".tipoauth = ?");
  465.                         if(CredenzialeTipo.APIKEY.equals(filtroRicerca.getCredenzialiSoggetto().getTipo())){
  466.                             sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".cert_strict_verification = ?");
  467.                         }
  468.                     }
  469.                     if(filtroRicerca.getCredenzialiSoggetto().getUser()!=null){
  470.                         sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".utente = ?");
  471.                     }
  472.                     if(filtroRicerca.getCredenzialiSoggetto().getPassword()!=null){
  473.                         //sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".password = ?");
  474.                         sqlQueryObject.addSelectField(CostantiDB.SOGGETTI+".password");
  475.                     }
  476.                     if(filtroRicerca.getCredenzialiSoggetto().getSubject()!=null){
  477.                        
  478.                         // Autenticazione SSL deve essere LIKE
  479.                         Map<String, List<String>> hashSubject = CertificateUtils.getPrincipalIntoMap(filtroRicerca.getCredenzialiSoggetto().getSubject(), PrincipalType.SUBJECT);
  480.                         Map<String, List<String>> hashIssuer = null;
  481.                         if(filtroRicerca.getCredenzialiSoggetto().getIssuer()!=null) {
  482.                             hashIssuer = CertificateUtils.getPrincipalIntoMap(filtroRicerca.getCredenzialiSoggetto().getIssuer(), PrincipalType.ISSUER);
  483.                         }
  484.                        
  485.                         for (String key : hashSubject.keySet()) {
  486.                             List<String> listValues = hashSubject.get(key);
  487.                             for (String value : listValues) {
  488.                                 sqlQueryObject.addWhereLikeCondition(CostantiDB.SOGGETTI+".subject", "/"+CertificateUtils.formatKeyPrincipal(key)+"="+CertificateUtils.formatValuePrincipal(value)+"/", true, true, false);
  489.                             }
  490.                            
  491.                             sqlQueryObject.addSelectField("subject");
  492.                         }
  493.                        
  494.                         if(hashIssuer!=null) {
  495.                             for (String key : hashIssuer.keySet()) {
  496.                                 List<String> listValues = hashIssuer.get(key);
  497.                                 for (String value : listValues) {
  498.                                     sqlQueryObject.addWhereLikeCondition(CostantiDB.SOGGETTI+".issuer", "/"+CertificateUtils.formatKeyPrincipal(key)+"="+CertificateUtils.formatValuePrincipal(value)+"/", true, true, false);
  499.                                 }
  500.                             }
  501.                            
  502.                             sqlQueryObject.addSelectField("issuer");
  503.                         }
  504.                         else {
  505.                             sqlQueryObject.addWhereIsNullCondition("issuer");
  506.                         }
  507.                        
  508.                     }
  509.                     if(filtroRicerca.getCredenzialiSoggetto().getCertificate()!=null){
  510.                         sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".cn_subject = ?");
  511.                         sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".cn_issuer = ?");
  512.                         sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".cert_strict_verification = ?");
  513.                        
  514.                         sqlQueryObject.addSelectField("certificate");
  515.                     }
  516.                 }
  517.                 DBUtils.setPropertiesForSearch(sqlQueryObject, filtroRicerca.getProprieta(), CostantiDB.SOGGETTI, CostantiDB.SOGGETTI_PROPS, "nome", "valore", "id_soggetto");
  518.                 this.protocolPropertiesDriver.setProtocolPropertiesForSearch(sqlQueryObject, filtroRicerca, CostantiDB.SOGGETTI);
  519.             }

  520.             sqlQueryObject.setANDLogicOperator(true);
  521.             String sqlQuery = sqlQueryObject.createSQLQuery();
  522.             this.driver.logDebug("eseguo query : " + sqlQuery );
  523.             stm = con.prepareStatement(sqlQuery);
  524.             int indexStmt = 1;
  525.             if(filtroRicerca!=null){
  526.                 if(filtroRicerca.getMinDate()!=null){
  527.                     this.driver.logDebug("minDate stmt.setTimestamp("+filtroRicerca.getMinDate()+")");
  528.                     stm.setTimestamp(indexStmt, new Timestamp(filtroRicerca.getMinDate().getTime()));
  529.                     indexStmt++;
  530.                 }
  531.                 if(filtroRicerca.getMaxDate()!=null){
  532.                     this.driver.logDebug("maxDate stmt.setTimestamp("+filtroRicerca.getMaxDate()+")");
  533.                     stm.setTimestamp(indexStmt, new Timestamp(filtroRicerca.getMaxDate().getTime()));
  534.                     indexStmt++;
  535.                 }  
  536.                 if(filtroRicerca.getTipo()!=null){
  537.                     this.driver.logDebug("tipoSoggetto stmt.setString("+filtroRicerca.getTipo()+")");
  538.                     stm.setString(indexStmt, filtroRicerca.getTipo());
  539.                     indexStmt++;
  540.                 }
  541.                 if(filtroRicerca.getNome()!=null){
  542.                     this.driver.logDebug("nomeSoggetto stmt.setString("+filtroRicerca.getNome()+")");
  543.                     stm.setString(indexStmt, filtroRicerca.getNome());
  544.                     indexStmt++;
  545.                 }  
  546.                 if(filtroRicerca.getNomePdd()!=null){
  547.                     this.driver.logDebug("nomePdD stmt.setString("+filtroRicerca.getNomePdd()+")");
  548.                     stm.setString(indexStmt, filtroRicerca.getNomePdd());
  549.                     indexStmt++;
  550.                 }
  551.                 if(filtroRicerca.getIdRuolo()!=null){
  552.                     this.driver.logDebug("ruolo stmt.setString("+filtroRicerca.getIdRuolo().getNome()+")");
  553.                     stm.setString(indexStmt, filtroRicerca.getIdRuolo().getNome());
  554.                     indexStmt++;
  555.                 }
  556.                 if(filtroRicerca.getCredenzialiSoggetto()!=null){
  557.                     if(filtroRicerca.getCredenzialiSoggetto().getTipo()!=null){
  558.                         this.driver.logDebug("credenziali.tipo stmt.setString("+filtroRicerca.getCredenzialiSoggetto().getTipo().getValue()+")");
  559.                         stm.setString(indexStmt, filtroRicerca.getCredenzialiSoggetto().getTipo().getValue());
  560.                         indexStmt++;
  561.                         if(CredenzialeTipo.APIKEY.equals(filtroRicerca.getCredenzialiSoggetto().getTipo())){
  562.                             int v = filtroRicerca.getCredenzialiSoggetto().isCertificateStrictVerification() ?  CostantiDB.TRUE :  CostantiDB.FALSE;
  563.                             this.driver.logDebug("credenziali.certificateStrictVerification stmt.setInt("+v+")");
  564.                             stm.setInt(indexStmt, v);
  565.                             indexStmt++;
  566.                         }
  567.                     }
  568.                     if(filtroRicerca.getCredenzialiSoggetto().getUser()!=null){
  569.                         this.driver.logDebug("credenziali.user stmt.setString("+filtroRicerca.getCredenzialiSoggetto().getUser()+")");
  570.                         stm.setString(indexStmt, filtroRicerca.getCredenzialiSoggetto().getUser());
  571.                         indexStmt++;
  572.                     }
  573. //                  if(filtroRicerca.getCredenzialiSoggetto().getPassword()!=null){
  574. //                      this.driver.logDebug("credenziali.password stmt.setString("+filtroRicerca.getCredenzialiSoggetto().getPassword()+")");
  575. //                      stm.setString(indexStmt, filtroRicerca.getCredenzialiSoggetto().getPassword());
  576. //                      indexStmt++;
  577. //                  }
  578.                     if(filtroRicerca.getCredenzialiSoggetto().getSubject()!=null){
  579.                         // nop;
  580.                     }
  581.                     if(filtroRicerca.getCredenzialiSoggetto().getCertificate()!=null){
  582.                        
  583.                         certificatoFiltro = ArchiveLoader.load(ArchiveType.CER, filtroRicerca.getCredenzialiSoggetto().getCertificate(), 0, null);  
  584.                         String cnSubject = certificatoFiltro.getCertificate().getSubject().getCN();
  585.                         String cnIssuer = certificatoFiltro.getCertificate().getIssuer().getCN();
  586.                        
  587.                         stm.setString(indexStmt++, cnSubject);
  588.                         this.driver.logDebug("credenziali.cnSubject stmt.setString(" + cnSubject +")");
  589.                         stm.setString(indexStmt++, cnIssuer);
  590.                         this.driver.logDebug("credenziali.cnIssuer stmt.setString(" + cnIssuer +")");
  591.                         if(filtroRicerca.getCredenzialiSoggetto().isCertificateStrictVerification()) {
  592.                             this.driver.logDebug("credenziali.strict stmt.setInt(" + CostantiDB.TRUE +")");
  593.                             stm.setInt(indexStmt++, CostantiDB.TRUE);
  594.                         }
  595.                         else {
  596.                             this.driver.logDebug("credenziali.strict stmt.setInt(" + CostantiDB.FALSE +")");
  597.                             stm.setInt(indexStmt++, CostantiDB.FALSE);
  598.                         }
  599.                     }
  600.                 }
  601.                 DBUtils.setPropertiesForSearch(stm, indexStmt, filtroRicerca.getProprieta(), this.driver.tipoDB, this.driver.log);
  602.                 this.protocolPropertiesDriver.setProtocolPropertiesForSearch(stm, indexStmt, filtroRicerca, ProprietariProtocolProperty.SOGGETTO);
  603.             }
  604.             rs = stm.executeQuery();
  605.             List<IDSoggetto> idSoggetti = new ArrayList<IDSoggetto>();
  606.             IJDBCAdapter jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(this.driver.tipoDB);
  607.             while (rs.next()) {
  608.                
  609.                 if(filtroRicerca!=null &&
  610.                         filtroRicerca.getCredenzialiSoggetto()!=null &&
  611.                         filtroRicerca.getCredenzialiSoggetto().getPassword()!=null){
  612.                     String passwordDB =  rs.getString("password");
  613.                    
  614.                     boolean found = false;
  615.                     if(testInChiaro) {
  616.                         found = filtroRicerca.getCredenzialiSoggetto().getPassword().equals(passwordDB);
  617.                     }
  618.                     if(!found && crypt!=null) {
  619.                         found = crypt.check(filtroRicerca.getCredenzialiSoggetto().getPassword(), passwordDB);
  620.                     }
  621.                     if( !found ) {
  622.                         continue;
  623.                     }
  624.                 }
  625.                
  626.                 if(filtroRicerca!=null &&
  627.                         filtroRicerca.getCredenzialiSoggetto()!=null &&
  628.                         filtroRicerca.getCredenzialiSoggetto().getSubject()!=null){
  629.                
  630.                     // Possono esistere piu' soggetti che hanno una porzione di subject uguale, devo quindi verificare che sia proprio quello che cerco
  631.                                        
  632.                     String subjectPotenziale =  rs.getString("subject");
  633.                     boolean subjectValid = CertificateUtils.sslVerify(subjectPotenziale, filtroRicerca.getCredenzialiSoggetto().getSubject(), PrincipalType.SUBJECT, this.driver.log);
  634.                    
  635.                     boolean issuerValid = true;
  636.                     if(filtroRicerca.getCredenzialiSoggetto().getIssuer()!=null) {
  637.                         String issuerPotenziale =  rs.getString("issuer");
  638.                         if(StringUtils.isNotEmpty(issuerPotenziale)) {
  639.                             issuerValid = CertificateUtils.sslVerify(issuerPotenziale, filtroRicerca.getCredenzialiSoggetto().getIssuer(), PrincipalType.ISSUER, this.driver.log);
  640.                         }
  641.                         else {
  642.                             issuerValid = false;
  643.                         }
  644.                     }
  645.                    
  646.                     if( !subjectValid || !issuerValid ) {
  647.                         continue;
  648.                     }
  649.                 }
  650.                
  651.                 if(certificatoFiltro!=null) {

  652.                     // ricerca per certificato
  653.                     // Possono esistere piu' soggetti che hanno un CN con subject e issuer diverso.
  654.                    
  655.                     byte[] certificatoBytes = jdbcAdapter.getBinaryData(rs, "certificate");
  656.                     Certificate certificato = ArchiveLoader.load(ArchiveType.CER, certificatoBytes, 0, null);
  657.                     //int tmpStrict = rs.getInt("cert_strict_verification");
  658.                     //boolean strict = tmpStrict == CostantiDB.TRUE;
  659.                    
  660.                     if(!certificatoFiltro.getCertificate().equals(certificato.getCertificate(),filtroRicerca.getCredenzialiSoggetto().isCertificateStrictVerification())) {
  661.                         continue;
  662.                     }
  663.                 }
  664.                                
  665.                 IDSoggetto idS = new IDSoggetto(rs.getString("tipo_soggetto"),rs.getString("nome_soggetto"));
  666.                 idSoggetti.add(idS);
  667.             }
  668.             if(idSoggetti.size()==0){
  669.                 if(filtroRicerca!=null)
  670.                     throw new DriverRegistroServiziNotFound("Soggetti non trovati che rispettano il filtro di ricerca selezionato: "+filtroRicerca.toString());
  671.                 else
  672.                     throw new DriverRegistroServiziNotFound("Soggetti non trovati");
  673.             }else{
  674.                 return idSoggetti;
  675.             }
  676.         }catch(DriverRegistroServiziNotFound de){
  677.             throw de;
  678.         }
  679.         catch(Exception e){
  680.             throw new DriverRegistroServiziException("getAllIdSoggettiRegistro error",e);
  681.         } finally {

  682.             //Chiudo statement and resultset
  683.             JDBCUtilities.closeResources(rs, stm);

  684.             this.driver.closeConnection(con);

  685.         }
  686.     }

  687.     protected void createSoggetto(org.openspcoop2.core.registry.Soggetto soggetto) throws DriverRegistroServiziException {
  688.         if (soggetto == null)
  689.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::createSoggetto] Parametro non valido.");

  690.         Connection con = null;
  691.         boolean error = false;

  692.         if (this.driver.atomica) {
  693.             try {
  694.                 con = this.driver.getConnectionFromDatasource("createSoggetto");
  695.                 con.setAutoCommit(false);
  696.             } catch (Exception e) {
  697.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::createSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  698.             }

  699.         } else
  700.             con = this.driver.globalConnection;

  701.         this.driver.logDebug("operazione atomica = " + this.driver.atomica);

  702.         try {
  703.             this.driver.logDebug("CRUDSoggetto type = 1");
  704.             // creo soggetto
  705.             DriverRegistroServiziDB_soggettiLIB.CRUDSoggetto(1, soggetto, con, this.driver.tipoDB, this.driver.getDriverWrapBYOK());

  706.         } catch (Exception qe) {
  707.             error = true;
  708.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::createSoggetto] Errore durante la creazione del soggetto : " + qe.getMessage(), qe);
  709.         } finally {

  710.             this.driver.closeConnection(error,con);
  711.         }
  712.     }

  713.     protected boolean existsSoggetto(IDSoggetto idSoggetto) throws DriverRegistroServiziException {
  714.         boolean exist = false;
  715.         Connection con = null;
  716.        
  717.         if (this.driver.atomica) {
  718.             try {
  719.                 con = this.driver.getConnectionFromDatasource("existsSoggetto(idSoggetto)");
  720.             } catch (Exception e) {
  721.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::existsSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  722.             }

  723.         } else
  724.             con = this.driver.globalConnection;

  725.         try {
  726.             exist = existsSoggetto(con, idSoggetto);

  727.         } catch (Exception e) {
  728.             exist = false;
  729.             this.driver.log.error("Errore durante verifica esistenza soggetto :", e);
  730.         } finally {

  731.             this.driver.closeConnection(con);
  732.         }

  733.         return exist;
  734.     }
  735.     protected boolean existsSoggetto(Connection conParam, IDSoggetto idSoggetto) throws DriverRegistroServiziException {
  736.         boolean exist = false;
  737.         PreparedStatement stm = null;
  738.         ResultSet rs = null;

  739.         if (idSoggetto == null)
  740.             throw new DriverRegistroServiziException("Parametro non valido");

  741.         String nome_soggetto = idSoggetto.getNome();
  742.         String tipo_soggetto = idSoggetto.getTipo();
  743.         if (nome_soggetto == null || nome_soggetto.equals(""))
  744.             throw new DriverRegistroServiziException("Parametro Nome non valido");
  745.         if (tipo_soggetto == null || tipo_soggetto.equals(""))
  746.             throw new DriverRegistroServiziException("Parametro Tipo non valido");

  747.         try {
  748.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  749.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  750.             sqlQueryObject.addSelectField("*");
  751.             sqlQueryObject.addWhereCondition("nome_soggetto = ?");
  752.             sqlQueryObject.addWhereCondition("tipo_soggetto = ?");
  753.             sqlQueryObject.setANDLogicOperator(true);
  754.             String sqlQuery = sqlQueryObject.createSQLQuery();
  755.             stm = conParam.prepareStatement(sqlQuery);
  756.             stm.setString(1, nome_soggetto);
  757.             stm.setString(2, tipo_soggetto);
  758.             rs = stm.executeQuery();
  759.             if (rs.next())
  760.                 exist = true;
  761.             rs.close();
  762.             stm.close();

  763.         } catch (Exception e) {
  764.             exist = false;
  765.             this.driver.log.error("Errore durante verifica esistenza soggetto :", e);
  766.         } finally {

  767.             //Chiudo statement and resultset
  768.             JDBCUtilities.closeResources(rs, stm);

  769.         }

  770.         return exist;
  771.     }

  772.     protected boolean existsSoggetto(long idSoggetto) throws DriverRegistroServiziException {
  773.         boolean exist = false;
  774.         Connection con = null;
  775.         PreparedStatement stm = null;
  776.         ResultSet rs = null;
  777.         if (this.driver.atomica) {
  778.             try {
  779.                 con = this.driver.getConnectionFromDatasource("existsSoggetto(longId)");
  780.             } catch (Exception e) {
  781.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::existsSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  782.             }

  783.         } else
  784.             con = this.driver.globalConnection;

  785.         try {
  786.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  787.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  788.             sqlQueryObject.addSelectField("*");
  789.             sqlQueryObject.addWhereCondition("id = ?");
  790.             String sqlQuery = sqlQueryObject.createSQLQuery();
  791.             stm = con.prepareStatement(sqlQuery);
  792.             stm.setLong(1, idSoggetto);
  793.             rs = stm.executeQuery();
  794.             if (rs.next())
  795.                 exist = true;
  796.             rs.close();
  797.             stm.close();

  798.         } catch (Exception e) {
  799.             exist = false;
  800.             this.driver.log.error("Errore durante verifica esistenza soggetto :", e);
  801.         } finally {
  802.             //Chiudo statement and resultset
  803.             JDBCUtilities.closeResources(rs, stm);
  804.             this.driver.closeConnection(con);
  805.         }

  806.         return exist;
  807.     }

  808.     protected boolean existsSoggetto(String codiceIPA) throws DriverRegistroServiziException {
  809.         boolean exist = false;
  810.         Connection con = null;
  811.         PreparedStatement stm = null;
  812.         ResultSet rs = null;

  813.         if (codiceIPA == null|| codiceIPA.equals(""))
  814.             throw new DriverRegistroServiziException("Parametro non valido");

  815.         if (this.driver.atomica) {
  816.             try {
  817.                 con = this.driver.getConnectionFromDatasource("existsSoggetto(codiceIPA)");
  818.             } catch (Exception e) {
  819.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::existsSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  820.             }

  821.         } else
  822.             con = this.driver.globalConnection;

  823.         try {
  824.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  825.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  826.             sqlQueryObject.addSelectField("*");
  827.             sqlQueryObject.addWhereCondition("codice_ipa = ?");
  828.             sqlQueryObject.setANDLogicOperator(true);
  829.             String sqlQuery = sqlQueryObject.createSQLQuery();
  830.             stm = con.prepareStatement(sqlQuery);
  831.             stm.setString(1, codiceIPA);
  832.             rs = stm.executeQuery();
  833.             if (rs.next())
  834.                 exist = true;
  835.             rs.close();
  836.             stm.close();

  837.         } catch (Exception e) {
  838.             exist = false;
  839.             this.driver.log.error("Errore durante verifica esistenza soggetto :", e);
  840.         } finally {

  841.             //Chiudo statement and resultset
  842.             JDBCUtilities.closeResources(rs, stm);

  843.             this.driver.closeConnection(con);
  844.         }

  845.         return exist;
  846.     }

  847.     protected Soggetto getSoggetto(String codiceIPA) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  848.         Connection con = null;
  849.         PreparedStatement stm = null;
  850.         ResultSet rs = null;
  851.         long idSoggetto = -1;

  852.         if (codiceIPA == null|| codiceIPA.equals(""))
  853.             throw new DriverRegistroServiziException("Parametro non valido");

  854.         if (this.driver.atomica) {
  855.             try {
  856.                 con = this.driver.getConnectionFromDatasource("getSoggetto(codiceIPA)");
  857.             } catch (Exception e) {
  858.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::existsSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  859.             }

  860.         } else
  861.             con = this.driver.globalConnection;

  862.         try {
  863.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  864.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  865.             sqlQueryObject.addSelectField("*");
  866.             sqlQueryObject.addWhereCondition("codice_ipa = ?");
  867.             sqlQueryObject.setANDLogicOperator(true);
  868.             String sqlQuery = sqlQueryObject.createSQLQuery();
  869.             stm = con.prepareStatement(sqlQuery);
  870.             stm.setString(1, codiceIPA);
  871.             rs = stm.executeQuery();
  872.             if (rs.next()){
  873.                 idSoggetto = rs.getLong("id");
  874.             }
  875.             rs.close();
  876.             stm.close();

  877.         } catch (Exception e) {
  878.             this.driver.log.error("Errore durante verifica esistenza soggetto :", e);
  879.         } finally {

  880.             //Chiudo statement and resultset
  881.             JDBCUtilities.closeResources(rs, stm);

  882.             this.driver.closeConnection(con);
  883.         }

  884.         if(idSoggetto<=0){
  885.             throw new DriverRegistroServiziNotFound("Soggetto con Codice IPA ["+codiceIPA+"] non trovato");
  886.         }
  887.         else{
  888.             return this.getSoggetto(idSoggetto);
  889.         }

  890.     }

  891.     protected String getCodiceIPA(IDSoggetto idSoggetto) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  892.         Connection con = null;
  893.         PreparedStatement stm = null;
  894.         ResultSet rs = null;
  895.         String codiceIPA = null;
  896.         if (idSoggetto == null)
  897.             throw new DriverRegistroServiziException("Parametro non valido");

  898.         String nome_soggetto = idSoggetto.getNome();
  899.         String tipo_soggetto = idSoggetto.getTipo();
  900.         if (nome_soggetto == null || nome_soggetto.equals(""))
  901.             throw new DriverRegistroServiziException("Parametro Nome non valido");
  902.         if (tipo_soggetto == null || tipo_soggetto.equals(""))
  903.             throw new DriverRegistroServiziException("Parametro Tipo non valido");

  904.         if (this.driver.atomica) {
  905.             try {
  906.                 con = this.driver.getConnectionFromDatasource("getCodiceIPA(idSoggetto)");
  907.             } catch (Exception e) {
  908.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::existsSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  909.             }

  910.         } else
  911.             con = this.driver.globalConnection;

  912.         try {
  913.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  914.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  915.             sqlQueryObject.addSelectField("*");
  916.             sqlQueryObject.addWhereCondition("tipo_soggetto = ?");
  917.             sqlQueryObject.addWhereCondition("nome_soggetto = ?");
  918.             sqlQueryObject.setANDLogicOperator(true);
  919.             String sqlQuery = sqlQueryObject.createSQLQuery();
  920.             stm = con.prepareStatement(sqlQuery);
  921.             stm.setString(1, tipo_soggetto);
  922.             stm.setString(2, nome_soggetto);
  923.             rs = stm.executeQuery();
  924.             if (rs.next()){
  925.                 codiceIPA = rs.getString("codice_ipa");
  926.             }
  927.             rs.close();
  928.             stm.close();

  929.         } catch (Exception e) {
  930.             this.driver.log.error("Errore durante verifica esistenza soggetto :", e);
  931.         } finally {

  932.             //Chiudo statement and resultset
  933.             JDBCUtilities.closeResources(rs, stm);

  934.             this.driver.closeConnection(con);
  935.         }

  936.         if(codiceIPA==null){
  937.             throw new DriverRegistroServiziNotFound("Soggetto ["+tipo_soggetto+"/"+nome_soggetto+"] non trovato");
  938.         }
  939.         else{
  940.             return codiceIPA;
  941.         }

  942.     }

  943.     protected void updateSoggetto(org.openspcoop2.core.registry.Soggetto soggetto) throws DriverRegistroServiziException {
  944.         if (soggetto == null)
  945.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updateSoggetto] Parametro non valido.");

  946.         PreparedStatement stm=null;
  947.         ResultSet rs=null;
  948.         Connection con = null;
  949.         boolean error = false;

  950.         if (this.driver.atomica) {
  951.             try {
  952.                 con = this.driver.getConnectionFromDatasource("updateSoggetto");
  953.                 con.setAutoCommit(false);
  954.             } catch (Exception e) {
  955.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updateSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  956.             }

  957.         } else
  958.             con = this.driver.globalConnection;

  959.         this.driver.logDebug("operazione atomica = " + this.driver.atomica);

  960.         try {

  961.             /**
  962.              * L'aggiornamente del tipo/nome soggetto scatena anche l'aggiornamento del nome
  963.              * dei connettori oltre che del soggetto stesso anche dei fruitori di servizi in cui il soggetto puo essere
  964.              * o l'erogatore o il fruitore
  965.              */
  966.             String oldNomeSoggetto=null;
  967.             String oldTipoSoggetto=null;
  968.             if(soggetto.getOldIDSoggettoForUpdate()!=null){
  969.                 oldNomeSoggetto = soggetto.getOldIDSoggettoForUpdate().getNome();
  970.                 oldTipoSoggetto = soggetto.getOldIDSoggettoForUpdate().getTipo();
  971.             }
  972.             String nomeSoggetto=soggetto.getNome();
  973.             String tipoSoggetto=soggetto.getTipo();

  974.             if(tipoSoggetto==null || tipoSoggetto.equals("")) throw new DriverRegistroServiziException("Parametro Tipo Soggetto non valido.");
  975.             if(nomeSoggetto==null || nomeSoggetto.equals("")) throw new DriverRegistroServiziException("Parametro Nome Soggetto non valido.");

  976.             if(oldNomeSoggetto==null || oldNomeSoggetto.equals("")) oldNomeSoggetto=nomeSoggetto;
  977.             if(oldTipoSoggetto==null || oldTipoSoggetto.equals("")) oldTipoSoggetto=tipoSoggetto;

  978.             //se tipo o nome sono cambiati effettuo modifiche su connettore
  979.             if(!tipoSoggetto.equals(oldTipoSoggetto) || !nomeSoggetto.equals(oldNomeSoggetto)){

  980.                 //pattern nome connettore servizio fruitore
  981.                 // CNT_SF_tipo/nome(fruitore del servizio)+tipo/nome(erogatore del servizio)+tipo/nome(servizio)
  982.                 // "CNT_SF_"  + tipoFruitore  + "/" + nomeFruitore+"_"+  tipoErogatore+"/"+nomeErogatore +"_"+ tiposervizio + "/" + nomeservizio
  983.                 //devo modificare la prima e la seconda parte del pattern
  984.                 String regex = "CNT_SF_(.*)\\/(.*)_(.*)\\/(.*)_(.*)\\/(.*)";

  985.                 //recupero i connettori da modificare dove il soggetto e' erogatore
  986.                 //0 soggetti
  987.                 //1 servizi
  988.                 //2 servizi_fruitori
  989.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  990.                 sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  991.                 sqlQueryObject.addFromTable(CostantiDB.SERVIZI);
  992.                 sqlQueryObject.addFromTable(CostantiDB.SERVIZI_FRUITORI);
  993.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI+".tipo_soggetto");
  994.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI+".nome_soggetto");
  995.                 sqlQueryObject.addSelectField(CostantiDB.SERVIZI_FRUITORI+".id_connettore");
  996.                 sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI_FRUITORI+".id_servizio = "+CostantiDB.SERVIZI+".id");
  997.                 sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI+".id_soggetto = "+CostantiDB.SOGGETTI+".id");
  998.                 sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".tipo_soggetto = ?");
  999.                 sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".nome_soggetto = ?");
  1000.                 sqlQueryObject.setANDLogicOperator(true);
  1001.                 String sqlQuery = sqlQueryObject.createSQLQuery();
  1002.                 stm=con.prepareStatement(sqlQuery);
  1003.                 stm.setString(1, oldTipoSoggetto);
  1004.                 stm.setString(2, oldNomeSoggetto);
  1005.                 rs=stm.executeQuery();
  1006.                 while(rs.next()){
  1007.                     long idConnettore=rs.getLong("id_connettore");
  1008.                     Connettore connettore = this.driver.getConnettore(idConnettore, con);
  1009.                     String oldNomeConnettore=connettore.getNome();
  1010.                     //controllo se il nome connettore matcha la regex
  1011.                     if(oldNomeConnettore.matches(regex)){
  1012.                         this.driver.logDebug("Tento aggiornamento connettore id: ["+idConnettore+"] oldNome: ["+oldNomeConnettore+"]...");
  1013.                         //splitto la stringa in modo da prendermi i valori separatamente
  1014.                         //l'array sara composto da [CNT,SF,tipo/nomeFruitore,tipo/nomeErogatore,tipo/nomeServizio]
  1015.                         String[] val=oldNomeConnettore.split("_");
  1016.                         //i=2 contiene il tipo/nome del soggetto fruitore
  1017.                         //i=3 contiene il tipo/nome del soggetto erogatore (che bisogna cambiare)
  1018.                         //i=4 contiene il tipo/nome del servizio
  1019.                         String newNomeConnettore = "CNT_SF_"+val[2]+ tipoSoggetto+"/"+nomeSoggetto +val[4];
  1020.                         this.driver.logDebug("nuovo nome connettore ["+newNomeConnettore+"]");
  1021.                         connettore.setNome(newNomeConnettore);
  1022.                         //aggiorno il connettore
  1023.                         DriverRegistroServiziDB_connettoriLIB.CRUDConnettore(CostantiDB.UPDATE, connettore, con, this.driver.getDriverWrapBYOK());
  1024.                     }
  1025.                 }
  1026.                 rs.close();
  1027.                 stm.close();
  1028.                 //recupero i connettori da modificare dove il soggetto e' fruitore
  1029.                 //0 soggetti
  1030.                 //1 servizi_fruitori
  1031.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  1032.                 sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  1033.                 sqlQueryObject.addFromTable(CostantiDB.SERVIZI_FRUITORI);
  1034.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI+".tipo_soggetto");
  1035.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI+".nome_soggetto");
  1036.                 sqlQueryObject.addSelectField(CostantiDB.SERVIZI_FRUITORI+".id_connettore");
  1037.                 sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI_FRUITORI+".id_soggetto = "+CostantiDB.SOGGETTI+".id");
  1038.                 sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".tipo_soggetto = ?");
  1039.                 sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI+".nome_soggetto = ?");
  1040.                 sqlQueryObject.setANDLogicOperator(true);
  1041.                 sqlQuery = sqlQueryObject.createSQLQuery();
  1042.                 stm=con.prepareStatement(sqlQuery);
  1043.                 stm.setString(1, oldTipoSoggetto);
  1044.                 stm.setString(2, oldNomeSoggetto);
  1045.                 rs=stm.executeQuery();
  1046.                 while(rs.next()){
  1047.                     long idConnettore=rs.getLong("id_connettore");
  1048.                     Connettore connettore = this.driver.getConnettore(idConnettore, con);
  1049.                     String oldNomeConnettore=connettore.getNome();
  1050.                     //controllo se il nome connettore matcha la regex
  1051.                     if(oldNomeConnettore.matches(regex)){
  1052.                         this.driver.logDebug("Tento aggiornamento connettore id: ["+idConnettore+"] oldNome: ["+oldNomeConnettore+"]...");
  1053.                         //splitto la stringa in modo da prendermi i valori separatamente
  1054.                         //l'array sara composto da [CNT,SF,tipo/nomeFruitore,tipo/nomeErogatore,tipo/nomeServizio]
  1055.                         String[] val=oldNomeConnettore.split("_");
  1056.                         //i=2 contiene il tipo/nome del soggetto fruitore (che bisogna cambiare)
  1057.                         //i=3 contiene il tipo/nome del soggetto erogatore
  1058.                         //i=4 contiene il tipo/nome del servizio
  1059.                         String newNomeConnettore = "CNT_SF_"+tipoSoggetto+"/"+nomeSoggetto+val[3]+val[4];
  1060.                         this.driver.logDebug("nuovo nome connettore ["+newNomeConnettore+"]");
  1061.                         connettore.setNome(newNomeConnettore);
  1062.                         //aggiorno il connettore
  1063.                         DriverRegistroServiziDB_connettoriLIB.CRUDConnettore(CostantiDB.UPDATE, connettore, con, this.driver.getDriverWrapBYOK());
  1064.                     }
  1065.                 }
  1066.                 rs.close();
  1067.                 stm.close();
  1068.             }

  1069.             // UPDATE soggetto
  1070.             DriverRegistroServiziDB_soggettiLIB.CRUDSoggetto(2, soggetto, con, this.driver.tipoDB, this.driver.getDriverWrapBYOK());

  1071.         } catch (Exception qe) {
  1072.             error = true;
  1073.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updateSoggetto] Errore durante l'aggiornamento del soggetto : " + qe.getMessage(),qe);
  1074.         } finally {

  1075.             JDBCUtilities.closeResources(rs, stm);

  1076.             this.driver.closeConnection(error,con);
  1077.         }
  1078.     }

  1079.     protected void deleteSoggetto(org.openspcoop2.core.registry.Soggetto soggetto) throws DriverRegistroServiziException {
  1080.         if (soggetto == null)
  1081.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::deleteSoggetto] Parametro non valido.");

  1082.         Connection con = null;
  1083.         boolean error = false;

  1084.         if (this.driver.atomica) {
  1085.             try {
  1086.                 con = this.driver.getConnectionFromDatasource("deleteSoggetto");
  1087.                 con.setAutoCommit(false);
  1088.             } catch (Exception e) {
  1089.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::deleteSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  1090.             }

  1091.         } else
  1092.             con = this.driver.globalConnection;

  1093.         this.driver.logDebug("operazione atomica = " + this.driver.atomica);

  1094.         try {
  1095.             this.driver.logDebug("CRUDSoggetto type = 3");
  1096.             // DELETE soggetto
  1097.             DriverRegistroServiziDB_soggettiLIB.CRUDSoggetto(3, soggetto, con, this.driver.tipoDB, this.driver.getDriverWrapBYOK());

  1098.         } catch (Exception qe) {
  1099.             error = true;
  1100.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::deleteSoggetto] Errore durante l'eliminazione del soggetto : " + qe.getMessage(),qe);
  1101.         } finally {

  1102.             this.driver.closeConnection(error,con);
  1103.         }
  1104.     }

  1105.     protected IDSoggetto[] getSoggettiWithSuperuser(String user) throws DriverRegistroServiziException {

  1106.         IDSoggetto [] idSoggetti = null;

  1107.         Connection con = null;
  1108.         PreparedStatement stm = null;
  1109.         ResultSet rs = null;
  1110.         String sqlQuery = "";

  1111.         if (this.driver.atomica) {
  1112.             try {
  1113.                 con = this.driver.getConnectionFromDatasource("getSoggettiWithSuperuser");

  1114.             } catch (Exception e) {
  1115.                 throw new DriverRegistroServiziException("[DriverRegistroServiziException::getSoggettiWithSuperuser] Exception accedendo al datasource :" + e.getMessage(),e);

  1116.             }

  1117.         } else
  1118.             con = this.driver.globalConnection;

  1119.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  1120.         try {
  1121.             List<IDSoggetto> idTrovati = new ArrayList<IDSoggetto>();
  1122.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  1123.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  1124.             sqlQueryObject.addSelectField("tipo_soggetto");
  1125.             sqlQueryObject.addSelectField("nome_soggetto");
  1126.             sqlQueryObject.addWhereCondition("superuser = ?");
  1127.             sqlQuery = sqlQueryObject.createSQLQuery();

  1128.             stm = con.prepareStatement(sqlQuery);

  1129.             stm.setString(1, user);

  1130.             this.driver.logDebug("eseguo query : " + DBUtils.formatSQLString(sqlQuery, CostantiRegistroServizi.ABILITATO));
  1131.             rs = stm.executeQuery();

  1132.             // prendo il primo router se c'e' altrimenti lancio eccezione.
  1133.             while (rs.next()) {
  1134.                 IDSoggetto id = new IDSoggetto();
  1135.                 id.setTipo(rs.getString("tipo_soggetto"));
  1136.                 id.setNome(rs.getString("nome_soggetto"));
  1137.                 idTrovati.add(id);
  1138.             }

  1139.             if(idTrovati.size()>0){
  1140.                 idSoggetti =  new IDSoggetto[1];
  1141.                 idSoggetti = idTrovati.toArray(idSoggetti);
  1142.             }

  1143.         } catch (SQLException se) {
  1144.             throw new DriverRegistroServiziException("[DriverRegistroServiziException::getSoggettiWithSuperuser] SqlException: " + se.getMessage(),se);
  1145.         }catch (Exception se) {
  1146.             throw new DriverRegistroServiziException("[DriverRegistroServiziException::getSoggettiWithSuperuser] Exception: " + se.getMessage(),se);
  1147.         } finally {
  1148.             //Chiudo statement and resultset
  1149.             JDBCUtilities.closeResources(rs, stm);
  1150.             this.driver.closeConnection(con);
  1151.         }
  1152.         return idSoggetti;
  1153.     }
  1154.    
  1155.     protected List<String> nomiProprietaSoggetti(List<String> tipoSoggettiProtocollo) throws DriverConfigurazioneException {
  1156.         String queryString;

  1157.         Connection con = null;
  1158.         PreparedStatement stmt=null;
  1159.         ResultSet risultato=null;
  1160.         ArrayList<String> lista = new ArrayList<>();

  1161.         if (this.driver.atomica) {
  1162.             try {
  1163.                 con = this.driver.getConnectionFromDatasource("nomiProprietaSoggetti");
  1164.             } catch (Exception e) {
  1165.                 throw new DriverConfigurazioneException("[DriverRegistroServiziDB::nomiProprietaSoggetti] Exception accedendo al datasource :" + e.getMessage(),e);

  1166.             }

  1167.         } else
  1168.             con = this.driver.globalConnection;

  1169.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  1170.         try {

  1171.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  1172.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI_PROPS);
  1173.             sqlQueryObject.setSelectDistinct(true);
  1174.             sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_PROPS + ".nome");
  1175.            
  1176.             if((tipoSoggettiProtocollo != null && tipoSoggettiProtocollo.size() > 0)) {
  1177.                 sqlQueryObject.addWhereCondition(CostantiDB.SOGGETTI_PROPS + ".id_soggetto = "+CostantiDB.SOGGETTI+".id");
  1178.                 sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  1179.                 String [] tipiServiziProtocolloS = tipoSoggettiProtocollo.toArray(new String[tipoSoggettiProtocollo.size()]);
  1180.                 sqlQueryObject.addWhereINCondition(CostantiDB.SOGGETTI+".tipo_soggetto", true, tipiServiziProtocolloS);
  1181.             }
  1182.            
  1183.             sqlQueryObject.addOrderBy(CostantiDB.SOGGETTI_PROPS + ".nome");
  1184.             sqlQueryObject.setSortType(true);
  1185.             sqlQueryObject.setANDLogicOperator(true);
  1186.            
  1187.             queryString = sqlQueryObject.createSQLQuery();
  1188.             stmt = con.prepareStatement(queryString);
  1189.             risultato = stmt.executeQuery();
  1190.             while (risultato.next()) {
  1191.                 lista.add(risultato.getString("nome"));
  1192.             }
  1193.             return lista;
  1194.         } catch (Exception qe) {
  1195.             throw new DriverConfigurazioneException("[DriverRegistroServiziDB::nomiProprietaSoggetti] Errore : " + qe.getMessage(),qe);
  1196.         } finally {
  1197.             //Chiudo statement and resultset
  1198.             JDBCUtilities.closeResources(risultato, stmt);
  1199.             this.driver.closeConnection(con);
  1200.         }
  1201.     }
  1202.    
  1203.     protected boolean isSoggettoInUsoInPackageFinali(Soggetto ss, Map<ErrorsHandlerCostant,String> whereIsInUso) throws DriverRegistroServiziException {
  1204.         return isSoggettoInUso(ss,whereIsInUso,true,false);
  1205.     }
  1206.     protected boolean isSoggettoInUsoInPackagePubblici(Soggetto ss, Map<ErrorsHandlerCostant,String> whereIsInUso) throws DriverRegistroServiziException {
  1207.         return isSoggettoInUso(ss,whereIsInUso,false,true);
  1208.     }
  1209.     protected boolean isSoggettoInUso(Soggetto ss, Map<ErrorsHandlerCostant,String> whereIsInUso) throws DriverRegistroServiziException {
  1210.         return isSoggettoInUso(ss,whereIsInUso,false,false);
  1211.     }
  1212.     private boolean isSoggettoInUso(Soggetto ss, Map<ErrorsHandlerCostant,String> whereIsInUso,boolean checkOnlyStatiFinali,boolean checkOnlyStatiPubblici) throws DriverRegistroServiziException {
  1213.         String nomeMetodo = "isSoggettoInUso";
  1214.         Connection con = null;
  1215.         PreparedStatement stmt = null;
  1216.         ResultSet risultato = null;

  1217.         if (this.driver.atomica) {
  1218.             try {
  1219.                 con = this.driver.getConnectionFromDatasource("isSoggettoInUso");

  1220.             } catch (Exception e) {
  1221.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  1222.             }

  1223.         } else
  1224.             con = this.driver.globalConnection;

  1225.         this.driver.logDebug("operazione this.atomica = " + this.driver.atomica);

  1226.         try {
  1227.             boolean isInUso = false;
  1228.             List<String> nomiServizi = new ArrayList<>();
  1229.             List<String> serviziFruitori = new ArrayList<>();
  1230.             List<String> accordi = new ArrayList<>();
  1231.             List<String> accordi_cooperazione = new ArrayList<>();
  1232.             List<String> partecipanti = new ArrayList<>();

  1233.             //controllo se in uso in servizi
  1234.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  1235.             sqlQueryObject.addFromTable(CostantiDB.SERVIZI);
  1236.             sqlQueryObject.addSelectField("*");
  1237.             sqlQueryObject.addWhereCondition("id_soggetto = ?");
  1238.             if(checkOnlyStatiFinali){
  1239.                 sqlQueryObject.addWhereCondition("stato = ?");
  1240.             }
  1241.             if(checkOnlyStatiPubblici){
  1242.                 sqlQueryObject.addWhereCondition("privato = ?");
  1243.             }
  1244.             sqlQueryObject.setANDLogicOperator(true);
  1245.             String queryString = sqlQueryObject.createSQLQuery();
  1246.             stmt = con.prepareStatement(queryString);
  1247.             stmt.setLong(1, ss.getId());
  1248.             int index = 2;
  1249.             if(checkOnlyStatiFinali){
  1250.                 stmt.setString(index, StatiAccordo.finale.toString());
  1251.                 index++;
  1252.             }
  1253.             if(checkOnlyStatiPubblici){
  1254.                 stmt.setInt(index, 0);
  1255.                 index++;
  1256.             }
  1257.             risultato = stmt.executeQuery();
  1258.             while (risultato.next()){
  1259.                 isInUso=true;
  1260.                 String nomeServizio = risultato.getString("tipo_servizio")+"/"+risultato.getString("nome_servizio");
  1261.                 nomiServizi.add(nomeServizio);
  1262.             }
  1263.             risultato.close();
  1264.             stmt.close();

  1265.             if(nomiServizi.size()>0) whereIsInUso.put(ErrorsHandlerCostant.IN_USO_IN_SERVIZI, nomiServizi.toString());

  1266.             // controllo se in uso in fruitori
  1267.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  1268.             sqlQueryObject.addFromTable(CostantiDB.SERVIZI_FRUITORI);
  1269.             sqlQueryObject.addSelectField("*");
  1270.             sqlQueryObject.addWhereCondition("id_soggetto = ?");
  1271.             if(checkOnlyStatiFinali){
  1272.                 sqlQueryObject.addWhereCondition("stato = ?");
  1273.             }
  1274.             sqlQueryObject.setANDLogicOperator(true);
  1275.             queryString = sqlQueryObject.createSQLQuery();
  1276.             stmt = con.prepareStatement(queryString);
  1277.             stmt.setLong(1, ss.getId());
  1278.             if(checkOnlyStatiFinali){
  1279.                 stmt.setString(2, StatiAccordo.finale.toString());
  1280.             }
  1281.             risultato = stmt.executeQuery();
  1282.             while (risultato.next()){
  1283.                 isInUso=true;
  1284.                 AccordoServizioParteSpecifica servizio = this.driver.getAccordoServizioParteSpecifica(risultato.getLong("id_servizio"));
  1285.                 String uriServizio = this.driver.idServizioFactory.getUriFromAccordo(servizio);
  1286.                 if(checkOnlyStatiPubblici){
  1287.                     if(servizio.getPrivato()==null || servizio.getPrivato()==false){
  1288.                         serviziFruitori.add(uriServizio);
  1289.                     }
  1290.                 }else{
  1291.                     serviziFruitori.add(uriServizio);
  1292.                 }      
  1293.             }
  1294.             risultato.close();
  1295.             stmt.close();

  1296.             if(serviziFruitori.size()>0) whereIsInUso.put(ErrorsHandlerCostant.POSSIEDE_FRUITORI, serviziFruitori.toString());


  1297.             //controllo se referente
  1298.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  1299.             sqlQueryObject.addFromTable(CostantiDB.ACCORDI);
  1300.             sqlQueryObject.addSelectField("*");
  1301.             sqlQueryObject.addWhereCondition("id_referente = ?");
  1302.             if(checkOnlyStatiFinali){
  1303.                 sqlQueryObject.addWhereCondition("stato = ?");
  1304.             }
  1305.             if(checkOnlyStatiPubblici){
  1306.                 sqlQueryObject.addWhereCondition("privato = ?");
  1307.             }
  1308.             sqlQueryObject.setANDLogicOperator(true);
  1309.             queryString = sqlQueryObject.createSQLQuery();
  1310.             stmt = con.prepareStatement(queryString);
  1311.             stmt.setLong(1, ss.getId());
  1312.             index = 2;
  1313.             if(checkOnlyStatiFinali){
  1314.                 stmt.setString(index, StatiAccordo.finale.toString());
  1315.                 index++;
  1316.             }
  1317.             if(checkOnlyStatiPubblici){
  1318.                 stmt.setInt(index, 0);
  1319.                 index++;
  1320.             }
  1321.             risultato = stmt.executeQuery();
  1322.             while (risultato.next()){
  1323.                 isInUso=true;
  1324.                 AccordoServizioParteComune accordo = this.driver.getAccordoServizioParteComune(risultato.getLong("id"));
  1325.                 accordi.add(this.driver.idAccordoFactory.getUriFromAccordo(accordo));
  1326.             }
  1327.             risultato.close();
  1328.             stmt.close();

  1329.             if(accordi.size()>0) whereIsInUso.put(ErrorsHandlerCostant.IS_REFERENTE, accordi.toString());


  1330.             //controllo se referente in accordi cooperazione
  1331.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  1332.             sqlQueryObject.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE);
  1333.             sqlQueryObject.addSelectField("*");
  1334.             sqlQueryObject.addWhereCondition("id_referente = ?");
  1335.             if(checkOnlyStatiFinali){
  1336.                 sqlQueryObject.addWhereCondition("stato = ?");
  1337.             }
  1338.             if(checkOnlyStatiPubblici){
  1339.                 sqlQueryObject.addWhereCondition("privato = ?");
  1340.             }
  1341.             sqlQueryObject.setANDLogicOperator(true);
  1342.             queryString = sqlQueryObject.createSQLQuery();
  1343.             stmt = con.prepareStatement(queryString);
  1344.             stmt.setLong(1, ss.getId());
  1345.             index = 2;
  1346.             if(checkOnlyStatiFinali){
  1347.                 stmt.setString(index, StatiAccordo.finale.toString());
  1348.                 index++;
  1349.             }
  1350.             if(checkOnlyStatiPubblici){
  1351.                 stmt.setInt(index, 0);
  1352.                 index++;
  1353.             }
  1354.             risultato = stmt.executeQuery();
  1355.             while (risultato.next()){
  1356.                 isInUso=true;
  1357.                 AccordoCooperazione accordo = this.driver.getAccordoCooperazione(risultato.getLong("id"));
  1358.                 accordi_cooperazione.add(this.driver.idAccordoCooperazioneFactory.getUriFromAccordo(accordo));
  1359.             }
  1360.             risultato.close();
  1361.             stmt.close();

  1362.             if(accordi_cooperazione.size()>0) whereIsInUso.put(ErrorsHandlerCostant.IS_REFERENTE_COOPERAZIONE, accordi_cooperazione.toString());


  1363.             //controllo se partecipante in cooperazione
  1364.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  1365.             sqlQueryObject.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI);
  1366.             sqlQueryObject.addSelectField("*");
  1367.             sqlQueryObject.addWhereCondition("id_soggetto = ?");
  1368.             sqlQueryObject.setANDLogicOperator(true);
  1369.             queryString = sqlQueryObject.createSQLQuery();
  1370.             stmt = con.prepareStatement(queryString);
  1371.             stmt.setLong(1, ss.getId());
  1372.             risultato = stmt.executeQuery();
  1373.             while (risultato.next()){
  1374.                 AccordoCooperazione accordo = this.driver.getAccordoCooperazione(risultato.getLong("id_accordo_cooperazione"));
  1375.                 if(checkOnlyStatiFinali){
  1376.                     if(StatiAccordo.finale.toString().equals(accordo.getStatoPackage())){
  1377.                         isInUso=true;
  1378.                         partecipanti.add(this.driver.idAccordoCooperazioneFactory.getUriFromAccordo(accordo));
  1379.                     }
  1380.                 }else if(checkOnlyStatiPubblici){
  1381.                     if(accordo.getPrivato()==null || accordo.getPrivato()==false){
  1382.                         isInUso=true;
  1383.                         partecipanti.add(this.driver.idAccordoCooperazioneFactory.getUriFromAccordo(accordo));
  1384.                     }
  1385.                 }else{
  1386.                     isInUso=true;
  1387.                     partecipanti.add(this.driver.idAccordoCooperazioneFactory.getUriFromAccordo(accordo));
  1388.                 }
  1389.             }
  1390.             risultato.close();
  1391.             stmt.close();

  1392.             if(partecipanti.size()>0) whereIsInUso.put(ErrorsHandlerCostant.IS_PARTECIPANTE_COOPERAZIONE, partecipanti.toString());


  1393.             return isInUso;

  1394.         } catch (Exception se) {

  1395.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  1396.         } finally {
  1397.             //Chiudo statement and resultset
  1398.             JDBCUtilities.closeResources(risultato, stmt);
  1399.             this.driver.closeConnection(con);
  1400.         }
  1401.     }

  1402.    
  1403.     protected IDSoggetto getIdSoggetto(long idSoggetto) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  1404.         return getIdSoggetto(idSoggetto,null);
  1405.     }
  1406.     protected IDSoggetto getIdSoggetto(long idSoggetto,Connection conParam) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {

  1407.         // conrollo consistenza
  1408.         if (idSoggetto <= 0)
  1409.             return null;

  1410.         IDSoggetto idSoggettoObject = null;

  1411.         Connection con = null;
  1412.         PreparedStatement stm = null;
  1413.         ResultSet rs = null;

  1414.         if(conParam!=null){
  1415.             con = conParam;
  1416.         }
  1417.         else if (this.driver.atomica) {
  1418.             try {
  1419.                 con = this.driver.getConnectionFromDatasource("getIdSoggetto(longId)");

  1420.             } catch (Exception e) {
  1421.                 throw new DriverRegistroServiziException("DriverRegistroServiziDB::getIdSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  1422.             }

  1423.         } else
  1424.             con = this.driver.globalConnection;

  1425.         this.driver.logDebug("operazione atomica = " + this.driver.atomica);

  1426.         try {
  1427.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  1428.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  1429.             sqlQueryObject.addSelectField("tipo_soggetto");
  1430.             sqlQueryObject.addSelectField("nome_soggetto");
  1431.             sqlQueryObject.addWhereCondition("id = ?");
  1432.             String sqlQuery = sqlQueryObject.createSQLQuery();

  1433.             stm = con.prepareStatement(sqlQuery);

  1434.             stm.setLong(1, idSoggetto);

  1435.             this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, idSoggetto));
  1436.             rs = stm.executeQuery();

  1437.             if (rs.next()) {
  1438.                 idSoggettoObject = new IDSoggetto();

  1439.                 // String tmp = rs.getString("nomeprov");
  1440.                 // soggetto.setNome(( (tmp==null || tmp.equals("") ) ?
  1441.                 // null : tmp));
  1442.                 idSoggettoObject.setNome(rs.getString("nome_soggetto"));
  1443.                 // tmp = rs.getString("tipoprov");
  1444.                 // soggetto.setTipo(( (tmp==null || tmp.equals("") ) ?
  1445.                 // null : tmp));
  1446.                 idSoggettoObject.setTipo(rs.getString("tipo_soggetto"));

  1447.             }else{
  1448.                 throw new DriverRegistroServiziNotFound("Nessun risultato trovato eseguendo: "+DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, idSoggetto));
  1449.             }

  1450.             return idSoggettoObject;
  1451.         } catch (SQLException se) {
  1452.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getIdSoggetto] SqlException: " + se.getMessage(),se);
  1453.         }catch (DriverRegistroServiziNotFound se) {
  1454.             throw se;
  1455.         }catch (Exception se) {
  1456.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getIdSoggetto] Exception: " + se.getMessage(),se);
  1457.         } finally {

  1458.             //Chiudo statement and resultset
  1459.             JDBCUtilities.closeResources(rs, stm);

  1460.             this.driver.closeConnection(conParam, con);
  1461.         }
  1462.     }
  1463.    
  1464.    
  1465.    
  1466.     protected List<IDSoggetto> getSoggettiDefault() throws DriverRegistroServiziException {
  1467.         return getSoggettiDefault(null);
  1468.     }
  1469.     protected List<IDSoggetto> getSoggettiDefault(Connection conParam) throws DriverRegistroServiziException {

  1470.         List<IDSoggetto> soggettiDefault = new ArrayList<IDSoggetto>();

  1471.         Connection con = null;
  1472.         PreparedStatement stm = null;
  1473.         ResultSet rs = null;

  1474.         if(conParam!=null){
  1475.             con = conParam;
  1476.         }
  1477.         else if (this.driver.atomica) {
  1478.             try {
  1479.                 con = this.driver.getConnectionFromDatasource("getSoggettiDefault()");

  1480.             } catch (Exception e) {
  1481.                 throw new DriverRegistroServiziException("DriverRegistroServiziDB::getSoggettiDefault] Exception accedendo al datasource :" + e.getMessage(),e);

  1482.             }

  1483.         } else
  1484.             con = this.driver.globalConnection;

  1485.         this.driver.logDebug("operazione atomica = " + this.driver.atomica);

  1486.         try {
  1487.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  1488.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  1489.             sqlQueryObject.addSelectField("tipo_soggetto");
  1490.             sqlQueryObject.addSelectField("nome_soggetto");
  1491.             sqlQueryObject.addWhereCondition("is_default = ?");
  1492.             sqlQueryObject.setANDLogicOperator(true);
  1493.             String sqlQuery = sqlQueryObject.createSQLQuery();

  1494.             stm = con.prepareStatement(sqlQuery);

  1495.             stm.setInt(1, CostantiDB.TRUE);

  1496.             this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, CostantiDB.TRUE));
  1497.             rs = stm.executeQuery();

  1498.             while (rs.next()) {
  1499.                
  1500.                 IDSoggetto idSoggetto = new IDSoggetto(rs.getString("tipo_soggetto"), rs.getString("nome_soggetto"));
  1501.                 soggettiDefault.add(idSoggetto);

  1502.             }
  1503.            
  1504.             if(soggettiDefault.isEmpty()) {
  1505.                 throw new DriverRegistroServiziNotFound("Nessun risultato trovato eseguendo: "+DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, CostantiDB.TRUE));
  1506.             }

  1507.             return soggettiDefault;
  1508.         } catch (SQLException se) {
  1509.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getSoggettiDefault] SqlException: " + se.getMessage(),se);
  1510.         }catch (Exception se) {
  1511.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getSoggettiDefault] Exception: " + se.getMessage(),se);
  1512.         } finally {

  1513.             //Chiudo statement and resultset
  1514.             JDBCUtilities.closeResources(rs, stm);

  1515.             this.driver.closeConnection(conParam, con);
  1516.         }
  1517.     }
  1518. }