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

live.document.mavenplugin.docify.CustomMindMap4CallTree Maven / Gradle / Ivy

package live.document.mavenplugin.docify;

import live.document.generator.model.CallNode;
import live.document.generator.model.MindMap4CallTree;
import live.document.mavenplugin.style.MindMapConstants;
import live.document.scanner.MethodObject;
import org.apache.maven.plugin.logging.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

public class CustomMindMap4CallTree extends MindMap4CallTree {

    protected Map colors = new HashMap<>();
    protected List transactionPatterns = new ArrayList<>();
    protected final Map patterns = new HashMap<>();
    protected Log logger;
    protected boolean displayClassAndMethodName = true;

    public void setColors(Map colors) {
        this.colors = colors;
    }

    @Override
    protected String getSuffix(CallNode node) {
        StringBuilder stringBuilder = new StringBuilder();

        if (displayClassAndMethodName && node.getMethodObject() != null) {
            if (node.getParent() != null && node.getMethodObject().getClassName().equals(node.getParent().getMethodObject().getClassName())) {
                stringBuilder.append(String.format("\\n%s", node.getMethodObject().getMethodName()));
            } else {
                stringBuilder.append(String.format("\\n%s.%s", getSimpleClassName(node.getMethodObject().getClassName()), node.getMethodObject().getMethodName()));
            }
        }

        int transactionCount = getNodeTransactionCount(node.getMethodObject(), new HashSet<>());
        if (transactionCount > 0) {
            stringBuilder.append("\\n");
        }

        if (node.isLink()) {
            stringBuilder.append(" <>");
            return stringBuilder.toString();
        }

        stringBuilder.append(getStyle(node));

        return stringBuilder.toString();
    }

    protected String getStyle(CallNode node) {
        if (node.getMethodObject() == null) {
            if (hasDynamicCall(node)) {
                return " <>";
            }
            return " <>";
        }

        String[] types = {"controller", "applicationService", "domainService", "entity", "repository", "acl",
                "resourceLayer", "applicationLayer", "domainLayer", "infrastructureLayer", "aclLayer"};

        for (String type : types) {
            if (classNameMatch(type, node.getMethodObject().getClassName())) {
                return String.format(" <<%s>>", type);
            }
        }

        return "";
    }

    protected boolean hasDynamicCall(CallNode node) {
        for (CallNode child : node.getChildren()) {
            if (child.getDbOperation() != null) {
                if (child.getDbOperation().getName().startsWith("EXECUTE_IMMEDIATE") || child.getDbOperation().getName().startsWith("LANGUAGE_JAVA")) {
                    return true;
                }
            }
        }

        return false;
    }

    @Override
    protected String getDocumentPrefix() {
        return MindMapConstants.style;
    }

    @Override
    protected String getDocumentSuffix() {
        return MindMapConstants.sampleDiagram;
    }

    protected boolean classNameMatch(String objectType, String className) {
        String[] regexPatterns = colors.get(objectType);
        if (regexPatterns == null) {
            return false;
        }

        for (String regexPattern : regexPatterns) {
            if (regexPattern == null || regexPattern.trim().length() == 0) {
                return false;
            }

            Pattern pattern = getPattern(regexPattern);
            if (pattern.matcher(className).find()) {
                return true;
            }
        }

        return false;
    }

    protected Pattern getPattern(String regex) {
        if (!patterns.containsKey(regex)) {
            Pattern pattern =  Pattern.compile(regex);
            patterns.put(regex, pattern);
        }

        return patterns.get(regex);
    }

    protected int getNodeTransactionCount(MethodObject methodObject, Set scannedMethods) {
        if (methodObject == null) {
            return 0;
        }

        int result = 0;

        if (isTransactionMethod(methodObject)) {
            result = result + 1;
        }

        if (scannedMethods.contains(methodObject)) {
            return result;
        }
        scannedMethods.add(methodObject);

        return result + methodObject.calleeStream(MethodObject.class)
                .mapToInt(callee -> getNodeTransactionCount(callee, scannedMethods))
                .sum();
    }


    protected boolean isTransactionMethod(MethodObject methodObject) {
        String fullMethodName = methodObject.getClassName() + "." + methodObject.getMethodName();
        for (Pattern transactionPattern : transactionPatterns) {
            if (transactionPattern.matcher(fullMethodName).find()) {
                return true;
            }
        }

        return false;
    }

    public void setTransactionPattern(String[] transactionRegexes) {
        if (transactionRegexes == null) {
            return;
        }

        for (String regex : transactionRegexes) {
            this.transactionPatterns.add(Pattern.compile(regex));
        }
    }

    public void setLogger(Log logger) {
        this.logger = logger;
    }

    public void setDisplayClassAndMethodName(Boolean displayClassAndMethodName) {
        this.displayClassAndMethodName = displayClassAndMethodName;
    }

    public Boolean getDisplayClassAndMethodName() {
        return displayClassAndMethodName;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy