@@ -30,7 +30,6 @@ import net.corda.core.node.services.vault.SortAttribute
3030import net.corda.core.schemas.StatePersistable
3131import kotlin.reflect.KProperty1
3232import 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}
0 commit comments