RollbackRepositoryBuste.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 org.slf4j.Logger;
  24. import org.openspcoop2.protocol.engine.Configurazione;
  25. import org.openspcoop2.protocol.engine.constants.Costanti;
  26. import org.openspcoop2.protocol.engine.driver.repository.IGestoreRepository;
  27. import org.openspcoop2.protocol.sdk.ProtocolException;
  28. import org.openspcoop2.protocol.sdk.state.IState;
  29. import org.openspcoop2.protocol.sdk.state.StateMessage;
  30. import org.openspcoop2.utils.LoggerWrapperFactory;


  31. /**
  32.  * Classe utilizzata per effettuare rollback applicativo
  33.  * di informazioni salvate precedentemente nei DB.
  34.  *
  35.  *
  36.  * @author Poli Andrea (apoli@link.it)
  37.  * @author Tronci Fabio (tronci@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */

  41. public class RollbackRepositoryBuste implements java.io.Serializable {

  42.     /**
  43.      * serialVersionUID
  44.      */
  45.     private static final long serialVersionUID = 1L;

  46.     /** Logger utilizzato per debug. */
  47.     private Logger log = null;

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

  51.     /** Identificativo */
  52.     private String idBusta;

  53.     /** GestoreRepository */
  54.     private IGestoreRepository gestoreRepositoryBuste;

  55.     /** Indicazione se stiamo gestendo il onewat in modalita 11 */
  56.     //private boolean oneWay11 = false;


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

  58.     /**
  59.      * Costruttore.
  60.      *
  61.      * @param id ID su cui effettuare il rollback delle precedenti informazioni salvate.
  62.      * @param state Oggetto che rappresenta lo stato di una busta
  63.      *
  64.      */
  65.     public RollbackRepositoryBuste(String id, IState state,boolean oneWay11){
  66.         this(id,state,Configurazione.getLibraryLog(),oneWay11);
  67.     }
  68.     /**
  69.      * Costruttore.
  70.      *
  71.      * @param id ID su cui effettuare il rollback delle precedenti informazioni salvate.
  72.      * @param state Oggetto che rappresenta lo stato di una busta
  73.      *
  74.      */
  75.     public RollbackRepositoryBuste(String id,IState state,Logger aLog,boolean oneWay11){
  76.         this.idBusta = id;
  77.         this.state = state;
  78.         this.gestoreRepositoryBuste = Configurazione.getGestoreRepositoryBuste();
  79.         if(aLog!=null)
  80.             this.log = aLog;
  81.         else
  82.             this.log = LoggerWrapperFactory.getLogger(RollbackRepositoryBuste.class.getName());
  83.         //this.oneWay11 = oneWay11;
  84.     }




  85.     /**
  86.      * Metodo che si occupa di eliminare tutti i dati creati durante una gestione di una richiesta
  87.      * in una fase di Porta di Dominio Delegata (Invocazione di una porta di dominio delegata).
  88.      *
  89.      *
  90.      */
  91.     public void rollbackBustaIntoOutBox() throws ProtocolException{
  92.         this.rollback(Costanti.OUTBOX,true);
  93.     }
  94.     /**
  95.      * Metodo che si occupa di eliminare tutti i dati creati durante una gestione di una richiesta
  96.      * in una fase di Porta di Dominio Delegata (Invocazione di una porta di dominio delegata).
  97.      *
  98.      * @param rollbackAccessoHistory rollback dell'accesso effettuato dall'History
  99.      *
  100.      */
  101.     public void rollbackBustaIntoOutBox(boolean rollbackAccessoHistory) throws ProtocolException{  
  102.         this.rollback(Costanti.OUTBOX,rollbackAccessoHistory);
  103.     }

  104.     /**
  105.      * Metodo che si occupa di eliminare tutti i dati creati durante una gestione di una richiesta
  106.      * in una fase di Porta di Dominio Applicativa (Invocazione di una porta di dominio applicativa).
  107.      *
  108.      *
  109.      */
  110.     public void rollbackBustaIntoInBox() throws ProtocolException{
  111.         this.rollback(Costanti.INBOX,true);
  112.     }
  113.     /**
  114.      * Metodo che si occupa di eliminare tutti i dati creati durante una gestione di una richiesta
  115.      * in una fase di Porta di Dominio Applicativa (Invocazione di una porta di dominio applicativa).
  116.      *
  117.      * @param rollbackAccessoHistory rollback dell'accesso effettuato dall'History
  118.      *
  119.      */
  120.     public void rollbackBustaIntoInBox(boolean rollbackAccessoHistory) throws ProtocolException{
  121.         this.rollback(Costanti.INBOX,rollbackAccessoHistory);
  122.     }









  123.     /* ********  UTILITY DI ROLLBACK APPLICATIVO  ******** */
  124.     /**
  125.      * Metodo che si occupa di eliminare i dati di una busta presente nel RepositoryBuste.
  126.      *
  127.      * @param tipoBusta tipo di busta INBOX/OUTBOX
  128.      * @param rollbackAccessoHistory rollback dell'accesso effettuato dall'History
  129.      *
  130.      */
  131.     public void rollback(String tipoBusta,boolean rollbackAccessoHistory) throws ProtocolException{

  132.         StateMessage stateMSG = (StateMessage)this.state;
  133.         Connection connectionDB = stateMSG.getConnectionDB();

  134.         if(connectionDB!=null){
  135.             PreparedStatement pstmtUpdateHistory = null;
  136.             PreparedStatement pstmtUpdateProfilo = null;
  137.             PreparedStatement pstmtUpdatePdd = null;
  138.             try{    
  139.    
  140.                 // Le prepared Stamenent devono essere suddivise, per Oracle, ad es, non accetta SET di stessi field.
  141.    
  142.                 // rollback AccessoHistory
  143.                 if(rollbackAccessoHistory){
  144.                     StringBuilder queryUpdateHistory = new StringBuilder();
  145.                     queryUpdateHistory.append("UPDATE ");
  146.                     queryUpdateHistory.append(Costanti.REPOSITORY);
  147.                     queryUpdateHistory.append(" SET ");
  148.                     queryUpdateHistory.append(this.gestoreRepositoryBuste.createSQLSet_History(false));
  149.                     queryUpdateHistory.append(" WHERE ID_MESSAGGIO = ? AND TIPO=?");
  150.                     pstmtUpdateHistory =  connectionDB.prepareStatement(queryUpdateHistory.toString());
  151.                     pstmtUpdateHistory.setString(1,this.idBusta);
  152.                     pstmtUpdateHistory.setString(2,tipoBusta);
  153.                     // Add PreparedStatement
  154.                     stateMSG.getPreparedStatement().put("RollbackGeneraleHISTORY_"+tipoBusta+"_"+this.idBusta,pstmtUpdateHistory);
  155.                 }
  156.    
  157.                 // rollback profilo
  158.                 StringBuilder queryUpdateProfilo= new StringBuilder();
  159.                 queryUpdateProfilo.append("UPDATE ");
  160.                 queryUpdateProfilo.append(Costanti.REPOSITORY);
  161.                 queryUpdateProfilo.append(" SET ");
  162.                 queryUpdateProfilo.append(this.gestoreRepositoryBuste.createSQLSet_ProfiloCollaborazione(false));
  163.                 queryUpdateProfilo.append(" WHERE ID_MESSAGGIO = ? AND TIPO=?");
  164.                 pstmtUpdateProfilo =  connectionDB.prepareStatement(queryUpdateProfilo.toString());
  165.                 pstmtUpdateProfilo.setString(1,this.idBusta);
  166.                 pstmtUpdateProfilo.setString(2,tipoBusta);
  167.                 // Add PreparedStatement
  168.                 stateMSG.getPreparedStatement().put("RollbackGeneralePROFILO_"+tipoBusta+"_"+this.idBusta,pstmtUpdateProfilo);
  169.    
  170.                 // rollback pdd
  171.                 StringBuilder queryUpdatePdd = new StringBuilder();
  172.                 queryUpdatePdd.append("UPDATE ");
  173.                 queryUpdatePdd.append(Costanti.REPOSITORY);
  174.                 queryUpdatePdd.append(" SET ");
  175.                 queryUpdatePdd.append(this.gestoreRepositoryBuste.createSQLSet_PdD(false));
  176.                 queryUpdatePdd.append(" WHERE ID_MESSAGGIO = ? AND TIPO=?");
  177.                 pstmtUpdatePdd =  connectionDB.prepareStatement(queryUpdatePdd.toString());
  178.                 pstmtUpdatePdd.setString(1,this.idBusta);
  179.                 pstmtUpdatePdd.setString(2,tipoBusta);
  180.                 // Add PreparedStatement
  181.                 stateMSG.getPreparedStatement().put("RollbackGeneralePDD_"+tipoBusta+"_"+this.idBusta,pstmtUpdatePdd);
  182.    
  183.             } catch(Exception e) {
  184.                 String errorMsg = "ROLLBACK_BUSTE, Errore "+tipoBusta+"/"+this.idBusta+": "+e.getMessage();
  185.                 this.log.info(errorMsg,e);
  186.                 try{
  187.                     if( pstmtUpdateHistory != null )
  188.                         pstmtUpdateHistory.close();
  189.                 } catch(Exception er) {
  190.                     // close
  191.                 }
  192.                 try{
  193.                     if( pstmtUpdateProfilo != null )
  194.                         pstmtUpdateProfilo.close();
  195.                 } catch(Exception er) {
  196.                     // close
  197.                 }
  198.                 try{
  199.                     if( pstmtUpdatePdd != null )
  200.                         pstmtUpdatePdd.close();
  201.                 } catch(Exception er) {
  202.                     // close
  203.                 }
  204.                 throw new ProtocolException(errorMsg,e);
  205.             }
  206.         }else{
  207.             this.log.debug("Rollback("+tipoBusta+"/"+this.idBusta+") non effettuato, connessione is null");
  208.         }

  209.     }

  210.     /* ********  UTILITY DI SET ACCESSI  ******** */
  211.     /**
  212.      * Metodo che si occupa di eliminare i dati di una busta presente nel RepositoryBuste.
  213.      *
  214.      * @param history accesso effettuato dall'History
  215.      * @param profilo accesso effettuato dall'History
  216.      * @param pdd accesso effettuato dall'History
  217.      *
  218.      */
  219.     public void clearAccessiIntoInBox(boolean history,boolean profilo,boolean pdd) throws ProtocolException{
  220.         clearAccessi(Costanti.INBOX,history,profilo,pdd);
  221.     }
  222.     /**
  223.      * Metodo che si occupa di eliminare i dati di una busta presente nel RepositoryBuste.
  224.      *
  225.      * @param history accesso effettuato dall'History
  226.      * @param profilo accesso effettuato dall'History
  227.      * @param pdd accesso effettuato dall'History
  228.      *
  229.      */
  230.     public void clearAccessiIntoOutBox(boolean history,boolean profilo,boolean pdd) throws ProtocolException{
  231.         clearAccessi(Costanti.OUTBOX,history,profilo,pdd);
  232.     }
  233.     /**
  234.      * Metodo che si occupa di eliminare i dati di una busta presente nel RepositoryBuste.
  235.      *
  236.      * @param tipoBusta tipo di busta INBOX/OUTBOX
  237.      * @param history accesso effettuato dall'History
  238.      * @param profilo accesso effettuato dall'History
  239.      * @param pdd accesso effettuato dall'History
  240.      *
  241.      */
  242.     private void clearAccessi(String tipoBusta,boolean history,boolean profilo,boolean pdd) throws ProtocolException{
  243.         StateMessage stateMSG = (StateMessage)this.state;
  244.         Connection connectionDB = stateMSG.getConnectionDB();

  245.         if(connectionDB!=null){
  246.             PreparedStatement pstmtUpdateHistory = null;
  247.             PreparedStatement pstmtUpdateProfilo = null;
  248.             PreparedStatement pstmtUpdatePdd = null;
  249.             try{    
  250.    
  251.                 if(history==false && profilo==false && pdd==false)
  252.                     return;
  253.    
  254.                 // rollback AccessoHistory
  255.                 if(history){
  256.                     StringBuilder queryUpdateHistory = new StringBuilder();
  257.                     queryUpdateHistory.append("UPDATE ");
  258.                     queryUpdateHistory.append(Costanti.REPOSITORY);
  259.                     queryUpdateHistory.append(" SET ");
  260.                     queryUpdateHistory.append(this.gestoreRepositoryBuste.createSQLSet_History(false));
  261.                     queryUpdateHistory.append(" WHERE ID_MESSAGGIO = ? AND TIPO=?");
  262.                     pstmtUpdateHistory =  connectionDB.prepareStatement(queryUpdateHistory.toString());
  263.                     pstmtUpdateHistory.setString(1,this.idBusta);
  264.                     pstmtUpdateHistory.setString(2,tipoBusta);
  265.                     // Add PreparedStatement
  266.                     stateMSG.getPreparedStatement().put("ClearAccessiHISTORY_"+tipoBusta+"_"+this.idBusta,pstmtUpdateHistory);
  267.                 }
  268.    
  269.                 // rollback profilo
  270.                 if(profilo){
  271.                     StringBuilder queryUpdateProfilo= new StringBuilder();
  272.                     queryUpdateProfilo.append("UPDATE ");
  273.                     queryUpdateProfilo.append(Costanti.REPOSITORY);
  274.                     queryUpdateProfilo.append(" SET ");
  275.                     queryUpdateProfilo.append(this.gestoreRepositoryBuste.createSQLSet_ProfiloCollaborazione(false));
  276.                     queryUpdateProfilo.append(" WHERE ID_MESSAGGIO = ? AND TIPO=?");
  277.                     pstmtUpdateProfilo =  connectionDB.prepareStatement(queryUpdateProfilo.toString());
  278.                     pstmtUpdateProfilo.setString(1,this.idBusta);
  279.                     pstmtUpdateProfilo.setString(2,tipoBusta);
  280.                     // Add PreparedStatement
  281.                     stateMSG.getPreparedStatement().put("ClearAccessiPROFILO_"+tipoBusta+"_"+this.idBusta,pstmtUpdateProfilo);
  282.                 }
  283.    
  284.                 // rollback pdd
  285.                 if(pdd){
  286.                     StringBuilder queryUpdatePdd = new StringBuilder();
  287.                     queryUpdatePdd.append("UPDATE ");
  288.                     queryUpdatePdd.append(Costanti.REPOSITORY);
  289.                     queryUpdatePdd.append(" SET ");
  290.                     queryUpdatePdd.append(this.gestoreRepositoryBuste.createSQLSet_PdD(false));
  291.                     queryUpdatePdd.append(" WHERE ID_MESSAGGIO = ? AND TIPO=?");
  292.                     pstmtUpdatePdd = connectionDB.prepareStatement(queryUpdatePdd.toString());
  293.                     pstmtUpdatePdd.setString(1,this.idBusta);
  294.                     pstmtUpdatePdd.setString(2,tipoBusta);
  295.                     // Add PreparedStatement
  296.                     stateMSG.getPreparedStatement().put("ClearAccessiPDD_"+tipoBusta+"_"+this.idBusta,pstmtUpdatePdd);
  297.                 }
  298.    
  299.    
  300.             } catch(Exception e) {
  301.                 String errorMsg = "ROLLBACK_BUSTE, setAccessi Errore "+tipoBusta+"/"+this.idBusta+": "+e.getMessage();
  302.                 this.log.info(errorMsg,e);
  303.                 try{
  304.                     if( pstmtUpdateHistory != null )
  305.                         pstmtUpdateHistory.close();
  306.                 } catch(Exception er) {
  307.                     // close
  308.                 }
  309.                 try{
  310.                     if( pstmtUpdateProfilo != null )
  311.                         pstmtUpdateProfilo.close();
  312.                 } catch(Exception er) {
  313.                     // close
  314.                 }
  315.                 try{
  316.                     if( pstmtUpdatePdd != null )
  317.                         pstmtUpdatePdd.close();
  318.                 } catch(Exception er) {
  319.                     // close
  320.                 }
  321.                 throw new ProtocolException(errorMsg,e);
  322.             }
  323.         }else{
  324.             this.log.debug("clearAccessi("+tipoBusta+"/"+this.idBusta+") non effettuato, connessione is null");
  325.         }
  326.     }
  327. }