StateMessage.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.sdk.state;

  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.util.Map;

  24. import org.slf4j.Logger;
  25. import org.openspcoop2.utils.UtilsException;
  26. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  27. /**
  28.  * Oggetto che rappresenta lo stato di una busta
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author Fabio Tronci (tronci@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class StateMessage implements IState {

  36.     /** Connessione database */
  37.     private Connection connectionDB;

  38.     /** Tabella Hash contenente le PreparedStatement da eseguire */
  39.     private StateMap tablePstmt;
  40.    
  41.     /** Logger */
  42.     private Logger log = null;
  43.    
  44.     public StateMessage(){
  45.        
  46.     }
  47.    
  48.    
  49.     public StateMessage(Connection con, Logger log) {
  50.         this.connectionDB = con;
  51.         this.log = log;
  52.         this.tablePstmt = new StateMap(log);
  53.     }
  54.    

  55.     public StateMessage(Connection con, Logger log, StateMap preparedStatement) {
  56.         this.connectionDB = con;
  57.         this.log = log;
  58.         this.tablePstmt = preparedStatement;
  59.     }


  60.     public Logger getLog() {
  61.         return this.log;
  62.     }


  63.     public void setLog(Logger log) {
  64.         this.log = log;
  65.     }


  66.     public Connection getConnectionDB() {
  67.         return this.connectionDB;
  68.     }

  69.     public void setConnectionDB(Connection connectionDB) {
  70.         this.connectionDB = connectionDB;
  71.     }

  72.     /**
  73.      * Imposta la connessione.
  74.      *
  75.      * @param con Connessione al Database
  76.      *
  77.      */
  78.     public void updateConnection(Connection con){
  79.         this.connectionDB = con;
  80.     }
  81.    
  82.     /**
  83.      * Ritorna una tabella Hash contenente le preparedStatement da eseguire.
  84.      *
  85.      * @return Tabella Hash contenente le PreparedStatement da eseguire.
  86.      *
  87.      */
  88.     public StateMap getPreparedStatement(){
  89.         return this.tablePstmt;
  90.     }
  91.    
  92.     public void setPreparedStatement(Map<String,PreparedStatement> pstm){
  93.         this.tablePstmt.setPreparedStatement(pstm);
  94.     }
  95.     public void setPreparedStatement(StateMap pstm){
  96.         this.tablePstmt = pstm;
  97.     }
  98.    
  99.    
  100.    
  101.     /**
  102.      * Chiude tutte le PreparedStatement.
  103.      *
  104.      *
  105.      */
  106.     public void closePreparedStatement(){
  107.         JDBCUtilities.closePreparedStatement(this.tablePstmt.getPreparedStatement(),this.log);
  108.     }


  109.     /**
  110.      * Esegue e Chiude tutte le PreparedStatement.
  111.      *
  112.      *
  113.      */
  114.     public void executePreparedStatement() throws UtilsException{
  115.         JDBCUtilities.executePreparedStatement(this.tablePstmt.getPreparedStatement());
  116.     }
  117.    
  118.     /**
  119.      * Aggiunge le pstmt alla tabella delle pstmt.
  120.      *
  121.      * @param pstmt Tabella Hash contenente le PreparedStatement da eseguire.
  122.      *
  123.      */
  124.     public void addPreparedStatement(Map<String,PreparedStatement> pstmt)throws UtilsException{
  125.         JDBCUtilities.addPreparedStatement(pstmt,this.tablePstmt.getPreparedStatement(),this.log);
  126.     }
  127.    
  128.    
  129.     public static Connection getConnection(StateMessage state) {
  130.         if(state!=null){
  131.             boolean validConnection = false;
  132.             if(state.getConnectionDB()!=null) {
  133.                 try{
  134.                     validConnection = !state.getConnectionDB().isClosed();
  135.                 }catch(Exception e){}
  136.             }
  137.             if(validConnection)
  138.                 return state.getConnectionDB();
  139.         }
  140.         return null;
  141.     }
  142. }