DBPropertiesUtils.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.mvc.properties.utils;

  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Properties;

  29. import org.apache.commons.lang.StringUtils;
  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.config.driver.db.DriverConfigurazioneDB_genericPropertiesDriver;
  34. import org.openspcoop2.utils.sql.ISQLQueryObject;
  35. import org.openspcoop2.utils.sql.SQLObjectFactory;

  36. /**    
  37.  * DBPropertiesUtils
  38.  *
  39.  * @author Poli Andrea (poli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public class DBPropertiesUtils {

  44.     private DBPropertiesUtils() {}
  45.    
  46.     public static Map<String, String> readProperties(Connection con, String tipoDB, String nomeTabella, String nomeColonnaKey, String nomeColonnaValue, String nomeColonnaEncValue,
  47.             String nomeColonnaParent, Long idParent, IDriverBYOK driverBYOK) throws CoreException{
  48.         String methodName = "readProperties Tabella["+nomeTabella+"], NomeColonnaKey["+nomeColonnaKey+"], NomeColonnaValue["+nomeColonnaValue+"], nomeColonnaEncValue["+nomeColonnaEncValue+"], NomeColonnaParent["+nomeColonnaParent+"], IDParent["+idParent+"]";
  49.         Map<String, String> map = new HashMap<>();

  50.         PreparedStatement stmt = null;
  51.         ResultSet risultato = null;
  52.         try {
  53.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  54.             sqlQueryObject.addFromTable(nomeTabella);
  55.             sqlQueryObject.addSelectField(nomeTabella +"." + nomeColonnaKey);
  56.             sqlQueryObject.addSelectField(nomeTabella +"." + nomeColonnaValue);
  57.             sqlQueryObject.addSelectField(nomeTabella +"." + nomeColonnaEncValue);
  58.             sqlQueryObject.addWhereCondition(nomeTabella +"." + nomeColonnaParent +" = ?");
  59.             sqlQueryObject.addOrderBy(nomeTabella +"." + nomeColonnaKey);
  60.             sqlQueryObject.setSortType(true);

  61.             String queryString = sqlQueryObject.createSQLQuery();

  62.             stmt = con.prepareStatement(queryString);

  63.             int index = 1;
  64.             stmt.setLong(index++, idParent);

  65.             risultato = stmt.executeQuery();

  66.             while (risultato.next()) {
  67.                 String key = risultato.getString(nomeColonnaKey);
  68.                
  69.                 String value = null;
  70.                 String plainValue = risultato.getString(nomeColonnaValue);
  71.                 String encValue = risultato.getString(nomeColonnaEncValue);
  72.                 if(encValue!=null && StringUtils.isNotEmpty(encValue)) {
  73.                     if(driverBYOK!=null) {
  74.                         value = driverBYOK.unwrapAsString(encValue);
  75.                     }
  76.                     else {
  77.                         value = encValue;
  78.                     }
  79.                 }
  80.                 else {
  81.                     value = plainValue;
  82.                 }

  83.                 map.put(key, value);
  84.             }

  85.             return map;

  86.         }catch(Exception e){
  87.             throw new CoreException(methodName + " error",e);
  88.         } finally {

  89.             //Chiudo statement and resultset
  90.             try{
  91.                 if(risultato!=null) risultato.close();
  92.             }catch (Exception e) {
  93.                 //ignore
  94.             }
  95.             try{
  96.                 if(stmt!=null) stmt.close();
  97.             }catch (Exception e) {
  98.                 //ignore
  99.             }

  100.         }
  101.     }

  102.     public static void writeProperties(Connection con, String tipoDB, Map<String, String> map, String nomeTabella, String nomeColonnaKey, String nomeColonnaValue, String nomeColonnaEncValue,
  103.             String nomeColonnaParent, Long idParent, IDriverBYOK driverBYOK, String tipoParent) throws CoreException{
  104.         String methodName = "writeProperties Tabella["+nomeTabella+"], NomeColonnaKey["+nomeColonnaKey+"], NomeColonnaValue["+nomeColonnaValue+"], nomeColonnaEncValue["+nomeColonnaEncValue+"], NomeColonnaParent["+nomeColonnaParent+"], IDParent["+idParent+"]";

  105.         PreparedStatement stmt = null;

  106.         if(idParent <=0){
  107.             throw new CoreException("IdParent non fornito");
  108.         }

  109.         try {
  110.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  111.             sqlQueryObject.addInsertTable(nomeTabella);
  112.             sqlQueryObject.addInsertField(nomeColonnaKey, "?");
  113.             sqlQueryObject.addInsertField(nomeColonnaValue, "?");
  114.             sqlQueryObject.addInsertField(nomeColonnaEncValue, "?");
  115.             sqlQueryObject.addInsertField(nomeColonnaParent, "?");
  116.             String queryString = sqlQueryObject.createSQLInsert();

  117.             for (String key : map.keySet()) {
  118.                 stmt = con.prepareStatement(queryString);
  119.                 int index = 1;
  120.                 stmt.setString(index++, key);
  121.                
  122.                 String plainValue = map.get(key);
  123.                 String encValue = null;
  124.                 if(driverBYOK!=null && DriverConfigurazioneDB_genericPropertiesDriver.isConfidentialProperty(tipoParent, key)) {
  125.                     BYOKWrappedValue byokValue = driverBYOK.wrap(plainValue);
  126.                     if(byokValue!=null) {
  127.                         encValue = byokValue.getWrappedValue();
  128.                         plainValue = byokValue.getWrappedPlainValue();
  129.                     }
  130.                 }
  131.                
  132.                 stmt.setString(index++, plainValue);
  133.                 stmt.setString(index++, encValue);
  134.                
  135.                 stmt.setLong(index++, idParent);
  136.                 stmt.executeUpdate();
  137.                 stmt.close();
  138.             }

  139.         }catch(Exception e){
  140.             throw new CoreException(methodName + " error",e);
  141.         } finally {

  142.             try{
  143.                 if(stmt!=null) stmt.close();
  144.             }catch (Exception e) {
  145.                 //ignore
  146.             }

  147.         }
  148.     }

  149.     public static void deleteProperties(Connection con, String tipoDB, String nomeTabella, String nomeColonnaParent, Long idParent) throws CoreException{
  150.         String methodName = "deleteProperties Tabella["+nomeTabella+"], NomeColonnaParent["+nomeColonnaParent+"], IDParent["+idParent+"]";

  151.         PreparedStatement stmt = null;

  152.         if(idParent <=0){
  153.             throw new CoreException("IdParent non fornito");
  154.         }

  155.         try {
  156.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  157.             sqlQueryObject.addDeleteTable(nomeTabella);
  158.             sqlQueryObject.addWhereCondition(nomeTabella +"." + nomeColonnaParent +" = ?");
  159.             sqlQueryObject.setANDLogicOperator(true);
  160.             String queryString = sqlQueryObject.createSQLDelete();

  161.             stmt = con.prepareStatement(queryString);
  162.             stmt.setLong(1, idParent);
  163.             stmt.executeUpdate();
  164.             stmt.close();

  165.         }catch(Exception e){
  166.             throw new CoreException(methodName + " error",e);
  167.         } finally {

  168.             try{
  169.                 if(stmt!=null) stmt.close();
  170.             }catch (Exception e) {
  171.                 //ignore
  172.             }

  173.         }
  174.     }


  175.     public static Map<String, String> toMap(Map<String, Properties> propertiesMap) throws Exception {
  176.         Map<String, String> map = null;
  177.         if(propertiesMap == null)
  178.             return map;

  179.         map = new HashMap<>();

  180.         for (String nomeProperties : propertiesMap.keySet()) {
  181.             Properties properties = propertiesMap.get(nomeProperties);
  182.             for (Object key : properties.keySet()) {
  183.                 String keyAsString = (String) key;
  184.                 String val = properties.getProperty(keyAsString);

  185.                 if(!nomeProperties.equals(Costanti.NOME_MAPPA_PROPERTIES_DEFAULT)) {
  186.                     keyAsString = nomeProperties + Costanti.KEY_PROPERTIES_CUSTOM_SEPARATOR + keyAsString;
  187.                 }

  188.                 map.put(keyAsString, val);
  189.             }
  190.         }

  191.         return map;
  192.     }

  193.     public static Map<String, Properties> toMultiMap(Map<String, String> dbMap) throws Exception {
  194.         List<String> nomiProperties = new ArrayList<>();
  195.        
  196.         for (String key : dbMap.keySet()) {
  197.             String keyToAdd = key;
  198.             if(key.contains(Costanti.KEY_PROPERTIES_CUSTOM_SEPARATOR)) {
  199.                 int idx = key.indexOf(Costanti.KEY_PROPERTIES_CUSTOM_SEPARATOR);
  200.                 keyToAdd = key.substring(0,idx);
  201.             }
  202.            
  203.             if(!nomiProperties.contains(keyToAdd))
  204.                 nomiProperties.add(keyToAdd);
  205.         }
  206.        
  207.         return toMultiMap(dbMap, nomiProperties);
  208.     }
  209.    
  210.    
  211.     public static Map<String, Properties> toMultiMap(Map<String, String> dbMap, List<String> nomiProperties) {
  212.         Map<String, Properties> map = null;
  213.         if(dbMap == null)
  214.             return map;

  215.         map = new HashMap<>();

  216.         for (String key : dbMap.keySet()) {
  217.             String value = dbMap.get(key);
  218.             String nomeProperties = Costanti.NOME_MAPPA_PROPERTIES_DEFAULT;
  219.             String startsWith =  startsWith(nomiProperties, key);
  220.             // ho trovato una property da raggruppare
  221.             if(startsWith != null) {
  222.                 nomeProperties = startsWith;
  223.                 key = normalizePropertyName(nomeProperties, key);
  224.             }

  225.             Properties properties = map.remove(nomeProperties);
  226.             if(properties == null) {
  227.                 properties = new Properties();
  228.             }

  229.             properties.put(key, value);

  230.             map.put(nomeProperties, properties);
  231.         }
  232.         return map;
  233.     }

  234.     public static String startsWith(List<String> nomiProperties, String key) {
  235.         if(nomiProperties != null && !nomiProperties.isEmpty()) {
  236.             for (String string : nomiProperties) {
  237.                 if(key.startsWith(string+Costanti.KEY_PROPERTIES_CUSTOM_SEPARATOR))
  238.                     return string;
  239.             }
  240.         }

  241.         return null;
  242.     }

  243.     public static String normalizePropertyName(String nomiProperties, String key) {
  244.         int prefixLength = nomiProperties.length() + Costanti.KEY_PROPERTIES_CUSTOM_SEPARATOR.length();
  245.         return key.substring(prefixLength);
  246.     }

  247. }