DriverRegistroServiziDB_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.core.registry.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.text.MessageFormat;
  30. import java.util.List;

  31. import org.openspcoop2.core.byok.IDriverBYOK;
  32. import org.openspcoop2.core.commons.CoreException;
  33. import org.openspcoop2.core.commons.DBUtils;
  34. import org.openspcoop2.core.constants.CostantiDB;
  35. import org.openspcoop2.core.id.IDGruppo;
  36. import org.openspcoop2.core.id.IDRuolo;
  37. import org.openspcoop2.core.id.IDScope;
  38. import org.openspcoop2.core.mapping.DBProtocolPropertiesUtils;
  39. import org.openspcoop2.core.registry.Gruppo;
  40. import org.openspcoop2.core.registry.ProtocolProperty;
  41. import org.openspcoop2.core.registry.Ruolo;
  42. import org.openspcoop2.core.registry.Scope;
  43. import org.openspcoop2.core.registry.constants.BindingStyle;
  44. import org.openspcoop2.core.registry.constants.BindingUse;
  45. import org.openspcoop2.core.registry.constants.CostantiRegistroServizi;
  46. import org.openspcoop2.core.registry.constants.CredenzialeTipo;
  47. import org.openspcoop2.core.registry.constants.FormatoSpecifica;
  48. import org.openspcoop2.core.registry.constants.HttpMethod;
  49. import org.openspcoop2.core.registry.constants.MessageType;
  50. import org.openspcoop2.core.registry.constants.ParameterType;
  51. import org.openspcoop2.core.registry.constants.ProfiloCollaborazione;
  52. import org.openspcoop2.core.registry.constants.RepresentationType;
  53. import org.openspcoop2.core.registry.constants.RepresentationXmlType;
  54. import org.openspcoop2.core.registry.constants.RuoloContesto;
  55. import org.openspcoop2.core.registry.constants.RuoloTipologia;
  56. import org.openspcoop2.core.registry.constants.ScopeContesto;
  57. import org.openspcoop2.core.registry.constants.ServiceBinding;
  58. import org.openspcoop2.core.registry.constants.StatoFunzionalita;
  59. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  60. import org.openspcoop2.core.registry.driver.DriverRegistroServiziNotFound;
  61. import org.openspcoop2.generic_project.exception.NotFoundException;
  62. import org.openspcoop2.utils.LoggerWrapperFactory;
  63. import org.openspcoop2.utils.date.DateManager;
  64. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  65. import org.openspcoop2.utils.sql.ISQLQueryObject;
  66. import org.openspcoop2.utils.sql.SQLObjectFactory;
  67. import org.slf4j.Logger;

  68. /**
  69.  * Classe utilizzata per effettuare query ad un registro dei servizi openspcoop
  70.  * formato db.
  71.  *
  72.  *
  73.  * @author Sandra Giangrandi (sandra@link.it)
  74.  * @author Stefano Corallo (corallo@link.it)
  75.  * @author $Author$
  76.  * @version $Rev$, $Date$
  77.  */
  78. public class DriverRegistroServiziDB_LIB {

  79.     /** Logger utilizzato per debug. */
  80.     public static org.slf4j.Logger log = LoggerWrapperFactory.getLogger(CostantiRegistroServizi.REGISTRO_DRIVER_DB_LOGGER);
  81.    
  82.     static void logDebug(String msg) {
  83.         if(log!=null) {
  84.             log.debug(msg);
  85.         }
  86.     }
  87.     static void logDebug(String msg, Exception e) {
  88.         if(log!=null) {
  89.             log.debug(msg,e);
  90.         }
  91.     }
  92.    
  93.     static void logError(String msg) {
  94.         if(log!=null) {
  95.             log.error(msg);
  96.         }
  97.     }
  98.     static void logError(String msg, Exception e) {
  99.         if(log!=null) {
  100.             log.error(msg,e);
  101.         }
  102.     }

  103.     // Tipo database ereditato da DriverRegistroServiziDB
  104.     static String tipoDB = null;

  105.     /** Log ereditato da DriverRegistroServiziDB
  106.      */
  107.     private static boolean initialize = false;
  108.     public static void initStaticLogger(Logger aLog){
  109.         if(!DriverRegistroServiziDB_LIB.initialize){
  110.             if(aLog!=null){
  111.                 DriverRegistroServiziDB_LIB.log = aLog;
  112.             }
  113.             DriverRegistroServiziDB_LIB.initialize = true;
  114.         }
  115.     }
  116.     public static boolean isStaticLoggerInitialized(){
  117.         return DriverRegistroServiziDB_LIB.initialize;
  118.     }

  119.     // Setto il tipoDB
  120.     public static void setTipoDB(String tipoDatabase) {
  121.         DriverRegistroServiziDB_LIB.tipoDB = tipoDatabase;
  122.     }
  123.    
  124.     public static String getValue(StatoFunzionalita funzionalita){
  125.         if(funzionalita==null){
  126.             return null;
  127.         }
  128.         else{
  129.             return funzionalita.getValue();
  130.         }
  131.     }
  132.     public static String getValue(ProfiloCollaborazione profilo){
  133.         if(profilo==null){
  134.             return null;
  135.         }
  136.         else{
  137.             return profilo.getValue();
  138.         }
  139.     }
  140.     public static String getValue(BindingStyle style){
  141.         if(style==null){
  142.             return null;
  143.         }
  144.         else{
  145.             return style.getValue();
  146.         }
  147.     }
  148.     public static String getValue(BindingUse use){
  149.         if(use==null){
  150.             return null;
  151.         }
  152.         else{
  153.             return use.getValue();
  154.         }
  155.     }
  156.     public static String getValue(CredenzialeTipo tipo){
  157.         if(tipo==null){
  158.             return null;
  159.         }
  160.         else{
  161.             return tipo.getValue();
  162.         }
  163.     }
  164.     public static String getValue(ServiceBinding tipo){
  165.         if(tipo==null){
  166.             return null;
  167.         }
  168.         else{
  169.             return tipo.getValue();
  170.         }
  171.     }
  172.     public static String getValue(MessageType tipo){
  173.         if(tipo==null){
  174.             return null;
  175.         }
  176.         else{
  177.             return tipo.getValue();
  178.         }
  179.     }
  180.     public static String getValue(HttpMethod tipo){
  181.         if(tipo==null){
  182.             return CostantiDB.API_RESOURCE_HTTP_METHOD_ALL_VALUE;
  183.         }
  184.         else{
  185.             return tipo.getValue();
  186.         }
  187.     }
  188.     public static String getValue(FormatoSpecifica tipo){
  189.         if(tipo==null){
  190.             return null;
  191.         }
  192.         else{
  193.             return tipo.getValue();
  194.         }
  195.     }
  196.     public static String getValue(RepresentationType tipo){
  197.         if(tipo==null){
  198.             return null;
  199.         }
  200.         else{
  201.             return tipo.getValue();
  202.         }
  203.     }
  204.     public static String getValue(RepresentationXmlType tipo){
  205.         if(tipo==null){
  206.             return null;
  207.         }
  208.         else{
  209.             return tipo.getValue();
  210.         }
  211.     }
  212.     public static String getValue(ParameterType tipo){
  213.         if(tipo==null){
  214.             return null;
  215.         }
  216.         else{
  217.             return tipo.getValue();
  218.         }
  219.     }
  220.    
  221.     public static StatoFunzionalita getEnumStatoFunzionalita(String value){
  222.         if(value==null){
  223.             return null;
  224.         }
  225.         else{
  226.             return StatoFunzionalita.toEnumConstant(value);
  227.         }
  228.     }
  229.     public static ProfiloCollaborazione getEnumProfiloCollaborazione(String value){
  230.         if(value==null){
  231.             return null;
  232.         }
  233.         else{
  234.             return ProfiloCollaborazione.toEnumConstant(value);
  235.         }
  236.     }
  237.     public static BindingStyle getEnumBindingStyle(String value){
  238.         if(value==null){
  239.             return null;
  240.         }
  241.         else{
  242.             return BindingStyle.toEnumConstant(value);
  243.         }
  244.     }
  245.     public static BindingUse getEnumBindingUse(String value){
  246.         if(value==null){
  247.             return null;
  248.         }
  249.         else{
  250.             return BindingUse.toEnumConstant(value);
  251.         }
  252.     }
  253.     public static CredenzialeTipo getEnumCredenzialeTipo(String value){
  254.         if(value==null){
  255.             return null;
  256.         }
  257.         else{
  258.             return CredenzialeTipo.toEnumConstant(value);
  259.         }
  260.     }
  261.     public static ServiceBinding getEnumServiceBinding(String value){
  262.         if(value==null){
  263.             return null;
  264.         }
  265.         else{
  266.             return ServiceBinding.toEnumConstant(value);
  267.         }
  268.     }
  269.     public static MessageType getEnumMessageType(String value){
  270.         if(value==null){
  271.             return null;
  272.         }
  273.         else{
  274.             return MessageType.toEnumConstant(value);
  275.         }
  276.     }
  277.     public static HttpMethod getEnumHttpMethod(String value){
  278.         if(value==null || CostantiDB.API_RESOURCE_HTTP_METHOD_ALL_VALUE.equalsIgnoreCase(value)){
  279.             return null;
  280.         }
  281.         else{
  282.             return HttpMethod.toEnumConstant(value);
  283.         }
  284.     }
  285.     public static FormatoSpecifica getEnumFormatoSpecifica(String value){
  286.         if(value==null){
  287.             return null;
  288.         }
  289.         else{
  290.             return FormatoSpecifica.toEnumConstant(value);
  291.         }
  292.     }
  293.     public static RepresentationType getEnumRepresentationType(String value){
  294.         if(value==null){
  295.             return null;
  296.         }
  297.         else{
  298.             return RepresentationType.toEnumConstant(value);
  299.         }
  300.     }
  301.     public static RepresentationXmlType getEnumRepresentationXmlType(String value){
  302.         if(value==null){
  303.             return null;
  304.         }
  305.         else{
  306.             return RepresentationXmlType.toEnumConstant(value);
  307.         }
  308.     }
  309.     public static ParameterType getEnumParameterType(String value){
  310.         if(value==null){
  311.             return null;
  312.         }
  313.         else{
  314.             return ParameterType.toEnumConstant(value);
  315.         }
  316.     }


  317.     /**
  318.      * Utility per formattare la string sql con i parametri passati, e stamparla
  319.      * per debug
  320.      *
  321.      * @param sql
  322.      *            la string sql utilizzata nel prepared statement
  323.      * @param params
  324.      *            i parametri da inserire nella stringa che sostituiranno i '?'
  325.      * @return La stringa sql con al posto dei '?' ha i parametri passati
  326.      */
  327.     public static String formatSQLString(String sql, Object... params) {
  328.         String res = sql;

  329.         for (int i = 0; i < params.length; i++) {
  330.             res = res.replaceFirst("\\?", "{" + i + "}");
  331.         }

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

  333.     }

  334.    
  335.    
  336.     public static void CRUDGruppo(int type, Gruppo gruppo, Connection con)
  337.             throws DriverRegistroServiziException {
  338.         if (gruppo == null) {
  339.             throw new DriverRegistroServiziException(
  340.             "[DriverRegistroServiziDB_LIB::CRUDGruppo] Parametro non valido.");
  341.         }

  342.         /**if ((type != CostantiDB.CREATE) && (pdd.getId() <= 0)) {
  343.             throw new DriverRegistroServiziException(
  344.             "[DriverRegistroServiziDB_LIB::CRUDGruppo] ID Gruppo non valido.");
  345.         }*/

  346.         String nome = gruppo.getNome();
  347.         String descrizione = gruppo.getDescrizione();
  348.         ServiceBinding serviceBinding = gruppo.getServiceBinding();

  349.         String superuser = gruppo.getSuperUser();

  350.         PreparedStatement updateStmt = null;
  351.         String updateQuery = "";
  352.         PreparedStatement selectStmt = null;
  353.         String selectQuery = "";
  354.         ResultSet selectRS = null;
  355.         int n = 0;

  356.         try {

  357.             // preparo lo statement in base al tipo di operazione
  358.             switch (type) {
  359.             case CREATE:
  360.                 // CREATE
  361.                
  362.                 String utenteRichiedente = null;
  363.                 if(gruppo.getProprietaOggetto()!=null && gruppo.getProprietaOggetto().getUtenteRichiedente()!=null) {
  364.                     utenteRichiedente = gruppo.getProprietaOggetto().getUtenteRichiedente();
  365.                 }
  366.                 else {
  367.                     utenteRichiedente = superuser;
  368.                 }
  369.                
  370.                 Timestamp dataCreazione = null;
  371.                 if(gruppo.getProprietaOggetto()!=null && gruppo.getProprietaOggetto().getDataCreazione()!=null) {
  372.                     dataCreazione = new Timestamp(gruppo.getProprietaOggetto().getDataCreazione().getTime());
  373.                 }
  374.                 else if(gruppo.getOraRegistrazione()!=null){
  375.                     dataCreazione = new Timestamp(gruppo.getOraRegistrazione().getTime());
  376.                 }
  377.                 else {
  378.                     dataCreazione = DateManager.getTimestamp();
  379.                 }
  380.                
  381.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  382.                 sqlQueryObject.addInsertTable(CostantiDB.GRUPPI);
  383.                 sqlQueryObject.addInsertField("nome", "?");
  384.                 sqlQueryObject.addInsertField("descrizione", "?");
  385.                 sqlQueryObject.addInsertField("service_binding", "?");
  386.                 sqlQueryObject.addInsertField("superuser", "?");
  387.                 if(gruppo.getOraRegistrazione()!=null)
  388.                     sqlQueryObject.addInsertField("ora_registrazione", "?");
  389.                 if(utenteRichiedente!=null) {
  390.                     sqlQueryObject.addInsertField(CostantiDB.PROPRIETA_OGGETTO_UTENTE_RICHIEDENTE, "?");
  391.                 }
  392.                 if(dataCreazione!=null) {
  393.                     sqlQueryObject.addInsertField(CostantiDB.PROPRIETA_OGGETTO_DATA_CREAZIONE, "?");
  394.                 }

  395.                 updateQuery = sqlQueryObject.createSQLInsert();
  396.                 updateStmt = con.prepareStatement(updateQuery);

  397.                 int index = 1;
  398.                 updateStmt.setString(index++, nome);
  399.                 updateStmt.setString(index++, descrizione);
  400.                 updateStmt.setString(index++, serviceBinding!=null? serviceBinding.getValue() : null);
  401.                 updateStmt.setString(index++, superuser);
  402.                
  403.                 if(gruppo.getOraRegistrazione()!=null)
  404.                     updateStmt.setTimestamp(index++, new Timestamp(gruppo.getOraRegistrazione().getTime()));
  405.                
  406.                 if(utenteRichiedente!=null) {
  407.                     updateStmt.setString(index++, utenteRichiedente);
  408.                 }
  409.                
  410.                 if(dataCreazione!=null) {
  411.                     updateStmt.setTimestamp(index++, dataCreazione);
  412.                 }

  413.                 // eseguo lo statement
  414.                 n = updateStmt.executeUpdate();

  415.                 updateStmt.close();

  416.                 DriverRegistroServiziDB_LIB.logDebug("CRUDGruppo type = " + type
  417.                         + " row affected =" + n);

  418.                 DriverRegistroServiziDB_LIB.logDebug("CRUDGruppo CREATE : \n"
  419.                         + DriverRegistroServiziDB_LIB.formatSQLString(
  420.                                 updateQuery, nome, descrizione,(serviceBinding!=null? serviceBinding.getValue() : null),
  421.                                 superuser));

  422.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  423.                 sqlQueryObject.addFromTable(CostantiDB.GRUPPI);
  424.                 sqlQueryObject.addSelectField("id");
  425.                 sqlQueryObject.addWhereCondition("nome = ?");
  426.                 selectQuery = sqlQueryObject.createSQLQuery();
  427.                 selectStmt = con.prepareStatement(selectQuery);
  428.                 selectStmt.setString(1, nome);

  429.                 selectRS = selectStmt.executeQuery();
  430.                 if (selectRS.next()) {
  431.                     gruppo.setId(selectRS.getLong("id"));
  432.                 }
  433.                 selectRS.close();
  434.                 selectStmt.close();
  435.                 break;

  436.             case UPDATE:
  437.                 // UPDATE1

  438.                 String nomeGruppo = gruppo.getOldIDGruppoForUpdate()!=null ? gruppo.getOldIDGruppoForUpdate().getNome() : null;
  439.                 if(nomeGruppo==null || "".equals(nomeGruppo))
  440.                     nomeGruppo = gruppo.getNome();
  441.                
  442.                 IDGruppo idG = new IDGruppo(nomeGruppo);
  443.                 long idGruppo = DBUtils.getIdGruppo(idG, con, DriverRegistroServiziDB_LIB.tipoDB);
  444.                 if (idGruppo <= 0)
  445.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDGruppo(UPDATE)] Id Gruppo non valido.");
  446.                
  447.                 String utenteUltimaModifica = null;
  448.                 if(gruppo.getProprietaOggetto()!=null && gruppo.getProprietaOggetto().getUtenteUltimaModifica()!=null) {
  449.                     utenteUltimaModifica = gruppo.getProprietaOggetto().getUtenteUltimaModifica();
  450.                 }
  451.                 else {
  452.                     utenteUltimaModifica = superuser;
  453.                 }
  454.                
  455.                 Timestamp dataUltimaModifica = null;
  456.                 if(gruppo.getProprietaOggetto()!=null && gruppo.getProprietaOggetto().getDataUltimaModifica()!=null) {
  457.                     dataUltimaModifica = new Timestamp(gruppo.getProprietaOggetto().getDataUltimaModifica().getTime());
  458.                 }
  459.                 else {
  460.                     dataUltimaModifica = DateManager.getTimestamp();
  461.                 }
  462.                
  463.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  464.                 sqlQueryObject.addUpdateTable(CostantiDB.GRUPPI);
  465.                 sqlQueryObject.addUpdateField("nome", "?");
  466.                 sqlQueryObject.addUpdateField("descrizione", "?");
  467.                 sqlQueryObject.addUpdateField("service_binding", "?");
  468.                 sqlQueryObject.addUpdateField("superuser", "?");
  469.                 if(gruppo.getOraRegistrazione()!=null)
  470.                     sqlQueryObject.addUpdateField("ora_registrazione", "?");
  471.                 if(utenteUltimaModifica!=null) {
  472.                     sqlQueryObject.addUpdateField(CostantiDB.PROPRIETA_OGGETTO_UTENTE_ULTIMA_MODIFICA, "?");
  473.                 }
  474.                 if(dataUltimaModifica!=null) {
  475.                     sqlQueryObject.addUpdateField(CostantiDB.PROPRIETA_OGGETTO_DATA_ULTIMA_MODIFICA, "?");
  476.                 }
  477.                
  478.                 sqlQueryObject.addWhereCondition("id=?");
  479.                 updateQuery = sqlQueryObject.createSQLUpdate();
  480.                 updateStmt = con.prepareStatement(updateQuery);

  481.                 index = 1;
  482.                 updateStmt.setString(index++, nome);
  483.                 updateStmt.setString(index++, descrizione);
  484.                 updateStmt.setString(index++, serviceBinding!=null? serviceBinding.getValue() : null);
  485.                 updateStmt.setString(index++, superuser);
  486.                 if(gruppo.getOraRegistrazione()!=null)
  487.                     updateStmt.setTimestamp(index++, new Timestamp(gruppo.getOraRegistrazione().getTime()));
  488.                 if(utenteUltimaModifica!=null) {
  489.                     updateStmt.setString(index++, utenteUltimaModifica);
  490.                 }
  491.                 if(dataUltimaModifica!=null) {
  492.                     updateStmt.setTimestamp(index++, dataUltimaModifica);
  493.                 }
  494.    
  495.                 updateStmt.setLong(index++, idGruppo);

  496.                 // eseguo lo statement
  497.                 n = updateStmt.executeUpdate();
  498.                 updateStmt.close();
  499.                 DriverRegistroServiziDB_LIB.logDebug("CRUDGruppo type = " + type
  500.                         + " row affected =" + n);

  501.                 DriverRegistroServiziDB_LIB.logDebug("CRUDGruppo UPDATE : \n"
  502.                         + DriverRegistroServiziDB_LIB.formatSQLString(
  503.                                 updateQuery, nome, descrizione,(serviceBinding!=null? serviceBinding.getValue() : null),
  504.                                 superuser,idGruppo));

  505.                 break;

  506.             case DELETE:
  507.                 // DELETE

  508.                 idG = new IDGruppo(nome);
  509.                 idGruppo = DBUtils.getIdGruppo(idG, con, DriverRegistroServiziDB_LIB.tipoDB);
  510.                 if (idGruppo <= 0)
  511.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDGruppo(DELETE)] Id Gruppo non valido.");
  512.                
  513.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  514.                 sqlQueryObject.addDeleteTable(CostantiDB.GRUPPI);
  515.                 sqlQueryObject.addWhereCondition("id=?");
  516.                 updateQuery = sqlQueryObject.createSQLDelete();
  517.                 updateStmt = con.prepareStatement(updateQuery);

  518.                 updateStmt.setLong(1, idGruppo);

  519.                 // eseguo lo statement
  520.                 n = updateStmt.executeUpdate();
  521.                 updateStmt.close();
  522.                 DriverRegistroServiziDB_LIB.logDebug("CRUDGruppo type = " + type
  523.                         + " row affected =" + n);

  524.                 DriverRegistroServiziDB_LIB.logDebug("CRUDGruppo DELETE : \n"
  525.                         + DriverRegistroServiziDB_LIB.formatSQLString(
  526.                                 updateQuery, idGruppo));

  527.                 break;
  528.             }

  529.         } catch (SQLException se) {
  530.             throw new DriverRegistroServiziException(
  531.                     "[DriverControlStationDB_LIB::CRUDGruppo] SQLException ["
  532.                     + se.getMessage() + "].",se);
  533.         } catch (Exception se) {
  534.             throw new DriverRegistroServiziException(
  535.                     "[DriverControlStationDB_LIB::CRUDGruppo] Exception ["
  536.                     + se.getMessage() + "].",se);
  537.         } finally {

  538.             JDBCUtilities.closeResources(updateStmt);
  539.             JDBCUtilities.closeResources(selectRS, selectStmt);

  540.         }
  541.     }
  542.    
  543.     public static void CRUDRuolo(int type, Ruolo ruolo, Connection con)
  544.             throws DriverRegistroServiziException {
  545.         if (ruolo == null) {
  546.             throw new DriverRegistroServiziException(
  547.             "[DriverRegistroServiziDB_LIB::CRUDRuolo] Parametro non valido.");
  548.         }

  549.         /**if ((type != CostantiDB.CREATE) && (pdd.getId() <= 0)) {
  550.             throw new DriverRegistroServiziException(
  551.             "[DriverRegistroServiziDB_LIB::CRUDRuolo] ID Ruolo non valido.");
  552.         }*/

  553.         String nome = ruolo.getNome();
  554.         String descrizione = ruolo.getDescrizione();
  555.         RuoloTipologia ruoloTipologia = ruolo.getTipologia();
  556.         String nomeEsterno = ruolo.getNomeEsterno();
  557.         RuoloContesto ruoloContesto = ruolo.getContestoUtilizzo();

  558.         String superuser = ruolo.getSuperUser();

  559.         PreparedStatement updateStmt = null;
  560.         String updateQuery = "";
  561.         PreparedStatement selectStmt = null;
  562.         String selectQuery = "";
  563.         ResultSet selectRS = null;
  564.         int n = 0;

  565.         try {

  566.             // preparo lo statement in base al tipo di operazione
  567.             switch (type) {
  568.             case CREATE:
  569.                
  570.                 String utenteRichiedente = null;
  571.                 if(ruolo.getProprietaOggetto()!=null && ruolo.getProprietaOggetto().getUtenteRichiedente()!=null) {
  572.                     utenteRichiedente = ruolo.getProprietaOggetto().getUtenteRichiedente();
  573.                 }
  574.                 else {
  575.                     utenteRichiedente = superuser;
  576.                 }
  577.                
  578.                 Timestamp dataCreazione = null;
  579.                 if(ruolo.getProprietaOggetto()!=null && ruolo.getProprietaOggetto().getDataCreazione()!=null) {
  580.                     dataCreazione = new Timestamp(ruolo.getProprietaOggetto().getDataCreazione().getTime());
  581.                 }
  582.                 else if(ruolo.getOraRegistrazione()!=null){
  583.                     dataCreazione = new Timestamp(ruolo.getOraRegistrazione().getTime());
  584.                 }
  585.                 else {
  586.                     dataCreazione = DateManager.getTimestamp();
  587.                 }
  588.                
  589.                 // CREATE
  590.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  591.                 sqlQueryObject.addInsertTable(CostantiDB.RUOLI);
  592.                 sqlQueryObject.addInsertField("nome", "?");
  593.                 sqlQueryObject.addInsertField("descrizione", "?");
  594.                 sqlQueryObject.addInsertField("tipologia", "?");
  595.                 sqlQueryObject.addInsertField("nome_esterno", "?");
  596.                 sqlQueryObject.addInsertField("contesto_utilizzo", "?");
  597.                 sqlQueryObject.addInsertField("superuser", "?");
  598.                 if(ruolo.getOraRegistrazione()!=null)
  599.                     sqlQueryObject.addInsertField("ora_registrazione", "?");
  600.                 if(utenteRichiedente!=null) {
  601.                     sqlQueryObject.addInsertField(CostantiDB.PROPRIETA_OGGETTO_UTENTE_RICHIEDENTE, "?");
  602.                 }
  603.                 if(dataCreazione!=null) {
  604.                     sqlQueryObject.addInsertField(CostantiDB.PROPRIETA_OGGETTO_DATA_CREAZIONE, "?");
  605.                 }

  606.                 updateQuery = sqlQueryObject.createSQLInsert();
  607.                 updateStmt = con.prepareStatement(updateQuery);

  608.                 int index = 1;
  609.                 updateStmt.setString(index++, nome);
  610.                 updateStmt.setString(index++, descrizione);
  611.                 updateStmt.setString(index++, ruoloTipologia!=null? ruoloTipologia.getValue() : null);
  612.                 updateStmt.setString(index++, nomeEsterno);
  613.                 updateStmt.setString(index++, ruoloContesto!=null? ruoloContesto.getValue() : null);
  614.                 updateStmt.setString(index++, superuser);
  615.                
  616.                 if(ruolo.getOraRegistrazione()!=null)
  617.                     updateStmt.setTimestamp(index++, new Timestamp(ruolo.getOraRegistrazione().getTime()));
  618.                
  619.                 if(utenteRichiedente!=null) {
  620.                     updateStmt.setString(index++, utenteRichiedente);
  621.                 }
  622.                
  623.                 if(dataCreazione!=null) {
  624.                     updateStmt.setTimestamp(index++, dataCreazione);
  625.                 }

  626.                 // eseguo lo statement
  627.                 n = updateStmt.executeUpdate();

  628.                 updateStmt.close();

  629.                 DriverRegistroServiziDB_LIB.logDebug("CRUDRuolo type = " + type
  630.                         + " row affected =" + n);

  631.                 DriverRegistroServiziDB_LIB.logDebug("CRUDRuolo CREATE : \n"
  632.                         + DriverRegistroServiziDB_LIB.formatSQLString(
  633.                                 updateQuery, nome, descrizione,(ruoloTipologia!=null? ruoloTipologia.getValue() : null),
  634.                                 (ruoloContesto!=null? ruoloContesto.getValue() : null),superuser));

  635.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  636.                 sqlQueryObject.addFromTable(CostantiDB.RUOLI);
  637.                 sqlQueryObject.addSelectField("id");
  638.                 sqlQueryObject.addWhereCondition("nome = ?");
  639.                 selectQuery = sqlQueryObject.createSQLQuery();
  640.                 selectStmt = con.prepareStatement(selectQuery);
  641.                 selectStmt.setString(1, nome);

  642.                 selectRS = selectStmt.executeQuery();
  643.                 if (selectRS.next()) {
  644.                     ruolo.setId(selectRS.getLong("id"));
  645.                 }
  646.                 selectRS.close();
  647.                 selectStmt.close();
  648.                 break;

  649.             case UPDATE:
  650.                 // UPDATE1

  651.                 String nomeRuolo = ruolo.getOldIDRuoloForUpdate()!=null ? ruolo.getOldIDRuoloForUpdate().getNome() : null;
  652.                 if(nomeRuolo==null || "".equals(nomeRuolo))
  653.                     nomeRuolo = ruolo.getNome();
  654.                
  655.                 IDRuolo idR = new IDRuolo(nomeRuolo);
  656.                 long idRuolo = DBUtils.getIdRuolo(idR, con, DriverRegistroServiziDB_LIB.tipoDB);
  657.                 if (idRuolo <= 0)
  658.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDRuolo(UPDATE)] Id Ruolo non valido.");
  659.                
  660.                 String utenteUltimaModifica = null;
  661.                 if(ruolo.getProprietaOggetto()!=null && ruolo.getProprietaOggetto().getUtenteUltimaModifica()!=null) {
  662.                     utenteUltimaModifica = ruolo.getProprietaOggetto().getUtenteUltimaModifica();
  663.                 }
  664.                 else {
  665.                     utenteUltimaModifica = superuser;
  666.                 }
  667.                
  668.                 Timestamp dataUltimaModifica = null;
  669.                 if(ruolo.getProprietaOggetto()!=null && ruolo.getProprietaOggetto().getDataUltimaModifica()!=null) {
  670.                     dataUltimaModifica = new Timestamp(ruolo.getProprietaOggetto().getDataUltimaModifica().getTime());
  671.                 }
  672.                 else {
  673.                     dataUltimaModifica = DateManager.getTimestamp();
  674.                 }
  675.                
  676.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  677.                 sqlQueryObject.addUpdateTable(CostantiDB.RUOLI);
  678.                 sqlQueryObject.addUpdateField("nome", "?");
  679.                 sqlQueryObject.addUpdateField("descrizione", "?");
  680.                 sqlQueryObject.addUpdateField("tipologia", "?");
  681.                 sqlQueryObject.addUpdateField("nome_esterno", "?");
  682.                 sqlQueryObject.addUpdateField("contesto_utilizzo", "?");
  683.                 sqlQueryObject.addUpdateField("superuser", "?");
  684.                 if(ruolo.getOraRegistrazione()!=null)
  685.                     sqlQueryObject.addUpdateField("ora_registrazione", "?");
  686.                 if(utenteUltimaModifica!=null) {
  687.                     sqlQueryObject.addUpdateField(CostantiDB.PROPRIETA_OGGETTO_UTENTE_ULTIMA_MODIFICA, "?");
  688.                 }
  689.                 if(dataUltimaModifica!=null) {
  690.                     sqlQueryObject.addUpdateField(CostantiDB.PROPRIETA_OGGETTO_DATA_ULTIMA_MODIFICA, "?");
  691.                 }
  692.                
  693.                 sqlQueryObject.addWhereCondition("id=?");
  694.                 updateQuery = sqlQueryObject.createSQLUpdate();
  695.                 updateStmt = con.prepareStatement(updateQuery);

  696.                 index = 1;
  697.                 updateStmt.setString(index++, nome);
  698.                 updateStmt.setString(index++, descrizione);
  699.                 updateStmt.setString(index++, ruoloTipologia!=null? ruoloTipologia.getValue() : null);
  700.                 updateStmt.setString(index++, nomeEsterno);
  701.                 updateStmt.setString(index++, ruoloContesto!=null? ruoloContesto.getValue() : null);
  702.                 updateStmt.setString(index++, superuser);
  703.                 if(ruolo.getOraRegistrazione()!=null)
  704.                     updateStmt.setTimestamp(index++, new Timestamp(ruolo.getOraRegistrazione().getTime()));
  705.                 if(utenteUltimaModifica!=null) {
  706.                     updateStmt.setString(index++, utenteUltimaModifica);
  707.                 }
  708.                 if(dataUltimaModifica!=null) {
  709.                     updateStmt.setTimestamp(index++, dataUltimaModifica);
  710.                 }
  711.    
  712.                 updateStmt.setLong(index++, idRuolo);

  713.                 // eseguo lo statement
  714.                 n = updateStmt.executeUpdate();
  715.                 updateStmt.close();
  716.                 DriverRegistroServiziDB_LIB.logDebug("CRUDRuolo type = " + type
  717.                         + " row affected =" + n);

  718.                 DriverRegistroServiziDB_LIB.logDebug("CRUDRuolo UPDATE : \n"
  719.                         + DriverRegistroServiziDB_LIB.formatSQLString(
  720.                                 updateQuery, nome, descrizione,(ruoloTipologia!=null? ruoloTipologia.getValue() : null),
  721.                                 (ruoloContesto!=null? ruoloContesto.getValue() : null),superuser,idRuolo));

  722.                 break;

  723.             case DELETE:
  724.                 // DELETE

  725.                 idR = new IDRuolo(nome);
  726.                 idRuolo = DBUtils.getIdRuolo(idR, con, DriverRegistroServiziDB_LIB.tipoDB);
  727.                 if (idRuolo <= 0)
  728.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDRuolo(DELETE)] Id Ruolo non valido.");
  729.                
  730.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  731.                 sqlQueryObject.addDeleteTable(CostantiDB.RUOLI);
  732.                 sqlQueryObject.addWhereCondition("id=?");
  733.                 updateQuery = sqlQueryObject.createSQLDelete();
  734.                 updateStmt = con.prepareStatement(updateQuery);

  735.                 updateStmt.setLong(1, idRuolo);

  736.                 // eseguo lo statement
  737.                 n = updateStmt.executeUpdate();
  738.                 updateStmt.close();
  739.                 DriverRegistroServiziDB_LIB.logDebug("CRUDRuolo type = " + type
  740.                         + " row affected =" + n);

  741.                 DriverRegistroServiziDB_LIB.logDebug("CRUDRuolo DELETE : \n"
  742.                         + DriverRegistroServiziDB_LIB.formatSQLString(
  743.                                 updateQuery, idRuolo));

  744.                 break;
  745.             }

  746.         } catch (SQLException se) {
  747.             throw new DriverRegistroServiziException(
  748.                     "[DriverControlStationDB_LIB::CRUDRuolo] SQLException ["
  749.                     + se.getMessage() + "].",se);
  750.         } catch (Exception se) {
  751.             throw new DriverRegistroServiziException(
  752.                     "[DriverControlStationDB_LIB::CRUDRuolo] Exception ["
  753.                     + se.getMessage() + "].",se);
  754.         } finally {

  755.             JDBCUtilities.closeResources(updateStmt);
  756.             JDBCUtilities.closeResources(selectRS, selectStmt);
  757.         }
  758.     }
  759.    
  760.     public static void CRUDScope(int type, Scope scope, Connection con)
  761.             throws DriverRegistroServiziException {
  762.         if (scope == null) {
  763.             throw new DriverRegistroServiziException(
  764.             "[DriverRegistroServiziDB_LIB::CRUDScope] Parametro non valido.");
  765.         }

  766.         /**if ((type != CostantiDB.CREATE) && (pdd.getId() <= 0)) {
  767.             throw new DriverRegistroServiziException(
  768.             "[DriverRegistroServiziDB_LIB::CRUDScope] ID Scope non valido.");
  769.         }*/

  770.         String nome = scope.getNome();
  771.         String descrizione = scope.getDescrizione();
  772.         String scopeTipologia = scope.getTipologia();
  773.         String nomeEsterno = scope.getNomeEsterno();
  774.         ScopeContesto scopeContesto = scope.getContestoUtilizzo();

  775.         String superuser = scope.getSuperUser();

  776.         PreparedStatement updateStmt = null;
  777.         String updateQuery = "";
  778.         PreparedStatement selectStmt = null;
  779.         String selectQuery = "";
  780.         ResultSet selectRS = null;
  781.         int n = 0;

  782.         try {

  783.             // preparo lo statement in base al tipo di operazione
  784.             switch (type) {
  785.             case CREATE:
  786.                
  787.                 String utenteRichiedente = null;
  788.                 if(scope.getProprietaOggetto()!=null && scope.getProprietaOggetto().getUtenteRichiedente()!=null) {
  789.                     utenteRichiedente = scope.getProprietaOggetto().getUtenteRichiedente();
  790.                 }
  791.                 else {
  792.                     utenteRichiedente = superuser;
  793.                 }
  794.                
  795.                 Timestamp dataCreazione = null;
  796.                 if(scope.getProprietaOggetto()!=null && scope.getProprietaOggetto().getDataCreazione()!=null) {
  797.                     dataCreazione = new Timestamp(scope.getProprietaOggetto().getDataCreazione().getTime());
  798.                 }
  799.                 else if(scope.getOraRegistrazione()!=null){
  800.                     dataCreazione = new Timestamp(scope.getOraRegistrazione().getTime());
  801.                 }
  802.                 else {
  803.                     dataCreazione = DateManager.getTimestamp();
  804.                 }              
  805.                
  806.                 // CREATE
  807.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  808.                 sqlQueryObject.addInsertTable(CostantiDB.SCOPE);
  809.                 sqlQueryObject.addInsertField("nome", "?");
  810.                 sqlQueryObject.addInsertField("descrizione", "?");
  811.                 sqlQueryObject.addInsertField("tipologia", "?");
  812.                 sqlQueryObject.addInsertField("nome_esterno", "?");
  813.                 sqlQueryObject.addInsertField("contesto_utilizzo", "?");
  814.                 sqlQueryObject.addInsertField("superuser", "?");
  815.                 if(scope.getOraRegistrazione()!=null)
  816.                     sqlQueryObject.addInsertField("ora_registrazione", "?");
  817.                 if(utenteRichiedente!=null) {
  818.                     sqlQueryObject.addInsertField(CostantiDB.PROPRIETA_OGGETTO_UTENTE_RICHIEDENTE, "?");
  819.                 }
  820.                 if(dataCreazione!=null) {
  821.                     sqlQueryObject.addInsertField(CostantiDB.PROPRIETA_OGGETTO_DATA_CREAZIONE, "?");
  822.                 }

  823.                 updateQuery = sqlQueryObject.createSQLInsert();
  824.                 updateStmt = con.prepareStatement(updateQuery);

  825.                 int index = 1;
  826.                 updateStmt.setString(index++, nome);
  827.                 updateStmt.setString(index++, descrizione);
  828.                 updateStmt.setString(index++, scopeTipologia);
  829.                 updateStmt.setString(index++, nomeEsterno);
  830.                 updateStmt.setString(index++, scopeContesto!=null? scopeContesto.getValue() : null);
  831.                 updateStmt.setString(index++, superuser);
  832.                
  833.                 if(scope.getOraRegistrazione()!=null)
  834.                     updateStmt.setTimestamp(index++, new Timestamp(scope.getOraRegistrazione().getTime()));
  835.                
  836.                 if(utenteRichiedente!=null) {
  837.                     updateStmt.setString(index++, utenteRichiedente);
  838.                 }
  839.                
  840.                 if(dataCreazione!=null) {
  841.                     updateStmt.setTimestamp(index++, dataCreazione);
  842.                 }

  843.                 // eseguo lo statement
  844.                 n = updateStmt.executeUpdate();

  845.                 updateStmt.close();

  846.                 DriverRegistroServiziDB_LIB.logDebug("CRUDScope type = " + type
  847.                         + " row affected =" + n);

  848.                 DriverRegistroServiziDB_LIB.logDebug("CRUDScope CREATE : \n"
  849.                         + DriverRegistroServiziDB_LIB.formatSQLString(
  850.                                 updateQuery, nome, descrizione,scopeTipologia,
  851.                                 (scopeContesto!=null? scopeContesto.getValue() : null),superuser));

  852.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  853.                 sqlQueryObject.addFromTable(CostantiDB.SCOPE);
  854.                 sqlQueryObject.addSelectField("id");
  855.                 sqlQueryObject.addWhereCondition("nome = ?");
  856.                 selectQuery = sqlQueryObject.createSQLQuery();
  857.                 selectStmt = con.prepareStatement(selectQuery);
  858.                 selectStmt.setString(1, nome);

  859.                 selectRS = selectStmt.executeQuery();
  860.                 if (selectRS.next()) {
  861.                     scope.setId(selectRS.getLong("id"));
  862.                 }
  863.                 selectRS.close();
  864.                 selectStmt.close();
  865.                 break;

  866.             case UPDATE:
  867.                 // UPDATE1

  868.                 String nomeScope = scope.getOldIDScopeForUpdate()!=null ? scope.getOldIDScopeForUpdate().getNome() : null;
  869.                 if(nomeScope==null || "".equals(nomeScope))
  870.                     nomeScope = scope.getNome();
  871.                
  872.                 IDScope idS = new IDScope(nomeScope);
  873.                 long idScope = DBUtils.getIdScope(idS, con, DriverRegistroServiziDB_LIB.tipoDB);
  874.                 if (idScope <= 0)
  875.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDScope(UPDATE)] Id Scope non valido.");
  876.                
  877.                 String utenteUltimaModifica = null;
  878.                 if(scope.getProprietaOggetto()!=null && scope.getProprietaOggetto().getUtenteUltimaModifica()!=null) {
  879.                     utenteUltimaModifica = scope.getProprietaOggetto().getUtenteUltimaModifica();
  880.                 }
  881.                 else {
  882.                     utenteUltimaModifica = superuser;
  883.                 }
  884.                
  885.                 Timestamp dataUltimaModifica = null;
  886.                 if(scope.getProprietaOggetto()!=null && scope.getProprietaOggetto().getDataUltimaModifica()!=null) {
  887.                     dataUltimaModifica = new Timestamp(scope.getProprietaOggetto().getDataUltimaModifica().getTime());
  888.                 }
  889.                 else {
  890.                     dataUltimaModifica = DateManager.getTimestamp();
  891.                 }
  892.                
  893.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  894.                 sqlQueryObject.addUpdateTable(CostantiDB.SCOPE);
  895.                 sqlQueryObject.addUpdateField("nome", "?");
  896.                 sqlQueryObject.addUpdateField("descrizione", "?");
  897.                 sqlQueryObject.addUpdateField("tipologia", "?");
  898.                 sqlQueryObject.addUpdateField("nome_esterno", "?");
  899.                 sqlQueryObject.addUpdateField("contesto_utilizzo", "?");
  900.                 sqlQueryObject.addUpdateField("superuser", "?");
  901.                 if(scope.getOraRegistrazione()!=null)
  902.                     sqlQueryObject.addUpdateField("ora_registrazione", "?");
  903.                 if(utenteUltimaModifica!=null) {
  904.                     sqlQueryObject.addUpdateField(CostantiDB.PROPRIETA_OGGETTO_UTENTE_ULTIMA_MODIFICA, "?");
  905.                 }
  906.                 if(dataUltimaModifica!=null) {
  907.                     sqlQueryObject.addUpdateField(CostantiDB.PROPRIETA_OGGETTO_DATA_ULTIMA_MODIFICA, "?");
  908.                 }
  909.                
  910.                 sqlQueryObject.addWhereCondition("id=?");
  911.                 updateQuery = sqlQueryObject.createSQLUpdate();
  912.                 updateStmt = con.prepareStatement(updateQuery);

  913.                 index = 1;
  914.                 updateStmt.setString(index++, nome);
  915.                 updateStmt.setString(index++, descrizione);
  916.                 updateStmt.setString(index++, scopeTipologia);
  917.                 updateStmt.setString(index++, nomeEsterno);
  918.                 updateStmt.setString(index++, scopeContesto!=null? scopeContesto.getValue() : null);
  919.                 updateStmt.setString(index++, superuser);
  920.                 if(scope.getOraRegistrazione()!=null)
  921.                     updateStmt.setTimestamp(index++, new Timestamp(scope.getOraRegistrazione().getTime()));
  922.                 if(utenteUltimaModifica!=null) {
  923.                     updateStmt.setString(index++, utenteUltimaModifica);
  924.                 }
  925.                 if(dataUltimaModifica!=null) {
  926.                     updateStmt.setTimestamp(index++, dataUltimaModifica);
  927.                 }
  928.                
  929.                 updateStmt.setLong(index++, idScope);

  930.                 // eseguo lo statement
  931.                 n = updateStmt.executeUpdate();
  932.                 updateStmt.close();
  933.                 DriverRegistroServiziDB_LIB.logDebug("CRUDScope type = " + type
  934.                         + " row affected =" + n);

  935.                 DriverRegistroServiziDB_LIB.logDebug("CRUDScope UPDATE : \n"
  936.                         + DriverRegistroServiziDB_LIB.formatSQLString(
  937.                                 updateQuery, nome, descrizione,scopeTipologia,
  938.                                 (scopeContesto!=null? scopeContesto.getValue() : null),superuser,idScope));

  939.                 break;

  940.             case DELETE:
  941.                 // DELETE

  942.                 idS = new IDScope(nome);
  943.                 idScope = DBUtils.getIdScope(idS, con, DriverRegistroServiziDB_LIB.tipoDB);
  944.                 if (idScope <= 0)
  945.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDScope(DELETE)] Id Scope non valido.");
  946.                
  947.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  948.                 sqlQueryObject.addDeleteTable(CostantiDB.SCOPE);
  949.                 sqlQueryObject.addWhereCondition("id=?");
  950.                 updateQuery = sqlQueryObject.createSQLDelete();
  951.                 updateStmt = con.prepareStatement(updateQuery);

  952.                 updateStmt.setLong(1, idScope);

  953.                 // eseguo lo statement
  954.                 n = updateStmt.executeUpdate();
  955.                 updateStmt.close();
  956.                 DriverRegistroServiziDB_LIB.logDebug("CRUDScope type = " + type
  957.                         + " row affected =" + n);

  958.                 DriverRegistroServiziDB_LIB.logDebug("CRUDScope DELETE : \n"
  959.                         + DriverRegistroServiziDB_LIB.formatSQLString(
  960.                                 updateQuery, idScope));

  961.                 break;
  962.             }

  963.         } catch (SQLException se) {
  964.             throw new DriverRegistroServiziException(
  965.                     "[DriverControlStationDB_LIB::CRUDScope] SQLException ["
  966.                     + se.getMessage() + "].",se);
  967.         } catch (Exception se) {
  968.             throw new DriverRegistroServiziException(
  969.                     "[DriverControlStationDB_LIB::CRUDScope] Exception ["
  970.                     + se.getMessage() + "].",se);
  971.         } finally {

  972.             JDBCUtilities.closeResources(updateStmt);
  973.             JDBCUtilities.closeResources(selectRS, selectStmt);
  974.         }
  975.     }
  976.    

  977.    
  978.    
  979.     // UTILS ID
  980.    
  981.     /**
  982.      * Ritorna l'id del connettore di un soggetto
  983.      *
  984.      * @param nomeSoggetto
  985.      * @param tipoSoggetto
  986.      * @param con
  987.      * @return id del connettore del soggetto
  988.      * @throws DriverRegistroServiziException
  989.      */
  990.     static long getIdConnettoreSoggetto(String nomeSoggetto, String tipoSoggetto, Connection con) throws DriverRegistroServiziException {
  991.         PreparedStatement stm = null;
  992.         ResultSet rs = null;
  993.         long idConnettore = -1;
  994.         try {
  995.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  996.             sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  997.             sqlQueryObject.addSelectField("*");
  998.             sqlQueryObject.addWhereCondition("tipo_soggetto = ?");
  999.             sqlQueryObject.addWhereCondition("nome_soggetto = ?");
  1000.             sqlQueryObject.setANDLogicOperator(true);
  1001.             String query = sqlQueryObject.createSQLQuery();
  1002.             stm = con.prepareStatement(query);
  1003.             stm.setString(1, tipoSoggetto);
  1004.             stm.setString(2, nomeSoggetto);

  1005.             rs = stm.executeQuery();

  1006.             if (rs.next()) {
  1007.                 idConnettore = rs.getLong("id_connettore");
  1008.             }

  1009.             return idConnettore;

  1010.         } catch (SQLException e) {
  1011.             DriverRegistroServiziDB_LIB.log.error("Errore SQL", e);
  1012.             throw new DriverRegistroServiziException(e);
  1013.         }catch (Exception e) {
  1014.             DriverRegistroServiziDB_LIB.log.error("Errore", e);
  1015.             throw new DriverRegistroServiziException(e);
  1016.         } finally {
  1017.             JDBCUtilities.closeResources(rs, stm);
  1018.         }
  1019.     }

  1020.     /**
  1021.      * Ritorna l'id del connettore del servizio fornito da un soggetto
  1022.      *
  1023.      * @param nomeServizio
  1024.      * @param tipoServizio
  1025.      * @param nomeSoggetto
  1026.      * @param tipoSoggetto
  1027.      * @param con
  1028.      * @return id del connettore del servizio
  1029.      * @throws DriverRegistroServiziException
  1030.      */
  1031.     static long getIdConnettoreServizio(String nomeServizio, String tipoServizio, Integer versioneServizio, String nomeSoggetto, String tipoSoggetto, Connection con) throws DriverRegistroServiziException {
  1032.         PreparedStatement stm = null;
  1033.         ResultSet rs = null;
  1034.         long idConnettore = -1;
  1035.         long idSoggetto;
  1036.         try {

  1037.             idSoggetto = DBUtils.getIdSoggetto(nomeSoggetto, tipoSoggetto, con, DriverRegistroServiziDB_LIB.tipoDB);

  1038.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  1039.             sqlQueryObject.addFromTable(CostantiDB.SERVIZI);
  1040.             sqlQueryObject.addSelectField("*");
  1041.             sqlQueryObject.addWhereCondition("tipo_servizio = ?");
  1042.             sqlQueryObject.addWhereCondition("nome_servizio = ?");
  1043.             sqlQueryObject.addWhereCondition("versione_servizio = ?");
  1044.             sqlQueryObject.addWhereCondition("id_soggetto = ?");
  1045.             sqlQueryObject.setANDLogicOperator(true);
  1046.             String query = sqlQueryObject.createSQLQuery();
  1047.             stm = con.prepareStatement(query);
  1048.             int index = 1;
  1049.             stm.setString(index++, tipoServizio);
  1050.             stm.setString(index++, nomeServizio);
  1051.             stm.setInt(index++, versioneServizio);
  1052.             stm.setLong(index++, idSoggetto);

  1053.             rs = stm.executeQuery();

  1054.             if (rs.next()) {
  1055.                 idConnettore = rs.getLong("id_connettore");
  1056.             }

  1057.             return idConnettore;

  1058.         } catch (CoreException e) {
  1059.             DriverRegistroServiziDB_LIB.log.error("Errore Driver.", e);
  1060.             throw new DriverRegistroServiziException(e);
  1061.         } catch (SQLException e) {
  1062.             DriverRegistroServiziDB_LIB.log.error("Errore SQL", e);
  1063.             throw new DriverRegistroServiziException(e);
  1064.         } catch (Exception e) {
  1065.             DriverRegistroServiziDB_LIB.log.error("Errore", e);
  1066.             throw new DriverRegistroServiziException(e);
  1067.         }finally {
  1068.             JDBCUtilities.closeResources(rs, stm);
  1069.         }
  1070.     }

  1071.     static long getIdFruizione(long idServizio, String nomeSoggetto, String tipoSoggetto, Connection con) throws DriverRegistroServiziException {
  1072.         PreparedStatement stm = null;
  1073.         ResultSet rs = null;
  1074.         long idFruizione = -1;
  1075.         long idSoggetto;
  1076.         try {

  1077.             idSoggetto = DBUtils.getIdSoggetto(nomeSoggetto, tipoSoggetto, con, DriverRegistroServiziDB_LIB.tipoDB);

  1078.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  1079.             sqlQueryObject.addFromTable(CostantiDB.SERVIZI_FRUITORI);
  1080.             sqlQueryObject.addSelectField("id");
  1081.             sqlQueryObject.addWhereCondition("id_servizio = ?");
  1082.             sqlQueryObject.addWhereCondition("id_soggetto = ?");
  1083.             sqlQueryObject.setANDLogicOperator(true);
  1084.             String query = sqlQueryObject.createSQLQuery();
  1085.             stm = con.prepareStatement(query);
  1086.             stm.setLong(1, idServizio);
  1087.             stm.setLong(2, idSoggetto);

  1088.             rs = stm.executeQuery();

  1089.             if (rs.next()) {
  1090.                 idFruizione = rs.getLong("id");
  1091.             }

  1092.             return idFruizione;

  1093.         } catch (CoreException e) {
  1094.             DriverRegistroServiziDB_LIB.log.error("Errore Core: "+e.getMessage(),e);
  1095.             throw new DriverRegistroServiziException(e);
  1096.         } catch (SQLException e) {
  1097.             DriverRegistroServiziDB_LIB.log.error("Errore SQL: "+e.getMessage(), e);
  1098.             throw new DriverRegistroServiziException(e);
  1099.         }catch (Exception e) {
  1100.             DriverRegistroServiziDB_LIB.log.error("Errore Generico: "+e.getMessage(), e);
  1101.             throw new DriverRegistroServiziException(e);
  1102.         } finally {
  1103.             JDBCUtilities.closeResources(rs, stm);
  1104.         }
  1105.     }
  1106.    
  1107.     static long getIdConnettoreServizioFruitore(long idServizio, String nomeSoggetto, String tipoSoggetto, Connection con) throws DriverRegistroServiziException {
  1108.         PreparedStatement stm = null;
  1109.         ResultSet rs = null;
  1110.         long idConnettore = -1;
  1111.         long idSoggetto;
  1112.         try {

  1113.             idSoggetto = DBUtils.getIdSoggetto(nomeSoggetto, tipoSoggetto, con, DriverRegistroServiziDB_LIB.tipoDB);

  1114.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  1115.             sqlQueryObject.addFromTable(CostantiDB.SERVIZI_FRUITORI);
  1116.             sqlQueryObject.addSelectField("*");
  1117.             sqlQueryObject.addWhereCondition("id_servizio = ?");
  1118.             sqlQueryObject.addWhereCondition("id_soggetto = ?");
  1119.             sqlQueryObject.setANDLogicOperator(true);
  1120.             String query = sqlQueryObject.createSQLQuery();
  1121.             stm = con.prepareStatement(query);
  1122.             stm.setLong(1, idServizio);
  1123.             stm.setLong(2, idSoggetto);

  1124.             rs = stm.executeQuery();

  1125.             if (rs.next()) {
  1126.                 idConnettore = rs.getLong("id_connettore");
  1127.             }

  1128.             return idConnettore;

  1129.         } catch (CoreException e) {
  1130.             DriverRegistroServiziDB_LIB.log.error("Errore Core: "+e.getMessage(),e);
  1131.             throw new DriverRegistroServiziException(e);
  1132.         } catch (SQLException e) {
  1133.             DriverRegistroServiziDB_LIB.log.error("Errore SQL: "+e.getMessage(), e);
  1134.             throw new DriverRegistroServiziException(e);
  1135.         }catch (Exception e) {
  1136.             DriverRegistroServiziDB_LIB.log.error("Errore Generico: "+e.getMessage(), e);
  1137.             throw new DriverRegistroServiziException(e);
  1138.         } finally {
  1139.             JDBCUtilities.closeResources(rs, stm);
  1140.         }
  1141.     }
  1142.    
  1143.     static long getIdConnettoreServizioAzione(long idServizio, String azione, Connection con) throws DriverRegistroServiziException {
  1144.         PreparedStatement stm = null;
  1145.         ResultSet rs = null;
  1146.         long idConnettore = -1;
  1147.         try {

  1148.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  1149.             sqlQueryObject.addFromTable(CostantiDB.SERVIZI_AZIONI);
  1150.             sqlQueryObject.addFromTable(CostantiDB.SERVIZI_AZIONE);
  1151.             sqlQueryObject.addSelectField("*");
  1152.             sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI_AZIONI+".id="+CostantiDB.SERVIZI_AZIONE+".id_servizio_azioni");
  1153.             sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI_AZIONI+".id_servizio = ?");
  1154.             sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI_AZIONE+".nome_azione = ?");
  1155.             sqlQueryObject.setANDLogicOperator(true);
  1156.             String query = sqlQueryObject.createSQLQuery();
  1157.             stm = con.prepareStatement(query);
  1158.             stm.setLong(1, idServizio);
  1159.             stm.setString(2, azione);

  1160.             rs = stm.executeQuery();

  1161.             if (rs.next()) {
  1162.                 idConnettore = rs.getLong("id_connettore");
  1163.             }

  1164.             return idConnettore;

  1165.         } catch (SQLException e) {
  1166.             DriverRegistroServiziDB_LIB.log.error("Errore SQL", e);
  1167.             throw new DriverRegistroServiziException(e);
  1168.         }catch (Exception e) {
  1169.             DriverRegistroServiziDB_LIB.log.error("Errore", e);
  1170.             throw new DriverRegistroServiziException(e);
  1171.         } finally {
  1172.             JDBCUtilities.closeResources(rs, stm);
  1173.         }
  1174.     }
  1175.    
  1176.     static long getIdConnettoreFruizioneServizioAzione(long idFruizione, String azione, Connection con) throws DriverRegistroServiziException {
  1177.         PreparedStatement stm = null;
  1178.         ResultSet rs = null;
  1179.         long idConnettore = -1;
  1180.         try {

  1181.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  1182.             sqlQueryObject.addFromTable(CostantiDB.SERVIZI_FRUITORI_AZIONI);
  1183.             sqlQueryObject.addFromTable(CostantiDB.SERVIZI_FRUITORI_AZIONE);
  1184.             sqlQueryObject.addSelectField("*");
  1185.             sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI_FRUITORI_AZIONI+".id="+CostantiDB.SERVIZI_FRUITORI_AZIONE+".id_fruizione_azioni");
  1186.             sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI_FRUITORI_AZIONI+".id_fruizione = ?");
  1187.             sqlQueryObject.addWhereCondition(CostantiDB.SERVIZI_FRUITORI_AZIONE+".nome_azione = ?");
  1188.             sqlQueryObject.setANDLogicOperator(true);
  1189.             String query = sqlQueryObject.createSQLQuery();
  1190.             stm = con.prepareStatement(query);
  1191.             stm.setLong(1, idFruizione);
  1192.             stm.setString(2, azione);

  1193.             rs = stm.executeQuery();

  1194.             if (rs.next()) {
  1195.                 idConnettore = rs.getLong("id_connettore");
  1196.             }

  1197.             return idConnettore;

  1198.         } catch (SQLException e) {
  1199.             DriverRegistroServiziDB_LIB.log.error("Errore SQL", e);
  1200.             throw new DriverRegistroServiziException(e);
  1201.         }catch (Exception e) {
  1202.             DriverRegistroServiziDB_LIB.log.error("Errore", e);
  1203.             throw new DriverRegistroServiziException(e);
  1204.         } finally {
  1205.             JDBCUtilities.closeResources(rs, stm);
  1206.         }
  1207.     }

  1208.    
  1209.    
  1210.    
  1211.    
  1212.    

  1213.     public static void CRUDProtocolProperty(int type, List<ProtocolProperty> listPP, long idProprietario,
  1214.             org.openspcoop2.core.constants.ProprietariProtocolProperty tipologiaProprietarioProtocolProperty, Connection connection,
  1215.             String tipoDatabase, IDriverBYOK driverBYOK) throws DriverRegistroServiziException {
  1216.         try {
  1217.             DBProtocolPropertiesUtils.crudRegistryProtocolProperty(log, type, listPP, idProprietario, tipologiaProprietarioProtocolProperty, connection, tipoDatabase, driverBYOK);
  1218.         }catch(Exception e) {
  1219.             throw new DriverRegistroServiziException(e.getMessage(),e);
  1220.         }
  1221.     }
  1222.    
  1223.    
  1224.     public static List<ProtocolProperty> getListaProtocolProperty(long idProprietario, org.openspcoop2.core.constants.ProprietariProtocolProperty tipologiaProprietario,
  1225.             Connection connection,
  1226.             String tipoDatabase, IDriverBYOK driverBYOK) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  1227.         try {
  1228.             return DBProtocolPropertiesUtils.getListaProtocolPropertyRegistry(idProprietario,tipologiaProprietario,connection,tipoDatabase, driverBYOK);
  1229.         }
  1230.         catch(NotFoundException e) {
  1231.             throw new DriverRegistroServiziNotFound(e.getMessage(),e);
  1232.         }
  1233.         catch(Exception e) {
  1234.             throw new DriverRegistroServiziException(e.getMessage(),e);
  1235.         }
  1236.     }
  1237.    
  1238.     public static ProtocolProperty getProtocolProperty(long id, Connection connection, String tipoDatabase, IDriverBYOK driverBYOK) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  1239.         try {
  1240.             return DBProtocolPropertiesUtils.getProtocolPropertyRegistry(id, connection, tipoDatabase, driverBYOK);
  1241.         }
  1242.         catch(NotFoundException e) {
  1243.             throw new DriverRegistroServiziNotFound(e.getMessage(),e);
  1244.         }
  1245.         catch(Exception e) {
  1246.             throw new DriverRegistroServiziException(e.getMessage(),e);
  1247.         }
  1248.     }
  1249.    
  1250. }