use of io.automatiko.engine.codegen.BodyDeclarationComparator in project automatiko-engine by automatiko-io.
the class PersistenceGenerator method fileSystemBasedPersistence.
protected void fileSystemBasedPersistence(List<GeneratedFile> generatedFiles) {
ClassOrInterfaceDeclaration persistenceProviderClazz = new ClassOrInterfaceDeclaration().setName("ProcessInstancesFactoryImpl").setModifiers(Modifier.Keyword.PUBLIC).addExtendedType("io.automatiko.engine.addons.persistence.AbstractProcessInstancesFactory");
CompilationUnit compilationUnit = new CompilationUnit("io.automatiko.engine.addons.persistence.impl");
compilationUnit.getTypes().add(persistenceProviderClazz);
if (useInjection()) {
annotator.withApplicationComponent(persistenceProviderClazz);
FieldDeclaration pathField = new FieldDeclaration().addVariable(new VariableDeclarator().setType(new ClassOrInterfaceType(null, new SimpleName(Optional.class.getCanonicalName()), NodeList.nodeList(new ClassOrInterfaceType(null, String.class.getCanonicalName())))).setName(PATH_NAME));
annotator.withConfigInjection(pathField, PERSISTENCE_FS_PATH_PROP);
// allow to inject path for the file system storage
BlockStmt pathMethodBody = new BlockStmt();
pathMethodBody.addStatement(new ReturnStmt(new MethodCallExpr(new NameExpr(PATH_NAME), "orElse").addArgument(new StringLiteralExpr("/tmp"))));
MethodDeclaration pathMethod = new MethodDeclaration().addModifier(Keyword.PUBLIC).setName(PATH_NAME).setType(String.class).setBody(pathMethodBody);
persistenceProviderClazz.addMember(pathField);
persistenceProviderClazz.addMember(pathMethod);
addCodecComponents(persistenceProviderClazz);
}
String packageName = compilationUnit.getPackageDeclaration().map(pd -> pd.getName().toString()).orElse("");
String clazzName = packageName + "." + persistenceProviderClazz.findFirst(ClassOrInterfaceDeclaration.class).map(c -> c.getName().toString()).get();
generatedFiles.add(new GeneratedFile(GeneratedFile.Type.CLASS, clazzName.replace('.', '/') + ".java", compilationUnit.toString().getBytes(StandardCharsets.UTF_8)));
persistenceProviderClazz.getMembers().sort(new BodyDeclarationComparator());
}
use of io.automatiko.engine.codegen.BodyDeclarationComparator in project automatiko-engine by automatiko-io.
the class PersistenceGenerator method cassandraBasedPersistence.
protected void cassandraBasedPersistence(List<GeneratedFile> generatedFiles) {
ClassOrInterfaceDeclaration persistenceProviderClazz = new ClassOrInterfaceDeclaration().setName("ProcessInstancesFactoryImpl").setModifiers(Modifier.Keyword.PUBLIC).addExtendedType("io.automatiko.engine.addons.persistence.AbstractProcessInstancesFactory");
CompilationUnit compilationUnit = new CompilationUnit("io.automatiko.engine.addons.persistence.impl");
compilationUnit.getTypes().add(persistenceProviderClazz);
persistenceProviderClazz.addConstructor(Keyword.PUBLIC);
ConstructorDeclaration constructor = persistenceProviderClazz.addConstructor(Keyword.PUBLIC).addParameter("com.datastax.oss.driver.api.core.CqlSession", "cqlSession").addParameter("io.automatiko.engine.api.config.CassandraPersistenceConfig", "config");
BlockStmt body = new BlockStmt();
ExplicitConstructorInvocationStmt superExp = new ExplicitConstructorInvocationStmt(false, null, NodeList.nodeList(new NameExpr("cqlSession"), new NameExpr("config")));
body.addStatement(superExp);
constructor.setBody(body);
if (useInjection()) {
annotator.withApplicationComponent(persistenceProviderClazz);
annotator.withInjection(constructor);
addCodecComponents(persistenceProviderClazz);
}
String packageName = compilationUnit.getPackageDeclaration().map(pd -> pd.getName().toString()).orElse("");
String clazzName = packageName + "." + persistenceProviderClazz.findFirst(ClassOrInterfaceDeclaration.class).map(c -> c.getName().toString()).get();
generatedFiles.add(new GeneratedFile(GeneratedFile.Type.CLASS, clazzName.replace('.', '/') + ".java", compilationUnit.toString().getBytes(StandardCharsets.UTF_8)));
persistenceProviderClazz.getMembers().sort(new BodyDeclarationComparator());
}
use of io.automatiko.engine.codegen.BodyDeclarationComparator in project automatiko-engine by automatiko-io.
the class PersistenceGenerator method dynamoDBBasedPersistence.
protected void dynamoDBBasedPersistence(List<GeneratedFile> generatedFiles) {
ClassOrInterfaceDeclaration persistenceProviderClazz = new ClassOrInterfaceDeclaration().setName("ProcessInstancesFactoryImpl").setModifiers(Modifier.Keyword.PUBLIC).addExtendedType("io.automatiko.engine.addons.persistence.AbstractProcessInstancesFactory");
CompilationUnit compilationUnit = new CompilationUnit("io.automatiko.engine.addons.persistence.impl");
compilationUnit.getTypes().add(persistenceProviderClazz);
persistenceProviderClazz.addConstructor(Keyword.PUBLIC);
ConstructorDeclaration constructor = persistenceProviderClazz.addConstructor(Keyword.PUBLIC).addParameter("software.amazon.awssdk.services.dynamodb.DynamoDbClient", "dynamodb").addParameter("io.automatiko.engine.api.config.DynamoDBPersistenceConfig", "config");
BlockStmt body = new BlockStmt();
ExplicitConstructorInvocationStmt superExp = new ExplicitConstructorInvocationStmt(false, null, NodeList.nodeList(new NameExpr("dynamodb"), new NameExpr("config")));
body.addStatement(superExp);
constructor.setBody(body);
if (useInjection()) {
annotator.withApplicationComponent(persistenceProviderClazz);
annotator.withInjection(constructor);
addCodecComponents(persistenceProviderClazz);
}
String packageName = compilationUnit.getPackageDeclaration().map(pd -> pd.getName().toString()).orElse("");
String clazzName = packageName + "." + persistenceProviderClazz.findFirst(ClassOrInterfaceDeclaration.class).map(c -> c.getName().toString()).get();
generatedFiles.add(new GeneratedFile(GeneratedFile.Type.CLASS, clazzName.replace('.', '/') + ".java", compilationUnit.toString().getBytes(StandardCharsets.UTF_8)));
persistenceProviderClazz.getMembers().sort(new BodyDeclarationComparator());
}
use of io.automatiko.engine.codegen.BodyDeclarationComparator in project automatiko-engine by automatiko-io.
the class ProcessGenerator method classDeclaration.
public ClassOrInterfaceDeclaration classDeclaration(CompilationUnit compilationUnit) {
ClassOrInterfaceDeclaration cls = new ClassOrInterfaceDeclaration().setName(targetTypeName).setModifiers(Modifier.Keyword.PUBLIC);
if (useInjection()) {
annotator.withNamedApplicationComponent(cls, process.getId() + versionSuffix);
FieldDeclaration handlersInjectFieldDeclaration = new FieldDeclaration().addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, new SimpleName(annotator.multiInstanceInjectionType()), NodeList.nodeList(new ClassOrInterfaceType(null, WorkItemHandler.class.getCanonicalName()))), "handlers"));
cls.addMember(handlersInjectFieldDeclaration);
}
String processInstanceFQCN = ProcessInstanceGenerator.qualifiedName(packageName, typeName);
FieldDeclaration fieldDeclaration = new FieldDeclaration().addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, appCanonicalName), "app"));
ConstructorDeclaration emptyConstructorDeclaration = new ConstructorDeclaration().setName(targetTypeName).addModifier(Modifier.Keyword.PUBLIC);
ConstructorDeclaration baseConstructorDeclaration = new ConstructorDeclaration().setName(targetTypeName).addModifier(Modifier.Keyword.PUBLIC).addParameter(appCanonicalName, "app").setBody(new BlockStmt().addStatement(new MethodCallExpr(null, "super").addArgument(new MethodCallExpr(new MethodCallExpr(new NameExpr("app"), "config"), "process"))).addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "app"), new NameExpr("app"), AssignExpr.Operator.ASSIGN)));
ConstructorDeclaration constructorDeclaration;
if (useInjection()) {
constructorDeclaration = new ConstructorDeclaration().setName(targetTypeName).addModifier(Modifier.Keyword.PUBLIC).addParameter(appCanonicalName, "app").addParameter(new ClassOrInterfaceType(null, new SimpleName(annotator.multiInstanceInjectionType()), NodeList.nodeList(new ClassOrInterfaceType(null, WorkItemHandler.class.getCanonicalName()))), "handlers").addParameter(EndOfInstanceStrategy.class.getCanonicalName(), "strategy").setBody(new BlockStmt().addStatement(new MethodCallExpr(null, "super").addArgument(new MethodCallExpr(new MethodCallExpr(new NameExpr("app"), "config"), "process"))).addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "app"), new NameExpr("app"), AssignExpr.Operator.ASSIGN)).addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "handlers"), new NameExpr("handlers"), AssignExpr.Operator.ASSIGN)).addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "endOfInstanceStrategy"), new NameExpr("strategy"), AssignExpr.Operator.ASSIGN)));
} else {
constructorDeclaration = new ConstructorDeclaration().setName(targetTypeName).addModifier(Modifier.Keyword.PUBLIC).addParameter(appCanonicalName, "app").addParameter(EndOfInstanceStrategy.class.getCanonicalName(), "strategy").setBody(new BlockStmt().addStatement(new MethodCallExpr(null, "super").addArgument(new MethodCallExpr(new MethodCallExpr(new NameExpr("app"), "config"), "process"))).addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "app"), new NameExpr("app"), AssignExpr.Operator.ASSIGN)).addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "endOfInstanceStrategy"), new NameExpr("strategy"), AssignExpr.Operator.ASSIGN)));
}
ProcessMetaData processMetaData = processGenerator.generate();
if (!processMetaData.getSubProcesses().isEmpty()) {
for (Entry<String, String> subProcess : processMetaData.getSubProcesses().entrySet()) {
FieldDeclaration subprocessFieldDeclaration = new FieldDeclaration();
if (processIdToMetadata.get(subProcess.getKey()) != null) {
String subProcessPackage = processIdToMetadata.get(subProcess.getKey()).getPackageName();
if (subProcessPackage != null && !subProcessPackage.isEmpty()) {
compilationUnit.addImport(subProcessPackage + "." + StringUtils.capitalize(subProcess.getKey() + "Model"));
}
}
String fieldName = "process" + subProcess.getKey();
ClassOrInterfaceType modelType = new ClassOrInterfaceType(null, new SimpleName(io.automatiko.engine.api.workflow.Process.class.getCanonicalName()), NodeList.nodeList(new ClassOrInterfaceType(null, StringUtils.capitalize(subProcess.getKey() + "Model"))));
if (useInjection()) {
subprocessFieldDeclaration.addVariable(new VariableDeclarator(modelType, fieldName));
constructorDeclaration.addParameter(annotator.withNamed(new Parameter(modelType, fieldName), subProcess.getKey()));
constructorDeclaration.getBody().addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), fieldName), new NameExpr(fieldName), AssignExpr.Operator.ASSIGN));
} else {
// app.processes().processById()
MethodCallExpr initSubProcessField = new MethodCallExpr(new MethodCallExpr(new NameExpr("app"), "processes"), "processById").addArgument(new StringLiteralExpr(subProcess.getKey()));
subprocessFieldDeclaration.addVariable(new VariableDeclarator(modelType, fieldName));
baseConstructorDeclaration.getBody().addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), fieldName), new CastExpr(modelType, initSubProcessField), Operator.ASSIGN));
constructorDeclaration.getBody().addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), fieldName), new CastExpr(modelType, initSubProcessField), Operator.ASSIGN));
}
cls.addMember(subprocessFieldDeclaration);
subprocessFieldDeclaration.createGetter();
}
}
if (useInjection()) {
annotator.withInjection(constructorDeclaration);
} else {
emptyConstructorDeclaration.setBody(new BlockStmt().addStatement(new MethodCallExpr(null, "this").addArgument(new ObjectCreationExpr().setType(appCanonicalName))));
}
MethodDeclaration createModelMethod = new MethodDeclaration().addModifier(Keyword.PUBLIC).setName("createModel").setType(modelTypeName).setBody(new BlockStmt().addStatement(new ReturnStmt(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, modelTypeName), NodeList.nodeList()))));
cls.addExtendedType(abstractProcessType(modelTypeName)).addMember(fieldDeclaration).addMember(emptyConstructorDeclaration).addMember(baseConstructorDeclaration).addMember(constructorDeclaration).addMember(createInstanceMethod(processInstanceFQCN)).addMember(createInstanceWithBusinessKeyMethod(processInstanceFQCN)).addMember(createModelMethod).addMember(createInstanceGenericMethod(processInstanceFQCN)).addMember(createInstanceGenericWithBusinessKeyMethod(processInstanceFQCN)).addMember(createInstanceGenericWithWorkflowInstanceMethod(processInstanceFQCN)).addMember(createReadOnlyInstanceGenericWithWorkflowInstanceMethod(processInstanceFQCN)).addMember(internalConfigure(processMetaData)).addMember(internalRegisterListeners(processMetaData)).addMember(userTaskInputModels(processMetaData)).addMember(userTaskOutputModels(processMetaData)).addMember(process(processMetaData));
if (isServiceProject()) {
SvgProcessImageGenerator imageGenerator = isServerlessWorkflow() ? new SvgServerlessProcessImageGenerator(process) : new SvgBpmnProcessImageGenerator(process);
String svg = imageGenerator.generate();
if (svg != null && !svg.isEmpty()) {
MethodDeclaration processImageMethod = new MethodDeclaration().setName("image").setModifiers(Keyword.PUBLIC).setType(String.class).setBody(new BlockStmt().addStatement(new ReturnStmt(new StringLiteralExpr().setString(svg))));
cls.addMember(processImageMethod);
}
}
if (useInjection()) {
MethodDeclaration initMethod = annotator.withInitMethod(new MethodCallExpr(new ThisExpr(), "activate"));
cls.addMember(initMethod);
}
if (!processMetaData.getTriggers().isEmpty()) {
for (TriggerMetaData trigger : processMetaData.getTriggers()) {
// add message produces as field
if (trigger.getType().equals(TriggerMetaData.TriggerType.ProduceMessage)) {
String producerFieldType = packageName + "." + typeName + "MessageProducer_" + trigger.getOwnerId();
String producerFielName = "producer_" + trigger.getOwnerId();
FieldDeclaration producerFieldieldDeclaration = new FieldDeclaration().addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, producerFieldType), producerFielName));
cls.addMember(producerFieldieldDeclaration);
if (useInjection()) {
annotator.withInjection(producerFieldieldDeclaration);
} else {
AssignExpr assignExpr = new AssignExpr(new FieldAccessExpr(new ThisExpr(), producerFielName), new ObjectCreationExpr().setType(producerFieldType), AssignExpr.Operator.ASSIGN);
cls.getConstructors().forEach(c -> c.getBody().addStatement(assignExpr));
}
}
}
}
cls.getMembers().sort(new BodyDeclarationComparator());
return cls;
}
use of io.automatiko.engine.codegen.BodyDeclarationComparator in project automatiko-engine by automatiko-io.
the class ProcessInstanceGenerator method classDeclaration.
public ClassOrInterfaceDeclaration classDeclaration() {
ClassOrInterfaceDeclaration classDecl = new ClassOrInterfaceDeclaration().setName(targetTypeName).addModifier(Modifier.Keyword.PUBLIC);
classDecl.addExtendedType(new ClassOrInterfaceType(null, AbstractProcessInstance.class.getCanonicalName()).setTypeArguments(new ClassOrInterfaceType(null, model.getModelClassSimpleName()))).addMember(constructorDecl()).addMember(constructorWithBusinessKeyDecl()).addMember(constructorWithWorkflowInstanceAndRuntimeDecl()).addMember(constructorWorkflowInstanceDecl()).addMember(bind()).addMember(unbind());
if (generatorContext.getApplicationProperty("quarkus.automatiko.instance-locking").orElse("true").equalsIgnoreCase("false")) {
MethodDeclaration configureLock = new MethodDeclaration().setModifiers(Keyword.PROTECTED).setName("configureLock").addParameter(String.class.getCanonicalName(), "businessKey").setType(new VoidType());
classDecl.addMember(configureLock);
}
ProcessMetaData processMetaData = processGenerator.generate();
if (!processMetaData.getSubProcesses().isEmpty()) {
classDecl.getMembers().add(subprocessesMethod(processMetaData));
}
classDecl.getMembers().sort(new BodyDeclarationComparator());
return classDecl;
}
Aggregations