Skip to content

Commit

Permalink
LORD Added support for any subdto setters -> initalization of subdto …
Browse files Browse the repository at this point in the history
…with object factories, refactoring
  • Loading branch information
sgartner03 committed May 16, 2024
1 parent 08c8ea4 commit 5356553
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 135 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,56 @@
package com.gepardec.wor.lord.stdh.v2.visitors;

import com.gepardec.wor.lord.stdh.v2.common.Accessor;
import com.gepardec.wor.lord.stdh.v2.common.Accumulator;
import com.gepardec.wor.lord.util.ParserUtil;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Tree;
import org.openrewrite.java.AddImport;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.RemoveImport;
import org.openrewrite.java.tree.CoordinateBuilder;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaCoordinates;
import org.openrewrite.java.tree.Statement;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.openrewrite.java.JavaTemplate.apply;

public class BinaryDtoInitToWebVisitor extends JavaIsoVisitor<ExecutionContext> {
private String variableName;
private boolean usesStdh;
private Accessor accessor;
private Accumulator accumulator;

private List<String> settersAdded = new LinkedList<>();

private static final String OBJECT_FACTORY_NAME = "objectFactory";

private static final String NEW_WEB_DTO = "#{} #{} = new #{}();";
private static final String NEW_WEB_DTO = "%s %s = new %s();";

public static final String SET_NEW_STDH_TEMPLATE = "\n#{}.setOmStandardRequestHeader(%s.createOmStandardRequestHeader());";
private static final String NEW_WEB_DTO_WITH_STDH_TEMPLATE = NEW_WEB_DTO + SET_NEW_STDH_TEMPLATE;
public static final String SETTER_TEMPLATE = "%s.%s(%s);";


public BinaryDtoInitToWebVisitor(String variableName, boolean usesStdh, Accumulator accumulator) {
public BinaryDtoInitToWebVisitor(String variableName, Accessor accessor, Accumulator accumulator) {
this.variableName = variableName;
this.accessor = accessor;
this.accumulator = accumulator;
}

public BinaryDtoInitToWebVisitor(String variableName, Accumulator accumulator) {
this.variableName = variableName;
this.usesStdh = usesStdh;
this.accumulator = accumulator;
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
method = super.visitMethodDeclaration(method, ctx);
List<Statement> statements = method.getBody().getStatements();

boolean createsWebDto = false;
Optional<J.VariableDeclarations> dtoDeclarationsOptional = statements.stream()
.filter(J.VariableDeclarations.class::isInstance)
.map(J.VariableDeclarations.class::cast)
Expand All @@ -48,39 +62,91 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
}

J.VariableDeclarations dtoDeclarations = dtoDeclarationsOptional.get();
String binaryType = dtoDeclarations.getType().toString();
String dtoType = dtoDeclarationsOptional
.map(declaration -> declaration.getTypeExpression().toString())
.get();

Optional<String> wsdlTypeOptional = accumulator.getWsdlTypeFromBinary(binaryType);
String dtoShortType = Accumulator.shortNameOfFullyQualified(dtoType);

if (wsdlTypeOptional.isEmpty()) {
String wsdlType = null;
if (dtoShortType.contains("Dto")) {
createsWebDto = true;
String binaryType = dtoDeclarations.getType().toString();
Optional<String> wsdlTypeOptional = accumulator.getWsdlTypeFromBinary(binaryType);

if (wsdlTypeOptional.isEmpty()) {
return method;
}

wsdlType = wsdlTypeOptional.get();

doAfterVisit(new AddImport<>(wsdlType, null, false));
doAfterVisit(new ClassUsingBinaryDtoToWebVisitor(
OBJECT_FACTORY_NAME,
wsdlType.substring(0, wsdlType.lastIndexOf('.'))));
doAfterVisit(new RemoveImport<>(binaryType, true));
}
if (accumulator.getIOTypesShort().contains(dtoShortType)) {
wsdlType = dtoType;
}

if (wsdlType == null) {
return method;
}
String wsdlType = wsdlTypeOptional.get();
String shortWsdlType = Accumulator.shortNameOfFullyQualified(wsdlType);

if (usesStdh) {
doAfterVisit(new ClassUsingBinaryDtoToWebVisitor(OBJECT_FACTORY_NAME, packageOf(wsdlType)));
Optional<JavaTemplate> template = getCreateDtoJavaTemplate(Accumulator.shortNameOfFullyQualified(wsdlType), createsWebDto);

CoordinateBuilder.VariableDeclarations coordinateBuilder = dtoDeclarations.getCoordinates();
JavaCoordinates coordinates = createsWebDto ? coordinateBuilder.replace() : coordinateBuilder.after();
J.MethodDeclaration usedMethodDeclaration = method;
return template
.map(javaTemplate -> javaTemplate.apply(updateCursor(usedMethodDeclaration), coordinates))
.map(J.MethodDeclaration.class::cast)
.orElse(method);
}


public String createSetterStatement(Accessor accessor) {
if (accessor.getParent().isEmpty()) {
return "";
}
String getter = accessor.getParent().get().getName();
String setter = "set" + getter.substring(getter.startsWith("is") ? 2 : 3);
// settersAdded.add(setter);

String[] typeParts = accessor.getClazz().split("\\.");
String objectFactoryCreate = "%s.create%s()".formatted(OBJECT_FACTORY_NAME, typeParts[typeParts.length - 1]);

maybeAddImport(wsdlType);
doAfterVisit(new RemoveImport<>(binaryType, true));
J.MethodDeclaration declarationUsingWebDto = getCreateDtoJavaTemplate(wsdlType).apply(
updateCursor(method),
dtoDeclarations.getCoordinates().replace(),
getCreateDtoJavaTemplateParams(shortWsdlType)
);
return declarationUsingWebDto;
return SETTER_TEMPLATE.formatted(variableName, setter, objectFactoryCreate);
}

private Object[] getCreateDtoJavaTemplateParams(String shortWsdlType) {
return usesStdh ?
new Object[]{shortWsdlType, variableName, shortWsdlType, variableName} :
new Object[]{shortWsdlType, variableName, shortWsdlType};
return accessor == null ?
new Object[]{shortWsdlType, variableName, shortWsdlType} :
new Object[]{shortWsdlType, variableName, shortWsdlType, variableName};

}

private JavaTemplate getCreateDtoJavaTemplate(String wsdlType) {
String template = usesStdh ? String.format(NEW_WEB_DTO_WITH_STDH_TEMPLATE, OBJECT_FACTORY_NAME) : NEW_WEB_DTO;
return javaTemplateOf(template, wsdlType);
private Optional<JavaTemplate> getCreateDtoJavaTemplate(String wsdlType, boolean createsWebDto) {
StringBuilder template = new StringBuilder();

if (createsWebDto) {
template.append(NEW_WEB_DTO.formatted(wsdlType, variableName, wsdlType));
}
if (accessor != null)
template
.append(createsWebDto ? "\n" : "")
.append(createSetterStatement((accessor)));

if (template.isEmpty()) {
return Optional.empty();
}

return Optional.of(JavaTemplate.builder(template.toString())
.javaParser(ParserUtil.createParserWithRuntimeClasspath())
.imports(wsdlType)
.contextSensitive()
.build());
}

private static JavaTemplate javaTemplateOf(String template, String wsdlType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public class BinaryDtoToWebVisitor extends JavaIsoVisitor<ExecutionContext> {

private static final String STDH_GETTER_NAME = "getOmStandardRequestHeader";

private static final String BINARY_DTO_NAME = "LaqamhsuDto";

private static final JavaTemplate STDH_SETTER = JavaTemplate
.builder("#{}.#{}(#{});")
.javaParser(ParserUtil.createParserWithRuntimeClasspath())
Expand Down Expand Up @@ -53,13 +51,17 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu

String type = method.getSelect().getType().toString();
String methodName = method.getSimpleName();
String newSetter = generateWebDtoSetter(methodName, type);
Optional<Accessor> accessor = findAccessor(methodName, type);
if (accessor.isEmpty()) {
return method;
}
String newSetter = generateWebDtoSetter(accessor.get(), methodName);
if (isNotNested(methodName)) {
doAfterVisit(new BinaryDtoInitToWebVisitor(instanceName, false, accumulator));
doAfterVisit(new BinaryDtoInitToWebVisitor(instanceName, accumulator));
return method;
}

doAfterVisit(new BinaryDtoInitToWebVisitor(instanceName, true, accumulator));
doAfterVisit(new BinaryDtoInitToWebVisitor(instanceName, accessor.get(), accumulator));

String argument = method.getArguments().get(0).printTrimmed(getCursor());
return STDH_SETTER.apply(updateCursor(method),
Expand All @@ -70,13 +72,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
);
}

private String generateWebDtoSetter(String methodName, String methodType) {
Optional<Accessor> accessor = findAccessor(methodName, methodType);
if (accessor.isEmpty()) {
return methodName;
}

Optional<Accessor> nextAccessor = accessor.get().getParent();
private String generateWebDtoSetter(Accessor accessor, String methodName) {
Optional<Accessor> nextAccessor = accessor.getParent();
StringBuilder stringBuilder = new StringBuilder();
while (nextAccessor.isPresent()) {
Accessor accessorIteration = nextAccessor.get();
Expand Down
Loading

0 comments on commit 5356553

Please sign in to comment.