DriverConfigurazioneDB_gestioneErroreDriver.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.core.config.driver.db;

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

  25. import org.openspcoop2.core.commons.DBUtils;
  26. import org.openspcoop2.core.config.GestioneErrore;
  27. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  28. import org.openspcoop2.core.config.driver.DriverConfigurazioneNotFound;
  29. import org.openspcoop2.core.constants.CostantiDB;
  30. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  31. import org.openspcoop2.utils.sql.ISQLQueryObject;
  32. import org.openspcoop2.utils.sql.SQLObjectFactory;

  33. /**
  34.  * DriverConfigurazioneDB_gestioneErroreDriver
  35.  *
  36.  *
  37.  * @author Sandra Giangrandi (sandra@link.it)
  38.  * @author Stefano Corallo (corallo@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class DriverConfigurazioneDB_gestioneErroreDriver {

  43.     private DriverConfigurazioneDB driver = null;
  44.    
  45.     protected DriverConfigurazioneDB_gestioneErroreDriver(DriverConfigurazioneDB driver) {
  46.         this.driver = driver;
  47.     }
  48.    


  49.     /**
  50.      * Restituisce la gestione dell'errore di default definita nella Porta di Dominio per il componente di cooperazione
  51.      *
  52.      * @return La gestione dell'errore
  53.      *
  54.      */
  55.     protected GestioneErrore getGestioneErroreComponenteCooperazione() throws DriverConfigurazioneException,DriverConfigurazioneNotFound{
  56.         return getGestioneErrore(true);
  57.     }

  58.     /**
  59.      * Restituisce la gestione dell'errore di default definita nella Porta di Dominio per il componente di integrazione
  60.      *
  61.      * @return La gestione dell'errore
  62.      *
  63.      */
  64.     protected GestioneErrore getGestioneErroreComponenteIntegrazione() throws DriverConfigurazioneException,DriverConfigurazioneNotFound{
  65.         return getGestioneErrore(false);
  66.     }


  67.     /**
  68.      * Restituisce la gestione dell'errore di default definita nella Porta di
  69.      * Dominio
  70.      *
  71.      * @return La gestione dell'errore
  72.      *
  73.      */
  74.     private GestioneErrore getGestioneErrore(boolean cooperazione) throws DriverConfigurazioneException, DriverConfigurazioneNotFound {

  75.         Connection con = null;
  76.         PreparedStatement stm = null;
  77.         ResultSet rs = null;

  78.         String sqlQuery = "";

  79.         String tipoOperazione = "GestioneErrore per componente di Cooperazione";
  80.         if(cooperazione==false)
  81.             tipoOperazione = "GestioneErrore per componente di Integrazione";

  82.         if (this.driver.atomica) {
  83.             try {
  84.                 con = this.driver.getConnectionFromDatasource("getGestioneErrore("+cooperazione+")");

  85.             } catch (Exception e) {
  86.                 throw new DriverConfigurazioneException("["+tipoOperazione+"] Exception accedendo al datasource :" + e.getMessage(),e);

  87.             }

  88.         } else
  89.             con = this.driver.globalConnection;

  90.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  91.         try {
  92.             GestioneErrore gestioneErrore = null;
  93.             String nomeColonna = "id_ge_cooperazione";
  94.             if(cooperazione==false)
  95.                 nomeColonna = "id_ge_integrazione";
  96.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  97.             sqlQueryObject.addFromTable(CostantiDB.CONFIGURAZIONE);
  98.             sqlQueryObject.addSelectField(nomeColonna);
  99.             sqlQuery = sqlQueryObject.createSQLQuery();

  100.             this.driver.logDebug("eseguo query: " + DBUtils.formatSQLString(sqlQuery));
  101.             stm = con.prepareStatement(sqlQuery);
  102.             rs = stm.executeQuery();

  103.             if (rs.next()) {

  104.                 Long idGestioneErrore = rs.getLong(nomeColonna);
  105.                 if(idGestioneErrore>0){
  106.                     gestioneErrore = DriverConfigurazioneDB_gestioneErroreLIB.getGestioneErrore(idGestioneErrore, con);
  107.                 }

  108.             }

  109.             rs.close();
  110.             stm.close();

  111.             if(gestioneErrore==null){
  112.                 throw new DriverConfigurazioneNotFound(tipoOperazione +" non presente.");
  113.             }
  114.             return gestioneErrore;

  115.         } catch (SQLException se) {
  116.             throw new DriverConfigurazioneException(tipoOperazione +" SqlException: " + se.getMessage(),se);
  117.         }catch (DriverConfigurazioneNotFound e) {
  118.             throw new DriverConfigurazioneNotFound(e);
  119.         }catch (Exception se) {
  120.             throw new DriverConfigurazioneException(tipoOperazione +" Exception: " + se.getMessage(),se);
  121.         } finally {
  122.             //Chiudo statement and resultset
  123.             JDBCUtilities.closeResources(rs, stm);
  124.             this.driver.closeConnection(con);
  125.         }

  126.     }
  127.    
  128.     protected void createGestioneErroreComponenteCooperazione(GestioneErrore gestione) throws DriverConfigurazioneException{
  129.         Connection con = null;
  130.         boolean error = false;

  131.         if (this.driver.atomica) {
  132.             try {
  133.                 con = this.driver.getConnectionFromDatasource("createGestioneErroreComponenteCooperazione");
  134.                 con.setAutoCommit(false);
  135.             } catch (Exception e) {
  136.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createGestioneErroreComponenteCooperazione] Exception accedendo al datasource :" + e.getMessage(),e);

  137.             }

  138.         } else
  139.             con = this.driver.globalConnection;

  140.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  141.         PreparedStatement pstmt = null;
  142.         try {
  143.             this.driver.logDebug("CRUDGestioneErrore type = 1");
  144.             DriverConfigurazioneDB_gestioneErroreLIB.CRUDGestioneErroreComponenteCooperazione(CostantiDB.CREATE, gestione, con);

  145.             // Aggiorno configurazione
  146.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  147.             sqlQueryObject.addUpdateTable(CostantiDB.CONFIGURAZIONE);
  148.             sqlQueryObject.addUpdateField("id_ge_cooperazione", "?");
  149.             String updateQuery = sqlQueryObject.createSQLUpdate();
  150.             pstmt = con.prepareStatement(updateQuery);
  151.             pstmt.setLong(1, gestione.getId());
  152.             pstmt.execute();
  153.             pstmt.close();

  154.         } catch (Exception qe) {
  155.             error = true;
  156.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createGestioneErroreComponenteCooperazione] Errore: " + qe.getMessage(),qe);
  157.         } finally {

  158.             JDBCUtilities.closeResources(pstmt);

  159.             this.driver.closeConnection(error,con);
  160.         }
  161.     }

  162.     /**
  163.      * Aggiorna le informazioni per la gestione dell'errore per il ComponenteCooperazione
  164.      *
  165.      * @param gestione
  166.      * @throws DriverConfigurazioneException
  167.      */
  168.     protected void updateGestioneErroreComponenteCooperazione(GestioneErrore gestione) throws DriverConfigurazioneException{
  169.         Connection con = null;
  170.         boolean error = false;

  171.         if (this.driver.atomica) {
  172.             try {
  173.                 con = this.driver.getConnectionFromDatasource("updateGestioneErroreComponenteCooperazione");
  174.                 con.setAutoCommit(false);
  175.             } catch (Exception e) {
  176.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateGestioneErroreComponenteCooperazione] Exception accedendo al datasource :" + e.getMessage(),e);

  177.             }

  178.         } else
  179.             con = this.driver.globalConnection;

  180.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  181.         try {
  182.             this.driver.logDebug("CRUDGestioneErrore type = 2");
  183.             DriverConfigurazioneDB_gestioneErroreLIB.CRUDGestioneErroreComponenteCooperazione(CostantiDB.UPDATE, gestione, con);

  184.         } catch (Exception qe) {
  185.             error = true;
  186.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateGestioneErroreComponenteCooperazione] Errore: " + qe.getMessage(),qe);
  187.         } finally {

  188.             this.driver.closeConnection(error,con);
  189.         }
  190.     }

  191.     /**
  192.      * Elimina le informazioni per la gestione dell'errore per il ComponenteCooperazione
  193.      *
  194.      * @param gestione
  195.      * @throws DriverConfigurazioneException
  196.      */
  197.     protected void deleteGestioneErroreComponenteCooperazione(GestioneErrore gestione) throws DriverConfigurazioneException{
  198.         Connection con = null;
  199.         boolean error = false;

  200.         if (this.driver.atomica) {
  201.             try {
  202.                 con = this.driver.getConnectionFromDatasource("deleteGestioneErroreComponenteCooperazione");
  203.                 con.setAutoCommit(false);
  204.             } catch (Exception e) {
  205.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteGestioneErroreComponenteCooperazione] Exception accedendo al datasource :" + e.getMessage(),e);

  206.             }

  207.         } else
  208.             con = this.driver.globalConnection;

  209.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  210.         try {
  211.             this.driver.logDebug("CRUDGestioneErrore type = 3");
  212.             DriverConfigurazioneDB_gestioneErroreLIB.CRUDGestioneErroreComponenteCooperazione(CostantiDB.DELETE, gestione, con);

  213.         } catch (Exception qe) {
  214.             error = true;
  215.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteGestioneErroreComponenteCooperazione] Errore: " + qe.getMessage(),qe);
  216.         } finally {

  217.             this.driver.closeConnection(error,con);
  218.         }
  219.     }

  220.     /**
  221.      * Crea le informazioni per la gestione dell'errore per il ComponenteIntegrazione
  222.      *
  223.      * @param gestione
  224.      * @throws DriverConfigurazioneException
  225.      */
  226.     protected void createGestioneErroreComponenteIntegrazione(GestioneErrore gestione) throws DriverConfigurazioneException{
  227.         Connection con = null;
  228.         boolean error = false;

  229.         if (this.driver.atomica) {
  230.             try {
  231.                 con = this.driver.getConnectionFromDatasource("createGestioneErroreComponenteIntegrazione");
  232.                 con.setAutoCommit(false);
  233.             } catch (Exception e) {
  234.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createGestioneErroreComponenteIntegrazione] Exception accedendo al datasource :" + e.getMessage(),e);

  235.             }

  236.         } else
  237.             con = this.driver.globalConnection;

  238.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  239.         PreparedStatement pstmt = null;
  240.         try {
  241.             this.driver.logDebug("CRUDGestioneErrore type = 1");
  242.             DriverConfigurazioneDB_gestioneErroreLIB.CRUDGestioneErroreComponenteIntegrazione(CostantiDB.CREATE, gestione, con);

  243.             // Aggiorno configurazione
  244.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  245.             sqlQueryObject.addUpdateTable(CostantiDB.CONFIGURAZIONE);
  246.             sqlQueryObject.addUpdateField("id_ge_integrazione", "?");
  247.             String updateQuery = sqlQueryObject.createSQLUpdate();
  248.             pstmt = con.prepareStatement(updateQuery);
  249.             pstmt.setLong(1, gestione.getId());
  250.             pstmt.execute();
  251.             pstmt.close();

  252.         } catch (Exception qe) {
  253.             error = true;
  254.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createGestioneErroreComponenteIntegrazione] Errore: " + qe.getMessage(),qe);
  255.         } finally {

  256.             JDBCUtilities.closeResources(pstmt);

  257.             this.driver.closeConnection(error,con);
  258.         }
  259.     }

  260.     /**
  261.      * Aggiorna le informazioni per la gestione dell'errore per il ComponenteIntegrazione
  262.      *
  263.      * @param gestione
  264.      * @throws DriverConfigurazioneException
  265.      */
  266.     protected void updateGestioneErroreComponenteIntegrazione(GestioneErrore gestione) throws DriverConfigurazioneException{
  267.         Connection con = null;
  268.         boolean error = false;

  269.         if (this.driver.atomica) {
  270.             try {
  271.                 con = this.driver.getConnectionFromDatasource("updateGestioneErroreComponenteIntegrazione");
  272.                 con.setAutoCommit(false);
  273.             } catch (Exception e) {
  274.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateGestioneErroreComponenteIntegrazione] Exception accedendo al datasource :" + e.getMessage(),e);

  275.             }

  276.         } else
  277.             con = this.driver.globalConnection;

  278.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  279.         try {
  280.             this.driver.logDebug("CRUDGestioneErrore type = 2");
  281.             DriverConfigurazioneDB_gestioneErroreLIB.CRUDGestioneErroreComponenteIntegrazione(CostantiDB.UPDATE, gestione, con);

  282.         } catch (Exception qe) {
  283.             error = true;
  284.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateGestioneErroreComponenteIntegrazione] Errore: " + qe.getMessage(),qe);
  285.         } finally {

  286.             this.driver.closeConnection(error,con);
  287.         }
  288.     }

  289.     /**
  290.      * Elimina le informazioni per la gestione dell'errore per il ComponenteIntegrazione
  291.      *
  292.      * @param gestione
  293.      * @throws DriverConfigurazioneException
  294.      */
  295.     protected void deleteGestioneErroreComponenteIntegrazione(GestioneErrore gestione) throws DriverConfigurazioneException{
  296.         Connection con = null;
  297.         boolean error = false;

  298.         if (this.driver.atomica) {
  299.             try {
  300.                 con = this.driver.getConnectionFromDatasource("deleteGestioneErroreComponenteIntegrazione");
  301.                 con.setAutoCommit(false);
  302.             } catch (Exception e) {
  303.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteGestioneErroreComponenteIntegrazione] Exception accedendo al datasource :" + e.getMessage(),e);

  304.             }

  305.         } else
  306.             con = this.driver.globalConnection;

  307.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  308.         try {
  309.             this.driver.logDebug("CRUDGestioneErrore type = 3");
  310.             DriverConfigurazioneDB_gestioneErroreLIB.CRUDGestioneErroreComponenteIntegrazione(CostantiDB.DELETE, gestione, con);

  311.         } catch (Exception qe) {
  312.             error = true;
  313.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteGestioneErroreComponenteIntegrazione] Errore: " + qe.getMessage(),qe);
  314.         } finally {

  315.             this.driver.closeConnection(error,con);
  316.         }
  317.     }
  318. }