DriverConfigurazioneDBSoggetti.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.config.driver.db;

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

  28. import org.openspcoop2.core.commons.CoreException;
  29. import org.openspcoop2.core.commons.DBUtils;
  30. import org.openspcoop2.core.config.Soggetto;
  31. import org.openspcoop2.core.config.constants.CostantiConfigurazione;
  32. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  33. import org.openspcoop2.core.config.driver.DriverConfigurazioneNotFound;
  34. import org.openspcoop2.core.config.driver.FiltroRicercaSoggetti;
  35. import org.openspcoop2.core.config.driver.IDServizioUtils;
  36. import org.openspcoop2.core.constants.CostantiDB;
  37. import org.openspcoop2.core.id.IDServizio;
  38. import org.openspcoop2.core.id.IDSoggetto;
  39. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  40. import org.openspcoop2.utils.sql.ISQLQueryObject;
  41. import org.openspcoop2.utils.sql.SQLObjectFactory;

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

  52.     private DriverConfigurazioneDB driver = null;

  53.     private static final String ESEGUO_QUERY_PREFIX = "eseguo query : ";
  54.     private static final String OPERAZIONE_ATOMICA_PREFIX = "operazione this.driver.atomica = ";
  55.    
  56.     protected DriverConfigurazioneDBSoggetti(DriverConfigurazioneDB driver) {
  57.         this.driver = driver;
  58.     }
  59.    
  60.     protected IDSoggetto getIdSoggetto(long idSoggetto) throws DriverConfigurazioneException,DriverConfigurazioneNotFound {
  61.         return getIdSoggetto(idSoggetto,null);
  62.     }
  63.     protected IDSoggetto getIdSoggetto(long idSoggetto,Connection conParam) throws DriverConfigurazioneException,DriverConfigurazioneNotFound {

  64.         // conrollo consistenza
  65.         if (idSoggetto <= 0)
  66.             return null;

  67.         IDSoggetto idSoggettoObject = null;

  68.         Connection con = null;
  69.         PreparedStatement stm = null;
  70.         ResultSet rs = null;

  71.         if(conParam!=null){
  72.             con = conParam;
  73.         }
  74.         else if (this.driver.atomica) {
  75.             try {
  76.                 con = this.driver.getConnectionFromDatasource("getIdSoggetto(longId)");

  77.             } catch (Exception e) {
  78.                 throw new DriverConfigurazioneException("DriverConfigurazioneDB::getIdSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  79.             }

  80.         } else
  81.             con = this.driver.globalConnection;

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

  83.         try {
  84.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  85.             sqlQueryObject.addFromTable(this.driver.tabellaSoggetti);
  86.             sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO);
  87.             sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO);
  88.             sqlQueryObject.addWhereCondition("id = ?");
  89.             String sqlQuery = sqlQueryObject.createSQLQuery();

  90.             stm = con.prepareStatement(sqlQuery);

  91.             stm.setLong(1, idSoggetto);

  92.             this.driver.logDebug(ESEGUO_QUERY_PREFIX + DriverConfigurazioneDBLib.formatSQLString(sqlQuery, idSoggetto));
  93.             rs = stm.executeQuery();

  94.             if (rs.next()) {
  95.                 idSoggettoObject = new IDSoggetto();

  96.                 idSoggettoObject.setNome(rs.getString(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO));
  97.                 idSoggettoObject.setTipo(rs.getString(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO));

  98.             }else{
  99.                 throw new DriverConfigurazioneNotFound("Nessun risultato trovat eseguendo: "+DriverConfigurazioneDBLib.formatSQLString(sqlQuery, idSoggetto));
  100.             }

  101.             return idSoggettoObject;
  102.         } catch (SQLException se) {
  103.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getIdSoggetto] SqlException: " + se.getMessage(),se);
  104.         }catch (DriverConfigurazioneNotFound se) {
  105.             throw se;
  106.         }catch (Exception se) {
  107.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getIdSoggetto] Exception: " + se.getMessage(),se);
  108.         } finally {

  109.             //Chiudo statement and resultset
  110.             JDBCUtilities.closeResources(rs, stm);

  111.             this.driver.closeConnection(conParam, con);
  112.         }
  113.     }
  114.    
  115.    
  116.    
  117.    
  118.    
  119.     protected Soggetto getSoggetto(IDSoggetto aSoggetto) throws DriverConfigurazioneException,DriverConfigurazioneNotFound {

  120.         if ((aSoggetto == null) || (aSoggetto.getNome() == null) || (aSoggetto.getTipo() == null)) {
  121.             throw new DriverConfigurazioneException("[getSoggetto] Parametri Non Validi");
  122.         }

  123.         Soggetto soggetto = null;

  124.         Connection con = null;

  125.         String nomeSogg = aSoggetto.getNome();
  126.         String tipoSogg = aSoggetto.getTipo();
  127.         long idSoggetto= -1;
  128.         if (this.driver.atomica) {
  129.             try {
  130.                 con = this.driver.getConnectionFromDatasource("getSoggetto(idSoggetto)");

  131.             } catch (Exception e) {
  132.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  133.             }

  134.         } else
  135.             con = this.driver.globalConnection;

  136.         this.driver.logDebug(OPERAZIONE_ATOMICA_PREFIX + this.driver.atomica);

  137.         try {
  138.             idSoggetto=DBUtils.getIdSoggetto(nomeSogg, tipoSogg, con, this.driver.tipoDB,this.driver.tabellaSoggetti);
  139.             if(idSoggetto==-1)
  140.                 throw new DriverConfigurazioneNotFound("Soggetto ["+aSoggetto.toString()+"] non esistente");

  141.         } catch (DriverConfigurazioneNotFound se) {
  142.             throw se;
  143.         } catch (CoreException se) {
  144.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getSoggetto] DriverException: " + se.getMessage(),se);
  145.         } catch (Exception se) {
  146.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getSoggetto] Exception: " + se.getMessage(),se);
  147.         }finally {

  148.             this.driver.closeConnection(con);
  149.         }


  150.         soggetto=this.getSoggetto(idSoggetto);

  151.         if(soggetto==null){
  152.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getSoggetto] Soggetto non Esistente.");
  153.         }

  154.         return soggetto;
  155.     }

  156.     protected void createSoggetto(org.openspcoop2.core.config.Soggetto soggetto) throws DriverConfigurazioneException {
  157.         if (soggetto == null)
  158.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createSoggetto] Parametro non valido.");

  159.         Connection con = null;
  160.         boolean error = false;

  161.         if (this.driver.atomica) {
  162.             try {
  163.                 con = this.driver.getConnectionFromDatasource("createSoggetto");
  164.                 con.setAutoCommit(false);
  165.             } catch (Exception e) {
  166.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  167.             }

  168.         } else
  169.             con = this.driver.globalConnection;

  170.         this.driver.logDebug(OPERAZIONE_ATOMICA_PREFIX + this.driver.atomica);

  171.         try {
  172.             this.driver.logDebug("CRUDSoggetto type = 1");
  173.             // creo soggetto
  174.             DriverConfigurazioneDBSoggettiLib.CRUDSoggetto(1, soggetto, con);

  175.         } catch (Exception qe) {
  176.             error = true;
  177.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createSoggetto] Errore durante la creazione del soggetto : " + qe.getMessage(),qe);
  178.         } finally {

  179.             this.driver.closeConnection(error,con);
  180.         }
  181.     }

  182.     protected void updateSoggetto(org.openspcoop2.core.config.Soggetto soggetto) throws DriverConfigurazioneException {
  183.         if (soggetto == null)
  184.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateSoggetto] Parametro non valido.");

  185.         Connection con = null;
  186.         boolean error = false;

  187.         if (this.driver.atomica) {
  188.             try {
  189.                 con = this.driver.getConnectionFromDatasource("updateSoggetto");
  190.                 con.setAutoCommit(false);
  191.             } catch (Exception e) {
  192.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  193.             }

  194.         } else
  195.             con = this.driver.globalConnection;

  196.         this.driver.logDebug(OPERAZIONE_ATOMICA_PREFIX + this.driver.atomica);

  197.         try {
  198.             this.driver.logDebug("CRUDSoggetto type = 2");
  199.             // UPDATE soggetto
  200.             DriverConfigurazioneDBSoggettiLib.CRUDSoggetto(2, soggetto, con);

  201.         } catch (Exception qe) {
  202.             error = true;
  203.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateSoggetto] Errore durante la creazione del soggetto : " + qe.getMessage(),qe);
  204.         }finally {

  205.             this.driver.closeConnection(error,con);
  206.         }
  207.     }

  208.        
  209.     protected void deleteSoggetto(org.openspcoop2.core.config.Soggetto soggetto) throws DriverConfigurazioneException {
  210.         if (soggetto == null)
  211.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteSoggetto] Parametro non valido.");

  212.         Connection con = null;
  213.         boolean error = false;

  214.         if (this.driver.atomica) {
  215.             try {
  216.                 con = this.driver.getConnectionFromDatasource("deleteSoggetto");
  217.                 con.setAutoCommit(false);
  218.             } catch (Exception e) {
  219.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  220.             }

  221.         } else
  222.             con = this.driver.globalConnection;

  223.         this.driver.logDebug(OPERAZIONE_ATOMICA_PREFIX + this.driver.atomica);

  224.         try {
  225.             this.driver.logDebug("CRUDSoggetto type = 3");
  226.             // DELETE soggetto
  227.             DriverConfigurazioneDBSoggettiLib.CRUDSoggetto(3, soggetto, con);

  228.         } catch (Exception qe) {
  229.             error = true;
  230.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteSoggetto] Errore durante la creazione del soggetto : " + qe.getMessage(),qe);
  231.         }finally {

  232.             this.driver.closeConnection(error,con);
  233.         }
  234.     }
  235.    
  236.     protected Soggetto getRouter() throws DriverConfigurazioneException,DriverConfigurazioneNotFound {

  237.         Soggetto soggetto = null;

  238.         Connection con = null;
  239.         PreparedStatement stm = null;
  240.         ResultSet rs = null;
  241.         String sqlQuery = "";

  242.         if (this.driver.atomica) {
  243.             try {
  244.                 con = this.driver.getConnectionFromDatasource("getRouter");

  245.             } catch (Exception e) {
  246.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getRouter] Exception accedendo al datasource :" + e.getMessage(),e);

  247.             }

  248.         } else
  249.             con = this.driver.globalConnection;

  250.         this.driver.logDebug(OPERAZIONE_ATOMICA_PREFIX + this.driver.atomica);
  251.         long idRouter = -1;
  252.         try {
  253.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  254.             sqlQueryObject.addFromTable(this.driver.tabellaSoggetti);
  255.             sqlQueryObject.addSelectField("*");
  256.             sqlQueryObject.addWhereCondition("is_router = ?");
  257.             sqlQuery = sqlQueryObject.createSQLQuery();

  258.             stm = con.prepareStatement(sqlQuery);

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

  260.             this.driver.logDebug(ESEGUO_QUERY_PREFIX + DBUtils.formatSQLString(sqlQuery, CostantiConfigurazione.ABILITATO));
  261.             rs = stm.executeQuery();

  262.             // prendo il primo router se c'e' altrimenti lancio eccezione.
  263.             if (rs.next()) {
  264.                 idRouter = rs.getLong("id");
  265.             } else {
  266.                 throw new DriverConfigurazioneNotFound("[DriverConfigurazioneDB::getRouter] Non esiste un Soggetto Router.");
  267.             }

  268.         } catch (SQLException se) {
  269.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getRouter] SqlException: " + se.getMessage(),se);
  270.         } catch (DriverConfigurazioneNotFound se) {
  271.             throw se;
  272.         } catch (Exception se) {
  273.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getRouter] Exception: " + se.getMessage(),se);
  274.         } finally {
  275.             //Chiudo statement and resultset
  276.             JDBCUtilities.closeResources(rs, stm);
  277.             this.driver.closeConnection(con);
  278.         }

  279.         soggetto=this.getSoggetto(idRouter);
  280.         // e' sicuramente un router
  281.         soggetto.setRouter(true);
  282.         return soggetto;
  283.     }

  284.     protected List<IDSoggetto> getSoggettiWithSuperuser(String user) throws DriverConfigurazioneException {

  285.         List<IDSoggetto> idSoggetti = null;

  286.         Connection con = null;
  287.         PreparedStatement stm = null;
  288.         ResultSet rs = null;
  289.         String sqlQuery = "";

  290.         if (this.driver.atomica) {
  291.             try {
  292.                 con = this.driver.getConnectionFromDatasource("getSoggettiWithSuperuser");

  293.             } catch (Exception e) {
  294.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getSoggettiWithSuperuser] Exception accedendo al datasource :" + e.getMessage(),e);

  295.             }

  296.         } else
  297.             con = this.driver.globalConnection;

  298.         this.driver.logDebug(OPERAZIONE_ATOMICA_PREFIX + this.driver.atomica);

  299.         try {
  300.             List<IDSoggetto> idTrovati = new ArrayList<>();
  301.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  302.             sqlQueryObject.addFromTable(this.driver.tabellaSoggetti);
  303.             sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO);
  304.             sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO);
  305.             sqlQueryObject.addWhereCondition("superuser = ?");
  306.             sqlQuery = sqlQueryObject.createSQLQuery();

  307.             stm = con.prepareStatement(sqlQuery);

  308.             stm.setString(1, user);

  309.             this.driver.logDebug(ESEGUO_QUERY_PREFIX + DBUtils.formatSQLString(sqlQuery, CostantiConfigurazione.ABILITATO));
  310.             rs = stm.executeQuery();

  311.             // prendo il primo router se c'e' altrimenti lancio eccezione.
  312.             while (rs.next()) {
  313.                 IDSoggetto id = new IDSoggetto();
  314.                 id.setTipo(rs.getString(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO));
  315.                 id.setNome(rs.getString(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO));
  316.                 idTrovati.add(id);
  317.             }

  318.             if(!idTrovati.isEmpty()){
  319.                 idSoggetti =  new ArrayList<>();
  320.                 idSoggetti.addAll(idTrovati);
  321.             }

  322.         } catch (SQLException se) {
  323.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getSoggettiWithSuperuser] SqlException: " + se.getMessage(),se);
  324.         }catch (Exception se) {
  325.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getSoggettiWithSuperuser] Exception: " + se.getMessage(),se);
  326.         } finally {
  327.             //Chiudo statement and resultset
  328.             JDBCUtilities.closeResources(rs, stm);
  329.             this.driver.closeConnection(con);
  330.         }
  331.         return idSoggetti;
  332.     }

  333.     protected List<IDSoggetto> getSoggettiVirtuali() throws DriverConfigurazioneException,DriverConfigurazioneNotFound {

  334.         List<IDSoggetto> soggettiVirtuali = new ArrayList<>();
  335.         Connection con = null;
  336.         PreparedStatement stm = null;
  337.         ResultSet rs = null;
  338.         String sqlQuery = "";
  339.         if (this.driver.atomica) {
  340.             try {
  341.                 con = this.driver.getConnectionFromDatasource("getSoggettiVirtuali");

  342.             } catch (Exception e) {
  343.                 throw new DriverConfigurazioneException("Exception accedendo al datasource :" + e.getMessage(),e);

  344.             }

  345.         } else
  346.             con = this.driver.globalConnection;

  347.         this.driver.logDebug(OPERAZIONE_ATOMICA_PREFIX + this.driver.atomica);

  348.         try {
  349.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  350.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  351.             sqlQueryObject.setSelectDistinct(true);
  352.             sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO_VIRTUALE);
  353.             sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO_VIRTUALE);
  354.             sqlQueryObject.addWhereCondition("tipo_soggetto_virtuale IS NOT NULL");
  355.             sqlQueryObject.addWhereCondition("nome_soggetto_virtuale IS NOT NULL");
  356.             sqlQueryObject.addWhereCondition("tipo_soggetto_virtuale<>'\"\"'");
  357.             sqlQueryObject.addWhereCondition("nome_soggetto_virtuale<>'\"\"'");
  358.             sqlQueryObject.setANDLogicOperator(true);
  359.             sqlQuery = sqlQueryObject.createSQLQuery();

  360.             this.driver.logDebug(ESEGUO_QUERY_PREFIX + DBUtils.formatSQLString(sqlQuery));

  361.             stm = con.prepareStatement(sqlQuery);
  362.             rs = stm.executeQuery();

  363.             while (rs.next()) {
  364.                 IDSoggetto soggettoVirtuale = new IDSoggetto(rs.getString(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO_VIRTUALE) , rs.getString(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO_VIRTUALE));
  365.                 this.driver.logInfo("aggiunto Soggetto " + soggettoVirtuale + " alla lista dei Soggetti Virtuali");
  366.                 soggettiVirtuali.add(soggettoVirtuale);
  367.             }
  368.             rs.close();
  369.             stm.close();

  370.             if(soggettiVirtuali.isEmpty()){
  371.                 throw new DriverConfigurazioneNotFound("[getSoggettiVirtuali] Soggetti virtuali non esistenti");
  372.             }

  373.             this.driver.logInfo("aggiunti " + soggettiVirtuali.size() + " soggetti alla lista dei Soggetti Virtuali");
  374.             return soggettiVirtuali;
  375.         } catch (SQLException se) {
  376.             throw new DriverConfigurazioneException("SqlException: " + se.getMessage(), se);
  377.         } catch (DriverConfigurazioneNotFound se) {
  378.             throw se;
  379.         }catch (Exception se) {
  380.             throw new DriverConfigurazioneException("Exception: " + se.getMessage(), se);
  381.         } finally {
  382.             //Chiudo statement and resultset
  383.             JDBCUtilities.closeResources(rs, stm);
  384.             this.driver.closeConnection(con);
  385.         }

  386.     }
  387.    
  388.     /**
  389.      * Ritorna il {@linkplain Soggetto} utilizzando il {@link DriverConfigurazioneDB} che ha l'id passato come parametro
  390.      */
  391.     protected Soggetto getSoggetto(long idSoggetto) throws DriverConfigurazioneException,DriverConfigurazioneNotFound {
  392.         return getSoggetto(idSoggetto,null);
  393.     }
  394.     protected Soggetto getSoggetto(long idSoggetto,Connection conParam) throws DriverConfigurazioneException,DriverConfigurazioneNotFound {

  395.         if (idSoggetto <= 0)
  396.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getSoggetto] L'id del soggetto deve essere > 0.");

  397.         Soggetto soggetto = null;

  398.         Connection con = null;
  399.         PreparedStatement stm = null;
  400.         PreparedStatement stm1 = null;
  401.         ResultSet rs = null;
  402.         ResultSet rs1 = null;
  403.         String sqlQuery = "";

  404.         if(conParam!=null){
  405.             con = conParam;
  406.         }
  407.         else if (this.driver.atomica) {
  408.             try {
  409.                 con = this.driver.getConnectionFromDatasource("getSoggetto(longId)");
  410.             } catch (Exception e) {
  411.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);
  412.             }

  413.         } else
  414.             con = this.driver.globalConnection;

  415.         this.driver.logDebug(OPERAZIONE_ATOMICA_PREFIX + this.driver.atomica);

  416.         try {

  417.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  418.             sqlQueryObject.addFromTable(this.driver.tabellaSoggetti);
  419.             sqlQueryObject.addSelectField("*");
  420.             sqlQueryObject.addWhereCondition("id=?");
  421.             sqlQuery = sqlQueryObject.createSQLQuery();

  422.             this.driver.logDebug(OPERAZIONE_ATOMICA_PREFIX + this.driver.atomica);

  423.             stm = con.prepareStatement(sqlQuery);

  424.             stm.setLong(1, idSoggetto);

  425.             this.driver.logDebug(ESEGUO_QUERY_PREFIX + DBUtils.formatSQLString(sqlQuery, idSoggetto));
  426.             rs = stm.executeQuery();

  427.             if (rs.next()) {
  428.                 soggetto = new Soggetto();

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

  430.                 soggetto.setNome(rs.getString(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO));

  431.                 soggetto.setTipo(rs.getString(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO));

  432.                 soggetto.setSuperUser(rs.getString("superuser"));

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

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

  437.                 int defaultR = rs.getInt("is_default");
  438.                 boolean isDefault = false;
  439.                 if (defaultR == CostantiDB.TRUE)
  440.                     isDefault = true;
  441.                 soggetto.setDominioDefault(isDefault);
  442.                
  443.                 int router = rs.getInt("is_router");
  444.                 boolean isrouter = false;
  445.                 if (router == CostantiDB.TRUE)
  446.                     isrouter = true;
  447.                 soggetto.setRouter(isrouter);

  448.                 tmp = rs.getString("pd_url_prefix_rewriter");
  449.                 soggetto.setPdUrlPrefixRewriter(((tmp == null || tmp.equals("")) ? null : tmp));

  450.                 tmp = rs.getString("pa_url_prefix_rewriter");
  451.                 soggetto.setPaUrlPrefixRewriter(((tmp == null || tmp.equals("")) ? null : tmp));

  452.                 // Creava OutOfMemory, non dovrebbe servire
  453. /**             // Aggiungo i servizi applicativi
  454. //              sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  455. //              sqlQueryObject.addFromTable(CostantiDB.SERVIZI_APPLICATIVI);
  456. //              sqlQueryObject.addSelectField("id");
  457. //              sqlQueryObject.addSelectField("nome");
  458. //              sqlQueryObject.addWhereCondition("id_soggetto=?");
  459. //              sqlQuery = sqlQueryObject.createSQLQuery();
  460. //              stm1 = con.prepareStatement(sqlQuery);
  461. //              stm1.setLong(1, rs.getLong("id"));
  462. //              rs1 = stm1.executeQuery();
  463. //
  464. //              ServizioApplicativo servizioApplicativo = null;
  465. //              while (rs1.next()) {
  466. //                  // setto solo il nome come da specifica
  467. //                  servizioApplicativo = new ServizioApplicativo();
  468. //                  servizioApplicativo.setId(rs1.getLong("id"));
  469. //                  servizioApplicativo.setNome(rs1.getString("nome"));
  470. //                  Soggetto.addServizioApplicativo(servizioApplicativo);
  471. //              }
  472. //              rs1.close();
  473. //              stm1.close();*/

  474.             } else {
  475.                 throw new DriverConfigurazioneNotFound("[DriverConfigurazioneDB::getSoggetto] Soggetto non Esistente.");
  476.             }

  477.             return soggetto;

  478.         } catch (SQLException se) {

  479.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getSoggetto] SqlException: " + se.getMessage(),se);
  480.         }catch (DriverConfigurazioneNotFound e) {
  481.             throw new DriverConfigurazioneNotFound(e);
  482.         } catch (Exception se) {

  483.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getSoggetto] Exception: " + se.getMessage(),se);
  484.         } finally {
  485.             //Chiudo statement and resultset
  486.             JDBCUtilities.closeResources(rs1, stm1);
  487.             JDBCUtilities.closeResources(rs, stm);
  488.             this.driver.closeConnection(conParam, con);
  489.         }
  490.     }

  491.    
  492.     protected List<IDServizio> getServiziSoggettiVirtuali() throws DriverConfigurazioneException,DriverConfigurazioneNotFound {

  493.         List<IDServizio> servizi = new ArrayList<>();
  494.         Connection con = null;
  495.         PreparedStatement stm = null;
  496.         ResultSet rs = null;
  497.         String sqlQuery = "";
  498.         if (this.driver.atomica) {
  499.             try {
  500.                 con = this.driver.getConnectionFromDatasource("getServiziSoggettiVirtuali");

  501.             } catch (Exception e) {
  502.                 throw new DriverConfigurazioneException("Exception accedendo al datasource :" + e.getMessage(),e);

  503.             }

  504.         } else
  505.             con = this.driver.globalConnection;

  506.         this.driver.logDebug(OPERAZIONE_ATOMICA_PREFIX + this.driver.atomica);

  507.         try {
  508.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  509.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  510.             sqlQueryObject.setSelectDistinct(true);
  511.             sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO_VIRTUALE);
  512.             sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO_VIRTUALE);
  513.             sqlQueryObject.addSelectField("tipo_servizio");
  514.             sqlQueryObject.addSelectField("servizio");
  515.             sqlQueryObject.addSelectField("versione_servizio");
  516.             sqlQueryObject.setANDLogicOperator(false);
  517.             sqlQueryObject.addWhereCondition("id_soggetto_virtuale<>-1");
  518.             sqlQueryObject.addWhereCondition(true, "tipo_soggetto_virtuale is not null", "nome_soggetto_virtuale is not null");
  519.             sqlQueryObject.setANDLogicOperator(false);
  520.             sqlQuery = sqlQueryObject.createSQLQuery();

  521.             this.driver.logDebug(ESEGUO_QUERY_PREFIX + DBUtils.formatSQLString(sqlQuery));

  522.             stm = con.prepareStatement(sqlQuery);
  523.             rs = stm.executeQuery();

  524.             while (rs.next()) {
  525.                 IDServizio servizio = IDServizioUtils.buildIDServizio(rs.getString("tipo_servizio"), rs.getString("servizio"),
  526.                         new IDSoggetto(rs.getString(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO_VIRTUALE), rs.getString(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO_VIRTUALE)),
  527.                         rs.getInt("versione_servizio"));
  528.                 servizi.add(servizio);
  529.                 this.driver.logInfo("aggiunto Servizio " + servizio.toString() + " alla lista dei servizi erogati da Soggetti Virtuali");
  530.             }

  531.             if(servizi.isEmpty()){
  532.                 throw new DriverConfigurazioneNotFound("[getServiziSoggettiVirtuali] Servizi erogati da Soggetti virtuali non esistenti");
  533.             }

  534.             this.driver.logInfo("aggiunti " + servizi.size() + " servizi alla lista dei servizi erogati da Soggetti Virtuali");
  535.             return servizi;
  536.         } catch (SQLException se) {
  537.             throw new DriverConfigurazioneException("SqlException: " + se.getMessage(), se);
  538.         }  catch (DriverConfigurazioneNotFound se) {
  539.             throw se;
  540.         }catch (Exception se) {
  541.             throw new DriverConfigurazioneException("Exception: " + se.getMessage(), se);
  542.         }finally {
  543.             //Chiudo statement and resultset
  544.             JDBCUtilities.closeResources(rs, stm);
  545.             this.driver.closeConnection(con);
  546.         }

  547.     }
  548.    
  549.     protected boolean existsSoggetto(IDSoggetto idSoggetto) throws DriverConfigurazioneException {

  550.         if(idSoggetto==null)throw new DriverConfigurazioneException("[existsSoggetto::existsSoggetto] Soggetto non Impostato.");
  551.         if(idSoggetto.getNome()==null || "".equals(idSoggetto.getNome()))throw new DriverConfigurazioneException("[existsSoggetto::existsServizioApplicativo] Nome Soggetto non Impostato.");
  552.         if(idSoggetto.getTipo()==null || "".equals(idSoggetto.getTipo()))throw new DriverConfigurazioneException("[existsSoggetto::existsServizioApplicativo] Nome Soggetto non Impostato.");

  553.         Connection con = null;

  554.         try {
  555.            
  556.             if (this.driver.atomica) {
  557.                 try {
  558.                     con = this.driver.getConnectionFromDatasource("existsSoggetto");
  559.                 } catch (Exception e) {
  560.                     throw new DriverConfigurazioneException("[DriverConfigurazioneDB::existsSoggetto] Exception accedendo al datasource :" + e.getMessage(),e);

  561.                 }

  562.             } else
  563.                 con = this.driver.globalConnection;
  564.            
  565.             return DBUtils.getIdSoggetto(idSoggetto.getNome(), idSoggetto.getTipo(), con, this.driver.tipoDB,this.driver.tabellaSoggetti)>0;
  566.         } catch (Exception qe) {
  567.             throw new DriverConfigurazioneException(qe);
  568.         } finally {

  569.             this.driver.closeConnection(con);

  570.         }
  571.     }
  572.    
  573.     protected List<Soggetto> getAllSoggetti() throws DriverConfigurazioneException {
  574.         String nomeMetodo = "getAllSoggetti";

  575.         Connection con = null;
  576.         PreparedStatement stmt=null;
  577.         ResultSet risultato=null;
  578.         ArrayList<Soggetto> lista = new ArrayList<>();

  579.         if (this.driver.atomica) {
  580.             try {
  581.                 con = this.driver.getConnectionFromDatasource(nomeMetodo);
  582.             } catch (Exception e) {
  583.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  584.             }

  585.         } else
  586.             con = this.driver.globalConnection;

  587.         this.driver.logDebug(OPERAZIONE_ATOMICA_PREFIX + this.driver.atomica);

  588.         try {

  589.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  590.             sqlQueryObject.addFromTable(this.driver.tabellaSoggetti);
  591.             sqlQueryObject.addSelectField("id");
  592.             String queryString = sqlQueryObject.createSQLQuery();
  593.             stmt = con.prepareStatement(queryString);
  594.             risultato = stmt.executeQuery();

  595.             while (risultato.next()) {

  596.                 Long id = risultato.getLong("id");
  597.                 lista.add(this.getSoggetto(id));

  598.             }

  599.             return lista;

  600.         } catch (Exception qe) {
  601.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  602.         } finally {
  603.             //Chiudo statement and resultset
  604.             JDBCUtilities.closeResources(risultato, stmt);
  605.             this.driver.closeConnection(con);
  606.         }
  607.     }
  608.    
  609.     protected List<IDSoggetto> getAllIdSoggetti(
  610.             FiltroRicercaSoggetti filtroRicerca) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{

  611.         Connection con = null;
  612.         PreparedStatement stm = null;
  613.         ResultSet rs = null;

  614.         this.driver.logDebug("getAllIdSoggetti...");

  615.         try {
  616.             this.driver.logDebug("operazione atomica = " + this.driver.atomica);
  617.             // prendo la connessione dal pool
  618.             if (this.driver.atomica)
  619.                 con = this.driver.getConnectionFromDatasource("getAllIdSoggetti");
  620.             else
  621.                 con = this.driver.globalConnection;

  622.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  623.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  624.             sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO);
  625.             sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO);
  626.             if(filtroRicerca!=null){
  627.                 // Filtro By Data
  628.                 if(filtroRicerca.getMinDate()!=null)
  629.                     sqlQueryObject.addWhereCondition("ora_registrazione > ?");
  630.                 if(filtroRicerca.getMaxDate()!=null)
  631.                     sqlQueryObject.addWhereCondition("ora_registrazione < ?");
  632.                 if(filtroRicerca.getTipo()!=null)
  633.                     sqlQueryObject.addWhereCondition("tipo_soggetto = ?");
  634.                 if(filtroRicerca.getNome()!=null)
  635.                     sqlQueryObject.addWhereCondition("nome_soggetto = ?");
  636.             }

  637.             sqlQueryObject.setANDLogicOperator(true);
  638.             String sqlQuery = sqlQueryObject.createSQLQuery();
  639.             this.driver.logDebug(ESEGUO_QUERY_PREFIX + sqlQuery );
  640.             stm = con.prepareStatement(sqlQuery);
  641.             int indexStmt = 1;
  642.             if(filtroRicerca!=null){
  643.                 if(filtroRicerca.getMinDate()!=null){
  644.                     this.driver.logDebug("minDate stmt.setTimestamp("+filtroRicerca.getMinDate()+")");
  645.                     stm.setTimestamp(indexStmt, new Timestamp(filtroRicerca.getMinDate().getTime()));
  646.                     indexStmt++;
  647.                 }
  648.                 if(filtroRicerca.getMaxDate()!=null){
  649.                     this.driver.logDebug("maxDate stmt.setTimestamp("+filtroRicerca.getMaxDate()+")");
  650.                     stm.setTimestamp(indexStmt, new Timestamp(filtroRicerca.getMaxDate().getTime()));
  651.                     indexStmt++;
  652.                 }  
  653.                 if(filtroRicerca.getTipo()!=null){
  654.                     this.driver.logDebug("tipoSoggetto stmt.setString("+filtroRicerca.getTipo()+")");
  655.                     stm.setString(indexStmt, filtroRicerca.getTipo());
  656.                     indexStmt++;
  657.                 }
  658.                 if(filtroRicerca.getNome()!=null){
  659.                     this.driver.logDebug("nomeSoggetto stmt.setString("+filtroRicerca.getNome()+")");
  660.                     stm.setString(indexStmt, filtroRicerca.getNome());
  661.                     indexStmt++;
  662.                 }  
  663.             }
  664.             rs = stm.executeQuery();
  665.             List<IDSoggetto> idSoggetti = new ArrayList<>();
  666.             while (rs.next()) {
  667.                 IDSoggetto idS = new IDSoggetto(rs.getString(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO),rs.getString(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO));
  668.                 idSoggetti.add(idS);
  669.             }
  670.             if(idSoggetti.isEmpty()){
  671.                 if(filtroRicerca!=null)
  672.                     throw new DriverConfigurazioneNotFound("Soggetti non trovati che rispettano il filtro di ricerca selezionato: "+filtroRicerca.toString());
  673.                 else
  674.                     throw new DriverConfigurazioneNotFound("Soggetti non trovati");
  675.             }else{
  676.                 return idSoggetti;
  677.             }
  678.         }catch(DriverConfigurazioneNotFound de){
  679.             throw de;
  680.         }
  681.         catch(Exception e){
  682.             throw new DriverConfigurazioneException("getAllIdSoggetti error",e);
  683.         } finally {

  684.             //Chiudo statement and resultset
  685.             JDBCUtilities.closeResources(rs, stm);

  686.             this.driver.closeConnection(con);

  687.         }

  688.     }
  689. }