DriverRegistroServiziDB_pddDriver.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.openspcoop2.core.commons.ISearch;
  31. import org.openspcoop2.core.commons.Liste;
  32. import org.openspcoop2.core.constants.CostantiDB;
  33. import org.openspcoop2.core.registry.PortaDominio;
  34. import org.openspcoop2.core.registry.Soggetto;
  35. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  36. import org.openspcoop2.core.registry.driver.DriverRegistroServiziNotFound;
  37. import org.openspcoop2.core.registry.driver.FiltroRicerca;
  38. import org.openspcoop2.utils.certificate.CertificateUtils;
  39. import org.openspcoop2.utils.certificate.PrincipalType;
  40. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  41. import org.openspcoop2.utils.sql.ISQLQueryObject;
  42. import org.openspcoop2.utils.sql.SQLObjectFactory;

  43. /**
  44.  * DriverRegistroServiziDB_pddDriver
  45.  *
  46.  *
  47.  * @author Sandra Giangrandi (sandra@link.it)
  48.  * @author Stefano Corallo (corallo@link.it)
  49.  * @author $Author$
  50.  * @version $Rev$, $Date$
  51.  */
  52. public class DriverRegistroServiziDB_pddDriver {

  53.     private DriverRegistroServiziDB driver = null;
  54.    
  55.     protected DriverRegistroServiziDB_pddDriver(DriverRegistroServiziDB driver) {
  56.         this.driver = driver;
  57.     }
  58.    
  59.     protected org.openspcoop2.core.registry.PortaDominio getPortaDominio(String nomePdD) throws DriverRegistroServiziException,DriverRegistroServiziNotFound{

  60.         this.driver.logDebug("richiesto getPortaDominio: " + nomePdD);
  61.         // conrollo consistenza
  62.         if (nomePdD == null)
  63.             throw new DriverRegistroServiziException("[getPortaDominio] Parametro nomePdD is null");
  64.         if (nomePdD.trim().equals(""))
  65.             throw new DriverRegistroServiziException("[getPortaDominio] Parametro nomePdD non e' definito");

  66.         Connection con = null;
  67.         PreparedStatement stm = null;
  68.         ResultSet rs = null;

  69.         if (this.driver.atomica) {
  70.             try {
  71.                 con = this.driver.getConnectionFromDatasource("getPortaDominio(nome)");

  72.             } catch (Exception e) {
  73.                 throw new DriverRegistroServiziException("DriverRegistroServiziDB::getPortaDominio] Exception accedendo al datasource :" + e.getMessage(),e);

  74.             }

  75.         } else
  76.             con = this.driver.globalConnection;

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

  78.         try {

  79.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  80.             sqlQueryObject.addFromTable(CostantiDB.PDD);
  81.             sqlQueryObject.addSelectField("*");
  82.             sqlQueryObject.addWhereCondition("nome = ?");
  83.             String queryString = sqlQueryObject
  84.                     .createSQLQuery();
  85.             stm = con.prepareStatement(queryString);
  86.             stm.setString(1, nomePdD);
  87.             rs = stm.executeQuery();
  88.             PortaDominio pdd = null;
  89.             if (rs.next()) {
  90.                 pdd = new PortaDominio();
  91.                 pdd.setId(rs.getLong("id"));
  92.                 pdd.setNome(rs.getString("nome"));
  93.                 pdd.setDescrizione(rs.getString("descrizione"));
  94.                 pdd.setImplementazione(rs.getString("implementazione"));
  95.                 pdd.setSubject(rs.getString("subject"));
  96.                 pdd.setClientAuth(DriverRegistroServiziDB_LIB.getEnumStatoFunzionalita(rs.getString("client_auth")));
  97.                 pdd.setSuperUser(rs.getString("superuser"));

  98.                 // Ora Registrazione
  99.                 if(rs.getTimestamp("ora_registrazione")!=null){
  100.                     pdd.setOraRegistrazione(new Date(rs.getTimestamp("ora_registrazione").getTime()));
  101.                 }

  102.             } else {
  103.                 throw new DriverRegistroServiziNotFound("[DriverRegistroServiziDB::getPortaDominio] rs.next non ha restituito valori con la seguente interrogazione :\n" + DriverRegistroServiziDB_LIB.formatSQLString(queryString, nomePdD));
  104.             }

  105.             return pdd;

  106.         }catch (DriverRegistroServiziNotFound e) {
  107.             throw e;
  108.         } catch (SQLException se) {

  109.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getPortaDominio] SqlException: " + se.getMessage(),se);
  110.         }catch (Exception se) {

  111.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getPortaDominio] Exception: " + se.getMessage(),se);
  112.         } finally {
  113.             JDBCUtilities.closeResources(rs, stm);
  114.             this.driver.closeConnection(con);
  115.         }
  116.     }


  117.     protected List<String> getAllIdPorteDominio(FiltroRicerca filtroRicerca) throws DriverRegistroServiziException,DriverRegistroServiziNotFound{

  118.         Connection con = null;
  119.         PreparedStatement stm = null;
  120.         ResultSet rs = null;

  121.         this.driver.logDebug("getAllIdPorteDominio...");

  122.         try {
  123.             this.driver.logDebug("operazione atomica = " + this.driver.atomica);
  124.             // prendo la connessione dal pool
  125.             if (this.driver.atomica)
  126.                 con = this.driver.getConnectionFromDatasource("getAllIdPorteDominio");
  127.             else
  128.                 con = this.driver.globalConnection;

  129.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  130.             sqlQueryObject.addFromTable(CostantiDB.PDD);
  131.             sqlQueryObject.addSelectField("nome");
  132.             if(filtroRicerca!=null){
  133.                 // Filtro By Data
  134.                 if(filtroRicerca.getMinDate()!=null)
  135.                     sqlQueryObject.addWhereCondition("ora_registrazione > ?");
  136.                 if(filtroRicerca.getMaxDate()!=null)
  137.                     sqlQueryObject.addWhereCondition("ora_registrazione < ?");
  138.                 if(filtroRicerca.getNome()!=null)
  139.                     sqlQueryObject.addWhereCondition("nome = ?");
  140.                 if(filtroRicerca.getTipo()!=null)
  141.                     sqlQueryObject.addWhereCondition("tipo = ?");
  142.             }

  143.             sqlQueryObject.setANDLogicOperator(true);
  144.             String sqlQuery = sqlQueryObject.createSQLQuery();
  145.             this.driver.logDebug("eseguo query : " + sqlQuery );
  146.             stm = con.prepareStatement(sqlQuery);
  147.             int indexStmt = 1;
  148.             if(filtroRicerca!=null){
  149.                 if(filtroRicerca.getMinDate()!=null){
  150.                     this.driver.logDebug("minDate stmt.setTimestamp("+filtroRicerca.getMinDate()+")");
  151.                     stm.setTimestamp(indexStmt, new Timestamp(filtroRicerca.getMinDate().getTime()));
  152.                     indexStmt++;
  153.                 }
  154.                 if(filtroRicerca.getMaxDate()!=null){
  155.                     this.driver.logDebug("maxDate stmt.setTimestamp("+filtroRicerca.getMaxDate()+")");
  156.                     stm.setTimestamp(indexStmt, new Timestamp(filtroRicerca.getMaxDate().getTime()));
  157.                     indexStmt++;
  158.                 }  
  159.                 if(filtroRicerca.getNome()!=null){
  160.                     this.driver.logDebug("nome stmt.setString("+filtroRicerca.getNome()+")");
  161.                     stm.setString(indexStmt, filtroRicerca.getNome());
  162.                     indexStmt++;
  163.                 }  
  164.                 if(filtroRicerca.getTipo()!=null){
  165.                     this.driver.logDebug("tipoPdd stmt.setString("+filtroRicerca.getTipo()+")");
  166.                     stm.setString(indexStmt, filtroRicerca.getTipo());
  167.                     indexStmt++;
  168.                 }
  169.             }
  170.             rs = stm.executeQuery();
  171.             List<String> nomiPdd = new ArrayList<>();
  172.             while (rs.next()) {
  173.                 nomiPdd.add(rs.getString("nome"));
  174.             }
  175.             if(nomiPdd.size()==0){
  176.                 if(filtroRicerca!=null)
  177.                     throw new DriverRegistroServiziNotFound("Porte di Dominio non trovate che rispettano il filtro di ricerca selezionato: "+filtroRicerca.toString());
  178.                 else
  179.                     throw new DriverRegistroServiziNotFound("Porte di Dominio non trovate");
  180.             }else{
  181.                 return nomiPdd;
  182.             }
  183.         }catch(DriverRegistroServiziNotFound de){
  184.             throw de;
  185.         }
  186.         catch(Exception e){
  187.             throw new DriverRegistroServiziException("getAllIdPorteDominio error",e);
  188.         } finally {

  189.             //Chiudo statement and resultset
  190.             JDBCUtilities.closeResources(rs, stm);

  191.             this.driver.closeConnection(con);

  192.         }
  193.     }



  194.     protected void createPortaDominio(PortaDominio pdd) throws DriverRegistroServiziException{
  195.         if (pdd == null)
  196.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::createPortaDominio] Parametro non valido.");

  197.         Connection con = null;
  198.         boolean error = false;

  199.         if (this.driver.atomica) {
  200.             try {
  201.                 con = this.driver.getConnectionFromDatasource("createPortaDominio");
  202.                 con.setAutoCommit(false);
  203.             } catch (Exception e) {
  204.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::createPortaDominio] Exception accedendo al datasource :" + e.getMessage(),e);

  205.             }

  206.         } else
  207.             con = this.driver.globalConnection;

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

  209.         try {
  210.             this.driver.logDebug("CRUDPortaDominio type = 1");
  211.             DriverRegistroServiziDB_soggettiLIB.CRUDPortaDominio(CostantiDB.CREATE, pdd, con);

  212.         } catch (Exception qe) {
  213.             error = true;
  214.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::createPortaDominio] Errore durante la creazione della porta di dominio : " + qe.getMessage(), qe);
  215.         } finally {

  216.             this.driver.closeConnection(error,con);
  217.         }
  218.     }

  219.     protected boolean existsPortaDominio(String nome) throws DriverRegistroServiziException{
  220.         boolean exist = false;
  221.         Connection con = null;
  222.         PreparedStatement stm = null;
  223.         ResultSet rs = null;

  224.         if (nome == null)
  225.             throw new DriverRegistroServiziException("Parametro non valido");

  226.         if (nome.equals(""))
  227.             throw new DriverRegistroServiziException("Parametro vuoto non valido");

  228.         if (this.driver.atomica) {
  229.             try {
  230.                 con = this.driver.getConnectionFromDatasource("existsPortaDominio");
  231.             } catch (Exception e) {
  232.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::existsPortaDominio] Exception accedendo al datasource :" + e.getMessage(),e);

  233.             }

  234.         } else
  235.             con = this.driver.globalConnection;

  236.         try {
  237.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  238.             sqlQueryObject.addFromTable(CostantiDB.PDD);
  239.             sqlQueryObject.addSelectField("*");
  240.             sqlQueryObject.addWhereCondition("nome = ?");
  241.             sqlQueryObject.setANDLogicOperator(true);
  242.             String sqlQuery = sqlQueryObject.createSQLQuery();
  243.             stm = con.prepareStatement(sqlQuery);
  244.             stm.setString(1, nome);
  245.             rs = stm.executeQuery();
  246.             if (rs.next())
  247.                 exist = true;
  248.             rs.close();
  249.             stm.close();

  250.         } catch (Exception e) {
  251.             exist = false;
  252.             this.driver.log.error("Errore durante verifica esistenza porta di dominio :", e);
  253.         } finally {

  254.             //Chiudo statement and resultset
  255.             JDBCUtilities.closeResources(rs, stm);

  256.             this.driver.closeConnection(con);
  257.         }

  258.         return exist;
  259.     }

  260.     protected void updatePortaDominio(PortaDominio pdd) throws DriverRegistroServiziException{
  261.         if (pdd == null)
  262.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updatePortaDominio] Parametro non valido.");

  263.         Connection con = null;
  264.         boolean error = false;

  265.         if (this.driver.atomica) {
  266.             try {
  267.                 con = this.driver.getConnectionFromDatasource("updatePortaDominio");
  268.                 con.setAutoCommit(false);
  269.             } catch (Exception e) {
  270.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updatePortaDominio] Exception accedendo al datasource :" + e.getMessage(),e);

  271.             }

  272.         } else
  273.             con = this.driver.globalConnection;

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

  275.         try {

  276.             this.driver.logDebug("CRUDPortaDominio type = 2");
  277.             DriverRegistroServiziDB_soggettiLIB.CRUDPortaDominio(CostantiDB.UPDATE, pdd, con);

  278.         } catch (Exception qe) {
  279.             error = true;
  280.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updatePortaDominio] Errore durante l'aggiornamento della porta di dominio : " + qe.getMessage(),qe);
  281.         } finally {

  282.             this.driver.closeConnection(error,con);
  283.         }
  284.     }  


  285.     protected void updateTipoPortaDominio(String nomePdd,String tipo) throws DriverRegistroServiziException{
  286.         if (nomePdd == null)
  287.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updateTipoPortaDominio] Parametro non valido.");
  288.         if (tipo == null)
  289.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updateTipoPortaDominio] Parametro.tipo non valido.");

  290.         PreparedStatement stm=null;
  291.         Connection con = null;
  292.         boolean error = false;

  293.         if (this.driver.atomica) {
  294.             try {
  295.                 con = this.driver.getConnectionFromDatasource("updateTipoPortaDominio");
  296.                 con.setAutoCommit(false);
  297.             } catch (Exception e) {
  298.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updatePortaDominio] Exception accedendo al datasource :" + e.getMessage(),e);

  299.             }

  300.         } else
  301.             con = this.driver.globalConnection;

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

  303.         try {

  304.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  305.             sqlQueryObject.addUpdateTable(CostantiDB.PDD);
  306.             sqlQueryObject.addUpdateField("tipo", "?");
  307.             sqlQueryObject.addWhereCondition("nome = ?");
  308.             sqlQueryObject.setANDLogicOperator(true);
  309.             String sqlQuery = sqlQueryObject.createSQLUpdate();
  310.             stm = con.prepareStatement(sqlQuery);

  311.             stm.setString(1, tipo);
  312.             stm.setString(2, nomePdd);

  313.             this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, tipo, nomePdd));
  314.             stm.executeUpdate();

  315.         } catch (Exception qe) {
  316.             error = true;
  317.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updatePortaDominio] Errore durante l'aggiornamento della porta di dominio : " + qe.getMessage(),qe);
  318.         } finally {

  319.             JDBCUtilities.closeResources(stm);

  320.             this.driver.closeConnection(error,con);
  321.         }
  322.     }  
  323.    
  324.     protected String getTipoPortaDominio(String nome) throws DriverRegistroServiziException, DriverRegistroServiziNotFound{
  325.         Connection con = null;
  326.         PreparedStatement stm = null;
  327.         ResultSet rs = null;

  328.         if (nome == null)
  329.             throw new DriverRegistroServiziException("Parametro non valido");

  330.         if (nome.equals(""))
  331.             throw new DriverRegistroServiziException("Parametro vuoto non valido");

  332.         if (this.driver.atomica) {
  333.             try {
  334.                 con = this.driver.getConnectionFromDatasource("getTipoPortaDominio");
  335.             } catch (Exception e) {
  336.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getTipoPortaDominio] Exception accedendo al datasource :" + e.getMessage(),e);

  337.             }

  338.         } else
  339.             con = this.driver.globalConnection;

  340.         try {
  341.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  342.             sqlQueryObject.addFromTable(CostantiDB.PDD);
  343.             sqlQueryObject.addSelectField("tipo");
  344.             sqlQueryObject.addWhereCondition("nome = ?");
  345.             sqlQueryObject.setANDLogicOperator(true);
  346.             String sqlQuery = sqlQueryObject.createSQLQuery();
  347.             stm = con.prepareStatement(sqlQuery);
  348.             stm.setString(1, nome);
  349.             rs = stm.executeQuery();
  350.             if (rs.next()){
  351.                 return rs.getString("tipo");
  352.             }
  353.             rs.close();
  354.             stm.close();

  355.             throw new DriverRegistroServiziNotFound("Porta di Dominio ["+nome+"] non esistente");
  356.            
  357.         } catch (DriverRegistroServiziNotFound e) {
  358.             throw e;
  359.         }catch (Exception e) {
  360.             this.driver.log.error("Errore durante verifica esistenza porta di dominio :", e);
  361.             throw new DriverRegistroServiziException(e.getMessage(),e);
  362.         } finally {

  363.             //Chiudo statement and resultset
  364.             JDBCUtilities.closeResources(rs, stm);

  365.             this.driver.closeConnection(con);
  366.         }

  367.     }


  368.     protected void deletePortaDominio(PortaDominio pdd) throws DriverRegistroServiziException{
  369.         if (pdd == null)
  370.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::deletePortaDominio] Parametro non valido.");

  371.         Connection con = null;
  372.         boolean error = false;

  373.         if (this.driver.atomica) {
  374.             try {
  375.                 con = this.driver.getConnectionFromDatasource("deletePortaDominio");
  376.                 con.setAutoCommit(false);
  377.             } catch (Exception e) {
  378.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::deletePortaDominio] Exception accedendo al datasource :" + e.getMessage(),e);

  379.             }

  380.         } else
  381.             con = this.driver.globalConnection;

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

  383.         try {
  384.             this.driver.logDebug("CRUDPortaDominio type = 3");
  385.             DriverRegistroServiziDB_soggettiLIB.CRUDPortaDominio(CostantiDB.DELETE, pdd, con);

  386.         } catch (Exception qe) {
  387.             error = true;
  388.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::deletePortaDominio] Errore durante l'eliminazione della porta di dominio : " + qe.getMessage(),qe);
  389.         } finally {

  390.             this.driver.closeConnection(error,con);
  391.         }
  392.     }
  393.    
  394.     protected List<PortaDominio> porteDominioList(String superuser, ISearch ricerca) throws DriverRegistroServiziException{
  395.         return this.porteDominioList(superuser, null, ricerca);
  396.     }
  397.     protected List<PortaDominio> porteDominioList(String superuser,String tipo, ISearch ricerca) throws DriverRegistroServiziException{
  398.         String nomeMetodo = "porteDominioList";
  399.         int idLista = Liste.PDD;
  400.         int offset;
  401.         int limit;
  402.         String search;
  403.         String queryString;

  404.         limit = ricerca.getPageSize(idLista);
  405.         offset = ricerca.getIndexIniziale(idLista);
  406.         search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));


  407.         Connection con = null;
  408.         PreparedStatement stmt = null;
  409.         ResultSet risultato = null;

  410.         ArrayList<PortaDominio> lista = null;

  411.         if (this.driver.atomica) {
  412.             try {
  413.                 con = this.driver.getConnectionFromDatasource("porteDominioList");

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

  416.             }

  417.         } else {
  418.             con = this.driver.globalConnection;
  419.         }

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

  421.         try {

  422.             if (!search.equals("")) {
  423.                 // query con search
  424.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  425.                 sqlQueryObject
  426.                 .addFromTable(CostantiDB.PDD);
  427.                 sqlQueryObject.addSelectCountField("*",
  428.                         "cont");
  429.                 if(this.driver.useSuperUser && superuser!=null && (!superuser.equals("")))
  430.                     sqlQueryObject.addWhereCondition("superuser = ?");
  431.                 if (tipo!=null && (!tipo.equals("")))
  432.                     sqlQueryObject.addWhereCondition("tipo = ?");
  433.                 sqlQueryObject.addWhereLikeCondition(
  434.                         "nome", search, true, true);
  435.                 sqlQueryObject.setANDLogicOperator(true);
  436.                 queryString = sqlQueryObject
  437.                         .createSQLQuery();
  438.             } else {
  439.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  440.                 sqlQueryObject
  441.                 .addFromTable(CostantiDB.PDD);
  442.                 sqlQueryObject.addSelectCountField("*",
  443.                         "cont");
  444.                 if(this.driver.useSuperUser && superuser!=null && (!superuser.equals("")))
  445.                     sqlQueryObject.addWhereCondition("superuser = ?");
  446.                 if (tipo!=null && (!tipo.equals("")))
  447.                     sqlQueryObject.addWhereCondition("tipo = ?");
  448.                 sqlQueryObject.setANDLogicOperator(true);
  449.                 queryString = sqlQueryObject
  450.                         .createSQLQuery();
  451.             }
  452.             stmt = con.prepareStatement(queryString);
  453.             int index = 1;
  454.             if(this.driver.useSuperUser && superuser!=null && (!superuser.equals("")))
  455.                 stmt.setString(index++, superuser);
  456.             if (tipo!=null && (!tipo.equals("")))
  457.                 stmt.setString(index++, tipo);
  458.             risultato = stmt.executeQuery();
  459.             if (risultato.next()) {
  460.                 ricerca.setNumEntries(idLista, risultato.getInt(1));
  461.             }
  462.             risultato.close();
  463.             stmt.close();

  464.             // ricavo le entries
  465.             if (limit == 0) {
  466.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;
  467.             }
  468.             if (!search.equals("")) {
  469.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  470.                 sqlQueryObject
  471.                 .addFromTable(CostantiDB.PDD);
  472.                 sqlQueryObject.addSelectField("nome");
  473.                 if(this.driver.useSuperUser && superuser!=null && (!superuser.equals("")))
  474.                     sqlQueryObject.addWhereCondition("superuser = ?");
  475.                 if (tipo!=null && (!tipo.equals("")))
  476.                     sqlQueryObject.addWhereCondition("tipo = ?");
  477.                 sqlQueryObject.addWhereLikeCondition(
  478.                         "nome", search, true, true);
  479.                 sqlQueryObject.setANDLogicOperator(true);
  480.                 sqlQueryObject.addOrderBy("nome");
  481.                 sqlQueryObject.setSortType(true);
  482.                 sqlQueryObject.setLimit(limit);
  483.                 sqlQueryObject.setOffset(offset);
  484.                 queryString = sqlQueryObject
  485.                         .createSQLQuery();
  486.             } else {
  487.                 // senza search
  488.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  489.                 sqlQueryObject
  490.                 .addFromTable(CostantiDB.PDD);
  491.                 sqlQueryObject.addSelectField("nome");
  492.                 if(this.driver.useSuperUser && superuser!=null && (!superuser.equals("")))
  493.                     sqlQueryObject.addWhereCondition("superuser = ?");
  494.                 if (tipo!=null && (!tipo.equals("")))
  495.                     sqlQueryObject.addWhereCondition("tipo = ?");
  496.                 sqlQueryObject.setANDLogicOperator(true);
  497.                 sqlQueryObject.addOrderBy("nome");
  498.                 sqlQueryObject.setSortType(true);
  499.                 sqlQueryObject.setLimit(limit);
  500.                 sqlQueryObject.setOffset(offset);
  501.                 queryString = sqlQueryObject
  502.                         .createSQLQuery();
  503.             }
  504.             stmt = con.prepareStatement(queryString);
  505.             index = 1;
  506.             if(this.driver.useSuperUser && superuser!=null && (!superuser.equals("")))
  507.                 stmt.setString(index++, superuser);
  508.             if (tipo!=null && (!tipo.equals("")))
  509.                 stmt.setString(index++, tipo);
  510.             risultato = stmt.executeQuery();

  511.             PortaDominio pdd;
  512.             lista = new ArrayList<PortaDominio>();
  513.             while (risultato.next()) {
  514.                 String nome = risultato.getString("nome");
  515.                 pdd = this.getPortaDominio(nome);
  516.                 lista.add(pdd);
  517.             }

  518.             return lista;

  519.         } catch (Exception se) {

  520.             throw new DriverRegistroServiziException("[DriverControlStationDB::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  521.         } finally {
  522.             //Chiudo statement and resultset
  523.             JDBCUtilities.closeResources(risultato, stmt);
  524.             this.driver.closeConnection(con);
  525.         }
  526.     }

  527.     protected boolean isPddInUso(PortaDominio pdd, List<String> whereIsInUso)
  528.             throws DriverRegistroServiziException {
  529.         String nomeMetodo = "pddInUso";

  530.         Connection con = null;
  531.         PreparedStatement stmt = null;
  532.         ResultSet risultato = null;
  533.         String queryString;

  534.         if (this.driver.atomica) {
  535.             try {
  536.                 con = this.driver.getConnectionFromDatasource("isPddInUso");

  537.             } catch (Exception e) {
  538.                 throw new DriverRegistroServiziException(
  539.                         "[DriverRegistroServiziDB::" + nomeMetodo
  540.                         + "] Exception accedendo al datasource :"
  541.                         + e.getMessage(), e);

  542.             }

  543.         } else {
  544.             con = this.driver.globalConnection;
  545.         }

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

  547.         try {

  548.             // Controllo che il pdd non sia in uso
  549.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  550.             sqlQueryObject
  551.             .addFromTable(CostantiDB.SOGGETTI);
  552.             sqlQueryObject.addSelectField("*");
  553.             sqlQueryObject
  554.             .addWhereCondition("server = ?");
  555.             queryString = sqlQueryObject
  556.                     .createSQLQuery();
  557.             stmt = con.prepareStatement(queryString);
  558.             stmt.setString(1, pdd.getNome());
  559.             risultato = stmt.executeQuery();
  560.             boolean isInUso = false;
  561.             while (risultato.next()) {
  562.                 String tipo_soggetto = risultato.getString("tipo_soggetto");
  563.                 String nome_soggetto = risultato.getString("nome_soggetto");
  564.                 whereIsInUso.add(tipo_soggetto + "/" + nome_soggetto);
  565.                 isInUso = true;
  566.             }

  567.             return isInUso;

  568.         } catch (Exception se) {
  569.             throw new DriverRegistroServiziException("[DriverRegistroServiziException::"
  570.                     + nomeMetodo + "] Exception: " + se.getMessage(),se);
  571.         } finally {
  572.             // Chiudo statement and resultset
  573.             JDBCUtilities.closeResources(risultato, stmt);
  574.             this.driver.closeConnection(con);
  575.         }
  576.     }

  577.     protected List<Soggetto> pddSoggettiList(long idPdd, ISearch ricerca) throws DriverRegistroServiziException {
  578.         String nomeMetodo = "pddSoggettiList";
  579.         int idLista = Liste.PDD_SOGGETTI;
  580.         int offset;
  581.         int limit;
  582.         String search;
  583.         String queryString;

  584.         limit = ricerca.getPageSize(idLista);
  585.         offset = ricerca.getIndexIniziale(idLista);
  586.         search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));
  587.         ricerca.getSearchString(idLista);

  588.         Connection con = null;
  589.         PreparedStatement stmt = null;
  590.         ResultSet risultato = null;

  591.         if (this.driver.atomica) {
  592.             try {
  593.                 con = this.driver.getConnectionFromDatasource("pddSoggettiList");

  594.             } catch (Exception e) {
  595.                 throw new DriverRegistroServiziException(
  596.                         "[DriverRegistroServiziDB::" + nomeMetodo
  597.                         + "] Exception accedendo al datasource :"
  598.                         + e.getMessage(), e);

  599.             }

  600.         } else {
  601.             con = this.driver.globalConnection;
  602.         }

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

  604.         ArrayList<Soggetto> lista = null;

  605.         try {

  606.             // Prendo il nome del pdd
  607.             String nomePdd = "";
  608.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  609.             sqlQueryObject.addFromTable(CostantiDB.PDD);
  610.             sqlQueryObject.addSelectField("*");
  611.             sqlQueryObject.addWhereCondition("id = ?");
  612.             queryString = sqlQueryObject
  613.                     .createSQLQuery();
  614.             stmt = con.prepareStatement(queryString);
  615.             stmt.setLong(1, idPdd);
  616.             risultato = stmt.executeQuery();
  617.             if (risultato.next()) {
  618.                 nomePdd = risultato.getString("nome");
  619.             }
  620.             risultato.close();
  621.             stmt.close();

  622.             if (!search.equals("")) {
  623.                 // query con search
  624.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  625.                 sqlQueryObject
  626.                 .addFromTable(CostantiDB.SOGGETTI);
  627.                 sqlQueryObject.addSelectCountField("*",
  628.                         "cont");
  629.                 sqlQueryObject
  630.                 .addWhereCondition("server = ?");
  631.                 sqlQueryObject.addWhereCondition(false,
  632.                         sqlQueryObject
  633.                         .getWhereLikeCondition("nome_soggetto", search,
  634.                                 true, true),
  635.                                 sqlQueryObject
  636.                                 .getWhereLikeCondition("tipo_soggetto", search,
  637.                                         true, true));
  638.                 sqlQueryObject.setANDLogicOperator(true);
  639.                 queryString = sqlQueryObject
  640.                         .createSQLQuery();
  641.             } else {
  642.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  643.                 sqlQueryObject
  644.                 .addFromTable(CostantiDB.SOGGETTI);
  645.                 sqlQueryObject.addSelectCountField("*",
  646.                         "cont");
  647.                 sqlQueryObject
  648.                 .addWhereCondition("server = ?");
  649.                 queryString = sqlQueryObject
  650.                         .createSQLQuery();
  651.             }
  652.             stmt = con.prepareStatement(queryString);
  653.             stmt.setString(1, nomePdd);
  654.             risultato = stmt.executeQuery();
  655.             if (risultato.next()) {
  656.                 ricerca.setNumEntries(idLista, risultato.getInt(1));
  657.             }
  658.             risultato.close();
  659.             stmt.close();

  660.             // ricavo le entries
  661.             if (limit == 0) {
  662.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;
  663.             }
  664.             if (!search.equals("")) {
  665.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  666.                 sqlQueryObject
  667.                 .addFromTable(CostantiDB.SOGGETTI);
  668.                 sqlQueryObject.addSelectField("tipo_soggetto");
  669.                 sqlQueryObject.addSelectField("nome_soggetto");
  670.                 sqlQueryObject.addSelectField("server");
  671.                 sqlQueryObject.addSelectField("id");
  672.                 sqlQueryObject
  673.                 .addWhereCondition("server = ?");
  674.                 sqlQueryObject.addWhereCondition(false,
  675.                         sqlQueryObject
  676.                         .getWhereLikeCondition("nome_soggetto", search,
  677.                                 true, true),
  678.                                 sqlQueryObject
  679.                                 .getWhereLikeCondition("tipo_soggetto", search,
  680.                                         true, true));
  681.                 sqlQueryObject.setANDLogicOperator(true);
  682.                 sqlQueryObject
  683.                 .addOrderBy("tipo_soggetto");
  684.                 sqlQueryObject
  685.                 .addOrderBy("nome_soggetto");
  686.                 sqlQueryObject.setSortType(true);
  687.                 sqlQueryObject.setLimit(limit);
  688.                 sqlQueryObject.setOffset(offset);
  689.                 queryString = sqlQueryObject
  690.                         .createSQLQuery();
  691.             } else {
  692.                 // senza search
  693.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  694.                 sqlQueryObject
  695.                 .addFromTable(CostantiDB.SOGGETTI);
  696.                 sqlQueryObject.addSelectField("tipo_soggetto");
  697.                 sqlQueryObject.addSelectField("nome_soggetto");
  698.                 sqlQueryObject.addSelectField("server");
  699.                 sqlQueryObject.addSelectField("id");
  700.                 sqlQueryObject
  701.                 .addWhereCondition("server = ?");
  702.                 sqlQueryObject
  703.                 .addOrderBy("tipo_soggetto");
  704.                 sqlQueryObject
  705.                 .addOrderBy("nome_soggetto");
  706.                 sqlQueryObject.setSortType(true);
  707.                 sqlQueryObject.setLimit(limit);
  708.                 sqlQueryObject.setOffset(offset);
  709.                 queryString = sqlQueryObject
  710.                         .createSQLQuery();
  711.             }
  712.             stmt = con.prepareStatement(queryString);
  713.             stmt.setString(1, nomePdd);
  714.             risultato = stmt.executeQuery();

  715.             lista = new ArrayList<Soggetto>();
  716.             Soggetto sog = null;
  717.             while (risultato.next()) {
  718.                 long ids = risultato.getLong("id");
  719.                 sog = this.driver.getSoggetto(ids,con);
  720.                 lista.add(sog);
  721.             }

  722.             return lista;

  723.         } catch (Exception se) {

  724.             throw new DriverRegistroServiziException("[DriverControlStationDB::"
  725.                     + nomeMetodo + "] Exception: " + se.getMessage(), se);
  726.         } finally {
  727.             // Chiudo statement and resultset
  728.             JDBCUtilities.closeResources(risultato, stmt);
  729.             this.driver.closeConnection(con);
  730.         }

  731.     }
  732.    
  733.     protected List<PortaDominio> porteDominioWithSubject(String subject)throws DriverRegistroServiziException {
  734.         String nomeMetodo = "porteDominioWithSubject";
  735.         String queryString;

  736.         Connection con = null;
  737.         PreparedStatement stmt=null;
  738.         ResultSet risultato=null;
  739.         ArrayList<PortaDominio> lista = new ArrayList<>();

  740.         if (this.driver.atomica) {
  741.             try {
  742.                 con = this.driver.getConnectionFromDatasource("porteDominioWithSubject");
  743.             } catch (Exception e) {
  744.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  745.             }

  746.         } else
  747.             con = this.driver.globalConnection;

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

  749.         try {

  750.             Map<String, List<String>> hashSubject = CertificateUtils.getPrincipalIntoMap(subject, PrincipalType.SUBJECT);

  751.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  752.             sqlQueryObject.addFromTable(CostantiDB.PDD);
  753.             sqlQueryObject.addSelectField("nome");
  754.             sqlQueryObject.addSelectField("subject");
  755.             for (String key : hashSubject.keySet()) {              
  756.                 List<String> listValues = hashSubject.get(key);
  757.                 for (String value : listValues) {
  758.                     sqlQueryObject.addWhereLikeCondition("subject", "/"+CertificateUtils.formatKeyPrincipal(key)+"="+CertificateUtils.formatValuePrincipal(value)+"/", true, false);
  759.                 }
  760.             }
  761.             sqlQueryObject.setANDLogicOperator(true);
  762.             queryString = sqlQueryObject.createSQLQuery();
  763.             stmt = con.prepareStatement(queryString);

  764.             risultato = stmt.executeQuery();

  765.             PortaDominio pdd;
  766.             while (risultato.next()) {

  767.                 // Possono esistere piu' pdd che hanno una porzione di subject uguale, devo quindi verificare che sia proprio quello che cerco
  768.                 String subjectPotenziale =  risultato.getString("subject");
  769.                 if(CertificateUtils.sslVerify(subjectPotenziale, subject, PrincipalType.SUBJECT, this.driver.log)){
  770.                     pdd=this.getPortaDominio(risultato.getString("nome"));
  771.                     lista.add(pdd);
  772.                 }
  773.             }

  774.             return lista;

  775.         } catch (Exception qe) {
  776.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  777.         } finally {

  778.             //Chiudo statement and resultset
  779.             JDBCUtilities.closeResources(risultato, stmt);

  780.             this.driver.closeConnection(con);
  781.         }
  782.     }
  783. }