package in.co.sunrays.proj1.dao;
import in.co.sunrays.proj1.dto.CollegeDTO; 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 CollegeDAO * * @author SUNRAYS Technologies * @version 1.0 * @Copyright (c) SUNRAYS Technologies */ public class CollegeDAOHibImpl extends HibernateDaoSupport implements CollegeDAOInt { /** * Logger */ private static Logger log = Logger.getLogger(CollegeDAOHibImpl.class); /** * Add a College * * @param dto * @throws DatabaseException */ public long add(CollegeDTO 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 in College add"); } log.debug("DAO add End"); return pk; } /** * Update a College * * @param dto * @throws DatabaseException */ @Override public void update(CollegeDTO dto) throws DatabaseException { log.debug("DAO update Started"); try { getHibernateTemplate().update(dto); } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException("Exception in College Update"); } log.debug("DAO update End"); } /** * Delete a College * * @param dto * @throws DatabaseException */ @Override public void delete(CollegeDTO dto) throws DatabaseException { log.debug("DAO delete Started"); try { getHibernateTemplate().delete(dto); } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException("Exception in College delete"); } log.debug("DAO delete End"); } /** * Find College by Name * * @param name * : get parameter * @return dto * @throws DatabaseException */ @Override public CollegeDTO findByName(String name) throws DatabaseException { log.debug("DAO findByName Started"); CollegeDTO dto = null; try { DetachedCriteria criteria = DetachedCriteria .forClass(CollegeDTO.class); criteria.add(Restrictions.eq("name", name)); List list = getHibernateTemplate().findByCriteria(criteria); if (list.size() == 1) { dto = (CollegeDTO) list.get(0); } } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException("Exception in getting User by Login"); } log.debug("DAO findByName End"); return dto; } /** * Find College by PK * * @param pk * : get parameter * @return dto * @throws DatabaseException */ @Override public CollegeDTO findByPK(long pk) throws DatabaseException { log.debug("DAO findByPK Started"); CollegeDTO dto = null; try { dto = (CollegeDTO) getHibernateTemplate().get(CollegeDTO.class, pk); } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in getting College by pk"); } log.debug("DAO findByPK End"); return dto; } /** * Search Colleges with pagination * * @return list : List of Colleges * @param dto * : Search Parameters * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * * @throws DatabaseException */ @Override public List search(CollegeDTO dto, int pageNo, int pageSize) throws DatabaseException { log.debug("DAO search Started"); List list = null; try { DetachedCriteria criteria = DetachedCriteria .forClass(CollegeDTO.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.getAddress() != null && dto.getAddress().length() > 0) { criteria.add(Restrictions.like("address", dto.getAddress() + "%")); } if (dto.getState() != null && dto.getState().length() > 0) { criteria.add(Restrictions.like("state", dto.getState() + "%")); } if (dto.getCity() != null && dto.getCity().length() > 0) { criteria.add(Restrictions.like("city", dto.getCity() + "%")); } // 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 college search"); } log.debug("DAO search End"); return list; } /** * Search Colleges * * @return list : List of Colleges * @param dto * : Search Parameters * @throws DatabaseException */ @Override public List search(CollegeDTO dto) throws DatabaseException { return search(dto, 0, 0); } /** * Get List of Colleges * * @return list : List of College * @throws DatabaseException */ public List list() throws DatabaseException { return list(0, 0); } /** * Get List of Colleges with pagination * * @return list : List of College * @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(CollegeDTO.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; } } |