DriverConfigurazioneDBTracciamentoLIB.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 static org.openspcoop2.core.constants.CostantiDB.CREATE;
  22. import static org.openspcoop2.core.constants.CostantiDB.DELETE;
  23. import static org.openspcoop2.core.constants.CostantiDB.UPDATE;

  24. import java.sql.Connection;
  25. import java.sql.PreparedStatement;
  26. import java.sql.ResultSet;
  27. import java.sql.SQLException;

  28. import org.openspcoop2.core.config.TracciamentoConfigurazione;
  29. import org.openspcoop2.core.config.TracciamentoConfigurazioneFiletrace;
  30. import org.openspcoop2.core.config.TracciamentoConfigurazioneFiletraceConnector;
  31. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  32. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  33. import org.openspcoop2.core.constants.CostantiDB;
  34. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  35. import org.openspcoop2.utils.sql.ISQLQueryObject;
  36. import org.openspcoop2.utils.sql.SQLObjectFactory;
  37. import org.openspcoop2.utils.sql.SQLQueryObjectException;

  38. /**
  39.  * DriverConfigurazioneDBTracciamentoLIB
  40.  *
  41.  * @author Poli Andrea (poli@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */
  45. public class DriverConfigurazioneDBTracciamentoLIB {
  46.    
  47.     private DriverConfigurazioneDBTracciamentoLIB() {}


  48.     static void crudTracciamentoConfigurazione(int type, Connection con, TracciamentoConfigurazione tracciamentoConfig,
  49.             Long idProprietario, String tipoProprietario, String tipoConfigurazione) throws DriverConfigurazioneException {
  50.        
  51.         PreparedStatement updateStmt = null;
  52.         try {
  53.             switch (type) {
  54.             case CREATE:
  55.        
  56.                 if(tracciamentoConfig==null) {
  57.                     break;
  58.                 }
  59.                
  60.                 createTracciamentoConfigurazione(con, tracciamentoConfig,
  61.                         idProprietario, tipoProprietario, tipoConfigurazione);
  62.                
  63.                 break;
  64.                
  65.             case UPDATE:
  66.                
  67.                 // Per la delete recupero l'immagine attuale del config
  68.                 TracciamentoConfigurazione tracciamentoConfigOld = DriverConfigurazioneDBTracciamentoLIB.readTracciamentoConfigurazione(con, idProprietario, tipoProprietario, tipoConfigurazione);
  69.                 if(tracciamentoConfigOld!=null) {
  70.                     crudTracciamentoConfigurazione(DELETE, con, tracciamentoConfigOld, idProprietario, tipoProprietario, tipoConfigurazione);
  71.                 }
  72.                
  73.                 // Creo la nuova immagine
  74.                 if(tracciamentoConfig!=null) {
  75.                     crudTracciamentoConfigurazione(CREATE, con, tracciamentoConfig, idProprietario, tipoProprietario, tipoConfigurazione);
  76.                 }
  77.                 break;
  78.                
  79.             case DELETE:
  80.                
  81.                 if(tracciamentoConfig==null) {
  82.                     break;
  83.                 }
  84.                                
  85.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  86.                 sqlQueryObject.addDeleteTable(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE);
  87.                 sqlQueryObject.addWhereCondition(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_COLUMN_PROPRIETARIO+"=?");
  88.                 sqlQueryObject.addWhereCondition(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_COLUMN_TIPO+"=?");
  89.                 if(idProprietario!=null) {
  90.                     sqlQueryObject.addWhereCondition(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_COLUMN_ID_PROPRIETARIO+"=?");
  91.                 }
  92.                 sqlQueryObject.setANDLogicOperator(true);
  93.                 String updateQuery = sqlQueryObject.createSQLDelete();
  94.                 updateStmt = con.prepareStatement(updateQuery);
  95.                 int index = 1;
  96.                 updateStmt.setString(index++, tipoProprietario);
  97.                 updateStmt.setString(index++, tipoConfigurazione);
  98.                 if(idProprietario!=null) {
  99.                     updateStmt.setLong(index++, idProprietario);
  100.                 }
  101.                 updateStmt.executeUpdate();
  102.                
  103.                 break;
  104.                
  105.             default:
  106.                 break;
  107.            
  108.             }
  109.                    
  110.         } catch (SQLException se) {
  111.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::crudTracciamentoConfigurazione] SQLException [" + se.getMessage() + "].",se);
  112.         }catch (Exception se) {
  113.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::crudTracciamentoConfigurazione] Exception [" + se.getMessage() + "].",se);
  114.         } finally {
  115.    
  116.             JDBCUtilities.closeResources(updateStmt);
  117.            
  118.         }
  119.     }
  120.        
  121.     private static void createTracciamentoConfigurazione(Connection con, TracciamentoConfigurazione tracciamentoConfig,
  122.             Long idProprietario, String tipoProprietario, String tipoConfigurazione) throws SQLException, SQLQueryObjectException {
  123.         ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  124.         sqlQueryObject.addInsertTable(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE);
  125.         sqlQueryObject.addInsertField(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_COLUMN_PROPRIETARIO, "?");
  126.         sqlQueryObject.addInsertField(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_COLUMN_TIPO, "?");
  127.         sqlQueryObject.addInsertField(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_COLUMN_ID_PROPRIETARIO, "?");
  128.         sqlQueryObject.addInsertField("stato", "?");
  129.         sqlQueryObject.addInsertField("filtro_esiti", "?");
  130.         sqlQueryObject.addInsertField("request_in", "?");
  131.         sqlQueryObject.addInsertField("request_out", "?");
  132.         sqlQueryObject.addInsertField("response_out", "?");
  133.         sqlQueryObject.addInsertField("response_out_complete", "?");
  134.        
  135.         PreparedStatement updateStmt = null;
  136.         try {
  137.             String updateQuery = sqlQueryObject.createSQLInsert();
  138.             updateStmt = con.prepareStatement(updateQuery);
  139.             int index = 1;
  140.             updateStmt.setString(index++, tipoProprietario);
  141.             updateStmt.setString(index++, tipoConfigurazione);
  142.             updateStmt.setLong(index++, idProprietario!=null ? idProprietario : -1);
  143.             updateStmt.setString(index++, DriverConfigurazioneDBLib.getValue(tracciamentoConfig.getStato()));
  144.             updateStmt.setString(index++, DriverConfigurazioneDBLib.getValue(tracciamentoConfig.getFiltroEsiti()));
  145.             updateStmt.setString(index++, DriverConfigurazioneDBLib.getValue(tracciamentoConfig.getRequestIn()));
  146.             updateStmt.setString(index++, DriverConfigurazioneDBLib.getValue(tracciamentoConfig.getRequestOut()));
  147.             updateStmt.setString(index++, DriverConfigurazioneDBLib.getValue(tracciamentoConfig.getResponseOut()));
  148.             updateStmt.setString(index++, DriverConfigurazioneDBLib.getValue(tracciamentoConfig.getResponseOutComplete()));
  149.             updateStmt.executeUpdate();
  150.             updateStmt.close();
  151.             updateStmt=null;
  152.         } finally {
  153.             JDBCUtilities.closeResources(updateStmt);
  154.         }
  155.     }
  156.    
  157.     protected static TracciamentoConfigurazione readTracciamentoConfigurazione(Connection con, Long idProprietario, String tipoProprietario, String tipoConfigurazione) throws DriverConfigurazioneException {
  158.         PreparedStatement stm1=null;
  159.         ResultSet rs1= null;
  160.         try {
  161.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  162.             sqlQueryObject.addFromTable(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE);
  163.             sqlQueryObject.addSelectField("*");
  164.             sqlQueryObject.addWhereCondition(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_COLUMN_PROPRIETARIO+"=?");
  165.             sqlQueryObject.addWhereCondition(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_COLUMN_TIPO+"=?");
  166.             if(idProprietario!=null) {
  167.                 sqlQueryObject.addWhereCondition(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_COLUMN_ID_PROPRIETARIO+"=?");
  168.             }
  169.             sqlQueryObject.setANDLogicOperator(true);
  170.            
  171.             String sqlQuery = sqlQueryObject.createSQLQuery();
  172.            
  173.             stm1 = con.prepareStatement(sqlQuery);
  174.             int index = 1;
  175.             stm1.setString(index++, tipoProprietario);
  176.             stm1.setString(index++, tipoConfigurazione);
  177.             if(idProprietario!=null) {
  178.                 stm1.setLong(index++, idProprietario);
  179.             }
  180.             rs1 = stm1.executeQuery();
  181.            
  182.             TracciamentoConfigurazione tracciamentoConfig = null;
  183.             if(rs1.next()){
  184.                
  185.                 tracciamentoConfig = new TracciamentoConfigurazione();
  186.                
  187.                 tracciamentoConfig.setId(rs1.getLong("id"));
  188.                
  189.                 tracciamentoConfig.setStato(DriverConfigurazioneDBLib.getEnumStatoFunzionalitaConPersonalizzazione(rs1.getString("stato")));
  190.                 tracciamentoConfig.setFiltroEsiti(DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("filtro_esiti")));
  191.                 tracciamentoConfig.setRequestIn(DriverConfigurazioneDBLib.getEnumStatoFunzionalitaBloccante(rs1.getString("request_in")));
  192.                 tracciamentoConfig.setRequestOut(DriverConfigurazioneDBLib.getEnumStatoFunzionalitaBloccante(rs1.getString("request_out")));
  193.                 tracciamentoConfig.setResponseOut(DriverConfigurazioneDBLib.getEnumStatoFunzionalitaBloccante(rs1.getString("response_out")));
  194.                 tracciamentoConfig.setResponseOutComplete(DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("response_out_complete")));
  195.                
  196.             }
  197.             return tracciamentoConfig;

  198.         }catch (Exception se) {
  199.             throw new DriverConfigurazioneException(se.getMessage(),se);
  200.         }finally {
  201.             JDBCUtilities.closeResources(rs1, stm1);
  202.         }
  203.     }
  204.    
  205.    
  206.    
  207.    
  208.    
  209.    
  210.     static void crudTracciamentoConfigurazioneFiletrace(int type, Connection con, TracciamentoConfigurazioneFiletrace tracciamentoConfig,
  211.             Long idProprietario, String tipoProprietario) throws DriverConfigurazioneException {
  212.        
  213.         PreparedStatement updateStmt = null;
  214.         try {
  215.             switch (type) {
  216.             case CREATE:
  217.        
  218.                 if(tracciamentoConfig==null) {
  219.                     break;
  220.                 }
  221.                
  222.                 createTracciamentoConfigurazioneFiletrace(con, tracciamentoConfig,
  223.                         idProprietario, tipoProprietario);
  224.                
  225.                 break;
  226.                
  227.             case UPDATE:
  228.                
  229.                 // Per la delete recupero l'immagine attuale del config
  230.                 TracciamentoConfigurazioneFiletrace tracciamentoConfigOld = DriverConfigurazioneDBTracciamentoLIB.readTracciamentoConfigurazioneFiletrace(con, idProprietario, tipoProprietario);
  231.                 if(tracciamentoConfigOld!=null) {
  232.                     crudTracciamentoConfigurazioneFiletrace(DELETE, con, tracciamentoConfigOld, idProprietario, tipoProprietario);
  233.                 }
  234.                
  235.                 // Creo la nuova immagine
  236.                 if(tracciamentoConfig!=null) {
  237.                     crudTracciamentoConfigurazioneFiletrace(CREATE, con, tracciamentoConfig, idProprietario, tipoProprietario);
  238.                 }
  239.                 break;
  240.                
  241.             case DELETE:
  242.                
  243.                 if(tracciamentoConfig==null) {
  244.                     break;
  245.                 }
  246.                                
  247.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  248.                 sqlQueryObject.addDeleteTable(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_FILETRACE);
  249.                 sqlQueryObject.addWhereCondition(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_FILETRACE_COLUMN_PROPRIETARIO+"=?");
  250.                 if(idProprietario!=null) {
  251.                     sqlQueryObject.addWhereCondition(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_FILETRACE_COLUMN_ID_PROPRIETARIO+"=?");
  252.                 }
  253.                 sqlQueryObject.setANDLogicOperator(true);
  254.                 String updateQuery = sqlQueryObject.createSQLDelete();
  255.                 updateStmt = con.prepareStatement(updateQuery);
  256.                 int index = 1;
  257.                 updateStmt.setString(index++, tipoProprietario);
  258.                 if(idProprietario!=null) {
  259.                     updateStmt.setLong(index++, idProprietario);
  260.                 }
  261.                 updateStmt.executeUpdate();
  262.                
  263.                 break;
  264.                
  265.             default:
  266.                 break;
  267.            
  268.             }
  269.                    
  270.         } catch (SQLException se) {
  271.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::crudTracciamentoConfigurazioneFiletrace] SQLException [" + se.getMessage() + "].",se);
  272.         }catch (Exception se) {
  273.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::crudTracciamentoConfigurazioneFiletrace] Exception [" + se.getMessage() + "].",se);
  274.         } finally {
  275.    
  276.             JDBCUtilities.closeResources(updateStmt);
  277.            
  278.         }
  279.     }
  280.    
  281.     private static void createTracciamentoConfigurazioneFiletrace(Connection con, TracciamentoConfigurazioneFiletrace tracciamentoConfig,
  282.             Long idProprietario, String tipoProprietario) throws SQLException, SQLQueryObjectException {
  283.         ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  284.         sqlQueryObject.addInsertTable(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_FILETRACE);
  285.         sqlQueryObject.addInsertField(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_FILETRACE_COLUMN_PROPRIETARIO, "?");
  286.         sqlQueryObject.addInsertField(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_FILETRACE_COLUMN_ID_PROPRIETARIO, "?");
  287.         sqlQueryObject.addInsertField("config", "?");
  288.         sqlQueryObject.addInsertField("dump_in_stato", "?");
  289.         sqlQueryObject.addInsertField("dump_in_stato_hdr", "?");
  290.         sqlQueryObject.addInsertField("dump_in_stato_body", "?");
  291.         sqlQueryObject.addInsertField("dump_out_stato", "?");
  292.         sqlQueryObject.addInsertField("dump_out_stato_hdr", "?");
  293.         sqlQueryObject.addInsertField("dump_out_stato_body", "?");
  294.        
  295.         PreparedStatement updateStmt = null;
  296.         try {
  297.             String updateQuery = sqlQueryObject.createSQLInsert();
  298.             updateStmt = con.prepareStatement(updateQuery);
  299.             int index = 1;
  300.             updateStmt.setString(index++, tipoProprietario);
  301.             updateStmt.setLong(index++, idProprietario!=null ? idProprietario : -1);
  302.             updateStmt.setString(index++, tracciamentoConfig.getConfig());
  303.             updateStmt.setString(index++, tracciamentoConfig.getDumpIn()!=null ? DriverConfigurazioneDBLib.getValue(tracciamentoConfig.getDumpIn().getStato()) : null);
  304.             updateStmt.setString(index++, tracciamentoConfig.getDumpIn()!=null ? DriverConfigurazioneDBLib.getValue(tracciamentoConfig.getDumpIn().getHeader()) : null);
  305.             updateStmt.setString(index++, tracciamentoConfig.getDumpIn()!=null ? DriverConfigurazioneDBLib.getValue(tracciamentoConfig.getDumpIn().getPayload()) : null);
  306.             updateStmt.setString(index++, tracciamentoConfig.getDumpOut()!=null ? DriverConfigurazioneDBLib.getValue(tracciamentoConfig.getDumpOut().getStato()) : null);
  307.             updateStmt.setString(index++, tracciamentoConfig.getDumpOut()!=null ? DriverConfigurazioneDBLib.getValue(tracciamentoConfig.getDumpOut().getHeader()) : null);
  308.             updateStmt.setString(index++, tracciamentoConfig.getDumpOut()!=null ? DriverConfigurazioneDBLib.getValue(tracciamentoConfig.getDumpOut().getPayload()) : null);
  309.            
  310.             updateStmt.executeUpdate();
  311.             updateStmt.close();
  312.             updateStmt=null;
  313.         } finally {
  314.             JDBCUtilities.closeResources(updateStmt);
  315.         }
  316.     }
  317.        
  318.     protected static TracciamentoConfigurazioneFiletrace readTracciamentoConfigurazioneFiletrace(Connection con, Long idProprietario, String tipoProprietario) throws DriverConfigurazioneException {
  319.         PreparedStatement stm1=null;
  320.         ResultSet rs1= null;
  321.         try {
  322.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  323.             sqlQueryObject.addFromTable(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_FILETRACE);
  324.             sqlQueryObject.addSelectField("*");
  325.             sqlQueryObject.addWhereCondition(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_FILETRACE_COLUMN_PROPRIETARIO+"=?");
  326.             if(idProprietario!=null) {
  327.                 sqlQueryObject.addWhereCondition(CostantiDB.TRACCIAMENTO_CONFIGURAZIONE_FILETRACE_COLUMN_ID_PROPRIETARIO+"=?");
  328.             }
  329.             sqlQueryObject.setANDLogicOperator(true);
  330.            
  331.             String sqlQuery = sqlQueryObject.createSQLQuery();
  332.            
  333.             stm1 = con.prepareStatement(sqlQuery);
  334.             int index = 1;
  335.             stm1.setString(index++, tipoProprietario);
  336.             if(idProprietario!=null) {
  337.                 stm1.setLong(index++, idProprietario);
  338.             }
  339.             rs1 = stm1.executeQuery();
  340.            
  341.             TracciamentoConfigurazioneFiletrace tracciamentoConfig = null;
  342.             if(rs1.next()){
  343.                
  344.                 tracciamentoConfig = new TracciamentoConfigurazioneFiletrace();
  345.                
  346.                 tracciamentoConfig.setId(rs1.getLong("id"));
  347.                
  348.                 tracciamentoConfig.setConfig(rs1.getString("config"));
  349.                
  350.                 StatoFunzionalita inStato = DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("dump_in_stato"));
  351.                 StatoFunzionalita inStatoHdr = DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("dump_in_stato_hdr"));
  352.                 StatoFunzionalita inStatoBody = DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("dump_in_stato_body"));
  353.                 if(inStato!=null || inStatoHdr!=null || inStatoBody!=null) {
  354.                     tracciamentoConfig.setDumpIn(new TracciamentoConfigurazioneFiletraceConnector());
  355.                     tracciamentoConfig.getDumpIn().setStato(inStato);
  356.                     tracciamentoConfig.getDumpIn().setHeader(inStatoHdr);
  357.                     tracciamentoConfig.getDumpIn().setPayload(inStatoBody);
  358.                 }
  359.                
  360.                 StatoFunzionalita outStato = DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("dump_out_stato"));
  361.                 StatoFunzionalita outStatoHdr = DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("dump_out_stato_hdr"));
  362.                 StatoFunzionalita outStatoBody = DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("dump_out_stato_body"));
  363.                 if(outStato!=null || outStatoHdr!=null || outStatoBody!=null) {
  364.                     tracciamentoConfig.setDumpOut(new TracciamentoConfigurazioneFiletraceConnector());
  365.                     tracciamentoConfig.getDumpOut().setStato(outStato);
  366.                     tracciamentoConfig.getDumpOut().setHeader(outStatoHdr);
  367.                     tracciamentoConfig.getDumpOut().setPayload(outStatoBody);
  368.                 }

  369.             }
  370.             return tracciamentoConfig;

  371.         }catch (Exception se) {
  372.             throw new DriverConfigurazioneException(se.getMessage(),se);
  373.         }finally {
  374.             JDBCUtilities.closeResources(rs1, stm1);
  375.         }
  376.     }

  377.    
  378. }