@Override
  public UserAccount createNewUserAccount(final UserAccount userAccount) {
    UserAccount deplicate =
        this.userAccountDao.findUserAccountByUsername(userAccount.getUsername());

    if (deplicate != null) {
      throw new BusinessException("Username unavailable.");
    }

    PasswordEncoder encoder = new PasswordEncoder();
    String encodedPassword = encoder.encodePassword(userAccount.getPassword(), null);
    userAccount.setPassword(encodedPassword);
    return this.userAccountDao.persist(userAccount);
  }
  @Override
  @Transactional(readOnly = true)
  public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {

    UserAccount userAccount = null;

    try {
      userAccount = this.userAccountDao.findUserAccountByUsername(username);
    } catch (RuntimeException exception) {
      throw new UsernameNotFoundException(exception.getMessage());
    }

    if (userAccount == null) {
      throw new UsernameNotFoundException("User not found by username");
    }

    Collection<UserRole> userRoles = userAccount.getUserRoles();

    if (userRoles == null || userRoles.isEmpty()) {
      throw new UsernameNotFoundException("User roles are not defined for this account.");
    }

    Collection<UserGroup> userGroups = userAccount.getUserGroups();

    Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();

    for (UserRole userRole : userRoles) {

      GrantedAuthority grantedAuthority = new GrantedAuthorityImpl(userRole.getUserRole().name());
      grantedAuthorities.add(grantedAuthority);
    }

    if (userGroups != null && !userGroups.isEmpty()) {
      for (UserGroup userGroup : userGroups) {
        Collection<UserGroupAuthority> userGroupAuthorities =
            this.userGroupAuthorityService.findUserGroupAuthoritiesByUserGroup(userGroup);
        if (userGroupAuthorities != null) {

          for (UserGroupAuthority userGroupAuthority : userGroupAuthorities) {
            GrantedAuthority grantedAuthority =
                new GrantedAuthorityImpl(userGroupAuthority.getAuthority().name());
            if (!grantedAuthorities.contains(grantedAuthority)) {
              grantedAuthorities.add(grantedAuthority);
            }
          }
        }
      }
    }

    if (grantedAuthorities.isEmpty()) {
      throw new UsernameNotFoundException("User does not have granted authorities");
    }

    String password = userAccount.getPassword();

    boolean isActive = userAccount.isActive();

    boolean isAccountNotExpried = !userAccount.isAccountExpired();

    boolean isCredentialsNotExpired = !userAccount.isCredentialsExpired();

    boolean isAccountNotLocked = !userAccount.isAccountLocked();

    UserDetails userDetails =
        new org.springframework.security.core.userdetails.User(
            username,
            password,
            isActive,
            isAccountNotExpried,
            isCredentialsNotExpired,
            isAccountNotLocked,
            grantedAuthorities);

    return userDetails;
  }