diff --git a/kotlin-samples/advanceCustomQuery/contracts/src/main/kotlin/com/r3/developers/advanceCustomQuery/states/CustomChatQuery.kt b/kotlin-samples/advanceCustomQuery/contracts/src/main/kotlin/com/r3/developers/advanceCustomQuery/states/CustomChatQuery.kt index 477a8a7..4df50ae 100644 --- a/kotlin-samples/advanceCustomQuery/contracts/src/main/kotlin/com/r3/developers/advanceCustomQuery/states/CustomChatQuery.kt +++ b/kotlin-samples/advanceCustomQuery/contracts/src/main/kotlin/com/r3/developers/advanceCustomQuery/states/CustomChatQuery.kt @@ -10,17 +10,24 @@ import net.corda.v5.ledger.utxo.query.registration.VaultNamedQueryBuilderFactory class ChatCustomQueryFactory : VaultNamedQueryFactory { override fun create(vaultNamedQueryBuilderFactory: VaultNamedQueryBuilderFactory) { - vaultNamedQueryBuilderFactory.create("GET_ALL_MSG") //used only in the other "simple" sample + + //Returns all the chat states that stores in the vNode + //(not used in this sample app, used in the basic custom query app) + vaultNamedQueryBuilderFactory.create("GET_ALL_MSG") .whereJson( "WHERE visible_states.custom_representation ? 'com.r3.developers.advanceCustomQuery.states.ChatState' " ) .register() - vaultNamedQueryBuilderFactory.create("GET_MSG_FROM") //used only in the other "simple" sample + //Returns all the chat states that stores in the vNode that were sent from a particular sender. + //(not used in this sample app, used in the basic custom query app) + vaultNamedQueryBuilderFactory.create("GET_MSG_FROM") .whereJson( "WHERE visible_states.custom_representation -> 'com.r3.developers.advanceCustomQuery.states.ChatState' ->> 'messageContentFrom' = :nameOfSender" ) .register() + + //Returns all the chat states that their messages contains the word "hello" via filter feature vaultNamedQueryBuilderFactory.create("GET_MSGS_CONTAINING_HELLO") .whereJson( "WHERE visible_states.custom_representation ? 'com.r3.developers.advanceCustomQuery.states.ChatState' " @@ -28,6 +35,7 @@ class ChatCustomQueryFactory : VaultNamedQueryFactory { .filter(CustomQueryFilter()) //applies the filter to this query .register() + //Returns only the chat messages of all chat entries stored in the vNode via transformer feature vaultNamedQueryBuilderFactory.create("GET_ALL_MSGS_CONTENT") .whereJson( "WHERE visible_states.custom_representation ? 'com.r3.developers.advanceCustomQuery.states.ChatState' " @@ -35,6 +43,7 @@ class ChatCustomQueryFactory : VaultNamedQueryFactory { .map(CustomQueryTransformer()) //applies the transformer to this query .register() + //Returns the total amount of chat entries stored in the vNode via collector feature vaultNamedQueryBuilderFactory.create("GET_MSG_AMOUNT") .whereJson( "WHERE visible_states.custom_representation ? 'com.r3.developers.advanceCustomQuery.states.ChatState' " @@ -42,6 +51,8 @@ class ChatCustomQueryFactory : VaultNamedQueryFactory { .collect(CustomQueryCollector()) //applies the collector to this query .register() + //Returns the total amount of chat entries that has the word "hello" in the chat message + //via filter + collector feature vaultNamedQueryBuilderFactory.create("GET_MSG_AMOUNT_HAS_HELLO") .whereJson( "WHERE visible_states.custom_representation ? 'com.r3.developers.advanceCustomQuery.states.ChatState' " @@ -50,6 +61,8 @@ class ChatCustomQueryFactory : VaultNamedQueryFactory { .collect(CustomQueryCollector()) //applies the collector after the filter has been applied .register() + //Returns only the chat messages of all chat entries that has the word "hello" in the chat message + //via filter + transformer feature vaultNamedQueryBuilderFactory.create("GET_ALL_MSGS_CONTENT_HAS_HELLO") .whereJson( "WHERE visible_states.custom_representation ? 'com.r3.developers.advanceCustomQuery.states.ChatState' " @@ -61,30 +74,32 @@ class ChatCustomQueryFactory : VaultNamedQueryFactory { } -// for filtering states of type ChatState. +//Filtering states of type ChatState. class CustomQueryFilter : VaultNamedQueryStateAndRefFilter { + + // It returns true if the 'message' field in the state's contractState is lowercase and contains "hello". override fun filter(data: StateAndRef, parameters: MutableMap): Boolean { - return data.state.contractState.message.lowercase().contains("hello") // It returns true if the 'message' field in the state's contractState is lowercase and contains "hello". - } //return type is a boolean that will be checking for which entry passes and which fails. The query return type is still a StateAndRef + return data.state.contractState.message.lowercase().contains("hello") + } } -// for transforming states of type ChatState into a String. +//Transforming states of type ChatState into a String. class CustomQueryTransformer : VaultNamedQueryStateAndRefTransformer { + // It returns the 'message' field from the state's contractState. override fun transform(data: StateAndRef, parameters: MutableMap): String { return data.state.contractState.message - } //return type is a String. This also alters the return type for the query to a String. Ensure that the query serializes the result to String + } } -// to collect transformed data into a final result. It operates on String inputs and produces an Int result. +//Collect resultSet data into a final result. It operates on String inputs and produces an Int result. class CustomQueryCollector : VaultNamedQueryCollector { + // It returns a result containing the size of the resultSet override fun collect(resultSet: MutableList, parameters: MutableMap): VaultNamedQueryCollector.Result { return VaultNamedQueryCollector.Result(listOf(resultSet.size), true) } - //return type is a int. This also alters the return type for the query to a int. Ensure that the query serializes the result to integer - }