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

import <%=packageName%>.domain.User;
import <%=packageName%>.repository.UserRepository;
import <%=packageName%>.service.MailService;
import <%=packageName%>.service.UserService;
import <%=packageName%>.service.dto.UserDTO;

import com.google.protobuf.Empty;
import com.google.protobuf.StringValue;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import org.lognet.springboot.grpc.GRpcService;
import org.slf4j.LoggerFactory;

import java.util.Optional;

@GRpcService(interceptors = {AuthenticationInterceptor.class})
public class UserGrpcService extends UserServiceGrpc.UserServiceImplBase {

    private final org.slf4j.Logger log = LoggerFactory.getLogger(UserGrpcService.class);

    private final UserRepository userRepository;

    private final MailService mailService;

    private final UserService userService;

    private final UserProtoMapper userProtoMapper;

    public UserGrpcService(UserRepository userRepository, MailService mailService,
                           UserService userService, UserProtoMapper userProtoMapper) {

        this.userRepository = userRepository;
        this.mailService = mailService;
        this.userService = userService;
        this.userProtoMapper = userProtoMapper;
    }

    @Override
    public void createUser(UserProto userProto, StreamObserver<UserProto> responseObserver) {
        log.debug("gRPC request to save User : {}", userProto);

        if (userProto.getIdOneofCase() == UserProto.IdOneofCase.ID) {
            responseObserver.onError(Status.INVALID_ARGUMENT.withDescription("A new user cannot already have an ID").asException());
            // Lowercase the user login before comparing with database
        } else if (userRepository.findOneByLogin(userProto.getLogin().toLowerCase()).isPresent()) {
            responseObserver.onError(Status.ALREADY_EXISTS.withDescription("Login already in use").asException());
        } else if (userRepository.findOneByEmailIgnoreCase(userProto.getEmail()).isPresent()) {
            responseObserver.onError(Status.ALREADY_EXISTS.withDescription("Email already in use").asException());
        } else {
            User newUser = userService.createUser(userProtoMapper.userProtoToUserDTO(userProto));
            mailService.sendCreationEmail(newUser);
            responseObserver.onNext(userProtoMapper.userToUserProto(newUser));
            responseObserver.onCompleted();
        }
    }

    @Override
    public void updateUser(UserProto userProto, StreamObserver<UserProto> responseObserver) {
        log.debug("gRPC request to update User : {}", userProto);
        Optional<User> existingUser = userRepository.findOneByEmailIgnoreCase(userProto.getEmail());
        if (existingUser.isPresent() && (!existingUser.get().getId().equals(userProto.getId()))) {
            responseObserver.onError(Status.ALREADY_EXISTS.withDescription("Email already in use").asException());
            return;
        }
        existingUser = userRepository.findOneByLogin(userProto.getLogin().toLowerCase());
        if (existingUser.isPresent() && (!existingUser.get().getId().equals(userProto.getId()))) {
            responseObserver.onError(Status.ALREADY_EXISTS.withDescription("Login already in use").asException());
            return;
        }
        Optional<UserDTO> updatedUser = userService.updateUser(userProtoMapper.userProtoToUserDTO(userProto));
        if (updatedUser.isPresent()) {
            responseObserver.onNext(userProtoMapper.userDTOToUserProto(updatedUser.get()));
            responseObserver.onCompleted();
        } else {
            responseObserver.onError(Status.NOT_FOUND.asException());
        }
    }

    @Override
    public void getAllUsers(<% if (databaseType == 'sql' || databaseType == 'mongodb') { %>PageRequest<% } else { %>Empty<% } %> request, StreamObserver<UserProto> responseObserver) {
        log.debug("gRPC request to get all users");
        userService.getAllManagedUsers<% if (databaseType == 'sql' || databaseType == 'mongodb') { %>(ProtobufMappers.pageRequestProtoToPageRequest(request))<% } else { %>()<% } %>
            .forEach(userDTO -> responseObserver.onNext(userProtoMapper.userDTOToUserProto(userDTO)));
        responseObserver.onCompleted();
    }

    @Override
    public void getUser(StringValue login, StreamObserver<UserProto> responseObserver) {
        log.debug("gRPC request to get User : {}", login.getValue());
        Optional<User> user = userService.getUserWithAuthoritiesByLogin(login.getValue());
        if(user.isPresent()) {
            responseObserver.onNext(userProtoMapper.userToUserProto(user.get()));
            responseObserver.onCompleted();
        } else {
            responseObserver.onError(Status.NOT_FOUND.asException());
        }
    }

    @Override
    public void deleteUser(StringValue login, StreamObserver<Empty> responseObserver) {
        log.debug("gRPC request to delete User: {}", login.getValue());
        userService.deleteUser(login.getValue());
        responseObserver.onNext(Empty.newBuilder().build());
        responseObserver.onCompleted();
    }

}
<commit_msg>Add rxjava support to user services<commit_after>package <%=packageName%>.grpc;

import <%=packageName%>.domain.User;
import <%=packageName%>.repository.UserRepository;
import <%=packageName%>.service.MailService;
import <%=packageName%>.service.UserService;
import <%=packageName%>.service.dto.UserDTO;

import com.google.protobuf.Empty;
import com.google.protobuf.StringValue;
import io.grpc.Status;
import io.reactivex.Flowable;
import io.reactivex.Single;
import org.lognet.springboot.grpc.GRpcService;
import org.slf4j.LoggerFactory;

import java.util.Optional;

@GRpcService(interceptors = {AuthenticationInterceptor.class})
public class UserGrpcService extends RxUserServiceGrpc.UserServiceImplBase {

    private final org.slf4j.Logger log = LoggerFactory.getLogger(UserGrpcService.class);

    private final UserRepository userRepository;

    private final MailService mailService;

    private final UserService userService;

    private final UserProtoMapper userProtoMapper;

    public UserGrpcService(UserRepository userRepository, MailService mailService,
                           UserService userService, UserProtoMapper userProtoMapper) {

        this.userRepository = userRepository;
        this.mailService = mailService;
        this.userService = userService;
        this.userProtoMapper = userProtoMapper;
    }

    @Override
    public Single<UserProto> createUser(Single<UserProto> request) {
        return request
            .doOnSuccess(userProto -> log.debug("gRPC request to save User : {}", userProto))
            .filter(userProto -> userProto.getIdOneofCase() != UserProto.IdOneofCase.ID)
            .switchIfEmpty(Single.error(Status.INVALID_ARGUMENT.withDescription("A new user cannot already have an ID").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(userProtoMapper::userProtoToUserDTO)
            .map(userService::createUser)
            .doOnSuccess(mailService::sendCreationEmail)
            .map(userProtoMapper::userToUserProto);
    }

    @Override
    public Single<UserProto> updateUser(Single<UserProto> request) {
        return request
            .doOnSuccess(userProto -> log.debug("gRPC request to update User : {}", userProto))
            .filter(userProto -> !userRepository
                .findOneByEmailIgnoreCase(userProto.getEmail())
                .map(User::getId)
                .filter(id -> !id.equals(userProto.getId()))
                .isPresent()
            )
            .switchIfEmpty(Single.error(Status.ALREADY_EXISTS.withDescription("Email already in use").asException()))
            .filter(userProto -> !userRepository
                .findOneByLogin(userProto.getLogin().toLowerCase())
                .map(User::getId)
                .filter(id -> !id.equals(userProto.getId()))
                .isPresent()
            )
            .switchIfEmpty(Single.error(Status.ALREADY_EXISTS.withDescription("Login already in use").asException()))
            .map(userProtoMapper::userProtoToUserDTO)
            .map(user -> userService.updateUser(user).orElseThrow(Status.NOT_FOUND::asException))
            .map(userProtoMapper::userDTOToUserProto);
    }

    @Override
    public Flowable<UserProto> getAllUsers(Single<<% if (databaseType == 'sql' || databaseType == 'mongodb') { %>PageRequest<% } else { %>Empty<% } %>> request) {
        log.debug("gRPC request to get all users");
        return request
            <%_ if (databaseType == 'sql' || databaseType == 'mongodb') { _%>
            .map(ProtobufMappers::pageRequestProtoToPageRequest)
            .map(userService::getAllManagedUsers)
            <%_ } else { _%>
            .map(e-> userService.getAllManagedUsers())
            <%_ } _%>
            .flatMapPublisher(Flowable::fromIterable)
            .map(userProtoMapper::userDTOToUserProto);
    }

    @Override
    public void getUser(StringValue login, StreamObserver<UserProto> responseObserver) {
        log.debug("gRPC request to get User : {}", login.getValue());
        Optional<User> user = userService.getUserWithAuthoritiesByLogin(login.getValue());
        if(user.isPresent()) {
            responseObserver.onNext(userProtoMapper.userToUserProto(user.get()));
            responseObserver.onCompleted();
        } else {
            responseObserver.onError(Status.NOT_FOUND.asException());
        }
    }

    @Override
    public void deleteUser(StringValue login, StreamObserver<Empty> responseObserver) {
        log.debug("gRPC request to delete User: {}", login.getValue());
        userService.deleteUser(login.getValue());
        responseObserver.onNext(Empty.newBuilder().build());
        responseObserver.onCompleted();
    }

}

{'fraction_non_alphanumeric': 0.11665693714730492, 'fraction_numerical': 0.0003891807744697412, 'mean_word_length': 3.480819529206626, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 56, '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': '2971240', 'n_tokens_mistral': 2761, 'n_tokens_neox': 2717, 'n_words': 467}