package in.co.sunrays.proj1.dao;
import in.co.sunrays.proj1.dto.RoleDTO; import in.co.sunrays.proj1.exception.DatabaseException; import java.util.List; import org.apache.log4j.Logger; import org.hibernate.HibernateException; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Restrictions; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; /** * Hibernate Implementation of RoleDAO * * @author SUNRAYS Technologies * @version 1.0 * @Copyright (c) SUNRAYS Technologies */ public class RoleDAOHibImpl extends HibernateDaoSupport implements RoleDAOInt { /** * Logger */ private static Logger log = Logger.getLogger(RoleDAOHibImpl.class); /** * Add a Role * * @param dto * @throws DatabaseException */ public long add(RoleDTO dto) throws DatabaseException { log.debug("DAO add Started"); long pk = 0; try { pk = (Long) getHibernateTemplate().save(dto); } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException("Exception : Exception in add Role"); } log.debug("DAO add End"); return pk; } /** * Update a Role * * @param dto * @throws DatabaseException */ @Override public void update(RoleDTO dto) throws DatabaseException { log.debug("DAO update Started"); try { getHibernateTemplate().update(dto); } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException("Exception : Exception in update Role"); } log.debug("DAO update End"); } /** * Delete a Role * * @param dto * @throws DatabaseException */ @Override public void delete(RoleDTO dto) throws DatabaseException { log.debug("DAO delete Started"); try { getHibernateTemplate().delete(dto); } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException("Exception : Exception in delete Role"); } log.debug("DAO delete End"); } /** * Find Role by Name * * @param name * : get parameter * @return dto * @throws DatabaseException */ @Override public RoleDTO findByName(String name) throws DatabaseException { log.debug("DAO findByName Started"); RoleDTO dto = null; try { DetachedCriteria criteria = DetachedCriteria .forClass(RoleDTO.class); criteria.add(Restrictions.like("name", name)); List list = getHibernateTemplate().findByCriteria(criteria); if (list.size() == 1) { dto = (RoleDTO) list.get(0); } } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in getting Role by name"); } log.debug("DAO findByName End"); return dto; } /** * Find Role by PK * * @param pk * : get parameter * @return dto * @throws DatabaseException */ @Override public RoleDTO findByPK(long pk) throws DatabaseException { log.debug("DAO findByPK Started"); RoleDTO dto = null; try { dto = (RoleDTO) getHibernateTemplate().get(RoleDTO.class, pk); } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in getting Role by pk"); } log.debug("DAO findByPK End"); return dto; } /** * Search Roles with pagination * * @return list : List of Role * @param dto * : Search Parameters * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * @throws DatabaseException */ @Override public List search(RoleDTO dto, int pageNo, int pageSize) throws DatabaseException { log.debug("DAO search Started"); List list = null; try { DetachedCriteria criteria = DetachedCriteria .forClass(RoleDTO.class); if (dto.getId() > 0) { criteria.add(Restrictions.eq("id", dto.getId())); } if (dto.getName() != null && dto.getName().length() > 0) { criteria.add(Restrictions.like("name", dto.getName() + "%")); } if (dto.getDescription() != null && dto.getDescription().length() > 0) { criteria.add(Restrictions.like("description", dto.getDescription() + "%")); } // if page size is greater than zero the apply pagination if (pageSize > 0) { list = getHibernateTemplate().findByCriteria(criteria, ((pageNo - 1) * pageSize), pageSize); } else { list = getHibernateTemplate().findByCriteria(criteria); } } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException("Exception in search Role"); } log.debug("DAO search End"); return list; } /** * Search Roles * * @return list : List of Role * @param dto * : Search Parameters * @throws DatabaseException */ @Override public List search(RoleDTO dto) throws DatabaseException { return search(dto, 0, 0); } /** * Get List of Roles * * @return list : List of Roles * @throws DatabaseException */ @Override public List list() throws DatabaseException { return list(0, 0); } /** * Get List of Roles with pagination * * @return list : List of Roles * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * @throws DatabaseException */ public List list(int pageNo, int pageSize) throws DatabaseException { log.debug("DAO list Started"); List list = null; try { DetachedCriteria criteria = DetachedCriteria .forClass(RoleDTO.class); // if page size is greater than zero then apply pagination if (pageSize > 0) { list = getHibernateTemplate().findByCriteria(criteria, ((pageNo - 1) * pageSize), pageSize); } else { list = getHibernateTemplate().findByCriteria(criteria); } } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException("Exception : Exception in Role list"); } log.debug("DAO list End"); return list; } } |