KeyGeneratorFactory.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.utils.jdbc;

  21. import java.io.InputStream;
  22. import java.sql.Connection;
  23. import java.util.Properties;

  24. import org.openspcoop2.utils.TipiDatabase;

  25. /**
  26.  * Factory per un KeyGenerator
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class KeyGeneratorFactory {

  33.     private static boolean useAutoIncrementPostgresql = false;
  34.     public static boolean isUseAutoIncrementPostgresql() {
  35.         return KeyGeneratorFactory.useAutoIncrementPostgresql;
  36.     }
  37.     public static void setUseAutoIncrementPostgresql(boolean useAutoIncrementPostgresql) {
  38.         KeyGeneratorFactory.useAutoIncrementPostgresql = useAutoIncrementPostgresql;
  39.     }

  40.     static {
  41.         try (InputStream is = KeyGeneratorFactory.class.getResourceAsStream("/org/openspcoop2/utils/jdbc/jdbc.properties")){
  42.             if(is!=null) {
  43.                 Properties p = new Properties();
  44.                 p.load(is);
  45.                 String tmp = p.getProperty("postgresql.autoIncrement");
  46.                 if(tmp!=null) {
  47.                     if("true".equalsIgnoreCase(tmp.trim())) {
  48.                         KeyGeneratorFactory.useAutoIncrementPostgresql = true;
  49.                     }
  50.                 }
  51.             }
  52.         }catch(Throwable t) {}
  53.     }
  54.    
  55.     public static IKeyGenerator createKeyGeneratorFactory(String tipoDatabase,Connection connection,IKeyGeneratorObject object) throws KeyGeneratorException {
  56.         return KeyGeneratorFactory.toKeyGenerator(tipoDatabase, connection, object);
  57.     }

  58.     public static IKeyGenerator toKeyGenerator(String tipoDatabase,Connection connection,IKeyGeneratorObject object) throws KeyGeneratorException{
  59.        
  60.         if (TipiDatabase.POSTGRESQL.equals(tipoDatabase)) {
  61.             if(KeyGeneratorFactory.useAutoIncrementPostgresql) {
  62.                 return new PostgreSQLAutoKeyGenerator(connection, object);
  63.             }
  64.             else {
  65.                 return new PostgreSQLKeyGenerator(connection, object);
  66.             }
  67.         } else if (TipiDatabase.MYSQL.equals(tipoDatabase)) {
  68.             return new MySQLKeyGenerator(connection, object);
  69.         } else if (TipiDatabase.ORACLE.equals(tipoDatabase)) {
  70.             return new OracleKeyGenerator(connection, object);
  71.         } else if (TipiDatabase.HSQL.equals(tipoDatabase)) {
  72.             return new HyperSQLKeyGenerator(connection, object);
  73.         } else if (TipiDatabase.DERBY.equals(tipoDatabase)) {
  74.             return new DerbyKeyGenerator(connection, object);
  75.         } else if(TipiDatabase.SQLSERVER.toString().equals(tipoDatabase)){          
  76.             return new SQLServerKeyGenerator(connection, object);
  77.         } else if(TipiDatabase.DB2.toString().equals(tipoDatabase)){            
  78.             return new DB2KeyGenerator(connection, object);
  79.         } else {
  80.             throw new KeyGeneratorException("Tipo database non gestito ["+tipoDatabase+"]");
  81.         }

  82.     }
  83. }