DriverConfigurazioneDB_dumpLIB.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 java.util.ArrayList;
  29. import java.util.List;

  30. import org.openspcoop2.core.config.DumpConfigurazione;
  31. import org.openspcoop2.core.config.DumpConfigurazioneRegola;
  32. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  33. import org.openspcoop2.core.constants.CostantiDB;
  34. import org.openspcoop2.utils.TipiDatabase;
  35. import org.openspcoop2.utils.jdbc.CustomKeyGeneratorObject;
  36. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKey;
  37. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKeyJDBCType;
  38. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKeyObject;
  39. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  40. import org.openspcoop2.utils.sql.ISQLQueryObject;
  41. import org.openspcoop2.utils.sql.SQLObjectFactory;

  42. /**
  43.  * DriverConfigurazioneDB_configDumpLIB
  44.  *
  45.  * @author Stefano Corallo - corallo@link.it
  46.  * @author $Author$
  47.  * @version $Rev$, $Date$
  48.  */
  49. public class DriverConfigurazioneDB_dumpLIB {


  50.    
  51.     static void CRUDDumpConfigurazione(int type, Connection con, DumpConfigurazione dumpConfig,
  52.             Long idProprietario, String tipoProprietario) throws DriverConfigurazioneException {
  53.        
  54.         PreparedStatement updateStmt = null;
  55.         try {
  56.             switch (type) {
  57.             case CREATE:
  58.        
  59.                 if(dumpConfig==null) {
  60.                     break;
  61.                 }
  62.                
  63.                 long idRequestIn = -1;
  64.                 if(dumpConfig.getRichiestaIngresso()!=null) {
  65.                     idRequestIn = createDumpConfigurazioneRegola(dumpConfig.getRichiestaIngresso(), con);
  66.                 }
  67.                
  68.                 long idRequestOut = -1;
  69.                 if(dumpConfig.getRichiestaUscita()!=null) {
  70.                     idRequestOut = createDumpConfigurazioneRegola(dumpConfig.getRichiestaUscita(), con);
  71.                 }
  72.                
  73.                 long idResponseIn = -1;
  74.                 if(dumpConfig.getRispostaIngresso()!=null) {
  75.                     idResponseIn = createDumpConfigurazioneRegola(dumpConfig.getRispostaIngresso(), con);
  76.                 }
  77.                
  78.                 long idResponseOut = -1;
  79.                 if(dumpConfig.getRispostaUscita()!=null) {
  80.                     idResponseOut = createDumpConfigurazioneRegola(dumpConfig.getRispostaUscita(), con);
  81.                 }
  82.                
  83.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  84.                 sqlQueryObject.addInsertTable(CostantiDB.DUMP_CONFIGURAZIONE);
  85.                 sqlQueryObject.addInsertField("proprietario", "?");
  86.                 sqlQueryObject.addInsertField("id_proprietario", "?");
  87.                 sqlQueryObject.addInsertField("dump_realtime", "?");
  88.                 sqlQueryObject.addInsertField("id_richiesta_ingresso", "?");
  89.                 sqlQueryObject.addInsertField("id_richiesta_uscita", "?");
  90.                 sqlQueryObject.addInsertField("id_risposta_ingresso", "?");
  91.                 sqlQueryObject.addInsertField("id_risposta_uscita", "?");
  92.                 String updateQuery = sqlQueryObject.createSQLInsert();
  93.                 updateStmt = con.prepareStatement(updateQuery);
  94.                 int index = 1;
  95.                 updateStmt.setString(index++, tipoProprietario);
  96.                 updateStmt.setLong(index++, idProprietario!=null ? idProprietario : -1);
  97.                 updateStmt.setString(index++, DriverConfigurazioneDBLib.getValue(dumpConfig.getRealtime()));
  98.                 updateStmt.setLong(index++, idRequestIn);
  99.                 updateStmt.setLong(index++, idRequestOut);
  100.                 updateStmt.setLong(index++, idResponseIn);
  101.                 updateStmt.setLong(index++, idResponseOut);
  102.                 updateStmt.executeUpdate();
  103.                 updateStmt.close();
  104.                
  105.                 break;
  106.                
  107.             case UPDATE:
  108.                
  109.                 // Per la delete recupero l'immagine attuale del confi
  110.                 DumpConfigurazione dumpConfigOld = DriverConfigurazioneDB_dumpLIB.readDumpConfigurazione(con, idProprietario, tipoProprietario);
  111.                 if(dumpConfigOld!=null) {
  112.                     CRUDDumpConfigurazione(DELETE, con, dumpConfigOld, idProprietario, tipoProprietario);
  113.                 }
  114.                
  115.                 // Creo la nuova immagine
  116.                 if(dumpConfig!=null) {
  117.                     CRUDDumpConfigurazione(CREATE, con, dumpConfig, idProprietario, tipoProprietario);
  118.                 }
  119.                 break;
  120.                
  121.             case DELETE:
  122.                
  123.                 if(dumpConfig==null) {
  124.                     break;
  125.                 }
  126.                
  127.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  128.                 sqlQueryObject.addDeleteTable(CostantiDB.DUMP_CONFIGURAZIONE_REGOLA);
  129.                 sqlQueryObject.addWhereCondition("id=?");
  130.                 sqlQueryObject.setANDLogicOperator(true);
  131.                 updateQuery = sqlQueryObject.createSQLDelete();
  132.                
  133.                 if(dumpConfig.getRichiestaIngresso()!=null) {
  134.                     updateStmt = con.prepareStatement(updateQuery);
  135.                     updateStmt.setLong(1, dumpConfig.getRichiestaIngresso().getId());
  136.                     updateStmt.executeUpdate();
  137.                     updateStmt.close();
  138.                 }
  139.                 if(dumpConfig.getRichiestaUscita()!=null) {
  140.                     updateStmt = con.prepareStatement(updateQuery);
  141.                     updateStmt.setLong(1, dumpConfig.getRichiestaUscita().getId());
  142.                     updateStmt.executeUpdate();
  143.                     updateStmt.close();
  144.                 }
  145.                 if(dumpConfig.getRispostaIngresso()!=null) {
  146.                     updateStmt = con.prepareStatement(updateQuery);
  147.                     updateStmt.setLong(1, dumpConfig.getRispostaIngresso().getId());
  148.                     updateStmt.executeUpdate();
  149.                     updateStmt.close();
  150.                 }
  151.                 if(dumpConfig.getRispostaUscita()!=null) {
  152.                     updateStmt = con.prepareStatement(updateQuery);
  153.                     updateStmt.setLong(1, dumpConfig.getRispostaUscita().getId());
  154.                     updateStmt.executeUpdate();
  155.                     updateStmt.close();
  156.                 }
  157.                
  158.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  159.                 sqlQueryObject.addDeleteTable(CostantiDB.DUMP_CONFIGURAZIONE);
  160.                 if(!CostantiDB.OLD_BACKWARD_COMPATIBILITY_DUMP_CONFIGURAZIONE_PROPRIETARIO_CONFIG.equals(tipoProprietario) &&
  161.                         !CostantiDB.DUMP_CONFIGURAZIONE_PROPRIETARIO_CONFIG_PD.equals(tipoProprietario) &&
  162.                         !CostantiDB.DUMP_CONFIGURAZIONE_PROPRIETARIO_CONFIG_PA.equals(tipoProprietario)) {
  163.                     sqlQueryObject.addWhereCondition("id_proprietario=?");
  164.                 }
  165.                 sqlQueryObject.addWhereCondition("proprietario=?");
  166.                 sqlQueryObject.setANDLogicOperator(true);
  167.                 updateQuery = sqlQueryObject.createSQLDelete();
  168.                 updateStmt = con.prepareStatement(updateQuery);
  169.                 index = 1;
  170.                 if(!CostantiDB.OLD_BACKWARD_COMPATIBILITY_DUMP_CONFIGURAZIONE_PROPRIETARIO_CONFIG.equals(tipoProprietario) &&
  171.                         !CostantiDB.DUMP_CONFIGURAZIONE_PROPRIETARIO_CONFIG_PD.equals(tipoProprietario) &&
  172.                         !CostantiDB.DUMP_CONFIGURAZIONE_PROPRIETARIO_CONFIG_PA.equals(tipoProprietario)) {
  173.                     updateStmt.setLong(index++, idProprietario);
  174.                 }
  175.                 updateStmt.setString(index++, tipoProprietario);
  176.                 updateStmt.executeUpdate();
  177.                
  178.                 break;
  179.             }
  180.        
  181.         } catch (SQLException se) {
  182.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDDumpConfigurazione] SQLException [" + se.getMessage() + "].",se);
  183.         }catch (Exception se) {
  184.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDDumpConfigurazione] Exception [" + se.getMessage() + "].",se);
  185.         } finally {
  186.    
  187.             JDBCUtilities.closeResources(updateStmt);
  188.            
  189.         }
  190.     }
  191.    
  192.     private static long createDumpConfigurazioneRegola(DumpConfigurazioneRegola dumpRegola, Connection con) throws Exception {
  193.         List<InsertAndGeneratedKeyObject> listInsertAndGeneratedKeyObject = new ArrayList<>();
  194.         listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject("body", DriverConfigurazioneDBLib.getValue(dumpRegola.getBody()) , InsertAndGeneratedKeyJDBCType.STRING) );
  195.         listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject("payload", DriverConfigurazioneDBLib.getValue(dumpRegola.getPayload()) , InsertAndGeneratedKeyJDBCType.STRING) );
  196.         listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject("payload_parsing", DriverConfigurazioneDBLib.getValue(dumpRegola.getPayloadParsing()) , InsertAndGeneratedKeyJDBCType.STRING) );
  197.         listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject("attachments", DriverConfigurazioneDBLib.getValue(dumpRegola.getAttachments()) , InsertAndGeneratedKeyJDBCType.STRING) );
  198.         listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject("headers", DriverConfigurazioneDBLib.getValue(dumpRegola.getHeaders()) , InsertAndGeneratedKeyJDBCType.STRING) );
  199.        
  200.         long idDumpconfigurazioneRegola = InsertAndGeneratedKey.insertAndReturnGeneratedKey(con, TipiDatabase.toEnumConstant(DriverConfigurazioneDBLib.tipoDB),
  201.                 new CustomKeyGeneratorObject(CostantiDB.DUMP_CONFIGURAZIONE_REGOLA, CostantiDB.DUMP_CONFIGURAZIONE_REGOLA_COLUMN_ID,
  202.                         CostantiDB.DUMP_CONFIGURAZIONE_REGOLA_SEQUENCE, CostantiDB.DUMP_CONFIGURAZIONE_REGOLA_TABLE_FOR_ID),
  203.                 listInsertAndGeneratedKeyObject.toArray(new InsertAndGeneratedKeyObject[1]));
  204.         if(idDumpconfigurazioneRegola<=0){
  205.             throw new Exception("ID (dump configurazione regola) autoincrementale non ottenuto");
  206.         }
  207.         return idDumpconfigurazioneRegola;
  208.     }
  209.    
  210.     protected static DumpConfigurazione readDumpConfigurazione(Connection con, Long idProprietario, String tipoProprietario) throws Exception {
  211.         PreparedStatement stm1=null;
  212.         ResultSet rs1= null;
  213.         try {
  214.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  215.             sqlQueryObject.addFromTable(CostantiDB.DUMP_CONFIGURAZIONE);
  216.             sqlQueryObject.addSelectField("*");
  217.             if(!CostantiDB.OLD_BACKWARD_COMPATIBILITY_DUMP_CONFIGURAZIONE_PROPRIETARIO_CONFIG.equals(tipoProprietario) &&
  218.                     !CostantiDB.DUMP_CONFIGURAZIONE_PROPRIETARIO_CONFIG_PD.equals(tipoProprietario) &&
  219.                     !CostantiDB.DUMP_CONFIGURAZIONE_PROPRIETARIO_CONFIG_PA.equals(tipoProprietario)) {
  220.                 sqlQueryObject.addWhereCondition("id_proprietario=?");
  221.             }
  222.             sqlQueryObject.addWhereCondition("proprietario=?");
  223.             sqlQueryObject.setANDLogicOperator(true);
  224.            
  225.             String sqlQuery = sqlQueryObject.createSQLQuery();
  226.            
  227.             stm1 = con.prepareStatement(sqlQuery);
  228.             int index = 1;
  229.             if(!CostantiDB.OLD_BACKWARD_COMPATIBILITY_DUMP_CONFIGURAZIONE_PROPRIETARIO_CONFIG.equals(tipoProprietario) &&
  230.                     !CostantiDB.DUMP_CONFIGURAZIONE_PROPRIETARIO_CONFIG_PD.equals(tipoProprietario) &&
  231.                     !CostantiDB.DUMP_CONFIGURAZIONE_PROPRIETARIO_CONFIG_PA.equals(tipoProprietario)) {
  232.                 stm1.setLong(index++, idProprietario);
  233.             }
  234.             stm1.setString(index++, tipoProprietario);
  235.             rs1 = stm1.executeQuery();
  236.            
  237.             //recuper tutti gli appender e le prop di ogni appender
  238.             DumpConfigurazione dumpConfig = null;
  239.             if(rs1.next()){
  240.                
  241.                 dumpConfig = new DumpConfigurazione();
  242.                
  243.                 dumpConfig.setId(rs1.getLong("id"));
  244.                
  245.                 dumpConfig.setRealtime(DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("dump_realtime")));
  246.                
  247.                 long idRequestIn = rs1.getLong("id_richiesta_ingresso");
  248.                 if(idRequestIn>0) {
  249.                     dumpConfig.setRichiestaIngresso(readDumpConfigurazioneRegola(con, idRequestIn));
  250.                 }
  251.                
  252.                 long idRequestOut = rs1.getLong("id_richiesta_uscita");
  253.                 if(idRequestOut>0) {
  254.                     dumpConfig.setRichiestaUscita(readDumpConfigurazioneRegola(con, idRequestOut));
  255.                 }
  256.                
  257.                 long idResponseIn = rs1.getLong("id_risposta_ingresso");
  258.                 if(idResponseIn>0) {
  259.                     dumpConfig.setRispostaIngresso(readDumpConfigurazioneRegola(con, idResponseIn));
  260.                 }
  261.                
  262.                 long idResponseOut = rs1.getLong("id_risposta_uscita");
  263.                 if(idResponseOut>0) {
  264.                     dumpConfig.setRispostaUscita(readDumpConfigurazioneRegola(con, idResponseOut));
  265.                 }
  266.                
  267.             }
  268.             return dumpConfig;

  269.         }finally {
  270.             JDBCUtilities.closeResources(rs1, stm1);
  271.         }
  272.     }
  273.    
  274.     private static DumpConfigurazioneRegola readDumpConfigurazioneRegola(Connection con, long idRegola) throws Exception {
  275.         PreparedStatement stm1=null;
  276.         ResultSet rs1= null;
  277.         try {
  278.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  279.             sqlQueryObject.addFromTable(CostantiDB.DUMP_CONFIGURAZIONE_REGOLA);
  280.             sqlQueryObject.addSelectField("*");
  281.             sqlQueryObject.addWhereCondition("id=?");
  282.             sqlQueryObject.setANDLogicOperator(true);
  283.            
  284.             String sqlQuery = sqlQueryObject.createSQLQuery();
  285.            
  286.             stm1 = con.prepareStatement(sqlQuery);
  287.             stm1.setLong(1, idRegola);
  288.             rs1 = stm1.executeQuery();
  289.            
  290.             //recuper tutti gli appender e le prop di ogni appender
  291.             DumpConfigurazioneRegola dumpConfig = new DumpConfigurazioneRegola(); // ci sono i default
  292.             if(rs1.next()){
  293.                
  294.                 dumpConfig.setId(rs1.getLong("id"));
  295.                 dumpConfig.setBody(DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("body")));
  296.                 dumpConfig.setPayload(DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("payload")));
  297.                 dumpConfig.setPayloadParsing(DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("payload_parsing")));
  298.                 dumpConfig.setAttachments(DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("attachments")));
  299.                 dumpConfig.setHeaders(DriverConfigurazioneDBLib.getEnumStatoFunzionalita(rs1.getString("headers")));
  300.                
  301.             }

  302.             return dumpConfig;

  303.         }finally {
  304.             JDBCUtilities.closeResources(rs1, stm1);
  305.         }
  306.     }
  307.    

  308.    
  309. }