package in.co.sunrays.proj1.service;
import in.co.sunrays.proj1.dao.UserDAOInt; import in.co.sunrays.proj1.dto.RoleDTO; import in.co.sunrays.proj1.dto.UserDTO; import in.co.sunrays.proj1.exception.ApplicationException; import in.co.sunrays.proj1.exception.DatabaseException; import in.co.sunrays.proj1.exception.DuplicateRecordException; import in.co.sunrays.proj1.exception.RecordNotFoundException; import in.co.sunrays.proj1.util.EmailBuilder; import in.co.sunrays.proj1.util.EmailMessage; import in.co.sunrays.proj1.util.EmailUtility; import java.sql.Timestamp; import java.util.Date; import java.util.HashMap; import java.util.List; import org.apache.log4j.Logger; /** * Spring Implementation of User Service * * @author SUNRAYS Technologies * @version 1.0 * @Copyright (c) SUNRAYS Technologies */ public class UserServiceSpringImpl implements UserServiceInt { /** * Logger */ private static Logger log = Logger.getLogger(UserServiceSpringImpl.class); /** * Get UserDAO Instance */ private UserDAOInt dao; /** * Inject DAO object * * @param dao */ public void setDao(UserDAOInt dao) { this.dao = dao; } /** * Add a user * * @param dto * @throws ApplicationException * @throws DuplicateRecordException * : throws when Login is already exists */ @Override public long add(UserDTO dto) throws ApplicationException, DuplicateRecordException { log.debug("Service add Started"); long pk = 0; try { UserDTO dtoExist = dao.findByLogin(dto.getLogin()); if (dtoExist != null) { throw new DuplicateRecordException("Login is already exist."); } pk = dao.add(dto); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service add End"); return pk; } /** * Register a user * * @param dto * @throws ApplicationException * @throws DuplicateRecordException * : throws when Login is already exists */ @Override public long registerUser(UserDTO dto) throws ApplicationException, DuplicateRecordException { log.debug("Service add Started"); long pk = 0; try { UserDTO dtoExist = dao.findByLogin(dto.getLogin()); if (dtoExist != null) { throw new DuplicateRecordException("Login is already exist."); } pk = dao.add(dto); HashMap<String, String> map = new HashMap<String, String>(); map.put("login", dto.getLogin()); map.put("password", dto.getPassword()); String message = EmailBuilder.getUserRegistrationMessage(map); EmailMessage msg = new EmailMessage(); msg.setTo(dto.getLogin()); msg.setSubject("Registration is successful for ORS Project SUNRAYS Technologies."); msg.setMessage(message); msg.setMessageType(EmailMessage.HTML_MSG); EmailUtility.sendMail(msg); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service add End"); return pk; } /** * Update a User * * @param dto * @throws ApplicationException * @throws DuplicateRecordException * : throws when updated Login is already exist */ @Override public void update(UserDTO dto) throws ApplicationException, DuplicateRecordException { log.debug("Service update Started"); try { UserDTO dtoExist = dao.findByLogin(dto.getLogin()); // Check if updated Loign is already exist if (dtoExist != null && dtoExist.getId() != dto.getId()) { throw new DuplicateRecordException("Login is already exist."); } dao.update(dto); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service update End"); } /** * Delete a user * * @param dto * @throws ApplicationException */ @Override public void delete(UserDTO dto) throws ApplicationException { log.debug("Service Delete Started"); try { UserDTO dtoExist = dao.findByPK(dto.getId()); if (dtoExist == null) { throw new ApplicationException("User not exist."); } dao.delete(dto); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service delete End"); } /** * Find user by Login * * @param login * : get parameter * @return dto * @throws ApplicationException */ @Override public UserDTO findByLogin(String login) throws ApplicationException { log.debug("Service findByLoginId Started"); UserDTO dto = null; try { dto = dao.findByLogin(login); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service findByLoginId End"); return dto; } /** * Find user by PK * * @param pk * : get parameter * @return dto * @throws ApplicationException */ @Override public UserDTO findByPK(long pk) throws ApplicationException { log.debug("Service findByPK Started"); UserDTO dto = null; try { dto = dao.findByPK(pk); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service 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 ApplicationException */ @Override public List search(UserDTO dto, int pageNo, int pageSize) throws ApplicationException { log.debug("Service search Started"); List list = null; try { list = dao.search(dto, pageNo, pageSize); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service search End"); return list; } /** * Search Users * * @return list : List of Users * @param dto * : Search Parameters * @throws ApplicationException */ @Override public List search(UserDTO dto) throws ApplicationException { log.debug("Service search Started"); List list = null; try { list = dao.search(dto); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service search End"); return list; } /** * Get List of Users * * @return list : List of Users * @throws ApplicationException */ public List list() throws ApplicationException { log.debug("Service list Started"); List list; try { list = dao.list(); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service list End"); return list; } /** * Get List of Users with pagination * * @return list : List of Users * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * @throws ApplicationException */ public List list(int pageNo, int pageSize) throws ApplicationException { log.debug("Service list Started"); List list; try { list = dao.list(pageNo, pageSize); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service list End"); return list; } /** * Change Password By pk * * @param pk * ,oldPassword,newPassword : get parameter * @return dto * @throws ApplicationException * @throws RecordNotFoundException */ @Override public boolean changePassword(Long id, String oldPassword, String newPassword) throws RecordNotFoundException, ApplicationException { log.debug("Service changePassword Started"); boolean flag = false; UserDTO dtoExist = null; try { dtoExist = dao.findByPK(id); if (dtoExist != null && dtoExist.getPassword().equals(oldPassword)) { dtoExist.setPassword(newPassword); dao.update(dtoExist); flag = true; HashMap<String, String> map = new HashMap<String, String>(); map.put("firstName", dtoExist.getFirstName()); map.put("lastName", dtoExist.getLastName()); map.put("login", dtoExist.getLogin()); map.put("password", dtoExist.getPassword()); String message = EmailBuilder.getForgetPasswordMessage(map); EmailMessage msg = new EmailMessage(); msg.setTo(dtoExist.getLogin()); msg.setSubject("SUNARYS ORS Password has been changed Successfully."); msg.setMessage(message); msg.setMessageType(EmailMessage.HTML_MSG); EmailUtility.sendMail(msg); } else { throw new RecordNotFoundException("Old Password not match."); } } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service changePassword End"); return flag; } /** * User Authentication * * @return dto : Contains User's information * @param dto * @throws ApplicationException */ @Override public UserDTO authenticate(UserDTO dto) throws ApplicationException { try { UserDTO dtoExist = dao.findByLogin(dto.getLogin()); if (dtoExist != null && dtoExist.getPassword().equals(dto.getPassword())) { dtoExist.setLastLogin(new Timestamp(new Date().getTime())); dtoExist.setLastLoginIP(dto.getLastLoginIP()); dao.update(dtoExist); return dtoExist; } else { throw new ApplicationException("Invalid Username or Password"); } } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } } /** * Lock User for certain time duration * * @return boolean : true if success otherwise false * @param login * : User Login * @throws ApplicationException * @throws RecordNotFoundException * : throws when user not found */ @Override public boolean lock(String login) throws RecordNotFoundException, ApplicationException { log.debug("Service lock Started"); boolean flag = false; UserDTO dtoExist = null; try { dtoExist = findByLogin(login); if (dtoExist != null) { dtoExist.setLock(UserDTO.ACTIVE); dao.update(dtoExist); flag = true; } else { throw new RecordNotFoundException("LoginId not exist"); } } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service lock End"); return flag; } /** * Reset Password of User with auto generated Password * * @return boolean : true if success otherwise false * @param login * : User Login * @throws ApplicationException * @throws RecordNotFoundException * : throws when user not found */ @Override public boolean resetPassword(String login) throws RecordNotFoundException, ApplicationException { log.debug("Service restPassword Started"); boolean flag = false; UserDTO dtoExist = null; try { dtoExist = dao.findByLogin(login); if (dtoExist != null) { String newPassword = String.valueOf(new Date().getTime()) .substring(0, 4); dtoExist.setPassword(newPassword); dao.update(dtoExist); HashMap<String, String> map = new HashMap<String, String>(); map.put("login", dtoExist.getLogin()); map.put("password", dtoExist.getPassword()); map.put("firstName", dtoExist.getFirstName()); map.put("lastName", dtoExist.getLastName()); String message = EmailBuilder.getForgetPasswordMessage(map); EmailMessage msg = new EmailMessage(); msg.setTo(dtoExist.getLogin()); msg.setSubject("Password has been reset."); msg.setMessage(message); msg.setMessageType(EmailMessage.HTML_MSG); EmailUtility.sendMail(msg); flag = true; } else { throw new RecordNotFoundException("LoginId not exist"); } } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service restPassword End"); return flag; } /** * Send the password of User to his Email * * @return boolean : true if success otherwise false * @param login * : User Login * @throws ApplicationException * @throws RecordNotFoundException * : throws when user not found */ public boolean forgetPassword(String login) throws RecordNotFoundException, ApplicationException { log.debug("Service forgetPassword Started"); boolean flag = false; UserDTO dtoExist = null; try { dtoExist = dao.findByLogin(login); if (dtoExist != null) { HashMap<String, String> map = new HashMap<String, String>(); map.put("firstName", dtoExist.getFirstName()); map.put("lastName", dtoExist.getLastName()); map.put("login", dtoExist.getLogin()); map.put("password", dtoExist.getPassword()); String message = EmailBuilder.getForgetPasswordMessage(map); EmailMessage msg = new EmailMessage(); msg.setTo(dtoExist.getLogin()); msg.setSubject("SUNARYS ORS Password reset."); msg.setMessage(message); msg.setMessageType(EmailMessage.HTML_MSG); EmailUtility.sendMail(msg); flag = true; } else { throw new RecordNotFoundException("LoginId not exist"); } } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service forgetPassword End"); return flag; } /** * Get User Roles * * @return RoleDTO : User Role * @param dto * @throws ApplicationException */ @Override public RoleDTO getRole(UserDTO dto) throws ApplicationException { return null; } /** * Update User access * * @return dto * @param dto * @throws ApplicationException */ @Override public UserDTO updateAccess(UserDTO dto) throws ApplicationException { return null; } } |