TracciaDriver.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */



  20. package org.openspcoop2.protocol.basic.tracciamento;

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

  30. import javax.sql.DataSource;

  31. import org.openspcoop2.core.constants.CostantiDB;
  32. import org.openspcoop2.core.id.IDSoggetto;
  33. import org.openspcoop2.protocol.basic.BasicComponentFactory;
  34. import org.openspcoop2.protocol.basic.Costanti;
  35. import org.openspcoop2.protocol.basic.ProtocolliRegistrati;
  36. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  37. import org.openspcoop2.protocol.sdk.ProtocolException;
  38. import org.openspcoop2.protocol.sdk.constants.RuoloMessaggio;
  39. import org.openspcoop2.protocol.sdk.diagnostica.DriverMsgDiagnosticiException;
  40. import org.openspcoop2.protocol.sdk.tracciamento.DriverTracciamentoException;
  41. import org.openspcoop2.protocol.sdk.tracciamento.DriverTracciamentoNotFoundException;
  42. import org.openspcoop2.protocol.sdk.tracciamento.FiltroRicercaTracce;
  43. import org.openspcoop2.protocol.sdk.tracciamento.FiltroRicercaTracceConPaginazione;
  44. import org.openspcoop2.protocol.sdk.tracciamento.ITracciaDriver;
  45. import org.openspcoop2.protocol.sdk.tracciamento.Traccia;
  46. import org.openspcoop2.utils.LoggerWrapperFactory;
  47. import org.openspcoop2.utils.StringWrapper;
  48. import org.openspcoop2.utils.TipiDatabase;
  49. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  50. import org.openspcoop2.utils.resources.GestoreJNDI;
  51. import org.openspcoop2.utils.sql.ISQLQueryObject;
  52. import org.slf4j.Logger;



  53. /**
  54.  * Interfaccia Tracciamento
  55.  *
  56.  * @author Stefano Corallo (corallo@link.it)
  57.  * @author $Author$
  58.  * @version $Rev$, $Date$
  59.  */
  60. public class TracciaDriver extends BasicComponentFactory implements ITracciaDriver {

  61.     /**
  62.      * DataSource
  63.      */
  64.     DataSource datasource = null;
  65.     /**
  66.      * Connessione
  67.      */
  68.     Connection connection = null;
  69.     boolean connectionOpenViaJDBCInCostructor = false;
  70.     /**
  71.      * SQLQueryObject
  72.      */
  73.     String tipoDatabase = null;



  74.     public static final String IDTRACCIA = "@@@@@-----@@@@-----IDTRACCIA-DB----@@@@@-----@@@@";
  75.    
  76.     private static final String TRACCIA_EXCEPTION_PREFIX = "Tracciamento exception: ";
  77.    
  78.     /**
  79.      * Properties
  80.      */
  81.     private List<String> properties;
  82.     public void setProperties(List<String> properties) {
  83.         this.properties = properties;
  84.     }
  85.    
  86.     private ProtocolliRegistrati protocolliRegistrati;
  87.    
  88.     public TracciaDriver(IProtocolFactory<?> factory) throws ProtocolException{
  89.         super(factory);
  90.     }
  91.    
  92.    
  93.     @Override
  94.     public IProtocolFactory<?> getProtocolFactory() {
  95.         return this.protocolFactory;
  96.     }
  97.    
  98.     public void init(ProtocolliRegistrati protocolliRegistrati,String nomeDataSource, String tipoDatabase, Properties jndiContext) throws DriverTracciamentoException {
  99.         init(protocolliRegistrati,nomeDataSource,tipoDatabase,jndiContext,null);
  100.     }
  101.    
  102.     public void init(ProtocolliRegistrati protocolliRegistrati,String nomeDataSource, String tipoDatabase, Properties jndiContext, Logger log) throws DriverTracciamentoException {
  103.    
  104.         this.protocolliRegistrati = protocolliRegistrati;
  105.        
  106.         // Logger
  107.         try {
  108.             if(log==null)
  109.                 this.log = LoggerWrapperFactory.getLogger(Costanti.LOGANALIZER_DRIVER_DB_LOGGER);
  110.             else
  111.                 this.log = log;
  112.         } catch (Exception e) {
  113.             throw new DriverTracciamentoException("Errore durante l'inizializzazione del logger (datasource params): "+e.getMessage(),e);
  114.         }

  115.         // Datasource
  116.         try {
  117.             this.log.info("Inizializzo DriverLogAnalyzer (datasource params)");
  118.             GestoreJNDI gestoreJNDI = new GestoreJNDI(jndiContext);
  119.             this.datasource = (DataSource) gestoreJNDI.lookup(nomeDataSource);
  120.             if (this.datasource == null)
  121.                 throw new ProtocolException ("datasource is null");

  122.             this.log.info("Inizializzo DriverLogAnalyzer terminata.");
  123.         } catch (Exception e) {
  124.             this.logError("Errore durante la ricerca del datasource(datasource params): "+e.getMessage(),e);
  125.             throw new DriverTracciamentoException("Errore durante la ricerca del datasource (datasource params): "+e.getMessage(),e);
  126.         }

  127.         // ISQLQueryObject
  128.         try {
  129.             this.log.info("Inizializzo ISQLQueryObject (datasource params)");
  130.             if (TipiDatabase.isAMember(tipoDatabase)) {
  131.                 this.tipoDatabase = tipoDatabase;              
  132.             } else {
  133.                 throw new ProtocolException("Tipo database non gestito (datasource params)");
  134.             }
  135.             this.log.info("Inizializzo ISQLQueryObject terminata (datasource params)");

  136.         } catch (Exception e) {
  137.             this.logError("Errore durante la ricerca del SQLQueryObject(datasource params): "+e.getMessage(),e);
  138.             throw new DriverTracciamentoException("Errore durante la ricerca del SQLQueryObject (datasource params): "+e.getMessage(),e);
  139.         }
  140.        
  141.     }
  142.    
  143.     public void init(ProtocolliRegistrati protocolliRegistrati,DataSource dataSourceObject, String tipoDatabase) throws DriverTracciamentoException {
  144.         init(protocolliRegistrati,dataSourceObject,tipoDatabase,null);
  145.     }
  146.    
  147.     public void init(ProtocolliRegistrati protocolliRegistrati,DataSource dataSourceObject, String tipoDatabase, Logger log) throws DriverTracciamentoException {
  148.        
  149.         this.protocolliRegistrati = protocolliRegistrati;
  150.        
  151.         // Logger
  152.         try {
  153.             if(log==null)
  154.                 this.log = LoggerWrapperFactory.getLogger(Costanti.LOGANALIZER_DRIVER_DB_LOGGER);
  155.             else
  156.                 this.log = log;
  157.         } catch (Exception e) {
  158.             throw new DriverTracciamentoException("Errore durante l'inizializzazione del logger (datasource): "+e.getMessage(),e);
  159.         }

  160.         // Datasource
  161.         try {
  162.             this.datasource = dataSourceObject;
  163.             if (this.datasource == null)
  164.                 throw new ProtocolException ("datasource is null");
  165.         } catch (Exception e) {
  166.             this.logError("Errore durante la ricerca del datasource...",e);
  167.             throw new DriverTracciamentoException("Errore durante la ricerca del datasource (datasource): "+e.getMessage(),e);
  168.         }

  169.         // ISQLQueryObject
  170.         try {
  171.             this.log.info("Inizializzo ISQLQueryObject (datasource)");
  172.             if (TipiDatabase.isAMember(tipoDatabase)) {
  173.                 this.tipoDatabase = tipoDatabase;              
  174.             } else {
  175.                 throw new ProtocolException("Tipo database non gestito (datasource)");
  176.             }
  177.             this.log.info("Inizializzo ISQLQueryObject terminata (datasource)");

  178.         } catch (Exception e) {
  179.             this.logError("Errore durante la ricerca del SQLQueryObject (datasource): "+e.getMessage(),e);
  180.             throw new DriverTracciamentoException("Errore durante la ricerca del SQLQueryObject (datasource): "+e.getMessage(),e);
  181.         }
  182.     }
  183.    
  184.     public void init(ProtocolliRegistrati protocolliRegistrati,Connection connection, String tipoDatabase, Logger log) throws DriverTracciamentoException {
  185.        
  186.         this.protocolliRegistrati = protocolliRegistrati;
  187.        
  188.         // Logger
  189.         try {
  190.             this.log = log;
  191.         } catch (Exception e) {
  192.             throw new DriverTracciamentoException("Errore durante l'inizializzazione del logger (connection): "+e.getMessage(),e);
  193.         }

  194.         // connection
  195.         this.connection = connection;
  196.        
  197.         // ISQLQueryObject
  198.         try {
  199.             this.log.info("Inizializzo ISQLQueryObject (connection)");
  200.             if (TipiDatabase.isAMember(tipoDatabase)) {
  201.                 this.tipoDatabase = tipoDatabase;              
  202.             } else {
  203.                 throw new ProtocolException("Tipo database non gestito (connection)");
  204.             }
  205.             this.log.info("Inizializzo ISQLQueryObject terminata (connection)");

  206.         } catch (Exception e) {
  207.             this.logError("Errore durante la ricerca del SQLQueryObject (connection): "+e.getMessage(),e);
  208.             throw new DriverTracciamentoException("Errore durante la ricerca del SQLQueryObject (connection): "+e.getMessage(),e);
  209.         }

  210.     }
  211.    
  212.     public void init(ProtocolliRegistrati protocolliRegistrati,String urlJDBC,String driverJDBC,
  213.             String username,String password,
  214.             String tipoDatabase, Logger log) throws DriverTracciamentoException {
  215.        
  216.         this.protocolliRegistrati = protocolliRegistrati;
  217.        
  218.         // Logger
  219.         try {
  220.             this.log = log;
  221.         } catch (Exception e) {
  222.             throw new DriverTracciamentoException("Errore durante l'inizializzazione del logger (connectionUrl params): "+e.getMessage(),e);
  223.         }

  224.         // connection
  225.         try {
  226.             Class.forName(driverJDBC);
  227.            
  228.             if(username!=null){
  229.                 this.connection = DriverManager.getConnection(urlJDBC,username,password);
  230.             }else{
  231.                 this.connection = DriverManager.getConnection(urlJDBC);
  232.             }
  233.             this.connectionOpenViaJDBCInCostructor = true;
  234.            
  235.         } catch (Exception e) {
  236.             this.logError("Errore durante l'inizializzazione della connessione (connectionUrl params): "+e.getMessage(),e);
  237.             throw new DriverTracciamentoException("Errore durante l'inizializzazione della connessione (connectionUrl params): "+e.getMessage(),e);
  238.         }
  239.        
  240.         // ISQLQueryObject
  241.         try {
  242.             this.log.info("Inizializzo ISQLQueryObject (connectionUrl params)");
  243.             if (TipiDatabase.isAMember(tipoDatabase)) {
  244.                 this.tipoDatabase = tipoDatabase;              
  245.             } else {
  246.                 throw new ProtocolException("Tipo database non gestito (connectionUrl params)");
  247.             }
  248.             this.log.info("Inizializzo ISQLQueryObject terminata (connectionUrl params)");

  249.         } catch (Exception e) {
  250.             this.logError("Errore durante la ricerca del SQLQueryObject (connectionUrl params): "+e.getMessage(),e);
  251.             throw new DriverTracciamentoException("Errore durante la ricerca del SQLQueryObject (connectionUrl params): "+e.getMessage(),e);
  252.         }

  253.     }
  254.    

  255.    
  256.    
  257.    
  258.    
  259.    
  260.    
  261.    
  262.     /* *********** ACCESSI TRAMITE RICERCHE ******* */
  263.    
  264.     /**
  265.      * Si occupa di ritornare il numero di tracce che rispettano il filtro di ricerca
  266.      *
  267.      * @param filtro Filtro di ricerca
  268.      * @return numero di tracce che rispettano il filtro di ricerca
  269.      *
  270.      */
  271.     @Override
  272.     public int countTracce(FiltroRicercaTracce filtro) throws DriverTracciamentoException{
  273.         Connection connectionDB = null;
  274.         PreparedStatement pstmt = null;
  275.         ResultSet rs = null;
  276.         int contatore = 0;
  277.        
  278.         try{
  279.            
  280.             // Get Connection
  281.             if(this.connection!=null)
  282.                 connectionDB = this.connection;
  283.             else{
  284.                 connectionDB = this.datasource.getConnection();
  285.             }
  286.            
  287.             checkConnection(connectionDB);
  288.        
  289.             ISQLQueryObject sqlQueryObject =
  290.                 TracciaDriverUtilities.createSQLQueryObj_countTracce(filtro, this.tipoDatabase);
  291.             String sql = sqlQueryObject.createSQLQuery();
  292.            
  293.             StringWrapper sqlDebug = new StringWrapper(sqlQueryObject.createSQLQuery());
  294.             TracciaDriverUtilities.setValuesSearch(sqlDebug, filtro, 1);
  295.             this.logDebug("Query: "+sqlDebug);
  296.            
  297.             pstmt = connectionDB.prepareStatement(sql);
  298.             TracciaDriverUtilities.setValuesSearch(pstmt, filtro, 1);
  299.             rs = pstmt.executeQuery();
  300.            
  301.             if(rs.next()){
  302.                 contatore = rs.getInt("countTracce");  
  303.             }
  304.             rs.close();
  305.             pstmt.close();
  306.            
  307.             this.logDebug("Query found "+contatore+" rows ");
  308.            
  309.             return contatore;
  310.            
  311.         }catch(Exception e){
  312.             throw new DriverTracciamentoException(TRACCIA_EXCEPTION_PREFIX+e.getMessage(),e);
  313.         }finally{
  314.             JDBCUtilities.closeResources(rs, pstmt);
  315.             try{
  316.                 if(this.connection==null &&
  317.                     connectionDB!=null) {
  318.                     JDBCUtilities.closeConnection(BasicComponentFactory.getCheckLogger(), connectionDB, BasicComponentFactory.isCheckAutocommit(), BasicComponentFactory.isCheckIsClosed());
  319.                 }
  320.             }catch(Exception eClose){
  321.                 // close
  322.             }
  323.         }
  324.     }
  325.    
  326.    
  327.     /**
  328.      * Si occupa di ritornare le tracce che rispettano il filtro di ricerca
  329.      *
  330.      * @param filtro Filtro di ricerca
  331.      * @return tracce che rispettano il filtro di ricerca
  332.      *
  333.      */
  334.     @Override
  335.     public List<Traccia> getTracce(FiltroRicercaTracceConPaginazione filtro)  throws DriverTracciamentoException, DriverTracciamentoNotFoundException{
  336.         Connection connectionDB = null;
  337.         PreparedStatement pstmt = null;
  338.         ResultSet rs = null;
  339.         List<Traccia> tracce = new ArrayList<>();
  340.        
  341.         try{
  342.            
  343.             // Get Connection
  344.             if(this.connection!=null)
  345.                 connectionDB = this.connection;
  346.             else{
  347.                 connectionDB = this.datasource.getConnection();
  348.             }
  349.            
  350.             checkConnection(connectionDB);
  351.            
  352.             ISQLQueryObject sqlQueryObject =
  353.                 TracciaDriverUtilities.createSQLQueryObj_searchTracce(filtro, this.tipoDatabase);
  354.             String sql = sqlQueryObject.createSQLQuery();
  355.            
  356.             StringWrapper sqlDebug = new StringWrapper(sqlQueryObject.createSQLQuery());
  357.             TracciaDriverUtilities.setValuesSearch(sqlDebug, filtro, 1);
  358.             this.logDebug("Query: "+sqlDebug);
  359.            
  360.             pstmt = connectionDB.prepareStatement(sql);
  361.             TracciaDriverUtilities.setValuesSearch(pstmt, filtro, 1);
  362.             rs = pstmt.executeQuery();
  363.            
  364.             while(rs.next()){
  365.                
  366.                 Long idTraccia = rs.getLong("idTraccia");
  367.                 Traccia traccia =
  368.                     TracciaDriverUtilities.getTraccia(connectionDB, this.tipoDatabase,
  369.                             this.log, idTraccia, this.properties,
  370.                             this.protocolliRegistrati);
  371.                 tracce.add(traccia);
  372.             }
  373.             rs.close();
  374.             pstmt.close();
  375.            
  376.             this.logDebug("Query found "+tracce.size()+" rows");
  377.            
  378.             if(!tracce.isEmpty())
  379.                 return tracce;
  380.             else
  381.                 throw new DriverTracciamentoNotFoundException("Non sono state trovate tracce che rispettano i criteri di ricerca impostati");
  382.            
  383.         }catch(DriverTracciamentoNotFoundException e){
  384.             throw e;
  385.         }catch(Exception e){
  386.             throw new DriverTracciamentoException(TRACCIA_EXCEPTION_PREFIX+e.getMessage(),e);
  387.         }finally{
  388.             JDBCUtilities.closeResources(rs, pstmt);
  389.             try{
  390.                 if(this.connection==null &&
  391.                     connectionDB!=null) {
  392.                     JDBCUtilities.closeConnection(BasicComponentFactory.getCheckLogger(), connectionDB, BasicComponentFactory.isCheckAutocommit(), BasicComponentFactory.isCheckIsClosed());
  393.                 }
  394.             }catch(Exception eClose){
  395.                 // close
  396.             }
  397.         }
  398.     }
  399.    
  400.     /**
  401.      * Si occupa di eliminare le tracce che rispettano il filtro di ricerca
  402.      *
  403.      * @param filtro Filtro di ricerca
  404.      * @return numero di tracce eliminate
  405.      * @throws DriverTracciamentoException
  406.      */
  407.     @Override
  408.     public int deleteTracce(FiltroRicercaTracce filtro) throws DriverTracciamentoException{
  409.         int deleted = 0;
  410.         Connection con = getConnection();
  411.         PreparedStatement stmt = null;
  412.        
  413.         try {
  414.            
  415.             ISQLQueryObject sqlQueryObject =
  416.                 TracciaDriverUtilities.createSQLQueryObj_deleteTracce(filtro, this.tipoDatabase);
  417.             String sql = sqlQueryObject.createSQLDelete();
  418.            
  419.             StringWrapper sqlDebug = new StringWrapper(sqlQueryObject.createSQLDelete());
  420.             TracciaDriverUtilities.setValuesSearch(sqlDebug, filtro, 1);
  421.             this.logDebug("Delete: "+sqlDebug);
  422.            
  423.             stmt = con.prepareStatement(sql);
  424.             TracciaDriverUtilities.setValuesSearch(stmt, filtro, 1);
  425.             deleted = stmt.executeUpdate();
  426.             this.logDebug("Deleted "+deleted+" rows");
  427.            
  428.             return deleted;
  429.            
  430.         } catch (Exception e) {
  431.             throw new DriverTracciamentoException(e);
  432.         } finally {
  433.             if(stmt!=null)
  434.                 try{ stmt.close(); } catch (SQLException e) {
  435.                     // close
  436.                 }
  437.             releaseConnection(con);
  438.             this.logDebug("Tracce Eliminate: " + deleted );
  439.         }
  440.     }
  441.    
  442.    
  443.    
  444.    
  445.    
  446.    
  447.    

  448.    
  449.     /* ******* ACCESSI PUNTUALI ********** */
  450.    
  451.     /**
  452.      * Recupera la traccia
  453.      *
  454.      * @return Traccia
  455.      * @throws DriverMsgDiagnosticiException
  456.      */
  457.     @Override
  458.     public Traccia getTraccia(String idBusta,IDSoggetto codicePorta) throws DriverTracciamentoException, DriverTracciamentoNotFoundException{
  459.         return getTracciaEngine(idBusta,codicePorta,false);
  460.     }
  461.    
  462.     /**
  463.      * Recupera la traccia
  464.      *
  465.      * @return Traccia
  466.      * @throws DriverMsgDiagnosticiException
  467.      */
  468.     @Override
  469.     public Traccia getTraccia(String idBusta,IDSoggetto codicePorta,boolean ricercaIdBustaComeRiferimentoMessaggio) throws DriverTracciamentoException, DriverTracciamentoNotFoundException{
  470.         return getTracciaEngine(idBusta,codicePorta,ricercaIdBustaComeRiferimentoMessaggio);
  471.     }
  472.    
  473.     private Traccia getTracciaEngine(String idBusta,IDSoggetto codicePorta,boolean rifMsg) throws DriverTracciamentoException, DriverTracciamentoNotFoundException{
  474.        
  475.         Connection connectionDB = null;
  476.         PreparedStatement pstmt = null;
  477.         ResultSet rs = null;
  478.         Traccia tr = null;
  479.        
  480.         try{
  481.            
  482.             // Get Connection
  483.             if(this.connection!=null)
  484.                 connectionDB = this.connection;
  485.             else{
  486.                 connectionDB = this.datasource.getConnection();
  487.             }
  488.            
  489.             checkConnection(connectionDB);
  490.            
  491.             ISQLQueryObject sqlQueryObject = TracciaDriverUtilities.getSQLQueryObject(this.tipoDatabase);
  492.             sqlQueryObject.addFromTable(CostantiDB.TRACCE);
  493.             sqlQueryObject.addSelectField(CostantiDB.TRACCE_COLUMN_ID);
  494.             if(rifMsg){
  495.                 sqlQueryObject.addWhereCondition(CostantiDB.TRACCE_COLUMN_RIFERIMENTO_MESSAGGIO+"=?");
  496.             }else{
  497.                 sqlQueryObject.addWhereCondition(CostantiDB.TRACCE_COLUMN_ID_MESSAGGIO+"=?");
  498.             }
  499.             if(TracciaDriverUtilities.isDefined(codicePorta)){
  500.                 if(TracciaDriverUtilities.isDefined(codicePorta.getCodicePorta())){
  501.                     sqlQueryObject.addWhereCondition(CostantiDB.TRACCE_COLUMN_PDD_CODICE+"=?");
  502.                 }
  503.                 if(TracciaDriverUtilities.isDefined(codicePorta.getTipo())){
  504.                     sqlQueryObject.addWhereCondition(CostantiDB.TRACCE_COLUMN_PDD_TIPO_SOGGETTO+"=?");
  505.                 }
  506.                 if(TracciaDriverUtilities.isDefined(codicePorta.getNome())){
  507.                     sqlQueryObject.addWhereCondition(CostantiDB.TRACCE_COLUMN_PDD_NOME_SOGGETTO+"=?");
  508.                 }
  509.             }
  510.             sqlQueryObject.setANDLogicOperator(true);
  511.            
  512.             pstmt = connectionDB.prepareStatement(sqlQueryObject.toString());
  513.             int index = 1;
  514.             pstmt.setString(index++, idBusta);
  515.             if(TracciaDriverUtilities.isDefined(codicePorta)){
  516.                 if(TracciaDriverUtilities.isDefined(codicePorta.getCodicePorta())){
  517.                     pstmt.setString(index++, codicePorta.getCodicePorta());
  518.                 }
  519.                 if(TracciaDriverUtilities.isDefined(codicePorta.getTipo())){
  520.                     pstmt.setString(index++, codicePorta.getTipo());
  521.                 }
  522.                 if(TracciaDriverUtilities.isDefined(codicePorta.getNome())){
  523.                     pstmt.setString(index++, codicePorta.getNome());
  524.                 }
  525.             }
  526.            
  527.             rs = pstmt.executeQuery();
  528.             if(rs.next()){
  529.                
  530.                 Long idTraccia = rs.getLong(CostantiDB.TRACCE_COLUMN_ID);
  531.                 tr =  TracciaDriverUtilities.getTraccia(connectionDB, this.tipoDatabase,
  532.                         this.log, idTraccia, this.properties, this.protocolliRegistrati);
  533.                
  534.             }
  535.             rs.close();
  536.             pstmt.close();
  537.            
  538.             if(tr==null){
  539.                 throw new DriverTracciamentoNotFoundException("Traccia non trovata (idBusta:"+idBusta+") (dominio-porta:"+codicePorta+") (rifmsg:"+rifMsg+")");
  540.             }
  541.             return tr;
  542.            
  543.         }catch(DriverTracciamentoNotFoundException d){
  544.             throw d;
  545.         }catch(Exception e){
  546.             throw new DriverTracciamentoException(TRACCIA_EXCEPTION_PREFIX+e.getMessage(),e);
  547.         }finally{
  548.             JDBCUtilities.closeResources(rs, pstmt);
  549.             try{
  550.                 if(this.connection==null &&
  551.                     connectionDB!=null) {
  552.                     JDBCUtilities.closeConnection(BasicComponentFactory.getCheckLogger(), connectionDB, BasicComponentFactory.isCheckAutocommit(), BasicComponentFactory.isCheckIsClosed());
  553.                 }
  554.             }catch(Exception eClose){
  555.                 // close
  556.             }
  557.         }
  558.     }
  559.    
  560.     /**
  561.      * Recupera la traccia in base all'id di transazione
  562.      *
  563.      * @return Traccia
  564.      * @throws DriverMsgDiagnosticiException
  565.      */
  566.     @Override
  567.     public Traccia getTraccia(String idTransazione, RuoloMessaggio tipoTraccia) throws DriverTracciamentoException, DriverTracciamentoNotFoundException{
  568.        
  569.         Connection connectionDB = null;
  570.         PreparedStatement pstmt = null;
  571.         ResultSet rs = null;
  572.         Traccia tr = null;
  573.        
  574.         try{
  575.            
  576.             // Get Connection
  577.             if(this.connection!=null)
  578.                 connectionDB = this.connection;
  579.             else{
  580.                 connectionDB = this.datasource.getConnection();
  581.             }
  582.             checkConnection(connectionDB);
  583.            
  584.             ISQLQueryObject sqlQueryObject = TracciaDriverUtilities.getSQLQueryObject(this.tipoDatabase);
  585.             sqlQueryObject.addFromTable(CostantiDB.TRACCE);
  586.             sqlQueryObject.addSelectField(CostantiDB.TRACCE_COLUMN_ID);
  587.             sqlQueryObject.addWhereCondition(CostantiDB.TRACCE_COLUMN_ID_TRANSAZIONE+"=?");
  588.             sqlQueryObject.addWhereCondition(CostantiDB.TRACCE_COLUMN_TIPO_MESSAGGIO+"=?");
  589.             sqlQueryObject.setANDLogicOperator(true);
  590.            
  591.             pstmt = connectionDB.prepareStatement(sqlQueryObject.toString());
  592.             int index = 1;
  593.             pstmt.setString(index++, idTransazione);
  594.             pstmt.setString(index++, tipoTraccia.toString());
  595.             rs = pstmt.executeQuery();
  596.             if(rs.next()){
  597.                
  598.                 Long idTraccia = rs.getLong(CostantiDB.TRACCE_COLUMN_ID);
  599.                 tr =  TracciaDriverUtilities.getTraccia(connectionDB, this.tipoDatabase,
  600.                         this.log, idTraccia, this.properties, this.protocolliRegistrati);

  601.             }
  602.             rs.close();
  603.             pstmt.close();
  604.            
  605.             if(tr==null){
  606.                 throw new DriverTracciamentoNotFoundException("Traccia non trovata (idTransazione:"+idTransazione+") (tipo:"+tipoTraccia+")");
  607.             }
  608.             return tr;
  609.            
  610.         }catch(DriverTracciamentoNotFoundException d){
  611.             throw d;
  612.         }catch(Exception e){
  613.             throw new DriverTracciamentoException(TRACCIA_EXCEPTION_PREFIX+e.getMessage(),e);
  614.         }finally{
  615.             JDBCUtilities.closeResources(rs, pstmt);
  616.             try{
  617.                 if(this.connection==null &&
  618.                     connectionDB!=null) {
  619.                     JDBCUtilities.closeConnection(BasicComponentFactory.getCheckLogger(), connectionDB, BasicComponentFactory.isCheckAutocommit(), BasicComponentFactory.isCheckIsClosed());
  620.                 }
  621.             }catch(Exception eClose){
  622.                 // close
  623.             }
  624.         }  
  625.     }
  626.    
  627.     /**
  628.      * Recupera la traccia in base ad una serie di properties
  629.      *
  630.      * @return Traccia
  631.      * @throws DriverMsgDiagnosticiException
  632.      */
  633.     @Override
  634.     public Traccia getTraccia(RuoloMessaggio tipoTraccia,Map<String, String> propertiesRicerca) throws DriverTracciamentoException, DriverTracciamentoNotFoundException{
  635.        
  636.         Connection connectionDB = null;
  637.         PreparedStatement pstmt = null;
  638.         ResultSet rs = null;
  639.         Traccia tr = null;
  640.        
  641.         try{
  642.            
  643.             // Get Connection
  644.             if(this.connection!=null)
  645.                 connectionDB = this.connection;
  646.             else{
  647.                 connectionDB = this.datasource.getConnection();
  648.             }
  649.             checkConnection(connectionDB);
  650.            
  651.             ISQLQueryObject sqlQueryObject = TracciaDriverUtilities.getSQLQueryObject(this.tipoDatabase);
  652.             sqlQueryObject.addFromTable(CostantiDB.TRACCE);
  653.             sqlQueryObject.addSelectField(CostantiDB.TRACCE_COLUMN_ID);
  654.             if(propertiesRicerca!=null && propertiesRicerca.size()>0){
  655.                 for (String key : propertiesRicerca.keySet()) {
  656.                     if(TracciaDriver.IDTRACCIA.equals(key)){
  657.                         // Caso particolare dell'id long della traccia
  658.                         sqlQueryObject.addWhereCondition(CostantiDB.TRACCE_COLUMN_ID+"=?");
  659.                     }else{
  660.                         sqlQueryObject.addWhereCondition(key+"=?");
  661.                     }
  662.                 }
  663.             }else{
  664.                 throw new ProtocolException("Properties di ricerca non fornite");
  665.             }
  666.             sqlQueryObject.addWhereCondition(CostantiDB.TRACCE_COLUMN_TIPO_MESSAGGIO+"=?");
  667.             sqlQueryObject.setANDLogicOperator(true);
  668.            
  669.             pstmt = connectionDB.prepareStatement(sqlQueryObject.toString());
  670.             int index = 1;
  671.             for (String key : propertiesRicerca.keySet()) {
  672.                 String value = propertiesRicerca.get(key);
  673.                 if(TracciaDriver.IDTRACCIA.equals(key)){
  674.                     // Caso particolare dell'id long della traccia
  675.                     pstmt.setLong(index, Long.parseLong(value));
  676.                 }else{
  677.                     pstmt.setString(index, value);
  678.                 }
  679.                 index++;
  680.             }
  681.             pstmt.setString(index, tipoTraccia.toString());
  682.             rs = pstmt.executeQuery();
  683.             if(rs.next()){
  684.                
  685.                 Long idTraccia = rs.getLong(CostantiDB.TRACCE_COLUMN_ID);
  686.                 tr =  TracciaDriverUtilities.getTraccia(connectionDB, this.tipoDatabase,
  687.                         this.log, idTraccia, this.properties, this.protocolliRegistrati);

  688.             }
  689.             rs.close();
  690.             pstmt.close();
  691.            
  692.             if(tr==null){
  693.                 throw new DriverTracciamentoNotFoundException("Traccia non trovata (sizeProperties:"+propertiesRicerca.size()+") (tipo:"+tipoTraccia+")");
  694.             }
  695.             return tr;
  696.            
  697.         }catch(DriverTracciamentoNotFoundException d){
  698.             throw d;
  699.         }catch(Exception e){
  700.             throw new DriverTracciamentoException(TRACCIA_EXCEPTION_PREFIX+e.getMessage(),e);
  701.         }finally{
  702.             JDBCUtilities.closeResources(rs, pstmt);
  703.             try{
  704.                 if(this.connection==null &&
  705.                     connectionDB!=null) {
  706.                     JDBCUtilities.closeConnection(BasicComponentFactory.getCheckLogger(), connectionDB, BasicComponentFactory.isCheckAutocommit(), BasicComponentFactory.isCheckIsClosed());
  707.                 }
  708.             }catch(Exception eClose){
  709.                 // close
  710.             }
  711.         }
  712.     }
  713.    
  714.    
  715.    
  716.    
  717.    
  718.     /* ******* RISORSE INTERNE ********** */
  719.    
  720.     @Override
  721.     public void close() throws DriverTracciamentoException {
  722.         try{
  723.             if(this.connectionOpenViaJDBCInCostructor &&
  724.                 this.connection!=null && !this.connection.isClosed()){
  725.                 JDBCUtilities.closeConnection(BasicComponentFactory.getCheckLogger(), this.connection, BasicComponentFactory.isCheckAutocommit(), BasicComponentFactory.isCheckIsClosed());
  726.             }
  727.         }catch(Exception e){
  728.             throw new DriverTracciamentoException(e.getMessage(),e);
  729.         }
  730.     }
  731.    
  732.    
  733.    
  734.    
  735.    
  736.    
  737.    
  738.     /* ------------- UTILITY INTERNE -------------------------- */
  739.    
  740.    
  741.     private void checkConnection(Connection connectionDB) throws ProtocolException {
  742.         if(connectionDB==null &&
  743.             this.connection==null) {
  744.             throw new ProtocolException("Connection non ottenuta dal datasource["+this.datasource+"]");
  745.         }
  746.     }

  747.     /**
  748.      * Viene chiamato in causa per ottenere una connessione al DB
  749.      */
  750.     public java.sql.Connection getConnection() {
  751.         if (this.datasource == null) {
  752.             return null;
  753.         }

  754.         Connection connectionDB = null;
  755.         try {
  756.             connectionDB = this.datasource.getConnection();
  757.         } catch (Exception e) {
  758.             return null;
  759.         }

  760.         return connectionDB;
  761.     }

  762.     /**
  763.      * Viene chiamato in causa per rilasciare una connessione al DB, effettuando
  764.      * precedentemente un commit
  765.      *
  766.      * @param connectionDB
  767.      *            Connessione da rilasciare.
  768.      */
  769.     public void releaseConnection(java.sql.Connection connectionDB) {
  770.         try {
  771.             JDBCUtilities.closeConnection(BasicComponentFactory.getCheckLogger(), connectionDB, BasicComponentFactory.isCheckAutocommit(), BasicComponentFactory.isCheckIsClosed());
  772.         } catch (SQLException e) {
  773.             // ignore
  774.         }
  775.     }

  776.     /**
  777.      * Viene chiamato in causa per ottenere il tipoDatabase
  778.      */
  779.     public String getTipoDatabase() {
  780.         return this.tipoDatabase;
  781.     }

  782.    
  783. }