JavaxPersistenceSqlGenerator.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.sql;
  21. import java.io.IOException;
  22. import java.lang.reflect.Method;
  23. import java.nio.file.Files;
  24. import java.nio.file.Paths;
  25. import java.util.HashMap;
  26. import java.util.Map;

  27. import org.openspcoop2.utils.TipiDatabase;

  28. /**
  29.  * JavaxPersistenceSqlGenerator
  30.  *
  31.  * @author Andrea Poli (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class JavaxPersistenceSqlGenerator {
  36.    
  37.     private JavaxPersistenceSqlGenerator() {}

  38.     public static void generate(String persistenceId, String folder) throws IOException {
  39.        
  40.         TipiDatabase[] values = {TipiDatabase.POSTGRESQL,TipiDatabase.MYSQL,TipiDatabase.ORACLE, TipiDatabase.DERBY};
  41.         for(TipiDatabase tipoDatabase: values) {
  42.             generate(persistenceId, folder, tipoDatabase);
  43.         }
  44.     }
  45.    
  46.     private static void generate(String persistenceId, String folder, TipiDatabase tipoDatabase) throws IOException {

  47.         String create= folder + tipoDatabase.toString().toLowerCase() +  "/"+persistenceId+".sql";
  48.         String drop = folder + tipoDatabase.toString().toLowerCase()+ "/"+persistenceId+"_drop.sql";

  49.         Files.deleteIfExists(Paths.get(create));
  50.         Files.deleteIfExists(Paths.get(drop));

  51.         Map<String, String> map = getMap(persistenceId, create, drop, tipoDatabase);
  52.        
  53.         /** Persistence.generateSchema(persistenceId, map);*/
  54.         try {
  55.             Class<?> cPersistence = Class.forName("javax.persistence.Persistence");
  56.             Method m = cPersistence.getMethod("generateSchema", persistenceId.getClass(), java.util.Map.class);
  57.             m.invoke(null, persistenceId, map);
  58.         }catch(Exception t) {
  59.             throw new IOException(t);
  60.         }
  61.      
  62.     }

  63.     private static Map<String, String> getMap(String persistenceId, String create, String drop, TipiDatabase tipoDatabase) {
  64.        
  65.         if(persistenceId!=null) {
  66.             // nop
  67.         }
  68.        
  69.         Map<String, String> map = new HashMap<>();

  70.         map.put("javax.persistence.schema-generation.scripts.action", "drop-and-create");
  71.         map.put("javax.persistence.schema-generation.scripts.create-target", create);
  72.         map.put("javax.persistence.schema-generation.scripts.drop-target", drop);
  73.         map.put("hibernate.hbm2ddl.delimiter", ";");
  74.         map.put("hibernate.format_sql", "true");
  75.        
  76.         String productName = "javax.persistence.database-product-name";
  77.         String dialect = "hibernate.dialect";
  78.        
  79.         switch(tipoDatabase) {
  80.         case DERBY:
  81.             map.put(productName, "Derby");
  82.             map.put(dialect,"org.hibernate.dialect.DerbyTenSevenDialect");
  83.             break;
  84.         case MYSQL:
  85.             map.put(productName, "Mysql");
  86.             map.put(dialect,"org.hibernate.dialect.MySQL5InnoDBDialect");
  87.             break;
  88.         case ORACLE:
  89.             map.put(productName, "Oracle");
  90.             map.put(dialect,"org.hibernate.dialect.Oracle10gDialect");
  91.             break;
  92.         case POSTGRESQL:
  93.             map.put(productName, "Postgresql");
  94.             map.put(dialect, "org.hibernate.dialect.PostgreSQLDialect");
  95.             map.put("javax.persistence.database-major-version", "9");
  96.             map.put("javax.persistence.database-minor-version", "1");
  97.             break;
  98.         default:
  99.             break;
  100.         }

  101.         return map;
  102.     }

  103. }