com.ihr.xbrl.manage

## Class DbUtil

- java.lang.Object
- - com.ihr.xbrl.manage.DbUtil

-     public class DbUtil
      extends java.lang.Object

  This class is responsible to initialize the Hibernate SessionFactory that will be used in obtaining objects from the XBRL Database.
  Author:
    Ignacio

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `DbUtil.Database` Enumerates the different database engines the storage subsystem can work with.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `DbUtil(java.util.Properties properties)` Creates a new instance of the DbUtil object.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addClass(org.hibernate.cfg.Configuration conf, java.lang.Class<?> clazz)` Depending on the dialect used to talk with the database this method will load the hbm.xml file from the right source directory and load it into the configuration
    `java.lang.String` | `getDatabasePrefix()` Obtains a short string to locate resources for each database type.
    `DbUtil.Database` | `getDatabaseType()` Reads the properties of this DBconnection and sets the database subsystem to the appropriate type.
    `static java.lang.String` | `getDialect(DbUtil.Database database)` Returns the class name implementing the dialect to create SQL statements for the specific database.
    `static java.lang.String` | `getDriver(DbUtil.Database database)` Returns the driver class name associated to the database type.
    `java.lang.Throwable` | `getLastException()` Returns the last error produced during the call to `getSessionFactory(RegisterExtraClasses[])` or null in case of no errors was produced.
    `java.util.Properties` | `getProperties()` Access to the Properties object in order to allow the pojo generation process to be tuned
    `org.hibernate.SessionFactory` | `getSessionFactory(RegisterExtraClasses[] registers)` gets existing or creates a new session factory.
    `boolean` | `isDatabaseAvailable()` Returns true if the database is available.
    `static void` | `registerDriver(java.lang.String driver, java.lang.String dialect)` Registers a new database.
    `void` | `resetLastException()` Clear the last exception stored on this class.
    `void` | `resetSessionFactory()` Clear current session factory and the last exception.

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

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

- - ### Constructor Detail

    - #### DbUtil

          public DbUtil(java.util.Properties properties)

      Creates a new instance of the DbUtil object. Applications should not create instances of this object because the creator of XBRLDatabaseAccessApp already creates the instance of DbUtil object.
      Parameters:
        `properties` - Properties object. Extra properties for the Hibernate connection to the database and tune the pojo generation process

  - ### Method Detail

    - #### resetSessionFactory

          public void resetSessionFactory()

      Clear current session factory and the last exception. The next call to `getSessionFactory(RegisterExtraClasses[])` will create a new one.
      See Also:
        `getSessionFactory(RegisterExtraClasses[])`

    - #### resetLastException

          public void resetLastException()

      Clear the last exception stored on this class.

    - #### getSessionFactory

          public org.hibernate.SessionFactory getSessionFactory(RegisterExtraClasses[] registers)

      gets existing or creates a new session factory. If the session factory does not exist it is created and stored for later use.

      The parameter is optional (can be null), if supplied it is an array of objects that implements the RegisterExtraClasse interface and this causes the configuration object to load more classes in the data model. If on the first call this parameter is not null, next calls to this method will return the same session factory even if the parameter is null (the created session factory is stored on this class and returned immediately)

      Returns null in case there are any Exceptions during the connection to the database or creating the model. In that case, the method `getLastException()` will return the last error.

      Parameters:
        `registers` - Optional parameter, can be null.
      Returns:
        SessionFactory suitable for generating sessions
      See Also:
        `resetSessionFactory()`, `getLastException()`

    - #### registerDriver

          public static void registerDriver(java.lang.String driver,
                            java.lang.String dialect)

      Registers a new database. A jar on the class path shall be provided. The jar shall contain hbm.xml files for all classes in the com.ihr.xbrl.pojo package that are entities to be stored to the database and additional extensions packages such as the "user" and the "etl" packages. The database driver enumeration will be "other" and the prefix will be "" so the DBM files shall be available in the class loader using the name "com.ihr.xbrl.pojo.{entityclassname}" the URL the driver will use to search for resources will take the form "com/ihr/xbrl/pojo/{entityclassname}.hbm.xml"
      Parameters:
        `driver` - (java class name implementing the JDBC driver. Must be reachable in the classpath)
        `dialect` - (java class name implementing the Hibernate Dialect. Must be in the classpath)

    - #### getDatabaseType

          public DbUtil.Database getDatabaseType()

      Reads the properties of this DBconnection and sets the database subsystem to the appropriate type.
      Returns:
        Database

    - #### getDatabasePrefix

          public java.lang.String getDatabasePrefix()

      Obtains a short string to locate resources for each database type.
      Returns:
        the database prefix

    - #### addClass

          public void addClass(org.hibernate.cfg.Configuration conf,
                      java.lang.Class<?> clazz)

      Depending on the dialect used to talk with the database this method will load the hbm.xml file from the right source directory and load it into the configuration
      Parameters:
        `conf` - the conf
        `clazz` - the clazz

    - #### isDatabaseAvailable

          public boolean isDatabaseAvailable()

      Returns true if the database is available. A database is available if `getSessionFactory(RegisterExtraClasses[])` returned no errors last time it was called. Note this method does not execute tests to ensure the database is still available this method returns true if the first call was a success
      Returns:
        Boolean

    - #### getLastException

          public java.lang.Throwable getLastException()

      Returns the last error produced during the call to `getSessionFactory(RegisterExtraClasses[])` or null in case of no errors was produced.
      Returns:
        Throwable

    - #### getDriver

          public static java.lang.String getDriver(DbUtil.Database database)

      Returns the driver class name associated to the database type.
      Parameters:
        `database` - a value from the Database enumeration
      Returns:
        String

    - #### getDialect

          public static java.lang.String getDialect(DbUtil.Database database)

      Returns the class name implementing the dialect to create SQL statements for the specific database.
      Parameters:
        `database` - the database
      Returns:
        String

    - #### getProperties

          public java.util.Properties getProperties()

      Access to the Properties object in order to allow the pojo generation process to be tuned
      Returns:
        Properties