PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/spring/demo/services/UserService.java

https://bitbucket.org/denisabarar/spring_demo_local
Java | 120 lines | 101 code | 19 blank | 0 comment | 15 complexity | 2509aefec4b17c54624c531312f209ad MD5 | raw file
  1. package spring.demo.services;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import spring.demo.errorhandler.EntityValidationException;
  10. import spring.demo.errorhandler.ResourceNotFoundException;
  11. import spring.demo.dto.UserDTO;
  12. import spring.demo.entities.User;
  13. import spring.demo.repositories.UserRepository;
  14. @Service
  15. public class UserService {
  16. private static final String SPLIT_CH = " ";
  17. public static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$",
  18. Pattern.CASE_INSENSITIVE);
  19. @Autowired
  20. private UserRepository usrRepository;
  21. public UserDTO findUserById(int userId) {
  22. User usr = usrRepository.findById(userId);
  23. if (usr == null) {
  24. throw new ResourceNotFoundException(User.class.getSimpleName());
  25. }
  26. String[] names = extractNames(usr.getName());
  27. UserDTO dto = new UserDTO.Builder()
  28. .id(usr.getId())
  29. .firstname(names[0])
  30. .surname(names[1])
  31. .city(usr.getCity())
  32. .address(usr.getAddress())
  33. .email(usr.getEmail())
  34. .telephone(usr.getTelephone())
  35. .country(usr.getCountry())
  36. .postcode(usr.getPostcode())
  37. .create();
  38. return dto;
  39. }
  40. public List<UserDTO> findAll() {
  41. List<User> users = usrRepository.findAll();
  42. List<UserDTO> toReturn = new ArrayList<UserDTO>();
  43. for (User user : users) {
  44. String[] names = extractNames(user.getName());
  45. UserDTO dto = new UserDTO.Builder()
  46. .id(user.getId())
  47. .firstname(names[0])
  48. .surname(names[1])
  49. .telephone(user.getTelephone())
  50. .create();
  51. toReturn.add(dto);
  52. }
  53. return toReturn;
  54. }
  55. public int create(UserDTO userDTO) {
  56. List<String> validationErrors = validateUser(userDTO);
  57. if (!validationErrors.isEmpty()) {
  58. throw new EntityValidationException(User.class.getSimpleName(),validationErrors);
  59. }
  60. User user = new User();
  61. user.setName(userDTO.getFirstname().trim() + SPLIT_CH + userDTO.getSurname().trim());
  62. user.setEmail(userDTO.getEmail());
  63. user.setAddress(userDTO.getAddress());
  64. user.setPostcode(userDTO.getPostcode());
  65. user.setCity(userDTO.getCity());
  66. user.setCountry(userDTO.getCountry());
  67. user.setTelephone(userDTO.getTelephone());
  68. user.setIBAN(userDTO.getIBAN());
  69. user.setCreated(new Date());
  70. User usr = usrRepository.save(user);
  71. return usr.getId();
  72. }
  73. private List<String> validateUser(UserDTO usr) {
  74. List<String> validationErrors = new ArrayList<String>();
  75. if (usr.getFirstname() == null || "".equals(usr.getFirstname())) {
  76. validationErrors.add("First Name field should not be empty");
  77. }
  78. if (usr.getSurname() == null || "".equals(usr.getSurname())) {
  79. validationErrors.add("Surname field should not be empty");
  80. }
  81. if (usr.getEmail() == null || !validateEmail(usr.getEmail())) {
  82. validationErrors.add("Email does not have the correct format.");
  83. }
  84. return validationErrors;
  85. }
  86. public static boolean validateEmail(String email) {
  87. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(email);
  88. return matcher.find();
  89. }
  90. private String[] extractNames(String fullname){
  91. String[] names = new String[2];
  92. int surnameIndex = fullname.lastIndexOf(SPLIT_CH);
  93. names[0] = fullname;
  94. names[1] = "";
  95. if (surnameIndex != -1) {
  96. names[0] = fullname.substring(0, surnameIndex).trim();
  97. names[1] = fullname.substring(surnameIndex).trim();
  98. }
  99. return names;
  100. }
  101. }