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;
}
Aggregations