Search in sources :

Example 1 with RelationService

use of org.thingsboard.server.dao.relation.RelationService in project thingsboard by thingsboard.

the class EntitiesRelatedEntityIdAsyncLoader method findEntityAsync.

public static ListenableFuture<EntityId> findEntityAsync(TbContext ctx, EntityId originator, RelationsQuery relationsQuery) {
    RelationService relationService = ctx.getRelationService();
    EntityRelationsQuery query = buildQuery(originator, relationsQuery);
    ListenableFuture<List<EntityRelation>> asyncRelation = relationService.findByQuery(ctx.getTenantId(), query);
    if (relationsQuery.getDirection() == EntitySearchDirection.FROM) {
        return Futures.transformAsync(asyncRelation, r -> CollectionUtils.isNotEmpty(r) ? Futures.immediateFuture(r.get(0).getTo()) : Futures.immediateFuture(null), MoreExecutors.directExecutor());
    } else if (relationsQuery.getDirection() == EntitySearchDirection.TO) {
        return Futures.transformAsync(asyncRelation, r -> CollectionUtils.isNotEmpty(r) ? Futures.immediateFuture(r.get(0).getFrom()) : Futures.immediateFuture(null), MoreExecutors.directExecutor());
    }
    return Futures.immediateFailedFuture(new IllegalStateException("Unknown direction"));
}
Also used : RelationService(org.thingsboard.server.dao.relation.RelationService) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) RelationsQuery(org.thingsboard.rule.engine.data.RelationsQuery) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) RelationsSearchParameters(org.thingsboard.server.common.data.relation.RelationsSearchParameters) TbContext(org.thingsboard.rule.engine.api.TbContext) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RelationService(org.thingsboard.server.dao.relation.RelationService) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) CollectionUtils(org.apache.commons.collections.CollectionUtils) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelationsQuery(org.thingsboard.server.common.data.relation.EntityRelationsQuery) EntityRelationsQuery(org.thingsboard.server.common.data.relation.EntityRelationsQuery) List(java.util.List)

Aggregations

Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 List (java.util.List)1 CollectionUtils (org.apache.commons.collections.CollectionUtils)1 TbContext (org.thingsboard.rule.engine.api.TbContext)1 RelationsQuery (org.thingsboard.rule.engine.data.RelationsQuery)1 EntityId (org.thingsboard.server.common.data.id.EntityId)1 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)1 EntityRelationsQuery (org.thingsboard.server.common.data.relation.EntityRelationsQuery)1 EntitySearchDirection (org.thingsboard.server.common.data.relation.EntitySearchDirection)1 RelationsSearchParameters (org.thingsboard.server.common.data.relation.RelationsSearchParameters)1 RelationService (org.thingsboard.server.dao.relation.RelationService)1