<commit_before>package <%=packageName%>.grpc;

import <%=packageName%>.domain.User;
<%_ if (authenticationType === 'session') { _%>
import <%=packageName%>.repository.PersistentTokenRepository;
<%_ } _%>
import <%=packageName%>.repository.UserRepository;
import <%=packageName%>.security.SecurityUtils;
import <%=packageName%>.service.MailService;
import <%=packageName%>.service.UserService;
import <%=packageName%>.service.dto.UserDTO;
import <%=packageName%>.web.rest.vm.ManagedUserVM;

import com.google.protobuf.Empty;
import com.google.protobuf.StringValue;
import io.grpc.Status;
<%_ if (authenticationType === 'session') { _%>
import io.reactivex.Flowable;
<%_ } _%>
import io.reactivex.Single;
import org.apache.commons.lang3.StringUtils;
import org.lognet.springboot.grpc.GRpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
<%_ if (databaseType === 'sql') { _%>
import org.springframework.transaction.TransactionSystemException;
<%_ } _%>

import javax.validation.ConstraintViolationException;
<%_ if (authenticationType === 'session') { _%>
import java.util.ArrayList;
<%_ } _%>
import java.util.Optional;

@GRpcService
public class AccountService extends RxAccountServiceGrpc.AccountServiceImplBase {

    private final Logger log = LoggerFactory.getLogger(AccountService.class);

    private final UserRepository userRepository;

    private final UserService userService;

    private final MailService mailService;

    <%_ if (authenticationType === 'session') { _%>
    private final PersistentTokenRepository persistentTokenRepository;

    <%_ } _%>
    private final UserProtoMapper userProtoMapper;

    public AccountService(UserRepository userRepository, UserService userService, MailService mailService, <% if (authenticationType === 'session') { %>PersistentTokenRepository persistentTokenRepository, <% } %>UserProtoMapper userProtoMapper) {
        this.userRepository = userRepository;
        this.userService = userService;
        this.mailService = mailService;
        <%_ if (authenticationType === 'session') { _%>
        this.persistentTokenRepository = persistentTokenRepository;
        <%_ } _%>
        this.userProtoMapper = userProtoMapper;
    }

    @Override
    public Single<Empty> registerAccount(Single<UserProto> request) {
        return request
            .doOnSuccess(userProto -> log.debug("gRPC request to register account {}", userProto.getLogin()))
            .filter(userProto -> checkPasswordLength(userProto.getPassword()))
            .switchIfEmpty(Single.error(Status.INVALID_ARGUMENT.withDescription("Incorrect password").asException()))
            .filter(userProto -> !userRepository.findOneByLogin(userProto.getLogin().toLowerCase()).isPresent())
            .switchIfEmpty(Single.error(Status.ALREADY_EXISTS.withDescription("Login already in use").asException()))
            .filter(userProto -> !userRepository.findOneByEmailIgnoreCase(userProto.getEmail()).isPresent())
            .switchIfEmpty(Single.error(Status.ALREADY_EXISTS.withDescription("Email already in use").asException()))
            .map(userProto -> new ManagedUserVM(
                null,
                userProto.getLogin().isEmpty() ? null : userProto.getLogin(),
                userProto.getPassword().isEmpty() ? null : userProto.getPassword(),
                userProto.getFirstName().isEmpty() ? null : userProto.getFirstName(),
                userProto.getLastName().isEmpty() ? null : userProto.getLastName(),
                userProto.getEmail().isEmpty() ? null : userProto.getEmail().toLowerCase(),
                true,
                <%_ if (databaseType === 'mongodb' || databaseType === 'sql') { _%>
                userProto.getImageUrl().isEmpty() ? null : userProto.getImageUrl(),
                <%_ } _%>
                userProto.getLangKey().isEmpty() ? null : userProto.getLangKey(),
                <% if (databaseType === 'mongodb' || databaseType === 'sql') { %>null, null, null, null, <% } %>null
            ))
            .map(user -> {
                try {
                    return userService.registerUser(user);
                <%_ if (databaseType === 'sql') { _%>
                } catch (TransactionSystemException e) {
                    if (e.getOriginalException().getCause() instanceof ConstraintViolationException) {
                        log.info("Invalid user", e);
                        throw Status.INVALID_ARGUMENT.withDescription("Invalid user").asException();
                    } else {
                        throw e;
                    }
                <%_ } _%>
                } catch (ConstraintViolationException e) {<commit_msg>Fix to be compatible with JH4.9<commit_after>package <%=packageName%>.grpc;

import <%=packageName%>.domain.User;
<%_ if (authenticationType === 'session') { _%>
import <%=packageName%>.repository.PersistentTokenRepository;
<%_ } _%>
import <%=packageName%>.repository.UserRepository;
import <%=packageName%>.security.SecurityUtils;
import <%=packageName%>.service.MailService;
import <%=packageName%>.service.UserService;
import <%=packageName%>.service.dto.UserDTO;
import <%=packageName%>.web.rest.vm.ManagedUserVM;

import com.google.protobuf.Empty;
import com.google.protobuf.StringValue;
import io.grpc.Status;
<%_ if (authenticationType === 'session') { _%>
import io.reactivex.Flowable;
<%_ } _%>
import io.reactivex.Single;
import org.apache.commons.lang3.StringUtils;
import org.lognet.springboot.grpc.GRpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
<%_ if (databaseType === 'sql') { _%>
import org.springframework.transaction.TransactionSystemException;
<%_ } _%>

import javax.validation.ConstraintViolationException;
<%_ if (authenticationType === 'session') { _%>
import java.util.ArrayList;
<%_ } _%>
import java.util.Optional;

@GRpcService
public class AccountService extends RxAccountServiceGrpc.AccountServiceImplBase {

    private final Logger log = LoggerFactory.getLogger(AccountService.class);

    private final UserRepository userRepository;

    private final UserService userService;

    private final MailService mailService;

    <%_ if (authenticationType === 'session') { _%>
    private final PersistentTokenRepository persistentTokenRepository;

    <%_ } _%>
    private final UserProtoMapper userProtoMapper;

    public AccountService(UserRepository userRepository, UserService userService, MailService mailService, <% if (authenticationType === 'session') { %>PersistentTokenRepository persistentTokenRepository, <% } %>UserProtoMapper userProtoMapper) {
        this.userRepository = userRepository;
        this.userService = userService;
        this.mailService = mailService;
        <%_ if (authenticationType === 'session') { _%>
        this.persistentTokenRepository = persistentTokenRepository;
        <%_ } _%>
        this.userProtoMapper = userProtoMapper;
    }

    @Override
    public Single<Empty> registerAccount(Single<UserProto> request) {
        return request
            .doOnSuccess(userProto -> log.debug("gRPC request to register account {}", userProto.getLogin()))
            .filter(userProto -> checkPasswordLength(userProto.getPassword()))
            .switchIfEmpty(Single.error(Status.INVALID_ARGUMENT.withDescription("Incorrect password").asException()))
            .filter(userProto -> !userRepository.findOneByLogin(userProto.getLogin().toLowerCase()).isPresent())
            .switchIfEmpty(Single.error(Status.ALREADY_EXISTS.withDescription("Login already in use").asException()))
            .filter(userProto -> !userRepository.findOneByEmailIgnoreCase(userProto.getEmail()).isPresent())
            .switchIfEmpty(Single.error(Status.ALREADY_EXISTS.withDescription("Email already in use").asException()))
            .map(userProto -> {
                try {
                    return userService.createUser(
                        userProto.getLogin().isEmpty() ? null : userProto.getLogin(),
                        userProto.getPassword().isEmpty() ? null : userProto.getPassword(),
                        userProto.getFirstName().isEmpty() ? null : userProto.getFirstName(),
                        userProto.getLastName().isEmpty() ? null : userProto.getLastName(),
                        userProto.getEmail().isEmpty() ? null : userProto.getEmail().toLowerCase(),
                        <%_ if (databaseType === 'mongodb' || databaseType === 'sql') { _%>
                        userProto.getImageUrl().isEmpty() ? null : userProto.getImageUrl(),
                        <%_ } _%>
                        userProto.getLangKey().isEmpty() ? null : userProto.getLangKey()
                    );
                } catch (TransactionSystemException e) {
                    if (e.getOriginalException().getCause() instanceof ConstraintViolationException) {
                        log.info("Invalid user", e);
                        throw Status.INVALID_ARGUMENT.withDescription("Invalid user").asException();
                    } else {
                        throw e;
                    }
                } catch (ConstraintViolationException e) {
{'fraction_non_alphanumeric': 0.12529298955891754, 'fraction_numerical': 0.0008523332623055614, 'mean_word_length': 3.2169811320754715, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 72, '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': '2131156', 'n_tokens_mistral': 2517, 'n_tokens_neox': 2396, 'n_words': 423}