package jomm.dao.impl;
import org.hibernate.*;
import org.hibernate.impl.CriteriaImpl;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Order;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
import java.sql.SQLException;
import jomm.dao.IDomainObjectDao;
import jomm.dao.DaoException;
import jomm.dao.utils.HibernateUtils;
import pt.estgp.estgweb.domain.DomainObject;
import static org.
hibernate.
criterion.
Restrictions.
*;
/**
* This class will not be overwritten once it has been created. This is the
* superclass of all DAOs.
*/
public abstract class AbstractDao
<CLAZZ
> implements IDomainObjectDao
<CLAZZ
>
{
/**
* Return the number of results that match the given criteria.
*
* @return number count
* @param criteria to count results
*/
protected static long count
(Criteria criteria
) {
// apply the rowCount projection to the criteria
criteria.
setProjection(Projections.
rowCount());
// count the results
1.5.0/docs/api/java/lang/Integer.html">Integer rowCount =
(1.5.0/docs/api/java/lang/Integer.html">Integer) criteria.
uniqueResult();
// disable the projection
criteria.
setProjection(null);
criteria.
setResultTransformer(CriteriaSpecification.
DISTINCT_ROOT_ENTITY);
return rowCount.
longValue();
}
/**
*
* @param maxField field to find max to load object
* @return last inserted object
*/
public CLAZZ getLast
(1.5.0/docs/api/java/lang/String.html">String maxField
)
{
List
<CLAZZ
> objs = createCriteria
()
.
addOrder( Order.
desc(maxField
) )
.
setMaxResults(1
)
.
list();
if(objs
!=
null && objs.
size() > 0
)
return objs.
get(0
);
else
return null;
}
public CLAZZ findMaxId
(1.5.0/docs/api/java/lang/String.html">String field
){
Criteria c = createCriteria
(getReferenceClass
());
c.
setProjection(Projections.
projectionList().
add(Projections.
max(field
)));
c.
setMaxResults(1
);
List
<CLAZZ
> l = c.
list();
if(l==
null)
return null;
else
return l.
get(0
);
}
/**
* Return the specific Object class that will be used for class-specific
* implementation of this DAO.
* @return the reference Class
*/
protected abstract 1.5.0/docs/api/java/lang/Class.html">Class getReferenceClass
();
/**
* Close used resources.
*/
public static void close
()
{
flush
();
commit
();
getCurrentSession
().
close();
}
/**
* Flush used resources.
*/
public static void flush
()
{
try
{
getCurrentSession
().
flush();
}
catch (HibernateException e
) {
throw new DaoException
(e
);
}
}
public static void commit
() {
try
{
getCurrentSession
().
connection().
commit();
}
catch (HibernateException e
)
{
throw new DaoException
(e
);
}
catch (1.5.0/docs/api/java/sql/SQLException.html">SQLException e
)
{
throw new DaoException
(e
);
}
}
/******************
*
*
*Returns the Session object associated with either the current JTA transaction or the current thread.
* @return active examples
* @throws IllegalStateException when examples throws errors
*/
public static Session getCurrentSession
() throws 1.5.0/docs/api/java/lang/IllegalStateException.html">IllegalStateException
{
return HibernateUtils.
getCurrentSession();
}
/**
* Ensure that the given object is initialized from the database.
*
* @param obj to inicialize in examples
*/
public static void initialize
(5+0%2Fdocs%2Fapi+Object">Object obj
) {
try {
Hibernate.
initialize(obj
);
}
catch (HibernateException e
) {
throw new DaoException
(e
);
}
}
/**
* Create a criteria for this DAO using the correct LockMode.
* @return Criteria
*/
protected final Criteria createCriteria
() {
return createCriteria
(getReferenceClass
());
}
/**
* Create a criteria for this DAO and the given persistent class, using the
* correct LockMode.
* @return Criteria
* @param clazz Class to criteria
*/
protected final Criteria createCriteria
(1.5.0/docs/api/java/lang/Class.html">Class clazz
) {
return getCurrentSession
().
createCriteria(clazz
);
}
/**
* Create a Query for this DAO and the given persistent class, using the
* correct LockMode.
* @param queryString Str in HQL
* @return Query from queryString
*/
protected final 1.5.0/docs/api/javax/management/Query.html">Query createQuery
(1.5.0/docs/api/java/lang/String.html">String queryString
) {
try {
return getCurrentSession
().
createQuery(queryString
);
}
catch (HibernateException e
) {
throw new DaoException
(e
);
}
}
/**
* Create an SQLQuery based on the given query string.
*
* @param queryString an SQL query
* @return an SQLQuery
*/
final SQLQuery createSqlQuery
(1.5.0/docs/api/java/lang/String.html">String queryString
) {
try {
return getCurrentSession
().
createSQLQuery(queryString
);
}
catch (HibernateException e
) {
throw new DaoException
(e
);
}
}
/**
* Count all objects of this DAO's reference class.
*/
public final long countAll
() {
try {
// criteria selecting all instances of the referred class
return count
(createCriteria
());
}
catch (HibernateException e
) {
throw new DaoException
(e
);
}
}
/**
* Return all objects related to the implementation of this DAO with no
* filters.
*
* @SuppressWarnings("unchecked")
*/
public List
<CLAZZ
> findAll
() {
try {
return createCriteria
().
list();
}
catch (HibernateException e
) {
throw new DaoException
(e
);
}
}
/**
* Returns results as a page.
*
* Finds "sizeOfPage" objects, starting from the page "pageIndex".
*
* @param pageIndex page number
* @param sizeOfPage results in page
* @return persistent objects
* <BR><B>author <a href="mailto:luismmribeiro@gmail.com">Luis Ribeiro</a></B>
*
* @SuppressWarnings("unchecked")
*/
public List
<CLAZZ
> findByPage
(int pageIndex,
int sizeOfPage
)
{
try
{
Criteria criteria = createCriteria
();
criteria.
setFirstResult(pageIndex
* sizeOfPage
);
criteria.
setMaxResults(sizeOfPage
);
return criteria.
list();
}
catch (HibernateException e
)
{
throw new DaoException
(e
);
}
}
/**
* Obtain an instance of Query for a named query string defined in the
* mapping file.
*
* @param name
* the name of a query defined externally
* @return Query
*/
protected final 1.5.0/docs/api/javax/management/Query.html">Query getNamedQuery
(1.5.0/docs/api/java/lang/String.html">String name
) {
try {
return getCurrentSession
().
getNamedQuery(name
);
}
catch (HibernateException e
) {
throw new DaoException
(e
);
}
}
/**
* Used by the base DAO classes but here for your modification Load object
* matching the given key and return it.
*
* @SuppressWarnings("unchecked")
*/
public final CLAZZ load
(1.5.0/docs/api/java/io/Serializable.html">Serializable dbKey
) {
try
{
return (CLAZZ
) getCurrentSession
().
load(getReferenceClass
(), dbKey
);
}
catch (HibernateException e
)
{
throw new DaoException
(e
);
}
}
/**
* Used by the base DAO classes but here for your modification Load object
* matching the given key and return it.
*
* @SuppressWarnings("unchecked")
*/
public final CLAZZ loadOrNull
(1.5.0/docs/api/java/io/Serializable.html">Serializable dbKey
) {
try
{
return (CLAZZ
) getCurrentSession
().
load(getReferenceClass
(), dbKey
);
}
catch (HibernateException e
)
{
return null;
}
}
public CLAZZ loadOrNullLockUpgrade
(1.5.0/docs/api/java/io/Serializable.html">Serializable dbKey
)
{
return loadOrNullLockWrite
("id",dbKey
);
}
/**
* Used by the base DAO classes but here for your modification Load object
* matching the given key and return it.
*
* @SuppressWarnings("unchecked")
*/
public CLAZZ loadOrNullLockWrite
(1.5.0/docs/api/java/lang/String.html">String field,
1.5.0/docs/api/java/io/Serializable.html">Serializable dbKey
)
{
try
{
return (CLAZZ
) createCriteria
().
add(eq
(field,dbKey
)).
setLockMode(LockMode.
UPGRADE).
uniqueResult();
}
catch (HibernateException e
)
{
return null;
}
}
/**
* Remove this instance from the system cache.
*
* <P>Changes to the instance will not be synchronized with the database.
*
* @param object a persistent instance
*/
public static void evict
(5+0%2Fdocs%2Fapi+Object">Object object
) {
try {
getCurrentSession
().
evict(object
);
}
catch (HibernateException e
) {
throw new DaoException
(e
);
}
}
/**
* Wrap Hibernate's Session.get(Class, Serializable).
*
* @SuppressWarnings("unchecked")
*/
public final CLAZZ get
(1.5.0/docs/api/java/io/Serializable.html">Serializable dbKey
) {
try {
return (CLAZZ
) getCurrentSession
().
get(getReferenceClass
(), dbKey
);
}
catch (HibernateException e
) {
throw new DaoException
(e
);
}
}
/**
* Used by the base DAO classes but here for your modification Persist the
* given transient instance, first assigning a generated identifier. (Or
* using the current value of the identifier property if the assigned
* generator is used.)
*/
public final 1.5.0/docs/api/java/io/Serializable.html">Serializable save
(CLAZZ obj
) {
try {
return (1.5.0/docs/api/java/io/Serializable.html">Serializable) getCurrentSession
().
save(obj
);
}
catch (HibernateException e
) {
throw new DaoException
(e
);
}
}
/**
* Used by the base DAO classes but here for your modification Persist the
* given transient instance, first assigning a generated identifier. (Or
* using the current value of the identifier property if the assigned
* generator is used.)
*/
public final void saveOrUpdate
(CLAZZ obj
)
{
try
{
getCurrentSession
().
saveOrUpdate(obj
);
}
catch (HibernateException e
)
{
throw new DaoException
(e
);
}
}
/**
* Saves a collection of objects without returning the DbKey.
*
* To obtain the DbKey, use the object itself.
*
* @param objs List of objects to save
*/
public final void save
(Collection
<CLAZZ
> objs
)
{
try
{
Session session = getCurrentSession
();
for (CLAZZ obj: objs
)
{
session.
save(obj
);
}
}
catch (HibernateException e
) {
throw new DaoException
(e
);
}
}
/**
* Used by the base DAO classes but here for your modification Either save()
* or update() the given instance, depending upon the value of its
* identifier property.
* @param obj object to save or update
* @throws DaoException when Hibernate trwoes Hibernate Exception
*/
protected final void saveOrReattach
(CLAZZ obj
) throws DaoException
{
try
{
getCurrentSession
().
saveOrUpdate(obj
);
}
catch (HibernateException e
)
{
throw new DaoException
(e
);
}
}
/**
* Used by the base DAO classes but here for your modification Update the
* persistent state associated with the given identifier. An exception is
* thrown if there is a persistent instance with the same identifier in the
* current examples.
*
* @param obj
* a transient instance containing updated state
* @throws DaoException when Hibernate trwoes Hibernate Exception
*/
public final void reattach
(CLAZZ obj
) throws DaoException
{
try
{
getCurrentSession
().
update(obj
);
}
catch (HibernateException e
)
{
throw new DaoException
(e
);
}
}
/**
* Remove this instance from the examples cache. Changes to the instance will
* not be synchronized with the database. This operation cascades to
* associated instances if the association is mapped with cascade="all" or
* cascade="all-delete-orphan".
*
* @param object a persistent instance
* @throws DaoException when Hibernate trwoes Hibernate Exception
*/
protected final void dettach
(CLAZZ object
) throws DaoException
{
try
{
getCurrentSession
().
evict(object
);
}
catch (HibernateException e
) {
throw new DaoException
(e
);
}
}
/**
* Used by the base DAO classes but here for your modification Remove a
* persistent instance from the datastore. The argument may be an instance
* associated with the receiving Session or a transient instance with an
* identifier associated with existing persistent state.
*/
public final void delete
(CLAZZ obj
) {
try
{
getCurrentSession
().
delete(obj
);
}
catch (HibernateException e
)
{
throw new DaoException
(e
);
}
}
/**
* Refresh a collection of objects.
*
* @param objs Objects to delete
*/
public final void refresh
(Collection
<CLAZZ
> objs
)
{
try
{
Session session = getCurrentSession
();
for (CLAZZ obj: objs
)
{
session.
refresh(obj
);
}
}
catch (HibernateException e
)
{
throw new DaoException
(e
);
}
}
/**
* Refresh a collection of objects.
*
* @param objs Objects to delete
*/
public final void evictAndDelete
(Collection
<CLAZZ
> objs
)
{
try
{
Session session = getCurrentSession
();
for (CLAZZ obj: objs
)
{
obj =
(CLAZZ
) session.
load(getReferenceClass
(),
((DomainObject
)obj
).
getSerializable());
session.
delete(obj
);
}
}
catch (HibernateException e
)
{
throw new DaoException
(e
);
}
}
/**
* Deletes a collection of objects.
*
* @param objs Objects to delete
*/
public final void reattach
(Collection
<CLAZZ
> objs
)
{
try
{
Session session = getCurrentSession
();
for (CLAZZ obj: objs
)
{
session.
update(obj
);
}
}
catch (HibernateException e
)
{
throw new DaoException
(e
);
}
}
/**
* Deletes a collection of objects.
*
* @param objs Objects to delete
*/
public final void delete
(Collection
<CLAZZ
> objs
)
{
try
{
Session session = getCurrentSession
();
for (CLAZZ obj: objs
)
{
session.
delete(obj
);
}
}
catch (HibernateException e
)
{
throw new DaoException
(e
);
}
}
/**
* Completely clearObjects the examples. Evict all loaded instances and cancel all pending
* saves, updates and deletions. Do not close open iterators or instances of
* <tt>ScrollableResults</tt>.
*/
public static void clear
()
{
getCurrentSession
().
clear();
}
/**
* Delete an object by its key.
*
* @param dbKey the database key
* @return true or false depending on the operation success
*/
public boolean delete
(1.5.0/docs/api/java/io/Serializable.html">Serializable dbKey
)
{
try
{
final CLAZZ object = load
(dbKey
);
getCurrentSession
().
delete(object
);
return true;
}
catch (HibernateException e
)
{
throw new DaoException
(e
);
}
}
/**
* Apply the dbKeys filters for the objects being filtered by the given criteria.
*
*/
// protected static void applyDbKeysFilter(Criteria criteria, List objectDbKeys) {
//
// // if there are no matches then we need a constraint
// // that will always evaluate to false
// if (objectDbKeys.isEmpty()) {
// criteria.add(Restrictions.isNull(PROPERTY_id));
// }
// else {
// Disjunction disjunction = Restrictions.disjunction();
//
// for (Iterator it = objectDbKeys.iterator(); it.hasNext(); ) {
// disjunction.add(Restrictions.eq(PROPERTY_id, it.next()));
// }
//
// criteria.add(disjunction);
// }
// }
/**
* Return the given criteria with additional paging constraints applied to it.
* @return Criteria paged
* @param criteria criteria to turn paged
* @param pageSize page size in results
* @param pageNumber actual page
* @param count number of pages
*/
protected static Criteria createPagedCriteria
(Criteria criteria,
int pageSize,
int pageNumber,
long count
) {
if (pageSize
>= 0
&& pageSize
< count
) {
return criteria.
setFirstResult(pageSize
* pageNumber
)
.
setMaxResults(pageSize
);
}
return criteria
;
}
}