org.appng.core.service

## Class DatabaseService

- java.lang.Object
- - org.appng.core.service.MigrationService
  - - org.appng.core.service.DatabaseService

-     public class DatabaseService
      extends MigrationService

  A service offering methods for creating and migrating `DatabaseConnection`s of a `SiteApplication`.
  Author:
    Matthias Müller

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.appng.core.service.MigrationService

      `MigrationService.MigrationStatus`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected DatabaseConnectionRepository` | `databaseConnectionRepository`

    - ### Fields inherited from class org.appng.core.service.MigrationService

      `APP_NG_ROOT_DATABASE, DATABASE_MAX_CONNECTIONS, DATABASE_MIN_CONNECTIONS, DATABASE_NAME, DATABASE_NAME_PREFIX, DATABASE_REPAIR, DATABASE_TYPE, DATABASE_VALIDATION_PERIOD, DATABASE_VALIDATION_QUERY, HIBERNATE_CONNECTION_DRIVER_CLASS, HIBERNATE_CONNECTION_PASSWORD, HIBERNATE_CONNECTION_URL, HIBERNATE_CONNECTION_USERNAME`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `DatabaseService()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `dropApplicationConnection(DatabaseConnection databaseConnection, DataSource dataSource)`
    `DatabaseConnection` | `getRootConnectionOfType(DatabaseConnection.DatabaseType type)`
    `protected String` | `getUserName(Site site, Application application)`
    `protected void` | `initApplicationConnection(DatabaseConnection applicationConnection, DataSource dataSource)`
    `DatabaseConnection` | `initDatabase(Properties config, boolean managed, boolean setActive)` Configures and (optionally) migrates the appNG root `DatabaseConnection` from the given `Properties`.
    `MigrationService.MigrationStatus` | `manageApplicationConnection(SiteApplication siteApplication, File sqlFolder, String databasePrefix)` Migrates the database for the given `SiteApplication`.
    `void` | `resetApplicationConnection(SiteApplication siteApplication, String databasePrefix)` Resets the configuration for the `DatabaseConnection` (if existing) of the given `SiteApplication` to the default values.
    `void` | `save(DatabaseConnection databaseConnection)` Persists the given `DatabaseConnection`.
    `DatabaseConnection` | `setActiveConnection(DatabaseConnection rootConnection, boolean changeManagedState)` Configures the appNG root `DatabaseConnection`s by either creating a new one or updating the existing `DatabaseConnection` for each `DatabaseConnection.DatabaseType`.

    - ### Methods inherited from class org.appng.core.service.MigrationService

      `getDataSource, getDataSource, getFlyway, getPlatformConnection, initDatabase, initDatabase, migrate, status, status, statusComplete, statusComplete, statusComplete`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### databaseConnectionRepository

          @Autowired
          protected DatabaseConnectionRepository databaseConnectionRepository

  - ### Constructor Detail

    - #### DatabaseService

          public DatabaseService()

  - ### Method Detail

    - #### initApplicationConnection

          protected void initApplicationConnection(DatabaseConnection applicationConnection,
                                                   DataSource dataSource)
                                            throws IOException,
                                                   URISyntaxException

      Throws:
        `IOException`
        `URISyntaxException`

    - #### resetApplicationConnection

          public void resetApplicationConnection(SiteApplication siteApplication,
                                                 String databasePrefix)

      Resets the configuration for the `DatabaseConnection` (if existing) of the given `SiteApplication` to the default values.
      Parameters:
        `siteApplication` - a `SiteApplication`

    - #### getUserName

          protected String getUserName(Site site,
                                       Application application)

    - #### dropApplicationConnection

          protected void dropApplicationConnection(DatabaseConnection databaseConnection,
                                                   DataSource dataSource)
                                            throws IOException,
                                                   URISyntaxException

      Throws:
        `IOException`
        `URISyntaxException`

    - #### initDatabase

          @Transactional
          public DatabaseConnection initDatabase(Properties config,
                                                                boolean managed,
                                                                boolean setActive)

      Configures and (optionally) migrates the appNG root `DatabaseConnection` from the given `Properties`.
      Parameters:
        `config` - the properties read from "/conf/appNG.properties"
        `managed` - whether to make the connection managed
        `setActive` - if the connection should be set as he active root connection, creating a new `DatabaseConnection` if necessary. Only applied if `MigrationService.status(DatabaseConnection)` returns a non-null value.
      Returns:
        the appNG root `DatabaseConnection`

    - #### setActiveConnection

          @Transactional
          public DatabaseConnection setActiveConnection(DatabaseConnection rootConnection,
                                                                       boolean changeManagedState)

      Configures the appNG root `DatabaseConnection`s by either creating a new one or updating the existing `DatabaseConnection` for each `DatabaseConnection.DatabaseType`. Also checks if the connections are working and calls `DatabaseConnection#setActive(false)` if this is not the case.
      Parameters:
        `rootConnection` - the current root connection
        `changeManagedState` - if set to `true`, the managed state for an existing connection is set to `rootConnection#isManaged()`

    - #### getRootConnectionOfType

          public DatabaseConnection getRootConnectionOfType(DatabaseConnection.DatabaseType type)

    - #### manageApplicationConnection

          @Transactional
          public MigrationService.MigrationStatus manageApplicationConnection(SiteApplication siteApplication,
                                                                                             File sqlFolder,
                                                                                             String databasePrefix)

      Migrates the database for the given `SiteApplication`.
      Parameters:
        `siteApplication` - the `SiteApplication` to migrate the database for
        `sqlFolder` - the root folder for the migration-scripts provided by the `SiteApplication`
        `databasePrefix` - the prefix for the database to create
      Returns:
        the `MigrationService.MigrationStatus`

    - #### save

          @Transactional
          public void save(DatabaseConnection databaseConnection)

      Persists the given `DatabaseConnection`.
      Parameters:
        `databaseConnection` - the connection to persist.

Copyright © 2011–2023 aiticon GmbH. All rights reserved.