DriverConfigurazioneDB_pluginsLIB.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.sql.Timestamp;
  29. import java.util.ArrayList;
  30. import java.util.List;

  31. import org.openspcoop2.core.commons.DBUtils;
  32. import org.openspcoop2.core.config.RegistroPlugin;
  33. import org.openspcoop2.core.config.RegistroPluginArchivio;
  34. import org.openspcoop2.core.config.constants.PluginSorgenteArchivio;
  35. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  36. import org.openspcoop2.core.constants.CostantiDB;
  37. import org.openspcoop2.utils.TipiDatabase;
  38. import org.openspcoop2.utils.date.DateManager;
  39. import org.openspcoop2.utils.jdbc.CustomKeyGeneratorObject;
  40. import org.openspcoop2.utils.jdbc.IJDBCAdapter;
  41. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKey;
  42. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKeyJDBCType;
  43. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKeyObject;
  44. import org.openspcoop2.utils.jdbc.JDBCAdapterFactory;
  45. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  46. import org.openspcoop2.utils.sql.ISQLQueryObject;
  47. import org.openspcoop2.utils.sql.SQLObjectFactory;

  48. /**
  49.  * DriverConfigurazioneDB_pluginLIB
  50.  *
  51.  * @author Stefano Corallo - corallo@link.it
  52.  * @author $Author$
  53.  * @version $Rev$, $Date$
  54.  */
  55. public class DriverConfigurazioneDB_pluginsLIB {


  56.    
  57.    
  58.    
  59.     public static void CRUDRegistroPlugin(int type, RegistroPlugin plugin, Connection con) throws DriverConfigurazioneException {
  60.         if (plugin == null)
  61.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDRegistroPlugin] Il plugin non possono essere NULL");
  62.         PreparedStatement updateStmt = null;
  63.         String updateQuery = "";
  64.         PreparedStatement selectStmt = null;
  65.         //String selectQuery = "";
  66.         ResultSet selectRS = null;
  67.        

  68.         try {
  69.            
  70.             if(plugin.getNome()==null) {
  71.                 throw new DriverConfigurazioneException("Nome non fornito");
  72.             }
  73.            
  74.             // Recupero id generic properties
  75.             long idParent = -1;
  76.             if(type == CostantiDB.UPDATE || type == CostantiDB.DELETE) {
  77.                
  78.                 idParent = DBUtils.getIdRegistroPlugin(plugin.getNome(), con, DriverConfigurazioneDBLib.tipoDB);
  79.                 if(idParent<=0) {
  80.                     throw new DriverConfigurazioneException("Plugin con nome '"+plugin.getNome()+"' non trovato");
  81.                 }
  82.                
  83.                 // Elimino anche gli archivi (nell'update le ricreo)
  84.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  85.                 sqlQueryObject.addDeleteTable(CostantiDB.REGISTRO_PLUGINS_ARCHIVE);
  86.                 sqlQueryObject.addWhereCondition("id_plugin=?");
  87.                 sqlQueryObject.setANDLogicOperator(true);
  88.                 updateQuery = sqlQueryObject.createSQLDelete();
  89.                 updateStmt = con.prepareStatement(updateQuery);
  90.                 updateStmt.setLong(1, idParent);
  91.                 updateStmt.executeUpdate();
  92.                 updateStmt.close();

  93.                 // delete
  94.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  95.                 sqlQueryObject.addDeleteTable(CostantiDB.REGISTRO_PLUGINS);
  96.                 sqlQueryObject.addWhereCondition("id=?");
  97.                 updateQuery = sqlQueryObject.createSQLDelete();
  98.                 updateStmt = con.prepareStatement(updateQuery);
  99.                 updateStmt.setLong(1, idParent);
  100.                 updateStmt.executeUpdate();
  101.                 updateStmt.close();
  102.             }
  103.            

  104.             switch (type) {
  105.             case CREATE:
  106.             case UPDATE:
  107.        
  108.                 // insert

  109.                 List<InsertAndGeneratedKeyObject> listInsertAndGeneratedKeyObject = new ArrayList<>();
  110.                 listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject("nome", plugin.getNome() , InsertAndGeneratedKeyJDBCType.STRING) );
  111.                 listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject("posizione", plugin.getPosizione() , InsertAndGeneratedKeyJDBCType.INT) );
  112.                 listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject("stato",  DriverConfigurazioneDBLib.getValue(plugin.getStato()) , InsertAndGeneratedKeyJDBCType.STRING) );
  113.                 listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject("descrizione", plugin.getDescrizione() , InsertAndGeneratedKeyJDBCType.STRING) );
  114.                 listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject("data", plugin.getData() , InsertAndGeneratedKeyJDBCType.TIMESTAMP) );
  115.                 String compatibilita = null;
  116.                 if(plugin.sizeCompatibilitaList()>0) {
  117.                     StringBuilder bf = new StringBuilder();
  118.                     for (int i = 0; i < plugin.sizeCompatibilitaList(); i++) {
  119.                         if(i>0) {
  120.                             bf.append(",");
  121.                         }
  122.                         bf.append(plugin.getCompatibilita(i));
  123.                     }
  124.                     if(bf.length()>0) {
  125.                         compatibilita = bf.toString();
  126.                     }
  127.                 }
  128.                 listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject("compatibilita", compatibilita , InsertAndGeneratedKeyJDBCType.STRING) );
  129.                            
  130.                 long idPlugin = InsertAndGeneratedKey.insertAndReturnGeneratedKey(con, TipiDatabase.toEnumConstant(DriverConfigurazioneDBLib.tipoDB),
  131.                         new CustomKeyGeneratorObject(CostantiDB.REGISTRO_PLUGINS, CostantiDB.REGISTRO_PLUGINS_COLUMN_ID,
  132.                                 CostantiDB.REGISTRO_PLUGINS_SEQUENCE, CostantiDB.REGISTRO_PLUGINS_TABLE_FOR_ID),
  133.                         listInsertAndGeneratedKeyObject.toArray(new InsertAndGeneratedKeyObject[1]));
  134.                 if(idPlugin<=0){
  135.                     throw new Exception("ID (RegistroPlugins) autoincrementale non ottenuto");
  136.                 }

  137.                 for(int l=0; l<plugin.sizeArchivioList();l++){
  138.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  139.                     sqlQueryObject.addInsertTable(CostantiDB.REGISTRO_PLUGINS_ARCHIVE);
  140.                     sqlQueryObject.addInsertField("id_plugin", "?");
  141.                     sqlQueryObject.addInsertField("nome", "?");
  142.                     sqlQueryObject.addInsertField("data", "?");
  143.                     sqlQueryObject.addInsertField("sorgente", "?");
  144.                     sqlQueryObject.addInsertField("contenuto", "?");
  145.                     sqlQueryObject.addInsertField("url", "?");
  146.                     sqlQueryObject.addInsertField("dir", "?");
  147.                     updateQuery = sqlQueryObject.createSQLInsert();
  148.                     updateStmt = con.prepareStatement(updateQuery);
  149.                     int index = 1;
  150.                     updateStmt.setLong(index++, idPlugin);
  151.                     updateStmt.setString(index++, plugin.getArchivio(l).getNome());
  152.                    
  153.                     Timestamp t = DateManager.getTimestamp();
  154.                     if( plugin.getArchivio(l).getData()!=null ) {
  155.                         t = new Timestamp( plugin.getArchivio(l).getData().getTime() );
  156.                     }
  157.                     updateStmt.setTimestamp(index++, t);
  158.                    
  159.                     PluginSorgenteArchivio sorgente = plugin.getArchivio(l).getSorgente();
  160.                     updateStmt.setString(index++, DriverConfigurazioneDBLib.getValue(sorgente));
  161.                     IJDBCAdapter jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(DriverConfigurazioneDBLib.tipoDB);
  162.                     byte[] archivio = null;
  163.                     String url = null;
  164.                     String dir = null;
  165.                     switch (sorgente) {
  166.                     case JAR:
  167.                         archivio = plugin.getArchivio(l).getContenuto();
  168.                         break;
  169.                     case URL:
  170.                         url = plugin.getArchivio(l).getUrl();
  171.                         break;
  172.                     case DIR:
  173.                         dir = plugin.getArchivio(l).getDir();
  174.                         break;
  175.                     }
  176.                     jdbcAdapter.setBinaryData(updateStmt, index++, archivio);
  177.                     updateStmt.setString(index++, url);
  178.                     updateStmt.setString(index++, dir);
  179.                    
  180.                     updateStmt.executeUpdate();
  181.                     updateStmt.close();
  182.                 }

  183.                 break;
  184.             case DELETE:
  185.                 // non rimuovo in quanto gia fatto sopra.
  186.                 break;
  187.             }


  188.         } catch (SQLException se) {
  189.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDRegistroPlugin] SQLException [" + se.getMessage() + "].",se);
  190.         }catch (Exception se) {
  191.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDRegistroPlugin] Exception [" + se.getMessage() + "].",se);
  192.         } finally {

  193.             JDBCUtilities.closeResources(selectRS, selectStmt);
  194.             JDBCUtilities.closeResources(updateStmt);
  195.         }

  196.     }
  197.    
  198.     public static void updateDatiRegistroPlugin(String nome, RegistroPlugin plugin, Connection con) throws DriverConfigurazioneException{
  199.         if (plugin == null)
  200.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDRegistroPlugin] Il plugin non possono essere NULL");
  201.         PreparedStatement updateStmt = null;
  202.         String updateQuery = "";
  203.         PreparedStatement selectStmt = null;
  204.         ResultSet selectRS = null;
  205.    
  206.         try {
  207.            
  208.             long idParent = DBUtils.getIdRegistroPlugin(nome, con, DriverConfigurazioneDBLib.tipoDB);
  209.             if(idParent<=0) {
  210.                 throw new DriverConfigurazioneException("Plugin con nome '"+nome+"' non trovato");
  211.             }
  212.            
  213.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  214.             sqlQueryObject.addUpdateTable(CostantiDB.REGISTRO_PLUGINS);
  215.             sqlQueryObject.addUpdateField("nome", "?");
  216.             sqlQueryObject.addUpdateField("posizione", "?");
  217.             sqlQueryObject.addUpdateField("stato", "?");
  218.             sqlQueryObject.addUpdateField("descrizione", "?");
  219.             sqlQueryObject.addUpdateField("data", "?");
  220.             sqlQueryObject.addUpdateField("compatibilita", "?");
  221.             sqlQueryObject.addWhereCondition("id=?");
  222.             sqlQueryObject.setANDLogicOperator(true);
  223.            
  224.             updateQuery = sqlQueryObject.createSQLUpdate();
  225.             updateStmt = con.prepareStatement(updateQuery);
  226.             int index = 1;
  227.             updateStmt.setString(index++, plugin.getNome());
  228.             updateStmt.setInt(index++, plugin.getPosizione());
  229.             updateStmt.setString(index++, DriverConfigurazioneDBLib.getValue(plugin.getStato()));
  230.             updateStmt.setString(index++, plugin.getDescrizione());
  231.            
  232.             Timestamp t = DateManager.getTimestamp();
  233.             if( plugin.getData()!=null ) {
  234.                 t = new Timestamp( plugin.getData().getTime() );
  235.             }
  236.             updateStmt.setTimestamp(index++, t);
  237.            
  238.             String compatibilita = null;
  239.             if(plugin.sizeCompatibilitaList()>0) {
  240.                 StringBuilder bf = new StringBuilder();
  241.                 for (int i = 0; i < plugin.sizeCompatibilitaList(); i++) {
  242.                     if(i>0) {
  243.                         bf.append(",");
  244.                     }
  245.                     bf.append(plugin.getCompatibilita(i));
  246.                 }
  247.                 if(bf.length()>0) {
  248.                     compatibilita = bf.toString();
  249.                 }
  250.             }
  251.             updateStmt.setString(index++, compatibilita);
  252.                    
  253.             updateStmt.setLong(index++, idParent);
  254.    
  255.             updateStmt.executeUpdate();
  256.             updateStmt.close();
  257.             updateStmt=null;

  258.         } catch (SQLException se) {
  259.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDRegistroPlugin] SQLException [" + se.getMessage() + "].",se);
  260.         }catch (Exception se) {
  261.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDRegistroPlugin] Exception [" + se.getMessage() + "].",se);
  262.         } finally {

  263.             JDBCUtilities.closeResources(selectRS, selectStmt);
  264.             JDBCUtilities.closeResources(updateStmt);
  265.         }
  266.     }
  267.    
  268.     public static void CRUDRegistroPluginArchivio(int type, String nomePlugin, RegistroPluginArchivio plugin, Connection con) throws DriverConfigurazioneException {
  269.         if (plugin == null)
  270.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDRegistroPluginArchivio] Il plugin non possono essere NULL");
  271.         PreparedStatement updateStmt = null;
  272.         String updateQuery = "";
  273.         PreparedStatement selectStmt = null;
  274.         //String selectQuery = "";
  275.         ResultSet selectRS = null;
  276.        

  277.         try {
  278.            
  279.             long idParent = DBUtils.getIdRegistroPlugin(nomePlugin, con, DriverConfigurazioneDBLib.tipoDB);
  280.             if(idParent<=0) {
  281.                 throw new DriverConfigurazioneException("Plugin con nome '"+nomePlugin+"' non trovato");
  282.             }
  283.            
  284.             switch (type) {
  285.             case CREATE:
  286.                
  287.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  288.                 sqlQueryObject.addInsertTable(CostantiDB.REGISTRO_PLUGINS_ARCHIVE);
  289.                 sqlQueryObject.addInsertField("id_plugin", "?");
  290.                 sqlQueryObject.addInsertField("nome", "?");
  291.                 sqlQueryObject.addInsertField("data", "?");
  292.                 sqlQueryObject.addInsertField("sorgente", "?");
  293.                 sqlQueryObject.addInsertField("contenuto", "?");
  294.                 sqlQueryObject.addInsertField("url", "?");
  295.                 sqlQueryObject.addInsertField("dir", "?");
  296.                 updateQuery = sqlQueryObject.createSQLInsert();
  297.                 updateStmt = con.prepareStatement(updateQuery);
  298.                 int index = 1;
  299.                 updateStmt.setLong(index++, idParent);
  300.                 updateStmt.setString(index++, plugin.getNome());
  301.                
  302.                 Timestamp t = DateManager.getTimestamp();
  303.                 if( plugin.getData()!=null ) {
  304.                     t = new Timestamp( plugin.getData().getTime() );
  305.                 }
  306.                 updateStmt.setTimestamp(index++, t);
  307.                
  308.                 PluginSorgenteArchivio sorgente = plugin.getSorgente();
  309.                 updateStmt.setString(index++, DriverConfigurazioneDBLib.getValue(sorgente));
  310.                 IJDBCAdapter jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(DriverConfigurazioneDBLib.tipoDB);
  311.                 byte[] archivio = null;
  312.                 String url = null;
  313.                 String dir = null;
  314.                 switch (sorgente) {
  315.                 case JAR:
  316.                     archivio = plugin.getContenuto();
  317.                     break;
  318.                 case URL:
  319.                     url = plugin.getUrl();
  320.                     break;
  321.                 case DIR:
  322.                     dir = plugin.getDir();
  323.                     break;
  324.                 }
  325.                 jdbcAdapter.setBinaryData(updateStmt, index++, archivio);
  326.                 updateStmt.setString(index++, url);
  327.                 updateStmt.setString(index++, dir);
  328.                
  329.                 updateStmt.executeUpdate();
  330.                 updateStmt.close();
  331.                
  332.                 break;
  333.                
  334.             case UPDATE:
  335.        
  336.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  337.                 sqlQueryObject.addUpdateTable(CostantiDB.REGISTRO_PLUGINS_ARCHIVE);
  338.                 sqlQueryObject.addUpdateField("data", "?");
  339.                 sqlQueryObject.addUpdateField("sorgente", "?");
  340.                 sqlQueryObject.addUpdateField("contenuto", "?");
  341.                 sqlQueryObject.addUpdateField("url", "?");
  342.                 sqlQueryObject.addUpdateField("dir", "?");
  343.                 sqlQueryObject.addWhereCondition("id_plugin = ?");
  344.                 sqlQueryObject.addWhereCondition("nome = ?");
  345.                 sqlQueryObject.setANDLogicOperator(true);
  346.                 updateQuery = sqlQueryObject.createSQLUpdate();
  347.                 updateStmt = con.prepareStatement(updateQuery);
  348.                 index = 1;
  349.                
  350.                 t = DateManager.getTimestamp();
  351.                 updateStmt.setTimestamp(index++, t);
  352.                
  353.                 sorgente = plugin.getSorgente();
  354.                 updateStmt.setString(index++, DriverConfigurazioneDBLib.getValue(sorgente));
  355.                 jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(DriverConfigurazioneDBLib.tipoDB);
  356.                 archivio = null;
  357.                 url = null;
  358.                 dir = null;
  359.                 switch (sorgente) {
  360.                 case JAR:
  361.                     archivio = plugin.getContenuto();
  362.                     break;
  363.                 case URL:
  364.                     url = plugin.getUrl();
  365.                     break;
  366.                 case DIR:
  367.                     dir = plugin.getDir();
  368.                     break;
  369.                 }
  370.                 jdbcAdapter.setBinaryData(updateStmt, index++, archivio);
  371.                 updateStmt.setString(index++, url);
  372.                 updateStmt.setString(index++, dir);

  373.                 updateStmt.setLong(index++, idParent);
  374.                 updateStmt.setString(index++, plugin.getNome());
  375.                 updateStmt.executeUpdate();
  376.                 updateStmt.close();
  377.                
  378.                 break;
  379.             case DELETE:
  380.                
  381.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverConfigurazioneDBLib.tipoDB);
  382.                 sqlQueryObject.addDeleteTable(CostantiDB.REGISTRO_PLUGINS_ARCHIVE);
  383.                 sqlQueryObject.addWhereCondition("id_plugin = ?");
  384.                 sqlQueryObject.addWhereCondition("nome = ?");
  385.                 sqlQueryObject.setANDLogicOperator(true);
  386.                 updateQuery = sqlQueryObject.createSQLDelete();
  387.                 updateStmt = con.prepareStatement(updateQuery);
  388.                 index = 1;
  389.                 updateStmt.setLong(index++, idParent);
  390.                 updateStmt.setString(index++, plugin.getNome());
  391.                 updateStmt.executeUpdate();
  392.                 updateStmt.close();
  393.                
  394.                 break;
  395.             }


  396.         } catch (SQLException se) {
  397.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDRegistroPluginArchivio] SQLException [" + se.getMessage() + "].",se);
  398.         }catch (Exception se) {
  399.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB_LIB::CRUDRegistroPluginArchivio] Exception [" + se.getMessage() + "].",se);
  400.         } finally {

  401.             JDBCUtilities.closeResources(selectRS, selectStmt);
  402.             JDBCUtilities.closeResources(updateStmt);
  403.         }

  404.     }
  405.    
  406. }