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

live.document.mavenplugin.methods.CsvExport Maven / Gradle / Ivy

package live.document.mavenplugin.methods;

import live.document.generator.model.CallNode;
import live.document.generator.model.CallTree;
import live.document.plsqlscanner.PlSqlExplained;
import live.document.plsqlscanner.PlSqlObject;
import live.document.scanner.CallGraph;
import live.document.scanner.ClassObject;
import live.document.scanner.DbObjectTypeEnum;
import live.document.scanner.MethodObject;

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.function.Function;
import java.util.regex.Pattern;

public class CsvExport {
    protected final Map patterns = new HashMap<>();

    private PlSqlExplained plSqlExplained;
    private List methodPatterns = new ArrayList<>();

    public void setMethodPatterns(List methodPatterns) {
        this.methodPatterns = methodPatterns;
    }

    public void setPlSqlExplained(PlSqlExplained plSqlExplained) {
        this.plSqlExplained = plSqlExplained;
    }

    public String generate(CallGraph callGraph, PlSqlExplained plSqlExplained, Function qualifiedMethodFunc, Function visibleFunc) {
        List methods = findMethods(callGraph);
        StringBuilder result = new StringBuilder("METHOD_NAME,OBJECT,LINE_OF_CODE,CRUD_STATEMENTS,TABLE,ACTION\n");

        for (MethodObject method : methods) {
            CallTree callTree = new CallTree(callGraph, plSqlExplained, method, qualifiedMethodFunc);
            if (visibleFunc != null) {
                callTree.updateNodesVisible(visibleFunc);
            }

            String methodCsv = exportCallTree(callTree);
            result.append(methodCsv);
        }

        return result.toString();
    }

    private String exportCallTree(CallTree callTree) {
        StringBuilder result = new StringBuilder();

        for (CallNode node : callTree.getNodes()) {
            String nodeText = node.getText();
            if (nodeText.length() == 0 && node.getMethodObject() != null) {
                nodeText = node.getMethodObject().getFullName();
            }
            Set procedures = new HashSet<>();
            Set javaMethodRelTables = new HashSet<>();

            exportNode(node, procedures, javaMethodRelTables);
            for (String javaMethodRelTable : javaMethodRelTables) {
                int i = javaMethodRelTable.indexOf(",");
                String tableName = javaMethodRelTable.substring(0, i);
                String type = javaMethodRelTable.substring(i + 1);
                if (! "RW".equals(type) && javaMethodRelTables.contains(tableName + ",RW")) {
                    continue;
                }
                if (! "W".equals(type) && ! "RW".equals(type)  && javaMethodRelTables.contains(tableName + ",W")) {
                    continue;
                }

                result.append(String.format("%s,%s,0,0,%s,%s\n", nodeText, "ALL", tableName, type));
            }

            for (String procedure : procedures) {
                PlSqlObject plSqlObject = plSqlExplained.findByName(procedure);
                if (plSqlObject != null) {
                    for (String table : plSqlObject.getQueryTables()) {
                        result.append(String.format("%s,%s,%d,%d,%s,%s\n",
                                nodeText, procedure, plSqlObject.getLineOfCode(), plSqlObject.getCrudStatements(), table, "Query"));
                    }
                    for (String table : plSqlObject.getInsertTables()) {
                        result.append(String.format("%s,%s,%d,%d,%s,%s\n",
                                nodeText, procedure, plSqlObject.getLineOfCode(), plSqlObject.getCrudStatements(), table, "Insert"));
                    }
                    for (String table : plSqlObject.getUpdateTables()) {
                        result.append(String.format("%s,%s,%d,%d,%s,%s\n",
                                nodeText, procedure, plSqlObject.getLineOfCode(), plSqlObject.getCrudStatements(), table, "Update"));
                    }
                    for (String table : plSqlObject.getDeleteTables()) {
                        result.append(String.format("%s,%s,%d,%d,%s,%s\n",
                                nodeText, procedure, plSqlObject.getLineOfCode(), plSqlObject.getCrudStatements(), table, "Delete"));
                    }
                }
            }
        }

        return result.toString();
    }

    private void exportNode(CallNode node, Set procedures, Set javaMethodRelTables) {
        if (node.getDbOperation() != null) {
            if (node.getDbOperation().getObjectType() == DbObjectTypeEnum.PROCEDURE_FUNCTION) {
                procedures.add(node.getDbOperation().getName());
            }

            if (node.getDbOperation().getObjectType() == DbObjectTypeEnum.TABLE) {
                javaMethodRelTables.add(node.getDbOperation().getName() + "," + node.getDbOperation().getOperationType());
            }
        }

        for (CallNode child : node.getChildren()) {
            exportNode(child, procedures, javaMethodRelTables);
        }

        if (node.isLink()) {
            return;
        }
    }

    private List findMethods(CallGraph callGraph) {
        List results = new ArrayList<>();

        for (ClassObject aClass : callGraph.getClasses()) {
            for (MethodObject method : aClass.getMethods()) {
                if (isMethodMatch(method)) {
                    results.add(method);
                }
            }
        }

        return results;
    }

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

        return patterns.get(regex);
    }

    private boolean isMethodMatch(MethodObject method) {
        for (String regexPattern : methodPatterns) {
            if (regexPattern == null || regexPattern.trim().length() == 0) {
                return false;
            }

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

        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy