History.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.engine.driver;


  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;

  24. import org.slf4j.Logger;
  25. import org.openspcoop2.protocol.engine.Configurazione;
  26. import org.openspcoop2.protocol.engine.constants.Costanti;
  27. import org.openspcoop2.protocol.engine.driver.repository.IGestoreRepository;
  28. import org.openspcoop2.protocol.sdk.ProtocolException;
  29. import org.openspcoop2.protocol.sdk.state.IState;
  30. import org.openspcoop2.protocol.sdk.state.StateMessage;
  31. import org.openspcoop2.protocol.sdk.state.StatefulMessage;
  32. import org.openspcoop2.utils.LoggerWrapperFactory;


  33. /**
  34.  * Sono inclusi i metodi per la gestione dell'History delle buste inviate/ricevuta.
  35.  *
  36.  *
  37.  * @author Poli Andrea (apoli@link.it)
  38.  * @author Tronci Fabio (tronci@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */

  42. public class History  {

  43.     /** Logger utilizzato per debug. */
  44.     private Logger log = null;

  45.     /** Se IState e' un'istanza di StatefulMessage possiede una Connessione SQL in autoCommit mode su cui effettuare query
  46.      *  Altrimenti, e' un'istanza di StatelessMessage e nn necessita di connessioni
  47.      * */
  48.     private IState state;


  49.     /** GestoreRepository */
  50.     private IGestoreRepository gestoreRepositoryBuste;


  51.     /* ********  C O S T R U T T O R E  ******** */

  52.     /**
  53.      * Costruttore.
  54.      *
  55.      * @param state Oggetto che rappresenta lo stato di una busta
  56.      *
  57.      */
  58.     public History(IState state){
  59.         this(state,Configurazione.getLibraryLog());
  60.     }
  61.     /**
  62.      * Costruttore.
  63.      *
  64.      * @param state Oggetto che rappresenta lo stato di una busta
  65.      *
  66.      */
  67.     public History(IState state, Logger alog){
  68.         this.state = state;
  69.         this.gestoreRepositoryBuste = Configurazione.getGestoreRepositoryBuste();
  70.         if(alog!=null){
  71.             this.log = alog;
  72.         }else{
  73.             this.log = LoggerWrapperFactory.getLogger(History.class.getName());
  74.         }
  75.     }

  76.     public void updateState(IState state){
  77.         this.state = state;
  78.     }




  79.     /* ********  B U S T E     I N V I A T E  ******** */
  80.     /**
  81.      * Metodo che si occupa di salvare una busta, precedentemente registrata,
  82.      * nell'history delle buste inviate.
  83.      *
  84.      * @param id identificativo della busta da salvare.
  85.      *
  86.      */
  87.     public void registraBustaInviata(String id) throws ProtocolException{
  88.         registraBusta(id,Costanti.OUTBOX);
  89.     }
  90.     /**
  91.      * Metodo che si occupa di eliminare una busta, precedentemente registrata,
  92.      * nell'history delle buste inviate.
  93.      *
  94.      * @param id identificativo della busta da eliminare.
  95.      *
  96.      */
  97.     public void eliminaBustaInviata(String id) throws ProtocolException{
  98.         eliminaBusta(id,Costanti.OUTBOX);
  99.     }
  100.     public void eliminaBustaInviataPerRiscontri(String id) throws ProtocolException{
  101.         eliminaBustaPerRiscontri(id,Costanti.OUTBOX);
  102.     }

















  103.     /* ********  B U S T E     R I C E V U T E  ******** */
  104.     /**
  105.      * Metodo che si occupa di salvare una busta, precedentemente registrata,
  106.      * nell'history delle buste ricevute.
  107.      *
  108.      * @param id identificativo della busta da salvare.
  109.      *
  110.      */
  111.     public void registraBustaRicevuta(String id) throws ProtocolException{
  112.         registraBusta(id,Costanti.INBOX);
  113.     }
  114.     /**
  115.      * Metodo che si occupa di eliminare una busta, precedentemente registrata,
  116.      * nell'history delle buste ricevute.
  117.      *
  118.      * @param id identificativo della busta da eliminare.
  119.      *
  120.      */
  121.     public void eliminaBustaRicevuta(String id) throws ProtocolException{
  122.         eliminaBusta(id,Costanti.INBOX);
  123.     }
  124.     public void eliminaBustaRicevutaPerRiscontri(String id) throws ProtocolException{
  125.         eliminaBustaPerRiscontri(id,Costanti.INBOX);
  126.     }

  127.     /**
  128.      * Ritorna true, se una busta con il medesimo identificativo e' gia' stata precedentemente registrata.
  129.      *
  130.      * @param id identificativo della busta.
  131.      * @return true se una busta con il medesimo identificativo e' gia' stata precedentemente registrata. false altrimenti
  132.      *
  133.      */
  134.     public boolean bustaRicevutaPrecedentemente(String id) throws ProtocolException{
  135.         StateMessage stateMSG = (StateMessage)this.state;
  136.         Connection connectionDB = stateMSG.getConnectionDB();

  137.         PreparedStatement pstmt = null;
  138.         ResultSet rs = null;
  139.         try{    

  140.             StringBuilder query = new StringBuilder();
  141.             query.append("select ID_MESSAGGIO from ");
  142.             query.append(Costanti.REPOSITORY);
  143.             query.append(" WHERE ID_MESSAGGIO = ? AND TIPO=? AND ");
  144.            
  145.             // controllo anche accesso_history=1 perche' puo' darsi che sia in
  146.             // corso di gestione...questa non la devo considerare gia ricevuta!
  147.             // per attuare questo controllo guardo che la busta non possiede accesso_history=1 (se e' gia' stata ricevuta possiede accesso_history=1)
  148.             query.append(this.gestoreRepositoryBuste.createSQLCondition_History(true));
  149.            
  150.             pstmt = connectionDB.prepareStatement(query.toString());
  151.             pstmt.setString(1,id);
  152.             pstmt.setString(2,Costanti.INBOX);

  153.             rs = pstmt.executeQuery();
  154.             if(rs != null){
  155.                 if(rs.next() == true) {
  156.                     rs.close();
  157.                     pstmt.close();
  158.                     return true;
  159.                 }
  160.                 else {
  161.                     rs.close();
  162.                     pstmt.close();
  163.                     return false;
  164.                 }
  165.             }

  166.             pstmt.close();

  167.             return false;

  168.         } catch(Exception e) {
  169.             String errorMsg = "HISTORY_BUSTE, Errore durante il check bustaRicevutaPrecedentemente "+id+": "+e.getMessage();        
  170.             this.log.error(errorMsg,e);
  171.             try{
  172.                 if(rs!=null)
  173.                     rs.close();
  174.             } catch(Exception er) {
  175.                 // Eccezione SQL.
  176.             }
  177.             try{
  178.                 if(pstmt!=null)
  179.                     pstmt.close();
  180.             } catch(Exception er) {
  181.                 // Eccezione SQL.
  182.             }
  183.             throw new ProtocolException(errorMsg,e);
  184.         }
  185.     }  


  186.     // ************ GESTIONE HISTORY ************

  187.     /**
  188.      * Metodo che si occupa di salvare una busta, precedentemente registrata,
  189.      * nell'history delle buste.
  190.      *
  191.      * @param id identificativo della busta da salvare.
  192.      * @param tipoBusta Indicazione sul tipo di busta inviata/ricevuta
  193.      *
  194.      */
  195.     public void registraBusta(String id,String tipoBusta) throws ProtocolException{

  196.         StateMessage stateMSG = (StateMessage)this.state;
  197.         Connection connectionDB = stateMSG.getConnectionDB();

  198.         PreparedStatement pstmtUpdate = null;
  199.         try{    

  200.             StringBuilder queryUpdate = new StringBuilder();
  201.             queryUpdate.append("UPDATE ");
  202.             queryUpdate.append(Costanti.REPOSITORY);
  203.             queryUpdate.append(" SET ");
  204.             queryUpdate.append(this.gestoreRepositoryBuste.createSQLSet_History(true));
  205.             queryUpdate.append(" WHERE  ID_MESSAGGIO = ? AND TIPO=?");
  206.             pstmtUpdate = connectionDB.prepareStatement(queryUpdate.toString());
  207.             pstmtUpdate.setString(1,id);
  208.             pstmtUpdate.setString(2,tipoBusta);

  209.             // Add PreparedStatement
  210.             stateMSG.getPreparedStatement().put("UPDATE saveBustaForHistory"+tipoBusta+"_"+id,pstmtUpdate);

  211.         } catch(Exception e) {
  212.             String errorMsg = "HISTORY_BUSTE, Errore di registrazione "+tipoBusta+"/"+id+": "+e.getMessage();      
  213.             this.log.error(errorMsg,e);
  214.             try{
  215.                 if( pstmtUpdate != null )
  216.                     pstmtUpdate.close();
  217.             } catch(Exception er) {
  218.                 // Eccezione SQL.
  219.             }
  220.             throw new ProtocolException(errorMsg,e);
  221.         }
  222.     }
  223.    
  224.     /**
  225.      * Metodo che si occupa di eliminare una busta, precedentemente registrata,
  226.      * nell'history delle buste.
  227.      *
  228.      * @param id identificativo della busta da salvare.
  229.      * @param tipoBusta Indicazione sul tipo di busta inviata/ricevuta
  230.      *
  231.      */
  232.     public void eliminaBusta(String id,String tipoBusta) throws ProtocolException{
  233.         eliminaBusta(id,tipoBusta,false);
  234.     }
  235.     public void eliminaBusta(String id,String tipoBusta,boolean forzaEliminazioneDb) throws ProtocolException{

  236.         if(this.state instanceof StatefulMessage || forzaEliminazioneDb) {
  237.             StateMessage state = (StateMessage)this.state;
  238.             Connection connectionDB = state.getConnectionDB();

  239.             PreparedStatement pstmtUpdate = null;
  240.             try{    

  241.                 StringBuilder queryUpdate = new StringBuilder();
  242.                 queryUpdate.append("UPDATE ");
  243.                 queryUpdate.append(Costanti.REPOSITORY);
  244.                 queryUpdate.append(" SET ");
  245.                 queryUpdate.append(this.gestoreRepositoryBuste.createSQLSet_History(false));
  246.                 queryUpdate.append(" WHERE  ID_MESSAGGIO = ? AND TIPO=?");
  247.                 pstmtUpdate = connectionDB.prepareStatement(queryUpdate.toString());
  248.                 pstmtUpdate.setString(1,id);
  249.                 pstmtUpdate.setString(2,tipoBusta);

  250.                 // Add PreparedStatement
  251.                 state.getPreparedStatement().put("UPDATE eliminaBustaForHistory"+tipoBusta+"_"+id,pstmtUpdate);

  252.             } catch(Exception e) {
  253.                 String errorMsg = "HISTORY_BUSTE, Errore di cancellazione "+tipoBusta+"/"+id+": "+e.getMessage();      
  254.                 this.log.error(errorMsg,e);
  255.                 try{
  256.                     if( pstmtUpdate != null )
  257.                         pstmtUpdate.close();
  258.                 } catch(Exception er) {
  259.                     // Eccezione SQL.
  260.                 }
  261.                 throw new ProtocolException(errorMsg,e);
  262.             }
  263.         }
  264.         else {
  265.             throw new ProtocolException ("Metodo non invocabile in modalita' stateless");
  266.         }
  267.     }
  268.    
  269.     /**
  270.      * Metodo che si occupa di eliminare una busta, precedentemente registrata,
  271.      * nell'history delle buste.
  272.      *
  273.      * @param id identificativo della busta da salvare.
  274.      * @param tipoBusta Indicazione sul tipo di busta inviata/ricevuta
  275.      *
  276.      */
  277.     public void eliminaBustaPerRiscontri(String id,String tipoBusta) throws ProtocolException{

  278.         StateMessage stateMSG = (StateMessage)this.state;
  279.         Connection connectionDB = stateMSG.getConnectionDB();

  280.         PreparedStatement pstmtUpdate = null;
  281.         try{    

  282.             StringBuilder queryUpdate = new StringBuilder();
  283.             queryUpdate.append("UPDATE ");
  284.             queryUpdate.append(Costanti.REPOSITORY);
  285.             queryUpdate.append(" SET ");
  286.             queryUpdate.append(this.gestoreRepositoryBuste.createSQLSet_History(false));
  287.             queryUpdate.append(" WHERE  ID_MESSAGGIO = ? AND TIPO=?");
  288.             pstmtUpdate = connectionDB.prepareStatement(queryUpdate.toString());
  289.             pstmtUpdate.setString(1,id);
  290.             pstmtUpdate.setString(2,tipoBusta);

  291.             // Add PreparedStatement
  292.             stateMSG.getPreparedStatement().put("UPDATE eliminaBustaForHistory"+tipoBusta+"_"+id,pstmtUpdate);

  293.         } catch(Exception e) {
  294.             String errorMsg = "HISTORY_BUSTE, Errore di cancellazione "+tipoBusta+"/"+id+": "+e.getMessage();      
  295.             this.log.error(errorMsg,e);
  296.             try{
  297.                 if( pstmtUpdate != null )
  298.                     pstmtUpdate.close();
  299.             } catch(Exception er) {
  300.                 // Eccezione SQL.
  301.             }
  302.             throw new ProtocolException(errorMsg,e);
  303.         }
  304.     }
  305. }