NotifierDump.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.pdd.core.handlers.notifier.engine;

  21. import java.io.File;
  22. import java.io.InputStream;
  23. import java.sql.Connection;
  24. import java.sql.PreparedStatement;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;

  28. import org.openspcoop2.core.commons.dao.DAOFactory;
  29. import org.openspcoop2.core.commons.dao.DAOFactoryProperties;
  30. import org.openspcoop2.core.id.IDSoggetto;
  31. import org.openspcoop2.core.transazioni.DumpMessaggio;
  32. import org.openspcoop2.core.transazioni.constants.TipoMessaggio;
  33. import org.openspcoop2.core.transazioni.dao.jdbc.JDBCDumpMessaggioService;
  34. import org.openspcoop2.generic_project.expression.impl.sql.ISQLFieldConverter;
  35. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  36. import org.openspcoop2.pdd.config.DBTransazioniManager;
  37. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  38. import org.openspcoop2.pdd.config.Resource;
  39. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  40. import org.openspcoop2.protocol.sdk.tracciamento.TracciamentoException;
  41. import org.openspcoop2.utils.date.DateManager;
  42. import org.openspcoop2.utils.jdbc.JDBCAdapterFactory;
  43. import org.openspcoop2.utils.sql.ISQLQueryObject;
  44. import org.openspcoop2.utils.sql.SQLObjectFactory;
  45. import org.slf4j.Logger;

  46. /**    
  47.  * NotifierDump
  48.  *
  49.  * @author Poli Andrea (poli@link.it)
  50.  * @author $Author$
  51.  * @version $Rev$, $Date$
  52.  */
  53. public class NotifierDump {

  54.     // STATIC

  55.     private static NotifierDump staticInstance = null;
  56.     private static synchronized void initialize() throws TracciamentoException{
  57.         if(staticInstance==null){
  58.             try{

  59.                 OpenSPCoop2Properties op2Properties = OpenSPCoop2Properties.getInstance();
  60.                
  61.                 String tipoDatabase = op2Properties.getDatabaseType();
  62.                 if(tipoDatabase==null){
  63.                     throw new TracciamentoException("Tipo Database non definito");
  64.                 }

  65.                 boolean debug = op2Properties.isTransazioniDebug();
  66.                 Logger daoFactoryLogger = null;
  67.                 DAOFactory daoFactory = null;
  68.                 ServiceManagerProperties daoFactoryServiceManagerPropertiesTransazioni = null;
  69.                 try{
  70.                     DAOFactoryProperties daoFactoryProperties = null;
  71.                     daoFactoryLogger = OpenSPCoop2Logger.getLoggerOpenSPCoopTransazioniSql(debug);
  72.                     daoFactory = DAOFactory.getInstance(daoFactoryLogger);
  73.                     daoFactoryProperties = DAOFactoryProperties.getInstance(daoFactoryLogger);
  74.                     daoFactoryServiceManagerPropertiesTransazioni = daoFactoryProperties.getServiceManagerProperties(org.openspcoop2.core.transazioni.utils.ProjectInfo.getInstance());
  75.                     daoFactoryServiceManagerPropertiesTransazioni.setShowSql(debug);    
  76.                     daoFactoryServiceManagerPropertiesTransazioni.setDatabaseType(DBTransazioniManager.getInstance().getTipoDatabase());
  77.                 }catch(Exception e){
  78.                     throw new TracciamentoException("Errore durante l'inizializzazione del daoFactory: "+e.getMessage(),e);
  79.                 }
  80.                
  81.                 // GestoreDump
  82.                 staticInstance = new NotifierDump(debug,
  83.                         //ds,
  84.                         tipoDatabase, daoFactory, daoFactoryServiceManagerPropertiesTransazioni, daoFactoryLogger);

  85.             }catch(Exception e){
  86.                 throw new TracciamentoException("Errore durante l'inizializzazione del NotifierDump: "+e.getMessage(),e);
  87.             }
  88.         }
  89.     }
  90.     public static NotifierDump getInstance() throws TracciamentoException {
  91.         if(staticInstance==null){
  92.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  93.             synchronized (NotifierDump.class) {
  94.                 initialize();
  95.             }
  96.         }
  97.         return staticInstance;
  98.     }



  99.     // INSTANCE

  100.     private String tipoDatabase;
  101.    
  102.     private DAOFactory daoFactory = null;
  103.     private ServiceManagerProperties daoFactoryServiceManagerPropertiesTransazioni = null;
  104.     private Logger daoFactoryLogger = null;
  105.    
  106.     boolean debug = false;
  107.    
  108.     private NotifierDump( boolean debug,
  109.             String tipoDatabase,
  110.             DAOFactory daoFactory, ServiceManagerProperties daoFactoryServiceManagerPropertiesTransazioni, Logger daoFactoryLogger){
  111.        
  112.         this.debug = debug;
  113.        
  114.         this.tipoDatabase = tipoDatabase;
  115.        
  116.         this.daoFactory = daoFactory;
  117.         this.daoFactoryServiceManagerPropertiesTransazioni = daoFactoryServiceManagerPropertiesTransazioni;
  118.         this.daoFactoryLogger = daoFactoryLogger;
  119.     }

  120.    
  121.     private int save(NotifierCallback notifierCallback,
  122.             String idTransazione,TipoMessaggio tipoMessaggio,Map<String, List<String>> headerTrasporto,
  123.             long idDumpConfigurazione,
  124.             String contentType,
  125.             InputStream is,File file,byte[]buffer,
  126.             IDSoggetto dominio) throws Exception{

  127.         PreparedStatement stmt = null;
  128.         DBTransazioniManager dbManager = null;
  129.         Resource r = null;
  130.         String idModulo = "NotifierDump.save"+tipoMessaggio.getValue();
  131.         try{
  132.             notifierCallback.debug("@save getConnection.....");
  133.            
  134.             dbManager = DBTransazioniManager.getInstance();
  135.             r = dbManager.getResource(dominio, idModulo, idTransazione);
  136.             if(r==null){
  137.                 throw new Exception("Risorsa al database non disponibile");
  138.             }
  139.             Connection con = (Connection) r.getResource();
  140.             if(con == null)
  141.                 throw new Exception("Connessione non disponibile");

  142.             boolean autoCommit = true;
  143.            
  144.             org.openspcoop2.core.transazioni.dao.jdbc.JDBCServiceManager jdbcServiceManager =
  145.                     (org.openspcoop2.core.transazioni.dao.jdbc.JDBCServiceManager) this.daoFactory.
  146.                         getServiceManager(org.openspcoop2.core.transazioni.utils.ProjectInfo.getInstance(), con, autoCommit,
  147.                             this.daoFactoryServiceManagerPropertiesTransazioni, this.daoFactoryLogger);
  148.             jdbcServiceManager.getJdbcProperties().setShowSql(this.debug);
  149.            
  150.             JDBCDumpMessaggioService dumpMessaggioService =  (JDBCDumpMessaggioService) jdbcServiceManager.getDumpMessaggioService();
  151.            
  152.             ISQLFieldConverter sqlFielConverter = dumpMessaggioService.getFieldConverter();
  153.            
  154.             // Devo usare per forza la prepared stameent per sfruttare la funzionalità di setStream.
  155.            
  156.             ISQLQueryObject insertSql = SQLObjectFactory.createSQLQueryObject(this.tipoDatabase);
  157.             insertSql.addInsertTable(sqlFielConverter.toTable(DumpMessaggio.model()));
  158.             insertSql.addInsertField(sqlFielConverter.toColumn(DumpMessaggio.model().ID_TRANSAZIONE, false), "?");
  159.             insertSql.addInsertField(sqlFielConverter.toColumn(DumpMessaggio.model().TIPO_MESSAGGIO, false), "?");
  160.             insertSql.addInsertField(sqlFielConverter.toColumn(DumpMessaggio.model().DUMP_TIMESTAMP, false), "?");
  161.             insertSql.addInsertField(sqlFielConverter.toColumn(DumpMessaggio.model().CONTENT_TYPE, false), "?");
  162.             insertSql.addInsertField(sqlFielConverter.toColumn(DumpMessaggio.model().POST_PROCESS_HEADER, false), "?");
  163.             if(file!=null){
  164.                 insertSql.addInsertField(sqlFielConverter.toColumn(DumpMessaggio.model().POST_PROCESS_FILENAME, false), "?");
  165.             }else{
  166.                 insertSql.addInsertField(sqlFielConverter.toColumn(DumpMessaggio.model().POST_PROCESS_CONTENT, false), "?");
  167.             }
  168.             insertSql.addInsertField("post_process_config_id", "?" );
  169.             insertSql.addInsertField(sqlFielConverter.toColumn(DumpMessaggio.model().POST_PROCESS_TIMESTAMP, false), "?");
  170.             insertSql.addInsertField(sqlFielConverter.toColumn(DumpMessaggio.model().POST_PROCESSED, false), "?");
  171.            
  172.             notifierCallback.debug("@save insertSql ["+insertSql.createSQLInsert()+"].....");
  173.                        
  174.             notifierCallback.debug("@save creo PreparedStatement.....");
  175.            
  176.             stmt = con.prepareStatement(insertSql.createSQLInsert());

  177.             int index = 1;
  178.             stmt.setString(index++, idTransazione);
  179.             stmt.setString(index++, tipoMessaggio.toString());
  180.             stmt.setTimestamp(index++, DateManager.getTimestamp());
  181.             stmt.setString(index++, contentType);
  182.             stmt.setString(index++, this.toString(headerTrasporto));
  183.             if(file!=null){
  184.                 notifierCallback.debug("set FileName.....");
  185.                 stmt.setString(index++, file.getName());
  186.             }else{
  187.                 if(is!=null){
  188.                     notifierCallback.debug("set BinaryStream.....");
  189.                     stmt.setBinaryStream(index++, is);
  190.                 }
  191.                 else{
  192.                     notifierCallback.debug("set JDBCAdapter.....");
  193.                     JDBCAdapterFactory.createJDBCAdapter(this.tipoDatabase).setBinaryData(stmt, index++, buffer);
  194.                 }
  195.             }
  196.             stmt.setLong(index++, idDumpConfigurazione);
  197.             stmt.setTimestamp(index++, DateManager.getTimestamp());
  198.             stmt.setInt(index++, 0); // dovra' essere processato in posto process.

  199.             notifierCallback.debug("set executeUpdate.....");
  200.            
  201.             int row = stmt.executeUpdate();
  202.            
  203.             notifierCallback.debug("return row ["+row+"].....");
  204.             return row;

  205.         }finally{
  206.             try{
  207.                 if(stmt!=null)
  208.                     stmt.close();
  209.             }catch(Exception eClose){}
  210.             try{
  211.                 if(r!=null)
  212.                     dbManager.releaseResource(dominio, idModulo, r);
  213.             }catch(Exception eClose){
  214.                 // close
  215.             }
  216.         }
  217.     }
  218.    
  219.     private String toString(Map<String, List<String>> headerTrasporto){
  220.         StringBuilder bf = new StringBuilder();
  221.         if(headerTrasporto!=null && headerTrasporto.size()>0){
  222.             Iterator<String> keys = headerTrasporto.keySet().iterator();
  223.             while (keys.hasNext()) {
  224.                 String key = keys.next();
  225.                 List<String> values = headerTrasporto.get(key);
  226.                 if(values!=null && !values.isEmpty()) {
  227.                     for (String value : values) {
  228.                         if(bf.length()>0){
  229.                             bf.append("\n");
  230.                         }
  231.                         bf.append(key).append("=").append(value);      
  232.                     }
  233.                 }
  234.             }
  235.         }
  236.         if(bf.length()>0){
  237.             return bf.toString();
  238.         }
  239.         else{
  240.             return null;
  241.         }
  242.     }
  243.    
  244.     public int saveOnDatabase(NotifierCallback notifierCallback,
  245.             String idTransazione,TipoMessaggio tipoMessaggio, Map<String, List<String>> headerTrasporto,
  246.             long idDumpConfigurazione,
  247.             String contentType,InputStream is,
  248.             IDSoggetto dominio) throws Exception{
  249.         return this.save(notifierCallback,idTransazione, tipoMessaggio, headerTrasporto, idDumpConfigurazione, contentType, is, null, null, dominio);
  250.     }
  251.    
  252.     public int saveOnFileSystem(NotifierCallback notifierCallback,
  253.             String idTransazione,TipoMessaggio tipoMessaggio, Map<String, List<String>> headerTrasporto,
  254.             long idDumpConfigurazione,
  255.             String contentType,File file,
  256.             IDSoggetto dominio) throws Exception{
  257.         return this.save(notifierCallback,idTransazione, tipoMessaggio, headerTrasporto, idDumpConfigurazione, contentType, null, file, null, dominio);
  258.     }
  259.    
  260.     public int saveBuffer(NotifierCallback notifierCallback,
  261.             String idTransazione,TipoMessaggio tipoMessaggio, Map<String, List<String>> headerTrasporto,
  262.             long idDumpConfigurazione,
  263.             String contentType,byte[] content,
  264.             IDSoggetto dominio) throws Exception{
  265.         return this.save(notifierCallback,idTransazione, tipoMessaggio, headerTrasporto, idDumpConfigurazione, contentType, null, null, content, dominio);
  266.     }
  267.    
  268.    
  269.     public int update(NotifierCallback notifierCallback,
  270.             String idTransazione,TipoMessaggio tipoMessaggio,Map<String, List<String>> headerTrasporto,
  271.             IDSoggetto dominio) throws Exception{

  272.         PreparedStatement stmt = null;
  273.         DBTransazioniManager dbManager = null;
  274.         Resource r = null;
  275.         String idModulo = "NotifierDump.update"+tipoMessaggio.getValue();
  276.         try{
  277.             notifierCallback.debug("@save getConnection.....");
  278.            
  279.             dbManager = DBTransazioniManager.getInstance();
  280.             r = dbManager.getResource(dominio, idModulo, idTransazione);
  281.             if(r==null){
  282.                 throw new Exception("Risorsa al database non disponibile");
  283.             }
  284.             Connection con = (Connection) r.getResource();
  285.             if(con == null)
  286.                 throw new Exception("Connessione non disponibile");

  287.             boolean autoCommit = true;
  288.            
  289.             org.openspcoop2.core.transazioni.dao.jdbc.JDBCServiceManager jdbcServiceManager =
  290.                     (org.openspcoop2.core.transazioni.dao.jdbc.JDBCServiceManager)
  291.                         this.daoFactory.getServiceManager(org.openspcoop2.core.transazioni.utils.ProjectInfo.getInstance(), con, autoCommit,
  292.                             this.daoFactoryServiceManagerPropertiesTransazioni, this.daoFactoryLogger);
  293.             jdbcServiceManager.getJdbcProperties().setShowSql(this.debug);
  294.            
  295.             JDBCDumpMessaggioService dumpMessaggioService =  (JDBCDumpMessaggioService) jdbcServiceManager.getDumpMessaggioService();
  296.            
  297.             ISQLFieldConverter sqlFielConverter = dumpMessaggioService.getFieldConverter();
  298.            
  299.             // Devo usare per forza la prepared stameent per sfruttare la funzionalità di ritornare le righe modificate
  300.            
  301.             ISQLQueryObject updateSql = SQLObjectFactory.createSQLQueryObject(this.tipoDatabase);
  302.             updateSql.addUpdateTable(sqlFielConverter.toTable(DumpMessaggio.model()));
  303.             updateSql.addUpdateField(sqlFielConverter.toColumn(DumpMessaggio.model().POST_PROCESS_HEADER, false), "?");
  304.             updateSql.setANDLogicOperator(true);
  305.             updateSql.addWhereCondition(sqlFielConverter.toColumn(DumpMessaggio.model().ID_TRANSAZIONE, false)+"=?");
  306.             updateSql.addWhereCondition(sqlFielConverter.toColumn(DumpMessaggio.model().TIPO_MESSAGGIO, false)+"=?");

  307.             notifierCallback.debug("@save updateSql ["+updateSql.createSQLUpdate()+"].....");
  308.                        
  309.             notifierCallback.debug("@save creo PreparedStatement.....");
  310.            
  311.             stmt = con.prepareStatement(updateSql.createSQLUpdate());

  312.             stmt.setString(1, this.toString(headerTrasporto));
  313.             stmt.setString(2, idTransazione);
  314.             stmt.setString(3, tipoMessaggio.toString());

  315.             notifierCallback.debug("set executeUpdate.....");
  316.            
  317.             int row = stmt.executeUpdate();
  318.            
  319.             notifierCallback.debug("return row ["+row+"].....");
  320.             return row;

  321.         }finally{
  322.             try{
  323.                 if(stmt!=null)
  324.                     stmt.close();
  325.             }catch(Exception eClose){}
  326.             try{
  327.                 if(r!=null)
  328.                     dbManager.releaseResource(dominio, idModulo, r);
  329.             }catch(Exception eClose){
  330.                 // close
  331.             }
  332.         }
  333.     }
  334.    
  335. }