content: /*
   * Copyright (C) 2012-2023 52°North Spatial Information Research GmbH
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 as published
   * by the Free Software Foundation.
   *
   * If the program is linked with libraries which are licensed under one of
   * the following licenses, the combination of the program with the linked
   * library is not considered a "derivative work" of the program:
   *
   *     - Apache License, version 2.0
   *     - Apache Software License, version 1.0
   *     - GNU Lesser General Public License, version 3
   *     - Mozilla Public License, versions 1.0, 1.1 and 2.0
   *     - Common Development and Distribution License (CDDL), version 1.0
   *
   * Therefore the distribution of the program linked with libraries licensed
   * under the aforementioned licenses, is permitted by the copyright holders
   * if the distribution is compliant with both the GNU General Public
   * License version 2 and the aforementioned licenses.
   *
   * This program 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 General
   * Public License for more details.
   */
  package org.n52.sos.ds.datasource;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  import java.util.Map;
  import java.util.Properties;
  import java.util.Set;
  
  import org.hibernate.boot.Metadata;
  import org.n52.faroe.SettingDefinition;
  import org.n52.sos.ds.hibernate.util.HibernateConstants;
  
  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
  
  public class AquariusInMemoryDatasource extends AbstractAquariusH2Datasource implements H2InMemory {
  
      private static final long serialVersionUID = 1L;
  
      private static final String DIALECT = "Proxy Aquarius (in-memory)";
  
      @Override
      public String getDialectName() {
          return DIALECT;
      }
  
      @Override
      public Set<SettingDefinition<?>> getSettingDefinitions() {
          Set<SettingDefinition<?>> settingDefinitions = super.getSettingDefinitions();
          return settingDefinitions;
      }
  
      @Override
      public boolean checkSchemaCreation(Map<String, Object> settings) {
          return true;
      }
  
      @Override
      public Properties getDatasourceProperties(Map<String, Object> settings) {
          Properties p = super.getDatasourceProperties(settings);
          p.put(HibernateConstants.HBM2DDL_AUTO, HibernateConstants.HBM2DDL_CREATE);
          return p;
      }
  
      @Override
      public Map<String, Object> parseDatasourceProperties(Properties current) {
          Map<String, Object> settings = super.parseDatasourceProperties(current);
          settings.put(HIBERNATE_DIRECTORY, current.get(HIBERNATE_DIRECTORY));
          settings.put(DATABASE_CONCEPT_KEY, current.getProperty(DATABASE_CONCEPT_KEY));
          settings.put(DATABASE_EXTENSION_KEY, current.getProperty(DATABASE_EXTENSION_KEY));
          return settings;
      }
  
      @Override
      public boolean needsSchema() {
          return false;
      }
  
      @Override
      protected void validatePrerequisites(Connection con, Metadata metadata, Map<String, Object> settings) {
      }
  
      @Override
      @SuppressFBWarnings("DMI_EMPTY_DB_PASSWORD")
      protected Connection openConnection(Map<String, Object> settings) throws SQLException {
          try {
              Class.forName(H2_DRIVER_CLASS);
              precheckDriver(JDBC_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD);
              return DriverManager.getConnection(JDBC_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD);
          } catch (ClassNotFoundException ex) {
              throw new RuntimeException(ex);
          }
      }
  
      @Override
      protected String toURL(Map<String, Object> settings) {
          return JDBC_URL;
      }
  
      @Override
      protected String[] parseURL(String url) {
          return new String[0];
      }
  
  }
  