edu.internet2.middleware.grouper.hibernate
Class HibUtils

java.lang.Object
  extended by edu.internet2.middleware.grouper.hibernate.HibUtils

public class HibUtils
extends Object

Author:
mchyzer

Constructor Summary
HibUtils()
           
 
Method Summary
static void assignProperty(Object[] state, String[] propertyNames, String propertyName, Object propertyValue)
          assign a property in hibernates arrays of states
static void attachParams(PreparedStatement statement, Object params)
          Attach params for a prepared statement.
static void closeQuietly(Connection connection)
          close a connection null safe and dont throw exception
static void closeQuietly(PreparedStatement preparedStatement)
          close a prepared statement
static void closeQuietly(ResultSet resultSet)
          close a resultSet null safe and dont throw exception
static void closeQuietly(org.hibernate.ScrollableResults scrollableResults)
           
static void closeQuietly(org.hibernate.Session session)
          close a session null safe and dont throw exception
static void closeQuietly(Statement statement)
          close a statement null safe and dont throw exception
static void convertFieldsToSqlInString(Collection<Field> fields, HqlQuery hqlQuery, StringBuilder sql, String fieldColumnName)
           
static String convertHqlToCountHql(String hql)
          convert an hql to a count hql
static String convertSourcesToSqlInString(Set<edu.internet2.middleware.subject.Source> sources)
          e.g.
static void convertSourcesToSqlInString(Set<edu.internet2.middleware.subject.Source> sources, HqlQuery hqlQuery, StringBuilder sql, String sourceColumnName)
           
static String convertToInClause(Collection<String> collection, HqlQuery scalarable)
          convert a collection of strings (no parens) to an in clause
static String convertToInClauseAnyType(Collection<?> collection, HqlQuery scalarable)
          convert a collection of strings (no parens) to an in clause
static String convertToInClauseForSqlStatic(Collection<String> collection)
          convert a collection of strings (no parens) to an in clause
static void convertToMultiKeyInClause(Collection<org.apache.commons.collections.keyvalue.MultiKey> collection, HqlQuery scalarable, Collection<String> columnNames, StringBuilder whereClause)
          convert a collection of multikeys to an in clause with multiple args.
static String convertToSubjectInClause(Collection<edu.internet2.middleware.subject.Subject> subjects, HqlQuery hqlQuery, String memberAlias)
           
static String equalsOrIs(Object value, String bindVar)
          if in an hql or sql query, depending on the value, pass is or = back
static String escapeSqlString(String input)
          escape the quotes from sql string
static void evict(HibernateSession hibernateSession, Collection<Object> list, boolean onlyEvictIfNotNew)
           evict a list of objects from hibernate.
static void evict(HibernateSession hibernateSession, Object object, boolean onlyEvictIfNotNew)
           evict a list of objects from hibernate.
static void evict(HibernateSession hibernateSession, Object object, boolean onlyEvictIfNotNew, boolean evictBeforeFlush)
           evict a list of objects from hibernate.
static org.hibernate.type.Type hibernateType(Object o)
          Returns a Hibernate Type for the given java type.
static List<org.hibernate.type.Type> hibernateTypes(List<Object> params)
          Returns a list of Hibernate types corresponding to the given params.
static org.hibernate.criterion.Criterion listCrit(org.hibernate.criterion.Criterion... criterions)
          make a list of criterions.
static org.hibernate.criterion.Criterion listCrit(List<org.hibernate.criterion.Criterion> criterions)
          make a list of criterions.
static org.hibernate.criterion.Criterion listCritOr(org.hibernate.criterion.Criterion... criterions)
          make a list of criterions.
static org.hibernate.criterion.Criterion listCritOr(List<org.hibernate.criterion.Criterion> criterions)
          make a list of criterions.
static List<Object> listObject(Object object)
          convert an object to a list of objects
static String paramsToString(Object params, Object types)
          Convert the params to friendly strings
static String parseAlias(String text, boolean exceptionIfNotFound)
          Parses an alias out of a sql query.
static int propertyIndex(String[] propertyNames, String propertyName)
          find the property index based on property name
static Object propertyValue(Object[] state, String[] propertyNames, String propertyName)
          find a property value in hibernates arrays of states
static void rollbackQuietly(Connection connection)
          rollback a connection quietly
static void rollbackQuietly(org.hibernate.Transaction transaction)
          rollback a transaction quietly
static String secondLevelCacheRegion(String cacheRegion, QueryOptions queryOptions)
           
static boolean secondLevelCaching(Boolean cacheable, QueryOptions queryOptions)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibUtils

public HibUtils()
Method Detail

paramsToString

public static String paramsToString(Object params,
                                    Object types)
Convert the params to friendly strings

Parameters:
params -
types -
Returns:
the string of the params (for logging)

parseAlias

public static String parseAlias(String text,
                                boolean exceptionIfNotFound)
Parses an alias out of a sql query.

Parameters:
text - is the text to find the alias in.
exceptionIfNotFound - if true, when an alias is not found, and exception will be thrown.
Returns:
the alias.

equalsOrIs

public static String equalsOrIs(Object value,
                                String bindVar)
if in an hql or sql query, depending on the value, pass is or = back

Parameters:
value -
bindVar -
Returns:
the query comparator

secondLevelCaching

public static boolean secondLevelCaching(Boolean cacheable,
                                         QueryOptions queryOptions)
Parameters:
cacheable -
queryOptions -
Returns:
if caching

secondLevelCacheRegion

public static String secondLevelCacheRegion(String cacheRegion,
                                            QueryOptions queryOptions)
Parameters:
cacheRegion -
queryOptions -
Returns:
if caching

convertHqlToCountHql

public static String convertHqlToCountHql(String hql)
convert an hql to a count hql

Parameters:
hql -
Returns:
the hql of the count query

closeQuietly

public static void closeQuietly(org.hibernate.ScrollableResults scrollableResults)
Parameters:
scrollableResults -

propertyIndex

public static int propertyIndex(String[] propertyNames,
                                String propertyName)
find the property index based on property name

Parameters:
propertyNames -
propertyName - e.g. userId
Returns:
the index (0 based) in the data arrays where the object is

assignProperty

public static void assignProperty(Object[] state,
                                  String[] propertyNames,
                                  String propertyName,
                                  Object propertyValue)
assign a property in hibernates arrays of states

Parameters:
state -
propertyNames -
propertyName -
propertyValue -

propertyValue

public static Object propertyValue(Object[] state,
                                   String[] propertyNames,
                                   String propertyName)
find a property value in hibernates arrays of states

Parameters:
state -
propertyNames -
propertyName -
Returns:
the object

closeQuietly

public static void closeQuietly(PreparedStatement preparedStatement)
close a prepared statement

Parameters:
preparedStatement -

evict

public static void evict(HibernateSession hibernateSession,
                         Object object,
                         boolean onlyEvictIfNotNew)
 evict a list of objects from hibernate.  do this always for two reasons:
 1. If you edit an object that is in the hibernate session, and commit, it will
 commit those changes magically.  Only objects called session.save(obj) or 
 update etc should be committed
 2. If you select an object, then try to store it back (but have a different
 reference, e.g. if the DTO went through it, then you will get an exception:
 "a different object with the same identifier value was already associated with the session"
 

Parameters:
hibernateSession - grouper hibernateSession, can be null if not known
object - to evict that was just retrieved, can be list or array
onlyEvictIfNotNew - true to only evict if this is a nested tx

evict

public static void evict(HibernateSession hibernateSession,
                         Object object,
                         boolean onlyEvictIfNotNew,
                         boolean evictBeforeFlush)
 evict a list of objects from hibernate.  do this always for two reasons:
 1. If you edit an object that is in the hibernate session, and commit, it will
 commit those changes magically.  Only objects called session.save(obj) or 
 update etc should be committed
 2. If you select an object, then try to store it back (but have a different
 reference, e.g. if the DTO went through it, then you will get an exception:
 "a different object with the same identifier value was already associated with the session"
 

Parameters:
hibernateSession - grouper hibernateSession, can be null if not known
object - to evict that was just retrieved, can be list or array
onlyEvictIfNotNew - true to only evict if this is a nested tx
evictBeforeFlush - if evict before flush (dont do this if iterating through list)

evict

public static void evict(HibernateSession hibernateSession,
                         Collection<Object> list,
                         boolean onlyEvictIfNotNew)
 evict a list of objects from hibernate.  do this always for two reasons:
 1. If you edit an object that is in the hibernate session, and commit, it will
 commit those changes magically.  Only objects called session.save(obj) or 
 update etc should be committed
 2. If you select an object, then try to store it back (but have a different
 reference, e.g. if the DTO went through it, then you will get an exception:
 "a different object with the same identifier value was already associated with the session"
 

Parameters:
hibernateSession - grouper hibernateSession
list - of objects from hibernate to evict
onlyEvictIfNotNew - true to only evict if this is a nested tx

listCrit

public static org.hibernate.criterion.Criterion listCrit(org.hibernate.criterion.Criterion... criterions)
make a list of criterions. e.g. listCrit(crit1, crit2, etc). will AND them together this is null and empty safe

Parameters:
criterions -
Returns:
the criterion containing the list or null if none passed in

listCrit

public static org.hibernate.criterion.Criterion listCrit(List<org.hibernate.criterion.Criterion> criterions)
make a list of criterions. e.g. listCrit(critList). will AND them together this is null and empty safe

Parameters:
criterions -
Returns:
the criterion containing the list or null if none passed in

listCritOr

public static org.hibernate.criterion.Criterion listCritOr(org.hibernate.criterion.Criterion... criterions)
make a list of criterions. e.g. listCrit(crit1, crit2, etc). will OR them together this is null and empty safe

Parameters:
criterions -
Returns:
the criterion containing the list or null if none passed in

listCritOr

public static org.hibernate.criterion.Criterion listCritOr(List<org.hibernate.criterion.Criterion> criterions)
make a list of criterions. e.g. listCrit(crits). will OR them together this is null and empty safe

Parameters:
criterions -
Returns:
the criterion containing the list or null if none passed in

closeQuietly

public static void closeQuietly(Connection connection)
close a connection null safe and dont throw exception

Parameters:
connection -

closeQuietly

public static void closeQuietly(ResultSet resultSet)
close a resultSet null safe and dont throw exception

Parameters:
resultSet -

closeQuietly

public static void closeQuietly(org.hibernate.Session session)
close a session null safe and dont throw exception

Parameters:
session -

closeQuietly

public static void closeQuietly(Statement statement)
close a statement null safe and dont throw exception

Parameters:
statement -

rollbackQuietly

public static void rollbackQuietly(Connection connection)
rollback a connection quietly

Parameters:
connection -

rollbackQuietly

public static void rollbackQuietly(org.hibernate.Transaction transaction)
rollback a transaction quietly

Parameters:
transaction -

attachParams

public static void attachParams(PreparedStatement statement,
                                Object params)
                         throws org.hibernate.HibernateException,
                                SQLException
Attach params for a prepared statement. The type of the params and types must be the same (e.g. either both array or list, but not one is Array, and the other list

Parameters:
statement -
params - either null, Object, Object[], or List of Objects
Throws:
org.hibernate.HibernateException
SQLException

listObject

public static List<Object> listObject(Object object)
convert an object to a list of objects

Parameters:
object -
Returns:
the list of objects

hibernateType

public static org.hibernate.type.Type hibernateType(Object o)
Returns a Hibernate Type for the given java type. Handles both primitives and Objects. Will throw an exception if the given object is null or if a type cannot be found for it.

Parameters:
o - is the object to find the Type for.
Returns:
the Type.

hibernateTypes

public static List<org.hibernate.type.Type> hibernateTypes(List<Object> params)
Returns a list of Hibernate types corresponding to the given params.

Parameters:
params - are the objects to get the types for. Can be list, Object, or array.
Returns:
the corresponding types.

convertToInClause

public static String convertToInClause(Collection<String> collection,
                                       HqlQuery scalarable)
convert a collection of strings (no parens) to an in clause

Parameters:
collection -
scalarable - to set the string
Returns:
the string of in clause (without parens)

convertToInClauseAnyType

public static String convertToInClauseAnyType(Collection<?> collection,
                                              HqlQuery scalarable)
convert a collection of strings (no parens) to an in clause

Parameters:
collection -
scalarable - to set the string
Returns:
the string of in clause (without parens)

convertToSubjectInClause

public static String convertToSubjectInClause(Collection<edu.internet2.middleware.subject.Subject> subjects,
                                              HqlQuery hqlQuery,
                                              String memberAlias)
Parameters:
memberAlias - is the alias of the table for members, e.g. gm
subjects - collection of subjects
hqlQuery - so far
Returns:
the query, e.g. ((gm.subject_id = '123' and gm.subject_source = 'jdbc') or (gm.subject_id = '234' and gm.subject_source = 'jdbc' ))

escapeSqlString

public static String escapeSqlString(String input)
escape the quotes from sql string

Parameters:
input -
Returns:
the escaped string

convertToInClauseForSqlStatic

public static String convertToInClauseForSqlStatic(Collection<String> collection)
convert a collection of strings (no parens) to an in clause

Parameters:
collection -
Returns:
the string of in clause (without parens)

convertToMultiKeyInClause

public static void convertToMultiKeyInClause(Collection<org.apache.commons.collections.keyvalue.MultiKey> collection,
                                             HqlQuery scalarable,
                                             Collection<String> columnNames,
                                             StringBuilder whereClause)
convert a collection of multikeys to an in clause with multiple args. currently this only works with strings, though we could add support for more types in future

Parameters:
collection -
scalarable - to set the string
columnNames - names of columns in multikey
whereClause -

convertFieldsToSqlInString

public static void convertFieldsToSqlInString(Collection<Field> fields,
                                              HqlQuery hqlQuery,
                                              StringBuilder sql,
                                              String fieldColumnName)
Parameters:
fields -
hqlQuery -
sql - before the and statement
fieldColumnName -

convertSourcesToSqlInString

public static void convertSourcesToSqlInString(Set<edu.internet2.middleware.subject.Source> sources,
                                               HqlQuery hqlQuery,
                                               StringBuilder sql,
                                               String sourceColumnName)
Parameters:
sources -
hqlQuery -
sql - before the and statement
sourceColumnName -

convertSourcesToSqlInString

public static String convertSourcesToSqlInString(Set<edu.internet2.middleware.subject.Source> sources)
e.g. ('g:gsa', 'jdbc')

Parameters:
sources -
Returns:
the in string, of sources sorted alphabetically