RemoteStoreProviderDriverUtils.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.pdd.core.keystore;

  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.Timestamp;
  26. import java.util.ArrayList;
  27. import java.util.Date;
  28. import java.util.List;

  29. import org.openspcoop2.core.commons.Filtri;
  30. import org.openspcoop2.core.commons.ISearch;
  31. import org.openspcoop2.core.commons.Liste;
  32. import org.openspcoop2.core.commons.SearchUtils;
  33. import org.openspcoop2.core.config.driver.db.DriverConfigurazioneDB;
  34. import org.openspcoop2.core.constants.CostantiDB;
  35. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  36. import org.openspcoop2.pdd.config.PDNDConfigUtilities;
  37. import org.openspcoop2.utils.TipiDatabase;
  38. import org.openspcoop2.utils.UtilsException;
  39. import org.openspcoop2.utils.certificate.remote.RemoteStoreClientInfo;
  40. import org.openspcoop2.utils.certificate.remote.RemoteStoreConfig;
  41. import org.openspcoop2.utils.date.DateManager;
  42. import org.openspcoop2.utils.date.DateUtils;
  43. import org.openspcoop2.utils.jdbc.CustomKeyGeneratorObject;
  44. import org.openspcoop2.utils.jdbc.IJDBCAdapter;
  45. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKey;
  46. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKeyJDBCType;
  47. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKeyObject;
  48. import org.openspcoop2.utils.jdbc.JDBCAdapterFactory;
  49. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  50. import org.openspcoop2.utils.json.JsonPathExpressionEngine;
  51. import org.openspcoop2.utils.sql.ISQLQueryObject;
  52. import org.openspcoop2.utils.sql.LikeConfig;
  53. import org.openspcoop2.utils.sql.SQLObjectFactory;
  54. import org.openspcoop2.utils.sql.SQLQueryObjectException;
  55. import org.slf4j.Logger;

  56. /**
  57.  * RemoteStoreProviderDriverUtils
  58.  *
  59.  * @author Poli Andrea (apoli@link.it)
  60.  * @author $Author$
  61.  * @version $Rev$, $Date$
  62.  */
  63. public class RemoteStoreProviderDriverUtils {
  64.    
  65.     private RemoteStoreProviderDriverUtils() {}

  66.     private static final String COLUMN_ID = "id";
  67.     private static final String COLUMN_NOME = "nome";
  68.     private static final String COLUMN_DATA_REGISTRAZIONE = "data_registrazione";
  69.     private static final String COLUMN_DATA_AGGIORNAMENTO = "data_aggiornamento";
  70.     private static final String COLUMN_LAST_EVENT = "last_event";
  71.    
  72.     private static final String COLUMN_ID_REMOTE_STORE = "id_remote_store";
  73.     private static final String COLUMN_KID = "kid";
  74.     private static final String COLUMN_KEY = "content_key";
  75.    
  76.     private static final String COLUMN_CLIENT_ID = "client_id";
  77.     private static final String COLUMN_CLIENT_DETAILS = "client_details";
  78.     private static final String COLUMN_ORGANIZATION_DETAILS = "organization_details";
  79.     private static final String COLUMN_CLIENT_DATA_AGGIORNAMENTO = "client_data_aggiornamento";
  80.    
  81.     private static final String SUFFIX_NOT_FOUND = " not found";
  82.    
  83.     public static long registerIfNotExistsRemoteStore(DriverConfigurazioneDB driverConfigurazioneDB, RemoteStoreConfig remoteStoreConfig) throws KeystoreException {
  84.         long idRemoteStore = getIdRemoteStore(driverConfigurazioneDB, remoteStoreConfig);
  85.         if(idRemoteStore<=0) {
  86.             return createRemoteStore(driverConfigurazioneDB, remoteStoreConfig);
  87.         }
  88.         return idRemoteStore;
  89.     }
  90.     public static long registerIfNotExistsRemoteStore(Connection con, String tipoDatabase, RemoteStoreConfig remoteStoreConfig) throws KeystoreException {
  91.         long idRemoteStore = getIdRemoteStore(con, tipoDatabase, remoteStoreConfig);
  92.         if(idRemoteStore<=0) {
  93.             return createRemoteStore(con, tipoDatabase, remoteStoreConfig);
  94.         }
  95.         return idRemoteStore;
  96.     }
  97.    
  98.     public static long createRemoteStore(DriverConfigurazioneDB driverConfigurazioneDB, RemoteStoreConfig remoteStoreConfig) throws KeystoreException {
  99.        
  100.         Connection con = null;
  101.         try {
  102.             con = driverConfigurazioneDB.getConnection("createRemoteStore");
  103.             return createRemoteStore(con, driverConfigurazioneDB.getTipoDB(), remoteStoreConfig);
  104.         }
  105.         catch(Exception e) {
  106.             throw new KeystoreException(e.getMessage(),e);
  107.         }
  108.         finally {
  109.             driverConfigurazioneDB.releaseConnection(con);
  110.         }
  111.        
  112.     }
  113.     public static long createRemoteStore(Connection con, String tipoDatabase, RemoteStoreConfig remoteStoreConfig) throws KeystoreException {
  114.        
  115.         try {
  116.             String remoteStoreName = RemoteStoreProviderDriver.getRemoteStoreConfigName(remoteStoreConfig);
  117.            
  118.             Timestamp now = DateManager.getTimestamp();
  119.            
  120.             List<InsertAndGeneratedKeyObject> listInsertAndGeneratedKeyObject = new ArrayList<>();
  121.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(COLUMN_NOME, remoteStoreName, InsertAndGeneratedKeyJDBCType.STRING) );
  122.             listInsertAndGeneratedKeyObject.add( new InsertAndGeneratedKeyObject(COLUMN_DATA_AGGIORNAMENTO, now, InsertAndGeneratedKeyJDBCType.TIMESTAMP) );
  123.            
  124.             long idRemoteStore = InsertAndGeneratedKey.insertAndReturnGeneratedKey(con, TipiDatabase.toEnumConstant(tipoDatabase),
  125.                     new CustomKeyGeneratorObject(CostantiDB.REMOTE_STORE, CostantiDB.REMOTE_STORE_COLUMN_ID,
  126.                             CostantiDB.REMOTE_STORE_SEQUENCE, CostantiDB.REMOTE_STORE_TABLE_FOR_ID),
  127.                     listInsertAndGeneratedKeyObject.toArray(new InsertAndGeneratedKeyObject[1]));
  128.             if(idRemoteStore<=0){
  129.                 throw new KeystoreException("ID (RemoteStore) autoincrementale non ottenuto");
  130.             }
  131.             return idRemoteStore;
  132.         }
  133.         catch(Exception e) {
  134.             throw new KeystoreException(e.getMessage(),e);
  135.         }
  136.        
  137.     }
  138.    
  139.     public static long getIdRemoteStore(DriverConfigurazioneDB driverConfigurazioneDB, RemoteStoreConfig remoteStoreConfig) throws KeystoreException {
  140.         Connection con = null;
  141.         try {
  142.             con = driverConfigurazioneDB.getConnection("getIdRemoteStore");
  143.             return getIdRemoteStore(con, driverConfigurazioneDB.getTipoDB(), remoteStoreConfig);
  144.         }
  145.         catch(Exception e) {
  146.             throw new KeystoreException(e.getMessage(),e);
  147.         }
  148.         finally {
  149.             driverConfigurazioneDB.releaseConnection(con);
  150.         }
  151.     }
  152.     public static long getIdRemoteStore(Connection con, String tipoDatabase, RemoteStoreConfig remoteStoreConfig) throws KeystoreException {
  153.        
  154.         PreparedStatement selectStmt = null;
  155.         ResultSet selectRS = null;
  156.         long idRemoteStore = -1;
  157.         try {
  158.             String remoteStoreName = RemoteStoreProviderDriver.getRemoteStoreConfigName(remoteStoreConfig);
  159.            
  160.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  161.             sqlQueryObject.addFromTable(CostantiDB.REMOTE_STORE);
  162.             sqlQueryObject.addSelectField(COLUMN_ID);
  163.             sqlQueryObject.addWhereCondition(COLUMN_NOME+"=?");
  164.             sqlQueryObject.setANDLogicOperator(true);
  165.             String sqlQuery = sqlQueryObject.createSQLQuery();
  166.             selectStmt = con.prepareStatement(sqlQuery);
  167.             selectStmt.setString(1, remoteStoreName);
  168.             selectRS = selectStmt.executeQuery();
  169.             if(selectRS.next()) {
  170.                 idRemoteStore = selectRS.getLong(COLUMN_ID);
  171.             }
  172.         }
  173.         catch(Exception e) {
  174.             throw new KeystoreException(e.getMessage(),e);
  175.         }
  176.         finally {
  177.            
  178.             JDBCUtilities.closeResources(selectRS, selectStmt);
  179.         }
  180.        
  181.         return idRemoteStore;
  182.     }
  183.    
  184.    
  185.    
  186.    
  187.     public static RemoteStore getRemoteStore(DriverConfigurazioneDB driverConfigurazioneDB, RemoteStoreConfig remoteStoreConfig, boolean throwExceptionNotFound) throws KeystoreException {
  188.         Connection con = null;
  189.         try {
  190.             con = driverConfigurazioneDB.getConnection("getRemoteStore");
  191.             return getRemoteStore(con, driverConfigurazioneDB.getTipoDB(), remoteStoreConfig, throwExceptionNotFound);
  192.         }
  193.         catch(Exception e) {
  194.             throw new KeystoreException(e.getMessage(),e);
  195.         }
  196.         finally {
  197.             driverConfigurazioneDB.releaseConnection(con);
  198.         }
  199.     }
  200.     public static RemoteStore getRemoteStore(Connection con, String tipoDatabase, RemoteStoreConfig remoteStoreConfig, boolean throwExceptionNotFound) throws KeystoreException {
  201.        
  202.         PreparedStatement selectStmt = null;
  203.         ResultSet selectRS = null;
  204.         RemoteStore remoteStore = null;
  205.         try {
  206.             String remoteStoreName = RemoteStoreProviderDriver.getRemoteStoreConfigName(remoteStoreConfig);
  207.            
  208.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  209.             sqlQueryObject.addFromTable(CostantiDB.REMOTE_STORE);
  210.             sqlQueryObject.addSelectField("*");
  211.             sqlQueryObject.addWhereCondition(COLUMN_NOME+"=?");
  212.             sqlQueryObject.setANDLogicOperator(true);
  213.             String sqlQuery = sqlQueryObject.createSQLQuery();
  214.             selectStmt = con.prepareStatement(sqlQuery);
  215.             selectStmt.setString(1, remoteStoreName);
  216.             selectRS = selectStmt.executeQuery();
  217.             if(selectRS.next()) {
  218.                 remoteStore = new RemoteStore();
  219.                 remoteStore.setId(selectRS.getLong(COLUMN_ID));
  220.                 remoteStore.setDataAggiornamento(selectRS.getTimestamp(COLUMN_DATA_AGGIORNAMENTO));
  221.                 remoteStore.setLastEvent(selectRS.getString(COLUMN_LAST_EVENT));
  222.                 remoteStore.setNome(selectRS.getString(COLUMN_NOME));
  223.             }
  224.             else if(throwExceptionNotFound){
  225.                 throw new KeystoreException("RemoteStore '"+remoteStoreName+"'"+SUFFIX_NOT_FOUND);
  226.             }
  227.         }
  228.         catch(Exception e) {
  229.             throw new KeystoreException(e.getMessage(),e);
  230.         }
  231.         finally {
  232.            
  233.             JDBCUtilities.closeResources(selectRS, selectStmt);
  234.         }
  235.        
  236.         return remoteStore;
  237.     }
  238.    
  239.    
  240.     public static int updateRemoteStore(DriverConfigurazioneDB driverConfigurazioneDB, long idStore, String lastEventId) throws KeystoreException {
  241.         Connection con = null;
  242.         try {
  243.             con = driverConfigurazioneDB.getConnection("updateRemoteStore");
  244.             return updateRemoteStore(con, driverConfigurazioneDB.getTipoDB(), idStore, lastEventId);
  245.         }
  246.         catch(Exception e) {
  247.             throw new KeystoreException(e.getMessage(),e);
  248.         }
  249.         finally {
  250.             driverConfigurazioneDB.releaseConnection(con);
  251.         }
  252.     }
  253.     public static int updateRemoteStore(Connection con, String tipoDatabase, long idStore, String lastEventId) throws KeystoreException {
  254.         PreparedStatement updateStmt = null;
  255.         try {
  256.             checkRemoteStoreParams(idStore, lastEventId);
  257.            
  258.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  259.             sqlQueryObject.addUpdateTable(CostantiDB.REMOTE_STORE);
  260.             sqlQueryObject.addUpdateField(COLUMN_LAST_EVENT, "?");
  261.             sqlQueryObject.addUpdateField(COLUMN_DATA_AGGIORNAMENTO, "?");
  262.             sqlQueryObject.addWhereCondition(COLUMN_ID+"=?");
  263.             sqlQueryObject.setANDLogicOperator(true);
  264.             String updateQuery = sqlQueryObject.createSQLUpdate();
  265.             updateStmt = con.prepareStatement(updateQuery);
  266.             int index = 1;
  267.             Timestamp now = DateManager.getTimestamp();
  268.             updateStmt.setString(index++, lastEventId);
  269.             updateStmt.setTimestamp(index++, now);
  270.             updateStmt.setLong(index++, idStore);
  271.             int rows = updateStmt.executeUpdate();
  272.             updateStmt.close();
  273.             return rows;
  274.         }
  275.         catch(Exception e) {
  276.             throw new KeystoreException(e.getMessage(),e);
  277.         }
  278.         finally {  
  279.             JDBCUtilities.closeResources(updateStmt);
  280.         }
  281.     }
  282.     private static void checkRemoteStoreParams(long idStore, String lastEventId) throws KeystoreException {
  283.         if(idStore<=0) {
  284.             throw new KeystoreException("IdStore undefined");
  285.         }
  286.         if(lastEventId==null) {
  287.             throw new KeystoreException("LastEventId undefined");
  288.         }
  289.     }
  290.    
  291.    
  292.     public static List<RemoteStore> getRemoteStores(DriverConfigurazioneDB driverConfigurazioneDB) throws KeystoreException {
  293.         Connection con = null;
  294.         try {
  295.             con = driverConfigurazioneDB.getConnection("getRemoteStores");
  296.             return getRemoteStores(con, driverConfigurazioneDB.getTipoDB());
  297.         }
  298.         catch(Exception e) {
  299.             throw new KeystoreException(e.getMessage(),e);
  300.         }
  301.         finally {
  302.             driverConfigurazioneDB.releaseConnection(con);
  303.         }
  304.     }
  305.     public static List<RemoteStore> getRemoteStores(Connection con, String tipoDatabase) throws KeystoreException {
  306.        
  307.         List<RemoteStore> list = new ArrayList<>();
  308.         PreparedStatement selectStmt = null;
  309.         ResultSet selectRS = null;
  310.         try {
  311.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  312.             sqlQueryObject.addFromTable(CostantiDB.REMOTE_STORE);
  313.             sqlQueryObject.setANDLogicOperator(true);
  314.             sqlQueryObject.addOrderBy(COLUMN_NOME);
  315.             String sqlQuery = sqlQueryObject.createSQLQuery();
  316.             selectStmt = con.prepareStatement(sqlQuery);
  317.             selectRS = selectStmt.executeQuery();
  318.             while(selectRS.next()) {
  319.                 long idRemoteStore = selectRS.getLong(COLUMN_ID);
  320.                 Date dataAggiornamento = selectRS.getTimestamp(COLUMN_DATA_AGGIORNAMENTO);
  321.                 String lastEvent =  selectRS.getString(COLUMN_LAST_EVENT);
  322.                 String nome = selectRS.getString(COLUMN_NOME);
  323.                 RemoteStore rs = new RemoteStore();
  324.                 rs.setId(idRemoteStore);
  325.                 rs.setDataAggiornamento(dataAggiornamento);
  326.                 rs.setLastEvent(lastEvent);
  327.                 rs.setNome(nome);
  328.                 list.add(rs);
  329.             }
  330.         }
  331.         catch(Exception e) {
  332.             throw new KeystoreException(e.getMessage(),e);
  333.         }
  334.         finally {
  335.            
  336.             JDBCUtilities.closeResources(selectRS, selectStmt);
  337.         }
  338.        
  339.         return list;
  340.     }
  341.    
  342.    
  343.    
  344.    
  345.    
  346.    
  347.     public static int addRemoteStoreKey(DriverConfigurazioneDB driverConfigurazioneDB, long idStore, String kid, byte[] key) throws KeystoreException {
  348.         Connection con = null;
  349.         PreparedStatement updateStmt = null;
  350.         try {
  351.             con = driverConfigurazioneDB.getConnection("addRemoteStoreKey");
  352.            
  353.             checkParams(idStore, kid, key);
  354.            
  355.             IJDBCAdapter jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(driverConfigurazioneDB.getTipoDB());
  356.            
  357.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(driverConfigurazioneDB.getTipoDB());
  358.             sqlQueryObject.addInsertTable(CostantiDB.REMOTE_STORE_KEY);
  359.             sqlQueryObject.addInsertField(COLUMN_ID_REMOTE_STORE, "?");
  360.             sqlQueryObject.addInsertField(COLUMN_KID, "?");
  361.             sqlQueryObject.addInsertField(COLUMN_KEY, "?");
  362.             sqlQueryObject.addInsertField(COLUMN_DATA_REGISTRAZIONE, "?");
  363.             sqlQueryObject.addInsertField(COLUMN_DATA_AGGIORNAMENTO, "?");
  364.             String updateQuery = sqlQueryObject.createSQLInsert();
  365.             updateStmt = con.prepareStatement(updateQuery);
  366.             int index = 1;
  367.             updateStmt.setLong(index++, idStore);
  368.             updateStmt.setString(index++, kid);
  369.             jdbcAdapter.setBinaryData(updateStmt, index++, key);
  370.             Timestamp now = DateManager.getTimestamp();
  371.             updateStmt.setTimestamp(index++, now);
  372.             updateStmt.setTimestamp(index++, now);
  373.             int rows = updateStmt.executeUpdate();
  374.             updateStmt.close();
  375.             return rows;
  376.         }
  377.         catch(Exception e) {
  378.             throw new KeystoreException(e.getMessage(),e);
  379.         }
  380.         finally {
  381.            
  382.             JDBCUtilities.closeResources(updateStmt);
  383.            
  384.             driverConfigurazioneDB.releaseConnection(con);
  385.         }
  386.     }
  387.    
  388.     public static int invalidRemoteStoreKey(DriverConfigurazioneDB driverConfigurazioneDB, long idStore, String kid) throws KeystoreException {
  389.         return updateRemoteStoreKeyEngine(driverConfigurazioneDB, idStore, kid, null, true);
  390.     }
  391.     public static int invalidRemoteStoreKey(Connection con, String tipoDatabase, long idStore, String kid) throws KeystoreException {
  392.         return updateRemoteStoreKeyEngine(con, tipoDatabase, idStore, kid, null, true);
  393.     }
  394.     public static int updateRemoteStoreKey(DriverConfigurazioneDB driverConfigurazioneDB, long idStore, String kid, byte[] key) throws KeystoreException {
  395.         return updateRemoteStoreKeyEngine(driverConfigurazioneDB, idStore, kid, key, false);
  396.     }
  397.     private static Timestamp dataInvalidaScaduta = null;
  398.     static{
  399.         try {
  400.             dataInvalidaScaduta = new Timestamp(DateUtils.getSimpleDateFormatDay().parse("2000-01-01").getTime());
  401.         }catch(Exception e) {
  402.             // ignore
  403.         }
  404.     }
  405.     private static int updateRemoteStoreKeyEngine(DriverConfigurazioneDB driverConfigurazioneDB, long idStore, String kid, byte[] key, boolean invalid) throws KeystoreException {
  406.         Connection con = null;
  407.         try {
  408.             con = driverConfigurazioneDB.getConnection(invalid ? "invalidRemoteStoreKey" : "updateRemoteStoreKey");
  409.             return updateRemoteStoreKeyEngine(con, driverConfigurazioneDB.getTipoDB(), idStore, kid, key, invalid);
  410.         }
  411.         catch(Exception e) {
  412.             throw new KeystoreException(e.getMessage(),e);
  413.         }
  414.         finally {
  415.             driverConfigurazioneDB.releaseConnection(con);
  416.         }
  417.     }
  418.     private static int updateRemoteStoreKeyEngine(Connection con, String tipoDatabase, long idStore, String kid, byte[] key, boolean invalid) throws KeystoreException {
  419.         PreparedStatement updateStmt = null;
  420.         try {
  421.             if(invalid) {
  422.                 checkParams(idStore, kid);  
  423.             }
  424.             else {
  425.                 checkParams(idStore, kid, key);
  426.             }
  427.            
  428.             IJDBCAdapter jdbcAdapter = null;
  429.            
  430.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  431.             sqlQueryObject.addUpdateTable(CostantiDB.REMOTE_STORE_KEY);
  432.             if(!invalid) {
  433.                 sqlQueryObject.addUpdateField(COLUMN_KEY, "?");
  434.                 jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(tipoDatabase);
  435.             }
  436.             sqlQueryObject.addUpdateField(COLUMN_DATA_AGGIORNAMENTO, "?");
  437.             if(invalid) {
  438.                 sqlQueryObject.addUpdateField(COLUMN_CLIENT_DATA_AGGIORNAMENTO, "?");
  439.             }
  440.             addWhereKidConditions(sqlQueryObject);
  441.             sqlQueryObject.setANDLogicOperator(true);
  442.             String updateQuery = sqlQueryObject.createSQLUpdate();
  443.             updateStmt = con.prepareStatement(updateQuery);
  444.             int index = 1;
  445.             Timestamp now = DateManager.getTimestamp();
  446.             if(invalid) {
  447.                 updateStmt.setTimestamp(index++, dataInvalidaScaduta);
  448.                 updateStmt.setTimestamp(index++, dataInvalidaScaduta);
  449.             }
  450.             else {
  451.                 jdbcAdapter.setBinaryData(updateStmt, index++, key);
  452.                 updateStmt.setTimestamp(index++, now);
  453.             }
  454.             updateStmt.setLong(index++, idStore);
  455.             updateStmt.setString(index++, kid);
  456.             int rows = updateStmt.executeUpdate();
  457.             updateStmt.close();
  458.             return rows;
  459.         }
  460.         catch(Exception e) {
  461.             throw new KeystoreException(e.getMessage(),e);
  462.         }
  463.         finally {
  464.             JDBCUtilities.closeResources(updateStmt);
  465.         }
  466.     }
  467.    
  468.     public static int deleteRemoteStoreKey(DriverConfigurazioneDB driverConfigurazioneDB, long idStore, String kid) throws KeystoreException {
  469.         Connection con = null;
  470.         try {
  471.             con = driverConfigurazioneDB.getConnection("deleteRemoteStoreKey");
  472.             return deleteRemoteStoreKey(con, driverConfigurazioneDB.getTipoDB(), idStore, kid);
  473.         }
  474.         catch(Exception e) {
  475.             throw new KeystoreException(e.getMessage(),e);
  476.         }
  477.         finally {
  478.             driverConfigurazioneDB.releaseConnection(con);
  479.         }
  480.     }
  481.     public static int deleteRemoteStoreKey(Connection con, String tipoDatabase, long idStore, String kid) throws KeystoreException {
  482.         PreparedStatement updateStmt = null;
  483.         try {
  484.             checkParams(idStore, kid);
  485.            
  486.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  487.             sqlQueryObject.addDeleteTable(CostantiDB.REMOTE_STORE_KEY);
  488.             addWhereKidConditions(sqlQueryObject);
  489.             sqlQueryObject.setANDLogicOperator(true);
  490.             String updateQuery = sqlQueryObject.createSQLDelete();
  491.             updateStmt = con.prepareStatement(updateQuery);
  492.             updateStmt.setLong(1, idStore);
  493.             updateStmt.setString(2, kid);
  494.             int rows = updateStmt.executeUpdate();
  495.             updateStmt.close();
  496.             return rows;
  497.         }
  498.         catch(Exception e) {
  499.             throw new KeystoreException(e.getMessage(),e);
  500.         }
  501.         finally {
  502.             JDBCUtilities.closeResources(updateStmt);
  503.         }
  504.     }
  505.    
  506.     public static RemoteStoreKey getRemoteStoreKey(DriverConfigurazioneDB driverConfigurazioneDB, long idStore, String kid) throws KeystoreException,KeystoreNotFoundException {
  507.        
  508.         Connection con = null;
  509.         PreparedStatement selectStmt = null;
  510.         ResultSet selectRS = null;
  511.         try {
  512.             con = driverConfigurazioneDB.getConnection("getRemoteStoreKey");
  513.            
  514.             checkParams(idStore, kid);
  515.            
  516.             IJDBCAdapter jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(driverConfigurazioneDB.getTipoDB());
  517.            
  518.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(driverConfigurazioneDB.getTipoDB());
  519.             sqlQueryObject.addFromTable(CostantiDB.REMOTE_STORE_KEY);
  520.             sqlQueryObject.addSelectField(COLUMN_KEY);
  521.             sqlQueryObject.addSelectField(COLUMN_DATA_AGGIORNAMENTO);
  522.             addWhereKidConditions(sqlQueryObject);
  523.             sqlQueryObject.setANDLogicOperator(true);
  524.             String sqlQuery = sqlQueryObject.createSQLQuery();
  525.             selectStmt = con.prepareStatement(sqlQuery);
  526.             selectStmt.setLong(1, idStore);
  527.             selectStmt.setString(2, kid);
  528.             selectRS = selectStmt.executeQuery();
  529.             if(selectRS.next()) {
  530.                 RemoteStoreKey rsk = new RemoteStoreKey();
  531.                 rsk.setKey(jdbcAdapter.getBinaryData(selectRS, COLUMN_KEY));
  532.                 rsk.setDataAggiornamento(selectRS.getTimestamp(COLUMN_DATA_AGGIORNAMENTO));
  533.                 rsk.setInvalid(dataInvalidaScaduta.equals(rsk.getDataAggiornamento()));
  534.                 return rsk;
  535.             }
  536.             throw new KeystoreNotFoundException("Key with kid '"+kid+"'"+SUFFIX_NOT_FOUND);
  537.         }
  538.         catch(KeystoreNotFoundException e) {
  539.             throw e;
  540.         }
  541.         catch(Exception e) {
  542.             throw new KeystoreException(e.getMessage(),e);
  543.         }
  544.         finally {
  545.            
  546.             JDBCUtilities.closeResources(selectRS, selectStmt);
  547.            
  548.             driverConfigurazioneDB.releaseConnection(con);
  549.         }
  550.        
  551.     }
  552.    
  553.     public static boolean existsRemoteStoreKey(DriverConfigurazioneDB driverConfigurazioneDB, long idStore, String kid, boolean checkDates) throws KeystoreException {
  554.        
  555.         Connection con = null;
  556.         try {
  557.             con = driverConfigurazioneDB.getConnection("existsRemoteStoreKey");
  558.             return existsRemoteStoreKey(con, driverConfigurazioneDB.getTipoDB(), idStore, kid, checkDates);
  559.         }
  560.         catch(Exception e) {
  561.             throw new KeystoreException(e.getMessage(),e);
  562.         }
  563.         finally {
  564.             driverConfigurazioneDB.releaseConnection(con);
  565.         }
  566.        
  567.     }
  568.     public static boolean existsRemoteStoreKey(Connection con, String tipoDatabase, long idStore, String kid, boolean checkDates) throws KeystoreException {
  569.        
  570.         PreparedStatement selectStmt = null;
  571.         ResultSet selectRS = null;
  572.         try {
  573.             checkParams(idStore, kid);
  574.            
  575.             Timestamp tooOld = null;
  576.             if(checkDates && RemoteStoreProviderDriver.getKeyMaxLifeMinutes()>0) {
  577.                 long maxLifeSeconds = RemoteStoreProviderDriver.getKeyMaxLifeMinutes() * 60l;
  578.                 long maxLifeMs = maxLifeSeconds * 1000l;
  579.                 tooOld = new Timestamp(DateManager.getTimeMillis()-maxLifeMs);
  580.             }
  581.            
  582.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  583.             sqlQueryObject.addFromTable(CostantiDB.REMOTE_STORE_KEY);
  584.             sqlQueryObject.addSelectField(COLUMN_KEY);
  585.             addWhereKidConditions(sqlQueryObject);
  586.             if(checkDates) {
  587.                 sqlQueryObject.addWhereCondition(COLUMN_DATA_AGGIORNAMENTO+">?");
  588.             }
  589.             sqlQueryObject.setANDLogicOperator(true);
  590.             String sqlQuery = sqlQueryObject.createSQLQuery();
  591.             selectStmt = con.prepareStatement(sqlQuery);
  592.             int index = 1;
  593.             selectStmt.setLong(index++, idStore);
  594.             selectStmt.setString(index++, kid);
  595.             if(checkDates) {
  596.                 if(tooOld!=null) {
  597.                     selectStmt.setTimestamp(index++, tooOld);
  598.                 }
  599.                 else {
  600.                     selectStmt.setTimestamp(index++, dataInvalidaScaduta);
  601.                 }
  602.             }
  603.             selectRS = selectStmt.executeQuery();
  604.             return selectRS.next();
  605.         }
  606.         catch(Exception e) {
  607.             throw new KeystoreException(e.getMessage(),e);
  608.         }
  609.         finally {
  610.             JDBCUtilities.closeResources(selectRS, selectStmt);
  611.         }
  612.        
  613.     }
  614.    
  615.    
  616.     public static RemoteStoreClientDetails getRemoteStoreClientDetails(DriverConfigurazioneDB driverConfigurazioneDB, long idStore, String kid, Logger log,
  617.             boolean createEntryIfNotExists) throws KeystoreException,KeystoreNotFoundException {
  618.        
  619.         Connection con = null;
  620.         try {
  621.             con = driverConfigurazioneDB.getConnection("getRemoteStoreClientDetails");
  622.            
  623.             return getRemoteStoreClientDetails(con, driverConfigurazioneDB.getTipoDB(), idStore, kid, log,
  624.                     createEntryIfNotExists);
  625.         }
  626.         catch(KeystoreNotFoundException e) {
  627.             throw e;
  628.         }
  629.         catch(Exception e) {
  630.             throw new KeystoreException(e.getMessage(),e);
  631.         }
  632.         finally {
  633.             driverConfigurazioneDB.releaseConnection(con);
  634.         }
  635.        
  636.     }
  637.     public static RemoteStoreClientDetails getRemoteStoreClientDetails(Connection con, String tipoDatabase, long idStore, String kid, Logger log,
  638.             boolean createEntryIfNotExists) throws KeystoreException,KeystoreNotFoundException {
  639.        
  640.         PreparedStatement selectStmt = null;
  641.         ResultSet selectRS = null;
  642.         try {
  643.             checkParams(idStore, kid);
  644.            
  645.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  646.             sqlQueryObject.addFromTable(CostantiDB.REMOTE_STORE_KEY);
  647.             sqlQueryObject.addSelectField(COLUMN_CLIENT_ID);
  648.             sqlQueryObject.addSelectField(COLUMN_CLIENT_DETAILS);
  649.             sqlQueryObject.addSelectField(COLUMN_ORGANIZATION_DETAILS);
  650.             sqlQueryObject.addSelectField(COLUMN_CLIENT_DATA_AGGIORNAMENTO);
  651.             addWhereKidConditions(sqlQueryObject);
  652.             sqlQueryObject.setANDLogicOperator(true);
  653.             String sqlQuery = sqlQueryObject.createSQLQuery();
  654.             selectStmt = con.prepareStatement(sqlQuery);
  655.             selectStmt.setLong(1, idStore);
  656.             selectStmt.setString(2, kid);
  657.             selectRS = selectStmt.executeQuery();
  658.             if(selectRS.next()) {
  659.                 RemoteStoreClientDetails rsk = new RemoteStoreClientDetails();
  660.                 rsk.setClientInfo(new RemoteStoreClientInfo());
  661.                 rsk.getClientInfo().setClientId(selectRS.getString(COLUMN_CLIENT_ID));
  662.                 rsk.getClientInfo().setClientDetails(selectRS.getString(COLUMN_CLIENT_DETAILS));
  663.                 if(rsk.getClientInfo().getClientId()!=null && rsk.getClientInfo().getClientDetails()!=null) {
  664.                     String jsonPath = OpenSPCoop2Properties.getInstance().getGestoreChiaviPDNDclientsOrganizationJsonPath();
  665.                     boolean readErrorAbortTransaction = false;
  666.                     String organizationId = PDNDConfigUtilities.readOrganizationId(jsonPath, readErrorAbortTransaction, rsk.getClientInfo().getClientDetails(), log);
  667.                     rsk.getClientInfo().setOrganizationId(organizationId);
  668.                 }
  669.                 rsk.getClientInfo().setOrganizationDetails(selectRS.getString(COLUMN_ORGANIZATION_DETAILS));
  670.                 rsk.setDataAggiornamento(selectRS.getTimestamp(COLUMN_CLIENT_DATA_AGGIORNAMENTO));
  671.                 rsk.setInvalid(dataInvalidaScaduta.equals(rsk.getDataAggiornamento()));
  672.                 return rsk;
  673.             }
  674.            
  675.             if(createEntryIfNotExists) {
  676.                 int row = initializeEmptyEntry(con, tipoDatabase, idStore, kid, log);
  677.                 if(row>0) {
  678.                     return new RemoteStoreClientDetails();
  679.                 }
  680.             }
  681.            
  682.             throw new KeystoreNotFoundException("Key with kid '"+kid+"'"+SUFFIX_NOT_FOUND);
  683.         }
  684.         catch(KeystoreNotFoundException e) {
  685.             throw e;
  686.         }
  687.         catch(Exception e) {
  688.             throw new KeystoreException(e.getMessage(),e);
  689.         }
  690.         finally {
  691.             JDBCUtilities.closeResources(selectRS, selectStmt);
  692.         }
  693.        
  694.     }
  695.    
  696.     private static final byte[] KEY_UNDEFINED = "KEY_UNDEFINED".getBytes();
  697.     private static int initializeEmptyEntry(Connection con, String tipoDB, long idStore, String kid, Logger log) {
  698.         PreparedStatement updateStmt = null;
  699.         try {
  700.             IJDBCAdapter jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(tipoDB);
  701.            
  702.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  703.             sqlQueryObject.addInsertTable(CostantiDB.REMOTE_STORE_KEY);
  704.             sqlQueryObject.addInsertField(COLUMN_ID_REMOTE_STORE, "?");
  705.             sqlQueryObject.addInsertField(COLUMN_KID, "?");
  706.             sqlQueryObject.addInsertField(COLUMN_KEY, "?");
  707.             sqlQueryObject.addInsertField(COLUMN_DATA_REGISTRAZIONE, "?");
  708.             sqlQueryObject.addInsertField(COLUMN_DATA_AGGIORNAMENTO, "?");
  709.             String updateQuery = sqlQueryObject.createSQLInsert();
  710.             updateStmt = con.prepareStatement(updateQuery);
  711.             int index = 1;
  712.             updateStmt.setLong(index++, idStore);
  713.             updateStmt.setString(index++, kid);
  714.             jdbcAdapter.setBinaryData(updateStmt, index++, KEY_UNDEFINED);
  715.             Timestamp now = DateManager.getTimestamp();
  716.             updateStmt.setTimestamp(index++, now);
  717.             updateStmt.setTimestamp(index++, dataInvalidaScaduta);
  718.             int rows = updateStmt.executeUpdate();
  719.             updateStmt.close();
  720.             return rows;
  721.         }
  722.         catch(Exception e) {
  723.             log.error("initializeEmptyEntry failed: "+e.getMessage(),e);
  724.             return -1;
  725.         }
  726.         finally {
  727.             JDBCUtilities.closeResources(updateStmt);
  728.         }
  729.     }
  730.    
  731.     public static int updateRemoteStoreClientDetails(DriverConfigurazioneDB driverConfigurazioneDB, long idStore, String kid, RemoteStoreClientDetails clientDetails) throws KeystoreException {
  732.         Connection con = null;
  733.         try {
  734.             con = driverConfigurazioneDB.getConnection("updateRemoteStoreClientDetails");
  735.             return updateRemoteStoreClientDetails(con, driverConfigurazioneDB.getTipoDB(), idStore, kid, clientDetails);
  736.         }
  737.         catch(Exception e) {
  738.             throw new KeystoreException(e.getMessage(),e);
  739.         }
  740.         finally {
  741.             driverConfigurazioneDB.releaseConnection(con);
  742.         }
  743.     }
  744.     public static int updateRemoteStoreClientDetails(Connection con, String tipoDatabase, long idStore, String kid, RemoteStoreClientDetails clientDetails) throws KeystoreException {
  745.         PreparedStatement updateStmt = null;
  746.         try {
  747.             checkParams(idStore, kid);  

  748.             if(clientDetails==null) {
  749.                 throw new KeystoreException("ClientDetails undefined");
  750.             }
  751.             if(clientDetails.getClientInfo()==null) {
  752.                 throw new KeystoreException("ClientInfo undefined");
  753.             }
  754.            
  755.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  756.             sqlQueryObject.addUpdateTable(CostantiDB.REMOTE_STORE_KEY);
  757.             sqlQueryObject.addUpdateField(COLUMN_CLIENT_ID, "?");
  758.             sqlQueryObject.addUpdateField(COLUMN_CLIENT_DETAILS, "?");
  759.             sqlQueryObject.addUpdateField(COLUMN_ORGANIZATION_DETAILS, "?");
  760.             sqlQueryObject.addUpdateField(COLUMN_CLIENT_DATA_AGGIORNAMENTO, "?");
  761.             addWhereKidConditions(sqlQueryObject);
  762.             sqlQueryObject.setANDLogicOperator(true);
  763.             String updateQuery = sqlQueryObject.createSQLUpdate();
  764.             updateStmt = con.prepareStatement(updateQuery);
  765.             int index = 1;
  766.             updateStmt.setString(index++, clientDetails.getClientInfo().getClientId());
  767.             updateStmt.setString(index++, clientDetails.getClientInfo().getClientDetails());
  768.             updateStmt.setString(index++, clientDetails.getClientInfo().getOrganizationDetails());
  769.             updateStmt.setTimestamp(index++, clientDetails.getDataAggiornamento()!=null ? new Timestamp(clientDetails.getDataAggiornamento().getTime()) : DateManager.getTimestamp());
  770.             updateStmt.setLong(index++, idStore);
  771.             updateStmt.setString(index++, kid);
  772.             int rows = updateStmt.executeUpdate();
  773.             updateStmt.close();
  774.             return rows;
  775.         }
  776.         catch(Exception e) {
  777.             throw new KeystoreException(e.getMessage(),e);
  778.         }
  779.         finally {
  780.             JDBCUtilities.closeResources(updateStmt);
  781.         }
  782.     }

  783.    
  784.     private static void checkParams(long idStore, String kid, byte[] key) throws KeystoreException {
  785.         checkParams(idStore, kid);
  786.        
  787.         if(key==null || key.length<=0) {
  788.             throw new KeystoreException("Key undefined");
  789.         }
  790.     }
  791.     private static void checkParams(long idStore, String kid) throws KeystoreException {
  792.         if(idStore<=0) {
  793.             throw new KeystoreException("IdStore undefined");
  794.         }
  795.         if(kid==null) {
  796.             throw new KeystoreException("Kid undefined");
  797.         }
  798.     }
  799.    
  800.     private static void addWhereKidConditions(ISQLQueryObject sqlQueryObject) throws SQLQueryObjectException {
  801.         sqlQueryObject.addWhereCondition(COLUMN_ID_REMOTE_STORE+"=?");
  802.         sqlQueryObject.addWhereCondition(COLUMN_KID+"=?");
  803.     }
  804.    
  805.    
  806.    
  807.    
  808.    
  809.    
  810.    
  811.    
  812.    
  813.    
  814.    
  815.    
  816.     public static List<RemoteStoreKeyEntry> getRemoteStoreKeyEntries(Logger log, DriverConfigurazioneDB driverConfigurazioneDB, ISearch ricerca, long idRemoteStore) throws KeystoreException {
  817.         Connection con = null;
  818.         try {
  819.             con = driverConfigurazioneDB.getConnection("getRemoteStoreKeyEntries");
  820.             return getRemoteStoreKeyEntries(log, con, driverConfigurazioneDB.getTipoDB(), ricerca, idRemoteStore);
  821.         }
  822.         catch(Exception e) {
  823.             throw new KeystoreException(e.getMessage(),e);
  824.         }
  825.         finally {
  826.             driverConfigurazioneDB.releaseConnection(con);
  827.         }
  828.     }
  829.     public static List<RemoteStoreKeyEntry> getRemoteStoreKeyEntries(Logger log, Connection con, String tipoDatabase, ISearch ricerca, long idRemoteStore) throws KeystoreException {
  830.        
  831.         int idLista = Liste.REMOTE_STORE_KEY;
  832.         int offset;
  833.         int limit;
  834.         limit = ricerca.getPageSize(idLista);
  835.         offset = ricerca.getIndexIniziale(idLista);
  836.        
  837.         if (limit == 0) // con limit
  838.             limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;
  839.        
  840.         String filtroKid = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_REMOTE_STORE_KEY_KID);
  841.         if((filtroKid!=null && "".equals(filtroKid))) {
  842.             filtroKid=null;
  843.         }
  844.        
  845.         String filtroClientId = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_REMOTE_STORE_KEY_CLIENT_ID);
  846.         if((filtroClientId!=null && "".equals(filtroClientId))) {
  847.             filtroClientId=null;
  848.         }
  849.        
  850.         String filtroOrganizzazione = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_REMOTE_STORE_KEY_ORGANIZZAZIONE);
  851.         if((filtroOrganizzazione!=null && "".equals(filtroOrganizzazione))) {
  852.             filtroOrganizzazione=null;
  853.         }

  854.         // count
  855.         PreparedStatement selectStmtCount = null;
  856.         ResultSet selectRSCount = null;
  857.         int size = 0;
  858.         try {
  859.             ISQLQueryObject sqlQueryObject = buildSqlRemoteStoreKeyEntries(tipoDatabase, filtroKid, filtroClientId, filtroOrganizzazione);
  860.             sqlQueryObject.addSelectCountField("somma");
  861.             String sqlQuery = sqlQueryObject.createSQLQuery();
  862.             selectStmtCount = con.prepareStatement(sqlQuery);
  863.             selectStmtCount.setLong(1, idRemoteStore);
  864.             selectRSCount = selectStmtCount.executeQuery();
  865.             if(selectRSCount.next()) {
  866.                 size = selectRSCount.getInt("somma");
  867.                 if(size<0) {
  868.                     size=0;
  869.                 }
  870.                 ricerca.setNumEntries(idLista, size);
  871.             }
  872.         }
  873.         catch(Exception e) {
  874.             throw new KeystoreException(e.getMessage(),e);
  875.         }
  876.         finally {
  877.             JDBCUtilities.closeResources(selectRSCount, selectStmtCount);
  878.         }
  879.        
  880.         List<RemoteStoreKeyEntry> list = new ArrayList<>();
  881.         PreparedStatement selectStmt = null;
  882.         ResultSet selectRS = null;
  883.         try {
  884.             IJDBCAdapter jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(tipoDatabase);
  885.            
  886.             ISQLQueryObject sqlQueryObject = buildSqlRemoteStoreKeyEntries(tipoDatabase, filtroKid, filtroClientId, filtroOrganizzazione);
  887.             sqlQueryObject.addOrderBy(COLUMN_DATA_REGISTRAZIONE, false);
  888.             sqlQueryObject.setOffset(offset);
  889.             sqlQueryObject.setLimit(limit);
  890.             String sqlQuery = sqlQueryObject.createSQLQuery();
  891.             selectStmt = con.prepareStatement(sqlQuery);
  892.             selectStmt.setLong(1, idRemoteStore);
  893.             selectRS = selectStmt.executeQuery();
  894.             while(selectRS.next()) {
  895.                 list.add(readKeyEntry(log, selectRS, jdbcAdapter));
  896.             }
  897.         }
  898.         catch(Exception e) {
  899.             throw new KeystoreException(e.getMessage(),e);
  900.         }
  901.         finally {
  902.            
  903.             JDBCUtilities.closeResources(selectRS, selectStmt);
  904.         }
  905.        
  906.         return list;
  907.     }
  908.    
  909.     private static ISQLQueryObject buildSqlRemoteStoreKeyEntries(String tipoDatabase, String filtroKid, String filtroClientId, String filtroOrganizzazione) throws KeystoreException {
  910.         ISQLQueryObject sqlQueryObject = null;
  911.         try {
  912.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  913.             sqlQueryObject.addFromTable(CostantiDB.REMOTE_STORE_KEY);
  914.             sqlQueryObject.addWhereCondition(COLUMN_ID_REMOTE_STORE+"=?");
  915.             if(filtroKid!=null) {
  916.                 sqlQueryObject.addWhereLikeCondition(COLUMN_KID, filtroKid, LikeConfig.contains(true));
  917.             }
  918.             if(filtroClientId!=null) {
  919.                 sqlQueryObject.addWhereLikeCondition(COLUMN_CLIENT_ID, filtroClientId, LikeConfig.contains(true));
  920.             }
  921.             if(filtroOrganizzazione!=null) {
  922.                 sqlQueryObject.addWhereLikeCondition(COLUMN_ORGANIZATION_DETAILS, filtroOrganizzazione, LikeConfig.contains(true));
  923.             }
  924.             sqlQueryObject.setANDLogicOperator(true);
  925.         }
  926.         catch(Exception e) {
  927.             throw new KeystoreException(e.getMessage(),e);
  928.         }
  929.         return sqlQueryObject;
  930.     }
  931.    
  932.     private static RemoteStoreKeyEntry readKeyEntry(Logger log, ResultSet selectRS, IJDBCAdapter jdbcAdapter) throws SQLException, UtilsException {
  933.         long id = selectRS.getLong(COLUMN_ID);
  934.         long idStore = selectRS.getLong(COLUMN_ID_REMOTE_STORE);
  935.         Date dataRegistrazione = selectRS.getTimestamp(COLUMN_DATA_REGISTRAZIONE);
  936.        
  937.         String kid = selectRS.getString(COLUMN_KID);
  938.         Date dataAggiornamento = selectRS.getTimestamp(COLUMN_DATA_AGGIORNAMENTO);
  939.        
  940.         String clientId =  selectRS.getString(COLUMN_CLIENT_ID);
  941.         String clientDetails = selectRS.getString(COLUMN_CLIENT_DETAILS);
  942.         String organizationDetails = selectRS.getString(COLUMN_ORGANIZATION_DETAILS);
  943.         Date clientDataAggiornamento = selectRS.getTimestamp(COLUMN_CLIENT_DATA_AGGIORNAMENTO);
  944.        
  945.         RemoteStoreKeyEntry rs = new RemoteStoreKeyEntry();
  946.         rs.setId(id);
  947.         rs.setDataRegistrazione(dataRegistrazione);
  948.         rs.setIdRemoteStore(idStore);
  949.        
  950.         rs.setContentKey(jdbcAdapter.getBinaryData(selectRS, COLUMN_KEY));
  951.         rs.setKid(kid);
  952.         rs.setDataAggiornamento(dataAggiornamento);
  953.        
  954.         rs.setClientId(clientId);
  955.         rs.setClientDetails(clientDetails);
  956.         rs.setOrganizationDetails(organizationDetails);
  957.         rs.setClientDataAggiornamento(clientDataAggiornamento);
  958.        
  959.         enrichOrganizationInfo(log, organizationDetails, rs);
  960.         return rs;
  961.     }
  962.    
  963.     private static void enrichOrganizationInfo(Logger log, String organizationDetails, RemoteStoreKeyEntry rs) {
  964.         if(organizationDetails!=null) {
  965.             try {
  966.                 rs.setOrganizationName(JsonPathExpressionEngine.extractAndConvertResultAsString(organizationDetails, "$.name", log));
  967.             }catch(Exception e) {
  968.                 // ignore
  969.             }
  970.             try {
  971.                 rs.setOrganizationExternalOrigin(JsonPathExpressionEngine.extractAndConvertResultAsString(organizationDetails, "$.externalId.origin", log));
  972.             }catch(Exception e) {
  973.                 // ignore
  974.             }
  975.             try {
  976.                 rs.setOrganizationExternalId(JsonPathExpressionEngine.extractAndConvertResultAsString(organizationDetails, "$.externalId.id", log));
  977.             }catch(Exception e) {
  978.                 // ignore
  979.             }
  980.             try {
  981.                 rs.setOrganizationCategory(JsonPathExpressionEngine.extractAndConvertResultAsString(organizationDetails, "$.category", log));
  982.             }catch(Exception e) {
  983.                 // ignore
  984.             }
  985.         }
  986.     }
  987.    
  988.    
  989.     public static int deleteRemoteStoreKeyEntry(DriverConfigurazioneDB driverConfigurazioneDB, long idRemoteStore, long idEntry) throws KeystoreException {
  990.         Connection con = null;
  991.         try {
  992.             con = driverConfigurazioneDB.getConnection("deleteRemoteStoreKeyEntry");
  993.             return deleteRemoteStoreKeyEntry(con, driverConfigurazioneDB.getTipoDB(), idRemoteStore, idEntry);
  994.         }
  995.         catch(Exception e) {
  996.             throw new KeystoreException(e.getMessage(),e);
  997.         }
  998.         finally {
  999.             driverConfigurazioneDB.releaseConnection(con);
  1000.         }
  1001.     }
  1002.     public static int deleteRemoteStoreKeyEntry(Connection con, String tipoDatabase, long idRemoteStore, long idEntry) throws KeystoreException {
  1003.         PreparedStatement updateStmt = null;
  1004.         try {
  1005.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  1006.             sqlQueryObject.addDeleteTable(CostantiDB.REMOTE_STORE_KEY);
  1007.             sqlQueryObject.addWhereCondition(COLUMN_ID_REMOTE_STORE+"=?");
  1008.             sqlQueryObject.addWhereCondition(COLUMN_ID+"=?");
  1009.             sqlQueryObject.setANDLogicOperator(true);
  1010.             String updateQuery = sqlQueryObject.createSQLDelete();
  1011.             updateStmt = con.prepareStatement(updateQuery);
  1012.             updateStmt.setLong(1, idRemoteStore);
  1013.             updateStmt.setLong(2, idEntry);
  1014.             int rows = updateStmt.executeUpdate();
  1015.             updateStmt.close();
  1016.             return rows;
  1017.         }
  1018.         catch(Exception e) {
  1019.             throw new KeystoreException(e.getMessage(),e);
  1020.         }
  1021.         finally {
  1022.             JDBCUtilities.closeResources(updateStmt);
  1023.         }
  1024.     }
  1025.    
  1026.     public static RemoteStoreKeyEntry getRemoteStoreKeyEntry(Logger log, DriverConfigurazioneDB driverConfigurazioneDB, long idRemoteStoreKey) throws KeystoreException, KeystoreNotFoundException {
  1027.         Connection con = null;
  1028.         try {
  1029.             con = driverConfigurazioneDB.getConnection("getRemoteStoreKeyEntry");
  1030.             return getRemoteStoreKeyEntry(log, con, driverConfigurazioneDB.getTipoDB(), idRemoteStoreKey);
  1031.         }
  1032.         catch(Exception e) {
  1033.             throw new KeystoreException(e.getMessage(),e);
  1034.         }
  1035.         finally {
  1036.             driverConfigurazioneDB.releaseConnection(con);
  1037.         }
  1038.     }
  1039.    
  1040.     public static RemoteStoreKeyEntry getRemoteStoreKeyEntry(Logger log, Connection con, String tipoDatabase, long idRemoteStoreKey) throws KeystoreException,KeystoreNotFoundException {
  1041.        
  1042.         PreparedStatement selectStmt = null;
  1043.         ResultSet selectRS = null;
  1044.         try {
  1045.             IJDBCAdapter jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(tipoDatabase);
  1046.            
  1047.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  1048.             sqlQueryObject.addFromTable(CostantiDB.REMOTE_STORE_KEY);
  1049.             sqlQueryObject.addWhereCondition(COLUMN_ID+"=?");
  1050.             sqlQueryObject.setANDLogicOperator(true);
  1051.             String sqlQuery = sqlQueryObject.createSQLQuery();
  1052.             selectStmt = con.prepareStatement(sqlQuery);
  1053.             selectStmt.setLong(1, idRemoteStoreKey);
  1054.             selectRS = selectStmt.executeQuery();
  1055.             if(selectRS.next()) {
  1056.                 return readKeyEntry(log, selectRS, jdbcAdapter);
  1057.             }
  1058.             throw new KeystoreNotFoundException("Key with id '"+idRemoteStoreKey+"'"+SUFFIX_NOT_FOUND);
  1059.         }
  1060.         catch(KeystoreNotFoundException e) {
  1061.             throw e;
  1062.         }
  1063.         catch(Exception e) {
  1064.             throw new KeystoreException(e.getMessage(),e);
  1065.         }
  1066.         finally {
  1067.             JDBCUtilities.closeResources(selectRS, selectStmt);
  1068.         }
  1069.     }
  1070. }