Search in sources :

Example 1 with INCOMING_PROP_PREFIX

use of io.automatiko.engine.codegen.CodeGenConstants.INCOMING_PROP_PREFIX in project automatiko-engine by automatiko-io.

the class MessageConsumerGenerator method generate.

public String generate() {
    String sanitizedName = CodegenUtils.triggerSanitizedName(trigger, process.getVersion());
    String connector = CodegenUtils.getConnector(INCOMING_PROP_PREFIX + sanitizedName + ".connector", context, (String) trigger.getContext("connector"));
    if (connector != null && !OPERATOR_CONNECTOR.equals(connector)) {
        context.setApplicationProperty(INCOMING_PROP_PREFIX + sanitizedName + ".connector", connector);
        appendConnectorSpecificProperties(connector);
    }
    // operator consumer only supports starting message endpoints
    if (OPERATOR_CONNECTOR.equals(connector) && !trigger.isStart()) {
        return null;
    }
    CompilationUnit clazz = parse(this.getClass().getResourceAsStream(consumerTemplate(connector)));
    clazz.setPackageDeclaration(process.getPackageName());
    clazz.addImport(modelfqcn);
    // add functions so they can be easily accessed in message consumer classes
    clazz.addImport(new ImportDeclaration(BaseFunctions.class.getCanonicalName(), true, true));
    context.getBuildContext().classThatImplement(Functions.class.getCanonicalName()).forEach(c -> clazz.addImport(new ImportDeclaration(c, true, true)));
    ClassOrInterfaceDeclaration template = clazz.findFirst(ClassOrInterfaceDeclaration.class).get();
    template.setName(resourceClazzName);
    template.findAll(ClassOrInterfaceType.class).forEach(cls -> interpolateTypes(cls, dataClazzName));
    template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("configure")).forEach(md -> md.addAnnotation("javax.annotation.PostConstruct"));
    template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("consume") || md.getNameAsString().equals("cleanup") || md.getNameAsString().equals("reconcile")).forEach(md -> {
        md.findAll(StringLiteralExpr.class).forEach(str -> str.setString(str.asString().replace("$Trigger$", trigger.getName())));
        md.findAll(ClassOrInterfaceType.class).forEach(t -> t.setName(t.getNameAsString().replace("$DataEventType$", messageDataEventClassName)));
        md.findAll(ClassOrInterfaceType.class).forEach(t -> t.setName(t.getNameAsString().replace("$DataType$", trigger.getDataType())));
        md.setType(md.getTypeAsString().replace("$DataType$", trigger.getDataType()));
    });
    template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("convert")).forEach(md -> {
        md.setType(md.getTypeAsString().replace("$DataType$", trigger.getDataType()));
        md.findAll(CastExpr.class).forEach(c -> c.setType(c.getTypeAsString().replace("$DataType$", trigger.getDataType())));
        md.findAll(ClassOrInterfaceType.class).forEach(t -> t.setName(t.getNameAsString().replace("$DataType$", trigger.getDataType())));
    });
    if (trigger.getModelRef().startsWith("#")) {
        template.findAll(MethodCallExpr.class).stream().filter(m -> m.getNameAsString().endsWith("$ModelRef$")).forEach(m -> {
            m.getParentNode().ifPresent(p -> p.removeForced());
        });
    } else {
        template.findAll(MethodCallExpr.class).forEach(this::interpolateStrings);
    }
    if (useInjection()) {
        if (!OPERATOR_CONNECTOR.equals(connector)) {
            annotator.withApplicationComponent(template);
        }
        template.findAll(FieldDeclaration.class, fd -> isProcessField(fd)).forEach(fd -> annotator.withNamedInjection(fd, processId + version));
        template.findAll(FieldDeclaration.class, fd -> isApplicationField(fd)).forEach(fd -> annotator.withInjection(fd));
        template.findAll(FieldDeclaration.class, fd -> fd.getVariables().get(0).getNameAsString().equals("converter")).forEach(fd -> {
            annotator.withInjection(fd);
            fd.getVariable(0).setType(fd.getVariable(0).getTypeAsString().replace("$DataType$", trigger.getDataType()));
        });
        template.findAll(FieldDeclaration.class, fd -> fd.getVariable(0).getNameAsString().equals("useCloudEvents")).forEach(fd -> annotator.withConfigInjection(fd, "quarkus.automatiko.messaging.as-cloudevents"));
        template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("consume")).forEach(md -> {
            if (persistence) {
                annotator.withBlocking(md);
            }
            annotator.withIncomingMessage(md, sanitizedName);
            if (context.getBuildContext().hasClassAvailable("org.eclipse.microprofile.opentracing.Traced")) {
                md.addAnnotation("org.eclipse.microprofile.opentracing.Traced");
            }
        });
    } else {
        template.findAll(FieldDeclaration.class, fd -> isProcessField(fd)).forEach(fd -> initializeProcessField(fd, template));
        template.findAll(FieldDeclaration.class, fd -> isApplicationField(fd)).forEach(fd -> initializeApplicationField(fd, template));
        template.findAll(FieldDeclaration.class, fd -> fd.getVariables().get(0).getNameAsString().equals("converter")).forEach(fd -> {
            fd.getVariable(0).setType(fd.getVariable(0).getTypeAsString().replace("$DataType$", trigger.getDataType()));
        });
    }
    BlockStmt filterExpressionBody = new BlockStmt();
    if (trigger.getContext("filterExpression") != null) {
        filterExpressionBody.addStatement(new ReturnStmt(new NameExpr((String) trigger.getContext("filterExpression"))));
    } else {
        filterExpressionBody.addStatement(new ReturnStmt(new BooleanLiteralExpr(true)));
    }
    BlockStmt body = new BlockStmt();
    if (trigger.getCorrelation() != null) {
        body.addStatement(new ReturnStmt(new StringLiteralExpr(trigger.getCorrelation())));
    } else if (trigger.getCorrelationExpression() != null) {
        body.addStatement(new ReturnStmt(new NameExpr(trigger.getCorrelationExpression())));
    } else {
        body.addStatement(new ReturnStmt(new NullLiteralExpr()));
    }
    boolean cloudEvents = context.getBuildContext().config().messaging().asCloudevents() || Boolean.parseBoolean(context.getApplicationProperty("quarkus.automatiko.messaging.as-cloudevents").orElse("false"));
    if (cloudEvents) {
        template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("correlationEvent")).forEach(md -> {
            md.setBody(body);
            md.getParameters().get(0).setType(messageDataEventClassName);
        });
        template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("acceptedEvent")).forEach(md -> {
            md.setBody(filterExpressionBody);
            md.getParameters().get(0).setType(messageDataEventClassName);
        });
    } else {
        template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("correlationPayload")).forEach(md -> {
            md.setBody(body);
            md.getParameters().get(0).setType(trigger.getDataType());
        });
        template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("acceptedPayload")).forEach(md -> {
            md.setBody(filterExpressionBody);
            md.getParameters().get(0).setType(trigger.getDataType());
        });
    }
    template.addMember(new MethodDeclaration().setName("canStartInstance").setType(Boolean.class).setModifiers(Keyword.PROTECTED).setBody(new BlockStmt().addStatement(new ReturnStmt(new BooleanLiteralExpr(trigger.isStart())))));
    // add connector and message name as static fields of the class
    FieldDeclaration connectorField = new FieldDeclaration().setStatic(true).setFinal(true).addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, "String"), "CONNECTOR", new StringLiteralExpr(connector)));
    template.addMember(connectorField);
    FieldDeclaration messageNameField = new FieldDeclaration().setStatic(true).setFinal(true).addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, "String"), "MESSAGE", new StringLiteralExpr(trigger.getName())));
    template.addMember(messageNameField);
    if (OPERATOR_CONNECTOR.equals(connector)) {
        // add event filter
        String eventFilterClass = (String) trigger.getContext("eventFilter");
        if (eventFilterClass != null && !eventFilterClass.isEmpty()) {
            AnnotationExpr controllerAnnotation = template.getAnnotationByName("Controller").get();
            ((NormalAnnotationExpr) controllerAnnotation).addPair("eventFilters", new NameExpr(eventFilterClass + ".class"));
        }
    }
    template.getMembers().sort(new BodyDeclarationComparator());
    ImportsOrganizer.organize(clazz);
    return clazz.toString().replaceAll("\\$DataType\\$", trigger.getDataType()).replaceAll("\\$DataEventType\\$", messageDataEventClassName).replaceAll("\\$ProcessId\\$", processId + version).replaceAll("\\$GenControllerParam\\$", (String) trigger.getContext("generationAware", "true")).replaceAll("\\$ControllerParam\\$", "{" + Stream.of(namespaces.split(",")).filter(s -> !s.trim().isEmpty()).map(s -> "\"" + s.trim() + "\"").collect(Collectors.joining(",")) + "}");
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) CAMEL_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.CAMEL_CONNECTOR) CodegenUtils.isApplicationField(io.automatiko.engine.codegen.CodegenUtils.isApplicationField) GeneratorContext(io.automatiko.engine.codegen.GeneratorContext) CastExpr(com.github.javaparser.ast.expr.CastExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) AMQP_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.AMQP_CONNECTOR) MQTT_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.MQTT_CONNECTOR) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) CodegenUtils.interpolateTypes(io.automatiko.engine.codegen.CodegenUtils.interpolateTypes) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) StringUtils(io.automatiko.engine.services.utils.StringUtils) CompilationUnit(com.github.javaparser.ast.CompilationUnit) TriggerMetaData(io.automatiko.engine.workflow.compiler.canonical.TriggerMetaData) HTTP_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.HTTP_CONNECTOR) KAFKA_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.KAFKA_CONNECTOR) Keyword(com.github.javaparser.ast.Modifier.Keyword) DependencyInjectionAnnotator(io.automatiko.engine.codegen.di.DependencyInjectionAnnotator) CodegenUtils.isProcessField(io.automatiko.engine.codegen.CodegenUtils.isProcessField) INCOMING_PROP_PREFIX(io.automatiko.engine.codegen.CodeGenConstants.INCOMING_PROP_PREFIX) CodegenUtils(io.automatiko.engine.codegen.CodegenUtils) BaseFunctions(io.automatiko.engine.services.execution.BaseFunctions) JMS_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.JMS_CONNECTOR) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) OPERATOR_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.OPERATOR_CONNECTOR) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) WorkflowProcess(io.automatiko.engine.api.definition.process.WorkflowProcess) BodyDeclarationComparator(io.automatiko.engine.codegen.BodyDeclarationComparator) NameExpr(com.github.javaparser.ast.expr.NameExpr) Collectors(java.util.stream.Collectors) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Stream(java.util.stream.Stream) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Functions(io.automatiko.engine.api.Functions) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ImportsOrganizer(io.automatiko.engine.codegen.ImportsOrganizer) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) CastExpr(com.github.javaparser.ast.expr.CastExpr) BodyDeclarationComparator(io.automatiko.engine.codegen.BodyDeclarationComparator) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

StaticJavaParser.parse (com.github.javaparser.StaticJavaParser.parse)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)1 Keyword (com.github.javaparser.ast.Modifier.Keyword)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)1 CastExpr (com.github.javaparser.ast.expr.CastExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1