From 241310bba294729cd7c476bcead6a25a84e0bc25 Mon Sep 17 00:00:00 2001 From: parisyup <66366646+parisyup@users.noreply.github.com> Date: Mon, 13 May 2024 15:29:35 +0400 Subject: [PATCH] Testing filtering queries --- .../states/CustomChatQuery.kt | 48 +++++++++++++++++++ .../workflows/ListChatsByCustomQueryFlow.kt | 3 +- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/kotlin-samples/customStateJSONandQuery/contracts/src/main/kotlin/com/r3/developers/customStateJSONandQuery/states/CustomChatQuery.kt b/kotlin-samples/customStateJSONandQuery/contracts/src/main/kotlin/com/r3/developers/customStateJSONandQuery/states/CustomChatQuery.kt index 07775a5..f6d3426 100644 --- a/kotlin-samples/customStateJSONandQuery/contracts/src/main/kotlin/com/r3/developers/customStateJSONandQuery/states/CustomChatQuery.kt +++ b/kotlin-samples/customStateJSONandQuery/contracts/src/main/kotlin/com/r3/developers/customStateJSONandQuery/states/CustomChatQuery.kt @@ -1,6 +1,10 @@ package com.r3.developers.customStateJSONandQuery.states +import net.corda.v5.ledger.utxo.StateAndRef +import net.corda.v5.ledger.utxo.query.VaultNamedQueryCollector import net.corda.v5.ledger.utxo.query.VaultNamedQueryFactory +import net.corda.v5.ledger.utxo.query.VaultNamedQueryStateAndRefFilter +import net.corda.v5.ledger.utxo.query.VaultNamedQueryStateAndRefTransformer import net.corda.v5.ledger.utxo.query.registration.VaultNamedQueryBuilderFactory class ChatCustomQueryFactory : VaultNamedQueryFactory { @@ -16,5 +20,49 @@ class ChatCustomQueryFactory : VaultNamedQueryFactory { "WHERE visible_states.custom_representation -> 'com.r3.developers.customStateJSONandQuery.states.ChatState' ->> 'messageContentFrom' = :nameOfSender" ) .register() + vaultNamedQueryBuilderFactory.create("GET_MSG_FROM_ALICE") + .whereJson( + "WHERE visible_states.custom_representation ? 'com.r3.developers.cordapptemplate.utxoexample.states.ChatState' " + ) + .filter(CustomQueryFilter()) + .register() + + vaultNamedQueryBuilderFactory.create("GET_ALL_MSGS_FROM") + .whereJson( + "WHERE visible_states.custom_representation ? 'com.r3.developers.cordapptemplate.utxoexample.states.ChatState' " + ) + .map(CustomQueryTransformer()) + .register() + + vaultNamedQueryBuilderFactory.create("GET_MSG_AMOUNT") + .whereJson( + "WHERE visible_states.custom_representation -> 'com.r3.developers.cordapptemplate.utxoexample.states.ChatState' ->> 'messageContentFrom' = :nameOfSender" + ) + .collect(CustomQueryCollector()) + .register() + } +} + +class CustomQueryFilter : VaultNamedQueryStateAndRefFilter { + override fun filter(data: StateAndRef, parameters: MutableMap): Boolean { + return true + } +} + +class CustomQueryTransformer : VaultNamedQueryStateAndRefTransformer { + override fun transform(data: StateAndRef, parameters: MutableMap): String { + return data.state.contractState.messageFrom.toString() + } +} + +class CustomQueryCollector : VaultNamedQueryCollector { + override fun collect( + resultSet: MutableList, + parameters: MutableMap + ): VaultNamedQueryCollector.Result { + return VaultNamedQueryCollector.Result( + listOf(resultSet.size), + true + ) } } \ No newline at end of file diff --git a/kotlin-samples/customStateJSONandQuery/workflows/src/main/kotlin/com/r3/developers/customStateJSONandQuery/workflows/ListChatsByCustomQueryFlow.kt b/kotlin-samples/customStateJSONandQuery/workflows/src/main/kotlin/com/r3/developers/customStateJSONandQuery/workflows/ListChatsByCustomQueryFlow.kt index d09254d..8e9a9b6 100644 --- a/kotlin-samples/customStateJSONandQuery/workflows/src/main/kotlin/com/r3/developers/customStateJSONandQuery/workflows/ListChatsByCustomQueryFlow.kt +++ b/kotlin-samples/customStateJSONandQuery/workflows/src/main/kotlin/com/r3/developers/customStateJSONandQuery/workflows/ListChatsByCustomQueryFlow.kt @@ -31,8 +31,7 @@ class ListChatsByCustomQueryFlow : ClientStartableFlow { log.info("ListChatsByCustomQueryFlow.call() called") //this is our custom query - val resultSet = ledgerService.query("GET_MSG_FROM", StateAndRef::class.java) - .setParameter("nameOfSender", "CN=Alice, OU=Test Dept, O=R3, L=London, C=GB") + val resultSet = ledgerService.query("GET_MSG_FROM_ALICE", StateAndRef::class.java) .setCreatedTimestampLimit(Instant.now()).setLimit(1000) .execute()