<commit_before>package com.google.rolecall.services;

import com.google.rolecall.jsonobjects.UserInfo;
import com.google.rolecall.models.User;
import com.google.rolecall.repos.UserRepository;
import com.google.rolecall.restcontrollers.exceptionhandling.RequestExceptions.EntityNotFoundException;
import com.google.rolecall.restcontrollers.exceptionhandling.RequestExceptions.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import org.springframework.stereotype.Service;

/* Utility classes for accessing Users while mantaining database consistencies. */
@Service
public class UserServices {
  
  private final UserRepository userRepo;

  public List<User> getAllUsers() {
    List<User> allUsers = new ArrayList<>();
    userRepo.findAll().forEach(allUsers::add);

    return allUsers;
  }

  public User getUser(int id) throws EntityNotFoundException{
    Optional<User> queryResult = userRepo.findById(id);

    if (!queryResult.isPresent()) {
        throw new EntityNotFoundException(String.format("userid %d does not exist", id));
    }

    return queryResult.get();
  }

  /* Creates a new User and adds it to the database with isActive set to true.
   * Requires firstName, lastName, and email fields and validates the email.
   * Emails are always stored as lowercase and cannot already exist in the system.
   * Throws exception on missing properties or invalid email.
   */
  public User createUser(UserInfo newUser) throws InvalidParameterException {
    User user = User.newBuilder()
        .setFirstName(newUser.firstName())
        .setLastName(newUser.lastName())
        .setEmail(newUser.email())
        .setDateJoined(newUser.dateJoined())
        .setEmergencyContactName(newUser.emergencyContactName())
        .setEmergencyContactNumber(newUser.emergencyContactNumber())
        .setComments(newUser.comments())
        .setIsActive(newUser.isActive())
        .setCanLogin(newUser.canLogin())
        .setAdmin(newUser.admin())
        .setRecievesNotifications(newUser.notifications())
        .setManagePerformances(newUser.managePerformances())
        .setManageCasts(newUser.manageCasts())
        .setManagePieces(newUser.managePieces())
        .setManageRoles(newUser.manageRoles())
        .setManageRules(newUser.manageRules())
        .build();

    if(!validateEmail(user.getEmail())) {
      throw new InvalidParameterException("User requires valid email address");
    } else if(userRepo.findByEmailIgnoreCase(user.getEmail()).isPresent()) {
      throw new InvalidParameterException("User requires unique email address");
    }

    return userRepo.save(user);
  }

  /* Edits an existing User according to supplied properties. Does not change a User's email.*/
  public User editUser(UserInfo newUser) throws EntityNotFoundException {
    User.Builder builder = this.getUser(newUser.id()).toBuilder()
        .setFirstName(newUser.firstName())
        .setLastName(newUser.lastName())
        .setDateJoined(newUser.dateJoined())
        .setEmergencyContactName(newUser.emergencyContactName())
        .setEmergencyContactNumber(newUser.emergencyContactNumber())
        .setComments(newUser.comments())
        .setIsActive(newUser.isActive())
        .setCanLogin(newUser.canLogin())
        .setAdmin(newUser.admin())
        .setRecievesNotifications(newUser.notifications())
        .setManagePerformances(newUser.managePerformances())
        .setManageCasts(newUser.manageCasts())
        .setManagePieces(newUser.managePieces())
        .setManageRoles(newUser.manageRoles())
        .setManageRules(newUser.manageRules());
    
    User user;
    try {
      user = builder.build();
    } catch(InvalidParameterException e) { 
      // Unreachable unless an invalid object exists in the database
      throw new Error("Tried to edit User with invalid properties");
    }
    
    return userRepo.save(user);
  }

  public void deleteUser(int id) throws EntityNotFoundException {
    getUser(id);
    userRepo.deleteById(id);
  }

  /* Determines if an email has a valid email format. */
  private boolean validateEmail(String email) {
    // Found at https://regexlib.com/REDetails.aspx?regexp_id=26
    String pattern = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)"
        + "|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    return Pattern.matches(pattern, email);
  }

  public UserServices(UserRepository userRepo) {
    this.userRepo = userRepo;
  }
}<commit_msg>More comments<commit_after>package com.google.rolecall.services;

import com.google.rolecall.jsonobjects.UserInfo;
import com.google.rolecall.models.User;
import com.google.rolecall.repos.UserRepository;
import com.google.rolecall.restcontrollers.exceptionhandling.RequestExceptions.EntityNotFoundException;
import com.google.rolecall.restcontrollers.exceptionhandling.RequestExceptions.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import org.springframework.stereotype.Service;

/* Utility classes for accessing Users while mantaining database consistencies. */
@Service
public class UserServices {
  
  private final UserRepository userRepo;

  public List<User> getAllUsers() {
    List<User> allUsers = new ArrayList<>();
    userRepo.findAll().forEach(allUsers::add);

    return allUsers;
  }

  public User getUser(int id) throws EntityNotFoundException{
    Optional<User> queryResult = userRepo.findById(id);

    if (!queryResult.isPresent()) {
        throw new EntityNotFoundException(String.format("userid %d does not exist", id));
    }

    return queryResult.get();
  }

  /** 
   * Creates a new {@link User} and adds it to the database with isActive set to true.
   * 
   * @param newUser {@link UserInfo} containing information describing the new user.
   * @return The new {@link User} created and stored.
   * @throws InvalidParameterException When firstName, lastName, or email are null in
   * {@link UserInfo} newUser and when the email is malformatted or already exists.
   */
  public User createUser(UserInfo newUser) throws InvalidParameterException {
    User user = User.newBuilder()
        .setFirstName(newUser.firstName())
        .setLastName(newUser.lastName())
        .setEmail(newUser.email())
        .setDateJoined(newUser.dateJoined())
        .setEmergencyContactName(newUser.emergencyContactName())
        .setEmergencyContactNumber(newUser.emergencyContactNumber())
        .setComments(newUser.comments())
        .setIsActive(newUser.isActive())
        .setCanLogin(newUser.canLogin())
        .setAdmin(newUser.admin())
        .setRecievesNotifications(newUser.notifications())
        .setManagePerformances(newUser.managePerformances())
        .setManageCasts(newUser.manageCasts())
        .setManagePieces(newUser.managePieces())
        .setManageRoles(newUser.manageRoles())
        .setManageRules(newUser.manageRules())
        .build();

    if(!validateEmail(user.getEmail())) {
      throw new InvalidParameterException("User requires valid email address");
    } else if(userRepo.findByEmailIgnoreCase(user.getEmail()).isPresent()) {
      throw new InvalidParameterException("User requires unique email address");
    }

    return userRepo.save(user);
  }

  /** 
   * Edits an existing {@link User} and updates it in the database.
   * 
   * @param newUser {@link UserInfo} containing information describing the user edits.
   * @return The updated {@link User}.
   * @throws EntityNotFoundException The id from {@link UserInfo} newUser does not exist
   * in the database.
   */
    public User editUser(UserInfo newUser) throws EntityNotFoundException {
    User.Builder builder = this.getUser(newUser.id()).toBuilder()
        .setFirstName(newUser.firstName())
        .setLastName(newUser.lastName())
        .setDateJoined(newUser.dateJoined())
        .setEmergencyContactName(newUser.emergencyContactName())
        .setEmergencyContactNumber(newUser.emergencyContactNumber())
        .setComments(newUser.comments())
        .setIsActive(newUser.isActive())
        .setCanLogin(newUser.canLogin())
        .setAdmin(newUser.admin())
        .setRecievesNotifications(newUser.notifications())
        .setManagePerformances(newUser.managePerformances())
        .setManageCasts(newUser.manageCasts())
        .setManagePieces(newUser.managePieces())
        .setManageRoles(newUser.manageRoles())
        .setManageRules(newUser.manageRules());
    
    User user;
    try {
      user = builder.build();
    } catch(InvalidParameterException e) { 
      // Unreachable unless an invalid object exists in the database
      throw new Error("Tried to edit User with invalid properties");
    }
    
    return userRepo.save(user);
  }

  /** 
   * Deletes an existing {@link User} object by id.
   * 
   * @param id Unique id for the {@link User} object to be deleted
   * @throws EntityNotFoundException The id from does not exist in the database.
   */
  public void deleteUser(int id) throws EntityNotFoundException {
    getUser(id);
    userRepo.deleteById(id);
  }

  /* Determines if an email has a valid email format. */
  private boolean validateEmail(String email) {
    // Found at https://regexlib.com/REDetails.aspx?regexp_id=26
    String pattern = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)"
        + "|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    return Pattern.matches(pattern, email);
  }

  public UserServices(UserRepository userRepo) {
    this.userRepo = userRepo;
  }
}
{'fraction_non_alphanumeric': 0.12087001553599172, 'fraction_numerical': 0.004971517348524081, 'mean_word_length': 3.651252408477842, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '1219378', 'n_tokens_mistral': 2902, 'n_tokens_neox': 2819, 'n_words': 593}