Copilot commented on code in PR #4105:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/4105#discussion_r2508490580


##########
jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/lifecycle/DefaultUserTaskLifeCycles.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.kie.kogito.usertask.impl.lifecycle;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.kie.kogito.usertask.lifecycle.UserTaskLifeCycle;
+import org.kie.kogito.usertask.lifecycle.UserTaskLifeCycleException;
+import org.kie.kogito.usertask.lifecycle.UserTaskLifeCycles;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DefaultUserTaskLifeCycles implements UserTaskLifeCycles {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(DefaultUserTaskLifeCycles.class);
+
+    private final Map<String, UserTaskLifeCycle> userTaskLifeCycleRegistry = 
new HashMap<>();
+    private String defaultUserTaskLifeCycleId;
+
+    public DefaultUserTaskLifeCycles() {
+        this.defaultUserTaskLifeCycleId = "kogito";
+        registerUserTaskLifeCycles();
+        LOG.info("Registered UserTaskLifeCycles {} with default lifecycle: 
{}", userTaskLifeCycleRegistry, this.defaultUserTaskLifeCycleId);
+    }
+
+    public DefaultUserTaskLifeCycles(String defaultUserTaskLifeCycleId, 
Iterable<UserTaskLifeCycle> userTaskLifeCycle) {
+        this.defaultUserTaskLifeCycleId = defaultUserTaskLifeCycleId;
+        registerCustomUserTaskLifeCycleIfAny(userTaskLifeCycle);
+        registerUserTaskLifeCycles();
+        LOG.info("Registered UserTaskLifeCycles {} with default lifecycle: 
{}", userTaskLifeCycleRegistry, this.defaultUserTaskLifeCycleId);
+    }
+
+    private void registerUserTaskLifeCycles() {
+        registerUserTaskLifeCycle("kogito", new DefaultUserTaskLifeCycle());
+        registerUserTaskLifeCycle("ws-human-task", new WsHumanTaskLifeCycle());
+    }
+
+    private void 
registerCustomUserTaskLifeCycleIfAny(Iterable<UserTaskLifeCycle> 
userTaskLifeCycle) {
+        var iterator = userTaskLifeCycle.iterator();
+        if (iterator.hasNext()) {
+            defaultUserTaskLifeCycleId = "custom";
+            registerUserTaskLifeCycle("custom", iterator.next());
+
+            if (iterator.hasNext()) {
+                var message = "Multiple custom usertask lifecycle 
implementations found";
+                LOG.error(message);
+                throw new UserTaskLifeCycleException(message);
+            }
+        }
+    }

Review Comment:
   When a custom lifecycle is registered, it unconditionally overwrites the 
`defaultUserTaskLifeCycleId` even if the user explicitly configured 'kogito' or 
'ws-human-task' via the constructor parameter. This could lead to unexpected 
behavior where the configured default is ignored. Consider only setting 
defaultUserTaskLifeCycleId to 'custom' if no explicit default was provided, or 
throw an exception if there's a conflict between the configured default and a 
custom lifecycle.



##########
jbpm/jbpm-usertask-workitem/src/main/java/org/kie/kogito/jbpm/usertask/handler/UserTaskKogitoWorkItemHandler.java:
##########
@@ -80,6 +82,7 @@ public Optional<WorkItemTransition> 
activateWorkItemHandler(KogitoWorkItemManage
         DefaultUserTaskInstance instance = (DefaultUserTaskInstance) 
userTask.createInstance();
 
         instance.setExternalReferenceId(workItem.getStringId());
+        instance.getMetadata().put("Lifecycle", 
handler.getApplication().config().get(UserTaskConfig.class).userTaskLifeCycles().getDefaultUserTaskLifeCycleId());

Review Comment:
   The metadata key 'Lifecycle' is a magic string used across multiple files. 
Consider defining it as a constant (e.g., `public static final String 
METADATA_LIFECYCLE = \"Lifecycle\";`) in a shared location to prevent typos and 
improve maintainability.



##########
jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/DefaultUserTaskInstance.java:
##########
@@ -262,7 +262,7 @@ public void initialize(Map<String, Object> data, 
IdentityProvider identity) {
 
     @Override
     public void transition(String transitionId, Map<String, Object> data, 
IdentityProvider identity) {
-        Optional<UserTaskTransitionToken> next = 
Optional.of(this.userTaskLifeCycle.newTransitionToken(transitionId, this, 
data));
+        Optional<UserTaskTransitionToken> next = 
Optional.ofNullable(this.userTaskLifeCycle.newTransitionToken(transitionId, 
this, data));

Review Comment:
   The change from `Optional.of()` to `Optional.ofNullable()` allows null 
tokens to silently skip transitions. If `newTransitionToken` returns null when 
it shouldn't (e.g., due to a bug), the transition will be silently ignored 
instead of failing with a clear error. Consider adding a comment explaining 
when null is expected, or validate that null is only returned in specific valid 
scenarios.
   ```suggestion
           // If newTransitionToken returns null, this usually indicates an 
invalid transition or a bug.
           // If null is expected for certain transitions, document those cases 
here.
           UserTaskTransitionToken nextToken = 
this.userTaskLifeCycle.newTransitionToken(transitionId, this, data);
           if (nextToken == null) {
               throw new IllegalStateException("Transition token is null for 
transitionId: " + transitionId + ". This may indicate an invalid transition or 
a bug.");
           }
           Optional<UserTaskTransitionToken> next = Optional.of(nextToken);
   ```



##########
jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/lifecycle/DefaultUserTaskLifeCycles.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.kie.kogito.usertask.impl.lifecycle;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.kie.kogito.usertask.lifecycle.UserTaskLifeCycle;
+import org.kie.kogito.usertask.lifecycle.UserTaskLifeCycleException;
+import org.kie.kogito.usertask.lifecycle.UserTaskLifeCycles;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DefaultUserTaskLifeCycles implements UserTaskLifeCycles {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(DefaultUserTaskLifeCycles.class);
+
+    private final Map<String, UserTaskLifeCycle> userTaskLifeCycleRegistry = 
new HashMap<>();
+    private String defaultUserTaskLifeCycleId;
+
+    public DefaultUserTaskLifeCycles() {
+        this.defaultUserTaskLifeCycleId = "kogito";
+        registerUserTaskLifeCycles();
+        LOG.info("Registered UserTaskLifeCycles {} with default lifecycle: 
{}", userTaskLifeCycleRegistry, this.defaultUserTaskLifeCycleId);
+    }
+
+    public DefaultUserTaskLifeCycles(String defaultUserTaskLifeCycleId, 
Iterable<UserTaskLifeCycle> userTaskLifeCycle) {
+        this.defaultUserTaskLifeCycleId = defaultUserTaskLifeCycleId;
+        registerCustomUserTaskLifeCycleIfAny(userTaskLifeCycle);
+        registerUserTaskLifeCycles();
+        LOG.info("Registered UserTaskLifeCycles {} with default lifecycle: 
{}", userTaskLifeCycleRegistry, this.defaultUserTaskLifeCycleId);
+    }
+
+    private void registerUserTaskLifeCycles() {
+        registerUserTaskLifeCycle("kogito", new DefaultUserTaskLifeCycle());
+        registerUserTaskLifeCycle("ws-human-task", new WsHumanTaskLifeCycle());
+    }
+
+    private void 
registerCustomUserTaskLifeCycleIfAny(Iterable<UserTaskLifeCycle> 
userTaskLifeCycle) {
+        var iterator = userTaskLifeCycle.iterator();
+        if (iterator.hasNext()) {
+            defaultUserTaskLifeCycleId = "custom";
+            registerUserTaskLifeCycle("custom", iterator.next());
+
+            if (iterator.hasNext()) {
+                var message = "Multiple custom usertask lifecycle 
implementations found";
+                LOG.error(message);
+                throw new UserTaskLifeCycleException(message);
+            }
+        }
+    }
+
+    private void registerUserTaskLifeCycle(String userTaskLifeCycleId, 
UserTaskLifeCycle userTaskLifeCycle) {
+        userTaskLifeCycleRegistry.put(userTaskLifeCycleId, userTaskLifeCycle);
+    }
+
+    @Override
+    public String getDefaultUserTaskLifeCycleId() {
+        return defaultUserTaskLifeCycleId;
+    }
+
+    @Override
+    public UserTaskLifeCycle getUserTaskLifeCycleById(String 
userTaskLifeCycleId) {
+        return userTaskLifeCycleRegistry.getOrDefault(userTaskLifeCycleId,
+                defaultUserTaskLifeCycleId.equals("custom") ? 
userTaskLifeCycleRegistry.get("custom") : 
userTaskLifeCycleRegistry.get("kogito"));

Review Comment:
   The fallback logic always returns the 'custom' or 'kogito' lifecycle even 
when a null userTaskLifeCycleId is passed. This could mask configuration 
errors. Consider adding validation to throw an exception when 
userTaskLifeCycleId is null or returning the default lifecycle explicitly using 
`getUserTaskLifeCycleById(defaultUserTaskLifeCycleId)` for clarity.
   ```suggestion
           if (userTaskLifeCycleId == null) {
               throw new UserTaskLifeCycleException("UserTaskLifeCycleId must 
not be null");
           }
           UserTaskLifeCycle lifeCycle = 
userTaskLifeCycleRegistry.get(userTaskLifeCycleId);
           if (lifeCycle == null) {
               // Fallback explicitly to the default lifecycle
               lifeCycle = 
userTaskLifeCycleRegistry.get(defaultUserTaskLifeCycleId);
           }
           return lifeCycle;
   ```



##########
kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskCodegen.java:
##########
@@ -168,6 +177,30 @@ protected Collection<GeneratedFile> internalGenerate() {
         return generatedFiles;
     }
 
+    private GeneratedFile generateLifeCycles() {
+        String packageName = context().getPackageName();
+
+        CompilationUnit compilationUnit = 
lifeCyclesGenerator.compilationUnitOrThrow("No lifecycle template found for 
user tasks");
+        compilationUnit.setPackageDeclaration(packageName);
+
+        var template = 
compilationUnit.findFirst(ClassOrInterfaceDeclaration.class)
+                .orElseThrow(() -> new 
ProcessCodegenException("UserTaskLifeCyclesTemplate doesn't contain a class or 
interface declaration!"));
+
+        String userTaskLifeCycleId = 
context().getApplicationProperty("kogito.usertasks.lifecycle").orElse("kogito");
+        if (!USERTASK_LIFECYCLES.contains(userTaskLifeCycleId)) {
+            throw new ProcessCodegenException("Illegal usertask lifecycle");

Review Comment:
   The error message 'Illegal usertask lifecycle' is not helpful for debugging. 
It should specify which lifecycle value was invalid and list the valid options. 
Consider: `String.format(\"Invalid user task lifecycle '%s'. Valid values are: 
%s\", userTaskLifeCycleId, USERTASK_LIFECYCLES)`
   ```suggestion
               throw new ProcessCodegenException(
                   String.format("Invalid user task lifecycle '%s'. Valid 
values are: %s", userTaskLifeCycleId, USERTASK_LIFECYCLES)
               );
   ```



-- 
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