Skip to content

Commit

Permalink
Update CustomChatQuery.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
peterli-r3 committed May 15, 2024
1 parent d327cc4 commit 00166e5
Showing 1 changed file with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,49 @@ 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' "
)
.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' "
)
.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' "
)
.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' "
Expand All @@ -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' "
Expand All @@ -61,30 +74,32 @@ class ChatCustomQueryFactory : VaultNamedQueryFactory {
}


// for filtering states of type ChatState.
//Filtering states of type ChatState.
class CustomQueryFilter : VaultNamedQueryStateAndRefFilter<ChatState> {

// It returns true if the 'message' field in the state's contractState is lowercase and contains "hello".
override fun filter(data: StateAndRef<ChatState>, parameters: MutableMap<String, Any>): 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<ChatState, String> {

// It returns the 'message' field from the state's contractState.
override fun transform(data: StateAndRef<ChatState>, parameters: MutableMap<String, Any>): 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<String, Int> {

// It returns a result containing the size of the resultSet
override fun collect(resultSet: MutableList<String>, parameters: MutableMap<String, Any>):
VaultNamedQueryCollector.Result<Int> {
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

}

0 comments on commit 00166e5

Please sign in to comment.