DriverConfigurazioneDB_handlerLIB.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.ConfigurazioneHandler;
  31. import org.openspcoop2.core.config.ConfigurazioneMessageHandlers;
  32. import org.openspcoop2.core.config.ConfigurazioneServiceHandlers;
  33. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  34. import org.openspcoop2.core.constants.CostantiDB;
  35. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  36. import org.openspcoop2.utils.sql.ISQLQueryObject;
  37. import org.openspcoop2.utils.sql.SQLObjectFactory;

  38. /**
  39.  * DriverConfigurazioneDB_handlerLIB
  40.  *
  41.  * @author Stefano Corallo - corallo@link.it
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */
  45. public class DriverConfigurazioneDB_handlerLIB {



  46.     protected static ConfigurazioneMessageHandlers readConfigurazioneMessageHandlers(Connection con, Long idPortaDelegata, Long idPortaApplicatva, boolean request) throws Exception {
  47.         PreparedStatement stm1=null;
  48.         ResultSet rs1= null;
  49.         try {
  50.            
  51.             ConfigurazioneMessageHandlers config = null;
  52.            
  53.             String tabella = CostantiDB.CONFIGURAZIONE_HANDLERS;
  54.             if(idPortaDelegata!=null) {
  55.                 tabella = CostantiDB.PORTE_DELEGATE_HANDLERS;
  56.             }
  57.             else if(idPortaApplicatva!=null) {
  58.                 tabella = CostantiDB.PORTE_APPLICATIVE_HANDLERS;
  59.             }
  60.            
  61.             List<String> tipologie = new ArrayList<>();
  62.             String suffix = request ? CostantiDB.HANDLER_REQUEST_SUFFIX : CostantiDB.HANDLER_RESPONSE_SUFFIX;
  63.             tipologie.add(CostantiDB.HANDLER_PRE_IN+suffix);
  64.             tipologie.add(CostantiDB.HANDLER_IN+suffix);
  65.             if(request) {
  66.                 tipologie.add(CostantiDB.HANDLER_IN_PROTOCOL+suffix);
  67.             }
  68.             tipologie.add(CostantiDB.HANDLER_OUT+suffix);
  69.             tipologie.add(CostantiDB.HANDLER_POST_OUT+suffix);
  70.            
  71.             for (String tipologia : tipologie) {
  72.                
  73.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  74.                 sqlQueryObject.addFromTable(tabella);
  75.                 sqlQueryObject.addSelectField("*");
  76.                 if(idPortaDelegata!=null || idPortaApplicatva!=null) {
  77.                     sqlQueryObject.addWhereCondition("id_porta=?");
  78.                 }
  79.                 sqlQueryObject.addWhereCondition("tipologia=?");
  80.                 sqlQueryObject.addOrderBy("posizione");
  81.                
  82.                 sqlQueryObject.setANDLogicOperator(true);
  83.                 String sqlQuery = sqlQueryObject.createSQLQuery();
  84.                 stm1 = con.prepareStatement(sqlQuery);
  85.                 int index = 1;
  86.                 if(idPortaDelegata!=null) {
  87.                     stm1.setLong(index++, idPortaDelegata);
  88.                 }
  89.                 else if(idPortaApplicatva!=null) {
  90.                     stm1.setLong(index++, idPortaApplicatva);
  91.                 }
  92.                 stm1.setString(index++, tipologia);
  93.                 rs1 = stm1.executeQuery();
  94.                 List<ConfigurazioneHandler> list = new ArrayList<ConfigurazioneHandler>();
  95.                 while(rs1.next()){
  96.                    
  97.                     ConfigurazioneHandler handler = new ConfigurazioneHandler();
  98.                     handler.setId(rs1.getLong("id"));
  99.                     handler.setTipo(rs1.getString("tipo"));
  100.                     handler.setPosizione(rs1.getInt("posizione"));
  101.                     String stato = rs1.getString("stato");
  102.                     if(stato!=null && !"".equals(stato)) {
  103.                         handler.setStato(DriverConfigurazioneDBLib.getEnumStatoFunzionalita(stato));
  104.                     }
  105.                     list.add(handler);
  106.                    
  107.                 }
  108.                 rs1.close();
  109.                 stm1.close();
  110.                
  111.                 if(!list.isEmpty()) {
  112.                     if(config==null) {
  113.                         config = new ConfigurazioneMessageHandlers();
  114.                     }
  115.                     if(tipologia.startsWith(CostantiDB.HANDLER_PRE_IN)) {
  116.                         config.setPreInList(list);
  117.                     }
  118.                     else if(tipologia.startsWith(CostantiDB.HANDLER_IN_PROTOCOL)) { // prima di in senno c'e' il bu
  119.                         config.setInProtocolInfoList(list);
  120.                     }
  121.                     else if(tipologia.startsWith(CostantiDB.HANDLER_IN)) {
  122.                         config.setInList(list);
  123.                     }
  124.                     else if(tipologia.startsWith(CostantiDB.HANDLER_OUT)) {
  125.                         config.setOutList(list);
  126.                     }
  127.                     else if(tipologia.startsWith(CostantiDB.HANDLER_POST_OUT)) {
  128.                         config.setPostOutList(list);
  129.                     }
  130.                 }
  131.             }
  132.            
  133.             return config;
  134.            
  135.         }finally {
  136.             JDBCUtilities.closeResources(rs1, stm1);
  137.         }
  138.     }
  139.    
  140.     protected static ConfigurazioneServiceHandlers readConfigurazioneServiceHandlers(Connection con, Long idPortaDelegata, Long idPortaApplicatva, boolean request) throws Exception {
  141.         PreparedStatement stm1=null;
  142.         ResultSet rs1= null;
  143.         try {
  144.            
  145.             ConfigurazioneServiceHandlers config = null;
  146.            
  147.             String tabella = CostantiDB.CONFIGURAZIONE_HANDLERS;
  148.             if(idPortaDelegata!=null) {
  149.                 tabella = CostantiDB.PORTE_DELEGATE_HANDLERS;
  150.             }
  151.             else if(idPortaApplicatva!=null) {
  152.                 tabella = CostantiDB.PORTE_APPLICATIVE_HANDLERS;
  153.             }
  154.            
  155.             List<String> tipologie = new ArrayList<>();
  156.             tipologie.add(CostantiDB.HANDLER_INIT);
  157.             tipologie.add(CostantiDB.HANDLER_EXIT);
  158.             tipologie.add(CostantiDB.HANDLER_INTEGRATION_MANAGER_REQUEST);
  159.             tipologie.add(CostantiDB.HANDLER_INTEGRATION_MANAGER_RESPONSE);
  160.            
  161.             for (String tipologia : tipologie) {
  162.                
  163.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  164.                 sqlQueryObject.addFromTable(tabella);
  165.                 sqlQueryObject.addSelectField("*");
  166.                 if(idPortaDelegata!=null || idPortaApplicatva!=null) {
  167.                     sqlQueryObject.addWhereCondition("id_porta=?");
  168.                 }
  169.                 sqlQueryObject.addWhereCondition("tipologia=?");
  170.                 sqlQueryObject.addOrderBy("posizione");
  171.                
  172.                 sqlQueryObject.setANDLogicOperator(true);
  173.                 String sqlQuery = sqlQueryObject.createSQLQuery();
  174.                 stm1 = con.prepareStatement(sqlQuery);
  175.                 int index = 1;
  176.                 if(idPortaDelegata!=null) {
  177.                     stm1.setLong(index++, idPortaDelegata);
  178.                 }
  179.                 else if(idPortaApplicatva!=null) {
  180.                     stm1.setLong(index++, idPortaApplicatva);
  181.                 }
  182.                 stm1.setString(index++, tipologia);
  183.                 rs1 = stm1.executeQuery();
  184.                 List<ConfigurazioneHandler> list = new ArrayList<ConfigurazioneHandler>();
  185.                 while(rs1.next()){
  186.                    
  187.                     ConfigurazioneHandler handler = new ConfigurazioneHandler();
  188.                     handler.setId(rs1.getLong("id"));
  189.                     handler.setTipo(rs1.getString("tipo"));
  190.                     handler.setPosizione(rs1.getInt("posizione"));
  191.                     String stato = rs1.getString("stato");
  192.                     if(stato!=null && !"".equals(stato)) {
  193.                         handler.setStato(DriverConfigurazioneDBLib.getEnumStatoFunzionalita(stato));
  194.                     }
  195.                     list.add(handler);
  196.                    
  197.                 }
  198.                 rs1.close();
  199.                 stm1.close();
  200.                
  201.                 if(!list.isEmpty()) {
  202.                     if(config==null) {
  203.                         config = new ConfigurazioneServiceHandlers();
  204.                     }
  205.                     if(tipologia.equals(CostantiDB.HANDLER_INIT)) {
  206.                         config.setInitList(list);
  207.                     }
  208.                     else if(tipologia.equals(CostantiDB.HANDLER_EXIT)) {
  209.                         config.setExitList(list);
  210.                     }
  211.                     else if(tipologia.equals(CostantiDB.HANDLER_INTEGRATION_MANAGER_REQUEST)) {
  212.                         config.setIntegrationManagerRequestList(list);
  213.                     }
  214.                     else if(tipologia.equals(CostantiDB.HANDLER_INTEGRATION_MANAGER_RESPONSE)) {
  215.                         config.setIntegrationManagerResponseList(list);
  216.                     }
  217.                 }
  218.             }
  219.            
  220.             return config;
  221.            
  222.         }finally {
  223.             JDBCUtilities.closeResources(rs1, stm1);
  224.         }
  225.     }
  226.    
  227.     static void CRUDConfigurazioneMessageHandlers(int type, Connection con, Long idPortaDelegata, Long idPortaApplicatva, boolean request, ConfigurazioneMessageHandlers config) throws DriverConfigurazioneException {
  228.        
  229.         Long idPorta = null;
  230.         String tabella = CostantiDB.CONFIGURAZIONE_HANDLERS;
  231.         if(idPortaDelegata!=null) {
  232.             tabella = CostantiDB.PORTE_DELEGATE_HANDLERS;
  233.             idPorta = idPortaDelegata;
  234.         }
  235.         else if(idPortaApplicatva!=null) {
  236.             tabella = CostantiDB.PORTE_APPLICATIVE_HANDLERS;
  237.             idPorta = idPortaApplicatva;
  238.         }
  239.        
  240.         String suffix = request ? CostantiDB.HANDLER_REQUEST_SUFFIX : CostantiDB.HANDLER_RESPONSE_SUFFIX;
  241.                
  242.         PreparedStatement updateStmt = null;
  243.         try {
  244.             switch (type) {
  245.             case CREATE:
  246.        
  247.                 if(config==null) {
  248.                     break;
  249.                 }
  250.                
  251.                 createConfigurazioneHandlers(con, config.getPreInList(), tabella, CostantiDB.HANDLER_PRE_IN+suffix, idPorta);
  252.                 createConfigurazioneHandlers(con, config.getInList(), tabella, CostantiDB.HANDLER_IN+suffix, idPorta);
  253.                 if(request) {
  254.                     createConfigurazioneHandlers(con, config.getInProtocolInfoList(), tabella, CostantiDB.HANDLER_IN_PROTOCOL+suffix, idPorta);
  255.                 }
  256.                 createConfigurazioneHandlers(con, config.getOutList(), tabella, CostantiDB.HANDLER_OUT+suffix, idPorta);
  257.                 createConfigurazioneHandlers(con, config.getPostOutList(), tabella, CostantiDB.HANDLER_POST_OUT+suffix, idPorta);

  258.                 break;
  259.                
  260.             case UPDATE:
  261.                
  262.                 // Faccio prima delete
  263.                 CRUDConfigurazioneMessageHandlers(DELETE, con, idPortaDelegata, idPortaApplicatva, request, null);
  264.                
  265.                 // Creo la nuova immagine
  266.                 if(config!=null) {
  267.                     CRUDConfigurazioneMessageHandlers(CREATE, con, idPortaDelegata, idPortaApplicatva, request, config);
  268.                 }
  269.                 break;
  270.                
  271.             case DELETE:
  272.                
  273.                 List<String> tipologie = new ArrayList<>();
  274.                 tipologie.add(CostantiDB.HANDLER_PRE_IN+suffix);
  275.                 tipologie.add(CostantiDB.HANDLER_IN+suffix);
  276.                 if(request) {
  277.                     tipologie.add(CostantiDB.HANDLER_IN_PROTOCOL+suffix);
  278.                 }
  279.                 tipologie.add(CostantiDB.HANDLER_OUT+suffix);
  280.                 tipologie.add(CostantiDB.HANDLER_POST_OUT+suffix);
  281.                
  282.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  283.                 sqlQueryObject.addDeleteTable(tabella);
  284.                 sqlQueryObject.setANDLogicOperator(true);
  285.                 if(idPorta!=null) {
  286.                     sqlQueryObject.addWhereCondition("id_porta=?");
  287.                 }
  288.                 sqlQueryObject.addWhereINCondition("tipologia", true, tipologie.toArray(new String[1]));
  289.                 String updateQuery = sqlQueryObject.createSQLDelete();
  290.                 updateStmt = con.prepareStatement(updateQuery);
  291.                 int index = 1;
  292.                 if(idPorta!=null) {
  293.                     updateStmt.setLong(index++, idPorta);
  294.                 }
  295.                 updateStmt.executeUpdate();
  296.                 updateStmt.close();

  297.                 break;
  298.             }
  299.        
  300.         } catch (SQLException se) {
  301.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDConfigurazioneMessageHandlers] SQLException [" + se.getMessage() + "].",se);
  302.         }catch (Exception se) {
  303.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDConfigurazioneMessageHandlers] Exception [" + se.getMessage() + "].",se);
  304.         } finally {
  305.    
  306.             JDBCUtilities.closeResources(updateStmt);
  307.            
  308.         }
  309.     }
  310.    
  311.     static void CRUDConfigurazioneServiceHandlers(int type, Connection con, Long idPortaDelegata, Long idPortaApplicatva, boolean request, ConfigurazioneServiceHandlers config) throws DriverConfigurazioneException {
  312.        
  313.         Long idPorta = null;
  314.         String tabella = CostantiDB.CONFIGURAZIONE_HANDLERS;
  315.         if(idPortaDelegata!=null) {
  316.             tabella = CostantiDB.PORTE_DELEGATE_HANDLERS;
  317.             idPorta = idPortaDelegata;
  318.         }
  319.         else if(idPortaApplicatva!=null) {
  320.             tabella = CostantiDB.PORTE_APPLICATIVE_HANDLERS;
  321.             idPorta = idPortaApplicatva;
  322.         }
  323.        
  324.         @SuppressWarnings("unused")
  325.         String suffix = request ? CostantiDB.HANDLER_REQUEST_SUFFIX : CostantiDB.HANDLER_RESPONSE_SUFFIX;
  326.                
  327.         PreparedStatement updateStmt = null;
  328.         try {
  329.             switch (type) {
  330.             case CREATE:
  331.        
  332.                 if(config==null) {
  333.                     break;
  334.                 }
  335.                
  336.                 createConfigurazioneHandlers(con, config.getInitList(), tabella, CostantiDB.HANDLER_INIT, idPorta);
  337.                 createConfigurazioneHandlers(con, config.getExitList(), tabella, CostantiDB.HANDLER_EXIT, idPorta);
  338.                 createConfigurazioneHandlers(con, config.getIntegrationManagerRequestList(), tabella, CostantiDB.HANDLER_INTEGRATION_MANAGER_REQUEST, idPorta);
  339.                 createConfigurazioneHandlers(con, config.getIntegrationManagerResponseList(), tabella, CostantiDB.HANDLER_INTEGRATION_MANAGER_RESPONSE, idPorta);      

  340.                 break;
  341.                
  342.             case UPDATE:
  343.                
  344.                 // Faccio prima delete
  345.                 CRUDConfigurazioneServiceHandlers(DELETE, con, idPortaDelegata, idPortaApplicatva, request, null);
  346.                
  347.                 // Creo la nuova immagine
  348.                 if(config!=null) {
  349.                     CRUDConfigurazioneServiceHandlers(CREATE, con, idPortaDelegata, idPortaApplicatva, request, config);
  350.                 }
  351.                 break;
  352.                
  353.             case DELETE:
  354.                
  355.                 List<String> tipologie = new ArrayList<>();
  356.                 tipologie.add(CostantiDB.HANDLER_INIT);
  357.                 tipologie.add(CostantiDB.HANDLER_EXIT);
  358.                 tipologie.add(CostantiDB.HANDLER_INTEGRATION_MANAGER_REQUEST);
  359.                 tipologie.add(CostantiDB.HANDLER_INTEGRATION_MANAGER_RESPONSE);
  360.                
  361.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  362.                 sqlQueryObject.addDeleteTable(tabella);
  363.                 sqlQueryObject.setANDLogicOperator(true);
  364.                 if(idPorta!=null) {
  365.                     sqlQueryObject.addWhereCondition("id_porta=?");
  366.                 }
  367.                 sqlQueryObject.addWhereINCondition("tipologia", true, tipologie.toArray(new String[1]));
  368.                 String updateQuery = sqlQueryObject.createSQLDelete();
  369.                 updateStmt = con.prepareStatement(updateQuery);
  370.                 int index = 1;
  371.                 if(idPorta!=null) {
  372.                     updateStmt.setLong(index++, idPorta);
  373.                 }
  374.                 updateStmt.executeUpdate();
  375.                 updateStmt.close();

  376.                 break;
  377.             }
  378.        
  379.         } catch (SQLException se) {
  380.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDConfigurazioneMessageHandlers] SQLException [" + se.getMessage() + "].",se);
  381.         }catch (Exception se) {
  382.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDConfigurazioneMessageHandlers] Exception [" + se.getMessage() + "].",se);
  383.         } finally {
  384.    
  385.             JDBCUtilities.closeResources(updateStmt);
  386.            
  387.         }
  388.     }
  389.    
  390.     private static void createConfigurazioneHandlers(Connection con, List<ConfigurazioneHandler> list, String tabella, String tipologia, Long idPorta) throws Exception {
  391.         if(list!=null && !list.isEmpty()) {
  392.             for (ConfigurazioneHandler handler : list) {
  393.                 PreparedStatement updateStmt = null;
  394.                 try {
  395.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  396.                     sqlQueryObject.addInsertTable(tabella);
  397.                     if(idPorta!=null) {
  398.                         sqlQueryObject.addInsertField("id_porta", "?");
  399.                     }
  400.                     sqlQueryObject.addInsertField("tipologia", "?");
  401.                     sqlQueryObject.addInsertField("tipo", "?");
  402.                     sqlQueryObject.addInsertField("posizione", "?");
  403.                     sqlQueryObject.addInsertField("stato", "?");
  404.                     String updateQuery = sqlQueryObject.createSQLInsert();
  405.                     updateStmt = con.prepareStatement(updateQuery);
  406.                     int index = 1;
  407.                     if(idPorta!=null) {
  408.                         updateStmt.setLong(index++, idPorta);
  409.                     }
  410.                     updateStmt.setString(index++, tipologia);
  411.                     updateStmt.setString(index++, handler.getTipo());
  412.                     updateStmt.setInt(index++, handler.getPosizione());
  413.                     updateStmt.setString(index++, DriverConfigurazioneDBLib.getValue(handler.getStato()));
  414.                     updateStmt.executeUpdate();
  415.                     updateStmt.close();
  416.                     updateStmt = null;
  417.                 } finally {
  418.                     JDBCUtilities.closeResources(updateStmt);  
  419.                 }
  420.             }
  421.         }
  422.     }

  423. }