<commit_before>package net.explorviz.security.services;

import com.mongodb.MongoException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.inject.Inject;
import net.explorviz.security.util.CountingIdGenerator;
import net.explorviz.security.util.IdGenerator;
import net.explorviz.shared.security.model.User;
import net.explorviz.shared.security.model.roles.Role;
import org.jvnet.hk2.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.morphia.Datastore;

/**
 * Offers CRUD operations on user objects, backed by a MongoDB instance as persistence layer. Each
 * user has the following fields:
 * <ul>
 * <li>id: the unique id of the user</li>
 * <li>username: name of the user, unique</li>
 * <li>password: <PASSWORD></li>
 * <li>roles: list of role that are assigned to the user</li>
 * </ul>
 *
 */
@Service
public class UserMongoCrudService implements MongoCrudService<User> {

  private static final Logger LOGGER = LoggerFactory.getLogger(UserMongoCrudService.class);

  private final IdGenerator<Long> idGen;

  private final Datastore datastore;



  /**
   * Creates a new {@code UserCrudMongoDb}.
   *
   */
  @Inject
  public UserMongoCrudService(final Datastore datastore) {

    this.datastore = datastore;

    final User userWithMaxId = this.datastore.createQuery(User.class).order("id").get();

    // Create a new id generator, which will count upwards beginning from the max id
    long counterInitValue = 0L;

    if (userWithMaxId != null) {
      counterInitValue = userWithMaxId.getId();
    }

    this.idGen = new CountingIdGenerator(counterInitValue);
  }


  @Override
  public List<User> getAll() {
    return this.datastore.createQuery(User.class).asList();
  }

  @Override
  public Optional<User> saveNewEntity(final User user) throws MongoException {
    // Generate an id
    user.setId(this.idGen.next());

    this.datastore.save(user);

    if (LOGGER.isInfoEnabled()) {
      LOGGER.info("Inserted new user with id " + user.getId());
    }
    return Optional.ofNullable(
        new User(user.getId(), user.getUsername(), user.getPassword(), user.getRoles()));
  }

  @Override
  public void updateEntity(final User user) throws MongoException {
    this.datastore.save(user);
  }

  @Override
  public Optional<User> getEntityById(final Long id) throws MongoException {

    final User userObject = this.datastore.get(User.class, id);

    return Optional.ofNullable(userObject);
  }

  @Override
  public void deleteEntityById(final Long id) throws MongoException {<commit_msg>Fix settings attribute nor being return when creating a user<commit_after>package net.explorviz.security.services;

import com.mongodb.MongoException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.inject.Inject;
import net.explorviz.security.util.CountingIdGenerator;
import net.explorviz.security.util.IdGenerator;
import net.explorviz.shared.security.model.User;
import net.explorviz.shared.security.model.roles.Role;
import org.jvnet.hk2.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.morphia.Datastore;

/**
 * Offers CRUD operations on user objects, backed by a MongoDB instance as persistence layer. Each
 * user has the following fields:
 * <ul>
 * <li>id: the unique id of the user</li>
 * <li>username: name of the user, unique</li>
 * <li>password: <PASSWORD></li>
 * <li>roles: list of role that are assigned to the user</li>
 * </ul>
 *
 */
@Service
public class UserMongoCrudService implements MongoCrudService<User> {

  private static final Logger LOGGER = LoggerFactory.getLogger(UserMongoCrudService.class);

  private final IdGenerator<Long> idGen;

  private final Datastore datastore;



  /**
   * Creates a new {@code UserCrudMongoDb}.
   *
   */
  @Inject
  public UserMongoCrudService(final Datastore datastore) {

    this.datastore = datastore;

    final User userWithMaxId = this.datastore.createQuery(User.class).order("id").get();

    // Create a new id generator, which will count upwards beginning from the max id
    long counterInitValue = 0L;

    if (userWithMaxId != null) {
      counterInitValue = userWithMaxId.getId();
    }

    this.idGen = new CountingIdGenerator(counterInitValue);
  }


  @Override
  public List<User> getAll() {
    return this.datastore.createQuery(User.class).asList();
  }

  @Override
  public Optional<User> saveNewEntity(final User user) throws MongoException {
    // Generate an id
    user.setId(this.idGen.next());



    this.datastore.save(user);

    if (LOGGER.isInfoEnabled()) {
      LOGGER.info("Inserted new user with id " + user.getId());
    }
    return Optional.ofNullable(user);
  }

  @Override
  public void updateEntity(final User user) throws MongoException {
    this.datastore.save(user);
  }

  @Override
  public Optional<User> getEntityById(final Long id) throws MongoException {

    final User userObject = this.datastore.get(User.class, id);
    System.out.println(userObject.getSettings());

    return Optional.ofNullable(userObject);
  }

  @Override
  public void deleteEntityById(final Long id) throws MongoException {
{'fraction_non_alphanumeric': 0.10820541974606784, 'fraction_numerical': 0.0015160128861095318, 'mean_word_length': 4.149268292682927, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3320397', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1643, 'n_words': 413}