Search code examples
javaxodus

Retrieve Entities based on logic


I have this code:

EntityIterable iterable = null;
                    if(authId== null) {
                        iterable = txn.find(entityType, "publicRead", true).skip(skip).take(limit);
                    } else {
                        iterable = txn.getAll(entityType)
                                .union(txn.find(entityType, "read(" + authId+ ")", true))
                                .union(txn.find(entityType, "publicRead", false))
                                .union(txn.find(entityType, "publicRead" ,true)).skip(skip).take(limit);
                    }
}

I'm trying to figure out a way to be able to get results based on this logic:

  • If publicRead is true then return all Entities that have the property set to true (trivia)

The problems is this:

  • If authId is present then retrieve all Entities with publicRead = false && read(userIdauthIdRoleId) = true or publicRead = true && read(authId) = true

How can this be achieved with the Xodus API?


Solution

  • This can be achieved in the following way:

    EntityIterable publicRead = txn.find(entityType, "publicRead", true);
    
    EntityIterable result;
    
    if (authId == null) {
        result = publicRead;
    } else {
        result =
            txn.getAll(entityType).minus(publicRead).intersect(txn.find(entityType, "read(userIdauthIdRoleId)", true))
            .union(publicRead.intersect(txn.find(entityType, "read(" + authId + ")", true)));
    }
    
    result = result.skip(skip).take(limit);