br.com.jhonsapp.bootstrap.user.service.UserServiceImpl Maven / Gradle / Ivy
package br.com.jhonsapp.bootstrap.user.service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import br.com.jhonsapp.bootstrap.user.exception.BusinessException;
import br.com.jhonsapp.bootstrap.user.model.FinalUser;
import br.com.jhonsapp.bootstrap.user.model.User;
import br.com.jhonsapp.bootstrap.user.repository.FinalUserRepository;
import br.com.jhonsapp.bootstrap.user.service.notification.NotificationResetPassword;
import br.com.jhonsapp.notifier.abstraction.Notification;
import br.com.jhonsapp.notifier.abstraction.Notifier;
import br.com.jhonsapp.notifier.annotation.NotifierType;
import br.com.jhonsapp.notifier.annotation.NotifierType.QualifierType;
/**
* {@link UserServiceImpl} is a abstract implementation of {@link UserService}
*
* @see User
*
* @author Jhonathan Camacho
* @author Jhonys Camacho
*
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private FinalUserRepository repository;
@Autowired
@NotifierType(QualifierType.EMAIL)
private Notifier notifier;
@Override
public FinalUser save(FinalUser user) {
if(user == null)
throw new NullPointerException("The user cannot be null");
if(!isUserNameValid(user.getUserName()))
throw new BusinessException("message.invalid-username");
if(!isPasswordValid(user.getPassword()))
throw new BusinessException("message.invalid-password");
user.setPassword(encodePassword(user));
return repository.save(user);
}
@Override
public FinalUser update(long id, FinalUser user) {
FinalUser userSaved = findById(id);
if(user == null)
throw new NullPointerException("The user cannot be null");
if (!hasSameUserName(user, userSaved))
throw new BusinessException("message.invalid-username");
if(!isPasswordValid(user.getPassword()))
throw new BusinessException("message.invalid-password");
user.setPassword(encodePassword(user));
BeanUtils.copyProperties(user, userSaved, "id");
return repository.save(userSaved);
}
private String encodePassword(FinalUser user) {
return user.getPassword().length() < 50 ? getPasswordEncoder().encode(user.getPassword()) : user.getPassword();
}
private boolean hasSameUserName(FinalUser user, FinalUser user2) {
return user.equals(user2);
}
@Bean
public BCryptPasswordEncoder getPasswordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
public boolean isUserNameValid(String username) {
Optional userSearch = findUser(username);
return userSearch.isPresent() ? false : true;
}
@Override
public boolean isPasswordValid(String password) {
return password != null && password.length() >= 8;
}
@Override
public FinalUser findById(long id) {
FinalUser finalUser = repository.findOne(id);
if (finalUser != null)
return finalUser;
else
throw new EmptyResultDataAccessException(1);
}
@Override
public List findAll() {
return repository.findAll();
}
@Override
public Optional findUser(String userName) {
return repository.findByUserName(userName);
}
@Override
public String findAndroidToken(String userName) {
return repository.findByUserName(userName).get().getAndroidToken().getToken();
}
@Override
public boolean sendResetPasswordCodeToEmail(String email) {
Optional user = findUser(email);
if(!user.isPresent())
return false;
Notification notification = new NotificationResetPassword(email, user.get().getResetPasswordCode());
save(user.get());
return notifier.notify(notification);
}
@Override
public boolean resetPassword(String userName, String code, String password) {
Optional user = findUser(userName);
if(user.get().isValidResetPasswordCode(code) && isPasswordValid(password) ){
user.get().clearResetPasswordCode();
save(user.get());
return true;
}else{
return false;
}
}
}