package in.co.sunrays.proj1.dao;
import in.co.sunrays.proj1.dto.UserDTO; 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 UserDAO * * @author SUNRAYS Technologies * @version 1.0 * @Copyright (c) SUNRAYS Technologies */ public class UserDAOHibImpl extends HibernateDaoSupport implements UserDAOInt { /** * Logger */ private static Logger log = Logger.getLogger(UserDAOHibImpl.class); /** * Add a User * * @param dto * @throws DatabaseException */ public long add(UserDTO 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 user"); } log.debug("DAO add End"); return pk; } /** * Update a User * * @param dto * @throws DatabaseException */ @Override public void update(UserDTO 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 User"); } log.debug("DAO update End"); } /** * Delete a User * * @param dto * @throws DatabaseException */ @Override public void delete(UserDTO 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 User"); } log.debug("DAO delete End"); } /** * Find User by Login * * @param login * : get parameter * @return dto * @throws DatabaseException */ @Override public UserDTO findByLogin(String login) throws DatabaseException { log.debug("DAO findByLoginId Started"); UserDTO dto = null; try { DetachedCriteria criteria = DetachedCriteria .forClass(UserDTO.class); criteria.add(Restrictions.eq("login", login)); List list = getHibernateTemplate().findByCriteria(criteria); if (list.size() == 1) { dto = (UserDTO) list.get(0); } } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in getting User by Login"); } log.debug("DAO findByLoginId End"); return dto; } /** * Find User by PK * * @param pk * : get parameter * @return dto * @throws DatabaseException */ @Override public UserDTO findByPK(long pk) throws DatabaseException { log.debug("DAO findByPK Started"); UserDTO dto = null; try { dto = (UserDTO) getHibernateTemplate().get(UserDTO.class, pk); } catch (HibernateException e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in getting User by pk"); } log.debug("DAO findByPK End"); return dto; } /** * Search Users with pagination * * @return list : List of Users * @param dto * : Search Parameters * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * * @throws DatabaseException */ @Override public List search(UserDTO dto, int pageNo, int pageSize) throws DatabaseException { log.debug("DAO search Started"); List list = null; try { DetachedCriteria criteria = DetachedCriteria .forClass(UserDTO.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.getLogin() != null && dto.getLogin().length() > 0) { criteria.add(Restrictions.like("login", dto.getLogin() + "%")); } if (dto.getPassword() != null && dto.getPassword().length() > 0) { criteria.add(Restrictions.like("password", dto.getPassword() + "%")); } if (dto.getGender() != null && dto.getGender().length() > 0) { criteria.add(Restrictions.eq("gender", dto.getLastName())); } if (dto.getDob() != null && dto.getDob().getDate() > 0) { criteria.add(Restrictions.eq("dob", dto.getDob())); } if (dto.getLastLogin() != null && dto.getLastLogin().getTime() > 0) { criteria.add(Restrictions.eq("lastLogin", dto.getLastLogin())); } if (dto.getRoleId() > 0) { criteria.add(Restrictions.eq("roleId", dto.getRoleId())); } if (dto.getUnSuccessfulLogin() > 0) { criteria.add(Restrictions.eq("unSuccessfulLogin", dto.getUnSuccessfulLogin())); } if (dto.getMobileNo() != null && dto.getMobileNo().length() > 0) { criteria.add(Restrictions.eq("mobileNo", dto.getMobileNo())); } // 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 user"); } log.debug("DAO search End"); return list; } /** * Search Users * * @return list : List of Users * @param dto * : Search Parameters * @throws DatabaseException */ @Override public List search(UserDTO dto) throws DatabaseException { return search(dto, 0, 0); } /** * Get List of Users * * @return list : List of Users * @throws DatabaseException */ @Override public List list() throws DatabaseException { return list(0, 0); } /** * Get List of Users with pagination * * @return list : List of Users * @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(UserDTO.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 Users list"); } log.debug("DAO list End"); return list; } } |