All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.infilos.spring.track.aop.AuditOptions Maven / Gradle / Ivy

There is a newer version: 2.7.18-0
Show newest version
package com.infilos.spring.track.aop;

import com.infilos.spring.track.api.AuditAction;
import com.infilos.spring.track.api.AuditAttri;
import com.infilos.spring.track.api.AuditOption;
import com.infilos.spring.track.api.AuditTransfer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

public class AuditOptions {
    private final AuditAction action;
    private final AuditOption option; // nullable

    public AuditOptions(AuditAction action, AuditOption option) {
        this.action = action;
        this.option = option;
    }

    public Optional org() {
        return actionOrOption(AuditAction::org, AuditOption::org);
    }

    public Optional role() {
        return actionOrOption(AuditAction::role, AuditOption::role);
    }

    public Optional user() {
        return actionOrOption(AuditAction::user, AuditOption::user);
    }

    public Optional action() {
        return actionOrOption(AuditAction::action, AuditOption::action);
    }

    /**
     * Extract attri from action, or option(if option defined).
     */
    private Optional actionOrOption(
        Function actionExtractor,
        Function optionExtractor) {
        AuditAttri attriOfAction = actionExtractor.apply(action);
        if (AuditSpec.isValid(attriOfAction)) {
            return Optional.of(attriOfAction);
        } else if (option != null) {
            AuditAttri attriOfOption = optionExtractor.apply(option);
            if (AuditSpec.isValid(attriOfOption)) {
                return Optional.of(attriOfOption);
            }
        }

        return Optional.empty();
    }

    public List tags() {
        List tags = new ArrayList<>();
        Arrays.stream(action.tags())
            .forEach(
                tag -> {
                    if (AuditSpec.isValid(tag)
                        && tags.stream().noneMatch(exist -> AuditSpec.isEquals(exist, tag))) {
                        tags.add(tag);
                    }
                });
        if (option != null) {
            Arrays.stream(option.tags())
                .forEach(
                    tag -> {
                        if (AuditSpec.isValid(tag)
                            && tags.stream().noneMatch(exist -> AuditSpec.isEquals(exist, tag))) {
                            tags.add(tag);
                        }
                    });
        }

        return tags;
    }

    public boolean loggingEnabled() {
        return action.log() || (option != null && option.log());
    }

    public Optional> transfer() {
        if (action.trans() != AuditTransfer.NoopTransfer.class) {
            return Optional.of(action.trans());
        } else if (option != null && option.trans() != AuditTransfer.NoopTransfer.class) {
            return Optional.of(option.trans());
        }

        return Optional.empty();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy