<commit_before>package net.hawkengine.services;

import net.hawkengine.db.DbRepositoryFactory;
import net.hawkengine.db.IDbRepository;
import net.hawkengine.db.redis.RedisRepository;
import net.hawkengine.model.ServiceResult;
import net.hawkengine.model.User;
import net.hawkengine.model.UserGroup;
import net.hawkengine.model.dto.UserGroupDto;
import net.hawkengine.services.interfaces.IUserGroupService;
import net.hawkengine.services.interfaces.IUserService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class UserGroupService extends CrudService<UserGroup> implements IUserGroupService {
    private static final Class CLASS_TYPE = UserGroup.class;

    private IUserService userService;

    public UserGroupService() {
        IDbRepository repository = DbRepositoryFactory.create(DATABASE_TYPE, CLASS_TYPE);
        super.setRepository(repository);
        this.userService = new UserService();
        super.setObjectType(CLASS_TYPE.getSimpleName());
    }

    public UserGroupService(IDbRepository repository, IUserService userService) {
        super.setRepository(repository);
        this.userService = userService;
        super.setObjectType(CLASS_TYPE.getSimpleName());
    }

    @Override
    public ServiceResult getById(String id) {
        return super.getById(id);
    }

    @Override
    public ServiceResult getAll() {
        return super.getAll();
    }

    @Override
    public ServiceResult add(UserGroup userGroup) {
        return super.add(userGroup);
    }

    @Override
    public ServiceResult update(UserGroup userGroup) {
        return super.update(userGroup);
    }

    @Override
    public ServiceResult delete(String id) {
        List<User> users = (List<User>) this.userService.getAll().getObject();

        for (User user : users) {
            List<String> userGroupIds = user.getUserGroupIds();

            for (Iterator<String> iter = userGroupIds.listIterator(); iter.hasNext(); ) {
                String currentUserGroupId = iter.next();
                if (currentUserGroupId.equals(id)) {
                    iter.remove();
                }
            }

            user.setUserGroupIds(userGroupIds);
            ServiceResult removeGroupFromAllUsers = this.userService.update(user);
            if (removeGroupFromAllUsers.hasError()) {
                return removeGroupFromAllUsers;
            }
        }

        return super.delete(id);
    }

    @Override
    public ServiceResult assignUserToGroup(User user, UserGroup userGroup) {
        userGroup = (UserGroup) this.getById(userGroup.getId()).getObject();
        userGroup.getUserIds().add(user.getId());

        String userGroupId = userGroup.getId();
        List<String> userGroupIds = user.getUserGroupIds();

        String exists = userGroupIds.stream().filter(u -> u.equals(userGroupId)).findAny().orElse(null);

        if (exists != null) {
            ServiceResult result = new ServiceResult();
            result.setError(true);
            result.setMessage("User is already in this User Group.");
            result.setObject(null);

            return result;
        }

        user.getUserGroupIds().add(userGroup.getId());
        ServiceResult updateUserServiceResult = this.userService.update(user);

        if (updateUserServiceResult.hasError()) {

            return updateUserServiceResult;
        }

        return this.update(userGroup);
    }

    @Override
    public ServiceResult unassignUserFromGroup(User user, UserGroup userGroup) {
        List<String> userIds = userGroup.getUserIds();

        for (Iterator<String> iter = userIds.listIterator(); iter.hasNext(); ) {
            String currentUserId = iter.next();
            if (currentUserId.equals(user.getId())) {
                iter.remove();
            }
        }
        List<String> userGroupIds = user.getUserGroupIds();

        for (Iterator<String> iter = userGroupIds.listIterator(); iter.hasNext(); ) {
            String currentUserGroupId = iter.next();
            if (currentUserGroupId.equals(userGroup.getId())) {
                iter.remove();
            }
        }

        user.setUserGroupIds(userGroupIds);

        ServiceResult updateUserServiceResult = this.userService.update(user);

        if (updateUserServiceResult.hasError()) {
            return updateUserServiceResult;
        }


        userGroup.setUserIds(userIds);

        return this.update(userGroup);
    }

    @Override
    public ServiceResult getAllUserGroups() {
        List<UserGroup> userGroups = (List<UserGroup>) this.getAll().getObject();
        List<UserGroupDto> userGroupDtos = new ArrayList<>();

        for (UserGroup userGroup : userGroups) {
            List<String> userIds = userGroup.getUserIds();
            UserGroupDto userGroupDto = new UserGroupDto();
            userGroupDto.setId(userGroup.getId());
            userGroupDto.setName(userGroup.getName());

            for (String userId : userIds) {
                User currentUser = (User) this.userService.getById(userId).getObject();
                userGroupDto.getUsers().add(currentUser);
            }

            userGroupDtos.add(userGroupDto);
        }
        ServiceResult userGroupDtosServiceResult = new ServiceResult();
        userGroupDtosServiceResult.setError(false);
        userGroupDtosServiceResult.setMessage("User Groups retrieved successfully.");
        userGroupDtosServiceResult.setObject(userGroupDtos);

        return userGroupDtosServiceResult;
    }

}<commit_msg>refactored assign/unassign user from groups #89<commit_after>package net.hawkengine.services;

import net.hawkengine.core.utilities.EndpointConnector;
import net.hawkengine.db.DbRepositoryFactory;
import net.hawkengine.db.IDbRepository;
import net.hawkengine.db.redis.RedisRepository;
import net.hawkengine.model.ServiceResult;
import net.hawkengine.model.User;
import net.hawkengine.model.UserGroup;
import net.hawkengine.model.dto.UserGroupDto;
import net.hawkengine.services.interfaces.IUserGroupService;
import net.hawkengine.services.interfaces.IUserService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class UserGroupService extends CrudService<UserGroup> implements IUserGroupService {
    private static final Class CLASS_TYPE = UserGroup.class;

    private IUserService userService;

    public UserGroupService() {
        IDbRepository repository = DbRepositoryFactory.create(DATABASE_TYPE, CLASS_TYPE);
        super.setRepository(repository);
        this.userService = new UserService();
        super.setObjectType(CLASS_TYPE.getSimpleName());
    }

    public UserGroupService(IDbRepository repository, IUserService userService) {
        super.setRepository(repository);
        this.userService = userService;
        super.setObjectType(CLASS_TYPE.getSimpleName());
    }

    @Override
    public ServiceResult getById(String id) {
        return super.getById(id);
    }

    @Override
    public ServiceResult getAll() {
        return super.getAll();
    }

    @Override
    public ServiceResult add(UserGroup userGroup) {
        return super.add(userGroup);
    }

    @Override
    public ServiceResult update(UserGroup userGroup) {
        return super.update(userGroup);
    }

    @Override
    public ServiceResult delete(String id) {
        List<User> users = (List<User>) this.userService.getAll().getObject();

        for (User user : users) {
            List<String> userGroupIds = user.getUserGroupIds();

            for (Iterator<String> iter = userGroupIds.listIterator(); iter.hasNext(); ) {
                String currentUserGroupId = iter.next();
                if (currentUserGroupId.equals(id)) {
                    iter.remove();
                }
            }

            user.setUserGroupIds(userGroupIds);
            ServiceResult removeGroupFromAllUsers = this.userService.update(user);
            if (removeGroupFromAllUsers.hasError()) {
                return removeGroupFromAllUsers;
            }
        }

        return super.delete(id);
    }

    @Override
    public ServiceResult assignUserToGroup(User user, UserGroup userGroup) {
        userGroup = (UserGroup) this.getById(userGroup.getId()).getObject();

        boolean userHasGroupId = user.getUserGroupIds().contains(userGroup.getId());
        boolean groupHasUserId = userGroup.getUserIds().contains(user.getId());

        ServiceResult userGroupResult = new ServiceResult();
        if (userHasGroupId && !groupHasUserId) {
            userGroup.getUserIds().add(user.getId());
            this.userService.update(user);
            this.update(userGroup);
            UserGroupDto userGroupDto = this.getUserGroupDto(userGroup);

            ServiceResult userResult = new ServiceResult();
            userResult.setError(false);
            userResult.setMessage("User assigned successfully.");
            userResult.setObject(user);
            EndpointConnector.passResultToEndpoint("UserService", "update", userResult);

            userGroupResult.setError(false);
            userGroupResult.setMessage("UserGroup updated successfully.");
            userGroupResult.setObject(userGroupDto);
        } else {
            userGroupResult.setError(true);
            userGroupResult.setMessage("User already assigned to User Group.");
            userGroupResult.setObject(null);
        }

        return userGroupResult;
    }

    @Override
    public ServiceResult unassignUserFromGroup(User user, UserGroup userGroup) {
        userGroup = (UserGroup) this.getById(userGroup.getId()).getObject();

        boolean userHasGroupId = user.getUserGroupIds().contains(userGroup.getId());
        boolean groupHasUserId = userGroup.getUserIds().contains(user.getId());

        ServiceResult userGroupResult = new ServiceResult();
        if (!userHasGroupId && groupHasUserId) {
            userGroup.getUserIds().remove(user.getId());
            this.userService.update(user);
            this.update(userGroup);
            UserGroupDto userGroupDto = this.getUserGroupDto(userGroup);

            ServiceResult userResult = new ServiceResult();
            userResult.setError(false);
            userResult.setMessage("User unassigned successfully.");
            userResult.setObject(user);
            EndpointConnector.passResultToEndpoint("UserService", "update", userResult);

            userGroupResult.setError(false);
            userGroupResult.setMessage("UserGroup updated successfully.");
            userGroupResult.setObject(userGroupDto);
        } else {
            userGroupResult.setError(true);
            userGroupResult.setMessage("User already unassigned from User Group.");
            userGroupResult.setObject(null);
        }

        return userGroupResult;
    }

    @Override
    public ServiceResult getAllUserGroups() {
        List<UserGroup> userGroups = (List<UserGroup>) this.getAll().getObject();
        List<UserGroupDto> userGroupDtos = new ArrayList<>();

        for (UserGroup userGroup : userGroups) {
            List<String> userIds = userGroup.getUserIds();
            UserGroupDto userGroupDto = new UserGroupDto();
            userGroupDto.setId(userGroup.getId());
            userGroupDto.setName(userGroup.getName());

            for (String userId : userIds) {
                User currentUser = (User) this.userService.getById(userId).getObject();
                userGroupDto.getUsers().add(currentUser);
            }

            userGroupDtos.add(userGroupDto);
        }
        ServiceResult userGroupDtosServiceResult = new ServiceResult();
        userGroupDtosServiceResult.setError(false);
        userGroupDtosServiceResult.setMessage("User Groups retrieved successfully.");
        userGroupDtosServiceResult.setObject(userGroupDtos);

        return userGroupDtosServiceResult;
    }
    
    private UserGroupDto getUserGroupDto(UserGroup userGroup) {
        UserGroupDto userGroupDto = new UserGroupDto();
        userGroupDto.setId(userGroup.getId());
        userGroupDto.setName(userGroup.getName());
        userGroupDto.setUsers(new ArrayList<>());
        List<String> userIds = userGroup.getUserIds();
        for (String userId : userIds) {
            User user = (User) this.userService.getById(userId).getObject();
            userGroupDto.getUsers().add(user);
        }

        return userGroupDto;
    }
}
{'fraction_non_alphanumeric': 0.09963782696177062, 'fraction_numerical': 0.00016096579476861168, 'mean_word_length': 2.7953573610262676, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 31, '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': '1693858', 'n_tokens_mistral': 3304, 'n_tokens_neox': 3291, 'n_words': 588}