gitgabrio commented on code in PR #3850:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/3850#discussion_r1971062622


##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java:
##########
@@ -582,4 +595,50 @@ public Optional<ApplicationSection> section() {
     public int priority() {
         return 10;
     }
+
+    private void processProducerTemplate(Optional<Expression> statement) {
+        TemplatedGenerator generator = TemplatedGenerator.builder()
+                .withTemplateBasePath("/class-templates/producer/")
+                .withFallbackContext(JavaKogitoBuildContext.CONTEXT_NAME)
+                .withTargetTypeName(BUSINESS_CALENDAR_PRODUCER_TEMPLATE)
+                .build(context(), BUSINESS_CALENDAR_PRODUCER_TEMPLATE);
+        CompilationUnit compilationUnit = generator.compilationUnitOrThrow();
+
+        statement.ifPresent(expression -> 
compilationUnit.findFirst(ClassOrInterfaceDeclaration.class)
+                .flatMap(NodeWithMembers::getDefaultConstructor)
+                .ifPresent(constructor -> 
constructor.getBody().getStatements().stream()
+                        .filter(Statement::isExpressionStmt)
+                        .flatMap(stmt -> stmt.getChildNodes().stream())
+                        .filter(AssignExpr.class::isInstance)
+                        .map(AssignExpr.class::cast)
+                        .filter(assignExpr -> assignExpr.getTarget() 
instanceof FieldAccessExpr field &&
+                                
BUSINESS_CALENDAR_FIELD_NAME.equals(field.getNameAsString()))
+                        .forEach(assignExpr -> 
assignExpr.setValue(expression))));
+
+        storeFile(PRODUCER_TYPE, generator.generatedFilePath(), 
compilationUnit.toString());
+    }
+
+    private Optional<Expression> 
deriveBusinessCalendarDeclarationStatement(String businessCalendarClassName) {
+        Class<?> businessCalendarClass;
+        boolean validBusinessCalendar = false;
+        ObjectCreationExpr declarationStmt = null;
+        if (Objects.isNull(businessCalendarClassName)) {
+            return Optional.empty();
+        }
+        try {
+            businessCalendarClass = Class.forName(businessCalendarClassName, 
true, context().getClassLoader());
+        } catch (ClassNotFoundException e) {

Review Comment:
   As explained time ago, we can't simply swallow up errors and exceptions: in 
that case, there is some configuration error of which the user must be aware, 
somehow: the application build should fail here with a clear message



##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java:
##########
@@ -582,4 +595,50 @@ public Optional<ApplicationSection> section() {
     public int priority() {
         return 10;
     }
+
+    private void processProducerTemplate(Optional<Expression> statement) {
+        TemplatedGenerator generator = TemplatedGenerator.builder()
+                .withTemplateBasePath("/class-templates/producer/")
+                .withFallbackContext(JavaKogitoBuildContext.CONTEXT_NAME)
+                .withTargetTypeName(BUSINESS_CALENDAR_PRODUCER_TEMPLATE)
+                .build(context(), BUSINESS_CALENDAR_PRODUCER_TEMPLATE);
+        CompilationUnit compilationUnit = generator.compilationUnitOrThrow();
+
+        statement.ifPresent(expression -> 
compilationUnit.findFirst(ClassOrInterfaceDeclaration.class)
+                .flatMap(NodeWithMembers::getDefaultConstructor)
+                .ifPresent(constructor -> 
constructor.getBody().getStatements().stream()
+                        .filter(Statement::isExpressionStmt)
+                        .flatMap(stmt -> stmt.getChildNodes().stream())
+                        .filter(AssignExpr.class::isInstance)
+                        .map(AssignExpr.class::cast)
+                        .filter(assignExpr -> assignExpr.getTarget() 
instanceof FieldAccessExpr field &&
+                                
BUSINESS_CALENDAR_FIELD_NAME.equals(field.getNameAsString()))
+                        .forEach(assignExpr -> 
assignExpr.setValue(expression))));
+
+        storeFile(PRODUCER_TYPE, generator.generatedFilePath(), 
compilationUnit.toString());
+    }
+
+    private Optional<Expression> 
deriveBusinessCalendarDeclarationStatement(String businessCalendarClassName) {
+        Class<?> businessCalendarClass;
+        boolean validBusinessCalendar = false;
+        ObjectCreationExpr declarationStmt = null;
+        if (Objects.isNull(businessCalendarClassName)) {
+            return Optional.empty();
+        }
+        try {
+            businessCalendarClass = Class.forName(businessCalendarClassName, 
true, context().getClassLoader());

Review Comment:
   Please replace with:
   
   `businessCalendarClass = 
context().getClassLoader().loadClass(businessCalendarClassName).asSubclass(DMNProfile.class);`
   (or equivalent) and deal with all possible exceptions in one single place



##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java:
##########
@@ -459,10 +471,11 @@ protected Collection<GeneratedFile> internalGenerate() {
         staticDependencyInjectionProducerGenerator.generate()
                 .entrySet()
                 .forEach(entry -> storeFile(PRODUCER_TYPE, entry.getKey(), 
entry.getValue()));
+
         Boolean isBusinessCalendarPresent = 
context().getContextAttribute(IS_BUSINESS_CALENDAR_PRESENT, Boolean.class);

Review Comment:
   Please replace with ternary operator:
   
   `Boolean isBusinessCalendarPresent = 
context().getContextAttribute(IS_BUSINESS_CALENDAR_PRESENT, Boolean.class) != 
null ?context().getContextAttribute(IS_BUSINESS_CALENDAR_PRESENT, 
Boolean.class) : false;` (or equivalent) to avoid the Object.nonNull check below
   



##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java:
##########
@@ -582,4 +595,50 @@ public Optional<ApplicationSection> section() {
     public int priority() {
         return 10;
     }
+
+    private void processProducerTemplate(Optional<Expression> statement) {
+        TemplatedGenerator generator = TemplatedGenerator.builder()
+                .withTemplateBasePath("/class-templates/producer/")
+                .withFallbackContext(JavaKogitoBuildContext.CONTEXT_NAME)
+                .withTargetTypeName(BUSINESS_CALENDAR_PRODUCER_TEMPLATE)
+                .build(context(), BUSINESS_CALENDAR_PRODUCER_TEMPLATE);
+        CompilationUnit compilationUnit = generator.compilationUnitOrThrow();
+
+        statement.ifPresent(expression -> 
compilationUnit.findFirst(ClassOrInterfaceDeclaration.class)
+                .flatMap(NodeWithMembers::getDefaultConstructor)
+                .ifPresent(constructor -> 
constructor.getBody().getStatements().stream()
+                        .filter(Statement::isExpressionStmt)
+                        .flatMap(stmt -> stmt.getChildNodes().stream())
+                        .filter(AssignExpr.class::isInstance)
+                        .map(AssignExpr.class::cast)
+                        .filter(assignExpr -> assignExpr.getTarget() 
instanceof FieldAccessExpr field &&
+                                
BUSINESS_CALENDAR_FIELD_NAME.equals(field.getNameAsString()))
+                        .forEach(assignExpr -> 
assignExpr.setValue(expression))));
+
+        storeFile(PRODUCER_TYPE, generator.generatedFilePath(), 
compilationUnit.toString());
+    }
+
+    private Optional<Expression> 
deriveBusinessCalendarDeclarationStatement(String businessCalendarClassName) {
+        Class<?> businessCalendarClass;
+        boolean validBusinessCalendar = false;
+        ObjectCreationExpr declarationStmt = null;
+        if (Objects.isNull(businessCalendarClassName)) {
+            return Optional.empty();
+        }
+        try {
+            businessCalendarClass = Class.forName(businessCalendarClassName, 
true, context().getClassLoader());
+        } catch (ClassNotFoundException e) {
+            LOGGER.error("Custom Business Calendar class {} not 
found.\nGenerating default business calendar", businessCalendarClassName);
+            return Optional.empty();
+        }
+        if (!businessCalendarClass.isInterface()) {
+            validBusinessCalendar = 
BusinessCalendar.class.isAssignableFrom(businessCalendarClass);
+        }
+        if (!validBusinessCalendar) {
+            LOGGER.error("Custom Business Calendar class {} does not implement 
BusinessCalendar interface.\nGenerating default business calendar", 
businessCalendarClassName);
+        } else {
+            declarationStmt = new ObjectCreationExpr(null, 
StaticJavaParser.parseClassOrInterfaceType(businessCalendarClassName), 
NodeList.nodeList());
+        }
+        return Optional.ofNullable(declarationStmt);

Review Comment:
   Please read carefully the logic you implemented.
   This `deriveBusinessCalendarDeclarationStatement(String)` method is invoked 
only when `businessCalendarClassName` is different then `null`, so it should 
never return an empty object: in case, it should throw an exception and fail 
the build, as explained in previous comment



##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java:
##########
@@ -459,10 +471,11 @@ protected Collection<GeneratedFile> internalGenerate() {
         staticDependencyInjectionProducerGenerator.generate()
                 .entrySet()
                 .forEach(entry -> storeFile(PRODUCER_TYPE, entry.getKey(), 
entry.getValue()));
+
         Boolean isBusinessCalendarPresent = 
context().getContextAttribute(IS_BUSINESS_CALENDAR_PRESENT, Boolean.class);
+        String customBusinessCalendarClassName = CodegenUtil.getProperty(this, 
context(), CUSTOM_BUSINESS_CALENDAR_PROPERTY, String::valueOf, null);
         if (Objects.nonNull(isBusinessCalendarPresent) && 
isBusinessCalendarPresent) {

Review Comment:
   See comment above to ensure `isBusinessCalendarPresent` is always populated



##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java:
##########
@@ -582,4 +595,50 @@ public Optional<ApplicationSection> section() {
     public int priority() {
         return 10;
     }
+
+    private void processProducerTemplate(Optional<Expression> statement) {
+        TemplatedGenerator generator = TemplatedGenerator.builder()
+                .withTemplateBasePath("/class-templates/producer/")
+                .withFallbackContext(JavaKogitoBuildContext.CONTEXT_NAME)
+                .withTargetTypeName(BUSINESS_CALENDAR_PRODUCER_TEMPLATE)
+                .build(context(), BUSINESS_CALENDAR_PRODUCER_TEMPLATE);
+        CompilationUnit compilationUnit = generator.compilationUnitOrThrow();
+
+        statement.ifPresent(expression -> 
compilationUnit.findFirst(ClassOrInterfaceDeclaration.class)
+                .flatMap(NodeWithMembers::getDefaultConstructor)
+                .ifPresent(constructor -> 
constructor.getBody().getStatements().stream()
+                        .filter(Statement::isExpressionStmt)
+                        .flatMap(stmt -> stmt.getChildNodes().stream())
+                        .filter(AssignExpr.class::isInstance)
+                        .map(AssignExpr.class::cast)
+                        .filter(assignExpr -> assignExpr.getTarget() 
instanceof FieldAccessExpr field &&
+                                
BUSINESS_CALENDAR_FIELD_NAME.equals(field.getNameAsString()))
+                        .forEach(assignExpr -> 
assignExpr.setValue(expression))));

Review Comment:
   Here you should verify there is one and only one assign expression, and use 
it; otherwise throws an exception



##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/StaticDependencyInjectionProducerGenerator.java:
##########
@@ -58,12 +60,23 @@ public Map<String, String> generate() {
      * @return Map with the generated resources
      */
     public Map<String, String> generate(List<String> templates) {
+        return generate(templates, null);
+    }
+
+    public Map<String, String> generate(List<String> templates, String 
customClass) {
         if (!context.hasDI()) {
             return Collections.emptyMap();
         }
         return templates.stream().map(this::buildProducerTemplatedGenerator)
                 
.collect(Collectors.toMap(TemplatedGenerator::generatedFilePath,
-                        generator -> 
generator.compilationUnitOrThrow().toString()));
+                        generator -> {
+                            CompilationUnit compilationUnit = 
generator.compilationUnitOrThrow();
+                            if (customClass != null) {
+                                //compilationUnit.replace(new 
SimpleName("$customBusinessCalendar$"), new SimpleName(customClass));
+                                return 
compilationUnit.toString().replace("$customBusinessCalendar$", customClass);

Review Comment:
   What is the need of this ?



##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java:
##########
@@ -582,4 +595,50 @@ public Optional<ApplicationSection> section() {
     public int priority() {
         return 10;
     }
+
+    private void processProducerTemplate(Optional<Expression> statement) {
+        TemplatedGenerator generator = TemplatedGenerator.builder()
+                .withTemplateBasePath("/class-templates/producer/")
+                .withFallbackContext(JavaKogitoBuildContext.CONTEXT_NAME)
+                .withTargetTypeName(BUSINESS_CALENDAR_PRODUCER_TEMPLATE)
+                .build(context(), BUSINESS_CALENDAR_PRODUCER_TEMPLATE);
+        CompilationUnit compilationUnit = generator.compilationUnitOrThrow();
+
+        statement.ifPresent(expression -> 
compilationUnit.findFirst(ClassOrInterfaceDeclaration.class)
+                .flatMap(NodeWithMembers::getDefaultConstructor)
+                .ifPresent(constructor -> 
constructor.getBody().getStatements().stream()
+                        .filter(Statement::isExpressionStmt)
+                        .flatMap(stmt -> stmt.getChildNodes().stream())
+                        .filter(AssignExpr.class::isInstance)
+                        .map(AssignExpr.class::cast)
+                        .filter(assignExpr -> assignExpr.getTarget() 
instanceof FieldAccessExpr field &&
+                                
BUSINESS_CALENDAR_FIELD_NAME.equals(field.getNameAsString()))
+                        .forEach(assignExpr -> 
assignExpr.setValue(expression))));
+
+        storeFile(PRODUCER_TYPE, generator.generatedFilePath(), 
compilationUnit.toString());
+    }
+
+    private Optional<Expression> 
deriveBusinessCalendarDeclarationStatement(String businessCalendarClassName) {
+        Class<?> businessCalendarClass;
+        boolean validBusinessCalendar = false;
+        ObjectCreationExpr declarationStmt = null;
+        if (Objects.isNull(businessCalendarClassName)) {
+            return Optional.empty();
+        }
+        try {
+            businessCalendarClass = Class.forName(businessCalendarClassName, 
true, context().getClassLoader());
+        } catch (ClassNotFoundException e) {
+            LOGGER.error("Custom Business Calendar class {} not 
found.\nGenerating default business calendar", businessCalendarClassName);
+            return Optional.empty();
+        }
+        if (!businessCalendarClass.isInterface()) {
+            validBusinessCalendar = 
BusinessCalendar.class.isAssignableFrom(businessCalendarClass);

Review Comment:
   Please remove, as it will be managed by the single line in previous comment



##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java:
##########
@@ -582,4 +595,50 @@ public Optional<ApplicationSection> section() {
     public int priority() {
         return 10;
     }
+
+    private void processProducerTemplate(Optional<Expression> statement) {
+        TemplatedGenerator generator = TemplatedGenerator.builder()
+                .withTemplateBasePath("/class-templates/producer/")
+                .withFallbackContext(JavaKogitoBuildContext.CONTEXT_NAME)
+                .withTargetTypeName(BUSINESS_CALENDAR_PRODUCER_TEMPLATE)
+                .build(context(), BUSINESS_CALENDAR_PRODUCER_TEMPLATE);
+        CompilationUnit compilationUnit = generator.compilationUnitOrThrow();
+
+        statement.ifPresent(expression -> 
compilationUnit.findFirst(ClassOrInterfaceDeclaration.class)
+                .flatMap(NodeWithMembers::getDefaultConstructor)
+                .ifPresent(constructor -> 
constructor.getBody().getStatements().stream()
+                        .filter(Statement::isExpressionStmt)
+                        .flatMap(stmt -> stmt.getChildNodes().stream())
+                        .filter(AssignExpr.class::isInstance)
+                        .map(AssignExpr.class::cast)
+                        .filter(assignExpr -> assignExpr.getTarget() 
instanceof FieldAccessExpr field &&
+                                
BUSINESS_CALENDAR_FIELD_NAME.equals(field.getNameAsString()))
+                        .forEach(assignExpr -> 
assignExpr.setValue(expression))));
+
+        storeFile(PRODUCER_TYPE, generator.generatedFilePath(), 
compilationUnit.toString());
+    }
+
+    private Optional<Expression> 
deriveBusinessCalendarDeclarationStatement(String businessCalendarClassName) {
+        Class<?> businessCalendarClass;

Review Comment:
   Please provider a better typing here:
   `Class<? extends BusinessCalendar> businessCalendarClass;` (double check 
syntax)



##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java:
##########
@@ -582,4 +595,50 @@ public Optional<ApplicationSection> section() {
     public int priority() {
         return 10;
     }
+
+    private void processProducerTemplate(Optional<Expression> statement) {
+        TemplatedGenerator generator = TemplatedGenerator.builder()
+                .withTemplateBasePath("/class-templates/producer/")
+                .withFallbackContext(JavaKogitoBuildContext.CONTEXT_NAME)
+                .withTargetTypeName(BUSINESS_CALENDAR_PRODUCER_TEMPLATE)
+                .build(context(), BUSINESS_CALENDAR_PRODUCER_TEMPLATE);
+        CompilationUnit compilationUnit = generator.compilationUnitOrThrow();
+
+        statement.ifPresent(expression -> 
compilationUnit.findFirst(ClassOrInterfaceDeclaration.class)
+                .flatMap(NodeWithMembers::getDefaultConstructor)
+                .ifPresent(constructor -> 
constructor.getBody().getStatements().stream()

Review Comment:
   Here the code should throw an exception, because the DefaultConstructor must 
be present in the template



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to