Mario Granero
Mario Granero

Reputation: 21

Retrieving info with linearId (simple query to a Corda node)

We are building a basic PoC example with Corda. Right now we have a basic Cordapp that send a String message from one node to another.

That works fine but next steps are to retrieve one message using the api.

We have this path to retrieve all, but we need just one.

@GET
@Path("cases")
@Produces(MediaType.APPLICATION_JSON)
public List<StateAndRef<CaseState>> getCases();
    return rpcOps.vaultQuery(CaseState.class).getStates();
}

We have already tried like this:

@GET
@Path("cases/{caseId}")
@Produces(MediaType.APPLICATION_JSON)
public StateAndRef<CaseState> getCase(@PathParam("caseId") String caseId) throws InterruptedException, ExecutionException  {

    UniqueIdentifier id = new UniqueIdentifier.fromString("caseId");
    QueryCriteria criteria = new QueryCriteria.LinearStateQueryCriteria(null, InmutableList.of(id), Vault.StateStatus.UNCONSUMED, null);
    return rpcOps.vaultQueryBy(CaseState.class).queryBy(criteria).getStates().get(0);

}

Can you help me?

Upvotes: 2

Views: 1216

Answers (3)

XP.
XP.

Reputation: 52

You can use something like this

UUID linearId = "2be921da-5a79-4513-8cb3-7b87ea9307cf";

QueryCriteria criteria = new QueryCriteria.LinearStateQueryCriteria( null, Arrays.asList(linearId), null, Vault.StateStatus.UNCONSUMED, null);

Upvotes: 0

Joel
Joel

Reputation: 23140

Trying to reconstruct the UniqueIdentifier from the externalId won't work, as you don't know what the UUID is.

The full constructor for LinearStateQueryCriteria is:

data class LinearStateQueryCriteria @JvmOverloads constructor(
    val participants: List<AbstractParty>? = null,
    val uuid: List<UUID>? = null,
    val externalId: List<String>? = null,
    override val status: Vault.StateStatus = Vault.StateStatus.UNCONSUMED,
    override val contractStateTypes: Set<Class<out ContractState>>? = null)

So you need to use the full constructor and query the vault using the following criteria:

QueryCriteria criteria = new QueryCriteria.LinearStateQueryCriteria(
        null, 
        null, 
        ImmutableList.of("caseId"), 
        Vault.StateStatus.UNCONSUMED, 
        null);

Note how here, we're not specifying the UUID, but we are specifying the externalId.

Upvotes: 0

Raymond Mogg
Raymond Mogg

Reputation: 192

I have done this in my Cordapps by querying by unconsumed states of the wanted type and then simply filtering the returned result to get what is needed. For example, the following is an example of a Account state being filtered by linearID (as needed in your example)

    //Query the vault for unconsumed states and then for account states
    val criteria = QueryCriteria.VaultQueryCriteria(status = Vault.StateStatus.UNCONSUMED)
    val customerStates = serviceHub.vaultService.queryBy<Account.State>(criteria)
    //Filter the customer states to find a matching linearId
    val filteredStates = customerStates.states.filter {
        it.state.data.linearId == linearId
    }

Hopefully this helps!

Upvotes: 2

Related Questions