Skip to content

Commit f7d6ed8

Browse files
Updated vault query API to address bugs.
1 parent 0d1fe1f commit f7d6ed8

File tree

6 files changed

+91
-25
lines changed

6 files changed

+91
-25
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ buildscript {
4242
}
4343

4444
group 'io.onixlabs'
45-
version '2.0.0-rc4'
45+
version '2.0.0-rc5'
4646

4747
subprojects {
4848
repositories {

onixlabs-corda-core-workflow/src/main/kotlin/io/onixlabs/corda/core/services/Extensions.CordaRPCOps.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,23 @@ package io.onixlabs.corda.core.services
1919
import net.corda.core.contracts.ContractState
2020
import net.corda.core.messaging.CordaRPCOps
2121

22+
/**
23+
* Creates a vault service, bound to the specified [ContractState] type.
24+
*
25+
* @param T The underlying [ContractState] type to bind the vault service to.
26+
* @param contractStateType The [Class] of the [ContractState] type to bind the vault service to.
27+
* @return Returns a [VaultService] instance bound to the underlying [ContractState] type.
28+
*/
29+
fun <T : ContractState> CordaRPCOps.vaultServiceFor(contractStateType: Class<T>): VaultService<T> {
30+
return VaultService.create(this, contractStateType)
31+
}
32+
2233
/**
2334
* Creates a vault service, bound to the specified [ContractState] type.
2435
*
2536
* @param T The underlying [ContractState] type to bind the vault service to.
2637
* @return Returns a [VaultService] instance bound to the underlying [ContractState] type.
2738
*/
2839
inline fun <reified T : ContractState> CordaRPCOps.vaultServiceFor(): VaultService<T> {
29-
return VaultService.create(this)
40+
return vaultServiceFor(T::class.java)
3041
}

onixlabs-corda-core-workflow/src/main/kotlin/io/onixlabs/corda/core/services/Extensions.QueryDsl.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import net.corda.core.node.services.vault.Sort
3333
* @param sort The sorting which will be applied to the query.
3434
* @return Returns the [QueryCriteria] that was created using the Query DSL.
3535
*/
36+
@QueryDslContext
3637
fun <T : ContractState> vaultQuery(
3738
contractStateType: Class<T>,
3839
stateStatus: Vault.StateStatus = Vault.StateStatus.UNCONSUMED,
@@ -60,6 +61,7 @@ fun <T : ContractState> vaultQuery(
6061
* @param sort The sorting which will be applied to the query.
6162
* @return Returns the [QueryCriteria] that was created using the Query DSL.
6263
*/
64+
@QueryDslContext
6365
inline fun <reified T : ContractState> vaultQuery(
6466
stateStatus: Vault.StateStatus = Vault.StateStatus.UNCONSUMED,
6567
relevancyStatus: Vault.RelevancyStatus = Vault.RelevancyStatus.RELEVANT,

onixlabs-corda-core-workflow/src/main/kotlin/io/onixlabs/corda/core/services/Extensions.ServiceHub.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,23 @@ package io.onixlabs.corda.core.services
1919
import net.corda.core.contracts.ContractState
2020
import net.corda.core.node.ServiceHub
2121

22+
/**
23+
* Creates a vault service, bound to the specified [ContractState] type.
24+
*
25+
* @param T The underlying [ContractState] type to bind the vault service to.
26+
* @param contractStateType The [Class] of the [ContractState] type to bind the vault service to.
27+
* @return Returns a [VaultService] instance bound to the underlying [ContractState] type.
28+
*/
29+
fun <T : ContractState> ServiceHub.vaultServiceFor(contractStateType: Class<T>): VaultService<T> {
30+
return VaultService.create(this, contractStateType)
31+
}
32+
2233
/**
2334
* Creates a vault service, bound to the specified [ContractState] type.
2435
*
2536
* @param T The underlying [ContractState] type to bind the vault service to.
2637
* @return Returns a [VaultService] instance bound to the underlying [ContractState] type.
2738
*/
2839
inline fun <reified T : ContractState> ServiceHub.vaultServiceFor(): VaultService<T> {
29-
return VaultService.create(this)
40+
return vaultServiceFor(T::class.java)
3041
}

onixlabs-corda-core-workflow/src/main/kotlin/io/onixlabs/corda/core/services/QueryDsl.kt

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import net.corda.core.node.services.vault.SortAttribute
3030
import net.corda.core.schemas.StatePersistable
3131
import kotlin.reflect.KProperty1
3232
import kotlin.reflect.jvm.javaType
33-
import kotlin.reflect.jvm.jvmErasure
3433

3534
/**
3635
* Represents a DSL for building vault queries.
@@ -48,15 +47,16 @@ class QueryDsl<T : ContractState> internal constructor(
4847
val criteria: QueryCriteria get() = queryCriteria
4948
val paging: PageSpecification get() = page
5049
val sorting: Sort get() = sort
50+
private val vaultCriteria: VaultQueryCriteria get() = queryCriteria as VaultQueryCriteria
5151

5252
/**
5353
* Specifies the state status of the query.
5454
*
5555
* @param status The status of the [ContractState] instances to apply to the query.
5656
*/
5757
@QueryDslContext
58-
fun status(status: Vault.StateStatus) {
59-
queryCriteria = (queryCriteria as VaultQueryCriteria).withStatus(status)
58+
fun stateStatus(status: Vault.StateStatus) {
59+
queryCriteria = vaultCriteria.withStatus(status)
6060
}
6161

6262
/**
@@ -66,7 +66,7 @@ class QueryDsl<T : ContractState> internal constructor(
6666
*/
6767
@QueryDslContext
6868
fun contractStateTypes(contractStateTypes: Set<Class<out T>>) {
69-
queryCriteria = (queryCriteria as VaultQueryCriteria).withContractStateTypes(contractStateTypes)
69+
queryCriteria = vaultCriteria.withContractStateTypes(contractStateTypes)
7070
}
7171

7272
/**
@@ -86,7 +86,7 @@ class QueryDsl<T : ContractState> internal constructor(
8686
*/
8787
@QueryDslContext
8888
fun stateRefs(stateRefs: List<StateRef>) {
89-
queryCriteria = (queryCriteria as VaultQueryCriteria).withStateRefs(stateRefs)
89+
queryCriteria = vaultCriteria.withStateRefs(stateRefs)
9090
}
9191

9292
/**
@@ -106,7 +106,7 @@ class QueryDsl<T : ContractState> internal constructor(
106106
*/
107107
@QueryDslContext
108108
fun notaries(notaries: List<AbstractParty>) {
109-
queryCriteria = (queryCriteria as VaultQueryCriteria).withNotary(notaries)
109+
queryCriteria = vaultCriteria.withNotary(notaries)
110110
}
111111

112112
/**
@@ -126,7 +126,7 @@ class QueryDsl<T : ContractState> internal constructor(
126126
*/
127127
@QueryDslContext
128128
fun softLockingCondition(softLockingCondition: SoftLockingCondition) {
129-
queryCriteria = (queryCriteria as VaultQueryCriteria).withSoftLockingCondition(softLockingCondition)
129+
queryCriteria = vaultCriteria.withSoftLockingCondition(softLockingCondition)
130130
}
131131

132132
/**
@@ -136,7 +136,7 @@ class QueryDsl<T : ContractState> internal constructor(
136136
*/
137137
@QueryDslContext
138138
fun timeCondition(timeCondition: TimeCondition) {
139-
queryCriteria = (queryCriteria as VaultQueryCriteria).withTimeCondition(timeCondition)
139+
queryCriteria = vaultCriteria.withTimeCondition(timeCondition)
140140
}
141141

142142
/**
@@ -146,7 +146,7 @@ class QueryDsl<T : ContractState> internal constructor(
146146
*/
147147
@QueryDslContext
148148
fun relevancyStatus(relevancyStatus: Vault.RelevancyStatus) {
149-
queryCriteria = (queryCriteria as VaultQueryCriteria).withRelevancyStatus(relevancyStatus)
149+
queryCriteria = vaultCriteria.withRelevancyStatus(relevancyStatus)
150150
}
151151

152152
/**
@@ -156,7 +156,7 @@ class QueryDsl<T : ContractState> internal constructor(
156156
*/
157157
@QueryDslContext
158158
fun constraintTypes(constraintTypes: Set<Vault.ConstraintInfo.Type>) {
159-
queryCriteria = (queryCriteria as VaultQueryCriteria).withConstraintTypes(constraintTypes)
159+
queryCriteria = vaultCriteria.withConstraintTypes(constraintTypes)
160160
}
161161

162162
/**
@@ -176,7 +176,7 @@ class QueryDsl<T : ContractState> internal constructor(
176176
*/
177177
@QueryDslContext
178178
fun constraints(constraints: Set<Vault.ConstraintInfo>) {
179-
queryCriteria = (queryCriteria as VaultQueryCriteria).withConstraints(constraints)
179+
queryCriteria = vaultCriteria.withConstraints(constraints)
180180
}
181181

182182
/**
@@ -196,7 +196,7 @@ class QueryDsl<T : ContractState> internal constructor(
196196
*/
197197
@QueryDslContext
198198
fun participants(participants: List<AbstractParty>) {
199-
queryCriteria = (queryCriteria as VaultQueryCriteria).withParticipants(participants)
199+
queryCriteria = vaultCriteria.withParticipants(participants)
200200
}
201201

202202
/**
@@ -216,7 +216,7 @@ class QueryDsl<T : ContractState> internal constructor(
216216
*/
217217
@QueryDslContext
218218
fun linearIds(linearIds: List<UniqueIdentifier>) {
219-
queryCriteria = queryCriteria.and(LinearStateQueryCriteria(linearId = linearIds))
219+
queryCriteria = queryCriteria.and(LinearStateQueryCriteria(linearId = linearIds).withRootCriteria())
220220
}
221221

222222
/**
@@ -229,14 +229,34 @@ class QueryDsl<T : ContractState> internal constructor(
229229
linearIds(linearIds.toList())
230230
}
231231

232+
/**
233+
* Specifies the external identifiers to apply to the query criteria.
234+
*
235+
* @param externalIds The [String] instances to apply to the query criteria.
236+
*/
237+
@QueryDslContext
238+
fun externalIds(externalIds: List<String?>) {
239+
queryCriteria = queryCriteria.and(LinearStateQueryCriteria(externalId = externalIds.filterNotNull()))
240+
}
241+
242+
/**
243+
* Specifies the external identifiers to apply to the query criteria.
244+
*
245+
* @param externalIds The [String] instances to apply to the query criteria.
246+
*/
247+
@QueryDslContext
248+
fun externalIds(vararg externalIds: String?) {
249+
externalIds(externalIds.toList())
250+
}
251+
232252
/**
233253
* Specifies custom query criteria to apply to the parent query criteria.
234254
*
235255
* @param criteria The custom criteria to apply to the parent query criteria.
236256
*/
237257
@QueryDslContext
238258
fun where(criteria: QueryCriteria) {
239-
queryCriteria = queryCriteria.and(criteria)
259+
queryCriteria = queryCriteria.and(criteria.withRootCriteria())
240260
}
241261

242262
/**
@@ -248,7 +268,7 @@ class QueryDsl<T : ContractState> internal constructor(
248268
fun and(action: QueryDsl<T>.() -> Unit) {
249269
val queryDsl = QueryDsl<T>()
250270
action(queryDsl)
251-
queryCriteria = queryCriteria.and(queryDsl.criteria)
271+
queryCriteria = queryCriteria.and(queryDsl.criteria.withRootCriteria())
252272
}
253273

254274
/**
@@ -260,7 +280,7 @@ class QueryDsl<T : ContractState> internal constructor(
260280
fun or(action: QueryDsl<T>.() -> Unit) {
261281
val queryDsl = QueryDsl<T>()
262282
action(queryDsl)
263-
queryCriteria = queryCriteria.or(queryDsl.criteria)
283+
queryCriteria = queryCriteria.or(queryDsl.criteria.withRootCriteria())
264284
}
265285

266286
/**
@@ -327,4 +347,26 @@ class QueryDsl<T : ContractState> internal constructor(
327347
fun sortByDescending(property: KProperty1<out StatePersistable, *>) {
328348
sortBy(property, Sort.Direction.DESC)
329349
}
350+
351+
/**
352+
* Copies properties of the root criteria to any sub-criteria.
353+
*/
354+
private fun QueryCriteria.withRootCriteria(): QueryCriteria = when (this) {
355+
is VaultQueryCriteria -> copy(
356+
contractStateTypes = vaultCriteria.contractStateTypes,
357+
relevancyStatus = vaultCriteria.relevancyStatus,
358+
status = vaultCriteria.status
359+
)
360+
is LinearStateQueryCriteria -> copy(
361+
contractStateTypes = vaultCriteria.contractStateTypes,
362+
relevancyStatus = vaultCriteria.relevancyStatus,
363+
status = vaultCriteria.status
364+
)
365+
is VaultCustomQueryCriteria<*> -> copy(
366+
contractStateTypes = vaultCriteria.contractStateTypes,
367+
relevancyStatus = vaultCriteria.relevancyStatus,
368+
status = vaultCriteria.status
369+
)
370+
else -> this
371+
}
330372
}

onixlabs-corda-core-workflow/src/main/kotlin/io/onixlabs/corda/core/services/VaultService.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ class VaultService<T : ContractState> private constructor(
4444
* Creates a vault service from a [CordaRPCOps] instance.
4545
*
4646
* @param cordaRPCOps The [CordaRPCOps] instance from which to create a [VaultService] instance.
47-
* @param contractStateClass The [Class] of the [ContractState] type to bind the vault service to.
47+
* @param contractStateType The [Class] of the [ContractState] type to bind the vault service to.
4848
*/
49-
fun <T : ContractState> create(cordaRPCOps: CordaRPCOps, contractStateClass: Class<T>): VaultService<T> {
50-
return VaultService(VaultAdapterCordaRPCOps(cordaRPCOps, contractStateClass), contractStateClass)
49+
fun <T : ContractState> create(cordaRPCOps: CordaRPCOps, contractStateType: Class<T>): VaultService<T> {
50+
return VaultService(VaultAdapterCordaRPCOps(cordaRPCOps, contractStateType), contractStateType)
5151
}
5252

5353
/**
@@ -64,10 +64,10 @@ class VaultService<T : ContractState> private constructor(
6464
* Creates a vault service from a [ServiceHub] instance.
6565
*
6666
* @param serviceHub The [ServiceHub] instance from which to create a [VaultService] instance.
67-
* @param contractStateClass The [Class] of the [ContractState] type to bind the vault service to.
67+
* @param contractStateType The [Class] of the [ContractState] type to bind the vault service to.
6868
*/
69-
fun <T : ContractState> create(serviceHub: ServiceHub, contractStateClass: Class<T>): VaultService<T> {
70-
return VaultService(VaultAdapterServiceHub(serviceHub, contractStateClass), contractStateClass)
69+
fun <T : ContractState> create(serviceHub: ServiceHub, contractStateType: Class<T>): VaultService<T> {
70+
return VaultService(VaultAdapterServiceHub(serviceHub, contractStateType), contractStateType)
7171
}
7272

7373
/**

0 commit comments

Comments
 (0)