DriverRegistroServiziDB_soggettiLIB.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 org.openspcoop2.core.byok.BYOKUtilities;
  30. import org.openspcoop2.core.byok.BYOKWrappedValue;
  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.constants.ProprietariProtocolProperty;
  36. import org.openspcoop2.core.id.IDRuolo;
  37. import org.openspcoop2.core.registry.Connettore;
  38. import org.openspcoop2.core.registry.CredenzialiSoggetto;
  39. import org.openspcoop2.core.registry.PortaDominio;
  40. import org.openspcoop2.core.registry.Proprieta;
  41. import org.openspcoop2.core.registry.RuoloSoggetto;
  42. import org.openspcoop2.core.registry.constants.StatoFunzionalita;
  43. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  44. import org.openspcoop2.utils.certificate.CertificateUtils;
  45. import org.openspcoop2.utils.certificate.PrincipalType;
  46. import org.openspcoop2.utils.date.DateManager;
  47. import org.openspcoop2.utils.jdbc.IJDBCAdapter;
  48. import org.openspcoop2.utils.jdbc.JDBCAdapterFactory;
  49. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  50. import org.openspcoop2.utils.sql.ISQLQueryObject;
  51. import org.openspcoop2.utils.sql.SQLObjectFactory;

  52. /**
  53.  * Classe utilizzata per effettuare query ad un registro dei servizi openspcoop
  54.  * formato db.
  55.  *
  56.  *
  57.  * @author Sandra Giangrandi (sandra@link.it)
  58.  * @author Stefano Corallo (corallo@link.it)
  59.  * @author $Author$
  60.  * @version $Rev$, $Date$
  61.  */
  62. public class DriverRegistroServiziDB_soggettiLIB {

  63.    

  64.     /** Metodi CRUD */

  65.     public static void CRUDPortaDominio(int type, PortaDominio pdd, Connection con)
  66.     throws DriverRegistroServiziException {
  67.         if (pdd == null) {
  68.             throw new DriverRegistroServiziException(
  69.             "[DriverRegistroServiziDB_LIB::CRUDPdd] Parametro non valido.");
  70.         }

  71.         /**if ((type != CostantiDB.CREATE) && (pdd.getId() <= 0)) {
  72.             throw new DriverRegistroServiziException(
  73.             "[DriverRegistroServiziDB_LIB::CRUDPdd] ID Pdd non valido.");
  74.         }*/

  75.         String nome = pdd.getNome();
  76.         String descrizione = pdd.getDescrizione();
  77.         String implementazione = pdd.getImplementazione();

  78.         String subject = pdd.getSubject();
  79.         StatoFunzionalita client_auth = pdd.getClientAuth();
  80.        
  81.         String superuser = pdd.getSuperUser();

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

  88.         try {

  89.             // preparo lo statement in base al tipo di operazione
  90.             switch (type) {
  91.             case CREATE:
  92.                 // CREATE
  93.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  94.                 sqlQueryObject.addInsertTable(CostantiDB.PDD);
  95.                 sqlQueryObject.addInsertField("nome", "?");
  96.                 sqlQueryObject.addInsertField("descrizione", "?");
  97.                 sqlQueryObject.addInsertField("implementazione", "?");
  98.                 sqlQueryObject.addInsertField("subject", "?");
  99.                 sqlQueryObject.addInsertField("client_auth", "?");
  100.                 sqlQueryObject.addInsertField("superuser", "?");
  101.                 if(pdd.getOraRegistrazione()!=null)
  102.                     sqlQueryObject.addInsertField("ora_registrazione", "?");

  103.                 updateQuery = sqlQueryObject.createSQLInsert();
  104.                 updateStmt = con.prepareStatement(updateQuery);

  105.                 updateStmt.setString(1, nome);
  106.                 updateStmt.setString(2, descrizione);
  107.                 updateStmt.setString(3, implementazione);
  108.                 updateStmt.setString(4, (subject != null ? CertificateUtils.formatPrincipal(subject, PrincipalType.SUBJECT) : null));
  109.                 updateStmt.setString(5, DriverRegistroServiziDB_LIB.getValue(client_auth));
  110.                 updateStmt.setString(6, superuser);
  111.                 if(pdd.getOraRegistrazione()!=null)
  112.                     updateStmt.setTimestamp(7, new Timestamp(pdd.getOraRegistrazione().getTime()));

  113.                 // eseguo lo statement
  114.                 n = updateStmt.executeUpdate();

  115.                 updateStmt.close();

  116.                 DriverRegistroServiziDB_LIB.logDebug("CRUDPdd type = " + type
  117.                         + " row affected =" + n);

  118.                 DriverRegistroServiziDB_LIB.logDebug("CRUDPdd CREATE : \n"
  119.                         + DriverRegistroServiziDB_LIB.formatSQLString(
  120.                                 updateQuery, nome, descrizione,implementazione,subject,client_auth));

  121.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  122.                 sqlQueryObject.addFromTable(CostantiDB.PDD);
  123.                 sqlQueryObject.addSelectField("id");
  124.                 sqlQueryObject.addWhereCondition("nome = ?");
  125.                 selectQuery = sqlQueryObject.createSQLQuery();
  126.                 selectStmt = con.prepareStatement(selectQuery);
  127.                 selectStmt.setString(1, nome);

  128.                 selectRS = selectStmt.executeQuery();
  129.                 if (selectRS.next()) {
  130.                     pdd.setId(selectRS.getLong("id"));
  131.                 }
  132.                 selectRS.close();
  133.                 selectStmt.close();
  134.                 break;

  135.             case UPDATE:
  136.                 // UPDATE

  137.                 String nomePdd = pdd.getOldNomeForUpdate();
  138.                 if(nomePdd==null || "".equals(nomePdd))
  139.                     nomePdd = pdd.getNome();
  140.                
  141.                 long idPdd = DBUtils.getIdPortaDominio(nomePdd, con, DriverRegistroServiziDB_LIB.tipoDB);
  142.                 if (idPdd <= 0)
  143.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDPortaDominio(UPDATE)] Id Porta di Dominio non valido.");
  144.                
  145.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  146.                 sqlQueryObject.addUpdateTable(CostantiDB.PDD);
  147.                 sqlQueryObject.addUpdateField("nome", "?");
  148.                 sqlQueryObject.addUpdateField("descrizione", "?");
  149.                 sqlQueryObject.addUpdateField("implementazione", "?");
  150.                 sqlQueryObject.addUpdateField("subject", "?");
  151.                 sqlQueryObject.addUpdateField("client_auth", "?");
  152.                 sqlQueryObject.addUpdateField("superuser", "?");
  153.                 if(pdd.getOraRegistrazione()!=null)
  154.                     sqlQueryObject.addUpdateField("ora_registrazione", "?");
  155.                 sqlQueryObject.addWhereCondition("id=?");
  156.                 updateQuery = sqlQueryObject.createSQLUpdate();
  157.                 updateStmt = con.prepareStatement(updateQuery);

  158.                 updateStmt.setString(1, nome);
  159.                 updateStmt.setString(2, descrizione);
  160.                 updateStmt.setString(3, implementazione);
  161.                 updateStmt.setString(4, (subject != null ? CertificateUtils.formatPrincipal(subject, PrincipalType.SUBJECT) : null));
  162.                 updateStmt.setString(5, DriverRegistroServiziDB_LIB.getValue(client_auth));
  163.                 updateStmt.setString(6, superuser);

  164.                 int paramIndex = 6;

  165.                 if(pdd.getOraRegistrazione()!=null)
  166.                     updateStmt.setTimestamp(++paramIndex, new Timestamp(pdd.getOraRegistrazione().getTime()));

  167.                 updateStmt.setLong(++paramIndex, idPdd);

  168.                 // eseguo lo statement
  169.                 n = updateStmt.executeUpdate();
  170.                 updateStmt.close();
  171.                 DriverRegistroServiziDB_LIB.logDebug("CRUDPdd type = " + type
  172.                         + " row affected =" + n);

  173.                 DriverRegistroServiziDB_LIB.logDebug("CRUDPdd UPDATE : \n"
  174.                         + DriverRegistroServiziDB_LIB.formatSQLString(
  175.                                 updateQuery, descrizione, implementazione,subject,client_auth,idPdd));

  176.                 break;

  177.             case DELETE:
  178.                 // DELETE

  179.                 idPdd = DBUtils.getIdPortaDominio(nome, con, DriverRegistroServiziDB_LIB.tipoDB);
  180.                 if (idPdd <= 0)
  181.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDPortaDominio(DELETE)] Id Porta di Dominio non valido.");
  182.                
  183.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  184.                 sqlQueryObject.addDeleteTable(CostantiDB.PDD);
  185.                 sqlQueryObject.addWhereCondition("id=?");
  186.                 updateQuery = sqlQueryObject.createSQLDelete();
  187.                 updateStmt = con.prepareStatement(updateQuery);

  188.                 updateStmt.setLong(1, idPdd);

  189.                 // eseguo lo statement
  190.                 n = updateStmt.executeUpdate();
  191.                 updateStmt.close();
  192.                 DriverRegistroServiziDB_LIB.logDebug("CRUDPdd type = " + type
  193.                         + " row affected =" + n);

  194.                 DriverRegistroServiziDB_LIB.logDebug("CRUDPdd DELETE : \n"
  195.                         + DriverRegistroServiziDB_LIB.formatSQLString(
  196.                                 updateQuery, idPdd));

  197.                 break;
  198.             }

  199.         } catch (SQLException se) {
  200.             throw new DriverRegistroServiziException(
  201.                     "[DriverControlStationDB_LIB::CRUDPdd] SQLException ["
  202.                     + se.getMessage() + "].",se);
  203.         } catch (Exception se) {
  204.             throw new DriverRegistroServiziException(
  205.                     "[DriverControlStationDB_LIB::CRUDPdd] Exception ["
  206.                     + se.getMessage() + "].",se);
  207.         } finally {
  208.             JDBCUtilities.closeResources(updateStmt);
  209.             JDBCUtilities.closeResources(selectRS, selectStmt);
  210.         }
  211.     }
  212.    
  213.     /**
  214.      * CRUD oggetto Soggetto Non si occupa di chiudere la connessione con
  215.      * il db in caso di errore in quanto verra' gestita dal metodo chiamante
  216.      *
  217.      * @param type
  218.      *            Tipo operazione {1 (CREATE),2 (UPDATE),3 (DELETE)}
  219.      * @param soggetto
  220.      * @throws DriverRegistroServiziException
  221.      */
  222.     public static long CRUDSoggetto(int type, org.openspcoop2.core.registry.Soggetto soggetto,
  223.             Connection con, String tipoDatabase, IDriverBYOK driverBYOK) throws DriverRegistroServiziException {
  224.         if (soggetto == null)
  225.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDSoggetto] Parametro non valido.");

  226.         String nome = soggetto.getNome();
  227.         String tipo = soggetto.getTipo();

  228.         if (nome == null || nome.equals(""))
  229.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDSoggetto] Parametro Nome non valido.");
  230.         if (tipo == null || tipo.equals(""))
  231.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDSoggetto] Parametro Tipo non valido.");

  232.         String descizione = soggetto.getDescrizione();
  233.         String identificativoPorta = soggetto.getIdentificativoPorta();
  234.         String server = soggetto.getPortaDominio();
  235.         Connettore connettore = soggetto.getConnettore();
  236.         String codiceIPA = soggetto.getCodiceIpa();
  237.         String superUser = soggetto.getSuperUser();

  238.         if (connettore == null && type != CostantiDB.CREATE && type!=CostantiDB.DELETE)
  239.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDSoggetto] Il connettore del soggetto e' null.");

  240.         PreparedStatement updateStmt = null;
  241.         String updateQuery = "";
  242.         PreparedStatement selectStmt = null;
  243.         String selectQuery = "";
  244.         ResultSet selectRS = null;
  245.         long idSoggetto = 0;
  246.         int n = 0;

  247.         try {
  248.             long idConnettore;

  249.             // preparo lo statement in base al tipo di operazione
  250.             switch (type) {
  251.             case CREATE:
  252.                 // CREATE

  253.                 String utenteRichiedente = null;
  254.                 if(soggetto.getProprietaOggetto()!=null && soggetto.getProprietaOggetto().getUtenteRichiedente()!=null) {
  255.                     utenteRichiedente = soggetto.getProprietaOggetto().getUtenteRichiedente();
  256.                 }
  257.                 else {
  258.                     utenteRichiedente = superUser;
  259.                 }
  260.                
  261.                 Timestamp dataCreazione = null;
  262.                 if(soggetto.getProprietaOggetto()!=null && soggetto.getProprietaOggetto().getDataCreazione()!=null) {
  263.                     dataCreazione = new Timestamp(soggetto.getProprietaOggetto().getDataCreazione().getTime());
  264.                 }
  265.                 else if(soggetto.getOraRegistrazione()!=null){
  266.                     dataCreazione = new Timestamp(soggetto.getOraRegistrazione().getTime());
  267.                 }
  268.                 else {
  269.                     dataCreazione = DateManager.getTimestamp();
  270.                 }
  271.                
  272.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  273.                 sqlQueryObject.addInsertTable(CostantiDB.SOGGETTI);
  274.                 sqlQueryObject.addInsertField("nome_soggetto", "?");
  275.                 sqlQueryObject.addInsertField("descrizione", "?");
  276.                 sqlQueryObject.addInsertField("identificativo_porta", "?");
  277.                 sqlQueryObject.addInsertField("tipo_soggetto", "?");
  278.                 sqlQueryObject.addInsertField("id_connettore", "?");
  279.                 sqlQueryObject.addInsertField("server", "?");
  280.                 sqlQueryObject.addInsertField("superuser", "?");
  281.                 sqlQueryObject.addInsertField("privato", "?");
  282.                 sqlQueryObject.addInsertField("profilo", "?");
  283.                 sqlQueryObject.addInsertField("codice_ipa", "?");
  284.                 sqlQueryObject.addInsertField("tipoauth", "?");
  285.                 sqlQueryObject.addInsertField("utente", "?");
  286.                 sqlQueryObject.addInsertField("password", "?");
  287.                 sqlQueryObject.addInsertField("subject", "?");
  288.                 sqlQueryObject.addInsertField("cn_subject", "?");
  289.                 sqlQueryObject.addInsertField("issuer", "?");
  290.                 sqlQueryObject.addInsertField("cn_issuer", "?");
  291.                 sqlQueryObject.addInsertField("certificate", "?");
  292.                 sqlQueryObject.addInsertField("cert_strict_verification", "?");
  293.                 if(soggetto.getOraRegistrazione()!=null)
  294.                     sqlQueryObject.addInsertField("ora_registrazione", "?");
  295.                 if(utenteRichiedente!=null) {
  296.                     sqlQueryObject.addInsertField(CostantiDB.PROPRIETA_OGGETTO_UTENTE_RICHIEDENTE, "?");
  297.                 }
  298.                 if(dataCreazione!=null) {
  299.                     sqlQueryObject.addInsertField(CostantiDB.PROPRIETA_OGGETTO_DATA_CREAZIONE, "?");
  300.                 }
  301.                 updateQuery = sqlQueryObject.createSQLInsert();
  302.                 updateStmt = con.prepareStatement(updateQuery);

  303.                 // Controllo se il connettore non e' presente ne creo uno
  304.                 // disabilitato
  305.                 // in questo caso setto il nome del connettore
  306.                 if (connettore == null) {
  307.                     connettore = new Connettore();
  308.                     connettore.setNome("CNT_" + tipo + "_" + nome);
  309.                 }

  310.                 if (connettore.getNome() == null || connettore.getNome().equals(""))
  311.                     connettore.setNome("CNT_" + tipo + "_" + nome);

  312.                 idConnettore = DriverRegistroServiziDB_connettoriLIB.CRUDConnettore(CostantiDB.CREATE, connettore, con, driverBYOK);

  313.                 int index = 1;
  314.                
  315.                 updateStmt.setString(index++, nome);
  316.                 updateStmt.setString(index++, descizione);
  317.                 updateStmt.setString(index++, identificativoPorta);
  318.                 updateStmt.setString(index++, tipo);
  319.                 updateStmt.setLong(index++, idConnettore);
  320.                 updateStmt.setString(index++, server);
  321.                 updateStmt.setString(index++, superUser);
  322.                 if(soggetto.getPrivato()!=null && soggetto.getPrivato())
  323.                     updateStmt.setInt(index++, 1);
  324.                 else
  325.                     updateStmt.setInt(index++, 0);
  326.                 updateStmt.setString(index++, soggetto.getVersioneProtocollo());
  327.                 updateStmt.setString(index++, codiceIPA);
  328.                
  329.                 CredenzialiSoggetto credenziali = (soggetto.sizeCredenzialiList() > 0 ? soggetto.getCredenziali(0) : null);
  330.                 updateStmt.setString(index++, (credenziali != null ? DriverRegistroServiziDB_LIB.getValue(credenziali.getTipo()) : null));
  331.                 updateStmt.setString(index++, (credenziali != null ? credenziali.getUser() : null));
  332.                 updateStmt.setString(index++, (credenziali != null ? credenziali.getPassword() : null));
  333.                
  334.                 String subject = null;
  335.                 if(credenziali!=null && credenziali.getSubject()!=null && !"".equals(credenziali.getSubject()))
  336.                     subject = credenziali.getSubject();
  337.                 updateStmt.setString(index++, (subject != null ? CertificateUtils.formatPrincipal(subject, PrincipalType.SUBJECT) : null));
  338.                 String subjectCN = null;
  339.                 if(credenziali!=null && credenziali.getCnSubject()!=null && !"".equals(credenziali.getCnSubject()))
  340.                     subjectCN = credenziali.getCnSubject();
  341.                 updateStmt.setString(index++, subjectCN);
  342.                
  343.                 String issuer = null;
  344.                 if(credenziali != null && org.openspcoop2.core.registry.constants.CredenzialeTipo.APIKEY.equals(credenziali.getTipo())) {
  345.                     updateStmt.setString(index++, CostantiDB.getIssuerApiKey(credenziali.isAppId()));
  346.                 }
  347.                 else {
  348.                     if(credenziali!=null && credenziali.getIssuer()!=null && !"".equals(credenziali.getIssuer()))
  349.                         issuer = credenziali.getIssuer();
  350.                     updateStmt.setString(index++, (issuer != null ? CertificateUtils.formatPrincipal(issuer, PrincipalType.ISSUER) : null));
  351.                 }
  352.                 String issuerCN = null;
  353.                 if(credenziali!=null && credenziali.getCnIssuer()!=null && !"".equals(credenziali.getCnIssuer()))
  354.                     issuerCN = credenziali.getCnIssuer();
  355.                 updateStmt.setString(index++, issuerCN);
  356.                
  357.                 byte [] certificate = null;
  358.                 if(credenziali!=null && credenziali.getCertificate()!=null) {
  359.                     certificate = credenziali.getCertificate();
  360.                 }
  361.                 IJDBCAdapter jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(DriverRegistroServiziDB_LIB.tipoDB);
  362.                 jdbcAdapter.setBinaryData(updateStmt, index++, certificate);
  363.                 if(credenziali!=null && credenziali.isCertificateStrictVerification()) {
  364.                     updateStmt.setInt(index++, CostantiDB.TRUE);
  365.                 }              
  366.                 else {
  367.                     updateStmt.setInt(index++, CostantiDB.FALSE);
  368.                 }
  369.                                
  370.                 if(soggetto.getOraRegistrazione()!=null){
  371.                     updateStmt.setTimestamp(index++, new Timestamp(soggetto.getOraRegistrazione().getTime()));
  372.                 }
  373.                
  374.                 if(utenteRichiedente!=null) {
  375.                     updateStmt.setString(index++, utenteRichiedente);
  376.                 }
  377.                
  378.                 if(dataCreazione!=null) {
  379.                     updateStmt.setTimestamp(index++, dataCreazione);
  380.                 }

  381.                 // eseguo lo statement
  382.                 n = updateStmt.executeUpdate();
  383.                 updateStmt.close();
  384.                 DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + n);
  385.                 DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto CREATE : \n" + DriverRegistroServiziDB_LIB.formatSQLString(updateQuery, nome, descizione, identificativoPorta, tipo, idConnettore, server));

  386.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  387.                 sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  388.                 sqlQueryObject.addSelectField("id");
  389.                 sqlQueryObject.addWhereCondition("tipo_soggetto = ?");
  390.                 sqlQueryObject.addWhereCondition("nome_soggetto = ?");
  391.                 sqlQueryObject.setANDLogicOperator(true);
  392.                 selectQuery = sqlQueryObject.createSQLQuery();
  393.                 selectStmt = con.prepareStatement(selectQuery);
  394.                 selectStmt.setString(1, tipo);
  395.                 selectStmt.setString(2, nome);
  396.                 selectRS = selectStmt.executeQuery();
  397.                 if (selectRS.next())
  398.                     idSoggetto = selectRS.getLong("id");

  399.                 soggetto.setId(idSoggetto);

  400.                 selectRS.close();
  401.                 selectStmt.close();
  402.                
  403.                
  404.                 // ProtocolProperties
  405.                 DriverRegistroServiziDB_LIB.CRUDProtocolProperty(CostantiDB.CREATE, soggetto.getProtocolPropertyList(),
  406.                         idSoggetto, ProprietariProtocolProperty.SOGGETTO, con, tipoDatabase, driverBYOK);
  407.                
  408.                
  409.                 // ruoli
  410.                
  411.                 n = 0;
  412.                 if(soggetto.getRuoli()!=null && soggetto.getRuoli().sizeRuoloList()>0){
  413.                     for (int i = 0; i < soggetto.getRuoli().sizeRuoloList(); i++) {
  414.                         RuoloSoggetto ruoloSoggetto = soggetto.getRuoli().getRuolo(i);
  415.                        
  416.                         IDRuolo idRuoloObject= new IDRuolo(ruoloSoggetto.getNome());
  417.                         long idRuolo = DBUtils.getIdRuolo(idRuoloObject, con, DriverRegistroServiziDB_LIB.tipoDB);
  418.                        
  419.                         sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  420.                         sqlQueryObject.addInsertTable(CostantiDB.SOGGETTI_RUOLI);
  421.                         sqlQueryObject.addInsertField("id_soggetto", "?");
  422.                         sqlQueryObject.addInsertField("id_ruolo", "?");
  423.                         updateQuery = sqlQueryObject.createSQLInsert();
  424.                         updateStmt = con.prepareStatement(updateQuery);
  425.                        
  426.                         updateStmt.setLong(1, idSoggetto);
  427.                         updateStmt.setLong(2, idRuolo);
  428.                        
  429.                         int r= updateStmt.executeUpdate();
  430.                         n++;
  431.                         updateStmt.close();
  432.                         DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + r+" create role ["+ruoloSoggetto.getNome()+"]");
  433.                     }
  434.                 }
  435.                
  436.                 DriverRegistroServiziDB_LIB.logDebug("Aggiunti " + n + " ruoli al soggetto "+idSoggetto);
  437.                
  438.                
  439.                 // credenziali (le credenziali in questa tabella partono dal numero maggiore di 1)
  440.                
  441.                 n = 0;
  442.                 if(soggetto.sizeCredenzialiList()>1){
  443.                     for (int i = 1; i < soggetto.sizeCredenzialiList(); i++) {
  444.                         CredenzialiSoggetto credenzialiSoggetto = soggetto.getCredenziali(i);
  445.                        
  446.                         sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  447.                         sqlQueryObject.addInsertTable(CostantiDB.SOGGETTI_CREDENZIALI);
  448.                         sqlQueryObject.addInsertField("id_soggetto", "?");
  449.                         sqlQueryObject.addInsertField("subject", "?");
  450.                         sqlQueryObject.addInsertField("cn_subject", "?");
  451.                         sqlQueryObject.addInsertField("issuer", "?");
  452.                         sqlQueryObject.addInsertField("cn_issuer", "?");
  453.                         sqlQueryObject.addInsertField("certificate", "?");
  454.                         sqlQueryObject.addInsertField("cert_strict_verification", "?");
  455.                         updateQuery = sqlQueryObject.createSQLInsert();
  456.                         updateStmt = con.prepareStatement(updateQuery);
  457.                        
  458.                         index = 1;
  459.                         updateStmt.setLong(index++, idSoggetto);
  460.                        
  461.                         String subjectCredenziali = null;
  462.                         if(credenzialiSoggetto!=null && credenzialiSoggetto.getSubject()!=null && !"".equals(credenzialiSoggetto.getSubject()))
  463.                             subjectCredenziali = credenzialiSoggetto.getSubject();
  464.                         updateStmt.setString(index++, (subjectCredenziali != null ? CertificateUtils.formatPrincipal(subjectCredenziali, PrincipalType.SUBJECT) : null));
  465.                         String subjectCredenzialiCN = null;
  466.                         if(credenzialiSoggetto!=null && credenzialiSoggetto.getCnSubject()!=null && !"".equals(credenzialiSoggetto.getCnSubject()))
  467.                             subjectCredenzialiCN = credenzialiSoggetto.getCnSubject();
  468.                         updateStmt.setString(index++, subjectCredenzialiCN);
  469.                        
  470.                         String issuerCredenziali = null;
  471.                         if(credenzialiSoggetto != null && org.openspcoop2.core.registry.constants.CredenzialeTipo.APIKEY.equals(credenzialiSoggetto.getTipo())) {
  472.                             updateStmt.setString(index++, CostantiDB.getIssuerApiKey(credenzialiSoggetto.isAppId()));
  473.                         }
  474.                         else {
  475.                             if(credenzialiSoggetto!=null && credenzialiSoggetto.getIssuer()!=null && !"".equals(credenzialiSoggetto.getIssuer()))
  476.                                 issuerCredenziali = credenzialiSoggetto.getIssuer();
  477.                             updateStmt.setString(index++, (issuerCredenziali != null ? CertificateUtils.formatPrincipal(issuerCredenziali, PrincipalType.ISSUER) : null));
  478.                         }
  479.                         String issuerCredenzialiCN = null;
  480.                         if(credenzialiSoggetto!=null && credenzialiSoggetto.getCnIssuer()!=null && !"".equals(credenzialiSoggetto.getCnIssuer()))
  481.                             issuerCredenzialiCN = credenzialiSoggetto.getCnIssuer();
  482.                         updateStmt.setString(index++, issuerCredenzialiCN);
  483.                        
  484.                         byte [] certificateCredenziali = null;
  485.                         if(credenzialiSoggetto!=null && credenzialiSoggetto.getCertificate()!=null) {
  486.                             certificateCredenziali = credenzialiSoggetto.getCertificate();
  487.                         }
  488.                         jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(DriverRegistroServiziDB_LIB.tipoDB);
  489.                         jdbcAdapter.setBinaryData(updateStmt, index++, certificateCredenziali);
  490.                         if(credenzialiSoggetto!=null && credenzialiSoggetto.isCertificateStrictVerification()) {
  491.                             updateStmt.setInt(index++, CostantiDB.TRUE);
  492.                         }              
  493.                         else {
  494.                             updateStmt.setInt(index++, CostantiDB.FALSE);
  495.                         }
  496.                        
  497.                         int r = updateStmt.executeUpdate();
  498.                         n++;
  499.                         updateStmt.close();
  500.                         DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + r+" create credenziale");
  501.                     }
  502.                    
  503.                 }
  504.                
  505.                 DriverRegistroServiziDB_LIB.logDebug("Aggiunte " + n + " credenziali al soggetto "+idSoggetto);
  506.                
  507.                
  508.                 // properties
  509.                
  510.                 n = 0;
  511.                 if(soggetto.sizeProprietaList()>0){
  512.                     for (int i = 0; i < soggetto.sizeProprietaList(); i++) {
  513.                        
  514.                         Proprieta proprieta = soggetto.getProprieta(i);
  515.                        
  516.                         sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  517.                         sqlQueryObject.addInsertTable(CostantiDB.SOGGETTI_PROPS);
  518.                         sqlQueryObject.addInsertField("id_soggetto", "?");
  519.                         sqlQueryObject.addInsertField("nome", "?");
  520.                         sqlQueryObject.addInsertField("valore", "?");
  521.                         sqlQueryObject.addInsertField("enc_value", "?");
  522.                         updateQuery = sqlQueryObject.createSQLInsert();
  523.                         updateStmt = con.prepareStatement(updateQuery);
  524.                        
  525.                         int indexP = 1;
  526.                         updateStmt.setLong(indexP++, idSoggetto);
  527.                         updateStmt.setString(indexP++, proprieta.getNome());
  528.                        
  529.                         String plainValue = proprieta.getValore();
  530.                         String encValue = null;
  531.                         if(driverBYOK!=null && BYOKUtilities.isWrappedValue(plainValue) ) {
  532.                             BYOKWrappedValue byokValue = driverBYOK.wrap(plainValue);
  533.                             if(byokValue!=null) {
  534.                                 encValue = byokValue.getWrappedValue();
  535.                                 plainValue = byokValue.getWrappedPlainValue();
  536.                             }
  537.                         }
  538.                        
  539.                         updateStmt.setString(indexP++, plainValue);
  540.                         updateStmt.setString(indexP++, encValue);
  541.                        
  542.                         int r= updateStmt.executeUpdate();
  543.                         n++;
  544.                         updateStmt.close();
  545.                         DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + r+" create property ["+proprieta.getNome()+"]");
  546.                     }
  547.                 }
  548.                
  549.                 DriverRegistroServiziDB_LIB.logDebug("Aggiunti " + n + " proprietà al soggetto "+idSoggetto);
  550.                
  551.                
  552.                 break;

  553.             case UPDATE:
  554.                 // UPDATE
  555.                 String oldNomeSoggetto = null;
  556.                 String oldTipoSoggetto = null;
  557.                 if(soggetto.getOldIDSoggettoForUpdate()!=null){
  558.                     oldNomeSoggetto = soggetto.getOldIDSoggettoForUpdate().getNome();
  559.                     oldTipoSoggetto = soggetto.getOldIDSoggettoForUpdate().getTipo();
  560.                 }

  561.                 // se i valori old... non sono settati allora uso quelli normali
  562.                 if (oldNomeSoggetto == null || oldNomeSoggetto.equals(""))
  563.                     oldNomeSoggetto = nome;
  564.                 if (oldTipoSoggetto == null || oldTipoSoggetto.equals(""))
  565.                     oldTipoSoggetto = tipo;

  566.                 String utenteUltimaModifica = null;
  567.                 if(soggetto.getProprietaOggetto()!=null && soggetto.getProprietaOggetto().getUtenteUltimaModifica()!=null) {
  568.                     utenteUltimaModifica = soggetto.getProprietaOggetto().getUtenteUltimaModifica();
  569.                 }
  570.                 else {
  571.                     utenteUltimaModifica = superUser;
  572.                 }
  573.                
  574.                 Timestamp dataUltimaModifica = null;
  575.                 if(soggetto.getProprietaOggetto()!=null && soggetto.getProprietaOggetto().getDataUltimaModifica()!=null) {
  576.                     dataUltimaModifica = new Timestamp(soggetto.getProprietaOggetto().getDataUltimaModifica().getTime());
  577.                 }
  578.                 else {
  579.                     dataUltimaModifica = DateManager.getTimestamp();
  580.                 }
  581.                
  582.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  583.                 sqlQueryObject.addUpdateTable(CostantiDB.SOGGETTI);
  584.                 sqlQueryObject.addUpdateField("nome_soggetto", "?");
  585.                 sqlQueryObject.addUpdateField("descrizione", "?");
  586.                 sqlQueryObject.addUpdateField("identificativo_porta", "?");
  587.                 sqlQueryObject.addUpdateField("tipo_soggetto", "?");
  588.                 sqlQueryObject.addUpdateField("server", "?");
  589.                 sqlQueryObject.addUpdateField("superuser", "?");
  590.                 sqlQueryObject.addUpdateField("privato", "?");
  591.                 sqlQueryObject.addUpdateField("profilo", "?");
  592.                 sqlQueryObject.addUpdateField("codice_ipa", "?");
  593.                 sqlQueryObject.addUpdateField("tipoauth", "?");
  594.                 sqlQueryObject.addUpdateField("utente", "?");
  595.                 sqlQueryObject.addUpdateField("password", "?");
  596.                 sqlQueryObject.addUpdateField("subject", "?");
  597.                 sqlQueryObject.addUpdateField("cn_subject", "?");
  598.                 sqlQueryObject.addUpdateField("issuer", "?");
  599.                 sqlQueryObject.addUpdateField("cn_issuer", "?");
  600.                 sqlQueryObject.addUpdateField("certificate", "?");
  601.                 sqlQueryObject.addUpdateField("cert_strict_verification", "?");
  602.                 if(soggetto.getOraRegistrazione()!=null)
  603.                     sqlQueryObject.addUpdateField("ora_registrazione", "?");
  604.                 if(utenteUltimaModifica!=null) {
  605.                     sqlQueryObject.addUpdateField(CostantiDB.PROPRIETA_OGGETTO_UTENTE_ULTIMA_MODIFICA, "?");
  606.                 }
  607.                 if(dataUltimaModifica!=null) {
  608.                     sqlQueryObject.addUpdateField(CostantiDB.PROPRIETA_OGGETTO_DATA_ULTIMA_MODIFICA, "?");
  609.                 }
  610.                
  611.                 sqlQueryObject.addWhereCondition("id=?");
  612.                 updateQuery = sqlQueryObject.createSQLUpdate();
  613.                 updateStmt = con.prepareStatement(updateQuery);

  614.                 idConnettore = DriverRegistroServiziDB_LIB.getIdConnettoreSoggetto(oldNomeSoggetto, oldTipoSoggetto, con);
  615.                 connettore.setId(idConnettore);

  616.                 idSoggetto = DBUtils.getIdSoggetto(oldNomeSoggetto, oldTipoSoggetto, con, DriverRegistroServiziDB_LIB.tipoDB);
  617.                 if (idSoggetto <= 0)
  618.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDSoggetto(UPDATE)] Id Soggetto non valido.");

  619.                 index = 1;
  620.                
  621.                 updateStmt.setString(index++, nome);
  622.                 updateStmt.setString(index++, descizione);
  623.                 updateStmt.setString(index++, identificativoPorta);
  624.                 updateStmt.setString(index++, tipo);
  625.                 updateStmt.setString(index++, server);
  626.                 updateStmt.setString(index++, superUser);
  627.                 if(soggetto.getPrivato()!=null && soggetto.getPrivato())
  628.                     updateStmt.setInt(index++, 1);
  629.                 else
  630.                     updateStmt.setInt(index++, 0);
  631.                 updateStmt.setString(index++, soggetto.getVersioneProtocollo());
  632.                 updateStmt.setString(index++, codiceIPA);
  633.                
  634.                 credenziali = (soggetto.sizeCredenzialiList() > 0 ? soggetto.getCredenziali(0) : null);
  635.                 updateStmt.setString(index++, (credenziali != null ? DriverRegistroServiziDB_LIB.getValue(credenziali.getTipo()) : null));
  636.                 updateStmt.setString(index++, (credenziali != null ? credenziali.getUser() : null));
  637.                 updateStmt.setString(index++, (credenziali != null ? credenziali.getPassword() : null));
  638.                
  639.                 subject = null;
  640.                 if(credenziali!=null && credenziali.getSubject()!=null && !"".equals(credenziali.getSubject()))
  641.                     subject = credenziali.getSubject();
  642.                 updateStmt.setString(index++, (subject != null ? CertificateUtils.formatPrincipal(subject, PrincipalType.SUBJECT) : null));
  643.                 subjectCN = null;
  644.                 if(credenziali!=null && credenziali.getCnSubject()!=null && !"".equals(credenziali.getCnSubject()))
  645.                     subjectCN = credenziali.getCnSubject();
  646.                 updateStmt.setString(index++, subjectCN);
  647.                
  648.                 issuer = null;
  649.                 if(credenziali != null && org.openspcoop2.core.registry.constants.CredenzialeTipo.APIKEY.equals(credenziali.getTipo())) {
  650.                     updateStmt.setString(index++, CostantiDB.getIssuerApiKey(credenziali.isAppId()));
  651.                 }
  652.                 else {
  653.                     if(credenziali!=null && credenziali.getIssuer()!=null && !"".equals(credenziali.getIssuer()))
  654.                         issuer = credenziali.getIssuer();
  655.                     updateStmt.setString(index++, (issuer != null ? CertificateUtils.formatPrincipal(issuer, PrincipalType.ISSUER) : null));
  656.                 }
  657.                 issuerCN = null;
  658.                 if(credenziali!=null && credenziali.getCnIssuer()!=null && !"".equals(credenziali.getCnIssuer()))
  659.                     issuerCN = credenziali.getCnIssuer();
  660.                 updateStmt.setString(index++, issuerCN);
  661.                
  662.                 certificate = null;
  663.                 if(credenziali!=null && credenziali.getCertificate()!=null) {
  664.                     certificate = credenziali.getCertificate();
  665.                 }
  666.                 jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(DriverRegistroServiziDB_LIB.tipoDB);
  667.                 jdbcAdapter.setBinaryData(updateStmt, index++, certificate);
  668.                 if(credenziali!=null && credenziali.isCertificateStrictVerification()) {
  669.                     updateStmt.setInt(index++, CostantiDB.TRUE);
  670.                 }              
  671.                 else {
  672.                     updateStmt.setInt(index++, CostantiDB.FALSE);
  673.                 }
  674.                
  675.                 if(soggetto.getOraRegistrazione()!=null){
  676.                     updateStmt.setTimestamp(index++, new Timestamp(soggetto.getOraRegistrazione().getTime()));
  677.                 }
  678.                
  679.                 if(utenteUltimaModifica!=null) {
  680.                     updateStmt.setString(index++, utenteUltimaModifica);
  681.                 }
  682.                
  683.                 if(dataUltimaModifica!=null) {
  684.                     updateStmt.setTimestamp(index++, dataUltimaModifica);
  685.                 }
  686.                
  687.                 updateStmt.setLong(index++, idSoggetto);

  688.                 // eseguo lo statement
  689.                 n = updateStmt.executeUpdate();
  690.                 updateStmt.close();
  691.                 DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + n);
  692.                 // modifico i dati del connettore
  693.                 //setto il nuovo nome
  694.                 String newNomeConnettore = "CNT_" + tipo + "_" + nome;
  695.                 connettore.setNome(newNomeConnettore);
  696.                 DriverRegistroServiziDB_connettoriLIB.CRUDConnettore(2, connettore, con, driverBYOK);

  697.                 DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto UPDATE : \n" + DriverRegistroServiziDB_LIB.formatSQLString(updateQuery, nome, descizione, identificativoPorta, tipo, idSoggetto));

  698.                
  699.                 // Ruoli
  700.                
  701.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  702.                 sqlQueryObject.addDeleteTable(CostantiDB.SOGGETTI_RUOLI);
  703.                 sqlQueryObject.addWhereCondition("id_soggetto=?");
  704.                 updateQuery = sqlQueryObject.createSQLDelete();
  705.                 updateStmt = con.prepareStatement(updateQuery);
  706.                 updateStmt.setLong(1, idSoggetto);
  707.                 n = updateStmt.executeUpdate();
  708.                 updateStmt.close();
  709.                 DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + n+" delete roles");
  710.                
  711.                 n = 0;
  712.                 if(soggetto.getRuoli()!=null && soggetto.getRuoli().sizeRuoloList()>0){
  713.                     for (int i = 0; i < soggetto.getRuoli().sizeRuoloList(); i++) {
  714.                         RuoloSoggetto ruoloSoggetto = soggetto.getRuoli().getRuolo(i);
  715.                        
  716.                         IDRuolo idRuoloObject= new IDRuolo(ruoloSoggetto.getNome());
  717.                         long idRuolo = DBUtils.getIdRuolo(idRuoloObject, con, DriverRegistroServiziDB_LIB.tipoDB);
  718.                        
  719.                         sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  720.                         sqlQueryObject.addInsertTable(CostantiDB.SOGGETTI_RUOLI);
  721.                         sqlQueryObject.addInsertField("id_soggetto", "?");
  722.                         sqlQueryObject.addInsertField("id_ruolo", "?");
  723.                         updateQuery = sqlQueryObject.createSQLInsert();
  724.                         updateStmt = con.prepareStatement(updateQuery);
  725.                        
  726.                         updateStmt.setLong(1, idSoggetto);
  727.                         updateStmt.setLong(2, idRuolo);
  728.                        
  729.                         int r = updateStmt.executeUpdate();
  730.                         n++;
  731.                         updateStmt.close();
  732.                         DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + r+" create role ["+ruoloSoggetto.getNome()+"]");
  733.                     }
  734.                 }
  735.                
  736.                 DriverRegistroServiziDB_LIB.logDebug("Aggiunti " + n + " ruoli al soggetto "+idSoggetto);
  737.                
  738.                
  739.                 // credenziali (le credenziali in questa tabella partono dal numero maggiore di 1)

  740.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  741.                 sqlQueryObject.addDeleteTable(CostantiDB.SOGGETTI_CREDENZIALI);
  742.                 sqlQueryObject.addWhereCondition("id_soggetto=?");
  743.                 updateQuery = sqlQueryObject.createSQLDelete();
  744.                 updateStmt = con.prepareStatement(updateQuery);
  745.                 updateStmt.setLong(1, idSoggetto);
  746.                 n = updateStmt.executeUpdate();
  747.                 updateStmt.close();
  748.                 DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + n+" delete roles");

  749.                 n = 0;
  750.                 if(soggetto.sizeCredenzialiList()>1){
  751.                     for (int i = 1; i < soggetto.sizeCredenzialiList(); i++) {
  752.                         CredenzialiSoggetto credenzialiSoggetto = soggetto.getCredenziali(i);
  753.                        
  754.                         sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  755.                         sqlQueryObject.addInsertTable(CostantiDB.SOGGETTI_CREDENZIALI);
  756.                         sqlQueryObject.addInsertField("id_soggetto", "?");
  757.                         sqlQueryObject.addInsertField("subject", "?");
  758.                         sqlQueryObject.addInsertField("cn_subject", "?");
  759.                         sqlQueryObject.addInsertField("issuer", "?");
  760.                         sqlQueryObject.addInsertField("cn_issuer", "?");
  761.                         sqlQueryObject.addInsertField("certificate", "?");
  762.                         sqlQueryObject.addInsertField("cert_strict_verification", "?");
  763.                         updateQuery = sqlQueryObject.createSQLInsert();
  764.                         updateStmt = con.prepareStatement(updateQuery);
  765.                        
  766.                         index = 1;
  767.                         updateStmt.setLong(index++, idSoggetto);
  768.                        
  769.                         String subjectCredenziali = null;
  770.                         if(credenzialiSoggetto!=null && credenzialiSoggetto.getSubject()!=null && !"".equals(credenzialiSoggetto.getSubject()))
  771.                             subjectCredenziali = credenzialiSoggetto.getSubject();
  772.                         updateStmt.setString(index++, (subjectCredenziali != null ? CertificateUtils.formatPrincipal(subjectCredenziali, PrincipalType.SUBJECT) : null));
  773.                         String subjectCredenzialiCN = null;
  774.                         if(credenzialiSoggetto!=null && credenzialiSoggetto.getCnSubject()!=null && !"".equals(credenzialiSoggetto.getCnSubject()))
  775.                             subjectCredenzialiCN = credenzialiSoggetto.getCnSubject();
  776.                         updateStmt.setString(index++, subjectCredenzialiCN);
  777.                        
  778.                         String issuerCredenziali = null;
  779.                         if(credenzialiSoggetto != null && org.openspcoop2.core.registry.constants.CredenzialeTipo.APIKEY.equals(credenzialiSoggetto.getTipo())) {
  780.                             updateStmt.setString(index++, CostantiDB.getIssuerApiKey(credenzialiSoggetto.isAppId()));
  781.                         }
  782.                         else {
  783.                             if(credenzialiSoggetto!=null && credenzialiSoggetto.getIssuer()!=null && !"".equals(credenzialiSoggetto.getIssuer()))
  784.                                 issuerCredenziali = credenzialiSoggetto.getIssuer();
  785.                             updateStmt.setString(index++, (issuerCredenziali != null ? CertificateUtils.formatPrincipal(issuerCredenziali, PrincipalType.ISSUER) : null));
  786.                         }
  787.                         String issuerCredenzialiCN = null;
  788.                         if(credenzialiSoggetto!=null && credenzialiSoggetto.getCnIssuer()!=null && !"".equals(credenzialiSoggetto.getCnIssuer()))
  789.                             issuerCredenzialiCN = credenzialiSoggetto.getCnIssuer();
  790.                         updateStmt.setString(index++, issuerCredenzialiCN);
  791.                        
  792.                         byte [] certificateCredenziali = null;
  793.                         if(credenzialiSoggetto!=null && credenzialiSoggetto.getCertificate()!=null) {
  794.                             certificateCredenziali = credenzialiSoggetto.getCertificate();
  795.                         }
  796.                         jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(DriverRegistroServiziDB_LIB.tipoDB);
  797.                         jdbcAdapter.setBinaryData(updateStmt, index++, certificateCredenziali);
  798.                         if(credenzialiSoggetto!=null && credenzialiSoggetto.isCertificateStrictVerification()) {
  799.                             updateStmt.setInt(index++, CostantiDB.TRUE);
  800.                         }              
  801.                         else {
  802.                             updateStmt.setInt(index++, CostantiDB.FALSE);
  803.                         }
  804.                        
  805.                         int r = updateStmt.executeUpdate();
  806.                         n++;
  807.                         updateStmt.close();
  808.                         DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + r+" create credenziale");
  809.                     }
  810.                    
  811.                 }
  812.                
  813.                 DriverRegistroServiziDB_LIB.logDebug("Aggiunte " + n + " credenziali al soggetto "+idSoggetto);
  814.                
  815.                
  816.                 // properties
  817.                
  818.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  819.                 sqlQueryObject.addDeleteTable(CostantiDB.SOGGETTI_PROPS);
  820.                 sqlQueryObject.addWhereCondition("id_soggetto=?");
  821.                 updateQuery = sqlQueryObject.createSQLDelete();
  822.                 updateStmt = con.prepareStatement(updateQuery);
  823.                 updateStmt.setLong(1, idSoggetto);
  824.                 n = updateStmt.executeUpdate();
  825.                 updateStmt.close();
  826.                 DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + n+" delete properties");
  827.                
  828.                 n = 0;
  829.                 if(soggetto.sizeProprietaList()>0){
  830.                     for (int i = 0; i < soggetto.sizeProprietaList(); i++) {
  831.                        
  832.                         Proprieta proprieta = soggetto.getProprieta(i);
  833.                        
  834.                         sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  835.                         sqlQueryObject.addInsertTable(CostantiDB.SOGGETTI_PROPS);
  836.                         sqlQueryObject.addInsertField("id_soggetto", "?");
  837.                         sqlQueryObject.addInsertField("nome", "?");
  838.                         sqlQueryObject.addInsertField("valore", "?");
  839.                         sqlQueryObject.addInsertField("enc_value", "?");
  840.                         updateQuery = sqlQueryObject.createSQLInsert();
  841.                         updateStmt = con.prepareStatement(updateQuery);
  842.                        
  843.                         int indexP = 1;
  844.                         updateStmt.setLong(indexP++, idSoggetto);
  845.                         updateStmt.setString(indexP++, proprieta.getNome());

  846.                         String plainValue = proprieta.getValore();
  847.                         String encValue = null;
  848.                         if(driverBYOK!=null && BYOKUtilities.isWrappedValue(plainValue) ) {
  849.                             BYOKWrappedValue byokValue = driverBYOK.wrap(plainValue);
  850.                             if(byokValue!=null) {
  851.                                 encValue = byokValue.getWrappedValue();
  852.                                 plainValue = byokValue.getWrappedPlainValue();
  853.                             }
  854.                         }
  855.                        
  856.                         updateStmt.setString(indexP++, plainValue);
  857.                         updateStmt.setString(indexP++, encValue);
  858.                        
  859.                         int r= updateStmt.executeUpdate();
  860.                         n++;
  861.                         updateStmt.close();
  862.                         DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + r+" create property ["+proprieta.getNome()+"]");
  863.                     }
  864.                 }
  865.                
  866.                 DriverRegistroServiziDB_LIB.logDebug("Aggiunti " + n + " proprietà al soggetto "+idSoggetto);
  867.                
  868.                
  869.                 // ProtocolProperties
  870.                 DriverRegistroServiziDB_LIB.CRUDProtocolProperty(CostantiDB.UPDATE, soggetto.getProtocolPropertyList(),
  871.                         idSoggetto, ProprietariProtocolProperty.SOGGETTO, con, tipoDatabase, driverBYOK);
  872.                
  873.                
  874.                 break;

  875.             case DELETE:
  876.                                
  877.                 // DELETE

  878.                 idSoggetto = DBUtils.getIdSoggetto(nome, tipo, con, DriverRegistroServiziDB_LIB.tipoDB);
  879.                 idConnettore = DriverRegistroServiziDB_LIB.getIdConnettoreSoggetto(nome, tipo, con);
  880.                 if (idSoggetto <= 0)
  881.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDSoggetto(DELETE)] Id Soggetto non valido.");
  882.                
  883.                 // ProtocolProperties
  884.                 DriverRegistroServiziDB_LIB.CRUDProtocolProperty(CostantiDB.DELETE, null,
  885.                         idSoggetto, ProprietariProtocolProperty.SOGGETTO, con, tipoDatabase, driverBYOK);
  886.                
  887.                 // elimino le proprieta' del soggetto
  888.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  889.                 sqlQueryObject.addDeleteTable(CostantiDB.SOGGETTI_PROPS);
  890.                 sqlQueryObject.addWhereCondition("id_soggetto=?");
  891.                 updateQuery = sqlQueryObject.createSQLDelete();
  892.                 updateStmt = con.prepareStatement(updateQuery);
  893.                 updateStmt.setLong(1, idSoggetto);
  894.                 n = updateStmt.executeUpdate();
  895.                 updateStmt.close();
  896.                 DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + n+" delete properties");
  897.                
  898.                 // elimino le credenziali del soggetto
  899.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  900.                 sqlQueryObject.addDeleteTable(CostantiDB.SOGGETTI_CREDENZIALI);
  901.                 sqlQueryObject.addWhereCondition("id_soggetto=?");
  902.                 updateQuery = sqlQueryObject.createSQLDelete();
  903.                 updateStmt = con.prepareStatement(updateQuery);
  904.                 updateStmt.setLong(1, idSoggetto);
  905.                 n = updateStmt.executeUpdate();
  906.                 updateStmt.close();
  907.                 DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + n+" delete credentials");
  908.                
  909.                 // elimino i ruoli del soggetto
  910.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  911.                 sqlQueryObject.addDeleteTable(CostantiDB.SOGGETTI_RUOLI);
  912.                 sqlQueryObject.addWhereCondition("id_soggetto=?");
  913.                 updateQuery = sqlQueryObject.createSQLDelete();
  914.                 updateStmt = con.prepareStatement(updateQuery);
  915.                 updateStmt.setLong(1, idSoggetto);
  916.                 n = updateStmt.executeUpdate();
  917.                 updateStmt.close();
  918.                 DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + n+" delete roles");
  919.                
  920.                 // elimino il soggetto
  921.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  922.                 sqlQueryObject.addDeleteTable(CostantiDB.SOGGETTI);
  923.                 sqlQueryObject.addWhereCondition("id=?");
  924.                 String sqlQuery = sqlQueryObject.createSQLDelete();
  925.                 updateStmt = con.prepareStatement(sqlQuery);
  926.                 updateStmt.setLong(1, idSoggetto);
  927.                 n=updateStmt.executeUpdate();
  928.                 updateStmt.close();
  929.                 DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto type = " + type + " row affected =" + n);

  930.                 // elimino il connettore
  931.                 connettore=new Connettore();
  932.                 connettore.setId(idConnettore);
  933.                 DriverRegistroServiziDB_connettoriLIB.CRUDConnettore(3, connettore, con, driverBYOK);

  934.                 DriverRegistroServiziDB_LIB.logDebug("CRUDSoggetto DELETE : \n" + DriverRegistroServiziDB_LIB.formatSQLString(updateQuery, idSoggetto));

  935.                 break;
  936.             }

  937.             if (type == CostantiDB.CREATE) {
  938.                 return idSoggetto;
  939.             } else {
  940.                 return n;
  941.             }

  942.         } catch (CoreException e) {
  943.             throw new DriverRegistroServiziException(e);
  944.         } catch (SQLException se) {
  945.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDSoggetto] SQLException [" + se.getMessage() + "].",se);
  946.         }catch (Exception se) {
  947.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDSoggetto] Exception [" + se.getMessage() + "].",se);
  948.         } finally {
  949.             JDBCUtilities.closeResources(updateStmt);
  950.             JDBCUtilities.closeResources(selectRS, selectStmt);
  951.         }

  952.     }

  953.    
  954. }