<commit_before>package org.hswebframework.web.workflow.dimension.parser;

import lombok.extern.slf4j.Slf4j;
import org.hswebframework.web.organizational.authorization.PersonnelAuthentication;
import org.hswebframework.web.organizational.authorization.PersonnelAuthenticationManager;
import org.hswebframework.web.organizational.authorization.relation.Relations;
import org.hswebframework.web.workflow.dimension.DimensionContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;

/**
 * @author zhouhao
 * @since 3.0.0-RC
 */
@Slf4j
public class RelationCandidateDimensionParserStrategy implements CandidateDimensionParserStrategy {

    @Autowired
    PersonnelAuthenticationManager authenticationManager;


    @Override
    public boolean support(String dimension) {
        return DIMENSION_RELATION.equals(dimension);
    }

    @Override
    public List<String> parse(DimensionContext context, StrategyConfig config) {
        String userType = config.getStringConfig("userType")
                .orElse("creator");

        String user;
        switch (userType) {
            case "fixed":
                user = config.getStringConfig("userId")
                        .orElse(context.getCreatorId());
                break;
            case "pre":
                user = context.getPreTask().getOwner();
                break;
            default:
                user = context.getCreatorId();
        }

        PersonnelAuthentication authentication = authenticationManager
                .getPersonnelAuthorizationByUserId(user);

        if (null == authentication) {
            return Collections.emptyList();
        }

        Relations relations = authentication.getRelations();

        if (relations == null || relations.getAll().isEmpty()) {
            return Collections.emptyList();
        }


        return Collections.emptyList();
    }

    public static class RelationInfo {

    }
}<commit_msg>完善关系维度解析<commit_after>package org.hswebframework.web.workflow.dimension.parser;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.expands.script.engine.DynamicScriptEngine;
import org.hswebframework.expands.script.engine.DynamicScriptEngineFactory;
import org.hswebframework.web.Maps;
import org.hswebframework.web.commons.entity.GenericEntity;
import org.hswebframework.web.organizational.authorization.relation.PersonRelations;
import org.hswebframework.web.organizational.authorization.relation.Relation;
import org.hswebframework.web.organizational.authorization.relation.RelationsManager;
import org.hswebframework.web.workflow.dimension.DimensionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author zhouhao
 * @since 3.0.0-RC
 */
@Slf4j
@ConditionalOnBean(RelationsManager.class)
public class RelationCandidateDimensionParserStrategy implements CandidateDimensionParserStrategy {

    @Autowired
    RelationsManager relationsManager;

    @Override
    public boolean support(String dimension) {
        return DIMENSION_RELATION.equals(dimension);
    }

    @Override
    @SneakyThrows
    @SuppressWarnings("all")
    public List<String> parse(DimensionContext context, StrategyConfig config) {
        String expression = config.getStringConfig("expression").orElse(null);
        String expressionLanguage = config.getStringConfig("expressionLanguage").orElse(null);

        if (StringUtils.isEmpty(expression)) {
            return Collections.emptyList();
        }

        String preId = context.getPreTask().getOwner();
        String creatorId = context.getCreatorId();

        Supplier<PersonRelations> pre = () -> relationsManager.getPersonRelationsByUserId(preId);

        Supplier<PersonRelations> creator = () -> relationsManager.getPersonRelationsByUserId(creatorId);

        DynamicScriptEngine engine = DynamicScriptEngineFactory.getEngine(expressionLanguage);

        if (engine == null) {
            throw new UnsupportedOperationException("不支持的脚本:" + expressionLanguage);
        }

        String id = DigestUtils.md5DigestAsHex(expression.getBytes());
        if (!engine.compiled(id)) {
            engine.compile(id, expression);
        }

        Object obj = engine.execute(id, Maps.<String, Object>buildMap()
                .put("pre", pre)
                .put("creator", creator)
                .put("preId", preId)
                .put("creatorId", creatorId)
                .put("context", context)
                .get())
                .getIfSuccess();

        Function<Object, String> userIdConverter = o -> {
            if (o instanceof String) {
                return (String) o;
            } else if (o instanceof Relation) {
                Object target = ((Relation) o).getTargetObject();
                if (target instanceof GenericEntity) {
                    return String.valueOf(((GenericEntity) target).getId());
                } else {
                    return ((Relation) o).getRelation();
                }
            } else {
                log.warn("不支持的关系:{}", o);
                return null;
            }
        };

        if (obj instanceof List) {
            List<Object> list = ((List) obj);
            return list.stream()
                    .map(userIdConverter)
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
        } else {
            String result = userIdConverter.apply(obj);
            if (result == null) {
                return Collections.emptyList();
            }
            return Collections.singletonList(result);
        }
    }

}
{'fraction_non_alphanumeric': 0.09968433294567204, 'fraction_numerical': 0.0021598272138228943, 'mean_word_length': 2.709180529882933, 'pattern_counts': {'":': 2, '<': 10, '<?xml version=': 0, '>': 13, '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': '6278919', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1545, 'n_words': 279}