TracciaProducer.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.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.util.ArrayList;
  25. import java.util.Date;
  26. import java.util.List;

  27. import org.openspcoop2.core.config.OpenspcoopAppender;
  28. import org.openspcoop2.core.constants.CostantiDB;
  29. import org.openspcoop2.core.id.IDSoggetto;
  30. import org.openspcoop2.protocol.basic.BasicConnectionResult;
  31. import org.openspcoop2.protocol.basic.BasicProducer;
  32. import org.openspcoop2.protocol.basic.BasicProducerType;
  33. import org.openspcoop2.protocol.sdk.Allegato;
  34. import org.openspcoop2.protocol.sdk.Busta;
  35. import org.openspcoop2.protocol.sdk.Eccezione;
  36. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  37. import org.openspcoop2.protocol.sdk.ProtocolException;
  38. import org.openspcoop2.protocol.sdk.Riscontro;
  39. import org.openspcoop2.protocol.sdk.Trasmissione;
  40. import org.openspcoop2.protocol.sdk.constants.TipoSerializzazione;
  41. import org.openspcoop2.protocol.sdk.tracciamento.ITracciaProducer;
  42. import org.openspcoop2.protocol.sdk.tracciamento.Traccia;
  43. import org.openspcoop2.protocol.sdk.tracciamento.TracciamentoException;
  44. import org.openspcoop2.utils.TipiDatabase;
  45. import org.openspcoop2.utils.jdbc.CustomKeyGeneratorObject;
  46. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKey;
  47. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKeyJDBCType;
  48. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKeyObject;
  49. import org.openspcoop2.utils.jdbc.JDBCUtilities;

  50. /**
  51.  * Contiene l'implementazione di un appender personalizzato,
  52.  * per la registrazione dei tracciamenti su database.
  53.  *
  54.  * @author Poli Andrea (apoli@link.it)
  55.  * @author $Author$
  56.  * @version $Rev$, $Date$
  57.  */

  58. public class TracciaProducer extends BasicProducer implements ITracciaProducer{

  59.     public TracciaProducer(IProtocolFactory<?> factory) throws ProtocolException{
  60.         super(factory, BasicProducerType.TRACCE);
  61.     }

  62.     /**
  63.      * Inizializza l'engine di un appender per la registrazione
  64.      * di un tracciamento emesso da una porta di dominio.
  65.      *
  66.      * @param appenderProperties Proprieta' dell'appender
  67.      * @throws TracciamentoException
  68.      */
  69.     @Override
  70.     public void initializeAppender(OpenspcoopAppender appenderProperties) throws TracciamentoException{
  71.         try{
  72.             this.initializeAppender(appenderProperties, true);
  73.         }catch(Exception e){
  74.             throw new TracciamentoException("Errore durante l'inizializzazione dell'appender: "+e.getMessage(),e);
  75.         }
  76.     }



  77.     /**
  78.      * Registra una traccia prodotta da una porta di dominio, utilizzando le informazioni definite dalla specifica SPC.
  79.      *
  80.      * @param conOpenSPCoopPdD Connessione verso il database
  81.      * @param traccia Traccia
  82.      * @throws TracciamentoException
  83.      */
  84.     @Override
  85.     public void log(Connection conOpenSPCoopPdD, Traccia traccia) throws TracciamentoException{

  86.         if(traccia==null)
  87.             throw new TracciamentoException("Errore durante il tracciamento: traccia is null");

  88.         Busta busta = traccia.getBusta();
  89.         if(busta==null)
  90.             throw new TracciamentoException("Errore durante il tracciamento di un busta: busta is null");

  91.         Date gdo = traccia.getGdo();
  92.         IDSoggetto idSoggetto = traccia.getIdSoggetto();
  93.         String tipoMessaggio = traccia.getTipoMessaggio().getTipo();
  94.         String location = traccia.getLocation();
  95.         String idCorrelazioneApplicativa = traccia.getCorrelazioneApplicativa();
  96.         String idCorrelazioneApplicativaRisposta = traccia.getCorrelazioneApplicativaRisposta();
  97.        
  98.         if(this.debug){
  99.             this.log.debug("@@ log["+busta.getID()+"] ....");
  100.         }
  101.        
  102.         PreparedStatement stmt = null;
  103.         ResultSet rs = null;
  104.         Connection con = null;
  105.         BasicConnectionResult cr = null;
  106.         try{
  107.             //  Connessione al DB
  108.             cr = this.getConnection(conOpenSPCoopPdD,"traccia.log");
  109.             con = cr.getConnection();

  110.             if(this.debug){
  111.                 this.log.debug("@@ log["+busta.getID()+"] (getConnection finished) ....");
  112.             }
  113.            
  114.             //Inserimento della traccia nel DB
  115.             if(!TipiDatabase.isAMember(this.tipoDatabase)){
  116.                 throw new TracciamentoException("Tipo database ["+this.tipoDatabase+"] non supportato");
  117.             }
  118.             TipiDatabase tipo = TipiDatabase.toEnumConstant(this.tipoDatabase);
  119.             // ** Preparazione parametri
  120.             java.sql.Timestamp gdoT = new java.sql.Timestamp(gdo.getTime());
  121.             java.sql.Timestamp oraRegistrazioneT = null;
  122.             java.sql.Timestamp scadenzaT = null;
  123.             if(busta.getOraRegistrazione()!=null)
  124.                 oraRegistrazioneT = new java.sql.Timestamp(busta.getOraRegistrazione().getTime());
  125.             if (busta.getScadenza() != null)
  126.                 scadenzaT =  new java.sql.Timestamp(busta.getScadenza().getTime());
  127.             int confermaRicezione = -1;
  128.             if(busta.isConfermaRicezione())
  129.                 confermaRicezione = 1;
  130.             else
  131.                 confermaRicezione = 0 ;
  132.            
  133.             String headerProtocollo = null;
  134.             if(traccia.getBustaAsRawContent()!=null){
  135.                 try{
  136.                     headerProtocollo = traccia.getBustaAsRawContent().toString(TipoSerializzazione.DEFAULT);
  137.                 }catch(Exception e){
  138.                     throw new Exception("Serializzazione RawContent non riuscita: "+e.getMessage(),e);
  139.                 }
  140.             }
  141.             else if(traccia.getBustaAsString()!=null){
  142.                 headerProtocollo = traccia.getBustaAsString();
  143.             }else if(traccia.getBustaAsByteArray()!=null){
  144.                 headerProtocollo = new String(traccia.getBustaAsByteArray());
  145.             }else{
  146.                 // ProtocolFactoryManager non utilizzabile in questo package
  147.                 /*
  148.                 headerProtocollo =
  149.                         ProtocolFactoryManager.getInstance().
  150.                             getProtocolFactoryByName(traccia.getProtocollo()).
  151.                                 createBustaBuilder().toString(busta, org.openspcoop2.protocol.sdk.constants.TipoTraccia.RICHIESTA.equals(traccia.getTipoMessaggio()));
  152.                                 */
  153.             }
  154.            
  155.             if(this.debug){
  156.                 this.log.debug("@@ log["+busta.getID()+"] (prima inserimento traccia) ....");
  157.             }
  158.             List<InsertAndGeneratedKeyObject> listInsertAndGeneratedKeyObject = new ArrayList<>();
  159.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_GDO, gdoT , InsertAndGeneratedKeyJDBCType.TIMESTAMP) );
  160.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_GDO_INT, gdoT.getTime(), InsertAndGeneratedKeyJDBCType.LONG) );
  161.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_PDD_CODICE, idSoggetto.getCodicePorta(), InsertAndGeneratedKeyJDBCType.STRING) );
  162.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_PDD_TIPO_SOGGETTO, idSoggetto.getTipo(), InsertAndGeneratedKeyJDBCType.STRING) );
  163.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_PDD_NOME_SOGGETTO, idSoggetto.getNome(), InsertAndGeneratedKeyJDBCType.STRING) );
  164.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_PDD_RUOLO, traccia.getTipoPdD().getTipo(), InsertAndGeneratedKeyJDBCType.STRING) );
  165.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_TIPO_MESSAGGIO, tipoMessaggio, InsertAndGeneratedKeyJDBCType.STRING) );
  166.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_ESITO_ELABORAZIONE, traccia.getEsitoElaborazioneMessaggioTracciato().getEsito().toString(), InsertAndGeneratedKeyJDBCType.STRING) );
  167.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_DETTAGLIO_ESITO_ELABORAZIONE, traccia.getEsitoElaborazioneMessaggioTracciato().getDettaglio(), InsertAndGeneratedKeyJDBCType.STRING) );
  168.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_MITTENTE_IDPORTA, busta.getIdentificativoPortaMittente(), InsertAndGeneratedKeyJDBCType.STRING) );
  169.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_MITTENTE_NOME, busta.getMittente(), InsertAndGeneratedKeyJDBCType.STRING) );
  170.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_MITTENTE_TIPO, busta.getTipoMittente(), InsertAndGeneratedKeyJDBCType.STRING) );
  171.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_MITTENTE_INDIRIZZO, busta.getIndirizzoMittente(), InsertAndGeneratedKeyJDBCType.STRING) );
  172.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_DESTINATARIO_IDPORTA, busta.getIdentificativoPortaDestinatario(), InsertAndGeneratedKeyJDBCType.STRING) );
  173.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_DESTINATARIO_NOME, busta.getDestinatario(), InsertAndGeneratedKeyJDBCType.STRING) );
  174.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_DESTINATARIO_TIPO, busta.getTipoDestinatario(), InsertAndGeneratedKeyJDBCType.STRING) );
  175.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_DESTINATARIO_INDIRIZZO, busta.getIndirizzoDestinatario(), InsertAndGeneratedKeyJDBCType.STRING) );
  176.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_PROFILO_COLLABORAZIONE, busta.getProfiloDiCollaborazioneValue(), InsertAndGeneratedKeyJDBCType.STRING) );
  177.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_PROFILO_COLLABORAZIONE_SDK_CONSTANT, busta.getProfiloDiCollaborazione() == null ? null : busta.getProfiloDiCollaborazione().getEngineValue(), InsertAndGeneratedKeyJDBCType.STRING) );
  178.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_SERVIZIO_CORRELATO_NOME, busta.getServizioCorrelato(), InsertAndGeneratedKeyJDBCType.STRING) );
  179.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_SERVIZIO_CORRELATO_TIPO, busta.getTipoServizioCorrelato(), InsertAndGeneratedKeyJDBCType.STRING) );
  180.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_COLLABORAZIONE, busta.getCollaborazione(), InsertAndGeneratedKeyJDBCType.STRING) );
  181.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_SERVIZIO_VERSIONE, busta.getVersioneServizio(), InsertAndGeneratedKeyJDBCType.INT) );
  182.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_SERVIZIO_NOME, busta.getServizio(), InsertAndGeneratedKeyJDBCType.STRING) );
  183.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_SERVIZIO_TIPO, busta.getTipoServizio(), InsertAndGeneratedKeyJDBCType.STRING) );
  184.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_AZIONE, busta.getAzione(), InsertAndGeneratedKeyJDBCType.STRING) );
  185.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_ID_MESSAGGIO, busta.getID(), InsertAndGeneratedKeyJDBCType.STRING) );
  186.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_ORA_REGISTRAZIONE,oraRegistrazioneT,InsertAndGeneratedKeyJDBCType.TIMESTAMP) );
  187.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_ORA_REGISTRAZIONE_TIPO, busta.getTipoOraRegistrazioneValue(), InsertAndGeneratedKeyJDBCType.STRING) );
  188.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_ORA_REGISTRAZIONE_TIPO_SDK_CONSTANT,  busta.getTipoOraRegistrazione() == null ? null : busta.getTipoOraRegistrazione().getEngineValue(), InsertAndGeneratedKeyJDBCType.STRING) );
  189.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_RIFERIMENTO_MESSAGGIO, busta.getRiferimentoMessaggio(), InsertAndGeneratedKeyJDBCType.STRING) );
  190.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_SCADENZA,scadenzaT,InsertAndGeneratedKeyJDBCType.TIMESTAMP) );
  191.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_INOLTRO, busta.getInoltroValue(), InsertAndGeneratedKeyJDBCType.STRING) );
  192.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_INOLTRO_SDK_CONSTANT, busta.getInoltro() == null ? null : busta.getInoltro().getEngineValue(), InsertAndGeneratedKeyJDBCType.STRING) );
  193.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_CONFERMA_RICEZIONE, confermaRicezione, InsertAndGeneratedKeyJDBCType.INT) );
  194.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_SEQUENZA, busta.getSequenza(), InsertAndGeneratedKeyJDBCType.LONG) );
  195.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_LOCATION, location, InsertAndGeneratedKeyJDBCType.STRING) );
  196.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_CORRELAZIONE_APPLICATIVA_RICHIESTA, idCorrelazioneApplicativa, InsertAndGeneratedKeyJDBCType.STRING) );
  197.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_CORRELAZIONE_APPLICATIVA_RISPOSTA, idCorrelazioneApplicativaRisposta, InsertAndGeneratedKeyJDBCType.STRING) );
  198.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_SA_FRUITORE, busta.getServizioApplicativoFruitore(), InsertAndGeneratedKeyJDBCType.STRING) );
  199.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_SA_EROGATORE, busta.getServizioApplicativoErogatore(), InsertAndGeneratedKeyJDBCType.STRING) );
  200.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_PROTOCOLLO, traccia.getProtocollo(), InsertAndGeneratedKeyJDBCType.STRING) );
  201.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_DIGEST, busta.getDigest(), InsertAndGeneratedKeyJDBCType.STRING) );
  202.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_SOAP, headerProtocollo, InsertAndGeneratedKeyJDBCType.STRING) );
  203.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(CostantiDB.TRACCE_COLUMN_ID_TRANSAZIONE, traccia.getIdTransazione(), InsertAndGeneratedKeyJDBCType.STRING) );
  204.            
  205.            
  206.             // ** Insert and return generated key
  207.             long idtraccia = InsertAndGeneratedKey.insertAndReturnGeneratedKey(con, tipo,
  208.                     new CustomKeyGeneratorObject(CostantiDB.TRACCE, CostantiDB.TRACCE_COLUMN_ID, CostantiDB.TRACCE_SEQUENCE, CostantiDB.TRACCE_TABLE_FOR_ID),
  209.                     listInsertAndGeneratedKeyObject.toArray(new InsertAndGeneratedKeyObject[1]));
  210.             if(idtraccia<=0){
  211.                 throw new Exception("ID autoincrementale non ottenuto");
  212.             }
  213.             traccia.setId(idtraccia);

  214.             if(this.debug){
  215.                 this.log.debug("@@ log["+busta.getID()+"] (traccia inserita) ....");
  216.             }
  217.            
  218.            
  219.            
  220.             String sqlString = null;
  221.            
  222.             if(this.debug){
  223.                 this.log.debug("@@ log["+busta.getID()+"] (inserimento "+busta.sizeListaRiscontri()+" riscontri) ....");
  224.             }
  225.             for (int i = 0; i < busta.sizeListaRiscontri(); i++) {
  226.                 Riscontro riscontro = busta.getRiscontro(i);

  227.                 //Inserimento nel DB
  228.                 sqlString = "INSERT INTO "+CostantiDB.TRACCE_RISCONTRI+" ("+
  229.                         CostantiDB.TRACCE_RISCONTRI_COLUMN_ID_TRACCIA+", "+
  230.                         CostantiDB.TRACCE_RISCONTRI_COLUMN_ID_RISCONTRO+", "+
  231.                         CostantiDB.TRACCE_RISCONTRI_COLUMN_RICEVUTA+", "+
  232.                         CostantiDB.TRACCE_RISCONTRI_COLUMN_ORA_REGISTRAZIONE+", "+
  233.                         CostantiDB.TRACCE_RISCONTRI_COLUMN_ORA_REGISTRAZIONE_TIPO+", "+
  234.                         CostantiDB.TRACCE_RISCONTRI_COLUMN_ORA_REGISTRAZIONE_TIPO_SDK_CONSTANT+", "+
  235.                         CostantiDB.TRACCE_RISCONTRI_COLUMN_GDO+
  236.                         ") VALUES (?, ?, ?, ?, ?, ?, ?)";
  237.                 stmt = con.prepareStatement(sqlString);
  238.                 int index = 1;
  239.                 stmt.setLong(index++, idtraccia);
  240.                 JDBCUtilities.setSQLStringValue(stmt,index++, riscontro.getID());
  241.                 JDBCUtilities.setSQLStringValue(stmt,index++, riscontro.getRicevuta());
  242.                 if(riscontro.getOraRegistrazione()!=null)
  243.                     stmt.setTimestamp(index++, new java.sql.Timestamp(riscontro.getOraRegistrazione().getTime()));
  244.                 else
  245.                     stmt.setTimestamp(index++, null);
  246.                 JDBCUtilities.setSQLStringValue(stmt,index++, riscontro.getTipoOraRegistrazioneValue(this.protocolFactory));
  247.                 JDBCUtilities.setSQLStringValue(stmt,index++, riscontro.getTipoOraRegistrazione().getEngineValue());
  248.                 stmt.setTimestamp(index++, gdoT);
  249.                 stmt.executeUpdate();
  250.                 stmt.close();
  251.             }
  252.             if(this.debug){
  253.                 this.log.debug("@@ log["+busta.getID()+"] (inserimento "+busta.sizeListaRiscontri()+" riscontri effettuato) ....");
  254.             }
  255.            
  256.             if(this.debug){
  257.                 this.log.debug("@@ log["+busta.getID()+"] (inserimento "+busta.sizeListaTrasmissioni()+" trasmissioni) ....");
  258.             }
  259.             for (int i = 0; i < busta.sizeListaTrasmissioni(); i++) {
  260.                 Trasmissione trasmissione = busta.getTrasmissione(i);

  261.                 //Inserimento nel DB
  262.                 sqlString = "INSERT INTO "+CostantiDB.TRACCE_TRASMISSIONI+" ("+
  263.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_ID_TRACCIA+", "+
  264.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_ORIGINE+", "+
  265.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_ORIGINE_TIPO+", "+
  266.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_ORIGINE_INDIRIZZO+", "+
  267.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_ORIGINE_IDPORTA+", "+
  268.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_DESTINAZIONE+", "+
  269.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_DESTINAZIONE_TIPO+", "+
  270.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_DESTINAZIONE_INDIRIZZO+", "+
  271.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_DESTINAZIONE_IDPORTA+", "+
  272.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_ORA_REGISTRAZIONE+", "+
  273.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_ORA_REGISTRAZIONE_TIPO+", "+
  274.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_ORA_REGISTRAZIONE_TIPO_SDK_CONSTANT+", "+
  275.                         CostantiDB.TRACCE_TRASMISSIONI_COLUMN_GDO+
  276.                         ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  277.                 stmt = con.prepareStatement(sqlString);
  278.                 int index = 1;
  279.                 stmt.setLong(index++, idtraccia);
  280.                 JDBCUtilities.setSQLStringValue(stmt,index++, trasmissione.getOrigine());
  281.                 JDBCUtilities.setSQLStringValue(stmt,index++, trasmissione.getTipoOrigine());
  282.                 JDBCUtilities.setSQLStringValue(stmt,index++, trasmissione.getIndirizzoOrigine());
  283.                 JDBCUtilities.setSQLStringValue(stmt,index++, trasmissione.getIdentificativoPortaOrigine());
  284.                 JDBCUtilities.setSQLStringValue(stmt,index++, trasmissione.getDestinazione());
  285.                 JDBCUtilities.setSQLStringValue(stmt,index++, trasmissione.getTipoDestinazione());
  286.                 JDBCUtilities.setSQLStringValue(stmt,index++, trasmissione.getIndirizzoDestinazione());
  287.                 JDBCUtilities.setSQLStringValue(stmt,index++, trasmissione.getIdentificativoPortaDestinazione());
  288.                 if(trasmissione.getOraRegistrazione()!=null)
  289.                     stmt.setTimestamp(index++, new java.sql.Timestamp(trasmissione.getOraRegistrazione().getTime()));
  290.                 else
  291.                     stmt.setTimestamp(index++, null);
  292.                 JDBCUtilities.setSQLStringValue(stmt,index++, trasmissione.getTempoValue(this.protocolFactory));
  293.                 JDBCUtilities.setSQLStringValue(stmt,index++, trasmissione.getTempo().getEngineValue());
  294.                 stmt.setTimestamp(index++, gdoT);
  295.                 stmt.executeUpdate();
  296.                 stmt.close();
  297.             }
  298.             if(this.debug){
  299.                 this.log.debug("@@ log["+busta.getID()+"] (inserimento "+busta.sizeListaTrasmissioni()+" trasmissioni effettuato) ....");
  300.             }
  301.            
  302.             if(this.debug){
  303.                 this.log.debug("@@ log["+busta.getID()+"] (inserimento "+busta.sizeListaEccezioni()+" eccezioni) ....");
  304.             }
  305.             for (int i = 0; i < busta.sizeListaEccezioni(); i++) {
  306.                 Eccezione eccezione = busta.getEccezione(i);

  307.                 //Inserimento nel DB
  308.                 String subCodiceMeta = "";
  309.                 String subCodiceMetaInsert = "";
  310.                 if(eccezione.getSubCodiceEccezione()!=null && eccezione.getSubCodiceEccezione().getSubCodice()!=null){
  311.                     subCodiceMeta = CostantiDB.TRACCE_ECCEZIONI_COLUMN_CODICE_ECCEZIONE_SUBCOD_SDK_CONSTANT+", ";
  312.                     subCodiceMetaInsert = "?,";
  313.                 }
  314.                 sqlString = "INSERT INTO "+CostantiDB.TRACCE_ECCEZIONI+" ("+
  315.                         CostantiDB.TRACCE_ECCEZIONI_COLUMN_ID_TRACCIA+", "+
  316.                         CostantiDB.TRACCE_ECCEZIONI_COLUMN_CONTESTO_CODIFICA+", "+
  317.                         CostantiDB.TRACCE_ECCEZIONI_COLUMN_CONTESTO_CODIFICA_SDK_CONSTANT+", "+
  318.                         CostantiDB.TRACCE_ECCEZIONI_COLUMN_CODICE_ECCEZIONE+", "+
  319.                         CostantiDB.TRACCE_ECCEZIONI_COLUMN_CODICE_ECCEZIONE_SDK_CONSTANT+", "+
  320.                         subCodiceMeta+
  321.                         CostantiDB.TRACCE_ECCEZIONI_COLUMN_RILEVANZA+", "+
  322.                         CostantiDB.TRACCE_ECCEZIONI_COLUMN_RILEVANZA_SDK_CONSTANT+", "+
  323.                         CostantiDB.TRACCE_ECCEZIONI_COLUMN_POSIZIONE+", "+
  324.                         CostantiDB.TRACCE_ECCEZIONI_COLUMN_GDO+
  325.                         ") VALUES (?, ?, ?, ?, ?, "+subCodiceMetaInsert+" ?, ? , ? , ?)";
  326.                 stmt = con.prepareStatement(sqlString);
  327.                 int index = 1;
  328.                 stmt.setLong(index++, idtraccia);
  329.                 JDBCUtilities.setSQLStringValue(stmt,index++, eccezione.getContestoCodificaValue(this.protocolFactory));
  330.                 JDBCUtilities.setSQLStringValue(stmt,index++, eccezione.getContestoCodifica().getEngineValue());
  331.                 JDBCUtilities.setSQLStringValue(stmt,index++, eccezione.getCodiceEccezioneValue(this.protocolFactory));
  332.                 stmt.setInt(index++, eccezione.getCodiceEccezione().getCodice());
  333.                 if(eccezione.getSubCodiceEccezione()!=null && eccezione.getSubCodiceEccezione().getSubCodice()!=null)
  334.                     stmt.setInt(index++, eccezione.getSubCodiceEccezione().getSubCodice());
  335.                 JDBCUtilities.setSQLStringValue(stmt,index++, eccezione.getRilevanzaValue(this.protocolFactory));
  336.                 JDBCUtilities.setSQLStringValue(stmt,index++, eccezione.getRilevanza().getEngineValue());
  337.                 JDBCUtilities.setSQLStringValue(stmt,index++, eccezione.getDescrizione(this.protocolFactory));
  338.                 stmt.setTimestamp(index++, gdoT);
  339.                 stmt.executeUpdate();
  340.                 stmt.close();
  341.             }
  342.             if(this.debug){
  343.                 this.log.debug("@@ log["+busta.getID()+"] (inserimento "+busta.sizeListaEccezioni()+" eccezioni effettuato) ....");
  344.             }
  345.            
  346.             if(this.debug){
  347.                 this.log.debug("@@ log["+busta.getID()+"] (inserimento "+traccia.sizeListaAllegati()+" allegati) ....");
  348.             }
  349.             for (int i = 0; i < traccia.sizeListaAllegati(); i++) {
  350.                 Allegato allegato = traccia.getAllegato(i);

  351.                 //Inserimento nel DB
  352.                 sqlString = "INSERT INTO "+CostantiDB.TRACCE_ALLEGATI+" ("+
  353.                         CostantiDB.TRACCE_ALLEGATI_COLUMN_ID_TRACCIA+", "+
  354.                         CostantiDB.TRACCE_ALLEGATI_COLUMN_CONTENT_ID+", "+
  355.                         CostantiDB.TRACCE_ALLEGATI_COLUMN_CONTENT_LOCATION+", "+
  356.                         CostantiDB.TRACCE_ALLEGATI_COLUMN_CONTENT_TYPE+", "+
  357.                         CostantiDB.TRACCE_ALLEGATI_COLUMN_DIGEST+", "+
  358.                         CostantiDB.TRACCE_ALLEGATI_COLUMN_GDO+
  359.                         ") VALUES (?, ?, ?, ?, ?, ?)";
  360.                 stmt = con.prepareStatement(sqlString);
  361.                 int index = 1;
  362.                 stmt.setLong(index++, idtraccia);
  363.                 JDBCUtilities.setSQLStringValue(stmt,index++, allegato.getContentId());
  364.                 JDBCUtilities.setSQLStringValue(stmt,index++, allegato.getContentLocation());
  365.                 JDBCUtilities.setSQLStringValue(stmt,index++, allegato.getContentType());
  366.                 JDBCUtilities.setSQLStringValue(stmt,index++, allegato.getDigest());
  367.                 stmt.setTimestamp(index++, gdoT);
  368.                 stmt.executeUpdate();
  369.                 stmt.close();
  370.             }
  371.             if(this.debug){
  372.                 this.log.debug("@@ log["+busta.getID()+"] (inserimento "+traccia.sizeListaAllegati()+" allegati effettuato) ....");
  373.             }
  374.                        
  375.             String [] propertiesNames = busta.getPropertiesNames();
  376.             if(propertiesNames!=null){
  377.                 if(this.debug){
  378.                     this.log.debug("@@ log["+busta.getID()+"] (inserimento "+propertiesNames.length+" ext-protocol-info) ....");
  379.                 }
  380.                 for (int i = 0; i < propertiesNames.length; i++) {
  381.    
  382.                     String v = busta.getProperty(propertiesNames[i]);
  383.                                        
  384.                     int limit = 2800; // TRACCE_EXT_SEARCH ON tracce_ext_protocol_info (name,value) la somma di name e value deve essere minore di 3072 bytes per mysql
  385.                     if(TipiDatabase.POSTGRESQL.equals(this.tipoDatabase)) {
  386.                         // The maximum length for a value in a B-tree index, which includes primary keys, is one third of the size of a buffer page, by default floor(8192/3) = 2730 bytes
  387.                         // Altrimenti si ha un errore del genere:
  388.                         // Caused by: org.postgresql.util.PSQLException: ERROR: index row size 2864 exceeds maximum 2712 for index "tracce_ext_search"
  389.                         //  Hint: Values larger than 1/3 of a buffer page cannot be indexed.
  390.                         //  Consider a function index of an MD5 hash of the value, or use full text indexing.
  391.                         int sum = 20; // si parte da 20, la dimensione dell'id della join
  392.                         if(propertiesNames[i]!=null) {
  393.                             sum = sum + propertiesNames[i].length();
  394.                         }
  395.                         if(v!=null) {
  396.                             sum = sum + v.length();
  397.                         }
  398.                         if(sum>2711) {
  399.                             limit = (2711 - 20) - propertiesNames[i].length();
  400.                         }
  401.                     }
  402.                    
  403.                     String columnExtV = "";
  404.                     String valueExtV = "";
  405.                     if(v!=null && v.length()>limit) {
  406.                         columnExtV = CostantiDB.TRACCE_EXT_PROTOCOL_INFO_COLUMN_EXT_VALUE+", ";
  407.                         valueExtV = ", ?";
  408.                     }
  409.                    
  410.                     //Inserimento nel DB
  411.                     sqlString = "INSERT INTO "+CostantiDB.TRACCE_EXT_INFO+" ("+
  412.                         CostantiDB.TRACCE_EXT_PROTOCOL_INFO_COLUMN_ID_TRACCIA+", "+
  413.                         CostantiDB.TRACCE_EXT_PROTOCOL_INFO_COLUMN_NAME+", "+
  414.                         CostantiDB.TRACCE_EXT_PROTOCOL_INFO_COLUMN_VALUE+", "+
  415.                         columnExtV+
  416.                         CostantiDB.TRACCE_EXT_PROTOCOL_INFO_COLUMN_GDO+
  417.                     ") VALUES (?, ?, ?"+valueExtV+", ?)";
  418.                     stmt = con.prepareStatement(sqlString);
  419.                     int index = 1;
  420.                     stmt.setLong(index++, idtraccia);
  421.                     JDBCUtilities.setSQLStringValue(stmt,index++, propertiesNames[i]);
  422.                     if(v!=null && v.length()>limit) {
  423.                         JDBCUtilities.setSQLStringValue(stmt,index++, v.substring(0, limit));
  424.                         JDBCUtilities.setSQLStringValue(stmt,index++, v);
  425.                     }
  426.                     else {
  427.                         JDBCUtilities.setSQLStringValue(stmt,index++, v);
  428.                     }
  429.                     stmt.setTimestamp(index++, gdoT);
  430.                     stmt.executeUpdate();
  431.                     stmt.close();
  432.                
  433.                 }
  434.                 if(this.debug){
  435.                     this.log.debug("@@ log["+busta.getID()+"] (inserimento "+propertiesNames.length+" effettuato) ....");
  436.                 }
  437.             }
  438.            
  439.             if(this.debug){
  440.                 this.log.debug("@@ log["+busta.getID()+"] completato");
  441.             }

  442.         }catch(Exception e){
  443.             throw new TracciamentoException("Errore durante il tracciamento di una busta: "+e.getMessage()+"\nL'id del messaggio della busta era: "+busta.getID(),e);
  444.         }finally{
  445.             try{
  446.                 if(rs!=null)
  447.                     rs.close();
  448.             }catch(Exception e){
  449.                 // close
  450.             }
  451.             try{
  452.                 if(stmt!=null)
  453.                     stmt.close();
  454.             }catch(Exception e){
  455.                 // close
  456.             }
  457.             try{
  458.                 this.releaseConnection(cr, "traccia.log");
  459.             }catch(Exception e){
  460.                 // close
  461.             }
  462.         }
  463.     }


  464. }