All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.bonita.util.DbTool Maven / Gradle / Ivy

/**
 * Copyright (C) 2007  Bull S. A. S.
 * Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA  02110-1301, USA.
 **/
package org.ow2.bonita.util;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.ow2.bonita.pvm.env.EnvironmentFactory;
import org.ow2.bonita.env.GlobalEnvironmentFactory;


/**
 * @author Guillaume Porcher
 * Helper class to initialize DB tables from the environment.
 */
public final class DbTool {

  private DbTool() { }

  private static SchemaExport getSchemaExport(String configurationName) {
    EnvironmentFactory envFactory = GlobalEnvironmentFactory.getEnvironmentFactory();
    if (envFactory == null) {
      throw new BonitaRuntimeException("Unable to find EnvironmentFactory.");
    }
    Configuration config = (Configuration) envFactory.get(configurationName);
    if (config == null) {
      throw new BonitaRuntimeException("Unable to find a hibernate configuration '" 
          + configurationName + "' in the EnvironmentFactory.");
    }
    return new SchemaExport(config, config.buildSettings());
  }
  
  /**
   * Export DB schema to the database defined in the environment.
   */
  public static void recreateDb(String configurationName) {
    SchemaExport schemaExport = getSchemaExport(configurationName);
    schemaExport.create(false, true);
  }
  
  public static void dropDb(String configurationName) {
    SchemaExport schemaExport = getSchemaExport(configurationName);
    schemaExport.drop(false, true);
  }
  
  public static void main(String[] args) {
    if (args == null || args.length < 1) {
      String message = "\n";
      message += "This tool helps you (re)creating a database. The database is splited in two : \n";
      message += "- core database (required)\n";
      message += "- history database (optionnal)\n";
      message += "When initializing the core database,  you need to specify the two following hibernate configuration :\n";
      message += "- core configuration : " + EnvConstants.CORE_HB_CONF_NAME + "\n";
      message += "When initializing the history database,  you need to specify the two following hibernate configuration :\n";
      message += "- history configuration : " + EnvConstants.HISTORY_HB_CONF_NAME + "\n";
      message += "If you specify the two configurations, both core and history databases will be (re)created.\n";
      throw new IllegalArgumentException(message);
    }
    for (String conf : args) {
      recreateDb(conf);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy