From f687d9f4ae7500556d78fff1b5dd461412440e01 Mon Sep 17 00:00:00 2001 From: "Benjamin, Max" Date: Thu, 10 Sep 2020 11:15:57 -0400 Subject: added fragment support to generated objects added fragment support to generated objects updated objects to support singular and plural fragments Issue-ID: SO-3240 Signed-off-by: Benjamin, Max (mb388a) Change-Id: Ia010c99ad36422db5f5b3729af3991767fe5138f --- .../graphinventory/generate/FluentGenerator.java | 51 ++++++++++++++++++++-- .../generate/FluentGeneratorMojo.java | 6 ++- 2 files changed, 53 insertions(+), 4 deletions(-) (limited to 'graph-inventory/fluent-builder-maven-plugin/src/main/java') diff --git a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGenerator.java b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGenerator.java index 4a8b7d90c6..efbeba203c 100644 --- a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGenerator.java +++ b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGenerator.java @@ -39,11 +39,13 @@ public class FluentGenerator { private final String pluralClass; private final String builderName; private final String nameClass; + private final String singleFragmentClass; + private final String pluralFragmentClass; public FluentGenerator(Log log, String location, String destinationClasspath, String swaggerLocation, String builderName, String singularBuilderClass, String pluralBuilderClass, String topLevelBuilderClass, - String baseBuilderClass, String singularClass, String pluralClass, String nameClass) - throws JsonProcessingException { + String baseBuilderClass, String singularClass, String pluralClass, String nameClass, + String singleFragmentClass, String pluralFragmentClass) throws JsonProcessingException { this.location = location; this.CLASSPATH = destinationClasspath; @@ -55,6 +57,8 @@ public class FluentGenerator { this.singularClass = singularClass; this.pluralClass = pluralClass; this.nameClass = nameClass; + this.singleFragmentClass = singleFragmentClass; + this.pluralFragmentClass = pluralFragmentClass; doc = new SwaggerConverter(log).getDoc(swaggerLocation); } @@ -241,7 +245,8 @@ public class FluentGenerator { } protected TypeSpec createTypes() { - List params = doc.values().stream().filter(item -> item.getType().equals("singular")) + List params = doc.values().stream() + .filter(item -> item.getType().equals("singular") || item.getType().equals("plural")) .sorted(Comparator.comparing(item -> item.getName())).map(item -> { ClassName nameType = ClassName.get(CLASSPATH, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, item.getName())) @@ -274,6 +279,18 @@ public class FluentGenerator { "\"" + oType.getPaths().stream().collect(Collectors.joining("\", \"")) + "\"") .build()); + if (oType.getType().equals("plural")) { + Pair path = splitClasspath(this.pluralFragmentClass); + ClassName fragmentClass = ClassName.get(path.getLeft(), path.getRight()); + path = splitClasspath(this.baseBuilderClass); + ClassName baseClass = ClassName.get(path.getLeft(), path.getRight()); + + classFields.add(FieldSpec.builder(fragmentClass, "fragment") + .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC) + .initializer("new $T(new $L(new $T(){}))", fragmentClass, upperCamel(oType.getName()), baseClass) + .build()); + } + ClassName superInterface; String name; if (oType.getType().equals("plural")) { @@ -294,6 +311,34 @@ public class FluentGenerator { .addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addStatement("return Info.paths").build()); methods.add(MethodSpec.methodBuilder("getPartialUri").returns(String.class).addModifiers(Modifier.PUBLIC) .addAnnotation(Override.class).addStatement("return Info.partialUri").build()); + + if (oType.getType().equals("plural")) { + Pair path = splitClasspath(this.pluralFragmentClass); + ClassName fragmentClass = ClassName.get(path.getLeft(), path.getRight()); + methods.add(MethodSpec.methodBuilder("getFragment").returns(fragmentClass).addModifiers(Modifier.PUBLIC) + .addStatement("return fragment").build()); + } else if (oType.getType().equals("singular")) { + Pair path = splitClasspath(this.singleFragmentClass); + ClassName fragmentClass = ClassName.get(path.getLeft(), path.getRight()); + path = splitClasspath(this.baseBuilderClass); + ClassName baseClass = ClassName.get(path.getLeft(), path.getRight()); + List typeParams = new ArrayList<>(); + + for (ObjectField oF : oType.getFields()) { + if (oF.getType().equals("string")) { + typeParams.add(ParameterSpec.builder(String.class, lowerCamel(makeValidJavaVariable(oF.getName()))) + .build()); + } else if (oF.getType().equals("integer")) { + typeParams.add( + ParameterSpec.builder(int.class, lowerCamel(makeValidJavaVariable(oF.getName()))).build()); + } + } + methods.add(MethodSpec.methodBuilder("getFragment").returns(fragmentClass).addParameters(typeParams) + .addModifiers(Modifier.PUBLIC) + .addStatement("return new $T(new $L(new $T(){}, $L))", fragmentClass, upperCamel(oType.getName()), + baseClass, typeParams.stream().map(item -> item.name).collect(Collectors.joining(", "))) + .build()); + } if (!oType.getType().equals("top level")) { classFields.add(FieldSpec.builder(String.class, "name") .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC).initializer("$S", name).build()); diff --git a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGeneratorMojo.java b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGeneratorMojo.java index 9de249faa2..8c672e3e7d 100644 --- a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGeneratorMojo.java +++ b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGeneratorMojo.java @@ -34,13 +34,17 @@ public class FluentGeneratorMojo extends AbstractMojo { private String pluralClass; @Parameter private String nameClass; + @Parameter + private String singleFragmentClass; + @Parameter + private String pluralFragmentClass; public void execute() throws MojoExecutionException, MojoFailureException { try { new FluentGenerator(getLog(), destination, destinationClasspath, swaggerLocation, builderName, singularBuilderClass, pluralBuilderClass, topLevelBuilderClass, baseBuilderClass, singularClass, - pluralClass, nameClass).run(); + pluralClass, nameClass, singleFragmentClass, pluralFragmentClass).run(); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); -- cgit 1.2.3-korg