/** @param args */
  public static void main(String[] args) throws Exception {
    BasicConfigurator.configure();
    Logger.getLogger("org.eclipse.emf.teneo").setLevel(Level.INFO);
    Logger.getLogger("org.hibernate").setLevel(Level.WARN);

    DatabaseSetupData setupData = new DatabaseSetupData();

    if (args != null && args.length > 0) {
      setupData.setSchemaFileName(args[0]);
      if (args.length > 1) {
        setupData.setDatabaseName(args[1]);
      }
    }

    if (!DatabaseSetupUtil.testServerConnection(setupData)) {
      throw new RuntimeException("Unable to connect to running server");
    }

    if (DatabaseSetupUtil.checkDatabaseExists(setupData)) {
      throw new RuntimeException("Database: '" + setupData.getDatabaseName() + "' exists!");
    }

    // documentation
    DatabaseSetupUtil.generateSchemaFile(setupData);
    DatabaseSetupUtil.generateHibernateMapping(setupData);

    // create live database
    DatabaseSetupUtil.createDatabase(setupData);

    // create seed data and initial dairy record
    DatabaseSetupUtil setupUtil = new DatabaseSetupUtil(setupData);
    setupUtil.intializeDairyDatabase("registration #", "licensee name");
  }
 /**
  * @param data
  * @return
  */
 private static DataSource createDataSource(DatabaseSetupData data) {
   MysqlDataSource dataSource = new MysqlDataSource();
   // dataSource.setUrl(getDatabaseURL());
   dataSource.setUser(data.getUserName());
   dataSource.setPassword(data.getPassword());
   dataSource.setPort(data.getPort());
   dataSource.setServerName(data.getHost());
   // dataSource.setDatabaseName(getDatabaseName());
   return dataSource;
 }
 /**
  * @param setupData
  * @return
  */
 public static boolean checkDatabaseExists(DatabaseSetupData setupData) throws SQLException {
   System.out.format("Creating database [%s]\n", setupData.getDatabaseName());
   ResultSet rs =
       runSQL(
           setupData,
           "select count(*) from INFORMATION_SCHEMA.schemata where schema_name = '"
               + setupData.getDatabaseName()
               + "'");
   rs.first();
   int count = rs.getInt(1);
   return count > 0;
 }
  /** @throws IOException */
  public static void generateSchemaFile(DatabaseSetupData data) throws IOException {
    // for teneo, we need to intialize the datastore in order ot properly
    // init the configuration. So we set an invalid db so init will fail,
    // but then we can use the config to generate schema...

    File f = new File(data.getSchemaFileName());
    System.out.println("Generating schema to file: " + f.getCanonicalPath());

    Configuration configuration = generateTeneoHibernateConfig(data);

    SchemaExport exporter = new SchemaExport(configuration);
    exporter.setHaltOnError(true);
    exporter.setOutputFile(f.getCanonicalPath());
    exporter.setDelimiter(";");
    exporter.setFormat(true);
    exporter.execute(true, false, false, true);
  }
  private static Configuration generateTeneoHibernateConfig(DatabaseSetupData data) {
    // for teneo, we need to intialize the datastore in order ot properly
    // init the configuration. So we set an invalid db so init will fail,
    // but then we can use the config to generate schema...
    HbDataStore tempDataStore = HbHelper.INSTANCE.createRegisterDataStore("fakedatastore");

    String savedDbName = data.getDatabaseName();
    data.setDatabaseName("generateschema" + data.hashCode());
    String savedUserName = data.getUserName();
    data.setUserName(data.hashCode() + "generateschema");
    try {
      tempDataStore.setProperties(data.getOptions());
      tempDataStore.setEPackages(EPACKAGES);
      tempDataStore.initialize();
    } catch (Throwable t) {
      System.out.println("Caught expected error: " + t.getMessage());
    }
    data.setUserName(savedUserName);
    data.setDatabaseName(savedDbName);

    Configuration configuration = tempDataStore.getHibernateConfiguration();
    return configuration;
  }
 /**
  * @param data
  * @throws SQLException
  */
 public static void createDatabase(DatabaseSetupData data) throws SQLException {
   System.out.format("Creating database [%s]\n", data.getDatabaseName());
   runSQL(data, String.format("create database  %s;", data.getDatabaseName()));
 }
 private void initializeDataStore() {
   hbds = HbHelper.INSTANCE.createRegisterDataStore(data.getDatabaseName());
   hbds.setProperties(data.getOptions());
   hbds.setEPackages(EPACKAGES);
   hbds.initialize();
 }