DBProtocolPropertiesUtils.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.mapping;

  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.util.ArrayList;
  29. import java.util.List;

  30. import org.apache.commons.lang.StringUtils;
  31. import org.openspcoop2.core.byok.BYOKWrappedValue;
  32. import org.openspcoop2.core.byok.IDriverBYOK;
  33. import org.openspcoop2.core.commons.CoreException;
  34. import org.openspcoop2.core.commons.DBUtils;
  35. import org.openspcoop2.core.constants.CostantiDB;
  36. import org.openspcoop2.core.constants.ProprietariProtocolProperty;
  37. import org.openspcoop2.generic_project.exception.NotFoundException;
  38. import org.openspcoop2.utils.TipiDatabase;
  39. import org.openspcoop2.utils.UtilsException;
  40. import org.openspcoop2.utils.jdbc.IJDBCAdapter;
  41. import org.openspcoop2.utils.jdbc.JDBCAdapterException;
  42. import org.openspcoop2.utils.jdbc.JDBCAdapterFactory;
  43. import org.openspcoop2.utils.jdbc.JDBCParameterUtilities;
  44. import org.openspcoop2.utils.sql.ISQLQueryObject;
  45. import org.openspcoop2.utils.sql.SQLObjectFactory;
  46. import org.openspcoop2.utils.sql.SQLQueryObjectException;
  47. import org.slf4j.Logger;

  48. /**
  49.  * Funzioni di utilita utilizzate dai driver
  50.  *
  51.  * @author Andrea Poli (apoli@link.it)
  52.  * @author $Author$
  53.  * @version $Rev$, $Date$
  54.  */
  55. public class DBProtocolPropertiesUtils {
  56.    
  57.     private DBProtocolPropertiesUtils() {}

  58.     private static void logDebug(Logger log, String msg) {
  59.         if(log!=null) {
  60.             log.debug(msg);
  61.         }
  62.     }
  63.    
  64.     private static List<String> protocolPropertiesConfidentials = new ArrayList<>();
  65.     public static List<String> getProtocolPropertiesConfidentials() {
  66.         return protocolPropertiesConfidentials;
  67.     }
  68.     public static void addConfidentialProtocolProperty(String nome){
  69.         if(!protocolPropertiesConfidentials.contains(nome)) {
  70.             protocolPropertiesConfidentials.add(nome);
  71.         }
  72.     }
  73.     public static boolean isConfidentialProtocolProperty(String nome) {
  74.         return protocolPropertiesConfidentials.contains(nome);
  75.     }
  76.    
  77.     public static void crudRegistryProtocolProperty(Logger log, int type, List<org.openspcoop2.core.registry.ProtocolProperty> listPP, long idProprietario,
  78.             ProprietariProtocolProperty tipologiaProprietarioProtocolProperty, Connection connection,
  79.             String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException {
  80.         List<ProtocolProperty> list = null;
  81.         if(listPP!=null) {
  82.             list = new ArrayList<>();
  83.             if(!listPP.isEmpty()) {
  84.                 for (org.openspcoop2.core.registry.ProtocolProperty pp : listPP) {
  85.                     list.add(new ProtocolProperty(pp));
  86.                 }
  87.             }
  88.         }
  89.         crudProtocolProperty(log, type, list, idProprietario, tipologiaProprietarioProtocolProperty, connection, tipoDatabase, driverBYOK);
  90.     }
  91.     public static void crudConfigProtocolProperty(Logger log, int type, List<org.openspcoop2.core.config.ProtocolProperty> listPP, long idProprietario,
  92.             ProprietariProtocolProperty tipologiaProprietarioProtocolProperty, Connection connection,
  93.             String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException {
  94.         List<ProtocolProperty> list = null;
  95.         if(listPP!=null) {
  96.             list = new ArrayList<>();
  97.             if(!listPP.isEmpty()) {
  98.                 for (org.openspcoop2.core.config.ProtocolProperty pp : listPP) {
  99.                     list.add(new ProtocolProperty(pp));
  100.                 }
  101.             }
  102.         }
  103.         crudProtocolProperty(log, type, list, idProprietario, tipologiaProprietarioProtocolProperty, connection, tipoDatabase, driverBYOK);
  104.     }

  105.     private static void crudProtocolProperty(Logger log, int type, List<ProtocolProperty> listPP, long idProprietario,
  106.             ProprietariProtocolProperty tipologiaProprietarioProtocolProperty, Connection connection,
  107.             String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException {
  108.        
  109.         // NOTA: l'update dei documenti, essendo mega di documenti non puo' essere implementata come delete + create
  110.        
  111.         PreparedStatement stm = null;
  112.         ResultSet rs=null;
  113.         String sqlQuery;

  114.         if((listPP == null) && (type!= CostantiDB.DELETE))
  115.             throw new CoreException("[DBProtocolProperties::CRUDProtocolProperty] L'oggetto listPP non puo essere null");
  116.         if(idProprietario <= 0 )
  117.             throw new CoreException("[DBProtocolProperties::CRUDProtocolProperty] id proprietario non definito");
  118.        
  119.         IJDBCAdapter jdbcAdapter = null;
  120.        
  121.         try {

  122.             jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(tipoDatabase);
  123.            
  124.             switch (type) {
  125.             case CREATE:

  126.                 if(listPP!=null) {
  127.                     for(int i=0; i<listPP.size(); i++){
  128.                    
  129.                         ProtocolProperty protocolProperty = listPP.get(i);
  130.                         if(protocolProperty.getName()==null || "".equals(protocolProperty.getName()))
  131.                             throw new CoreException("[DBProtocolProperties::CRUDProtocolProperty] Nome non definito per protocolProperty ["+i+"]");
  132.                        
  133.                         // Aggiorno proprietari
  134.                         protocolProperty.setIdProprietario(idProprietario);
  135.                         protocolProperty.setTipoProprietario(tipologiaProprietarioProtocolProperty.name());
  136.                        
  137.                         int contenutiDefiniti = 0;
  138.                        
  139.                         boolean stringValue = protocolProperty.getValue()!=null && !"".equals(protocolProperty.getValue());
  140.                         String plainContenutoString = null;
  141.                         String encContenutoString = null;
  142.                         if(stringValue){
  143.                             contenutiDefiniti++;
  144.                             plainContenutoString = protocolProperty.getValue();
  145.                             if(driverBYOK!=null && isConfidentialProtocolProperty(protocolProperty.getName())) {
  146.                                 BYOKWrappedValue byokValue = driverBYOK.wrap(protocolProperty.getValue());
  147.                                 if(byokValue!=null) {
  148.                                     encContenutoString = byokValue.getWrappedValue();
  149.                                     plainContenutoString = byokValue.getWrappedPlainValue();
  150.                                 }
  151.                             }
  152.                         }
  153.                        
  154.                         boolean numberValue = protocolProperty.getNumberValue()!=null;
  155.                         Long contenutoNumber = null;
  156.                         if(numberValue){
  157.                             contenutiDefiniti++;
  158.                             contenutoNumber = protocolProperty.getNumberValue();
  159.                         }
  160.                        
  161.                         boolean booleanValue = protocolProperty.getBooleanValue()!=null;
  162.                         Boolean contenutoBoolean = null;
  163.                         if(booleanValue){
  164.                             contenutiDefiniti++;
  165.                             contenutoBoolean = protocolProperty.getBooleanValue();
  166.                         }
  167.                        
  168.                         boolean binaryValue = protocolProperty.getByteFile()!=null && protocolProperty.getByteFile().length>0;
  169.                         byte[] contenutoBinario = null;
  170.                         String contenutoBinarioFileName = null;
  171.                         if(binaryValue){
  172.                             contenutiDefiniti++;
  173.                             contenutoBinario = protocolProperty.getByteFile();
  174.                             if(contenutoBinario.length<3){
  175.                                 String test = new String(contenutoBinario);
  176.                                 if("".equals(test.trim().replace("\n", ""))){
  177.                                     // eliminare \n\n
  178.                                     contenutoBinario = null;    
  179.                                     binaryValue = false;
  180.                                     contenutiDefiniti--;
  181.                                 }
  182.                             }
  183.                             if(binaryValue){
  184.                                 contenutoBinarioFileName = protocolProperty.getFile();
  185.                            
  186.                                 if(driverBYOK!=null && isConfidentialProtocolProperty(protocolProperty.getName())) {
  187.                                     BYOKWrappedValue byokValue = driverBYOK.wrap(contenutoBinario);
  188.                                     if(byokValue!=null) {
  189.                                         contenutoBinario = byokValue.getWrappedValue().getBytes();
  190.                                     }
  191.                                 }
  192.                             }
  193.                         }
  194.                        
  195.                         /**if(!stringValue && !numberValue && !binaryValue && !booleanValue){
  196.                             throw new DriverRegistroServiziException("[DBProtocolProperties::CRUDProtocolProperty] Contenuto non definito per protocolProperty ["+protocolProperty.getName()+"]");
  197.                         }*/
  198.                         // Per fare i filtri con is null e' necessario registrarlo!
  199.                         if(contenutiDefiniti>1){
  200.                             throw new CoreException("[DBProtocolProperties::CRUDProtocolProperty] Contenuto definito con più tipologie per protocolProperty ["+protocolProperty.getName()+
  201.                                     "] (string:"+stringValue+" number:"+numberValue+" binary:"+binaryValue+")");
  202.                         }
  203.                        
  204.                        
  205.                         // create
  206.                         ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  207.                         sqlQueryObject.addInsertTable(CostantiDB.PROTOCOL_PROPERTIES);
  208.                         sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_TIPO_PROPRIETARIO, "?");
  209.                         sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID_PROPRIETARIO, "?");
  210.                         sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_NAME, "?");
  211.                         if(stringValue){
  212.                             sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_STRING, "?");
  213.                             sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_ENCODING_STRING, "?");
  214.                         }
  215.                         if(numberValue){
  216.                             sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_NUMBER, "?");
  217.                         }
  218.                         if(booleanValue){
  219.                             sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_BOOLEAN, "?");
  220.                         }
  221.                         if(binaryValue){
  222.                             sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_BINARY, "?");
  223.                             sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_FILENAME, "?");
  224.                         }
  225.                         sqlQuery = sqlQueryObject.createSQLInsert();
  226.                         stm = connection.prepareStatement(sqlQuery);
  227.                         int index = 1;
  228.                         stm.setString(index++, tipologiaProprietarioProtocolProperty.name());
  229.                         stm.setLong(index++, idProprietario);
  230.                         stm.setString(index++, protocolProperty.getName());
  231.                         String debug = null;
  232.                         if(stringValue){
  233.                             stm.setString(index++, plainContenutoString);
  234.                             debug = plainContenutoString;
  235.                             stm.setString(index++, encContenutoString);
  236.                         }
  237.                         if(numberValue){
  238.                             stm.setLong(index++, contenutoNumber);
  239.                             debug = contenutoNumber+"";
  240.                         }
  241.                         if(booleanValue){
  242.                             if(contenutoBoolean!=null && contenutoBoolean.booleanValue()){
  243.                                 stm.setInt(index++,CostantiDB.TRUE);
  244.                                 debug = CostantiDB.TRUE+"";
  245.                             }
  246.                             else{
  247.                                 stm.setInt(index++,CostantiDB.FALSE);
  248.                                 debug = CostantiDB.FALSE+"";
  249.                             }
  250.                         }
  251.                         if(binaryValue){
  252.                             jdbcAdapter.setBinaryData(stm,index++,contenutoBinario);
  253.                             debug = "BinaryData";
  254.                             stm.setString(index++, contenutoBinarioFileName);
  255.                             debug = debug + "," + contenutoBinarioFileName;
  256.                         }
  257.                        
  258.                         logDebug(log,"CRUDProtocolProperty CREATE : \n" + DBUtils.
  259.                                 formatSQLString(sqlQuery, tipologiaProprietarioProtocolProperty.name(), idProprietario, protocolProperty.getName(), debug));
  260.        
  261.                         int n = stm.executeUpdate();
  262.                         stm.close();
  263.                         logDebug(log,"Inserted " + n + " row(s)");
  264.            
  265.                         sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  266.                         sqlQueryObject.addFromTable(CostantiDB.PROTOCOL_PROPERTIES);
  267.                         sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID);
  268.                         sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_TIPO_PROPRIETARIO+" = ?");
  269.                         sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID_PROPRIETARIO+" = ?");
  270.                         sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_NAME+" = ?");
  271.                         sqlQueryObject.setANDLogicOperator(true);
  272.                         sqlQuery = sqlQueryObject.createSQLQuery();
  273.                         stm = connection.prepareStatement(sqlQuery);
  274.                         index = 1;
  275.                         stm.setString(index++, tipologiaProprietarioProtocolProperty.name());
  276.                         stm.setLong(index++, idProprietario);
  277.                         stm.setString(index++, protocolProperty.getName());
  278.        
  279.                         logDebug(log,"Recupero id inserito : \n" + DBUtils.
  280.                                 formatSQLString(sqlQuery, tipologiaProprietarioProtocolProperty.name(), idProprietario, protocolProperty.getName()));
  281.        
  282.                         rs = stm.executeQuery();
  283.        
  284.                         if (rs.next()) {
  285.                             listPP.get(i).setId(rs.getLong(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID));
  286.                         } else {
  287.                             throw new CoreException("[DBProtocolProperties::CRUDProtocolProperty] Errore avvenuto durante il recupero dell'id dopo una create");
  288.                         }
  289.        
  290.                         rs.close();
  291.                         stm.close();
  292.                        
  293.                     }
  294.                 }
  295.                 break;

  296.             case UPDATE:
  297.                
  298.                 // Prelevo vecchia lista
  299.                 List<ProtocolProperty> oldLista = getListaProtocolPropertySafe(idProprietario,tipologiaProprietarioProtocolProperty, connection, tipoDatabase,
  300.                         null); //driverBYOK);  // serve solo a verificare se devo aggiornare o meno. Non serve quindi decodificare
  301.                
  302.                 // Gestico la nuova immagine
  303.                 if(listPP!=null) {
  304.                     for(int i=0; i<listPP.size(); i++){
  305.                        
  306.                         ProtocolProperty protocolProperty = listPP.get(i);
  307.                         if(protocolProperty.getName()==null || "".equals(protocolProperty.getName()))
  308.                             throw new CoreException("[DBProtocolProperties::CRUDProtocolProperty] Nome non definito per protocolProperty ["+i+"]");
  309.                        
  310.                         // Aggiorno proprietari
  311.                         protocolProperty.setIdProprietario(idProprietario);
  312.                         protocolProperty.setTipoProprietario(tipologiaProprietarioProtocolProperty.name());
  313.                        
  314.                         int contenutiDefiniti = 0;
  315.                        
  316.                         boolean stringValue = protocolProperty.getValue()!=null && !"".equals(protocolProperty.getValue());
  317.                         String plainContenutoString = null;
  318.                         String encContenutoString = null;
  319.                         if(stringValue){
  320.                             contenutiDefiniti++;
  321.                             plainContenutoString = protocolProperty.getValue();
  322.                             if(driverBYOK!=null && isConfidentialProtocolProperty(protocolProperty.getName())) {
  323.                                 BYOKWrappedValue byokValue = driverBYOK.wrap(protocolProperty.getValue());
  324.                                 if(byokValue!=null) {
  325.                                     encContenutoString = byokValue.getWrappedValue();
  326.                                     plainContenutoString = byokValue.getWrappedPlainValue();
  327.                                 }
  328.                             }
  329.                         }
  330.                        
  331.                         boolean numberValue = protocolProperty.getNumberValue()!=null;
  332.                         Long contenutoNumber = null;
  333.                         if(numberValue){
  334.                             contenutiDefiniti++;
  335.                             contenutoNumber = protocolProperty.getNumberValue();
  336.                         }
  337.                        
  338.                         boolean booleanValue = protocolProperty.getBooleanValue()!=null;
  339.                         Boolean contenutoBoolean = null;
  340.                         if(booleanValue){
  341.                             contenutiDefiniti++;
  342.                             contenutoBoolean = protocolProperty.getBooleanValue();
  343.                         }
  344.                        
  345.                         boolean binaryValue = protocolProperty.getByteFile()!=null && protocolProperty.getByteFile().length>0;
  346.                         byte[] contenutoBinario = null;
  347.                         String contenutoBinarioFileName = null;
  348.                         if(binaryValue){
  349.                             contenutiDefiniti++;
  350.                             contenutoBinario = protocolProperty.getByteFile();
  351.                             if(contenutoBinario.length<3){
  352.                                 String test = new String(contenutoBinario);
  353.                                 if("".equals(test.trim().replace("\n", ""))){
  354.                                     // eliminare \n\n
  355.                                     contenutoBinario = null;    
  356.                                     binaryValue = false;
  357.                                     contenutiDefiniti--;
  358.                                 }
  359.                             }
  360.                             if(binaryValue){
  361.                                 contenutoBinarioFileName = protocolProperty.getFile();
  362.                            
  363.                                 if(driverBYOK!=null && isConfidentialProtocolProperty(protocolProperty.getName())) {
  364.                                     BYOKWrappedValue byokValue = driverBYOK.wrap(contenutoBinario);
  365.                                     if(byokValue!=null) {
  366.                                         contenutoBinario = byokValue.getWrappedValue().getBytes();
  367.                                     }
  368.                                 }
  369.                             }
  370.                         }
  371.                        
  372.                         /**if(!stringValue && !numberValue && !binaryValue && !booleanValue){
  373.                             throw new DriverRegistroServiziException("[DBProtocolProperties::CRUDProtocolProperty] Contenuto non definito per protocolProperty ["+protocolProperty.getName()+"]");
  374.                         }*/
  375.                         // Per fare i filtri con is null e' necessario registrarlo!
  376.                         if(contenutiDefiniti>1){
  377.                             throw new CoreException("[DBProtocolProperties::CRUDProtocolProperty] Contenuto definito con più tipologie per protocolProperty ["+protocolProperty.getName()+
  378.                                     "] (string:"+stringValue+" number:"+numberValue+" binary:"+binaryValue+")");
  379.                         }
  380.                        
  381.                                        
  382.                         /**if(doc.getId()<=0){*/
  383.                         // Rileggo sempre id, puo' essere diverso (es. importato tramite sincronizzazioni)
  384.                         protocolProperty.setId(DBUtils.getIdProtocolProperty(protocolProperty.getTipoProprietario(), idProprietario,protocolProperty.getName(),
  385.                                 connection,
  386.                                 tipoDatabase));
  387.                                            
  388.                         boolean ppGiaPresente = false;
  389.                         boolean ppDaAggiornare = false;
  390.                         if(protocolProperty.getId()>0){
  391.                             for(int j=0; j<oldLista.size(); j++){
  392.                                 ProtocolProperty old = oldLista.get(j);
  393.            
  394.                                 /**System.out.println("OLD["+old.getId().longValue()+"]==ATTUALE["+doc.getId().longValue()+"] ("+((doc.getId().longValue() == old.getId().longValue()))+")");*/
  395.                                 if(protocolProperty.getId().longValue() == old.getId().longValue()){        
  396.                                     ppGiaPresente = true; // non devo fare una insert, ma una update...
  397.                                            
  398.                                     // rimuovo la vecchia immagine del documento dalla lista dei doc vecchi
  399.                                     oldLista.remove(j);
  400.                                        
  401.                                     ppDaAggiornare = true;
  402.                                     break;
  403.                                 }
  404.                             }
  405.                         }
  406.    
  407.                         if(ppGiaPresente){
  408.                             if(ppDaAggiornare){
  409.                                 // update
  410.                                 long idPP = protocolProperty.getId();
  411.                                 if(idPP<=0){
  412.                                     throw new CoreException("[DBProtocolProperties::CRUDProtocolProperty] ID non definito per documento da aggiorare ["+protocolProperty.getName()+"]");
  413.                                 }
  414.                                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  415.                                 sqlQueryObject.addUpdateTable(CostantiDB.PROTOCOL_PROPERTIES);
  416.                                 sqlQueryObject.addUpdateField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_STRING, "?");
  417.                                 sqlQueryObject.addUpdateField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_ENCODING_STRING, "?");
  418.                                 sqlQueryObject.addUpdateField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_NUMBER, "?");
  419.                                 sqlQueryObject.addUpdateField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_BOOLEAN, "?");
  420.                                 sqlQueryObject.addUpdateField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_BINARY, "?");
  421.                                 sqlQueryObject.addUpdateField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_FILENAME, "?");
  422.                                 sqlQueryObject.addWhereCondition("id=?");
  423.                                 sqlQuery = sqlQueryObject.createSQLUpdate();
  424.                                 stm = connection.prepareStatement(sqlQuery);
  425.                                 int index = 1;
  426.                                
  427.                                 stm.setString(index++, plainContenutoString);
  428.                                 stm.setString(index++, encContenutoString);
  429.                                
  430.                                 if(numberValue){
  431.                                     stm.setLong(index++, contenutoNumber);
  432.                                 }
  433.                                 else{
  434.                                     stm.setNull(index++, java.sql.Types.BIGINT);
  435.                                 }
  436.                                
  437.                                 if(booleanValue){
  438.                                     if(contenutoBoolean!=null && contenutoBoolean.booleanValue()){
  439.                                         stm.setInt(index++,CostantiDB.TRUE);
  440.                                     }
  441.                                     else{
  442.                                         stm.setInt(index++,CostantiDB.FALSE);
  443.                                     }
  444.                                 }
  445.                                 else{
  446.                                     stm.setNull(index++, java.sql.Types.INTEGER);
  447.                                 }
  448.                                
  449.                                 jdbcAdapter.setBinaryData(stm,index++,contenutoBinario);
  450.                                 stm.setString(index++, contenutoBinarioFileName);
  451.                                
  452.                                 stm.setLong(index++, idPP);
  453.                                 stm.executeUpdate();
  454.                                 stm.close();
  455.                             }
  456.                         }else{
  457.                             // create
  458.                             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  459.                             sqlQueryObject.addInsertTable(CostantiDB.PROTOCOL_PROPERTIES);
  460.                             sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_TIPO_PROPRIETARIO, "?");
  461.                             sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID_PROPRIETARIO, "?");
  462.                             sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_NAME, "?");
  463.                             if(stringValue){
  464.                                 sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_STRING, "?");
  465.                                 sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_ENCODING_STRING, "?");
  466.                             }
  467.                             if(numberValue){
  468.                                 sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_NUMBER, "?");
  469.                             }
  470.                             if(booleanValue){
  471.                                 sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_BOOLEAN, "?");
  472.                             }
  473.                             if(binaryValue){
  474.                                 sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_BINARY, "?");
  475.                                 sqlQueryObject.addInsertField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_FILENAME, "?");
  476.                             }
  477.                             sqlQuery = sqlQueryObject.createSQLInsert();
  478.                             stm = connection.prepareStatement(sqlQuery);
  479.                             int index = 1;
  480.                             stm.setString(index++, tipologiaProprietarioProtocolProperty.name());
  481.                             stm.setLong(index++, idProprietario);
  482.                             stm.setString(index++, protocolProperty.getName());
  483.                             String debug = null;
  484.                             if(stringValue){
  485.                                 stm.setString(index++, plainContenutoString);
  486.                                 debug = plainContenutoString;
  487.                                 stm.setString(index++, encContenutoString);
  488.                             }
  489.                             if(numberValue){
  490.                                 stm.setLong(index++, contenutoNumber);
  491.                                 debug = contenutoNumber+"";
  492.                             }
  493.                             if(booleanValue){
  494.                                 if(contenutoBoolean!=null && contenutoBoolean.booleanValue()){
  495.                                     stm.setInt(index++,CostantiDB.TRUE);
  496.                                     debug = CostantiDB.TRUE+"";
  497.                                 }
  498.                                 else{
  499.                                     stm.setInt(index++,CostantiDB.FALSE);
  500.                                     debug = CostantiDB.FALSE+"";
  501.                                 }
  502.                             }
  503.                             if(binaryValue){
  504.                                 jdbcAdapter.setBinaryData(stm,index++,contenutoBinario);
  505.                                 debug = "BinaryData";
  506.                                 stm.setString(index++, contenutoBinarioFileName);
  507.                                 debug = debug + "," + contenutoBinarioFileName;
  508.                             }
  509.                            
  510.                             logDebug(log,"CRUDProtocolProperty CREATE : \n" + DBUtils.
  511.                                     formatSQLString(sqlQuery, tipologiaProprietarioProtocolProperty.name(), idProprietario, protocolProperty.getName(), debug));
  512.            
  513.                             int n = stm.executeUpdate();
  514.                             stm.close();
  515.                             logDebug(log,"Inserted " + n + " row(s)");
  516.                
  517.                             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  518.                             sqlQueryObject.addFromTable(CostantiDB.PROTOCOL_PROPERTIES);
  519.                             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID);
  520.                             sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_TIPO_PROPRIETARIO+" = ?");
  521.                             sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID_PROPRIETARIO+" = ?");
  522.                             sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_NAME+" = ?");
  523.                             sqlQueryObject.setANDLogicOperator(true);
  524.                             sqlQuery = sqlQueryObject.createSQLQuery();
  525.                             stm = connection.prepareStatement(sqlQuery);
  526.                             index = 1;
  527.                             stm.setString(index++, tipologiaProprietarioProtocolProperty.name());
  528.                             stm.setLong(index++, idProprietario);
  529.                             stm.setString(index++, protocolProperty.getName());
  530.            
  531.                             logDebug(log,"Recupero id inserito : \n" + DBUtils.
  532.                                     formatSQLString(sqlQuery, tipologiaProprietarioProtocolProperty.name(), idProprietario, protocolProperty.getName()));
  533.            
  534.                             rs = stm.executeQuery();
  535.            
  536.                             if (rs.next()) {
  537.                                 listPP.get(i).setId(rs.getLong(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID));
  538.                             } else {
  539.                                 throw new CoreException("[DBProtocolProperties::CRUDProtocolProperty] Errore avvenuto durante il recupero dell'id dopo una create");
  540.                             }
  541.            
  542.                             rs.close();
  543.                             stm.close();
  544.                            
  545.                         }
  546.                        
  547.                     }
  548.                 }
  549.                
  550.                 if(!oldLista.isEmpty()){
  551.                     // Qualche documento e' stato cancellato.
  552.                     // Non e' piu' presente.
  553.                     for(int j=0; j<oldLista.size(); j++){
  554.                         ProtocolProperty old = oldLista.get(j);
  555.                        
  556.                         ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  557.                         sqlQueryObject.addDeleteTable(CostantiDB.PROTOCOL_PROPERTIES);
  558.                         sqlQueryObject.addWhereCondition("id=?");
  559.                         sqlQuery = sqlQueryObject.createSQLDelete();
  560.                         stm = connection.prepareStatement(sqlQuery);
  561.                         stm.setLong(1, old.getId());
  562.                         stm.executeUpdate();
  563.                         stm.close();
  564.                     }
  565.                 }
  566.                
  567.                 break;

  568.             case DELETE:
  569.                 // delete
  570.                
  571.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  572.                 sqlQueryObject.addDeleteTable(CostantiDB.PROTOCOL_PROPERTIES);
  573.                 sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_TIPO_PROPRIETARIO+" = ?");
  574.                 sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID_PROPRIETARIO+"=?");
  575.                 sqlQueryObject.setANDLogicOperator(true);
  576.                 sqlQuery = sqlQueryObject.createSQLDelete();
  577.                 stm = connection.prepareStatement(sqlQuery);
  578.                 stm.setString(1, tipologiaProprietarioProtocolProperty.name());
  579.                 stm.setLong(2, idProprietario);
  580.                 stm.executeUpdate();
  581.                 stm.close();

  582.                 logDebug(log,"CRUDDocumento DELETE : \n" + DBUtils.formatSQLString(sqlQuery, tipologiaProprietarioProtocolProperty.name(), idProprietario));

  583.                 break;
  584.                
  585.             default:
  586.                 // nop
  587.             }

  588.         } catch (SQLException se) {
  589.             throw new CoreException("[DBProtocolProperties::CRUDDocumento] SQLException : " + se.getMessage(),se);
  590.         }catch (Exception se) {
  591.             throw new CoreException("[DBProtocolProperties::CRUDDocumento] Exception : " + se.getMessage(),se);
  592.         } finally {
  593.             try {
  594.                 if(rs!=null)
  595.                     rs.close();
  596.             } catch (Exception e) {
  597.                 // ignore
  598.             }
  599.             try {
  600.                 if(stm!=null)
  601.                     stm.close();
  602.             } catch (Exception e) {
  603.                 // ignore
  604.             }
  605.         }
  606.     }
  607.    
  608.     public static List<org.openspcoop2.core.registry.ProtocolProperty> getListaProtocolPropertyRegistry(long idProprietario, ProprietariProtocolProperty tipologiaProprietario,
  609.             Connection connection,
  610.             String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException,NotFoundException {
  611.         List<ProtocolProperty> l = getListaProtocolProperty(idProprietario, tipologiaProprietario, connection, tipoDatabase, driverBYOK);
  612.         List<org.openspcoop2.core.registry.ProtocolProperty> lPP = null;
  613.         if(l==null) {
  614.             return lPP;
  615.         }
  616.         lPP = new ArrayList<>();
  617.         if(!l.isEmpty()) {
  618.             for (ProtocolProperty protocolProperty : l) {
  619.                 lPP.add(protocolProperty.toRegistry());
  620.             }
  621.         }
  622.         return lPP;
  623.     }
  624.     public static List<org.openspcoop2.core.config.ProtocolProperty> getListaProtocolPropertyConfig(long idProprietario, ProprietariProtocolProperty tipologiaProprietario,
  625.             Connection connection,
  626.             String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException,NotFoundException {
  627.         List<ProtocolProperty> l = getListaProtocolProperty(idProprietario, tipologiaProprietario, connection, tipoDatabase, driverBYOK);
  628.         List<org.openspcoop2.core.config.ProtocolProperty> lPP = null;
  629.         if(l==null) {
  630.             return lPP;
  631.         }
  632.         lPP = new ArrayList<>();
  633.         if(!l.isEmpty()) {
  634.             for (ProtocolProperty protocolProperty : l) {
  635.                 lPP.add(protocolProperty.toConfig());
  636.             }
  637.         }
  638.         return lPP;
  639.     }
  640.    
  641.     public static List<ProtocolProperty> getListaProtocolPropertySafe(long idProprietario, ProprietariProtocolProperty tipologiaProprietario,
  642.             Connection connection,
  643.             String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException  {
  644.         List<ProtocolProperty> l = null;
  645.         try{
  646.             l = getListaProtocolProperty(idProprietario,tipologiaProprietario, connection, tipoDatabase, driverBYOK);
  647.         }catch(NotFoundException dNotFound){
  648.             l = new ArrayList<>();
  649.         }
  650.         return l;
  651.     }
  652.     public static List<ProtocolProperty> getListaProtocolProperty(long idProprietario, ProprietariProtocolProperty tipologiaProprietario,
  653.             Connection connection,
  654.             String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException,NotFoundException {
  655.        
  656.         PreparedStatement stm = null;
  657.         ResultSet rs=null;
  658.         String sqlQuery;

  659.         if(idProprietario <= 0 )
  660.             throw new CoreException("[DBProtocolProperties::getListaProtocolProperty] id proprietario non definito");
  661.        
  662.         try {
  663.        
  664.             List<ProtocolProperty> listPP = new ArrayList<>();
  665.            
  666.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  667.             sqlQueryObject.addFromTable(CostantiDB.PROTOCOL_PROPERTIES);
  668.             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID);
  669.             sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_TIPO_PROPRIETARIO+" = ?");
  670.             sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID_PROPRIETARIO+" = ?");
  671.             sqlQueryObject.setANDLogicOperator(true);
  672.             sqlQuery = sqlQueryObject.createSQLQuery();
  673.             stm = connection.prepareStatement(sqlQuery);
  674.             stm.setString(1, tipologiaProprietario.name());
  675.             stm.setLong(2, idProprietario);
  676.             rs = stm.executeQuery();
  677.            
  678.             while(rs.next()){
  679.                 ProtocolProperty pp = getProtocolProperty(rs.getLong(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID), connection,tipoDatabase, driverBYOK);
  680.                 listPP.add(pp);
  681.             }
  682.            
  683.             if(listPP.isEmpty())
  684.                 throw new NotFoundException("ProtocolProperty con tipologiaProprietario["+tipologiaProprietario.name()+
  685.                         "] e idProprietario["+idProprietario+"] non trovati");
  686.            
  687.             return listPP;

  688.         } catch (SQLException se) {
  689.             throw new CoreException("[DBProtocolProperties::getListaProtocolProperty] SQLException : " + se.getMessage(),se);
  690.         }catch (NotFoundException dnf) {
  691.             throw dnf;
  692.         }catch (Exception se) {
  693.             throw new CoreException("[DBProtocolProperties::getListaProtocolProperty] Exception : " + se.getMessage(),se);
  694.         } finally {
  695.             try {
  696.                 if(rs!=null)
  697.                     rs.close();
  698.             } catch (Exception e) {
  699.                 // ignore
  700.             }
  701.             try {
  702.                 if(stm!=null)
  703.                     stm.close();
  704.             } catch (Exception e) {
  705.                 // ignore
  706.             }
  707.         }
  708.     }
  709.    
  710.     public static org.openspcoop2.core.registry.ProtocolProperty getProtocolPropertyRegistry(long id, Connection connection, String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException,NotFoundException {
  711.         ProtocolProperty pp = getProtocolProperty(id, connection, tipoDatabase, driverBYOK);
  712.         if(pp==null) {
  713.             return null;
  714.         }
  715.         return pp.toRegistry();
  716.     }
  717.     public static org.openspcoop2.core.config.ProtocolProperty getProtocolPropertyConfig(long id, Connection connection, String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException,NotFoundException {
  718.         ProtocolProperty pp = getProtocolProperty(id, connection, tipoDatabase, driverBYOK);
  719.         if(pp==null) {
  720.             return null;
  721.         }
  722.         return pp.toConfig();
  723.     }
  724.     public static ProtocolProperty getProtocolProperty(long id, Connection connection, String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException,NotFoundException {
  725.        
  726.         PreparedStatement stm = null;
  727.         ResultSet rs=null;
  728.         String sqlQuery;

  729.         if(id <= 0 )
  730.             throw new CoreException("[DBProtocolProperties::getProtocolProperty] id non definito");
  731.        
  732.         try {
  733.        
  734.             IJDBCAdapter jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(tipoDatabase);
  735.                
  736.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  737.             sqlQueryObject.addFromTable(CostantiDB.PROTOCOL_PROPERTIES);
  738.             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_TIPO_PROPRIETARIO);
  739.             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID_PROPRIETARIO);
  740.             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_NAME);
  741.             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_STRING);
  742.             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_ENCODING_STRING);
  743.             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_NUMBER);
  744.             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_BOOLEAN);
  745.             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_BINARY);
  746.             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_FILENAME);
  747.             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID);
  748.             sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID+" = ?");
  749.             sqlQueryObject.setANDLogicOperator(true);
  750.             sqlQuery = sqlQueryObject.createSQLQuery();
  751.             stm = connection.prepareStatement(sqlQuery);
  752.             stm.setLong(1, id);
  753.             rs = stm.executeQuery();
  754.            
  755.             ProtocolProperty pp = null;
  756.             if(rs.next()){
  757.                 pp = new ProtocolProperty();
  758.                 pp.setTipoProprietario(rs.getString(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_TIPO_PROPRIETARIO));
  759.                 pp.setIdProprietario(rs.getLong(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID_PROPRIETARIO));
  760.                
  761.                 String ppName = rs.getString(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_NAME);
  762.                 pp.setName(ppName);
  763.                
  764.                 String plainStringValue = rs.getString(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_STRING);
  765.                 String encStringValue = rs.getString(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_ENCODING_STRING);
  766.                 if(encStringValue!=null && StringUtils.isNotEmpty(encStringValue)) {
  767.                     if(driverBYOK!=null) {
  768.                         pp.setValue(driverBYOK.unwrapAsString(encStringValue));
  769.                     }
  770.                     else {
  771.                         pp.setValue(encStringValue);
  772.                     }
  773.                 }
  774.                 else {
  775.                     pp.setValue(plainStringValue);
  776.                 }
  777.                
  778.                 pp.setNumberValue(rs.getLong(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_NUMBER));
  779.                 if(rs.wasNull()){
  780.                     pp.setNumberValue(null);
  781.                 }
  782.                 int value = rs.getInt(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_BOOLEAN);
  783.                 pp.setBooleanValue(value == CostantiDB.TRUE);
  784.                 if(rs.wasNull()){
  785.                     pp.setBooleanValue(null);
  786.                 }
  787.                
  788.                 byte[]binaryValue = jdbcAdapter.getBinaryData(rs,CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_BINARY);
  789.                 if(binaryValue!=null && binaryValue.length>0 && driverBYOK!=null && isConfidentialProtocolProperty(ppName)) {
  790.                     binaryValue = driverBYOK.unwrap(binaryValue);
  791.                 }
  792.                 pp.setByteFile(binaryValue);
  793.                
  794.                 pp.setFile(rs.getString(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_FILENAME));
  795.                 pp.setId(rs.getLong(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID));
  796.             }
  797.            
  798.             if(pp==null)
  799.                 throw new NotFoundException("ProtocolProperty con id["+id+"] non trovato");
  800.            
  801.             return pp;

  802.         } catch (SQLException se) {
  803.             throw new CoreException("[DBProtocolProperties::getProtocolProperty] SQLException : " + se.getMessage(),se);
  804.         }catch (NotFoundException dnf) {
  805.             throw dnf;
  806.         }catch (Exception se) {
  807.             throw new CoreException("[DBProtocolProperties::getProtocolProperty] Exception : " + se.getMessage(),se);
  808.         } finally {
  809.             try {
  810.                 if(rs!=null)
  811.                     rs.close();
  812.             } catch (Exception e) {
  813.                 // ignore
  814.             }
  815.             try {
  816.                 if(stm!=null)
  817.                     stm.close();
  818.             } catch (Exception e) {
  819.                 // ignore
  820.             }
  821.         }
  822.     }
  823.    
  824.    
  825.    
  826.    
  827.    
  828.     public static boolean existsProtocolProperty(ProprietariProtocolProperty proprietarioProtocolProperty, long idProprietario, String nome,
  829.             Connection connection, String tipoDatabase) throws CoreException {

  830.         boolean exist = false;
  831.         PreparedStatement stm = null;
  832.         ResultSet rs = null;

  833.         try {
  834.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDatabase);
  835.             sqlQueryObject.addFromTable(CostantiDB.PROTOCOL_PROPERTIES);
  836.             sqlQueryObject.addSelectField(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID);
  837.             sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_TIPO_PROPRIETARIO+" = ?");
  838.             sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID_PROPRIETARIO+" = ?");
  839.             sqlQueryObject.addWhereCondition(CostantiDB.PROTOCOL_PROPERTIES_COLUMN_NAME+" = ?");
  840.             sqlQueryObject.setANDLogicOperator(true);
  841.             String sqlQuery = sqlQueryObject.createSQLQuery();
  842.             stm = connection.prepareStatement(sqlQuery);
  843.             stm.setString(1, proprietarioProtocolProperty.name());
  844.             stm.setLong(2, idProprietario);
  845.             stm.setString(3, nome);
  846.             rs = stm.executeQuery();
  847.             if (rs.next())
  848.                 exist = true;
  849.             rs.close();
  850.             stm.close();
  851.         } catch (Exception e) {
  852.             exist = false;
  853.             throw new CoreException(e.getMessage(),e);
  854.         } finally {

  855.             //Chiudo statement and resultset
  856.             try {
  857.                 if(rs!=null)
  858.                     rs.close();
  859.             } catch (Exception e) {
  860.                 // ignore
  861.             }
  862.             try {
  863.                 if(stm!=null)
  864.                     stm.close();
  865.             } catch (Exception e) {
  866.                 // ignore
  867.             }

  868.         }

  869.         return exist;
  870.     }

  871.     public static org.openspcoop2.core.registry.ProtocolProperty getProtocolPropertyRegistry(ProprietariProtocolProperty proprietarioProtocolProperty, long idProprietario, String nome,
  872.             Connection connection, String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException {
  873.         ProtocolProperty pp = getProtocolProperty(proprietarioProtocolProperty, idProprietario, nome, connection, tipoDatabase, driverBYOK);
  874.         if(pp==null) {
  875.             return null;
  876.         }
  877.         return pp.toRegistry();
  878.     }
  879.     public static org.openspcoop2.core.config.ProtocolProperty getProtocolPropertyConfig(ProprietariProtocolProperty proprietarioProtocolProperty, long idProprietario, String nome,
  880.             Connection connection, String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException {
  881.         ProtocolProperty pp = getProtocolProperty(proprietarioProtocolProperty, idProprietario, nome, connection, tipoDatabase, driverBYOK);
  882.         if(pp==null) {
  883.             return null;
  884.         }
  885.         return pp.toConfig();
  886.     }
  887.     public static ProtocolProperty getProtocolProperty(ProprietariProtocolProperty proprietarioProtocolProperty, long idProprietario, String nome,
  888.             Connection connection, String tipoDatabase, IDriverBYOK driverBYOK) throws CoreException {
  889.         String nomeMetodo = "getProtocolProperty";
  890.         try {
  891.             long idPP = DBUtils.getIdProtocolProperty(proprietarioProtocolProperty.name(),idProprietario,nome,connection,tipoDatabase);
  892.             return getProtocolProperty(idPP, connection, tipoDatabase, driverBYOK);

  893.         } catch (Exception se) {
  894.             throw new CoreException("[DriverRegistroServiziException::" + nomeMetodo + "] Exception: " + se.getMessage());
  895.         }
  896.     }
  897.    
  898.    
  899.     private static final String AND_SEPARATOR = " AND ";
  900.     private static final String IS_NULL_CONDITION = "is null";
  901.     public static void setProtocolPropertiesForSearch(ISQLQueryObject sqlQueryObject, List<FiltroRicercaProtocolProperty> list, String tabella) throws SQLQueryObjectException{
  902.         if(list!=null && !list.isEmpty()){
  903.             String [] conditions = new String[list.size()];
  904.             for (int i = 0; i < conditions.length; i++) {
  905.                 String aliasTabella = "pp"+i+tabella;
  906.                 sqlQueryObject.addFromTable(CostantiDB.PROTOCOL_PROPERTIES, aliasTabella);
  907.                 sqlQueryObject.setANDLogicOperator(true);
  908.                 sqlQueryObject.addWhereCondition(aliasTabella+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_TIPO_PROPRIETARIO+"=?");
  909.                 sqlQueryObject.addWhereCondition(aliasTabella+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID_PROPRIETARIO+"="+tabella+".id");
  910.                 FiltroRicercaProtocolProperty f = list.get(i);
  911.                                
  912.                 if(f.getName()!=null){
  913.                     if(conditions[i]!=null){
  914.                         conditions[i] = conditions[i] + AND_SEPARATOR;
  915.                     }
  916.                     else {
  917.                         conditions[i] = "";
  918.                     }
  919.                     conditions[i] = conditions[i] + " " + aliasTabella+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_NAME+"=?";
  920.                 }
  921.                
  922.                 if(f.getValueAsString()!=null){
  923.                     if(conditions[i]!=null){
  924.                         conditions[i] = conditions[i] + AND_SEPARATOR;
  925.                     }
  926.                     else {
  927.                         conditions[i] = "";
  928.                     }
  929.                     conditions[i] = conditions[i] + " " + aliasTabella+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_STRING+"=?";
  930.                 }
  931.                 else if(f.getValueAsLong()!=null){
  932.                     if(conditions[i]!=null){
  933.                         conditions[i] = conditions[i] + AND_SEPARATOR;
  934.                     }
  935.                     else {
  936.                         conditions[i] = "";
  937.                     }
  938.                     conditions[i] = conditions[i] + " " + aliasTabella+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_NUMBER+"=?";
  939.                 }
  940.                 else if(f.getValueAsBoolean()!=null){
  941.                     if(conditions[i]!=null){
  942.                         conditions[i] = conditions[i] + AND_SEPARATOR;
  943.                     }
  944.                     else {
  945.                         conditions[i] = "";
  946.                     }
  947.                     conditions[i] = conditions[i] + " " + aliasTabella+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_BOOLEAN+"=?";
  948.                 }
  949.                 else {
  950.                     if(conditions[i]!=null){
  951.                         conditions[i] = conditions[i] + AND_SEPARATOR;
  952.                     }
  953.                     else {
  954.                         conditions[i] = "";
  955.                     }
  956.                     conditions[i] = conditions[i] + " " + aliasTabella+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_STRING+" "+IS_NULL_CONDITION;
  957.                     conditions[i] = conditions[i] + AND_SEPARATOR;
  958.                     conditions[i] = conditions[i] + " " + aliasTabella+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_NUMBER+" "+IS_NULL_CONDITION;
  959.                     conditions[i] = conditions[i] + AND_SEPARATOR;
  960.                     conditions[i] = conditions[i] + " " + aliasTabella+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_VALUE_BOOLEAN+" "+IS_NULL_CONDITION;
  961.                 }
  962.                
  963.                 // casoSpecialeValoreNull
  964.                 ISQLQueryObject sqlQueryObjectPropertyNotExists = null;
  965.                 // in un caso dove il valore non e' definito nel database ci possono essere due casistiche:
  966.                 // 1) Passando via govwayConsole, la proprieta' esiste con il nome ('name') ed e' valorizzata null in tutte le colonne (value_string,value_number,value_boolean)
  967.                 // 2) Passando via govwayLoader, in una configurazione xml, non si definisce la proprietà senza il valore, quindi la riga con il nome non esistera proprio nel db.
  968.                 if(f.getValueAsString()==null && f.getValueAsLong()==null && f.getValueAsBoolean()==null){
  969.                    
  970.                     ISQLQueryObject sqlQueryObjectPropertyNotExistsInternal = sqlQueryObject.newSQLQueryObject();
  971.                     String aliasTabellaNotExists =  "not_exists_"+aliasTabella;
  972.                     sqlQueryObjectPropertyNotExistsInternal.addFromTable(CostantiDB.PROTOCOL_PROPERTIES, aliasTabellaNotExists);
  973.                     sqlQueryObjectPropertyNotExistsInternal.addSelectField(aliasTabellaNotExists, CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID);
  974.                     sqlQueryObjectPropertyNotExistsInternal.addWhereCondition(aliasTabellaNotExists+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID_PROPRIETARIO+"="+aliasTabella+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_ID_PROPRIETARIO);
  975.                     sqlQueryObjectPropertyNotExistsInternal.addWhereCondition(aliasTabellaNotExists+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_TIPO_PROPRIETARIO+"="+aliasTabella+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_TIPO_PROPRIETARIO);
  976.                     sqlQueryObjectPropertyNotExistsInternal.addWhereCondition(aliasTabellaNotExists+"."+CostantiDB.PROTOCOL_PROPERTIES_COLUMN_NAME+"=?");
  977.                     sqlQueryObjectPropertyNotExistsInternal.setANDLogicOperator(true);
  978.                    
  979.                     sqlQueryObjectPropertyNotExists = sqlQueryObject.newSQLQueryObject();
  980.                     sqlQueryObjectPropertyNotExists.addWhereExistsCondition(true, sqlQueryObjectPropertyNotExistsInternal);

  981.                     conditions[i] = "( " + conditions[i] + " ) OR ( " + sqlQueryObjectPropertyNotExists.createSQLConditions() + " )";
  982.                 }
  983.             }
  984.             sqlQueryObject.addWhereCondition(true, conditions);
  985.         }
  986.     }
  987.    
  988.     public static void setProtocolPropertiesForSearch(PreparedStatement stmt, int index,
  989.             List<FiltroRicercaProtocolProperty> list, ProprietariProtocolProperty proprietario,
  990.             String tipoDatabase, Logger log) throws SQLQueryObjectException, SQLException, JDBCAdapterException, UtilsException{
  991.        
  992.         JDBCParameterUtilities jdbcParameterUtilities = new JDBCParameterUtilities(TipiDatabase.toEnumConstant(tipoDatabase));
  993.        
  994.         if(list!=null && !list.isEmpty()){
  995.             for (int i = 0; i < list.size(); i++) {
  996.                
  997.                 logDebug(log,"FiltroRicercaProtocolProperty size:"+list.size()+" ["+i+"] Proprietario stmt.setString("+proprietario.name()+")");
  998.                 stmt.setString(index++, proprietario.name());
  999.                
  1000.             }
  1001.             for (int i = 0; i < list.size(); i++) {
  1002.                
  1003.                 String prefix = "FiltroRicercaProtocolProperty["+i+"] ";
  1004.                
  1005.                 FiltroRicercaProtocolProperty f = list.get(i);
  1006.                 if(f.getName()!=null){
  1007.                     logDebug(log,prefix+"Name stmt.setString("+f.getName()+")");
  1008.                     stmt.setString(index++, f.getName());
  1009.                 }
  1010.                
  1011.                 if(f.getValueAsString()!=null){
  1012.                     logDebug(log,prefix+"ValueAsString stmt.setString("+f.getValueAsString()+")");
  1013.                     jdbcParameterUtilities.setParameter(stmt, index++, f.getValueAsString(), String.class);
  1014.                 }
  1015.                 else if(f.getValueAsLong()!=null){
  1016.                     logDebug(log,prefix+"ValueAsLong stmt.setLong("+f.getValueAsLong()+")");
  1017.                     jdbcParameterUtilities.setParameter(stmt, index++, f.getValueAsLong(), Long.class);
  1018.                 }
  1019.                 else if(f.getValueAsBoolean()!=null){
  1020.                     int value = f.getValueAsBoolean()!=null && f.getValueAsBoolean().booleanValue() ? CostantiDB.TRUE : CostantiDB.FALSE;
  1021.                     logDebug(log,prefix+"ValueAsBoolean stmt.setInt("+value+")");
  1022.                     jdbcParameterUtilities.setParameter(stmt, index++, value, Integer.class);
  1023.                 }
  1024.                
  1025.                 // casoSpecialeValoreNull
  1026.                 // in un caso dove il valore non e' definito nel database ci possono essere due casistiche:
  1027.                 // 1) Passando via govwayConsole, la proprieta' esiste con il nome ('name') ed e' valorizzata null in tutte le colonne (value_string,value_number,value_boolean)
  1028.                 // 2) Passando via govwayLoader, in una configurazione xml, non si definisce la proprietà senza il valore, quindi la riga con il nome non esistera proprio nel db.
  1029.                 if(f.getValueAsString()==null && f.getValueAsLong()==null && f.getValueAsBoolean()==null){
  1030.                     logDebug(log,prefix+"Name stmt.setString("+f.getName()+")");
  1031.                     stmt.setString(index++, f.getName());
  1032.                 }
  1033.             }
  1034.         }
  1035.     }
  1036. }