You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kie.apache.org by "gitgabrio (via GitHub)" <gi...@apache.org> on 2025/02/25 15:01:31 UTC

Re: [PR] [Incubator-kie-issues-1790] Enhancing business calendar flexibility [incubator-kie-kogito-runtimes]

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


##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java:
##########
@@ -582,4 +591,44 @@ public Optional<ApplicationSection> section() {
     public int priority() {
         return 10;
     }
+
+    private void processProducerTemplate(String 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();
+        if (Objects.nonNull(statement)) {

Review Comment:
   Please replace null check with Optional.ifPresent one



##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java:
##########
@@ -582,4 +591,44 @@ public Optional<ApplicationSection> section() {
     public int priority() {
         return 10;
     }
+
+    private void processProducerTemplate(String 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();
+        if (Objects.nonNull(statement)) {
+            String constructorBody = String.format("{ this.businessCalendar =  %s; }", statement);
+            NodeList<com.github.javaparser.ast.stmt.Statement> statements = StaticJavaParser.parseBlock(constructorBody).getStatements();
+            compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).flatMap(NodeWithMembers::getDefaultConstructor)
+                    .ifPresent(a -> a.setBody(new BlockStmt(statements)));

Review Comment:
   please replace the set body, and all this string management, with idiomatic JavaPArser API, ie
   1. retrieve the variable assignment corresponding to `this.businessCalendar =`
   2. set its value to the expression returned from `deriveBusinessCalendarDeclarationStatement`



##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java:
##########
@@ -582,4 +591,44 @@ public Optional<ApplicationSection> section() {
     public int priority() {
         return 10;
     }
+
+    private void processProducerTemplate(String 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();
+        if (Objects.nonNull(statement)) {
+            String constructorBody = String.format("{ this.businessCalendar =  %s; }", statement);
+            NodeList<com.github.javaparser.ast.stmt.Statement> statements = StaticJavaParser.parseBlock(constructorBody).getStatements();
+            compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).flatMap(NodeWithMembers::getDefaultConstructor)
+                    .ifPresent(a -> a.setBody(new BlockStmt(statements)));
+        }
+        storeFile(PRODUCER_TYPE, generator.generatedFilePath(), compilationUnit.toString());
+    }
+
+    private String deriveBusinessCalendarDeclarationStatement(String businessCalendarClassName) {
+        Class<?> businessCalendarClass;
+        boolean validBusinessCalendar = false;
+        String declarationStmt = null;
+        if (Objects.isNull(businessCalendarClassName)) {
+            return declarationStmt;
+        }
+        try {
+            businessCalendarClass = Class.forName(businessCalendarClassName, true, Thread.currentThread().getContextClassLoader());
+        } catch (ClassNotFoundException e) {
+            LOGGER.error("Custom Business Calendar class {} not found.\nGenerating default business calendar", businessCalendarClassName);
+            return declarationStmt;
+        }
+        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 " + businessCalendarClassName + "()";

Review Comment:
   Please return a proper JapaParser expression here, e.g. 
   `new ObjectCreationExpr(null, variableType, arguments);`
   it would also provide type-checlk out of the box



-- 
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org