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