package in.co.sunrays.proj1.dao;
import in.co.sunrays.proj1.dto.StudentDTO; 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 StudentDAO * * @author SUNRAYS Technologies * @version 1.0 * @Copyright (c) SUNRAYS Technologies */ public class StudentDAOHibImpl extends HibernateDaoSupport implements StudentDAOInt { /** * Logger */ private static Logger log = Logger.getLogger(StudentDAOHibImpl.class); /** * Add a Student * * @param dto * @throws DatabaseException */ public long add(StudentDTO 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 Student"); } log.debug("DAO add End"); return pk; } /** * Update a Student * * @param dto * @throws DatabaseException */ @Override public void update(StudentDTO 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 Student"); } log.debug("DAO update End"); } /** * Delete a Student * * @param dto * @throws DatabaseException */ @Override public void delete(StudentDTO 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 Student"); } log.debug("DAO delete End"); } /** * Find Student by Email * * @param email * : get parameter * @return dto * @throws DatabaseException */ @Override public StudentDTO findByEmail(String email) throws DatabaseException { log.debug("DAO findByEmail Started"); StudentDTO dto = null; try { DetachedCriteria criteria = DetachedCriteria .forClass(StudentDTO.class); criteria.add(Restrictions.like("email", email)); List list = getHibernateTemplate().findByCriteria(criteria); if (list.size() == 1) { dto = (StudentDTO) list.get(0); } } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in getting Student by email"); } log.debug("DAO findByEmail End"); return dto; } /** * Find Student by PK * * @param pk * : get parameter * @return dto * @throws DatabaseException */ @Override public StudentDTO findByPK(long pk) throws DatabaseException { log.debug("DAO findByPK Started"); StudentDTO dto = null; try { dto = (StudentDTO) getHibernateTemplate().get(StudentDTO.class, pk); } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in getting Student by pk"); } log.debug("DAO findByPK End"); return dto; } /** * Search Student with pagination * * @return list : List of Student * @param dto * : Search Parameters * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * @throws DatabaseException */ @Override public List search(StudentDTO dto, int pageNo, int pageSize) throws DatabaseException { log.debug("DAO search Started"); List list = null; try { DetachedCriteria criteria = DetachedCriteria .forClass(StudentDTO.class); if (dto.getId() > 0) { criteria.add(Restrictions.eq("id", dto.getId())); } if (dto.getFirstName() != null && dto.getFirstName().length() > 0) { criteria.add(Restrictions.like("firstName", dto.getFirstName() + "%")); } if (dto.getLastName() != null && dto.getLastName().length() > 0) { criteria.add(Restrictions.like("lastName", dto.getLastName() + "%")); } if (dto.getDob() != null) { criteria.add(Restrictions.eq("dob", dto.getDob())); } if (dto.getMobileNo() != null && dto.getMobileNo().length() > 0) { criteria.add(Restrictions.like("mobileNo", dto.getMobileNo() + "%")); } if (dto.getEmail() != null && dto.getEmail().length() > 0) { criteria.add(Restrictions.like("email", dto.getEmail() + "%")); } // 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 Student"); } log.debug("DAO search End"); return list; } /** * Search Students * * @return list : List of Student * @param dto * : Search Parameters * @throws DatabaseException */ @Override public List search(StudentDTO dto) throws DatabaseException { return search(dto, 0, 0); } /** * Gets List of Student * * @return list : List of Students * @throws DatabaseException */ @Override public List list() throws DatabaseException { return list(0, 0); } /** * Get List of Students with pagination * * @return list : List of Students * @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(StudentDTO.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 Student list"); } log.debug("DAO list End"); return list; } } |