Search in sources :

Example 1 with EntityRelationsQuery

use of org.thingsboard.server.common.data.relation.EntityRelationsQuery in project thingsboard by thingsboard.

the class BaseRelationServiceTest method testCyclicRecursiveRelation.

@Test
public void testCyclicRecursiveRelation() throws ExecutionException, InterruptedException {
    // A -> B -> C -> A
    AssetId assetA = new AssetId(UUIDs.timeBased());
    AssetId assetB = new AssetId(UUIDs.timeBased());
    AssetId assetC = new AssetId(UUIDs.timeBased());
    EntityRelation relationA = new EntityRelation(assetA, assetB, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationB = new EntityRelation(assetB, assetC, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationC = new EntityRelation(assetC, assetA, EntityRelation.CONTAINS_TYPE);
    saveRelation(relationA);
    saveRelation(relationB);
    saveRelation(relationC);
    EntityRelationsQuery query = new EntityRelationsQuery();
    query.setParameters(new RelationsSearchParameters(assetA, EntitySearchDirection.FROM, -1));
    query.setFilters(Collections.singletonList(new EntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.singletonList(EntityType.ASSET))));
    List<EntityRelation> relations = relationService.findByQuery(query).get();
    Assert.assertEquals(3, relations.size());
    Assert.assertTrue(relations.contains(relationA));
    Assert.assertTrue(relations.contains(relationB));
    Assert.assertTrue(relations.contains(relationC));
}
Also used : EntityTypeFilter(org.thingsboard.server.common.data.relation.EntityTypeFilter) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) EntityRelationsQuery(org.thingsboard.server.common.data.relation.EntityRelationsQuery) RelationsSearchParameters(org.thingsboard.server.common.data.relation.RelationsSearchParameters) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Example 2 with EntityRelationsQuery

use of org.thingsboard.server.common.data.relation.EntityRelationsQuery in project thingsboard by thingsboard.

the class BaseAlarmService method createAlarmRelations.

private void createAlarmRelations(Alarm alarm) throws InterruptedException, ExecutionException {
    if (alarm.isPropagate()) {
        EntityRelationsQuery query = new EntityRelationsQuery();
        query.setParameters(new RelationsSearchParameters(alarm.getOriginator(), EntitySearchDirection.TO, Integer.MAX_VALUE));
        List<EntityId> parentEntities = relationService.findByQuery(query).get().stream().map(r -> r.getFrom()).collect(Collectors.toList());
        for (EntityId parentId : parentEntities) {
            createAlarmRelation(parentId, alarm.getId(), alarm.getStatus(), true);
        }
    }
    createAlarmRelation(alarm.getOriginator(), alarm.getId(), alarm.getStatus(), true);
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Autowired(org.springframework.beans.factory.annotation.Autowired) Tenant(org.thingsboard.server.common.data.Tenant) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) PreDestroy(javax.annotation.PreDestroy) Validator.validateId(org.thingsboard.server.dao.service.Validator.validateId) Service(org.springframework.stereotype.Service) org.thingsboard.server.common.data.alarm(org.thingsboard.server.common.data.alarm) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityService(org.thingsboard.server.dao.entity.EntityService) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) Function(com.google.common.base.Function) RelationsSearchParameters(org.thingsboard.server.common.data.relation.RelationsSearchParameters) Set(java.util.Set) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) TimePageData(org.thingsboard.server.common.data.page.TimePageData) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) TenantDao(org.thingsboard.server.dao.tenant.TenantDao) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) EntityRelationsQuery(org.thingsboard.server.common.data.relation.EntityRelationsQuery) StringUtils(org.springframework.util.StringUtils) EntityRelationsQuery(org.thingsboard.server.common.data.relation.EntityRelationsQuery) RelationsSearchParameters(org.thingsboard.server.common.data.relation.RelationsSearchParameters)

Example 3 with EntityRelationsQuery

use of org.thingsboard.server.common.data.relation.EntityRelationsQuery in project thingsboard by thingsboard.

the class AssetSearchQuery method toEntitySearchQuery.

public EntityRelationsQuery toEntitySearchQuery() {
    EntityRelationsQuery query = new EntityRelationsQuery();
    query.setParameters(parameters);
    query.setFilters(Collections.singletonList(new EntityTypeFilter(relationType == null ? EntityRelation.CONTAINS_TYPE : relationType, Collections.singletonList(EntityType.ASSET))));
    return query;
}
Also used : EntityTypeFilter(org.thingsboard.server.common.data.relation.EntityTypeFilter) EntityRelationsQuery(org.thingsboard.server.common.data.relation.EntityRelationsQuery)

Example 4 with EntityRelationsQuery

use of org.thingsboard.server.common.data.relation.EntityRelationsQuery in project thingsboard by thingsboard.

the class DeviceSearchQuery method toEntitySearchQuery.

public EntityRelationsQuery toEntitySearchQuery() {
    EntityRelationsQuery query = new EntityRelationsQuery();
    query.setParameters(parameters);
    query.setFilters(Collections.singletonList(new EntityTypeFilter(relationType == null ? EntityRelation.CONTAINS_TYPE : relationType, Collections.singletonList(EntityType.DEVICE))));
    return query;
}
Also used : EntityTypeFilter(org.thingsboard.server.common.data.relation.EntityTypeFilter) EntityRelationsQuery(org.thingsboard.server.common.data.relation.EntityRelationsQuery)

Example 5 with EntityRelationsQuery

use of org.thingsboard.server.common.data.relation.EntityRelationsQuery in project thingsboard by thingsboard.

the class BaseRelationServiceTest method testRecursiveRelation.

@Test
public void testRecursiveRelation() throws ExecutionException, InterruptedException {
    // A -> B -> [C,D]
    AssetId assetA = new AssetId(UUIDs.timeBased());
    AssetId assetB = new AssetId(UUIDs.timeBased());
    AssetId assetC = new AssetId(UUIDs.timeBased());
    DeviceId deviceD = new DeviceId(UUIDs.timeBased());
    EntityRelation relationAB = new EntityRelation(assetA, assetB, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationBC = new EntityRelation(assetB, assetC, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationBD = new EntityRelation(assetB, deviceD, EntityRelation.CONTAINS_TYPE);
    saveRelation(relationAB);
    saveRelation(relationBC);
    saveRelation(relationBD);
    EntityRelationsQuery query = new EntityRelationsQuery();
    query.setParameters(new RelationsSearchParameters(assetA, EntitySearchDirection.FROM, -1));
    query.setFilters(Collections.singletonList(new EntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.singletonList(EntityType.ASSET))));
    List<EntityRelation> relations = relationService.findByQuery(query).get();
    Assert.assertEquals(2, relations.size());
    Assert.assertTrue(relations.contains(relationAB));
    Assert.assertTrue(relations.contains(relationBC));
}
Also used : EntityTypeFilter(org.thingsboard.server.common.data.relation.EntityTypeFilter) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) EntityRelationsQuery(org.thingsboard.server.common.data.relation.EntityRelationsQuery) DeviceId(org.thingsboard.server.common.data.id.DeviceId) RelationsSearchParameters(org.thingsboard.server.common.data.relation.RelationsSearchParameters) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Aggregations

EntityRelationsQuery (org.thingsboard.server.common.data.relation.EntityRelationsQuery)5 EntityTypeFilter (org.thingsboard.server.common.data.relation.EntityTypeFilter)4 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)3 RelationsSearchParameters (org.thingsboard.server.common.data.relation.RelationsSearchParameters)3 Test (org.junit.Test)2 AssetId (org.thingsboard.server.common.data.id.AssetId)2 Function (com.google.common.base.Function)1 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 PostConstruct (javax.annotation.PostConstruct)1 PreDestroy (javax.annotation.PreDestroy)1