Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: filter process instances by multiple variables and return bpmn groups, documentation, and extensionProperties in process details #496

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ enum class BpmnElementType {
BUSINESS_RULE_TASK,
SCRIPT_TASK,
SEND_TASK,
INCLUSIVE_GATEWAY
INCLUSIVE_GATEWAY,
GROUP
}
22 changes: 22 additions & 0 deletions data/src/main/kotlin/io/zeebe/zeeqs/data/entity/VariableFilter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.zeebe.zeeqs.data.entity

enum class ComparisonOperation {
EQUALS,
CONTAINS
}

enum class FilterOperation {
AND,
OR
}

class VariableFilter (
val name: String,
val value: String,
val comparisonOperation: ComparisonOperation = ComparisonOperation.EQUALS
)

class VariableFilterGroup (
val variables: List<VariableFilter>,
val filterOperation: FilterOperation = FilterOperation.OR
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@ import org.springframework.data.domain.Pageable
import org.springframework.data.repository.PagingAndSortingRepository
import org.springframework.stereotype.Repository

interface ProcessInstanceKeyOnly {
fun getKey(): Long
}

@Repository
interface ProcessInstanceRepository : PagingAndSortingRepository<ProcessInstance, Long> {

fun findByParentProcessInstanceKey(parentProcessInstanceKey: Long): List<ProcessInstance>

fun findByProcessDefinitionKeyAndStateIn(processDefinitionKey: Long, stateIn: List<ProcessInstanceState>): List<ProcessInstanceKeyOnly>

fun findByProcessDefinitionKeyAndStateIn(processDefinitionKey: Long, stateIn: List<ProcessInstanceState>, pageable: Pageable): List<ProcessInstance>

fun countByProcessDefinitionKeyAndStateIn(processDefinitionKey: Long, stateIn: List<ProcessInstanceState>): Long

fun findByStateIn(stateIn: List<ProcessInstanceState>, pageable: Pageable): List<ProcessInstance>

fun findByStateIn(stateIn: List<ProcessInstanceState>): List<ProcessInstanceKeyOnly>

fun countByStateIn(stateIn: List<ProcessInstanceState>): Long

fun findByStateInAndKeyIn(stateIn: List<ProcessInstanceState>, key: List<Long>, pageable: Pageable): List<ProcessInstance>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ interface VariableRepository : PagingAndSortingRepository<Variable, Long> {

@Transactional(readOnly = true)
fun findByScopeKey(scopeKey: Long): List<Variable>

@Transactional(readOnly = true)
fun findByProcessInstanceKeyInAndName(processInstanceKey: List<Long>, name: String): List<Variable>


@Transactional(readOnly = true)
fun findByProcessInstanceKeyInAndNameIn(processInstanceKey: List<Long>, name: List<String>): List<Variable>

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ data class BpmnElementInfo(
val elementId: String,
val elementName: String?,
val elementType: BpmnElementType,
val metadata: BpmnElementMetadata
val metadata: BpmnElementMetadata,
val extensionElements: BpmnExtensionElements?,
val documentation: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.zeebe.zeeqs.data.service

data class BpmnExtensionProperty(
val name: String?,
val value: String?
)

data class IoMapping (
val source: String?,
val target: String?
)
data class ExtensionIoMapping(
val inputs: List<IoMapping>? = emptyList(),
val outputs: List<IoMapping>? = emptyList(),
)
data class BpmnExtensionElements(
val properties: List<BpmnExtensionProperty>? = emptyList(),
val ioMapping: ExtensionIoMapping? = ExtensionIoMapping(),
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package io.zeebe.zeeqs.data.service

import io.zeebe.zeeqs.data.entity.*
import io.zeebe.zeeqs.data.repository.ProcessInstanceKeyOnly
import io.zeebe.zeeqs.data.repository.ProcessInstanceRepository
import io.zeebe.zeeqs.data.repository.VariableRepository
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Component

@Component
class ProcessInstanceService(
private val processInstancesRepository: ProcessInstanceRepository,
private val variableRepository: VariableRepository) {

private fun getVariables(stateIn: List<ProcessInstanceState>, variableFilterGroup: VariableFilterGroup): List<Variable> {
val processInstances = processInstancesRepository.findByStateIn(stateIn).toList();
return getVariablesByProcessInstanceKeys(processInstances, variableFilterGroup);
}

private fun getVariables(stateIn: List<ProcessInstanceState>, processDefinitionKey: Long, variableFilterGroup: VariableFilterGroup): List<Variable> {
val processInstances = processInstancesRepository.findByProcessDefinitionKeyAndStateIn(processDefinitionKey, stateIn).toList();
return getVariablesByProcessInstanceKeys(processInstances, variableFilterGroup);

}

private fun matchesFilter(variable: Variable, filter: VariableFilter): Boolean {
return when (filter.comparisonOperation) {
ComparisonOperation.EQUALS -> variable.name == filter.name && variable.value == filter.value
ComparisonOperation.CONTAINS -> variable.name == filter.name && variable.value.contains(filter.value)
}
}

private fun getVariablesByProcessInstanceKeys(processInstances: List<ProcessInstanceKeyOnly>, variableFilterGroup: VariableFilterGroup): List<Variable> {
val processInstancesKeys = processInstances.map { it.getKey() }
val variableNames = variableFilterGroup.variables.map { it.name }
val variablesList = variableRepository.findByProcessInstanceKeyInAndNameIn(processInstancesKeys, variableNames)

return variablesList.filter { variable ->
if (variableFilterGroup.filterOperation == FilterOperation.AND) {
variableFilterGroup.variables.all { matchesFilter(variable, it) }
} else {
variableFilterGroup.variables.any { matchesFilter(variable, it) }
}
}
}


fun getProcessInstances(perPage: Int, page: Int, stateIn: List<ProcessInstanceState>, variableFilterGroup: VariableFilterGroup?): List<ProcessInstance> {
if (variableFilterGroup?.variables?.isNotEmpty() == true) {
val filteredVariables = getVariables(stateIn, variableFilterGroup);
val filteredProcessInstances = processInstancesRepository.findByStateInAndKeyIn(stateIn, filteredVariables.map { it.processInstanceKey }, PageRequest.of(page, perPage)).toList();
return filteredProcessInstances;
}
else {
return processInstancesRepository.findByStateIn(stateIn, PageRequest.of(page, perPage)).toList();
}
}

fun countProcessInstances(stateIn: List<ProcessInstanceState>, variableFilterGroup: VariableFilterGroup?): Long {
if (variableFilterGroup?.variables?.isNotEmpty() == true) {
val filteredVariables = getVariables(stateIn, variableFilterGroup);
return filteredVariables.count().toLong();
}

else {
return processInstancesRepository.countByStateIn(stateIn);
}
}


fun getProcessInstances(perPage: Int, page: Int, stateIn: List<ProcessInstanceState>, processDefinitionKey: Long, variableFilterGroup: VariableFilterGroup?): List<ProcessInstance> {
if (variableFilterGroup?.variables?.isNotEmpty() == true) {
val filteredVariables = getVariables(stateIn, processDefinitionKey, variableFilterGroup);
val filteredProcessInstances = processInstancesRepository.findByStateInAndKeyIn(stateIn, filteredVariables.map { it.processInstanceKey }, PageRequest.of(page, perPage)).toList();
return filteredProcessInstances;
}
else {
return processInstancesRepository.findByProcessDefinitionKeyAndStateIn(processDefinitionKey, stateIn, PageRequest.of(page, perPage)).toList();
}
}

fun countProcessInstances(stateIn: List<ProcessInstanceState>, processDefinitionKey: Long, variableFilterGroup: VariableFilterGroup?): Long {
if (variableFilterGroup?.variables?.isNotEmpty() == true) {
val filteredVariables = getVariables(stateIn, processDefinitionKey, variableFilterGroup);
return filteredVariables.count().toLong();
}

else {
return processInstancesRepository.countByProcessDefinitionKeyAndStateIn(processDefinitionKey, stateIn);
}
}

}
76 changes: 61 additions & 15 deletions data/src/main/kotlin/io/zeebe/zeeqs/data/service/ProcessService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,28 @@ class ProcessService(val processRepository: ProcessRepository) {
@Cacheable(cacheNames = ["bpmnElementInfo"])
fun getBpmnElementInfo(processDefinitionKey: Long): Map<String, BpmnElementInfo>? {
return getBpmnModel(processDefinitionKey)
?.let { it.getModelElementsByType(FlowElement::class.java) }
?.map { flowElement ->
Pair(
flowElement.id, BpmnElementInfo(
elementId = flowElement.id,
elementName = flowElement.name,
elementType = getBpmnElementType(flowElement),
metadata = getMetadata(flowElement)
)
)
}
?.toMap()
?.let { model ->
(model.getModelElementsByType(FlowElement::class.java)?.associate { flowElement ->
flowElement.id to BpmnElementInfo(
elementId = flowElement.id,
elementName = flowElement.name,
elementType = getBpmnElementType(flowElement),
metadata = getMetadata(flowElement),
extensionElements = getExtensionElements(flowElement),
documentation = getDocumentation(flowElement)
)
}.orEmpty() +
model.getModelElementsByType(Group::class.java)?.associate { groupElement ->
groupElement.id to BpmnElementInfo(
elementId = groupElement.id,
elementName = groupElement.category?.value ?: "",
elementType = getBpmnElementType(groupElement),
metadata = getMetadata(groupElement),
extensionElements = getExtensionElements(groupElement),
documentation = getDocumentation(groupElement)
)
}.orEmpty())
}
}

private fun getBpmnModel(processDefinitionKey: Long): BpmnModelInstance? {
Expand All @@ -41,7 +51,7 @@ class ProcessService(val processRepository: ProcessRepository) {
?.let { Bpmn.readModelFromStream(it) }
}

private fun getBpmnElementType(element: FlowElement): BpmnElementType {
private fun getBpmnElementType(element: BaseElement): BpmnElementType {
return when (element.elementType.typeName) {
BpmnModelConstants.BPMN_ELEMENT_PROCESS -> BpmnElementType.PROCESS
BpmnModelConstants.BPMN_ELEMENT_SUB_PROCESS -> getBpmnSubprocessType(element)
Expand All @@ -63,11 +73,12 @@ class ProcessService(val processRepository: ProcessRepository) {
BpmnModelConstants.BPMN_ELEMENT_BUSINESS_RULE_TASK -> BpmnElementType.BUSINESS_RULE_TASK
BpmnModelConstants.BPMN_ELEMENT_SCRIPT_TASK -> BpmnElementType.SCRIPT_TASK
BpmnModelConstants.BPMN_ELEMENT_INCLUSIVE_GATEWAY -> BpmnElementType.INCLUSIVE_GATEWAY
BpmnModelConstants.BPMN_ELEMENT_GROUP -> BpmnElementType.GROUP
else -> BpmnElementType.UNKNOWN
}
}

private fun getBpmnSubprocessType(element: FlowElement) =
private fun getBpmnSubprocessType(element: BaseElement) =
if (element is SubProcess) {
if (element.triggeredByEvent()) {
BpmnElementType.EVENT_SUB_PROCESS
Expand All @@ -78,7 +89,7 @@ class ProcessService(val processRepository: ProcessRepository) {
BpmnElementType.UNKNOWN
}

private fun getMetadata(element: FlowElement): BpmnElementMetadata {
private fun getMetadata(element: BaseElement): BpmnElementMetadata {
return BpmnElementMetadata(
jobType = element
.getSingleExtensionElement(ZeebeTaskDefinition::class.java)
Expand Down Expand Up @@ -156,6 +167,41 @@ class ProcessService(val processRepository: ProcessRepository) {
)
}

private fun getExtensionElements(element: BaseElement): BpmnExtensionElements {

val properties = element.extensionElements?.elementsQuery
?.filterByType(ZeebeProperties::class.java)
?.findSingleResult()
?.map { properties ->
properties.properties?.map { property ->
BpmnExtensionProperty(name = property.name, value = property.value)
}
}
?.orElse(emptyList()) ?: emptyList()

val ioMapping = element.extensionElements?.elementsQuery
?.filterByType(ZeebeIoMapping::class.java)
?.findSingleResult()
?.map { mapping ->
ExtensionIoMapping(
inputs = mapping.inputs?.map { input ->
IoMapping(source = input.source, target = input.target)
} ?: emptyList(),
outputs = mapping.outputs?.map { output ->
IoMapping(source = output.source, target = output.target)
} ?: emptyList()
)
}
?.orElse(ExtensionIoMapping(inputs = emptyList(), outputs = emptyList())) ?: ExtensionIoMapping(inputs = emptyList(), outputs = emptyList())

return BpmnExtensionElements(properties = properties, ioMapping = ioMapping)
}


private fun getDocumentation(element: BaseElement): String {
return element.documentations.joinToString(separator = "") { it.textContent }
}

@Cacheable(cacheNames = ["userTaskForm"])
fun getForm(processDefinitionKey: Long, formKey: String): String? {
return getBpmnModel(processDefinitionKey)
Expand Down
13 changes: 7 additions & 6 deletions data/src/test/kotlin/io/zeebe/zeeqs/ProcessServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.TestConfiguration
import java.time.Instant
Expand Down Expand Up @@ -52,12 +51,12 @@ class ProcessServiceTest(
// then
assertThat(info)
.isNotNull()
.contains(entry("s", BpmnElementInfo("s", "start", BpmnElementType.START_EVENT, BpmnElementMetadata())))
.contains(entry("t", BpmnElementInfo("t", "task", BpmnElementType.SERVICE_TASK, BpmnElementMetadata(jobType = "test"))))
.contains(entry("s", BpmnElementInfo("s", "start", BpmnElementType.START_EVENT, BpmnElementMetadata(), BpmnExtensionElements(properties = emptyList(), ioMapping = ExtensionIoMapping()), "")))
.contains(entry("t", BpmnElementInfo("t", "task", BpmnElementType.SERVICE_TASK, BpmnElementMetadata(jobType = "test"), BpmnExtensionElements(properties = emptyList(), ioMapping = ExtensionIoMapping()), "")))
.contains(entry("u", BpmnElementInfo("u", "userTask", BpmnElementType.USER_TASK, BpmnElementMetadata(
userTaskAssignmentDefinition = UserTaskAssignmentDefinition(assignee = "user1", candidateGroups = "group1"))))
userTaskAssignmentDefinition = UserTaskAssignmentDefinition(assignee = "user1", candidateGroups = "group1")), BpmnExtensionElements(properties = emptyList(), ioMapping = ExtensionIoMapping()), ""))
)
.contains(entry("e", BpmnElementInfo("e", null, BpmnElementType.END_EVENT, BpmnElementMetadata())))
.contains(entry("e", BpmnElementInfo("e", null, BpmnElementType.END_EVENT, BpmnElementMetadata(), BpmnExtensionElements(properties = emptyList(), ioMapping = ExtensionIoMapping()), "")))
}

@Test
Expand Down Expand Up @@ -90,7 +89,9 @@ class ProcessServiceTest(
key = "camunda-forms:bpmn:form_A",
resource = """{"x":1}"""
)
)
),
BpmnExtensionElements(properties = emptyList(), ioMapping = ExtensionIoMapping()),
""
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package io.zeebe.zeeqs.graphql.resolvers.query

import io.zeebe.zeeqs.data.entity.ProcessInstance
import io.zeebe.zeeqs.data.entity.ProcessInstanceState
import io.zeebe.zeeqs.data.entity.VariableFilterGroup
import io.zeebe.zeeqs.data.repository.ProcessInstanceRepository
import io.zeebe.zeeqs.data.service.ProcessInstanceService
import io.zeebe.zeeqs.graphql.resolvers.connection.ProcessInstanceConnection
import org.springframework.data.domain.PageRequest
import org.springframework.data.repository.findByIdOrNull
Expand All @@ -12,18 +14,20 @@ import org.springframework.stereotype.Controller

@Controller
class ProcessInstanceQueryResolver(
val processInstanceRepository: ProcessInstanceRepository
val processInstanceRepository: ProcessInstanceRepository,
val processInstanceService: ProcessInstanceService
) {

@QueryMapping
fun processInstances(
@Argument perPage: Int,
@Argument page: Int,
@Argument stateIn: List<ProcessInstanceState>
@Argument stateIn: List<ProcessInstanceState>,
@Argument variablesFilter: VariableFilterGroup?
): ProcessInstanceConnection {
return ProcessInstanceConnection(
getItems = { processInstanceRepository.findByStateIn(stateIn, PageRequest.of(page, perPage)).toList() },
getCount = { processInstanceRepository.countByStateIn(stateIn) }
getItems = { processInstanceService.getProcessInstances(perPage, page, stateIn, variablesFilter) },
getCount = { processInstanceService.countProcessInstances(stateIn, variablesFilter) }
)
}

Expand Down
Loading
Loading