From 38d3ac6da79a4bcf7a5381d35e32db2867ce5bc5 Mon Sep 17 00:00:00 2001 From: Hussain Mubaireek Date: Mon, 25 Mar 2024 14:59:11 +0300 Subject: [PATCH] feat: get process instances by variable name and value --- .../repository/ProcessInstanceRepository.kt | 9 +++ .../data/repository/VariableRepository.kt | 4 + .../data/service/ProcessInstanceService.kt | 79 +++++++++++++++++++ .../query/ProcessInstanceQueryResolver.kt | 12 ++- .../graphql/resolvers/type/ProcessResolver.kt | 25 ++++-- .../main/resources/graphql/Process.graphqls | 4 +- .../graphql/ProcessInstance.graphqls | 4 +- 7 files changed, 123 insertions(+), 14 deletions(-) create mode 100644 data/src/main/kotlin/io/zeebe/zeeqs/data/service/ProcessInstanceService.kt diff --git a/data/src/main/kotlin/io/zeebe/zeeqs/data/repository/ProcessInstanceRepository.kt b/data/src/main/kotlin/io/zeebe/zeeqs/data/repository/ProcessInstanceRepository.kt index f9bcff71..95959bbc 100644 --- a/data/src/main/kotlin/io/zeebe/zeeqs/data/repository/ProcessInstanceRepository.kt +++ b/data/src/main/kotlin/io/zeebe/zeeqs/data/repository/ProcessInstanceRepository.kt @@ -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 { fun findByParentProcessInstanceKey(parentProcessInstanceKey: Long): List + fun findByProcessDefinitionKeyAndStateIn(processDefinitionKey: Long, stateIn: List): List + fun findByProcessDefinitionKeyAndStateIn(processDefinitionKey: Long, stateIn: List, pageable: Pageable): List fun countByProcessDefinitionKeyAndStateIn(processDefinitionKey: Long, stateIn: List): Long fun findByStateIn(stateIn: List, pageable: Pageable): List + fun findByStateIn(stateIn: List): List + fun countByStateIn(stateIn: List): Long + fun findByStateInAndKeyIn(stateIn: List, key: List, pageable: Pageable): List } \ No newline at end of file diff --git a/data/src/main/kotlin/io/zeebe/zeeqs/data/repository/VariableRepository.kt b/data/src/main/kotlin/io/zeebe/zeeqs/data/repository/VariableRepository.kt index 5f9a315e..82c87003 100644 --- a/data/src/main/kotlin/io/zeebe/zeeqs/data/repository/VariableRepository.kt +++ b/data/src/main/kotlin/io/zeebe/zeeqs/data/repository/VariableRepository.kt @@ -13,4 +13,8 @@ interface VariableRepository : PagingAndSortingRepository { @Transactional(readOnly = true) fun findByScopeKey(scopeKey: Long): List + + @Transactional(readOnly = true) + fun findByProcessInstanceKeyInAndName(processInstanceKey: List, name: String): List + } \ No newline at end of file diff --git a/data/src/main/kotlin/io/zeebe/zeeqs/data/service/ProcessInstanceService.kt b/data/src/main/kotlin/io/zeebe/zeeqs/data/service/ProcessInstanceService.kt new file mode 100644 index 00000000..24c871ab --- /dev/null +++ b/data/src/main/kotlin/io/zeebe/zeeqs/data/service/ProcessInstanceService.kt @@ -0,0 +1,79 @@ +package io.zeebe.zeeqs.data.service + +import io.zeebe.zeeqs.data.entity.ProcessInstance +import io.zeebe.zeeqs.data.entity.ProcessInstanceState +import io.zeebe.zeeqs.data.entity.Variable +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, variableName: String, variableValue: String): List { + val processInstances = processInstancesRepository.findByStateIn(stateIn).toList(); + return getVariablesByProcessInstanceKeys(processInstances, variableName, variableValue); + } + + private fun getVariables(stateIn: List, processDefinitionKey: Long, variableName: String, variableValue: String): List { + val processInstances = processInstancesRepository.findByProcessDefinitionKeyAndStateIn(processDefinitionKey, stateIn).toList(); + return getVariablesByProcessInstanceKeys(processInstances, variableName, variableValue); + + } + + private fun getVariablesByProcessInstanceKeys(processInstances: List, variableName: String, variableValue: String): List { + val variables = variableRepository.findByProcessInstanceKeyInAndName(processInstances.map { it.getKey() }, variableName); + val filteredVariables = variables.filter { it.value == variableValue }; + return filteredVariables; + } + + fun getProcessInstances(perPage: Int, page: Int, stateIn: List, variableName: String?, variableValue: String?): List { + if(variableName != null && variableValue != null) { + val filteredVariables = getVariables(stateIn, variableName, variableValue); + 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, variableName: String?, variableValue: String?): Long { + if(variableName != null && variableValue != null) { + val filteredVariables = getVariables(stateIn, variableName, variableValue); + return filteredVariables.count().toLong(); + } + + else { + return processInstancesRepository.countByStateIn(stateIn); + } + } + + + fun getProcessInstances(perPage: Int, page: Int, stateIn: List, processDefinitionKey: Long, variableName: String?, variableValue: String?): List { + if(variableName != null && variableValue != null) { + val filteredVariables = getVariables(stateIn, processDefinitionKey, variableName, variableValue); + 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, processDefinitionKey: Long, variableName: String?, variableValue: String?): Long { + if(variableName != null && variableValue != null) { + val filteredVariables = getVariables(stateIn, processDefinitionKey, variableName, variableValue); + return filteredVariables.count().toLong(); + } + + else { + return processInstancesRepository.countByStateIn(stateIn); + } + } + +} \ No newline at end of file diff --git a/graphql-api/src/main/kotlin/io/zeebe/zeeqs/graphql/resolvers/query/ProcessInstanceQueryResolver.kt b/graphql-api/src/main/kotlin/io/zeebe/zeeqs/graphql/resolvers/query/ProcessInstanceQueryResolver.kt index 7943c905..7310f050 100644 --- a/graphql-api/src/main/kotlin/io/zeebe/zeeqs/graphql/resolvers/query/ProcessInstanceQueryResolver.kt +++ b/graphql-api/src/main/kotlin/io/zeebe/zeeqs/graphql/resolvers/query/ProcessInstanceQueryResolver.kt @@ -3,6 +3,7 @@ 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.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 @@ -12,18 +13,21 @@ 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 + @Argument stateIn: List, + @Argument variableName: String?, + @Argument variableValue: String? ): ProcessInstanceConnection { return ProcessInstanceConnection( - getItems = { processInstanceRepository.findByStateIn(stateIn, PageRequest.of(page, perPage)).toList() }, - getCount = { processInstanceRepository.countByStateIn(stateIn) } + getItems = { processInstanceService.getProcessInstances(perPage, page, stateIn, variableName, variableValue) }, + getCount = { processInstanceService.countProcessInstances(stateIn, variableName, variableValue) } ) } diff --git a/graphql-api/src/main/kotlin/io/zeebe/zeeqs/graphql/resolvers/type/ProcessResolver.kt b/graphql-api/src/main/kotlin/io/zeebe/zeeqs/graphql/resolvers/type/ProcessResolver.kt index 2a10a279..98172e21 100644 --- a/graphql-api/src/main/kotlin/io/zeebe/zeeqs/graphql/resolvers/type/ProcessResolver.kt +++ b/graphql-api/src/main/kotlin/io/zeebe/zeeqs/graphql/resolvers/type/ProcessResolver.kt @@ -6,6 +6,7 @@ import io.zeebe.zeeqs.data.repository.ProcessInstanceRepository import io.zeebe.zeeqs.data.repository.SignalSubscriptionRepository import io.zeebe.zeeqs.data.repository.TimerRepository import io.zeebe.zeeqs.data.service.BpmnElementInfo +import io.zeebe.zeeqs.data.service.ProcessInstanceService import io.zeebe.zeeqs.data.service.ProcessService import io.zeebe.zeeqs.graphql.resolvers.connection.ProcessInstanceConnection import org.springframework.data.domain.PageRequest @@ -19,6 +20,7 @@ class ProcessResolver( val timerRepository: TimerRepository, val messageSubscriptionRepository: MessageSubscriptionRepository, val processService: ProcessService, + val processInstanceService: ProcessInstanceService, private val signalSubscriptionRepository: SignalSubscriptionRepository ) { @@ -27,20 +29,27 @@ class ProcessResolver( process: Process, @Argument perPage: Int, @Argument page: Int, - @Argument stateIn: List + @Argument stateIn: List, + @Argument variableName: String?, + @Argument variableValue: String? ): ProcessInstanceConnection { return ProcessInstanceConnection( getItems = { - processInstanceRepository.findByProcessDefinitionKeyAndStateIn( - process.key, - stateIn, - PageRequest.of(page, perPage) + processInstanceService.getProcessInstances( + perPage, + page, + stateIn, + process.key, + variableName, + variableValue ).toList() }, getCount = { - processInstanceRepository.countByProcessDefinitionKeyAndStateIn( - process.key, - stateIn + processInstanceService.countProcessInstances( + stateIn, + process.key, + variableName, + variableValue ) } ) diff --git a/graphql-api/src/main/resources/graphql/Process.graphqls b/graphql-api/src/main/resources/graphql/Process.graphqls index 24dd7c93..d182d2bd 100644 --- a/graphql-api/src/main/resources/graphql/Process.graphqls +++ b/graphql-api/src/main/resources/graphql/Process.graphqls @@ -13,7 +13,9 @@ type Process { processInstances( perPage: Int = 10, page: Int = 0, - stateIn: [ProcessInstanceState!] = [ACTIVATED, COMPLETED, TERMINATED]): ProcessInstanceConnection! + stateIn: [ProcessInstanceState!] = [ACTIVATED, COMPLETED, TERMINATED] + variableName: String = null, + variableValue: String = null): ProcessInstanceConnection! # the scheduled timers of the timer start events of the process timers: [Timer!] # the opened message subscriptions of the message start events of the process diff --git a/graphql-api/src/main/resources/graphql/ProcessInstance.graphqls b/graphql-api/src/main/resources/graphql/ProcessInstance.graphqls index acafefd8..ff7afe3e 100644 --- a/graphql-api/src/main/resources/graphql/ProcessInstance.graphqls +++ b/graphql-api/src/main/resources/graphql/ProcessInstance.graphqls @@ -73,7 +73,9 @@ type Query { processInstances( perPage: Int = 10, page: Int = 0, - stateIn: [ProcessInstanceState!] = [ACTIVATED, COMPLETED, TERMINATED] + stateIn: [ProcessInstanceState!] = [ACTIVATED, COMPLETED, TERMINATED], + variableName: String = null, + variableValue: String = null ): ProcessInstanceConnection! }