@Override
 public UserDTO findOne(Long id) {
   User user = userRepository.findOne(id);
   UserDTO userDto = new UserDTO();
   userDto.toDTO(user);
   return userDto;
 }
 @Override
 public UserDTO findUserByEmailAndAlta(String email) {
   User u = findByEmailAndUserStateEnum(email, UserStateEnum.ALTA);
   UserDTO userDto = new UserDTO();
   userDto.toDTO(u);
   return userDto;
 }
 /**
  * Metodo que se va a utilizar para crear los usuarios
  *
  * @param userDto TODO: Queda por hacer el tema de los updates
  * @return
  */
 @Override
 public UserDTO saveAndFlush(UserDTO userDto) {
   Role iniciado = roleRepository.findOne(1L);
   User u = userDto.toEntity(null, iniciado);
   u.setPassword(passwordEncoder.encode(userDto.getPassword()));
   u = userRepository.saveAndFlush(u);
   userDto.toDTO(u);
   return userDto;
 }
 public Page<UserDTO> findAll(Integer page, Integer size) {
   PageRequest pageable = new PageRequest(page, size);
   Page<User> users = userRepository.findAll(pageable);
   List<UserDTO> userDTOs = new ArrayList<>();
   for (User user : users.getContent()) {
     UserDTO userDto = new UserDTO();
     userDto.toDTO(user);
     userDTOs.add(userDto);
   }
   Page<UserDTO> pages = new PageImpl<>(userDTOs, pageable, users.getTotalElements());
   return pages;
 }
 @Override
 public String checkLoginAndCalculateBasicAuth(UserDTO userDTO) {
   UserDTO user = findUserByEmailAndAlta(userDTO.getUsername());
   if (user != null) {
     if (passwordEncoder.matches(userDTO.getPassword(), user.getPassword())) {
       return PasswordUtils.calculateBasicAuth(user.getEmail(), userDTO.getPassword());
     }
   }
   return null;
 }
  /**
   * TODO: Hay que mirar una forma de rellenar los beans mas eficiente, ya que a veces no vendra el
   * usuario completo, si no solo una parte.
   *
   * @param user
   * @return
   */
  @Override
  public UserDTO updateAndFlush(UserDTO user, boolean updatePassword, boolean updateRole) {
    User u = userRepository.findOne(user.getUserId());
    u = user.updateEntity(u);
    if (updatePassword) {
      if (user.getPassword() != null)
        if (!passwordEncoder.matches(user.getPassword(), u.getPassword())) {
          u.setPassword(passwordEncoder.encode(user.getPassword()));
        }
    }

    if (updateRole) {
      Role rol = roleRepository.findOne(user.getRole().getRoleId());
      if (!rol.equals(u.getRole())) {
        u.setRole(rol);
      }
    }

    u = userRepository.saveAndFlush(u);
    user.toDTO(u);
    return user;
  }