From 4cc223495dd9f83e6ac655c239b90638664fe317 Mon Sep 17 00:00:00 2001 From: Alex Porcelli Date: Tue, 13 Feb 2024 16:15:31 -0500 Subject: [PATCH 1/2] RHPAM-4889: using a feature flag (jbpm.wb.querymode values DEFAULT or STRICT) to enable/disable strict mode. by default it'll continue behave as before this change. --- .../KieServerQueryDefinitionLoader.java | 37 +++++- .../default-query-definitions-strict.json | 74 ++++++++++++ ...erQueryDefinitionLoaderStrictModeTest.java | 113 ++++++++++++++++++ .../KieServerQueryDefinitionLoaderTest.java | 13 ++ 4 files changed, 232 insertions(+), 5 deletions(-) create mode 100644 jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/resources/default-query-definitions-strict.json create mode 100644 jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/test/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoaderStrictModeTest.java diff --git a/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoader.java b/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoader.java index 341aa2806f..9d14341d84 100644 --- a/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoader.java +++ b/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoader.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; +import java.util.Properties; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.event.Event; @@ -41,26 +42,52 @@ public class KieServerQueryDefinitionLoader { private static final Logger LOGGER = LoggerFactory.getLogger(KieServerQueryDefinitionLoader.class); + static final String JBPM_WB_QUERY_MODE = "jbpm.wb.querymode"; + enum QueryMode { + DEFAULT, + STRICT; + + static QueryMode convert(final String mode) { + try { + return QueryMode.valueOf(mode.toUpperCase()); + } catch (IllegalArgumentException ignored) { + } + return QueryMode.DEFAULT; + } + } + @Inject Event event; @PostConstruct public void init() { - loadDefaultQueryDefinitions(); + init(System.getProperties()); } - protected void loadDefaultQueryDefinitions() { + void init(final Properties properties) { + loadDefaultQueryDefinitions(QueryMode.convert(properties.getProperty(JBPM_WB_QUERY_MODE, QueryMode.DEFAULT.toString()))); + } + + protected void loadDefaultQueryDefinitions(final QueryMode queryMode) { + final String resourceName; + + if (QueryMode.STRICT.equals(queryMode)){ + resourceName = "/default-query-definitions-strict.json"; + } else { + resourceName = "/default-query-definitions.json"; + } + // load any default query definitions - try (InputStream qdStream = this.getClass().getResourceAsStream("/default-query-definitions.json")) { + try (InputStream qdStream = this.getClass().getResourceAsStream(resourceName)) { if (qdStream == null) { - LOGGER.info("Default query definitions file default-query-definitions.json not found"); + LOGGER.info("Default query definitions file " + resourceName + " not found"); return; } loadQueryDefinitions(qdStream, MarshallerFactory.getMarshaller(MarshallingFormat.JSON, this.getClass().getClassLoader())); } catch (Exception e) { - LOGGER.error("Error when loading default query definitions from default-query-definitions.json", + LOGGER.error("Error when loading default query definitions from " + resourceName, e); } } diff --git a/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/resources/default-query-definitions-strict.json b/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/resources/default-query-definitions-strict.json new file mode 100644 index 0000000000..19503934e7 --- /dev/null +++ b/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/resources/default-query-definitions-strict.json @@ -0,0 +1,74 @@ +[ + { + "query-name": "jbpmProcessInstances", + "query-source": "${org.kie.server.persistence.ds}", + "query-expression": "SELECT LOG.PROCESSINSTANCEID, LOG.PROCESSID, LOG.START_DATE, LOG.END_DATE, LOG.STATUS, LOG.PARENTPROCESSINSTANCEID, LOG.OUTCOME, LOG.DURATION, LOG.USER_IDENTITY, LOG.PROCESSVERSION, LOG.PROCESSNAME, LOG.CORRELATIONKEY, LOG.EXTERNALID, LOG.PROCESSINSTANCEDESCRIPTION, LOG.SLA_DUE_DATE, LOG.SLACOMPLIANCE, COALESCE ( INFO.LASTMODIFICATIONDATE, LOG.END_DATE ) AS LASTMODIFICATIONDATE, COUNT( ERRINFO.ID ) ERRORCOUNT FROM ProcessInstanceLog LOG LEFT JOIN ExecutionErrorInfo ERRINFO ON ERRINFO.PROCESS_INST_ID=LOG.PROCESSINSTANCEID AND ERRINFO.ERROR_ACK=0 LEFT JOIN ProcessInstanceInfo INFO ON INFO.INSTANCEID=LOG.PROCESSINSTANCEID GROUP BY LOG.PROCESSINSTANCEID, LOG.PROCESSID, LOG.START_DATE, LOG.END_DATE, LOG.STATUS, LOG.PARENTPROCESSINSTANCEID, LOG.OUTCOME, LOG.DURATION, LOG.USER_IDENTITY, LOG.PROCESSVERSION, LOG.PROCESSNAME, LOG.CORRELATIONKEY, LOG.EXTERNALID, LOG.PROCESSINSTANCEDESCRIPTION, LOG.SLA_DUE_DATE, LOG.SLACOMPLIANCE, COALESCE ( INFO.LASTMODIFICATIONDATE, LOG.END_DATE )", + "query-target": "FILTERED_PROCESS" + }, + { + "query-name": "jbpmProcessInstancesWithVariables", + "query-source": "${org.kie.server.persistence.ds}", + "query-expression": "select vil.processInstanceId, vil.processId, vil.id, vil.variableId, vil.value from VariableInstanceLog vil left join VariableInstanceLog vil2 on vil.processInstanceId = vil2.processInstanceId and vil.variableId = vil2.variableId and vil.id < vil2.id where vil2.id is null", + "query-target": "CUSTOM" + }, + { + "query-name": "processesMonitoring", + "query-source": "${org.kie.server.persistence.ds}", + "query-expression": "select log.processInstanceId, log.processId, log.start_date, log.end_date, log.status, log.duration, log.user_identity, log.processVersion, log.processName, log.externalId from ProcessInstanceLog log", + "query-target": "FILTERED_PROCESS" + }, + { + "query-name": "tasksMonitoring", + "query-source": "${org.kie.server.persistence.ds}", + "query-expression": "select p.processName, p.externalId, t.taskId, t.taskName, t.status, t.createdDate, t.startDate, t.endDate, t.processInstanceId, t.userId, t.duration from ProcessInstanceLog p inner join BAMTaskSummary t on (t.processInstanceId = p.processInstanceId) inner join (select min(pk) as pk from BAMTaskSummary group by taskId) d on t.pk = d.pk", + "query-target": "FILTERED_PROCESS" + }, + { + "query-name": "jbpmRequestList", + "query-source": "${org.kie.server.persistence.ds}", + "query-expression": "select ri.id, ri.timestamp, ri.status, ri.commandName, ri.message, ri.businessKey, ri.retries, ri.executions, pil.processName, pil.processInstanceId, pil.processInstanceDescription, ri.deploymentId from RequestInfo ri left join ProcessInstanceLog pil on pil.processInstanceId=ri.processInstanceId", + "query-target": "CUSTOM" + }, + { + "query-name": "jbpmExecutionErrorList", + "query-source": "${org.kie.server.persistence.ds}", + "query-expression": "select eri.ERROR_ACK, eri.ERROR_ACK_BY, eri.ERROR_ACK_AT, eri.ACTIVITY_ID, eri.ACTIVITY_NAME, eri.DEPLOYMENT_ID, eri.ERROR_DATE, eri.ERROR_ID, eri.ERROR_MSG, eri.JOB_ID, eri.PROCESS_ID, eri.PROCESS_INST_ID, eri.ERROR_TYPE from ExecutionErrorInfo eri", + "query-target": "CUSTOM" + }, + { + "query-name": "jbpmHumanTasks", + "query-source": "${org.kie.server.persistence.ds}", + "query-expression": "select t.activationTime, t.actualOwner, t.createdBy, t.createdOn, t.deploymentId, t.description, t.dueDate, t.name, t.parentId, t.priority, t.processId, t.processInstanceId, t.processSessionId, t.status, t.taskId, t.workItemId, t.lastModificationDate, pil.correlationKey, pil.processInstanceDescription, nil.sla_due_date, nil.slaCompliance from AuditTaskImpl t left join ProcessInstanceLog pil on pil.processInstanceId=t.processInstanceId left join NodeInstanceLog nil on nil.workItemId=t.workItemId", + "query-target": "CUSTOM" + }, + { + "query-name": "jbpmHumanTasksWithUser", + "query-source": "${org.kie.server.persistence.ds}", + "query-expression": "select t.activationTime, t.actualOwner, t.createdBy, t.createdOn, t.deploymentId, t.description, t.dueDate, t.name, t.parentId, t.priority, t.processId, t.processInstanceId, t.processSessionId, t.status, t.taskId, t.workItemId, t.lastModificationDate, pil.correlationKey, pil.processInstanceDescription , oe.id, eo.entity_id, nil.sla_due_date, nil.slaCompliance from AuditTaskImpl t left join PeopleAssignments_PotOwners po on t.taskId=po.task_id left join OrganizationalEntity oe on po.entity_id=oe.id left join ProcessInstanceLog pil on pil.processInstanceId=t.processInstanceId left join PeopleAssignments_ExclOwners eo on t.taskId=eo.task_id left join NodeInstanceLog nil on nil.workItemId=t.workItemId", + "query-target": "FILTERED_PO_TASK" + }, + { + "query-name": "jbpmHumanTasksWithAdmin", + "query-source": "${org.kie.server.persistence.ds}", + "query-expression": "select t.activationTime, t.actualOwner, t.createdBy, t.createdOn, t.deploymentId, t.description, t.dueDate, t.name, t.parentId, t.priority, t.processId, t.processInstanceId, t.processSessionId, t.status, t.taskId, t.workItemId, t.lastModificationDate, pil.correlationKey, pil.processInstanceDescription ,oe.id, nil.sla_due_date, nil.slaCompliance, (select COUNT(errInfo.id) from ExecutionErrorInfo errInfo where errInfo.ACTIVITY_ID = t.taskId and errInfo.PROCESS_INST_ID = pil.processInstanceId and errInfo.ERROR_ACK = 0 and errInfo.ERROR_TYPE = 'Task') as errorCount from AuditTaskImpl t left join ProcessInstanceLog pil on pil.processInstanceId = t.processInstanceId left join PeopleAssignments_BAs ba on t.taskId = ba.task_id left join OrganizationalEntity oe on ba.entity_id = oe.id left join NodeInstanceLog nil on nil.workItemId=t.workItemId", + "query-target": "FILTERED_BA_TASK" + }, + { + "query-name": "jbpmHumanTasksWithAdminExtended", + "query-source": "${org.kie.server.persistence.ds}", + "query-expression": "select t.activationTime, t.actualOwner, t.createdBy, t.createdOn, t.deploymentId, t.description, t.dueDate, t.name, t.parentId, t.priority, t.processId, t.processInstanceId, t.processSessionId, t.status, t.taskId, t.workItemId, t.lastModificationDate, pil.correlationKey, pil.processInstanceDescription ,oe.id, nil.sla_due_date, nil.slaCompliance,(select COUNT(errInfo.id) from ExecutionErrorInfo errInfo where errInfo.ACTIVITY_ID = t.taskId and errInfo.PROCESS_INST_ID = pil.processInstanceId and errInfo.ERROR_ACK = 0 and errInfo.ERROR_TYPE = 'Task') as errorCount, i18n.text as subject, i18n.language as language, task.formname as formname, (SELECT te1.userId FROM TaskEvent te1 LEFT JOIN TaskEvent te2 ON te1.id < te2.id WHERE te2.id IS NULL) as lastUser from AuditTaskImpl t left join ProcessInstanceLog pil on pil.processInstanceId = t.processInstanceId left join PeopleAssignments_BAs ba on t.taskId = ba.task_id left join OrganizationalEntity oe on ba.entity_id = oe.id left join NodeInstanceLog nil on nil.workItemId=t.workItemId left join Task task on task.id = t.taskId left join I18NText i18n ON i18n.Task_Subjects_Id = t.taskId", + "query-target": "FILTERED_BA_TASK" + }, + { + "query-name": "jbpmHumanTasksWithVariables", + "query-source": "${org.kie.server.persistence.ds}", + "query-expression": "select tvi.taskId, tvi.name, tvi.value from TaskVariableImpl tvi", + "query-target": "CUSTOM" + }, + { + "query-name": "jbpmProcessInstanceLogs", + "query-source": "${org.kie.server.persistence.ds}", + "query-expression": "select log.id, log.nodeId, log.nodeName, log.nodeType, log.externalId, log.processInstanceId, log.log_date, log.connection, log.type, log.workItemId, log.referenceId, log.nodeContainerId, log.sla_due_date, log.slaCompliance from NodeInstanceLog log ", + "query-target": "CUSTOM" + } +] \ No newline at end of file diff --git a/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/test/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoaderStrictModeTest.java b/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/test/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoaderStrictModeTest.java new file mode 100644 index 0000000000..246a85a669 --- /dev/null +++ b/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/test/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoaderStrictModeTest.java @@ -0,0 +1,113 @@ +/* + * Copyright 2021 Red Hat, Inc. and/or its affiliates. + * + * Licensed 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.jbpm.workbench.ks.integration; + +import org.jbpm.workbench.ks.integration.event.QueryDefinitionLoaded; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.kie.server.api.model.definition.QueryDefinition; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import javax.enterprise.event.Event; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.MockitoAnnotations.openMocks; + +/** + * This test class has been created to make contributors aware about the importance of the queries defined in the + * "default-query-definitions.json" file. These queries has been tested and optimized due to some important performance + * issues found on a very important and critical customer. + * Please, consider running a performance test when one of them is changed. + * + * @see JBPM-9099 + */ +@RunWith(MockitoJUnitRunner.class) +public class KieServerQueryDefinitionLoaderStrictModeTest { + + private final Map receivedEvents = new HashMap<>(); + + private static final int TOTAL_QUERY_DEFINITIONS_EXPECTED = 12; + + @Mock + private Event event; + + @InjectMocks + private KieServerQueryDefinitionLoader kieServerQueryDefinitionLoader; + + @Before + public void setup() { + openMocks(this); + doAnswer(invocation -> { + QueryDefinitionLoaded queryDefinitionLoaded = invocation.getArgument(0); + receivedEvents.put(queryDefinitionLoaded.getDefinition().getName(), queryDefinitionLoaded.getDefinition()); + return null; + }).when(event).fire(any(QueryDefinitionLoaded.class)); + this.kieServerQueryDefinitionLoader.init(new Properties(){{ + put(KieServerQueryDefinitionLoader.JBPM_WB_QUERY_MODE, "STRICT"); + }}); + } + + @Test + public void testJbpmProcessInstances() { + QueryDefinition expectedQuery = QueryDefinition.builder() + .name("jbpmProcessInstances") + .expression("SELECT LOG.PROCESSINSTANCEID, LOG.PROCESSID, LOG.START_DATE, LOG.END_DATE, LOG.STATUS, LOG.PARENTPROCESSINSTANCEID, LOG.OUTCOME, LOG.DURATION, LOG.USER_IDENTITY, LOG.PROCESSVERSION, LOG.PROCESSNAME, LOG.CORRELATIONKEY, LOG.EXTERNALID, LOG.PROCESSINSTANCEDESCRIPTION, LOG.SLA_DUE_DATE, LOG.SLACOMPLIANCE, COALESCE ( INFO.LASTMODIFICATIONDATE, LOG.END_DATE ) AS LASTMODIFICATIONDATE, COUNT( ERRINFO.ID ) ERRORCOUNT FROM ProcessInstanceLog LOG LEFT JOIN ExecutionErrorInfo ERRINFO ON ERRINFO.PROCESS_INST_ID=LOG.PROCESSINSTANCEID AND ERRINFO.ERROR_ACK=0 LEFT JOIN ProcessInstanceInfo INFO ON INFO.INSTANCEID=LOG.PROCESSINSTANCEID GROUP BY LOG.PROCESSINSTANCEID, LOG.PROCESSID, LOG.START_DATE, LOG.END_DATE, LOG.STATUS, LOG.PARENTPROCESSINSTANCEID, LOG.OUTCOME, LOG.DURATION, LOG.USER_IDENTITY, LOG.PROCESSVERSION, LOG.PROCESSNAME, LOG.CORRELATIONKEY, LOG.EXTERNALID, LOG.PROCESSINSTANCEDESCRIPTION, LOG.SLA_DUE_DATE, LOG.SLACOMPLIANCE, COALESCE ( INFO.LASTMODIFICATIONDATE, LOG.END_DATE )") + .target("FILTERED_PROCESS") + .build(); + testQueryDefinitionLoaded(expectedQuery); + } + + @Test + public void testProcessesMonitoring() { + QueryDefinition expectedQuery = QueryDefinition.builder() + .name("processesMonitoring") + .expression("select log.processInstanceId, log.processId, log.start_date, log.end_date, log.status, " + + "log.duration, log.user_identity, log.processVersion, log.processName, " + + "log.externalId from ProcessInstanceLog log") + .target("FILTERED_PROCESS") + .build(); + testQueryDefinitionLoaded(expectedQuery); + } + + @Test + public void testTasksMonitoring() { + QueryDefinition expectedQuery = QueryDefinition.builder() + .name("tasksMonitoring") + .expression("select p.processName, p.externalId, t.taskId, t.taskName, t.status, t.createdDate, " + + "t.startDate, t.endDate, t.processInstanceId, t.userId, t.duration " + + "from ProcessInstanceLog p inner join BAMTaskSummary t on " + + "(t.processInstanceId = p.processInstanceId) inner join (select min(pk) as pk " + + "from BAMTaskSummary group by taskId) d on t.pk = d.pk") + .target("FILTERED_PROCESS") + .build(); + testQueryDefinitionLoaded(expectedQuery); + } + + private void testQueryDefinitionLoaded(QueryDefinition expectedQueryDefinition) { + assertTrue("No named-queries were found", receivedEvents.size() > 0); + assertNotNull("No query definition found for " + expectedQueryDefinition.getName(), receivedEvents.get(expectedQueryDefinition.getName())); + assertEquals(expectedQueryDefinition.getExpression(), receivedEvents.get(expectedQueryDefinition.getName()).getExpression()); + assertEquals(expectedQueryDefinition.getTarget(), receivedEvents.get(expectedQueryDefinition.getName()).getTarget()); + } +} diff --git a/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/test/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoaderTest.java b/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/test/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoaderTest.java index 2899eaba3f..58d296a172 100644 --- a/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/test/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoaderTest.java +++ b/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/test/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoaderTest.java @@ -75,6 +75,7 @@ public void testJbpmProcessInstances() { QueryDefinition expectedQuery = QueryDefinition.builder() .name("jbpmProcessInstances") .expression("SELECT LOG.PROCESSINSTANCEID, LOG.PROCESSID, LOG.START_DATE, LOG.END_DATE, LOG.STATUS, LOG.PARENTPROCESSINSTANCEID, LOG.OUTCOME, LOG.DURATION, LOG.USER_IDENTITY, LOG.PROCESSVERSION, LOG.PROCESSNAME, LOG.CORRELATIONKEY, LOG.EXTERNALID, LOG.PROCESSINSTANCEDESCRIPTION, LOG.SLA_DUE_DATE, LOG.SLACOMPLIANCE, COALESCE ( INFO.LASTMODIFICATIONDATE, LOG.END_DATE ) AS LASTMODIFICATIONDATE, COUNT( ERRINFO.ID ) ERRORCOUNT FROM ProcessInstanceLog LOG LEFT JOIN ExecutionErrorInfo ERRINFO ON ERRINFO.PROCESS_INST_ID=LOG.PROCESSINSTANCEID AND ERRINFO.ERROR_ACK=0 LEFT JOIN ProcessInstanceInfo INFO ON INFO.INSTANCEID=LOG.PROCESSINSTANCEID GROUP BY LOG.PROCESSINSTANCEID, LOG.PROCESSID, LOG.START_DATE, LOG.END_DATE, LOG.STATUS, LOG.PARENTPROCESSINSTANCEID, LOG.OUTCOME, LOG.DURATION, LOG.USER_IDENTITY, LOG.PROCESSVERSION, LOG.PROCESSNAME, LOG.CORRELATIONKEY, LOG.EXTERNALID, LOG.PROCESSINSTANCEDESCRIPTION, LOG.SLA_DUE_DATE, LOG.SLACOMPLIANCE, COALESCE ( INFO.LASTMODIFICATIONDATE, LOG.END_DATE )") + .target("CUSTOM") .build(); testQueryDefinitionLoaded(expectedQuery); } @@ -87,6 +88,7 @@ public void testJbpmProcessInstancesWithVariables() { "vil.variableId, vil.value from VariableInstanceLog vil " + "left join VariableInstanceLog vil2 on vil.processInstanceId = vil2.processInstanceId " + "and vil.variableId = vil2.variableId and vil.id < vil2.id where vil2.id is null") + .target("CUSTOM") .build(); testQueryDefinitionLoaded(expectedQuery); } @@ -98,6 +100,7 @@ public void testProcessesMonitoring() { .expression("select log.processInstanceId, log.processId, log.start_date, log.end_date, log.status, " + "log.duration, log.user_identity, log.processVersion, log.processName, " + "log.externalId from ProcessInstanceLog log") + .target("CUSTOM") .build(); testQueryDefinitionLoaded(expectedQuery); } @@ -111,6 +114,7 @@ public void testTasksMonitoring() { "from ProcessInstanceLog p inner join BAMTaskSummary t on " + "(t.processInstanceId = p.processInstanceId) inner join (select min(pk) as pk " + "from BAMTaskSummary group by taskId) d on t.pk = d.pk") + .target("CUSTOM") .build(); testQueryDefinitionLoaded(expectedQuery); } @@ -123,6 +127,7 @@ public void testJbpmRequestList() { "ri.retries, ri.executions, pil.processName, pil.processInstanceId, " + "pil.processInstanceDescription, ri.deploymentId from RequestInfo ri left join " + "ProcessInstanceLog pil on pil.processInstanceId=ri.processInstanceId") + .target("CUSTOM") .build(); testQueryDefinitionLoaded(expectedQuery); } @@ -135,6 +140,7 @@ public void testJbpmExecutionErrorList() { "eri.ACTIVITY_NAME, eri.DEPLOYMENT_ID, eri.ERROR_DATE, eri.ERROR_ID, " + "eri.ERROR_MSG, eri.JOB_ID, eri.PROCESS_ID, eri.PROCESS_INST_ID, eri.ERROR_TYPE " + "from ExecutionErrorInfo eri") + .target("CUSTOM") .build(); testQueryDefinitionLoaded(expectedQuery); } @@ -150,6 +156,7 @@ public void testJbpmHumanTasks() { "nil.sla_due_date, nil.slaCompliance from AuditTaskImpl t left join " + "ProcessInstanceLog pil on pil.processInstanceId=t.processInstanceId left join " + "NodeInstanceLog nil on nil.workItemId=t.workItemId") + .target("CUSTOM") .build(); testQueryDefinitionLoaded(expectedQuery); } @@ -167,6 +174,7 @@ public void testJbpmHumanTasksWithUser() { "OrganizationalEntity oe on po.entity_id=oe.id left join ProcessInstanceLog pil on " + "pil.processInstanceId=t.processInstanceId left join PeopleAssignments_ExclOwners eo " + "on t.taskId=eo.task_id left join NodeInstanceLog nil on nil.workItemId=t.workItemId") + .target("FILTERED_PO_TASK") .build(); testQueryDefinitionLoaded(expectedQuery); } @@ -185,6 +193,7 @@ public void testJbpmHumanTasksWithAdmin() { "left join ProcessInstanceLog pil on pil.processInstanceId = t.processInstanceId " + "left join PeopleAssignments_BAs ba on t.taskId = ba.task_id left join OrganizationalEntity oe " + "on ba.entity_id = oe.id left join NodeInstanceLog nil on nil.workItemId=t.workItemId") + .target("FILTERED_BA_TASK") .build(); testQueryDefinitionLoaded(expectedQuery); } @@ -206,6 +215,7 @@ public void testJbpmHumanTasksWithAdminExtended() { "left join PeopleAssignments_BAs ba on t.taskId = ba.task_id left join OrganizationalEntity oe " + "on ba.entity_id = oe.id left join NodeInstanceLog nil on nil.workItemId=t.workItemId " + "left join Task task on task.id = t.taskId left join I18NText i18n ON i18n.Task_Subjects_Id = t.taskId") + .target("FILTERED_BA_TASK") .build(); testQueryDefinitionLoaded(expectedQuery); } @@ -215,6 +225,7 @@ public void testJbpmHumanTasksWithVariables() { QueryDefinition expectedQuery = QueryDefinition.builder() .name("jbpmHumanTasksWithVariables") .expression("select tvi.taskId, tvi.name, tvi.value from TaskVariableImpl tvi") + .target("CUSTOM") .build(); testQueryDefinitionLoaded(expectedQuery); } @@ -227,6 +238,7 @@ public void testJbpmProcessInstanceLogs() { "log.processInstanceId, log.log_date, log.connection, log.type, log.workItemId, " + "log.referenceId, log.nodeContainerId, log.sla_due_date, log.slaCompliance " + "from NodeInstanceLog log ") + .target("CUSTOM") .build(); testQueryDefinitionLoaded(expectedQuery); } @@ -264,5 +276,6 @@ private void testQueryDefinitionLoaded(QueryDefinition expectedQueryDefinition) assertTrue("No named-queries were found", receivedEvents.size() > 0); assertNotNull("No query definition found for " + expectedQueryDefinition.getName(), receivedEvents.get(expectedQueryDefinition.getName())); assertEquals(expectedQueryDefinition.getExpression(), receivedEvents.get(expectedQueryDefinition.getName()).getExpression()); + assertEquals(expectedQueryDefinition.getTarget(), receivedEvents.get(expectedQueryDefinition.getName()).getTarget()); } } From 619fe5738234d3e1ba8f75ac359cd5d9da17fad3 Mon Sep 17 00:00:00 2001 From: Alex Porcelli Date: Tue, 13 Feb 2024 17:25:58 -0500 Subject: [PATCH 2/2] RHPAM-4889: reducing duplication. --- .../KieServerQueryDefinitionLoader.java | 67 +++++++++---------- .../default-query-definitions-strict.json | 60 ----------------- 2 files changed, 33 insertions(+), 94 deletions(-) diff --git a/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoader.java b/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoader.java index 9d14341d84..154bb28b0c 100644 --- a/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoader.java +++ b/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/java/org/jbpm/workbench/ks/integration/KieServerQueryDefinitionLoader.java @@ -19,6 +19,8 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; @@ -69,53 +71,50 @@ void init(final Properties properties) { } protected void loadDefaultQueryDefinitions(final QueryMode queryMode) { - final String resourceName; + final Map applyStrict = new HashMap<>(); - if (QueryMode.STRICT.equals(queryMode)){ - resourceName = "/default-query-definitions-strict.json"; - } else { - resourceName = "/default-query-definitions.json"; + if (QueryMode.STRICT.equals(queryMode)) { + QueryDefinition[] queries = loadQueryDefinitions("/default-query-definitions-strict.json"); + for (QueryDefinition q : queries) { + applyStrict.put(q.getName(), q.getTarget()); + } } - // load any default query definitions + final QueryDefinition[] queries = loadQueryDefinitions("/default-query-definitions.json"); + for (QueryDefinition q : queries) { + if (applyStrict.containsKey(q.getName())){ + q.setTarget(applyStrict.get(q.getName())); + } + LOGGER.info("Loaded query definition: {}", q); + event.fire(new QueryDefinitionLoaded(q)); + } + } + + protected QueryDefinition[] loadQueryDefinitions(String resourceName) { + try (InputStream qdStream = this.getClass().getResourceAsStream(resourceName)) { if (qdStream == null) { LOGGER.info("Default query definitions file " + resourceName + " not found"); - return; + return new QueryDefinition[0]; } - loadQueryDefinitions(qdStream, - MarshallerFactory.getMarshaller(MarshallingFormat.JSON, - this.getClass().getClassLoader())); - } catch (Exception e) { - LOGGER.error("Error when loading default query definitions from " + resourceName, - e); - } - } - protected void loadQueryDefinitions(final InputStream qdStream, - final Marshaller marshaller) throws IOException { - final String qdString = IOUtils.toString(qdStream, - Charset.forName("UTF-8")); + final String qdString = IOUtils.toString(qdStream, Charset.forName("UTF-8")); - try { - QueryDefinition[] queries = marshaller.unmarshall(qdString, - QueryDefinition[].class); + QueryDefinition[] queries = MarshallerFactory.getMarshaller(MarshallingFormat.JSON, this.getClass().getClassLoader()).unmarshall(qdString, QueryDefinition[].class); - LOGGER.info("Found {} query definitions", - queries == null ? 0 : queries.length); + LOGGER.info("Found {} query definitions", queries == null ? 0 : queries.length); - if (queries == null) { - return; - } - for (QueryDefinition q : - queries) { - LOGGER.info("Loaded query definition: {}", - q); - event.fire(new QueryDefinitionLoaded(q)); + if (queries == null){ + return new QueryDefinition[0]; } + + return queries; } catch (MarshallingException e) { - LOGGER.error("Error when unmarshalling query definitions from stream.", - e); + LOGGER.error("Error when unmarshalling query definitions from stream.", e); + } catch (Exception e) { + LOGGER.error("Error when loading default query definitions from " + resourceName, e); } + + return new QueryDefinition[0]; } } diff --git a/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/resources/default-query-definitions-strict.json b/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/resources/default-query-definitions-strict.json index 19503934e7..5e1d434f3d 100644 --- a/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/resources/default-query-definitions-strict.json +++ b/jbpm-wb-kie-server/jbpm-wb-kie-server-backend/src/main/resources/default-query-definitions-strict.json @@ -1,74 +1,14 @@ [ { "query-name": "jbpmProcessInstances", - "query-source": "${org.kie.server.persistence.ds}", - "query-expression": "SELECT LOG.PROCESSINSTANCEID, LOG.PROCESSID, LOG.START_DATE, LOG.END_DATE, LOG.STATUS, LOG.PARENTPROCESSINSTANCEID, LOG.OUTCOME, LOG.DURATION, LOG.USER_IDENTITY, LOG.PROCESSVERSION, LOG.PROCESSNAME, LOG.CORRELATIONKEY, LOG.EXTERNALID, LOG.PROCESSINSTANCEDESCRIPTION, LOG.SLA_DUE_DATE, LOG.SLACOMPLIANCE, COALESCE ( INFO.LASTMODIFICATIONDATE, LOG.END_DATE ) AS LASTMODIFICATIONDATE, COUNT( ERRINFO.ID ) ERRORCOUNT FROM ProcessInstanceLog LOG LEFT JOIN ExecutionErrorInfo ERRINFO ON ERRINFO.PROCESS_INST_ID=LOG.PROCESSINSTANCEID AND ERRINFO.ERROR_ACK=0 LEFT JOIN ProcessInstanceInfo INFO ON INFO.INSTANCEID=LOG.PROCESSINSTANCEID GROUP BY LOG.PROCESSINSTANCEID, LOG.PROCESSID, LOG.START_DATE, LOG.END_DATE, LOG.STATUS, LOG.PARENTPROCESSINSTANCEID, LOG.OUTCOME, LOG.DURATION, LOG.USER_IDENTITY, LOG.PROCESSVERSION, LOG.PROCESSNAME, LOG.CORRELATIONKEY, LOG.EXTERNALID, LOG.PROCESSINSTANCEDESCRIPTION, LOG.SLA_DUE_DATE, LOG.SLACOMPLIANCE, COALESCE ( INFO.LASTMODIFICATIONDATE, LOG.END_DATE )", "query-target": "FILTERED_PROCESS" }, - { - "query-name": "jbpmProcessInstancesWithVariables", - "query-source": "${org.kie.server.persistence.ds}", - "query-expression": "select vil.processInstanceId, vil.processId, vil.id, vil.variableId, vil.value from VariableInstanceLog vil left join VariableInstanceLog vil2 on vil.processInstanceId = vil2.processInstanceId and vil.variableId = vil2.variableId and vil.id < vil2.id where vil2.id is null", - "query-target": "CUSTOM" - }, { "query-name": "processesMonitoring", - "query-source": "${org.kie.server.persistence.ds}", - "query-expression": "select log.processInstanceId, log.processId, log.start_date, log.end_date, log.status, log.duration, log.user_identity, log.processVersion, log.processName, log.externalId from ProcessInstanceLog log", "query-target": "FILTERED_PROCESS" }, { "query-name": "tasksMonitoring", - "query-source": "${org.kie.server.persistence.ds}", - "query-expression": "select p.processName, p.externalId, t.taskId, t.taskName, t.status, t.createdDate, t.startDate, t.endDate, t.processInstanceId, t.userId, t.duration from ProcessInstanceLog p inner join BAMTaskSummary t on (t.processInstanceId = p.processInstanceId) inner join (select min(pk) as pk from BAMTaskSummary group by taskId) d on t.pk = d.pk", "query-target": "FILTERED_PROCESS" - }, - { - "query-name": "jbpmRequestList", - "query-source": "${org.kie.server.persistence.ds}", - "query-expression": "select ri.id, ri.timestamp, ri.status, ri.commandName, ri.message, ri.businessKey, ri.retries, ri.executions, pil.processName, pil.processInstanceId, pil.processInstanceDescription, ri.deploymentId from RequestInfo ri left join ProcessInstanceLog pil on pil.processInstanceId=ri.processInstanceId", - "query-target": "CUSTOM" - }, - { - "query-name": "jbpmExecutionErrorList", - "query-source": "${org.kie.server.persistence.ds}", - "query-expression": "select eri.ERROR_ACK, eri.ERROR_ACK_BY, eri.ERROR_ACK_AT, eri.ACTIVITY_ID, eri.ACTIVITY_NAME, eri.DEPLOYMENT_ID, eri.ERROR_DATE, eri.ERROR_ID, eri.ERROR_MSG, eri.JOB_ID, eri.PROCESS_ID, eri.PROCESS_INST_ID, eri.ERROR_TYPE from ExecutionErrorInfo eri", - "query-target": "CUSTOM" - }, - { - "query-name": "jbpmHumanTasks", - "query-source": "${org.kie.server.persistence.ds}", - "query-expression": "select t.activationTime, t.actualOwner, t.createdBy, t.createdOn, t.deploymentId, t.description, t.dueDate, t.name, t.parentId, t.priority, t.processId, t.processInstanceId, t.processSessionId, t.status, t.taskId, t.workItemId, t.lastModificationDate, pil.correlationKey, pil.processInstanceDescription, nil.sla_due_date, nil.slaCompliance from AuditTaskImpl t left join ProcessInstanceLog pil on pil.processInstanceId=t.processInstanceId left join NodeInstanceLog nil on nil.workItemId=t.workItemId", - "query-target": "CUSTOM" - }, - { - "query-name": "jbpmHumanTasksWithUser", - "query-source": "${org.kie.server.persistence.ds}", - "query-expression": "select t.activationTime, t.actualOwner, t.createdBy, t.createdOn, t.deploymentId, t.description, t.dueDate, t.name, t.parentId, t.priority, t.processId, t.processInstanceId, t.processSessionId, t.status, t.taskId, t.workItemId, t.lastModificationDate, pil.correlationKey, pil.processInstanceDescription , oe.id, eo.entity_id, nil.sla_due_date, nil.slaCompliance from AuditTaskImpl t left join PeopleAssignments_PotOwners po on t.taskId=po.task_id left join OrganizationalEntity oe on po.entity_id=oe.id left join ProcessInstanceLog pil on pil.processInstanceId=t.processInstanceId left join PeopleAssignments_ExclOwners eo on t.taskId=eo.task_id left join NodeInstanceLog nil on nil.workItemId=t.workItemId", - "query-target": "FILTERED_PO_TASK" - }, - { - "query-name": "jbpmHumanTasksWithAdmin", - "query-source": "${org.kie.server.persistence.ds}", - "query-expression": "select t.activationTime, t.actualOwner, t.createdBy, t.createdOn, t.deploymentId, t.description, t.dueDate, t.name, t.parentId, t.priority, t.processId, t.processInstanceId, t.processSessionId, t.status, t.taskId, t.workItemId, t.lastModificationDate, pil.correlationKey, pil.processInstanceDescription ,oe.id, nil.sla_due_date, nil.slaCompliance, (select COUNT(errInfo.id) from ExecutionErrorInfo errInfo where errInfo.ACTIVITY_ID = t.taskId and errInfo.PROCESS_INST_ID = pil.processInstanceId and errInfo.ERROR_ACK = 0 and errInfo.ERROR_TYPE = 'Task') as errorCount from AuditTaskImpl t left join ProcessInstanceLog pil on pil.processInstanceId = t.processInstanceId left join PeopleAssignments_BAs ba on t.taskId = ba.task_id left join OrganizationalEntity oe on ba.entity_id = oe.id left join NodeInstanceLog nil on nil.workItemId=t.workItemId", - "query-target": "FILTERED_BA_TASK" - }, - { - "query-name": "jbpmHumanTasksWithAdminExtended", - "query-source": "${org.kie.server.persistence.ds}", - "query-expression": "select t.activationTime, t.actualOwner, t.createdBy, t.createdOn, t.deploymentId, t.description, t.dueDate, t.name, t.parentId, t.priority, t.processId, t.processInstanceId, t.processSessionId, t.status, t.taskId, t.workItemId, t.lastModificationDate, pil.correlationKey, pil.processInstanceDescription ,oe.id, nil.sla_due_date, nil.slaCompliance,(select COUNT(errInfo.id) from ExecutionErrorInfo errInfo where errInfo.ACTIVITY_ID = t.taskId and errInfo.PROCESS_INST_ID = pil.processInstanceId and errInfo.ERROR_ACK = 0 and errInfo.ERROR_TYPE = 'Task') as errorCount, i18n.text as subject, i18n.language as language, task.formname as formname, (SELECT te1.userId FROM TaskEvent te1 LEFT JOIN TaskEvent te2 ON te1.id < te2.id WHERE te2.id IS NULL) as lastUser from AuditTaskImpl t left join ProcessInstanceLog pil on pil.processInstanceId = t.processInstanceId left join PeopleAssignments_BAs ba on t.taskId = ba.task_id left join OrganizationalEntity oe on ba.entity_id = oe.id left join NodeInstanceLog nil on nil.workItemId=t.workItemId left join Task task on task.id = t.taskId left join I18NText i18n ON i18n.Task_Subjects_Id = t.taskId", - "query-target": "FILTERED_BA_TASK" - }, - { - "query-name": "jbpmHumanTasksWithVariables", - "query-source": "${org.kie.server.persistence.ds}", - "query-expression": "select tvi.taskId, tvi.name, tvi.value from TaskVariableImpl tvi", - "query-target": "CUSTOM" - }, - { - "query-name": "jbpmProcessInstanceLogs", - "query-source": "${org.kie.server.persistence.ds}", - "query-expression": "select log.id, log.nodeId, log.nodeName, log.nodeType, log.externalId, log.processInstanceId, log.log_date, log.connection, log.type, log.workItemId, log.referenceId, log.nodeContainerId, log.sla_due_date, log.slaCompliance from NodeInstanceLog log ", - "query-target": "CUSTOM" } ] \ No newline at end of file