gov.nih.nci.caarray.util
Class CaArrayHibernateHelperImpl

java.lang.Object
  extended by com.fiveamsolutions.nci.commons.util.HibernateHelper
      extended by com.fiveamsolutions.nci.commons.util.CsmEnabledHibernateHelper
          extended by gov.nih.nci.caarray.util.CaArrayHibernateHelperImpl
All Implemented Interfaces:
CaArrayHibernateHelper

public final class CaArrayHibernateHelperImpl
extends com.fiveamsolutions.nci.commons.util.CsmEnabledHibernateHelper
implements CaArrayHibernateHelper

Utility class to create and retrieve Hibernate sessions. Most methods are pass-throughs to HibernateHelper, except for the methods involving filters.

Author:
Rashmi Srinivasa

Field Summary
 
Fields inherited from class com.fiveamsolutions.nci.commons.util.HibernateHelper
MAX_IN_CLAUSE_LENGTH
 
Method Summary
 java.lang.String buildInClauses(java.util.List<? extends java.io.Serializable> items, java.lang.String columnName, java.util.Map<java.lang.String,java.util.List<? extends java.io.Serializable>> blocks)
          Break up a list of items into separate in clauses, to avoid limits imposed by databases or by Hibernate bug http://opensource.atlassian.com/projects/hibernate/browse/HHH-2166.
static CaArrayHibernateHelper create()
           
 void disableFilters()
          Disable security filters on the current session.
 java.lang.Object doUnfiltered(UnfilteredCallback uc)
          Do something in an unfiltered session.
 org.hibernate.Session getCurrentSession()
          Returns the current Hibernate session.
 java.sql.Connection getNewConnection()
          
 void initialize()
          Because of circular references (for instance, SecurityUtils references this object through the static factory), we must use a two stage construction/initialization process.
 void openAndBindSession()
          Open a hibernate session and bind it as the current session via org.hibernate.context.ManagedSessionContext#bind(org.hibernate.classic.Session).
 void setFiltersEnabled(boolean enable)
          Set whether security filters should be enabled for the next session returned from getCurrentSession().
 void setQueryParams(java.util.Map<java.lang.String,java.lang.Object> params, org.hibernate.Query q)
          Sets the named parameters in the given query from the given map.
 java.lang.Object unwrapProxy(java.lang.Object entity)
          If entity is a hibernate proxy, return the actual object it proxies, otherwise return the entity itself.
 
Methods inherited from class com.fiveamsolutions.nci.commons.util.CsmEnabledHibernateHelper
initializeConfig, modifyConfig, reinitializeCsmFilters, setSecurity
 
Methods inherited from class com.fiveamsolutions.nci.commons.util.HibernateHelper
beginTransaction, bindInClauseParameters, buildInClause, buildSessionFactory, convertMapListToMapArray, getClassValidator, getConfiguration, getSessionFactory, isManagedSession, openAndBindSession, rollbackTransaction, unbindAndCleanupSession, unbindAndCleanupSession, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface gov.nih.nci.caarray.util.CaArrayHibernateHelper
beginTransaction, getConfiguration, getSessionFactory, rollbackTransaction, unbindAndCleanupSession
 

Method Detail

create

public static CaArrayHibernateHelper create()
Returns:
a new CaArrayHibernateHelperImpl

initialize

public void initialize()
Because of circular references (for instance, SecurityUtils references this object through the static factory), we must use a two stage construction/initialization process. The factory will call initialize() after construction, allowing SecurityUtils and others to access this object before it is fully initialized. This needs to be refactored away.

Overrides:
initialize in class com.fiveamsolutions.nci.commons.util.HibernateHelper

getCurrentSession

public org.hibernate.Session getCurrentSession()
Returns the current Hibernate session. Note that this returns a special session that can be used only in the context of a transaction. (Assuming that the hibernate properties are set to use a JTA or JDBC transaction factory.)

Specified by:
getCurrentSession in interface CaArrayHibernateHelper
Overrides:
getCurrentSession in class com.fiveamsolutions.nci.commons.util.HibernateHelper
Returns:
a Hibernate session.
See Also:
HibernateHelper#getCurrentSession()

setFiltersEnabled

public void setFiltersEnabled(boolean enable)
Set whether security filters should be enabled for the next session returned from getCurrentSession(). This should generally only be called via test code.

Specified by:
setFiltersEnabled in interface CaArrayHibernateHelper
Parameters:
enable - whether the filters should be enabled.

openAndBindSession

public void openAndBindSession()
Open a hibernate session and bind it as the current session via org.hibernate.context.ManagedSessionContext#bind(org.hibernate.classic.Session). The hibernate property "hibernate.current_session_context_class" must be set to "managed" for this to have effect This method should be called from within an Interceptor or Filter type class that is setting up the scope of the Session. This method should then call CaArrayHibernateHelper.unbindAndCleanupSession() when the scope of the Session is expired. Clears the SecurityInterceptor.

Specified by:
openAndBindSession in interface CaArrayHibernateHelper
Overrides:
openAndBindSession in class com.fiveamsolutions.nci.commons.util.HibernateHelper
See Also:
HibernateHelper#openAndBindSession()

getNewConnection

public java.sql.Connection getNewConnection()
                                     throws java.sql.SQLException

Specified by:
getNewConnection in interface CaArrayHibernateHelper
Returns:
a new Connection from the data source underlying the hibernate session factory
Throws:
java.sql.SQLException - if an error occurs obtaining the connection

doUnfiltered

public java.lang.Object doUnfiltered(UnfilteredCallback uc)
Do something in an unfiltered session.

Specified by:
doUnfiltered in interface CaArrayHibernateHelper
Parameters:
uc - callback class
Returns:
the result

disableFilters

public void disableFilters()
Disable security filters on the current session.

Specified by:
disableFilters in interface CaArrayHibernateHelper

setQueryParams

public void setQueryParams(java.util.Map<java.lang.String,java.lang.Object> params,
                           org.hibernate.Query q)
Sets the named parameters in the given query from the given map.

Specified by:
setQueryParams in interface CaArrayHibernateHelper
Parameters:
params - map of parameter name -> value(s) for that named parameter.
q - the query

buildInClauses

public java.lang.String buildInClauses(java.util.List<? extends java.io.Serializable> items,
                                       java.lang.String columnName,
                                       java.util.Map<java.lang.String,java.util.List<? extends java.io.Serializable>> blocks)
Break up a list of items into separate in clauses, to avoid limits imposed by databases or by Hibernate bug http://opensource.atlassian.com/projects/hibernate/browse/HHH-2166.

Specified by:
buildInClauses in interface CaArrayHibernateHelper
Parameters:
items - list of items to include in the in clause
columnName - name of column to match against the list
blocks - empty Map of HQL param name to param list of values to be set in the HQL query - it will be populated by the method
Returns:
full HQL "in" clause, of the form: " columnName in (:block1) or ... or columnName in (:blockN)"

unwrapProxy

public java.lang.Object unwrapProxy(java.lang.Object entity)
If entity is a hibernate proxy, return the actual object it proxies, otherwise return the entity itself.

Specified by:
unwrapProxy in interface CaArrayHibernateHelper
Parameters:
entity - the object to unwrap (if it is a proxy)
Returns:
the unwrapped proxy, or original object.