Skip to content

Commit

Permalink
How to use with IC
Browse files Browse the repository at this point in the history
  • Loading branch information
valb3r committed Aug 21, 2024
1 parent 921c608 commit d3ceb80
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 210 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,5 @@
package com.valb3r.bpmn.intellij.activiti.plugin.actions

import com.intellij.database.psi.DbElement
import com.valb3r.bpmn.intellij.plugin.commons.actions.DefaultAttachBpmnDebuggerToDbAction
import com.valb3r.bpmn.intellij.plugin.commons.actions.IntelliJBpmnDebugger

class AttachActivitiBpmnDebuggerToDbAction : DefaultAttachBpmnDebuggerToDbAction({ ActivitiIntelliJBpmnDebugger(it) })

class ActivitiIntelliJBpmnDebugger(schema: DbElement): IntelliJBpmnDebugger(schema) {

override fun statementForRuntimeSelection(schema: String): String {
return """
SELECT re.act_id_ FROM ${"$schema."}act_ru_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_
WHERE re.proc_inst_id_ = (
SELECT re.proc_inst_id_ FROM ${"$schema."}act_ru_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_
WHERE re.start_time_ =
(
SELECT MAX(re.start_time_) FROM ${"$schema."}act_ru_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_ WHERE def.key_ = ?
)
LIMIT 1
) ORDER BY re.start_time_, re.id_
""".trimIndent()
}

override fun statementForHistoricalSelection(schema: String): String {
return """
SELECT re.act_id_ FROM ${"$schema."}act_hi_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_
WHERE re.proc_inst_id_ = (
SELECT re.proc_inst_id_ FROM ${"$schema."}act_hi_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_
WHERE re.start_time_ =
(
SELECT MAX(re.start_time_) FROM ${"$schema."}act_hi_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_ WHERE def.key_ = ?
)
LIMIT 1
) ORDER BY re.start_time_, re.id_
""".trimIndent()
}
}
class AttachActivitiBpmnDebuggerToDbAction : DefaultAttachBpmnDebuggerToDbAction()
Original file line number Diff line number Diff line change
@@ -1,120 +1,34 @@
package com.valb3r.bpmn.intellij.plugin.commons.actions

import com.intellij.database.dataSource.connection.DGDepartment
import com.intellij.database.model.DasNamespace
import com.intellij.database.psi.DbElement
import com.intellij.database.remote.jdbc.RemoteConnection
import com.intellij.database.util.DbImplUtil
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.LangDataKeys
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.valb3r.bpmn.intellij.plugin.bpmn.api.bpmn.BpmnElementId
import com.valb3r.bpmn.intellij.plugin.core.CANVAS_PAINT_TOPIC
import com.valb3r.bpmn.intellij.plugin.core.debugger.BpmnDebugger
import com.valb3r.bpmn.intellij.plugin.core.debugger.ExecutedElements
import com.valb3r.bpmn.intellij.plugin.core.debugger.detachDebugger
import com.valb3r.bpmn.intellij.plugin.core.debugger.prepareDebugger
import java.sql.Connection
import java.time.Duration
import java.time.Instant
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.atomic.AtomicReference

abstract class DefaultAttachBpmnDebuggerToDbAction(private val debugger: (schema: DbElement) -> BpmnDebugger) : AnAction() {
abstract class DefaultAttachBpmnDebuggerToDbAction() : AnAction() {

override fun actionPerformed(anActionEvent: AnActionEvent) {
val project = anActionEvent.project ?: return
val schema = properElem(anActionEvent) ?: return
prepareDebugger(project, debugger(schema))
ApplicationManager.getApplication().invokeLater {
anActionEvent.project!!.messageBus.syncPublisher(CANVAS_PAINT_TOPIC).repaint()
}
}

override fun update(anActionEvent: AnActionEvent) {
val project = anActionEvent.project
anActionEvent.presentation.isEnabledAndVisible = project != null && null != properElem(anActionEvent)
}

private fun properElem(anActionEvent: AnActionEvent): DbElement? {
return psiElements(anActionEvent)
?.filterIsInstance<DasNamespace>()
?.filterIsInstance<DbElement>()
?.firstOrNull()
}

private fun psiElements(anActionEvent: AnActionEvent) =
anActionEvent.getData(LangDataKeys.PSI_ELEMENT_ARRAY)
}

abstract class IntelliJBpmnDebugger(private val schema: DbElement): BpmnDebugger {

private val cacheTTL = Duration.ofSeconds(1)
private val worker: ForkJoinPool = ForkJoinPool(1)

private var cachedResult: AtomicReference<ExecutedElements?> = AtomicReference()
private var cachedAtTime: AtomicReference<Instant?> = AtomicReference()
abstract class IntelliJBpmnDebugger: BpmnDebugger {

override fun executionSequence(project: Project, processId: String): ExecutedElements? {
val cachedExpiry = cachedAtTime.get()?.plus(cacheTTL)
if (cachedExpiry?.isAfter(Instant.now()) == true) {
return cachedResult.get()
}

worker.submit {
cachedResult.set(fetchFromDb(project, processId))
cachedAtTime.set(Instant.now())
}

return cachedResult.get()
}

private fun fetchFromDb(project: Project, processId: String): ExecutedElements? {
try {
val connProvider = DbImplUtil.getDatabaseConnection(schema, DGDepartment.INTROSPECTION)?.get()
val remoteConn = connProvider?.remoteConnection
try {
remoteConn?.let {return readExecutionIds { stmt -> listIds(processId, stmt, it) }}
} finally {
remoteConn?.close()
}
} catch (ex: RuntimeException) {
detachDebugger(project)
}

return null
}

private fun readExecutionIds(idsFetch: (statement: String) -> List<String>): ExecutedElements? {
val ruIds = idsFetch(statementForRuntimeSelection(schema.name))
if (ruIds.isNotEmpty()) {
return ExecutedElements(ruIds.map { BpmnElementId(it) })
}

val hiIds = idsFetch(statementForHistoricalSelection(schema.name))
if (hiIds.isNotEmpty()) {
return ExecutedElements(hiIds.map { BpmnElementId(it) })
}

return null
}

private fun listIds(processId: String, statement: String, conn: Connection): List<String> {
val ruQuery = conn.prepareStatement(statement)
ruQuery.setString(1, processId)
val result = ruQuery.executeQuery()
return result.use {generateSequence { if (result.next()) result.getString(1) else null }.toList()}
}

private fun listIds(processId: String, statement: String, conn: RemoteConnection): List<String> {
val ruQuery = conn.prepareStatement(statement)
ruQuery.setString(1, processId)
val result = ruQuery.executeQuery()
return generateSequence { if (result.next()) result.getString(1) else null }.toList()
}

protected abstract fun statementForRuntimeSelection(schema: String): String
protected abstract fun statementForHistoricalSelection(schema: String): String
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.valb3r.bpmn.intellij.plugin.commons.actions

import com.intellij.database.model.DasNamespace
import com.intellij.database.psi.DbElement
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.LangDataKeys
import com.intellij.openapi.application.ApplicationManager
import com.valb3r.bpmn.intellij.plugin.core.CANVAS_PAINT_TOPIC
import com.valb3r.bpmn.intellij.plugin.core.debugger.currentDebugger
import com.valb3r.bpmn.intellij.plugin.core.debugger.detachDebugger

abstract class DefaultDetachBpmnDebuggerFromDbAction : AnAction() {
Expand All @@ -21,17 +18,9 @@ abstract class DefaultDetachBpmnDebuggerFromDbAction : AnAction() {
}

override fun update(anActionEvent: AnActionEvent) {
val project = anActionEvent.project
anActionEvent.presentation.isEnabledAndVisible =
project != null && null != properElem(anActionEvent) && (null != currentDebugger(project))
// NOP
}

private fun properElem(anActionEvent: AnActionEvent): DbElement? {
return psiElements(anActionEvent)
?.filterIsInstance<DasNamespace>()
?.filterIsInstance<DbElement>()
?.firstOrNull()
}

private fun psiElements(anActionEvent: AnActionEvent) =
anActionEvent.getData(LangDataKeys.PSI_ELEMENT_ARRAY)
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ plugins {
}

ext {
intellijPlatform = 'IU-2022.3'
intellijPlatformPlugins = ['java', 'DatabaseTools'] // DatabaseTools is for BPMN process 'debugging'
intellijPlatform = 'IC-2022.3'
intellijPlatformPlugins = ['java'] // DatabaseTools is for BPMN process 'debugging'

kotlinStdlib = '1.5.30'
kotlinApiVersion = '1.3' // It must be same as JB supplied Kotlin STDLIB i.e. for 2018 = 1.3. See https://youtrack.jetbrains.com/issue/KT-37435
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,5 @@
package com.valb3r.bpmn.intellij.plugin.camunda.actions

import com.intellij.database.psi.DbElement
import com.valb3r.bpmn.intellij.plugin.commons.actions.DefaultAttachBpmnDebuggerToDbAction
import com.valb3r.bpmn.intellij.plugin.commons.actions.IntelliJBpmnDebugger

class AttachCamundaBpmnDebuggerToDbAction : DefaultAttachBpmnDebuggerToDbAction({ CamundaIntelliJBpmnDebugger(it) })

class CamundaIntelliJBpmnDebugger(schema: DbElement): IntelliJBpmnDebugger(schema) {

override fun statementForRuntimeSelection(schema: String): String {
return """
SELECT re.act_id_ FROM ${"$schema."}act_ru_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_
WHERE re.proc_inst_id_ = (
SELECT re.proc_inst_id_ FROM ${"$schema."}act_ru_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_
WHERE re.start_time_ =
(
SELECT MAX(re.start_time_) FROM ${"$schema."}act_ru_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_ WHERE def.key_ = ?
)
LIMIT 1
) ORDER BY re.start_time_, re.id_
""".trimIndent()
}

override fun statementForHistoricalSelection(schema: String): String {
return """
SELECT re.act_id_ FROM ${"$schema."}act_hi_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_
WHERE re.proc_inst_id_ = (
SELECT re.proc_inst_id_ FROM ${"$schema."}act_hi_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_
WHERE re.start_time_ =
(
SELECT MAX(re.start_time_) FROM ${"$schema."}act_hi_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_ WHERE def.key_ = ?
)
LIMIT 1
) ORDER BY re.start_time_, re.id_
""".trimIndent()
}
}
class AttachCamundaBpmnDebuggerToDbAction : DefaultAttachBpmnDebuggerToDbAction()
Original file line number Diff line number Diff line change
@@ -1,40 +1,5 @@
package com.valb3r.bpmn.intellij.plugin.flowable.actions

import com.intellij.database.psi.DbElement
import com.valb3r.bpmn.intellij.plugin.commons.actions.DefaultAttachBpmnDebuggerToDbAction
import com.valb3r.bpmn.intellij.plugin.commons.actions.IntelliJBpmnDebugger

class AttachFlowableBpmnDebuggerToDbAction : DefaultAttachBpmnDebuggerToDbAction({ FlowableIntelliJBpmnDebugger(it) })

class FlowableIntelliJBpmnDebugger(schema: DbElement): IntelliJBpmnDebugger(schema) {

override fun statementForRuntimeSelection(schema: String): String {
return """
SELECT re.act_id_ FROM ${"$schema."}act_ru_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_
WHERE re.proc_inst_id_ = (
SELECT re.proc_inst_id_ FROM ${"$schema."}act_ru_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_
WHERE re.start_time_ =
(
SELECT MAX(re.start_time_) FROM ${"$schema."}act_ru_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_ WHERE def.key_ = ?
)
LIMIT 1
) ORDER BY re.start_time_, re.id_
""".trimIndent()
}

override fun statementForHistoricalSelection(schema: String): String {
return """
SELECT re.act_id_ FROM ${"$schema."}act_hi_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_
WHERE re.proc_inst_id_ = (
SELECT re.proc_inst_id_ FROM ${"$schema."}act_hi_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_
WHERE re.start_time_ =
(
SELECT MAX(re.start_time_) FROM ${"$schema."}act_hi_actinst re JOIN ${"$schema."}act_re_procdef def ON re.proc_def_id_ = def.id_ WHERE def.key_ = ?
)
LIMIT 1
) ORDER BY re.start_time_, re.id_
""".trimIndent()
}
}
class AttachFlowableBpmnDebuggerToDbAction : DefaultAttachBpmnDebuggerToDbAction()

0 comments on commit d3ceb80

Please sign in to comment.