edu.wpi.prc.persistence
Class HibernateController

java.lang.Object
  extended by edu.wpi.prc.persistence.HibernateController

public class HibernateController
extends java.lang.Object

The HibernateController class controls access to important Hibernate functions.

Author:
James Baldassari

Field Summary
private  org.hibernate.cfg.Configuration config
           
private  DatabaseType dbType
           
private static HibernateController hibernateController
           
private  java.util.Properties hibernateProperties
           
private  boolean isConfigured
           
private  org.hibernate.SessionFactory sessions
           
 
Constructor Summary
private HibernateController()
          Private constructor prevents direct instantiation of this class.
 
Method Summary
 org.hibernate.Session createSession()
          Creates a new Hibernate session to use for database access.
 void exportSchema()
          Exports the schema to the database.
 java.util.Properties getHibernateProperties()
           
 void initialize()
          Initializes hibernate.
 void initialize(DatabaseType db)
          Initializes hibernate with the specified Hibernate configuration file.
 void initialize(java.lang.String configFilePath)
          Initializes hibernate using an alternate project configuration file path.
 void initializeForUnitTests()
          Initializes Hibernate for use with the JUnit tests.
static HibernateController instance()
           
 boolean isConfigured()
           
private  java.util.Properties readConfigFromFile()
          Reads project configuration from the project properties file, then translates it into Hibernate properties.
private  java.util.Properties readConfigFromFile(java.lang.String configFilePath)
          Reads project configuration from the project properties file, then translates it into Hibernate properties.
private  java.util.Properties readProjectProperties(Project proj)
          Configures Hibernate by inspecting a project.
private  void setConfiguration()
          Sets the Hibernate configuration that was extracted from the project.
 void setDatabaseType(DatabaseType db)
          Sets the type of database hibernate will use if none is specified.
 void setHibernateProperties(java.util.Properties hibernateProperties)
           
 void shutdown()
          Shuts down Hibernate and closes all open sessions.
 void updateSchema()
          Updates the database schema.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hibernateController

private static HibernateController hibernateController

dbType

private DatabaseType dbType

hibernateProperties

private java.util.Properties hibernateProperties

config

private org.hibernate.cfg.Configuration config

sessions

private org.hibernate.SessionFactory sessions

isConfigured

private boolean isConfigured
Constructor Detail

HibernateController

private HibernateController()
Private constructor prevents direct instantiation of this class.

Method Detail

getHibernateProperties

public java.util.Properties getHibernateProperties()
Returns:
Returns the hibernateProperties.

setHibernateProperties

public void setHibernateProperties(java.util.Properties hibernateProperties)
Parameters:
hibernateProperties - The hibernateProperties to set.

readConfigFromFile

private java.util.Properties readConfigFromFile()
                                         throws MissingConfigurationFileException
Reads project configuration from the project properties file, then translates it into Hibernate properties.

Returns:
Returns a Properties object with Hibernate configuration information.
Throws:
MissingConfigurationFileException

readConfigFromFile

private java.util.Properties readConfigFromFile(java.lang.String configFilePath)
                                         throws MissingConfigurationFileException
Reads project configuration from the project properties file, then translates it into Hibernate properties.

Parameters:
configFilePath - to the project configuration file.
Returns:
Returns a Properties object with Hibernate configuration information.
Throws:
MissingConfigurationFileException

readProjectProperties

private java.util.Properties readProjectProperties(Project proj)
Configures Hibernate by inspecting a project.

Parameters:
proj - The project to use to configure Hibernate.
Returns:
Returns a properties object containing Hibernate configuration information.

setDatabaseType

public void setDatabaseType(DatabaseType db)
Sets the type of database hibernate will use if none is specified.

Parameters:
db - The default database type to set.

initialize

public void initialize(DatabaseType db)
                throws MissingConfigurationFileException,
                       org.hibernate.HibernateException
Initializes hibernate with the specified Hibernate configuration file.

Parameters:
db - The type of database to use.
Throws:
MissingConfigurationFileException
org.hibernate.HibernateException

initialize

public void initialize(java.lang.String configFilePath)
                throws MissingConfigurationFileException,
                       org.hibernate.HibernateException
Initializes hibernate using an alternate project configuration file path.

Parameters:
configFilePath - Path to the project configuration file.
Throws:
MissingConfigurationFileException
org.hibernate.HibernateException

initialize

public void initialize()
                throws MissingConfigurationFileException,
                       org.hibernate.HibernateException
Initializes hibernate.

Throws:
MissingConfigurationFileException
org.hibernate.HibernateException

isConfigured

public boolean isConfigured()
Returns:
Returns true if Hibernate has been configured and initialized, false otherwise.

setConfiguration

private void setConfiguration()
                       throws org.hibernate.HibernateException
Sets the Hibernate configuration that was extracted from the project.

Throws:
org.hibernate.HibernateException

initializeForUnitTests

public void initializeForUnitTests()
Initializes Hibernate for use with the JUnit tests. Hibernate is configured to use the HSQLDB database in data/prc. Using this method instead of initialize() frees the unit tests from the need to have a project configuration file.


createSession

public org.hibernate.Session createSession()
Creates a new Hibernate session to use for database access.

Returns:
A new Hibernate session.

exportSchema

public void exportSchema()
                  throws org.hibernate.HibernateException
Exports the schema to the database. WARNING: This process will first drop all tables in the database, erasing all data!

Throws:
org.hibernate.HibernateException

updateSchema

public void updateSchema()
                  throws org.hibernate.HibernateException
Updates the database schema. This process should not result in any data loss.

Throws:
org.hibernate.HibernateException

shutdown

public void shutdown()
Shuts down Hibernate and closes all open sessions.


instance

public static HibernateController instance()
Returns:
Returns the singleton instance of the HibernateController class.