<commit_before>package ua.com.fielden.platform.entity;

import static java.util.Optional.of;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.fetchKeyAndDescOnly;
import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.from;
import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.select;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import com.google.inject.Inject;
import com.google.inject.name.Named;

import ua.com.fielden.platform.dao.CommonEntityDao;
import ua.com.fielden.platform.dao.ISecurityRoleAssociation;
import ua.com.fielden.platform.dao.annotations.SessionRequired;
import ua.com.fielden.platform.entity.annotation.EntityType;
import ua.com.fielden.platform.entity.query.IFilter;
import ua.com.fielden.platform.entity.query.model.EntityResultQueryModel;
import ua.com.fielden.platform.security.provider.ISecurityTokenNodeTransformation;
import ua.com.fielden.platform.security.provider.SecurityTokenNode;
import ua.com.fielden.platform.security.provider.SecurityTokenProvider;
import ua.com.fielden.platform.security.user.SecurityRoleAssociation;
import ua.com.fielden.platform.security.user.UserRole;

@EntityType(SecurityMatrixInsertionPoint.class)
public class SecurityMatrixInsertionPointDao extends CommonEntityDao<SecurityMatrixInsertionPoint> implements ISecurityMatrixInsertionPoint {

    private final SecurityTokenProvider tokenProvider;
    private final ISecurityTokenNodeTransformation tokenTransformation;

    @Inject
    public SecurityMatrixInsertionPointDao(final IFilter filter,
            @Named("tokens.path") final String tokenPath,
            @Named("tokens.package") final String tokenPackage,
            final ISecurityTokenNodeTransformation tokenTransformation) {
        super(filter);
        this.tokenProvider = new SecurityTokenProvider(tokenPath, tokenPackage);
        this.tokenTransformation = tokenTransformation;
    }

    @Override
    @SessionRequired
    public SecurityMatrixInsertionPoint save(final SecurityMatrixInsertionPoint entity) {
        final List<SecurityTokenTreeNodeEntity> tokenEntities = tokenTransformation.transform(tokenProvider.getTopLevelSecurityTokenNodes()).stream().map(token -> createTokenNodeEntity(Optional.empty(), token)).collect(toList());
        final EntityResultQueryModel<UserRole> userRoleQueryModel = select(UserRole.class).model();
        try (final Stream<UserRole> stream = co(UserRole.class).stream(from(userRoleQueryModel).with(fetchKeyAndDescOnly(UserRole.class)).model())) {
            entity.setUserRoles(stream.collect(toList()));
        }
        entity.setTokens(tokenEntities);
        final ISecurityRoleAssociation coTokenRoleAssociation = co(SecurityRoleAssociation.class);
        final Map<String, List<Long>> tokenRoleMap = coTokenRoleAssociation.findAllAssociations().entrySet().stream().collect(toMap(entry -> entry.getKey().getName(), entry -> entry.getValue().stream().map(UserRole::getId).collect(toList())));
        entity.setTokenRoleMap(tokenRoleMap);
        entity.setCalculated(true);
        return super.save(entity);
    }

    private SecurityTokenTreeNodeEntity createTokenNodeEntity(final Optional<SecurityTokenTreeNodeEntity> parentNode, final SecurityTokenNode tokenNode) {
        final SecurityTokenTreeNodeEntity tokenTreeNode = new SecurityTokenTreeNodeEntity();
        tokenTreeNode.setParent(parentNode.orElse(null))<commit_msg>#1030 enhnaced reloading logic so that it clears filtering editors.<commit_after>package ua.com.fielden.platform.entity;

import static java.util.Optional.of;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.fetchKeyAndDescOnly;
import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.from;
import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.select;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import com.google.inject.Inject;
import com.google.inject.name.Named;

import ua.com.fielden.platform.dao.CommonEntityDao;
import ua.com.fielden.platform.dao.ISecurityRoleAssociation;
import ua.com.fielden.platform.dao.annotations.SessionRequired;
import ua.com.fielden.platform.entity.annotation.EntityType;
import ua.com.fielden.platform.entity.query.IFilter;
import ua.com.fielden.platform.entity.query.model.EntityResultQueryModel;
import ua.com.fielden.platform.security.provider.ISecurityTokenNodeTransformation;
import ua.com.fielden.platform.security.provider.SecurityTokenNode;
import ua.com.fielden.platform.security.provider.SecurityTokenProvider;
import ua.com.fielden.platform.security.user.SecurityRoleAssociation;
import ua.com.fielden.platform.security.user.UserRole;

@EntityType(SecurityMatrixInsertionPoint.class)
public class SecurityMatrixInsertionPointDao extends CommonEntityDao<SecurityMatrixInsertionPoint> implements ISecurityMatrixInsertionPoint {

    private final SecurityTokenProvider tokenProvider;
    private final ISecurityTokenNodeTransformation tokenTransformation;

    @Inject
    public SecurityMatrixInsertionPointDao(final IFilter filter,
            @Named("tokens.path") final String tokenPath,
            @Named("tokens.package") final String tokenPackage,
            final ISecurityTokenNodeTransformation tokenTransformation) {
        super(filter);
        this.tokenProvider = new SecurityTokenProvider(tokenPath, tokenPackage);
        this.tokenTransformation = tokenTransformation;
    }

    @Override
    @SessionRequired
    public SecurityMatrixInsertionPoint save(final SecurityMatrixInsertionPoint entity) {
        final List<SecurityTokenTreeNodeEntity> tokenEntities = tokenTransformation.transform(tokenProvider.getTopLevelSecurityTokenNodes()).stream().map(token -> createTokenNodeEntity(Optional.empty(), token)).collect(toList());
        final EntityResultQueryModel<UserRole> userRoleQueryModel = select(UserRole.class).model();
        try (final Stream<UserRole> stream = co(UserRole.class).stream(from(userRoleQueryModel).with(fetchKeyAndDescOnly(UserRole.class)).model())) {
            entity.setUserRoles(stream.collect(toList()));
        }
        entity.setTokens(tokenEntities);
        final ISecurityRoleAssociation coTokenRoleAssociation = co(SecurityRoleAssociation.class);
        final Map<String, List<Long>> tokenRoleMap = coTokenRoleAssociation.findAllAssociations().entrySet().stream().collect(toMap(entry -> entry.getKey().getName(), entry -> entry.getValue().stream().map(UserRole::getId).collect(toList())));
        entity.setTokenRoleMap(tokenRoleMap)
              .setCalculated(true)
              .setRoleFilter("")
              .setTokenFilter("");
        return super.save(entity);
    }

    private SecurityTokenTreeNodeEntity createTokenNodeEntity(final Optional<SecurityTokenTreeNodeEntity> parentNode, final SecurityTokenNode tokenNode) {
        final SecurityTokenTreeNodeEntity tokenTreeNode = new SecurityTokenTreeNodeEntity();
        tokenTreeNode.setParent(parentNode.orElse(null))
{'fraction_non_alphanumeric': 0.09725186766275347, 'fraction_numerical': 0.0005336179295624333, 'mean_word_length': 5.490909090909091, 'pattern_counts': {'":': 0, '<': 17, '<?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': '22624', 'n_tokens_mistral': 2030, 'n_tokens_neox': 2032, 'n_words': 314}