<commit_before>package pl.consdata.ico.sqcompanion.members;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pl.consdata.ico.sqcompanion.config.AppConfig;
import pl.consdata.ico.sqcompanion.config.model.GroupLightModel;
import pl.consdata.ico.sqcompanion.config.model.Member;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class MemberService {
    private final AppConfig appConfig;
    private final MemberRepository memberRepository;
    private final MembershipRepository membershipRepository;

    public void syncMembers() {
        syncLocalMembers();
    }

    public void syncLocalMembers() {
        log.info("> Sync local members");
        memberRepository.saveAll(appConfig.getMembers().getLocal()
                .stream().map(definition -> MemberEntryEntity.builder()
                        .firstName(definition.getFirstName())
                        .lastName(definition.getLastName())
                        .id(definition.getUuid())
                        .aliases(definition.getAliases())
                        .mail(definition.getMail())
                        .build()
                )
                .collect(Collectors.toList()));

        appConfig.getMembers().getLocal().forEach(this::addLocalDetachedEvents);
        appConfig.getMembers().getLocal().forEach(this::addLocalAttachedEvents);
        log.info("< Done");
    }

    private void addLocalAttachedEvents(Member member) {
        log.info("> Sync attached events");

        final MemberEntryEntity memberEntryEntity = memberRepository.getOne(member.getUuid());
        member.getGroups().forEach(groupId -> processAttached(memberEntryEntity, groupId));

        log.info("< Done");
    }

    private void processAttached(MemberEntryEntity memberEntryEntity, String groupId) {
        Optional<MembershipEntryEntity> latestEvent = membershipRepository.findFirstByMemberIdAndGroupIdOrderByDateDesc(memberEntryEntity.getId(), groupId);

        if (latestEvent.isPresent() && !latestEvent.get().getDate().isBefore(LocalDate.now())) {
            latestEvent.get().setEvent(MembershipEntryEntity.Event.ATTACHED);
            membershipRepository.save(latestEvent.get());
        } else if (!latestEvent.isPresent() || isDetached(latestEvent.get())) {
            membershipRepository.save(MembershipEntryEntity.builder()
                    .date(LocalDate.now())
                    .event(MembershipEntryEntity.Event.ATTACHED)
                    .member(memberEntryEntity)
                    .groupId(groupId)
                    .build());

        }
    }

    private void addLocalDetachedEvents(Member member) {
        log.info("> Sync detached events");
        final MemberEntryEntity memberEntryEntity = memberRepository.getOne(member.getUuid());
        membershipRepository.findByMemberId(member.getUuid()).stream()
                .map(GroupsOnlyProjection::getGroupId)
                .filter(groupId -> !member.getGroups().contains(groupId))
                .forEach(groupId -> processDetached(memberEntryEntity, groupId));
        log.info("< Done");
    }

    private void processDetached(MemberEntryEntity memberEntryEntity, String groupId) {
        Optional<MembershipEntryEntity> latestEvent = membershipRepository.findFirstByMemberIdAndGroupIdOrderByDateDesc(memberEntryEntity.getId(), groupId);
        if (latestEvent.isPresent() && !latestEvent.get().getDate().isBefore(LocalDate.now())) {
            latestEvent.get().setEvent(MembershipEntryEntity.Event.DETACHED);
            membershipRepository.save(latestEvent.get());
        } else if (latestEvent.isPresent() && isAttached(latestEvent.get())) {
            membershipRepository.save(MembershipEntryEntity.builder()
                    .date(LocalDate.now())
                    .event(MembershipEntryEntity.Event.DETACHED)
                    .member(memberEntryEntity)
                    .groupId(groupId)
                    .build());

        }
    }

    public List<Member> groupMembers(String groupId) {<commit_msg>code smells<commit_after>package pl.consdata.ico.sqcompanion.members;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pl.consdata.ico.sqcompanion.config.AppConfig;
import pl.consdata.ico.sqcompanion.config.model.GroupLightModel;
import pl.consdata.ico.sqcompanion.config.model.Member;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class MemberService {
    private final AppConfig appConfig;
    private final MemberRepository memberRepository;
    private final MembershipRepository membershipRepository;

    public void syncMembers() {
        syncLocalMembers();
    }

    public void syncLocalMembers() {
        log.info("> Sync local members");
        memberRepository.saveAll(appConfig.getMembers().getLocal()
                .stream().map(definition -> MemberEntryEntity.builder()
                        .firstName(definition.getFirstName())
                        .lastName(definition.getLastName())
                        .id(definition.getUuid())
                        .aliases(definition.getAliases())
                        .mail(definition.getMail())
                        .build()
                )
                .collect(Collectors.toList()));

        appConfig.getMembers().getLocal().forEach(this::addLocalDetachedEvents);
        appConfig.getMembers().getLocal().forEach(this::addLocalAttachedEvents);
        log.info("< Synced local members");
    }

    private void addLocalAttachedEvents(Member member) {
        log.info("> Sync attached events");

        final MemberEntryEntity memberEntryEntity = memberRepository.getOne(member.getUuid());
        member.getGroups().forEach(groupId -> processAttached(memberEntryEntity, groupId));

        log.info("< Synced attached events");
    }

    private void processAttached(MemberEntryEntity memberEntryEntity, String groupId) {
        Optional<MembershipEntryEntity> latestEvent = membershipRepository.findFirstByMemberIdAndGroupIdOrderByDateDesc(memberEntryEntity.getId(), groupId);

        if (latestEvent.isPresent() && !latestEvent.get().getDate().isBefore(LocalDate.now())) {
            latestEvent.get().setEvent(MembershipEntryEntity.Event.ATTACHED);
            membershipRepository.save(latestEvent.get());
        } else if (!latestEvent.isPresent() || isDetached(latestEvent.get())) {
            membershipRepository.save(MembershipEntryEntity.builder()
                    .date(LocalDate.now())
                    .event(MembershipEntryEntity.Event.ATTACHED)
                    .member(memberEntryEntity)
                    .groupId(groupId)
                    .build());

        }
    }

    private void addLocalDetachedEvents(Member member) {
        log.info("> Sync detached events");
        final MemberEntryEntity memberEntryEntity = memberRepository.getOne(member.getUuid());
        membershipRepository.findByMemberId(member.getUuid()).stream()
                .map(GroupsOnlyProjection::getGroupId)
                .filter(groupId -> !member.getGroups().contains(groupId))
                .forEach(groupId -> processDetached(memberEntryEntity, groupId));
        log.info("< Synced detached events");
    }

    private void processDetached(MemberEntryEntity memberEntryEntity, String groupId) {
        Optional<MembershipEntryEntity> latestEvent = membershipRepository.findFirstByMemberIdAndGroupIdOrderByDateDesc(memberEntryEntity.getId(), groupId);
        if (latestEvent.isPresent() && !latestEvent.get().getDate().isBefore(LocalDate.now())) {
            latestEvent.get().setEvent(MembershipEntryEntity.Event.DETACHED);
            membershipRepository.save(latestEvent.get());
        } else if (latestEvent.isPresent() && isAttached(latestEvent.get())) {
            membershipRepository.save(MembershipEntryEntity.builder()
                    .date(LocalDate.now())
                    .event(MembershipEntryEntity.Event.DETACHED)
                    .member(memberEntryEntity)
                    .groupId(groupId)
                    .build());

        }
    }

    public List<Member> groupMembers(String groupId) {
{'fraction_non_alphanumeric': 0.10892940626460963, 'fraction_numerical': 0.0007012622720897616, 'mean_word_length': 2.882486388384755, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, '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': '3233534', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2130, 'n_words': 323}