DriverConfigurazioneDB_canaliLIB.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.List;

  29. import org.openspcoop2.core.commons.DBUtils;
  30. import org.openspcoop2.core.config.CanaleConfigurazione;
  31. import org.openspcoop2.core.config.CanaleConfigurazioneNodo;
  32. import org.openspcoop2.core.config.CanaliConfigurazione;
  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_canaliLIB
  40.  *
  41.  * @author Stefano Corallo - corallo@link.it
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */
  45. public class DriverConfigurazioneDB_canaliLIB {


  46.     protected static void readCanaliConfigurazione(Connection con, CanaliConfigurazione configCanali, boolean readNodi) throws Exception {
  47.         PreparedStatement stm1=null;
  48.         ResultSet rs1= null;
  49.         try {
  50.            
  51.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  52.             sqlQueryObject.addFromTable(CostantiDB.CONFIGURAZIONE_CANALI);
  53.             sqlQueryObject.addSelectField("*");
  54.             sqlQueryObject.setANDLogicOperator(true);
  55.             String sqlQuery = sqlQueryObject.createSQLQuery();
  56.             stm1 = con.prepareStatement(sqlQuery);
  57.             rs1 = stm1.executeQuery();
  58.             while(rs1.next()){
  59.                
  60.                 CanaleConfigurazione canale = new CanaleConfigurazione();
  61.                 canale.setId(rs1.getLong("id"));
  62.                 canale.setNome(rs1.getString("nome"));
  63.                 canale.setDescrizione(rs1.getString("descrizione"));
  64.                 int v = rs1.getInt("canale_default");
  65.                 if(v == CostantiDB.TRUE) {
  66.                     canale.setCanaleDefault(true);
  67.                 }
  68.                 else {
  69.                     canale.setCanaleDefault(false);
  70.                 }
  71.                 configCanali.addCanale(canale);
  72.                
  73.             }
  74.             rs1.close();
  75.             stm1.close();

  76.             if(readNodi) {
  77.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  78.                 sqlQueryObject.addFromTable(CostantiDB.CONFIGURAZIONE_CANALI_NODI);
  79.                 sqlQueryObject.addSelectField("*");
  80.                 sqlQueryObject.setANDLogicOperator(true);
  81.                 sqlQuery = sqlQueryObject.createSQLQuery();
  82.                 stm1 = con.prepareStatement(sqlQuery);
  83.                 rs1 = stm1.executeQuery();
  84.                 while(rs1.next()){
  85.                    
  86.                     CanaleConfigurazioneNodo nodo = new CanaleConfigurazioneNodo();
  87.                     nodo.setId(rs1.getLong("id"));
  88.                     nodo.setNome(rs1.getString("nome"));
  89.                     nodo.setDescrizione(rs1.getString("descrizione"));
  90.                     List<String> l = DBUtils.convertToList(rs1.getString("canali"));
  91.                     nodo.setCanaleList(l);
  92.                     configCanali.addNodo(nodo);
  93.                    
  94.                 }
  95.             }

  96.         }finally {
  97.             JDBCUtilities.closeResources(rs1, stm1);
  98.         }
  99.     }
  100.    
  101.     static void CRUDCanaliConfigurazione(int type, Connection con, CanaliConfigurazione canaliConfigurazione) throws DriverConfigurazioneException {
  102.        
  103.         PreparedStatement updateStmt = null;
  104.         try {
  105.             switch (type) {
  106.             case CREATE:
  107.        
  108.                 if(canaliConfigurazione==null) {
  109.                     break;
  110.                 }
  111.                
  112.                 if(canaliConfigurazione.sizeCanaleList()>0) {
  113.                     for (CanaleConfigurazione canale : canaliConfigurazione.getCanaleList()) {
  114.                         ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  115.                         sqlQueryObject.addInsertTable(CostantiDB.CONFIGURAZIONE_CANALI);
  116.                         sqlQueryObject.addInsertField("nome", "?");
  117.                         sqlQueryObject.addInsertField("descrizione", "?");
  118.                         sqlQueryObject.addInsertField("canale_default", "?");
  119.                         String updateQuery = sqlQueryObject.createSQLInsert();
  120.                         updateStmt = con.prepareStatement(updateQuery);
  121.                         int index = 1;
  122.                         updateStmt.setString(index++, canale.getNome());
  123.                         updateStmt.setString(index++, canale.getDescrizione());
  124.                         updateStmt.setInt(index++, canale.isCanaleDefault() ? CostantiDB.TRUE : CostantiDB.FALSE);
  125.                         updateStmt.executeUpdate();
  126.                         updateStmt.close();
  127.                     }
  128.                 }
  129.                
  130.                 if(canaliConfigurazione.sizeNodoList()>0) {
  131.                     for (CanaleConfigurazioneNodo nodo : canaliConfigurazione.getNodoList()) {
  132.                         ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  133.                         sqlQueryObject.addInsertTable(CostantiDB.CONFIGURAZIONE_CANALI_NODI);
  134.                         sqlQueryObject.addInsertField("nome", "?");
  135.                         sqlQueryObject.addInsertField("descrizione", "?");
  136.                         sqlQueryObject.addInsertField("canali", "?");
  137.                         String updateQuery = sqlQueryObject.createSQLInsert();
  138.                         updateStmt = con.prepareStatement(updateQuery);
  139.                         int index = 1;
  140.                         updateStmt.setString(index++, nodo.getNome());
  141.                         updateStmt.setString(index++, nodo.getDescrizione());
  142.                         StringBuilder bf = new StringBuilder();
  143.                         for (int i = 0; i < nodo.sizeCanaleList(); i++) {
  144.                             if(i>0) {
  145.                                 bf.append(",");
  146.                             }
  147.                             bf.append(nodo.getCanale(i));
  148.                         }
  149.                         String canali = bf.toString();
  150.                         updateStmt.setString(index++, canali);
  151.                         updateStmt.executeUpdate();
  152.                         updateStmt.close();
  153.                     }
  154.                 }
  155.                
  156.                 break;
  157.                
  158.             case UPDATE:
  159.                
  160.                 // Faccio prima delete
  161.                 CRUDCanaliConfigurazione(DELETE, con, canaliConfigurazione);
  162.                
  163.                 // Creo la nuova immagine
  164.                 if(canaliConfigurazione!=null) {
  165.                     CRUDCanaliConfigurazione(CREATE, con, canaliConfigurazione);
  166.                 }
  167.                 break;
  168.                
  169.             case DELETE:
  170.                
  171.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  172.                 sqlQueryObject.addDeleteTable(CostantiDB.CONFIGURAZIONE_CANALI);
  173.                 sqlQueryObject.setANDLogicOperator(true);
  174.                 String updateQuery = sqlQueryObject.createSQLDelete();
  175.                 updateStmt = con.prepareStatement(updateQuery);
  176.                 updateStmt.executeUpdate();
  177.                 updateStmt.close();
  178.                
  179.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  180.                 sqlQueryObject.addDeleteTable(CostantiDB.CONFIGURAZIONE_CANALI_NODI);
  181.                 sqlQueryObject.setANDLogicOperator(true);
  182.                 updateQuery = sqlQueryObject.createSQLDelete();
  183.                 updateStmt = con.prepareStatement(updateQuery);
  184.                 updateStmt.executeUpdate();
  185.                 updateStmt.close();

  186.                 break;
  187.             }
  188.        
  189.         } catch (SQLException se) {
  190.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDCanaliConfigurazione] SQLException [" + se.getMessage() + "].",se);
  191.         }catch (Exception se) {
  192.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDCanaliConfigurazione] Exception [" + se.getMessage() + "].",se);
  193.         } finally {
  194.             JDBCUtilities.closeResources(updateStmt);          
  195.         }
  196.     }
  197.    
  198.    
  199.    
  200. }