package in.co.sunrays.proj1.dao;
import in.co.sunrays.proj1.dto.MarksheetDTO; import in.co.sunrays.proj1.exception.DatabaseException; import in.co.sunrays.proj1.exception.DuplicateRecordException; 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 MarksheetDAO * * @author SUNRAYS Technologies * @version 1.0 * @Copyright (c) SUNRAYS Technologies */ public class MarksheetDAOHibImpl extends HibernateDaoSupport implements MarksheetDAOInt { /** * Logger */ private static Logger log = Logger.getLogger(MarksheetDAOHibImpl.class); /** * Add a Marksheet * * @param dto * @throws DatabaseException */ public long add(MarksheetDTO 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 marksheet"); } log.debug("DAO add End"); return pk; } /** * Update a Marksheet * * @param dto * @throws DatabaseException */ @Override public void update(MarksheetDTO 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 marksheet"); } log.debug("DAO update End"); } /** * Delete a Marksheet * * @param dto * @throws DatabaseException */ @Override public void delete(MarksheetDTO 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 marksheet"); } log.debug("DAO delete End"); } /** * Find Marksheet by Roll No * * @param rollNo * : get parameter * @return dto * @throws DuplicateRecordException */ @Override public MarksheetDTO findByRollNo(String rollNo) throws DatabaseException { log.debug("DAO findByRollNo Started"); MarksheetDTO dto = null; try { DetachedCriteria criteria = DetachedCriteria .forClass(MarksheetDTO.class); criteria.add(Restrictions.eq("rollNo", rollNo)); List list = getHibernateTemplate().findByCriteria(criteria); if (list.size() == 1) { dto = (MarksheetDTO) list.get(0); } } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in getting marksheet by rollno"); } log.debug("DAO findByRollNo End"); return dto; } /** * Find Marksheet by PK * * @param pk * : get parameter * @return dto * @throws DatabaseException */ @Override public MarksheetDTO findByPK(long pk) throws DatabaseException { log.debug("DAO findByPK Started"); MarksheetDTO dto = null; try { dto = (MarksheetDTO) getHibernateTemplate().get(MarksheetDTO.class, pk); } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in getting marksheet by pk"); } log.debug("DAO findByPK End"); return dto; } /** * Search Marksheets * * @param dto * : Search Parameters * @throws DatabaseException */ @Override public List search(MarksheetDTO dto) throws DatabaseException { return search(dto, 0, 0); } /** * Search Marksheets with pagination * * @return list : List of Marksheets * @param dto * : Search Parameters * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * @throws DatabaseException */ @Override public List search(MarksheetDTO dto, int pageNo, int pageSize) throws DatabaseException { log.debug("DAO search Started"); List list = null; try { DetachedCriteria criteria = DetachedCriteria .forClass(MarksheetDTO.class); if (dto.getId() > 0) { criteria.add(Restrictions.eq("id", dto.getId())); } if (dto.getRollNo() != null && dto.getRollNo().length() > 0) { criteria.add(Restrictions.eq("rollNo", dto.getRollNo())); } if (dto.getName() != null && dto.getName().length() > 0) { criteria.add(Restrictions.like("name", dto.getName() + "%")); } if (dto.getPhysics() != null && dto.getPhysics() > 0) { criteria.add(Restrictions.eq("physics", dto.getPhysics())); } if (dto.getChemistry() != null && dto.getChemistry() > 0) { criteria.add(Restrictions.eq("chemistry", dto.getChemistry())); } if (dto.getMaths() != null && dto.getMaths() > 0) { criteria.add(Restrictions.eq("maths", dto.getMaths())); } // 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 marksheet "); } log.debug("DAO search End"); return list; } /** * Get List of Marksheets * * @return list : List of Marksheets * @throws DatabaseException */ @Override public List list() throws DatabaseException { return list(0, 0); } /** * Get List of Marksheets with pagination * * @return list : List of Marksheets * @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(MarksheetDTO.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 marksheet list"); } log.debug("DAO list End"); return list; } /** * Get Merit List of Marksheets with pagination * * @return list : List of Marksheets * @param pageNo * : Current Page No * @param pageSize * : Size of Page * @throws DatabaseException */ @Override public List getMeritList(int pageNo, int pageSize) throws DatabaseException { log.debug("DAO getMeritList Started"); List list = null; try { list = getHibernateTemplate() .find("from MarksheetDTO order by (physics + chemistry + maths) desc"); } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException("Exception : Exception in meritlist"); } log.debug("DAO getMeritList End"); return list; } } |