jstastny-cz commented on code in PR #4273:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/4273#discussion_r3371569196


##########
addons/common/jbpm-usertask-storage-jpa/src/main/java/org/jbpm/usertask/jpa/repository/UserTaskInstanceRepository.java:
##########
@@ -36,9 +42,61 @@ public UserTaskInstanceRepository(UserTaskJPAContext 
context) {
     }
 
     public List<UserTaskInstanceEntity> findByIdentity(IdentityProvider 
identityProvider) {
-        TypedQuery<UserTaskInstanceEntity> query = 
getEntityManager().createNamedQuery(GET_INSTANCES_BY_IDENTITY, 
UserTaskInstanceEntity.class);
-        query.setParameter("userId", identityProvider.getName());
-        query.setParameter("roles", identityProvider.getRoles());
+        return findByIdentity(identityProvider, null);
+    }
+
+    public List<UserTaskInstanceEntity> findByIdentity(IdentityProvider 
identityProvider, UserTaskFilter filter) {
+        String userId = identityProvider.getName();
+        Collection<String> roles = identityProvider.getRoles();
+
+        // Build query dynamically - start with base query
+        String jpql = BASE_IDENTITY_QUERY;
+
+        boolean hasTaskNameFilter = filter != null
+                && filter.taskName() != null
+                && !filter.taskName().isEmpty();
+
+        boolean hasStatusFilter = filter != null
+                && filter.statuses() != null
+                && !filter.statuses().isEmpty();
+
+        // Conditionally append filter clauses
+        if (hasTaskNameFilter) {
+            jpql = jpql.concat(TASKNAME_FILTER_CLAUSE);
+        }
+
+        if (hasStatusFilter) {
+            jpql = jpql.concat(STATUS_FILTER_CLAUSE);
+        }
+
+        // Create typed query with the final JPQL
+        TypedQuery<UserTaskInstanceEntity> query = getEntityManager()
+                .createQuery(jpql, UserTaskInstanceEntity.class)
+                .setParameter("userId", userId)
+                .setParameter("roles", roles);
+
+        if (filter != null) {
+            query.setParameter("processId", filter.processId());
+            query.setParameter("processInstanceId", 
filter.processInstanceId());
+
+            // Only set taskName if we added it to the query
+            if (hasTaskNameFilter) {
+                query.setParameter("taskName", filter.taskName());
+            }
+
+            // Only set statusFilter if we added it to the query
+            if (hasStatusFilter) {
+                List<String> statusFilter = filter.statuses().stream()
+                        .map(UserTaskState::getName)

Review Comment:
   entity.setStatus(userTaskInstance.getStatus().getName());
   
   Check the mapper - we have full control over the format of the state value - 
it's being persisted via getName() of the enum. We should drop any lower-case 
transformation, especially from the SQL query, and just make sure we pass the 
correctly formatted values.



##########
addons/common/jbpm-usertask-storage-jpa/src/main/java/org/jbpm/usertask/jpa/model/UserTaskInstanceEntity.java:
##########
@@ -29,25 +29,33 @@
 import jakarta.persistence.*;
 
 @Entity
-@NamedQuery(name = UserTaskInstanceEntity.GET_INSTANCES_BY_IDENTITY,
-        query = "select userTask from UserTaskInstanceEntity userTask " +
-                "left join userTask.adminGroups adminGroup " +
-                "left join userTask.potentialGroups potentialGroup " +
-                "where :userId member of userTask.adminUsers " +
-                "or adminGroup in (:roles) " +
-                "or userTask.actualOwner = :userId " +
-                "or (userTask.actualOwner is null " + // checking if task is 
not reserved, we cannot check by status since lifecycle can be customized
-                "and :userId not member of userTask.excludedUsers " +
-                "and (:userId member of userTask.potentialUsers or 
potentialGroup in (:roles)" +
-                "))")
 @NamedNativeQuery(
         name = UserTaskInstanceEntity.DELETE_BY_ID,
         query = "delete from jbpm_user_tasks where id = :taskId")
 @Table(name = "jbpm_user_tasks")
 public class UserTaskInstanceEntity {
-    public static final String GET_INSTANCES_BY_IDENTITY = 
"UserTaskInstanceEntity.GetInstanceByIdentity";
     public static final String DELETE_BY_ID = 
"UserTaskInstanceEntity.DeleteById";
 
+    // Base query for finding instances by identity (without status filter)
+    public static final String BASE_IDENTITY_QUERY =
+            "select userTask from UserTaskInstanceEntity userTask " +
+                    "left join userTask.adminGroups adminGroup " +
+                    "left join userTask.potentialGroups potentialGroup " +
+                    "where (:userId member of userTask.adminUsers " +
+                    "or adminGroup in (:roles) " +
+                    "or userTask.actualOwner = :userId " +
+                    "or (userTask.actualOwner is null " + // checking if task 
is not reserved, we cannot check by status since lifecycle can be customized
+                    "and :userId not member of userTask.excludedUsers " +
+                    "and (:userId member of userTask.potentialUsers or 
potentialGroup in (:roles)))) " +
+                    "and (cast(:processId as string) is null or 
userTask.processInfo.processId = :processId) " +

Review Comment:
   Do we need such casts for a NULL check?
   Why don't we append the query similarly to how we handle taskName and status 
fields? That's IMHO cleaner approach, also you have such logic already applied 
for taskName and status, why not also processId and processInstanceId?



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