DriverConfigurazioneDB_routingTableDriver.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 java.util.ArrayList;
  26. import java.util.List;

  27. import org.openspcoop2.core.commons.DBUtils;
  28. import org.openspcoop2.core.commons.ISearch;
  29. import org.openspcoop2.core.commons.Liste;
  30. import org.openspcoop2.core.config.Route;
  31. import org.openspcoop2.core.config.RouteGateway;
  32. import org.openspcoop2.core.config.RouteRegistro;
  33. import org.openspcoop2.core.config.RoutingTable;
  34. import org.openspcoop2.core.config.RoutingTableDefault;
  35. import org.openspcoop2.core.config.RoutingTableDestinazione;
  36. import org.openspcoop2.core.config.constants.CostantiConfigurazione;
  37. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  38. import org.openspcoop2.core.constants.CostantiDB;
  39. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  40. import org.openspcoop2.utils.sql.ISQLQueryObject;
  41. import org.openspcoop2.utils.sql.SQLObjectFactory;

  42. /**
  43.  * DriverConfigurazioneDB_routingTableDriver
  44.  *
  45.  *
  46.  * @author Sandra Giangrandi (sandra@link.it)
  47.  * @author Stefano Corallo (corallo@link.it)
  48.  * @author $Author$
  49.  * @version $Rev$, $Date$
  50.  */
  51. public class DriverConfigurazioneDB_routingTableDriver {

  52.     private DriverConfigurazioneDB driver = null;
  53.    
  54.     protected DriverConfigurazioneDB_routingTableDriver(DriverConfigurazioneDB driver) {
  55.         this.driver = driver;
  56.     }

  57.    
  58.     protected RoutingTable getRoutingTable() throws DriverConfigurazioneException {

  59.         Connection con = null;
  60.         PreparedStatement stm = null;
  61.         ResultSet rs = null;
  62.         PreparedStatement stmSearch = null;
  63.         ResultSet rsSearch = null;
  64.         String sqlQuery = "";

  65.         if (this.driver.atomica) {
  66.             try {
  67.                 con = this.driver.getConnectionFromDatasource("getRoutingTable");

  68.             } catch (Exception e) {
  69.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getRoutingTable] Exception accedendo al datasource :" + e.getMessage(),e);

  70.             }

  71.         } else
  72.             con = this.driver.globalConnection;

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

  74.         try {
  75.             boolean routingEnabled = false;
  76.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  77.             sqlQueryObject.addFromTable(CostantiDB.CONFIGURAZIONE);
  78.             sqlQueryObject.addSelectField("*");
  79.             sqlQuery = sqlQueryObject.createSQLQuery();
  80.             this.driver.logDebug("eseguo query per routing enabled : " + DBUtils.formatSQLString(sqlQuery));
  81.             stm = con.prepareStatement(sqlQuery);

  82.             rs = stm.executeQuery();

  83.             if (rs.next()) {
  84.                 this.driver.logDebug("ConfigurazionePresente");
  85.                 this.driver.logDebug("Risultato query per routing enabled ["+rs.getString("routing_enabled")+"]");
  86.                 routingEnabled = CostantiConfigurazione.ABILITATO.equals(DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs.getString("routing_enabled")));
  87.             }
  88.             rs.close();
  89.             stm.close();
  90.             this.driver.logDebug("RoutingEnabled: "+routingEnabled);

  91.             RoutingTable rt = new RoutingTable();
  92.             rt.setAbilitata(routingEnabled);
  93.             //sia che il routing sia abilitato/disabilitato
  94.             //le rotte possono essere comunque presenti
  95.             //if (routingEnabled) {

  96.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  97.             sqlQueryObject.addFromTable(CostantiDB.ROUTING);
  98.             sqlQueryObject.addSelectField("*");
  99.             sqlQuery = sqlQueryObject.createSQLQuery();
  100.             this.driver.logDebug("eseguo query per routing table : " + DBUtils.formatSQLString(sqlQuery));
  101.             stm = con.prepareStatement(sqlQuery);

  102.             rs = stm.executeQuery();

  103.             String tipo = null;
  104.             String nome = null;
  105.             String tipoRotta = null;
  106.             String tiposoggrotta = null;
  107.             String nomesoggrotta = null;
  108.             long id_registrorotta = 0;
  109.             boolean is_default = false;
  110.             long idR = 0;

  111.             RoutingTableDefault rtdefault = null;
  112.             Route route = null;
  113.             RouteGateway routeGateway = null;
  114.             RouteRegistro routeRegistro = null;
  115.             RoutingTableDestinazione rtd = null;

  116.             int nroute = 0;
  117.             this.driver.logDebug("Check esistenza rotte....");
  118.             while (rs.next()) {

  119.                 this.driver.logDebug("Nuova rotta....["+rs.getInt("is_default")+"]");

  120.                 nroute++;
  121.                 // nuova route
  122.                 route = new Route();

  123.                 idR = rs.getLong("id");
  124.                 tipo = rs.getString("tipo");
  125.                 nome = rs.getString("nome");
  126.                 tipoRotta = rs.getString("tiporotta");
  127.                 nomesoggrotta = rs.getString("nomesoggrotta");
  128.                 tiposoggrotta = rs.getString("tiposoggrotta");
  129.                 id_registrorotta = rs.getLong("registrorotta");
  130.                 if(rs.getInt("is_default")==1)
  131.                     is_default = true;
  132.                 else
  133.                     is_default = false;

  134.                 if (tipoRotta.equalsIgnoreCase("registro")) {
  135.                     // e' una rotta registro
  136.                     routeRegistro = new RouteRegistro();

  137.                     // se e' 0 allora significa ke voglio tutte le rotte
  138.                     if (id_registrorotta != 0) {
  139.                         // mi serve il nome di questa rotta
  140.                         sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  141.                         sqlQueryObject.addFromTable(CostantiDB.REGISTRI);
  142.                         sqlQueryObject.addSelectField("*");
  143.                         sqlQueryObject.addWhereCondition("id = ?");
  144.                         sqlQuery = sqlQueryObject.createSQLQuery();
  145.                         stmSearch = con.prepareStatement(sqlQuery);
  146.                         stmSearch.setLong(1, id_registrorotta);

  147.                         this.driver.logDebug("eseguo query : " + DBUtils.formatSQLString(sqlQuery, id_registrorotta));

  148.                         rsSearch = stmSearch.executeQuery();
  149.                         if (rsSearch.next()) {
  150.                             routeRegistro.setNome(rsSearch.getString("nome"));

  151.                         }
  152.                         rsSearch.close();
  153.                         stmSearch.close();
  154.                     }

  155.                     route.setRegistro(routeRegistro);

  156.                 } else if (tipoRotta.equalsIgnoreCase("gateway")) {
  157.                     // e' una rotta gw
  158.                     routeGateway = new RouteGateway();

  159.                     routeGateway.setNome(nomesoggrotta);
  160.                     routeGateway.setTipo(tiposoggrotta);

  161.                     route.setGateway(routeGateway);
  162.                 }

  163.                 route.setId(idR);

  164.                 // e' di default
  165.                 if (is_default){
  166.                     if(rtdefault==null){
  167.                         rtdefault = new RoutingTableDefault();
  168.                         rt.setDefault(rtdefault);
  169.                     }
  170.                     rt.getDefault().addRoute(route);
  171.                 }
  172.                 else {// allora e' di destinazione
  173.                     rtd = new RoutingTableDestinazione();
  174.                     rtd.setNome(nome);
  175.                     rtd.setTipo(tipo);
  176.                     rtd.addRoute(route);
  177.                     rt.addDestinazione(rtd);
  178.                 }
  179.             }

  180.             this.driver.logDebug("Ci sono " + nroute + " rotte configurate.");
  181.             rs.close();
  182.             stm.close();
  183.             //if (nroute == 0)
  184.             //  throw new DriverConfigurazioneNotFound("[DriverConfigurazioneDB::getRoutingTable] Routing Abilitato ma nessuna route trovata.]");



  185.             //}
  186.             //else {
  187.             //  throw new DriverConfigurazioneNotFound("[DriverConfigurazioneDB::getRoutingTable] Routing Disabilitato]");
  188.             //}

  189.             return rt;


  190.         } catch (SQLException se) {

  191.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getRoutingTable] SqlException: " + se.getMessage(),se);
  192.         }catch (Exception se) {

  193.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::getRoutingTable] Exception: " + se.getMessage(),se);
  194.         } finally {

  195.             //Chiudo statement and resultset
  196.             JDBCUtilities.closeResources(rsSearch, stmSearch);
  197.             JDBCUtilities.closeResources(rs, stm);

  198.             this.driver.closeConnection(con);
  199.         }

  200.     }

  201.     protected void createRoutingTable(RoutingTable routingTable) throws DriverConfigurazioneException {

  202.         if (routingTable == null ||  routingTable.getDefault() == null || routingTable.getDefault().sizeRouteList() == 0)
  203.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createRoutingTable] Parametri non validi.");

  204.         Connection con = null;
  205.         boolean error = false;

  206.         if (this.driver.atomica) {
  207.             try {
  208.                 con = this.driver.getConnectionFromDatasource("createRoutingTable");
  209.                 con.setAutoCommit(false);
  210.             } catch (Exception e) {
  211.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createRoutingTable] Exception accedendo al datasource :" + e.getMessage(),e);

  212.             }

  213.         } else
  214.             con = this.driver.globalConnection;

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

  216.         try {
  217.             this.driver.logDebug("CRUDRoutingTable type = 1");
  218.             // creo soggetto
  219.             DriverConfigurazioneDB_routingTableLIB.CRUDRoutingTable(1, routingTable, con);

  220.         } catch (Exception qe) {
  221.             error = true;
  222.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createRoutingTable] Errore durante la creazione della RoutingTable : " + qe.getMessage(),qe);
  223.         } finally {

  224.             this.driver.closeConnection(error,con);
  225.         }
  226.     }

  227.     protected void updateRoutingTable(RoutingTable routingTable) throws DriverConfigurazioneException {
  228.         if (routingTable == null ||  routingTable.getDefault() == null || routingTable.getDefault().sizeRouteList() == 0)
  229.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateRoutingTable] Parametri non validi.");

  230.         Connection con = null;
  231.         boolean error = false;

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

  238.             }

  239.         } else
  240.             con = this.driver.globalConnection;

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

  242.         try {
  243.             this.driver.logDebug("CRUDRoutingTable type = 2");
  244.             // creo soggetto
  245.             DriverConfigurazioneDB_routingTableLIB.CRUDRoutingTable(2, routingTable, con);

  246.         } catch (Exception qe) {
  247.             error = true;
  248.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateRoutingTable] Errore durante la update della RoutingTable : " + qe.getMessage(),qe);
  249.         } finally {

  250.             this.driver.closeConnection(error,con);
  251.         }
  252.     }

  253.     protected void deleteRoutingTable(RoutingTable routingTable) throws DriverConfigurazioneException {
  254.         if (routingTable == null)
  255.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteRoutingTable] Parametri non validi.");

  256.         Connection con = null;
  257.         boolean error = false;

  258.         if (this.driver.atomica) {
  259.             try {
  260.                 con = this.driver.getConnectionFromDatasource("deleteRoutingTable");
  261.                 con.setAutoCommit(false);
  262.             } catch (Exception e) {
  263.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteRoutingTable] Exception accedendo al datasource :" + e.getMessage(),e);

  264.             }

  265.         } else
  266.             con = this.driver.globalConnection;

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

  268.         try {
  269.             this.driver.logDebug("CRUDRoutingTable type = 3");
  270.             // creo soggetto
  271.             DriverConfigurazioneDB_routingTableLIB.CRUDRoutingTable(3, routingTable, con);

  272.         } catch (Exception qe) {
  273.             error = true;
  274.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteRoutingTable] Errore durante la delete della RoutingTable : " + qe.getMessage(),qe);
  275.         } finally {

  276.             this.driver.closeConnection(error,con);
  277.         }
  278.     }

  279.     protected List<RoutingTableDestinazione> routingList(ISearch ricerca) throws DriverConfigurazioneException {
  280.         String nomeMetodo = "routingList";
  281.         int idLista = Liste.ROUTING;
  282.         int offset;
  283.         int limit;
  284.         String search;
  285.         String queryString;

  286.         limit = ricerca.getPageSize(idLista);
  287.         offset = ricerca.getIndexIniziale(idLista);
  288.         search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));


  289.         Connection con = null;
  290.         PreparedStatement stmt=null;
  291.         ResultSet risultato=null;
  292.         ArrayList<RoutingTableDestinazione> lista = new ArrayList<>();

  293.         if (this.driver.atomica) {
  294.             try {
  295.                 con = this.driver.getConnectionFromDatasource("routingList");
  296.             } catch (Exception e) {
  297.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  298.             }

  299.         } else
  300.             con = this.driver.globalConnection;

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

  302.         try {

  303.             if (!search.equals("")) {
  304.                 //query con search
  305.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  306.                 sqlQueryObject.addFromTable(CostantiDB.ROUTING);
  307.                 sqlQueryObject.addSelectCountField("*", "cont");
  308.                 sqlQueryObject.addWhereCondition("is_default = 0");
  309.                 sqlQueryObject.addWhereCondition(false, sqlQueryObject.getWhereLikeCondition("tipo",search,true,true), sqlQueryObject.getWhereLikeCondition("nome",search,true,true));
  310.                 sqlQueryObject.setANDLogicOperator(true);
  311.                 queryString = sqlQueryObject.createSQLQuery();
  312.             } else {
  313.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  314.                 sqlQueryObject.addFromTable(CostantiDB.ROUTING);
  315.                 sqlQueryObject.addSelectCountField("*", "cont");
  316.                 sqlQueryObject.addWhereCondition("is_default = 0");
  317.                 queryString = sqlQueryObject.createSQLQuery();
  318.             }
  319.             stmt = con.prepareStatement(queryString);
  320.             risultato = stmt.executeQuery();
  321.             if (risultato.next())
  322.                 ricerca.setNumEntries(idLista,risultato.getInt(1));
  323.             risultato.close();
  324.             stmt.close();

  325.             // ricavo le entries
  326.             if (limit == 0) // con limit
  327.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;

  328.             if (!search.equals("")) { // con search
  329.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  330.                 sqlQueryObject.addFromTable(CostantiDB.ROUTING);
  331.                 sqlQueryObject.addSelectField("id");
  332.                 sqlQueryObject.addSelectField("nome");
  333.                 sqlQueryObject.addSelectField("tipo");
  334.                 sqlQueryObject.addSelectField("tiporotta");
  335.                 sqlQueryObject.addSelectField("is_default");
  336.                 sqlQueryObject.addWhereCondition("is_default = 0");
  337.                 sqlQueryObject.addWhereCondition(false, sqlQueryObject.getWhereLikeCondition("tipo",search,true,true), sqlQueryObject.getWhereLikeCondition("nome",search,true,true));
  338.                 sqlQueryObject.setANDLogicOperator(true);
  339.                 sqlQueryObject.addOrderBy("tipo");
  340.                 sqlQueryObject.addOrderBy("nome");
  341.                 sqlQueryObject.setSortType(true);
  342.                 sqlQueryObject.setLimit(limit);
  343.                 sqlQueryObject.setOffset(offset);
  344.                 queryString = sqlQueryObject.createSQLQuery();
  345.             } else {
  346.                 // senza search
  347.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  348.                 sqlQueryObject.addFromTable(CostantiDB.ROUTING);
  349.                 sqlQueryObject.addSelectField("id");
  350.                 sqlQueryObject.addSelectField("nome");
  351.                 sqlQueryObject.addSelectField("tipo");
  352.                 sqlQueryObject.addSelectField("tiporotta");
  353.                 sqlQueryObject.addSelectField("is_default");
  354.                 sqlQueryObject.addWhereCondition("is_default = 0");
  355.                 sqlQueryObject.addOrderBy("tipo");
  356.                 sqlQueryObject.addOrderBy("nome");
  357.                 sqlQueryObject.setSortType(true);
  358.                 sqlQueryObject.setLimit(limit);
  359.                 sqlQueryObject.setOffset(offset);
  360.                 queryString = sqlQueryObject.createSQLQuery();
  361.             }
  362.             stmt = con.prepareStatement(queryString);
  363.             risultato = stmt.executeQuery();

  364.             RoutingTableDestinazione rtd;
  365.             while (risultato.next()) {

  366.                 rtd = new RoutingTableDestinazione();

  367.                 rtd.setId(risultato.getLong("id"));
  368.                 rtd.setNome(risultato.getString("nome"));
  369.                 rtd.setTipo(risultato.getString("tipo"));
  370.                 // Non è necessario popolare rg e rr
  371.                 // perchè tanto il metodo che prepara la lista
  372.                 // deve solo decidere il tiporotta scelto
  373.                 Route tmpR = new Route();
  374.                 if ("gateway".equals(risultato.getString("tiporotta"))) {
  375.                     RouteGateway rg = new RouteGateway();
  376.                     tmpR.setGateway(rg);
  377.                 } else {
  378.                     RouteRegistro rr = new RouteRegistro();
  379.                     tmpR.setRegistro(rr);
  380.                 }
  381.                 rtd.addRoute(tmpR);

  382.                 lista.add(rtd);
  383.             }

  384.             return lista;

  385.         } catch (Exception qe) {
  386.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  387.         } finally {

  388.             //Chiudo statement and resultset
  389.             JDBCUtilities.closeResources(risultato, stmt);

  390.             this.driver.closeConnection(con);
  391.         }
  392.     }
  393. }