In-mem constraints example

Hello, I am developing a CUBA app (I am a CUBA license owner).

Currently I have a database, where I have SkillRequest which has association to Supplier which has association with Users.

I need to create a group constraint which will filter only those SkillRequests, which are linked to Supplier who is linked to the current user.

To create a database constraint was easy:

Join:


join {E}.supplier s
join s.users u

Where:


u.id = :session$userId

But I have problems to create the corresponding Groovy script.

I must use in mem checking, because I have nested datasources which has to be checked.

Thank you,

Delyan

Hi,
I tried to adjust the following constraints in my test project, and they worked.
If supplier has a link to one user the script might be:

def result = false
if ({E}.supplier != null){ 
    result = ({E}.supplier.user == userSession.user)
    }
return result

If a supplier has collection of users (many_to_many association):

def result = false
if ({E}.supplier!= null){ 
    result = ({E}.supplier.users.contains(userSession.user))
    }
return result

And if both relations have many_to_many cardinality:

def result = false
def suppliers = {E}.suppliers

if(suppliers !=null){

        for (item in suppliers) {
            if (item.users != null && item.users.contains(userSession.user)) {
                result = true
                break
            }
        }
}
return result

Regards.