Querying Array concept in Composer - hyperledger

How to write a query for an attribute in the array concept.
Example: fetch the address based on city attribute.
namespace org.sample.basic
participant User identified by userId{
o String userId
o Address[] address optional
}
concept Address {
o String address1
o String address2
o String city
o String state
o Integer zipcode
o String country
o Boolean isAddressValidated default = false
}

given data of:
{ "$class": "org.sample.basic.User", "userId": "id1", "address": [ { $class": "org.sample.basic.Address", "address1": "the-vines", "city": "newyork" }, { $class": "org.sample.basic.Address", "address1": "the-gables", "city": "dallas" } ] } // and so on
your querydef (not posted all fields FYI) would be :
query myquery {
description: "Select all x" statement:
SELECT org.sample.basic.User
WHERE (address CONTAINS (city == "dallas") OR (city == "newyork") )
}

I hope you're using Hyperledger Query Language. Can you try something like this ?
SELECT org.sample.basic.User
WHERE ( address CONTAINS (city== _$city) )

Related

How to skip field in every object of list while mapping

I am trying to map Entity with ManyToMany relationship to DTO.
public class SkillEntity {
#Id
#GeneratedValue(generator = "system-uuid")
#GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;
private String name;
#ManyToMany(mappedBy = "skills")
private Set<UnifiedOfferEntity> unifiedOffers = new HashSet<>();
#Data
public class SkillDTO {
private String name;
private Set<UnifiedOfferDTO> unifiedOffers;
}
And I have problem with mapping that Set. I did custom mapper and it works, I am getting resposne like below but I want to skip "skills" field in UnifiedOfferDTO.
{
"name": "REACT NATIVE",
"unifiedOffers": [
{
"companyName": "QLOC S.A.",
"title": "Tester",
"skills": null,
},
{
"companyName": "Apptension sp. z o.o.",
"title": "Junior Frontend Developer",
"skills": null,
}
]
}
I created typeMap:
modelMapper.typeMap(SkillEntity.class, SkillDTO.class)
.addMappings(m -> m.using(skillEntityUnifiedOffersListConverter)
.map(skillEntity -> skillEntity, SkillDTO::setUnifiedOffers))
and i would like to skip that field "skills" from unifiedOfferDTO but do not know if I should add mapper.skip or something else.

How to query relationships of neo4j with custom query with cypher using Grandstack?

I have these models :
type User {
userId: ID!
mail: String!
password: String! #private
meals: [Meal!] #relationship(type: "IN_MEALS", direction: OUT)
}
type Meal {
name: String!
createdBy: User! #relationship(type: "IN_MEALS", direction: IN)
}
The relationship is made in neo4j and when I query all users but when I use this custom query for the custom users, meals are defined as null I don't know why
type Query {
currentUser: User
#cypher(
statement: """
MATCH (u:User {userId: $auth.jwt.userId})
OPTIONAL MATCH (u)-[r:IN_MEALS]->(m:Meal)
RETURN u,r,m
"""
)
}
When I do this query in graphql
{
currentUser {
userId
mail
meals {
name
}
}
}
It tells me that meals are null but they are not ...
{
"data": {
"currentUser": {
"userId": "02e7b50a-1a51-4d3b-b26b-57ed08d89c5a",
"mail": "mak#gmail.com",
"meals": null
}
}
}
I know that by looking into my neo4j database and also because when I query all users
{
users {
mail
meals {
name
}
}
}
I can see the values in meals :
{
"data": {
"users": [
{
"mail": "mak#gmail.com",
"meals": [
{
"name": "frites"
},
{
"name": "mcdo"
},
{
"name": "sushi"
}
]
}
]
}
}
From what I understand, you only need to provide the toplevel node and then the GRAND stack will handle fetching additional relationships etc. The documentation is not so clear unfortunately it seems. Try using:
type Query {
currentUser: User
#cypher(
statement: """
MATCH (u:User {userId: $auth.jwt.userId})
RETURN u
"""
)
}

Avro default value for user defined types

Is it possible to have a default value for a user defined type?
ie given the avdl:
protocol {
record A { }
record B {
union { A, string } foo = A;
}
}
record B is valid and thing by default is an instance of A?
Found the answer, {}
idl:
protocol {
record A { }
record B {
union { A, string } foo = {};
}
}
results in the avsc:
{
"type" : "record",
"name" : "B",
"fields" : [ {
"name" : "foo",
"type" : [ {
"type" : "record",
"name" : "A",
"fields" : [ ]
}, "string" ],
"default" : { }
} ]
}
This implies: new of the first type of the union, in this case A.

Grails how to JSONify domain object including its nested domain classes?

I am trying to do one to many domain mapping in grails.Here are the two classes :
class TNDetails {
String tn
String tnpk
static hasMany = [iccid: ICCID]
static mapping = {
table 'ni_tn'
version false
tnpk column : 'TN_PK'
tn column: 'TN'
id column: 'TN_PK',name: 'tnpk'
}
}
class ICCID {
String sim
String customer
static belongsTo = [tn: TNDetails]
static mapping = {
table 'ni_sim'
version false
sim column: 'ICCID'
customer column: 'CUSTOMER'
tn column: 'TN_FK'
id column: 'SIM_PK'
}
}
The corresponding query can be written as : select TN,ICCID from ni_tn,ni_sim where ni_tn.TN_PK = ni_sim.RELATED_TN and tn_pk=1290.Now in my controller when i am fetch the details by passing tn_pk like this :
def index() {
def pk = params.tnPK
def details = TNDetails.findAll {
(tnpk == pk)
}
respond details
}
i get following result :
[
{
"class": "com.evolving.resource.tn.TNDetails",
"id": 1290,
"tnpk": "1290",
"iccid": [
{
"class": "com.evolving.resource.iccid.ICCID",
"id": 4209
}
],
"tn": "447400002035"
}
]
Now the problem here is it is not displaying the attributes sim and customer(from class ICCID).How do i display these two parameters also.What am i doing wrong here?
change your:
respond details
to:
JSON.use("deep") {
respond details as JSON
}
The Deep Converters fully render the associations (nested domainclass instances) and also handle circular relations (documentation)

HasOne and HasMany of the same domain class and cascade save

I have 2 domain classes; the forst of them represents an email address, with the name of the user and the email address itself. The other class represents an email message, with the subject, the sender (one email address) and the recipients (a list of email addresses):
class EmailMessage {
static hasMany = [ to: EmailAddress ]
EmailAddress from
String subject
}
class EmailAddress {
String name
String address
}
But this code doesn't work as I expect:
EmailMessage msg = new EmailMessage()
msg.from = new EmailAddress(name: "User 1", address: "user1#domain.com")
[new EmailAddress(name: "User 2", address: "user2#domain.com"), new EmailAddress(name: "User 3", address: "user3#domain.com")].each {
msg.addToTo(it)
}
msg.subject = "Asunto"
msg.save(failOnError: true)
I get this error:
| Error 2013-08-14 21:08:40,362 [localhost-startStop-1] ERROR util.JDBCExceptionReporter - La columna "FROM_ID" no permite valores nulos (NULL)
NULL not allowed for column "FROM_ID"; SQL statement: insert into email_message (id, version, from_id, subject) values (null, ?, ?, ?) [23502-164]
| Error 2013-08-14 21:08:40,375 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: could not insert: [prueba.EmailMessage];
SQL [insert into email_message (id, version, from_id, subject) values (null, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [prueba.EmailMessage]
Message: could not insert: [prueba.EmailMessage]; SQL [insert into email_message (id, version, from_id, subject) values (null, ?, ?, ?)]; constraint [null];
nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [prueba.EmailMessage]
I don't know if this even can be done, or the cascade save doesn't work as I suppose.
I thik I have tried all combinations of hasMany, belongsTo, etc. in each class, without success.
I have also read this topic but it doesn't work as expected Grails hasOne and hasMany same domain.
Could any one help me? Regards and thanks in advance.
This works for me in the standard memory database
class EmailMessage {
static hasMany = [ to: EmailAddress ]
EmailAddress from
String subject
static mapping = {
from cascade: "save-update" //Pretty sure this is unnecessary
}
}
class EmailAddress {
String name
String address
static belongsTo = EmailMessage
}
Unfortunately you have to save your from before you save the parent.
EmailMessage msg = new EmailMessage()
msg.from = new EmailAddress(name: "User 1", address: "user1#domain.com")
if (!msg.from.save(flush: true)) {
log.error("message could not be saved: " + msg.from.errors)
}
[new EmailAddress(name: "User 2", address: "user2#domain.com"), new EmailAddress(name: "User 3", address: "user3#domain.com")].each {
msg.addToTo(it)
}
msg.subject = "Asunto"
msg.save(failOnError: true)
There is no cascading save in a 1:1 relation.

Resources