Search in sources :

Example 1 with ExternalIdType

use of oap.dictionary.ExternalIdType in project oap by oaplatform.

the class DictionaryMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    final List<Path> paths = Files.fastWildcard(Paths.get(sourceDirectory), "*.json").stream().filter(p -> {
        final boolean b = !Arrays.stream(exclude).filter(e -> FilenameUtils.wildcardMatchOnSystem(separatorsToUnix(p.toString()), e)).findAny().isPresent();
        if (!b)
            getLog().debug("exclude " + p);
        return b;
    }).collect(toList());
    getLog().debug("found " + paths);
    paths.forEach(path -> {
        getLog().info("dictionary " + path + "...");
        final DictionaryRoot dictionary = DictionaryParser.parse(path);
        final StringBuilder out = new StringBuilder();
        final String enumClass = toEnumName(dictionary.name);
        out.append("package " + dictionaryPackage + ";\n\n").append("import oap.dictionary.Dictionary;\n\n").append("import java.util.Map;\n").append("import java.util.Optional;\n").append("import java.util.List;\n\n").append("import static java.util.Collections.emptyList;\n").append("import static java.util.Collections.emptyMap;\n").append("import static java.util.Arrays.asList;\n\n").append("public enum " + enumClass + " implements Dictionary {\n");
        final Set<String> properties = dictionary.getValues().stream().flatMap(v -> v.getProperties().keySet().stream()).collect(toSet());
        final Map<String, Boolean> optional = properties.stream().collect(toMap(k -> k, k -> dictionary.getValues().stream().anyMatch(v -> !v.containsProperty(k))));
        final Map<String, Class<?>> types = properties.stream().collect(toMap(k -> k, k -> dictionary.getValues().stream().filter(v -> v.containsProperty(k)).findAny().get().getProperty(k).get().getClass()));
        out.append(dictionary.getValues().stream().map(d -> "  " + d.getId() + "(" + convert(d.getExternalId(), dictionary.externalIdAs) + ", " + d.isEnabled() + properties(d.getProperties(), properties, optional, types) + ")").collect(joining(",\n")));
        final String externalIdType = dictionary.externalIdAs.javaType.getSimpleName();
        out.append(";\n\n").append("  private final " + externalIdType + " externalId;\n").append("  private final boolean enabled;\n\n");
        for (String property : properties) {
            out.append("  private final " + propertyType(property, optional, types) + " " + property + ";\n");
        }
        out.append("\n");
        for (String property : properties) {
            out.append("  public final " + propertyType(property, optional, types) + " " + property + "(){return " + property + ";}\n");
        }
        out.append("\n  " + enumClass + "( " + externalIdType + " externalId, boolean enabled");
        final String cParameters = properties.stream().map(p -> propertyType(p, optional, types) + " " + p).collect(joining(", "));
        out.append(cParameters.length() > 0 ? ", " : "").append(cParameters + " ) {\n").append("    this.externalId = externalId;\n").append("    this.enabled = enabled;\n");
        for (String property : properties) {
            out.append("    this." + property + " = " + property + ";\n");
        }
        out.append("  }\n" + "\n" + "  public static " + enumClass + " valueOf( int externalId ) {\n" + "    switch( externalId ) {\n");
        dictionary.getValues().forEach(d -> {
            out.append("      case ").append(d.getExternalId()).append(": return ").append(d.getId()).append(";\n");
        });
        out.append("      default: ");
        if (dictionary.containsValueWithId("UNKNOWN")) {
            out.append("return UNKNOWN");
        } else {
            out.append("throw new java.lang.IllegalArgumentException( \"Unknown id \" + externalId )");
        }
        out.append(";\n" + "    }\n" + "  }\n" + "\n" + "  @Override\n" + "  public int getOrDefault( String id, int defaultValue ) {\n" + "    return defaultValue;\n" + "  }\n" + "\n" + "  @Override\n" + "  public Integer get( String id ) {\n" + "    return null;\n" + "  }\n" + "\n" + "  @Override\n" + "  public String getOrDefault( int externlId, String defaultValue ) {\n" + "    return defaultValue;\n" + "  }\n" + "\n" + "  @Override\n" + "  public boolean containsValueWithId( String id ) {\n" + "    return false;\n" + "  }\n" + "\n" + "  @Override\n" + "  public List<String> ids() {\n" + "    return emptyList();\n" + "  }\n" + "\n" + "  @Override\n" + "  public int[] externalIds() {\n" + "    return new int[0];\n" + "  }\n" + "\n" + "  @Override\n" + "  public Map<String, Object> getProperties() {\n" + "    return emptyMap();\n" + "  }\n" + "\n" + "  @Override\n" + "  public Optional<? extends Dictionary> getValueOpt( String name ) {\n" + "    return Optional.empty();\n" + "  }\n" + "\n" + "  @Override\n" + "  public Dictionary getValue( String name ) {\n" + "    return null;\n" + "  }\n" + "\n" + "  @Override\n" + "  public Dictionary getValue( int externalId ) {\n" + "    return null;\n" + "  }\n" + "\n" + "  @Override\n" + "  public List<? extends Dictionary> getValues() {\n" + "    return emptyList();\n" + "  }\n" + "\n" + "  @Override\n" + "  public String getId() {\n" + "    return name();\n" + "  }\n" + "\n" + "  @Override\n" + "  public Optional<Object> getProperty( String name ) {\n" + "    return Optional.empty();\n" + "  }\n" + "\n" + "  @Override\n" + "  public boolean isEnabled() {\n" + "    return enabled;\n" + "  }\n" + "\n" + "  @Override\n" + "  public int getExternalId() {\n" + "    return externalId;\n" + "  }\n" + "  public " + externalIdType + " externalId() {\n" + "    return externalId;\n" + "  }\n" + "\n" + "  @Override\n" + "  public boolean containsProperty( String name ) {\n" + "    return false;\n" + "  }\n").append("}\n");
        final Path outPath = Paths.get(outputDirectory, dictionaryPackage.replace(".", "/"), enumClass + ".java");
        if (!java.nio.file.Files.exists(outPath) || !Files.readString(outPath).equals(out.toString())) {
            Files.writeString(outPath, IoStreams.Encoding.PLAIN, out.toString());
        } else {
            getLog().debug(outPath + " is not modified.");
        }
    });
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) Files(oap.io.Files) ExternalIdType(oap.dictionary.ExternalIdType) Parameter(org.apache.maven.plugins.annotations.Parameter) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ClassUtils(org.apache.commons.lang3.ClassUtils) Mojo(org.apache.maven.plugins.annotations.Mojo) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) Stream(oap.util.Stream) Path(java.nio.file.Path) StringUtils.split(org.apache.commons.lang3.StringUtils.split) Collectors.toSet(java.util.stream.Collectors.toSet) DictionaryParser(oap.dictionary.DictionaryParser) Set(java.util.Set) DictionaryRoot(oap.dictionary.DictionaryRoot) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Collectors.joining(java.util.stream.Collectors.joining) MojoFailureException(org.apache.maven.plugin.MojoFailureException) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) IoStreams(oap.io.IoStreams) Paths(java.nio.file.Paths) FilenameUtils.separatorsToUnix(org.apache.commons.io.FilenameUtils.separatorsToUnix) FilenameUtils(org.apache.commons.io.FilenameUtils) AbstractMojo(org.apache.maven.plugin.AbstractMojo) DictionaryRoot(oap.dictionary.DictionaryRoot)

Aggregations

Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors.joining (java.util.stream.Collectors.joining)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 DictionaryParser (oap.dictionary.DictionaryParser)1 DictionaryRoot (oap.dictionary.DictionaryRoot)1 ExternalIdType (oap.dictionary.ExternalIdType)1 Files (oap.io.Files)1 IoStreams (oap.io.IoStreams)1 Stream (oap.util.Stream)1 FilenameUtils (org.apache.commons.io.FilenameUtils)1 FilenameUtils.separatorsToUnix (org.apache.commons.io.FilenameUtils.separatorsToUnix)1 ClassUtils (org.apache.commons.lang3.ClassUtils)1