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"));
}
Aggregations