Search in sources :

Example 1 with SpringBootKogitoBuildContext

use of org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext in project kogito-runtimes by kiegroup.

the class PersistenceGenerator method postgresqlBasedPersistence.

private Collection<GeneratedFile> postgresqlBasedPersistence() {
    ClassOrInterfaceDeclaration persistenceProviderClazz = new ClassOrInterfaceDeclaration().setName(KOGITO_PROCESS_INSTANCE_FACTORY_IMPL).setModifiers(Modifier.Keyword.PUBLIC).addExtendedType(KOGITO_PROCESS_INSTANCE_FACTORY_PACKAGE);
    Collection<GeneratedFile> generatedFiles = new ArrayList<>();
    if (context().hasDI()) {
        context().getDependencyInjectionAnnotator().withApplicationComponent(persistenceProviderClazz);
        // injecting constructor with parameter
        final String pgPoolClass = "io.vertx.pgclient.PgPool";
        ConstructorDeclaration constructor = persistenceProviderClazz.addConstructor(Keyword.PUBLIC).addParameter(pgPoolClass, CLIENT).addParameter(StaticJavaParser.parseClassOrInterfaceType(Boolean.class.getName()), AUTO_DDL).addParameter(StaticJavaParser.parseClassOrInterfaceType(Long.class.getName()), QUERY_TIMEOUT).setBody(new BlockStmt().addStatement(new ExplicitConstructorInvocationStmt().setThis(false).addArgument(new NameExpr(CLIENT)).addArgument(AUTO_DDL).addArgument(QUERY_TIMEOUT)));
        context().getDependencyInjectionAnnotator().withConfigInjection(constructor.getParameterByName(AUTO_DDL).get(), KOGITO_PERSISTENCE_AUTO_DDL, Boolean.TRUE.toString());
        context().getDependencyInjectionAnnotator().withConfigInjection(constructor.getParameterByName(QUERY_TIMEOUT).get(), KOGITO_PERSISTENCE_QUERY_TIMEOUT, String.valueOf(10000));
        context().getDependencyInjectionAnnotator().withInjection(constructor);
        // empty constructor for DI
        persistenceProviderClazz.addConstructor(Keyword.PROTECTED);
        if (context() instanceof SpringBootKogitoBuildContext) {
            context().getDependencyInjectionAnnotator().withNamed(constructor.getParameterByName(CLIENT).get(), KOGITO);
            // creating PgClient default producer class
            ClassOrInterfaceDeclaration pgClientProducerClazz = new ClassOrInterfaceDeclaration().setName("PgClientProducer").setModifiers(Modifier.Keyword.PUBLIC);
            // creating PgClient producer
            Parameter uriConfigParam = new Parameter().setType(StaticJavaParser.parseClassOrInterfaceType(Optional.class.getName()).setTypeArguments(StaticJavaParser.parseClassOrInterfaceType(String.class.getName()))).setName("uri");
            MethodDeclaration clientProviderMethod = pgClientProducerClazz.addMethod(CLIENT, Keyword.PUBLIC).setType(// PgPool
            pgPoolClass).addParameter(uriConfigParam).setBody(// return uri.isPresent() ?  PgPool.pool(uri.get()) : PgPool.pool()
            new BlockStmt().addStatement(new ReturnStmt(new ConditionalExpr(new MethodCallExpr(new NameExpr(uriConfigParam.getName()), "isPresent"), new MethodCallExpr(new NameExpr(pgPoolClass), "pool").addArgument(new MethodCallExpr(new NameExpr("uri"), "get")), new MethodCallExpr(new NameExpr(pgPoolClass), "pool")))));
            // inserting DI annotations
            context().getDependencyInjectionAnnotator().withConfigInjection(uriConfigParam, KOGITO_PERSISTENCE_POSTGRESQL_CONNECTION_URI);
            context().getDependencyInjectionAnnotator().withProduces(clientProviderMethod, true);
            context().getDependencyInjectionAnnotator().withNamed(clientProviderMethod, KOGITO);
            context().getDependencyInjectionAnnotator().withApplicationComponent(pgClientProducerClazz);
            Optional<GeneratedFile> generatedPgClientFile = generatePersistenceProviderClazz(pgClientProducerClazz, new CompilationUnit(KOGITO_PROCESS_INSTANCE_PACKAGE).addType(pgClientProducerClazz));
            generatedPgClientFile.ifPresent(generatedFiles::add);
        }
    }
    addOptimisticLockFlag(persistenceProviderClazz);
    Optional<GeneratedFile> generatedPgClientFile = generatePersistenceProviderClazz(persistenceProviderClazz, new CompilationUnit(KOGITO_PROCESS_INSTANCE_PACKAGE).addType(persistenceProviderClazz));
    generatedPgClientFile.ifPresent(generatedFiles::add);
    return generatedFiles;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) NameExpr(com.github.javaparser.ast.expr.NameExpr) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) ConditionalExpr(com.github.javaparser.ast.expr.ConditionalExpr) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) SpringBootKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext) Parameter(com.github.javaparser.ast.body.Parameter) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 2 with SpringBootKogitoBuildContext

use of org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext in project kogito-runtimes by kiegroup.

the class PersistenceGenerator method mongodbBasedPersistence.

private Collection<GeneratedFile> mongodbBasedPersistence() {
    ClassOrInterfaceDeclaration persistenceProviderClazz = new ClassOrInterfaceDeclaration().setName(KOGITO_PROCESS_INSTANCE_FACTORY_IMPL).setModifiers(Modifier.Keyword.PUBLIC).addExtendedType(KOGITO_PROCESS_INSTANCE_FACTORY_PACKAGE);
    persistenceProviderClazz.addConstructor(Keyword.PUBLIC).setBody(new BlockStmt().addStatement(new ExplicitConstructorInvocationStmt(false, null, NodeList.nodeList(new NullLiteralExpr()))));
    ConstructorDeclaration constructor = createConstructorForClazz(persistenceProviderClazz);
    Optional<GeneratedFile> generatedClientFile = Optional.empty();
    Optional<GeneratedFile> generatedTMFile = Optional.empty();
    if (context().hasDI()) {
        context().getDependencyInjectionAnnotator().withApplicationComponent(persistenceProviderClazz);
        context().getDependencyInjectionAnnotator().withInjection(constructor);
        FieldDeclaration dbNameField = new FieldDeclaration().addVariable(new VariableDeclarator().setType(new ClassOrInterfaceType(null, new SimpleName(Optional.class.getCanonicalName()), NodeList.nodeList(new ClassOrInterfaceType(null, String.class.getCanonicalName())))).setName(MONGODB_DB_NAME));
        // injecting dbName from quarkus/springboot properties else default kogito
        if (context() instanceof QuarkusKogitoBuildContext) {
            context().getDependencyInjectionAnnotator().withConfigInjection(dbNameField, QUARKUS_MONGODB_DATABASE);
        } else if (context() instanceof SpringBootKogitoBuildContext) {
            context().getDependencyInjectionAnnotator().withConfigInjection(dbNameField, SPRING_DATA_MONGODB_DATABASE);
        }
        BlockStmt dbNameMethodBody = new BlockStmt();
        dbNameMethodBody.addStatement(new ReturnStmt(new MethodCallExpr(new NameExpr(MONGODB_DB_NAME), OR_ELSE).addArgument(new StringLiteralExpr(KOGITO))));
        MethodDeclaration dbNameMethod = new MethodDeclaration().addModifier(Keyword.PUBLIC).setName(MONGODB_DB_NAME).setType(String.class).setBody(dbNameMethodBody);
        persistenceProviderClazz.addMember(dbNameField);
        persistenceProviderClazz.addMember(dbNameMethod);
        generatedTMFile = mongodbBasedTransaction(persistenceProviderClazz);
        addOptimisticLockFlag(persistenceProviderClazz);
        generatedClientFile = generatePersistenceProviderClazz(persistenceProviderClazz, new CompilationUnit(KOGITO_PROCESS_INSTANCE_PACKAGE).addType(persistenceProviderClazz));
    }
    Collection<GeneratedFile> generatedFiles = new ArrayList<>();
    generatedClientFile.ifPresent(generatedFiles::add);
    generatedTMFile.ifPresent(generatedFiles::add);
    return generatedFiles;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Optional(java.util.Optional) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) SimpleName(com.github.javaparser.ast.expr.SimpleName) QuarkusKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.QuarkusKogitoBuildContext) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ArrayList(java.util.ArrayList) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) SpringBootKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 3 with SpringBootKogitoBuildContext

use of org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext in project kogito-runtimes by kiegroup.

the class PostgrePersistenceGeneratorTest method testGeneratedFiles.

@ParameterizedTest
@MethodSource("persistenceTestContexts")
void testGeneratedFiles(KogitoBuildContext context) {
    context.setApplicationProperty(KOGITO_PERSISTENCE_TYPE, persistenceType());
    ReflectionProtoGenerator protoGenerator = ReflectionProtoGenerator.builder().build(Collections.singleton(GeneratedPOJO.class));
    PersistenceGenerator persistenceGenerator = new PersistenceGenerator(context, protoGenerator, new ReflectionMarshallerGenerator(context));
    Collection<GeneratedFile> generatedFiles = persistenceGenerator.generate();
    int factoryFiles = context instanceof SpringBootKogitoBuildContext ? 2 : 1;
    int marshallerFiles = hasProtoMarshaller(context) ? 14 : 0;
    int dataIndexFiles = hasDataIndexProto(context) ? 2 : 0;
    int expectedNumberOfFiles = factoryFiles + marshallerFiles + dataIndexFiles;
    assertThat(generatedFiles).hasSize(expectedNumberOfFiles);
    Optional<GeneratedFile> persistenceFactoryImpl = generatedFiles.stream().filter(gf -> gf.relativePath().equals("org/kie/kogito/persistence/KogitoProcessInstancesFactoryImpl.java")).findFirst();
    assertThat(persistenceFactoryImpl).isPresent();
    validateClassInCompilationUnit(persistenceFactoryImpl.get().contents());
    Optional<GeneratedFile> pgClientProducer = generatedFiles.stream().filter(gf -> gf.relativePath().equals("org/kie/kogito/persistence/PgClientProducer.java")).findFirst();
    if (context instanceof SpringBootKogitoBuildContext) {
        assertThat(pgClientProducer).isPresent();
        validateClassInCompilationUnit(pgClientProducer.get().contents());
    } else {
        assertThat(pgClientProducer).isEmpty();
    }
}
Also used : ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) PersistenceGenerator.hasDataIndexProto(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasDataIndexProto) Collection(java.util.Collection) SpringBootKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ByteArrayInputStream(java.io.ByteArrayInputStream) POSTGRESQL_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.POSTGRESQL_PERSISTENCE_TYPE) PersistenceGenerator.hasProtoMarshaller(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasProtoMarshaller) KOGITO_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE) Optional(java.util.Optional) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) CompilationUnit(com.github.javaparser.ast.CompilationUnit) NoSuchElementException(java.util.NoSuchElementException) Collections(java.util.Collections) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodSource(org.junit.jupiter.params.provider.MethodSource) ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) SpringBootKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)3 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)3 GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)3 SpringBootKogitoBuildContext (org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext)3 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)2 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)2 ArrayList (java.util.ArrayList)2 Optional (java.util.Optional)2 StaticJavaParser.parse (com.github.javaparser.StaticJavaParser.parse)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 ConditionalExpr (com.github.javaparser.ast.expr.ConditionalExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 SimpleName (com.github.javaparser.ast.expr.SimpleName)1