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

live.document.mavenplugin.neo4j.ExportToNeo4jMojo Maven / Gradle / Ivy

package live.document.mavenplugin.neo4j;

import live.document.generator.launcher.RootMethodChecker;
import live.document.generator.launcher.TaskLauncher;
import live.document.scanner.asm.VisitConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import java.io.File;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Mojo(name = "export-neo4j",
        requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
        configurator = "include-project-dependencies")
public class ExportToNeo4jMojo extends AbstractMojo {
    @Parameter(defaultValue = "${project.basedir}", required = true)
    private File projectRoot;

    @Parameter
    private String[] rootMethodPatterns;

    @Parameter
    private String[] rootMethodAnnotations;

    @Parameter
    private String[] rootMethodExcludePatterns;

    @Parameter(defaultValue = "", required = true)
    private String analysisResultOutputDir;

    @Parameter
    private String[] plsqlPaths;

    @Parameter
    private String[] sqlExecuteFullMethodNames;

    @Parameter
    private String[] acceptAnnotationNames;

    @Parameter
    private String[] sqlAnnotationNames;

    @Parameter
    private String[] databaseDefineFiles;

    @Parameter
    private String[] moduleOrders;

    @Parameter(defaultValue = "true")
    private boolean countLines = true;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        showAllParameters();
        RootMethodChecker rootMethodChecker = createRootMethodChecker();

        TaskLauncher generator = new TaskLauncher(
                projectRoot.getAbsolutePath(),
                analysisResultOutputDir,
                safeList(plsqlPaths),
                VisitConfig.builder()
                        .sqlExecuteMethodNames(safeSet(sqlExecuteFullMethodNames))
                        .sqlAnnotationNames(safeSet(sqlAnnotationNames))
                        .acceptAnnotationNames(safeSet(acceptAnnotationNames))
                        .build(),
                safeList(databaseDefineFiles),
                rootMethodChecker);
        generator.setModuleOrders(safeList(moduleOrders));
        generator.setCountLines(countLines);

        generator.launch(TaskLauncher.Outputs.Neo4jData);
    }

    private void showAllParameters() {
        for (Field field : this.getClass().getDeclaredFields()) {
            try {
                Object fieldValue = field.get(this);
                String valueText = formatValue(fieldValue, field.getType());
                log.info(MessageFormat.format("{0}={1}", field.getName(), valueText));
            } catch (IllegalAccessException e) {
                log.error("Failed to read field :" + field.getName(), e);
            }
        }
    }

    private String formatValue(Object fieldValue, Class fieldType) {
        if (fieldValue == null) {
            return "null";
        }
        if (fieldType.isArray()) {
            List values = new ArrayList<>();
            for (int i = 0, len = Array.getLength(fieldValue); i < len; i++) {
                Object itemValue = Array.get(fieldValue, i);
                values.add(formatValue(itemValue, itemValue == null ? Void.class : itemValue.getClass()));
            }
            return values.toString();
        }
        if (fieldType == File.class) {
            return ((File) fieldValue).toPath().normalize().toAbsolutePath().toString();
        }

        return fieldValue.toString();
    }

    private  Set safeSet(T[] array) {
        if (array == null) {
            return Collections.emptySet();
        }

        Set set = new HashSet<>();
        for (T v : array) {
            set.add(v);
        }
        return set;
    }

    private  List safeList(T[] array) {
        if (array == null) {
            return Collections.emptyList();
        }
        return Arrays.asList(array);
    }

    private RootMethodChecker createRootMethodChecker() {
        RootMethodChecker rootMethodChecker = new RootMethodChecker();
        rootMethodChecker.setRootMethodPatterns(rootMethodPatterns);
        rootMethodChecker.setRootMethodAnnotations(rootMethodAnnotations);
        rootMethodChecker.setRootMethodExcludePatterns(rootMethodExcludePatterns);
        return rootMethodChecker;
    }
}