Search in sources :

Example 1 with QuarkusKogitoBuildContext

use of org.kie.kogito.codegen.api.context.impl.QuarkusKogitoBuildContext 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)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)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 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 SimpleName (com.github.javaparser.ast.expr.SimpleName)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)1 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)1 QuarkusKogitoBuildContext (org.kie.kogito.codegen.api.context.impl.QuarkusKogitoBuildContext)1 SpringBootKogitoBuildContext (org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext)1