DriverControlStationDB_LIB.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.web.ctrlstat.driver;

  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.text.MessageFormat;

  30. import org.openspcoop2.core.constants.CostantiDB;
  31. import org.openspcoop2.core.registry.constants.StatoFunzionalita;
  32. import org.openspcoop2.core.registry.driver.db.DriverRegistroServiziDB_LIB;
  33. import org.openspcoop2.utils.certificate.CertificateUtils;
  34. import org.openspcoop2.utils.certificate.PrincipalType;
  35. import org.openspcoop2.utils.sql.ISQLQueryObject;
  36. import org.openspcoop2.utils.sql.SQLObjectFactory;
  37. import org.openspcoop2.web.ctrlstat.dao.PdDControlStation;
  38. import org.slf4j.Logger;

  39. /**
  40.  * DriverControlStationDB_LIB
  41.  *
  42.  * @author Andrea Poli (apoli@link.it)
  43.  * @author Stefano Corallo (corallo@link.it)
  44.  * @author Sandra Giangrandi (sandra@link.it)
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47.  *
  48.  */
  49. public class DriverControlStationDB_LIB {

  50.     private static Logger log = null;
  51.     public static void initialize(Logger logParam){
  52.         log = logParam;
  53.     }

  54.     // Tipo database ereditato da DriverControlStationDB
  55.     private static String tipoDB = null;

  56.     // Setto il tipoDB
  57.     public static void setTipoDB(String tipoDatabase) {
  58.         DriverControlStationDB_LIB.tipoDB = tipoDatabase;
  59.     }

  60.     public static void CRUDPdd(int type, PdDControlStation pdd, Connection con) throws DriverControlStationException {
  61.         if (pdd == null) {
  62.             throw new DriverControlStationException("[DriverControlStationDB_LIB::CRUDPdd] Parametro non valido.");
  63.         }

  64.         if ((type != CostantiDB.CREATE) && (pdd.getId() <= 0)) {
  65.             throw new DriverControlStationException("[DriverControlStationDB_LIB::CRUDPdd] ID Pdd non valido.");
  66.         }

  67.         String nome = pdd.getNome();
  68.         String descrizione = pdd.getDescrizione();

  69.         String ip = pdd.getIp();
  70.         int porta = pdd.getPorta();
  71.         String ipGestione = pdd.getIpGestione();
  72.         int portaGestione = pdd.getPortaGestione();
  73.         String protocollo = pdd.getProtocollo();
  74.         String protocolloGestione = pdd.getProtocolloGestione();

  75.         String tipo = pdd.getTipo();
  76.         String implementazione = pdd.getImplementazione();

  77.         String subject = pdd.getSubject();
  78.         String password = pdd.getPassword();
  79.         StatoFunzionalita client_auth = pdd.getClientAuth();

  80.         String superuser = pdd.getSuperUser();

  81.         PreparedStatement updateStmt = null;
  82.         String updateQuery = "";
  83.         PreparedStatement selectStmt = null;
  84.         String selectQuery = "";
  85.         ResultSet selectRS = null;
  86.         int n = 0;

  87.         try {

  88.             // preparo lo statement in base al tipo di operazione
  89.             switch (type) {
  90.                 case CREATE:
  91.                     // CREATE
  92.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverControlStationDB_LIB.tipoDB);
  93.                     sqlQueryObject.addInsertTable(CostantiDB.PDD);
  94.                     sqlQueryObject.addInsertField("nome", "?");
  95.                     sqlQueryObject.addInsertField("descrizione", "?");
  96.                     sqlQueryObject.addInsertField("ip", "?");
  97.                     sqlQueryObject.addInsertField("porta", "?");
  98.                     sqlQueryObject.addInsertField("ip_gestione", "?");
  99.                     sqlQueryObject.addInsertField("porta_gestione", "?");
  100.                     sqlQueryObject.addInsertField("protocollo", "?");
  101.                     sqlQueryObject.addInsertField("protocollo_gestione", "?");
  102.                     sqlQueryObject.addInsertField("tipo", "?");
  103.                     sqlQueryObject.addInsertField("implementazione", "?");
  104.                     sqlQueryObject.addInsertField("subject", "?");
  105.                     sqlQueryObject.addInsertField("password", "?");
  106.                     sqlQueryObject.addInsertField("client_auth", "?");
  107.                     sqlQueryObject.addInsertField("superuser", "?");
  108.                     if (pdd.getOraRegistrazione() != null)
  109.                         sqlQueryObject.addInsertField("ora_registrazione", "?");

  110.                     updateQuery = sqlQueryObject.createSQLInsert();
  111.                     updateStmt = con.prepareStatement(updateQuery);

  112.                     int index = 1;
  113.                     updateStmt.setString(index++, nome);
  114.                     updateStmt.setString(index++, descrizione);
  115.                     updateStmt.setString(index++, ip);
  116.                     updateStmt.setInt(index++, porta);
  117.                     updateStmt.setString(index++, ipGestione);
  118.                     updateStmt.setInt(index++, portaGestione);
  119.                     updateStmt.setString(index++, protocollo);
  120.                     updateStmt.setString(index++, protocolloGestione);
  121.                     updateStmt.setString(index++, tipo);
  122.                     updateStmt.setString(index++, implementazione);
  123.                     updateStmt.setString(index++, (subject != null ? CertificateUtils.formatPrincipal(subject, PrincipalType.SUBJECT) : null));
  124.                     updateStmt.setString(index++, password);
  125.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(client_auth));
  126.                     updateStmt.setString(index++, superuser);
  127.                     if (pdd.getOraRegistrazione() != null)
  128.                         updateStmt.setTimestamp(index++, new Timestamp(pdd.getOraRegistrazione().getTime()));

  129.                     // eseguo lo statement
  130.                     n = updateStmt.executeUpdate();

  131.                     updateStmt.close();

  132.                     DriverControlStationDB_LIB.log.debug("CRUDPdd type = " + type + " row affected =" + n);

  133.                     DriverControlStationDB_LIB.log.debug("CRUDPdd CREATE : \n" + DriverControlStationDB_LIB.formatSQLString(updateQuery, nome, descrizione, ip, porta, ipGestione, portaGestione, protocollo, protocolloGestione, tipo, implementazione, subject, password, client_auth));

  134.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverControlStationDB_LIB.tipoDB);
  135.                     sqlQueryObject.addFromTable(CostantiDB.PDD);
  136.                     sqlQueryObject.addSelectField("id");
  137.                     sqlQueryObject.addWhereCondition("nome = ?");
  138.                     selectQuery = sqlQueryObject.createSQLQuery();
  139.                     selectStmt = con.prepareStatement(selectQuery);
  140.                     selectStmt.setString(1, nome);

  141.                     selectRS = selectStmt.executeQuery();
  142.                     if (selectRS.next()) {
  143.                         pdd.setId(selectRS.getLong("id"));
  144.                     }
  145.                     selectRS.close();
  146.                     selectStmt.close();
  147.                     break;

  148.                 case UPDATE:
  149.                     // UPDATE

  150.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverControlStationDB_LIB.tipoDB);
  151.                     sqlQueryObject.addUpdateTable(CostantiDB.PDD);
  152.                     sqlQueryObject.addUpdateField("descrizione", "?");
  153.                     sqlQueryObject.addUpdateField("ip", "?");
  154.                     sqlQueryObject.addUpdateField("porta", "?");
  155.                     sqlQueryObject.addUpdateField("ip_gestione", "?");
  156.                     sqlQueryObject.addUpdateField("porta_gestione", "?");
  157.                     sqlQueryObject.addUpdateField("protocollo", "?");
  158.                     sqlQueryObject.addUpdateField("protocollo_gestione", "?");
  159.                     sqlQueryObject.addUpdateField("tipo", "?");
  160.                     sqlQueryObject.addUpdateField("implementazione", "?");
  161.                     sqlQueryObject.addUpdateField("subject", "?");
  162.                     sqlQueryObject.addUpdateField("password", "?");
  163.                     sqlQueryObject.addUpdateField("client_auth", "?");
  164.                     sqlQueryObject.addUpdateField("superuser", "?");
  165.                     sqlQueryObject.addUpdateField("nome", "?");
  166.                     if (pdd.getOraRegistrazione() != null)
  167.                         sqlQueryObject.addUpdateField("ora_registrazione", "?");
  168.                     sqlQueryObject.addWhereCondition("id=?");
  169.                     updateQuery = sqlQueryObject.createSQLUpdate();
  170.                     updateStmt = con.prepareStatement(updateQuery);

  171.                     index = 1;
  172.                     updateStmt.setString(index++, descrizione);
  173.                     updateStmt.setString(index++, ip);
  174.                     updateStmt.setInt(index++, porta);
  175.                     updateStmt.setString(index++, ipGestione);
  176.                     updateStmt.setInt(index++, portaGestione);
  177.                     updateStmt.setString(index++, protocollo);
  178.                     updateStmt.setString(index++, protocolloGestione);
  179.                     updateStmt.setString(index++, tipo);
  180.                     updateStmt.setString(index++, implementazione);
  181.                     updateStmt.setString(index++, (subject != null ? CertificateUtils.formatPrincipal(subject, PrincipalType.SUBJECT) : null));
  182.                     updateStmt.setString(index++, password);
  183.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(client_auth));
  184.                     updateStmt.setString(index++, superuser);
  185.                     updateStmt.setString(index++, nome);
  186.                     if (pdd.getOraRegistrazione() != null)
  187.                         updateStmt.setTimestamp(index++, new Timestamp(pdd.getOraRegistrazione().getTime()));

  188.                     updateStmt.setLong(index++, pdd.getId());

  189.                     // eseguo lo statement
  190.                     n = updateStmt.executeUpdate();
  191.                     updateStmt.close();
  192.                     DriverControlStationDB_LIB.log.debug("CRUDPdd type = " + type + " row affected =" + n);

  193.                     DriverControlStationDB_LIB.log.debug("CRUDPdd UPDATE : \n" + DriverControlStationDB_LIB.formatSQLString(updateQuery, descrizione, ip, porta, ipGestione, portaGestione, protocollo, protocolloGestione, tipo, implementazione, subject, password, client_auth, pdd.getId()));

  194.                     break;

  195.                 case DELETE:
  196.                     // DELETE

  197.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverControlStationDB_LIB.tipoDB);
  198.                     sqlQueryObject.addDeleteTable(CostantiDB.PDD);
  199.                     sqlQueryObject.addWhereCondition("id=?");
  200.                     updateQuery = sqlQueryObject.createSQLDelete();
  201.                     updateStmt = con.prepareStatement(updateQuery);

  202.                     updateStmt.setLong(1, pdd.getId());

  203.                     // eseguo lo statement
  204.                     n = updateStmt.executeUpdate();
  205.                     updateStmt.close();
  206.                     DriverControlStationDB_LIB.log.debug("CRUDPdd type = " + type + " row affected =" + n);

  207.                     DriverControlStationDB_LIB.log.debug("CRUDPdd DELETE : \n" + DriverControlStationDB_LIB.formatSQLString(updateQuery, pdd.getId()));

  208.                     break;
  209.             }

  210.         } catch (SQLException se) {
  211.             throw new DriverControlStationException("[DriverControlStationDB_LIB::CRUDPdd] SQLException [" + se.getMessage() + "].");
  212.         } catch (Exception se) {
  213.             throw new DriverControlStationException("[DriverControlStationDB_LIB::CRUDPdd] Exception [" + se.getMessage() + "].");
  214.         } finally {

  215.             try {
  216.                 if(updateStmt!=null) {
  217.                     updateStmt.close();
  218.                 }
  219.             } catch (Exception e) {
  220.                 // ignore exception
  221.             }
  222.             try {
  223.                 if(selectRS!=null) {
  224.                     selectRS.close();
  225.                 }
  226.             } catch (Exception e) {
  227.                 // ignore exception
  228.             }
  229.             try {
  230.                 if(selectStmt!=null) {
  231.                     selectStmt.close();
  232.                 }
  233.             } catch (Exception e) {
  234.                 // ignore exception
  235.             }
  236.         }
  237.     }

  238.     /**
  239.      * Utility per formattare la string sql con i parametri passati, e stamparla
  240.      * per debug
  241.      *
  242.      * @param sql
  243.      *            la string sql utilizzata nel prepared statement
  244.      * @param params
  245.      *            i parametri da inserire nella stringa che sostituiranno i '?'
  246.      * @return La stringa sql con al posto dei '?' ha i parametri passati
  247.      */
  248.     public static String formatSQLString(String sql, Object... params) {
  249.         String res = sql;

  250.         for (int i = 0; i < params.length; i++) {
  251.             res = res.replaceFirst("\\?", "{" + i + "}");
  252.         }

  253.         return MessageFormat.format(res, params);

  254.     }

  255. }