Search in sources :

Example 11 with EntityRelation

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

the class DeviceServiceImpl method findDevicesByQuery.

@Override
public ListenableFuture<List<Device>> findDevicesByQuery(DeviceSearchQuery query) {
    ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(query.toEntitySearchQuery());
    ListenableFuture<List<Device>> devices = Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<Device>>) relations1 -> {
        EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
        List<ListenableFuture<Device>> futures = new ArrayList<>();
        for (EntityRelation relation : relations1) {
            EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
            if (entityId.getEntityType() == EntityType.DEVICE) {
                futures.add(findDeviceByIdAsync(new DeviceId(entityId.getId())));
            }
        }
        return Futures.successfulAsList(futures);
    });
    devices = Futures.transform(devices, new Function<List<Device>, List<Device>>() {

        @Nullable
        @Override
        public List<Device> apply(@Nullable List<Device> deviceList) {
            return deviceList == null ? Collections.emptyList() : deviceList.stream().filter(device -> query.getDeviceTypes().contains(device.getType())).collect(Collectors.toList());
        }
    });
    return devices;
}
Also used : java.util(java.util) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Cache(org.springframework.cache.Cache) Cacheable(org.springframework.cache.annotation.Cacheable) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Autowired(org.springframework.beans.factory.annotation.Autowired) CacheEvict(org.springframework.cache.annotation.CacheEvict) TextPageData(org.thingsboard.server.common.data.page.TextPageData) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) org.thingsboard.server.common.data(org.thingsboard.server.common.data) CacheManager(org.springframework.cache.CacheManager) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) Nullable(javax.annotation.Nullable) DEVICE_CACHE(org.thingsboard.server.common.data.CacheConstants.DEVICE_CACHE) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Function(com.google.common.base.Function) DeviceCredentialsType(org.thingsboard.server.common.data.security.DeviceCredentialsType) DaoUtil.toUUIDs(org.thingsboard.server.dao.DaoUtil.toUUIDs) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Validator(org.thingsboard.server.dao.service.Validator) Collectors(java.util.stream.Collectors) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) CustomerDao(org.thingsboard.server.dao.customer.CustomerDao) PaginatedRemover(org.thingsboard.server.dao.service.PaginatedRemover) TenantDao(org.thingsboard.server.dao.tenant.TenantDao) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) DeviceSearchQuery(org.thingsboard.server.common.data.device.DeviceSearchQuery) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) StringUtils(org.springframework.util.StringUtils) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Function(com.google.common.base.Function) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Nullable(javax.annotation.Nullable)

Example 12 with EntityRelation

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

the class CassandraDashboardInfoDao method findDashboardsByTenantIdAndCustomerId.

@Override
public ListenableFuture<List<DashboardInfo>> findDashboardsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TimePageLink pageLink) {
    log.debug("Try to find dashboards by tenantId [{}], customerId[{}] and pageLink [{}]", tenantId, customerId, pageLink);
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(new CustomerId(customerId), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.DASHBOARD, EntityType.DASHBOARD, pageLink);
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<DashboardInfo>>) input -> {
        List<ListenableFuture<DashboardInfo>> dashboardFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            dashboardFutures.add(findByIdAsync(relation.getTo().getId()));
        }
        return Futures.successfulAsList(dashboardFutures);
    });
}
Also used : DashboardInfoEntity(org.thingsboard.server.dao.model.nosql.DashboardInfoEntity) CassandraAbstractSearchTextDao(org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) QueryBuilder.eq(com.datastax.driver.core.querybuilder.QueryBuilder.eq) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) NoSqlDao(org.thingsboard.server.dao.util.NoSqlDao) Autowired(org.springframework.beans.factory.annotation.Autowired) UUID(java.util.UUID) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) EntityType(org.thingsboard.server.common.data.EntityType) DaoUtil(org.thingsboard.server.dao.DaoUtil) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Collections(java.util.Collections) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) List(java.util.List) CustomerId(org.thingsboard.server.common.data.id.CustomerId) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo)

Example 13 with EntityRelation

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

the class DashboardServiceImpl method unassignDashboardFromCustomer.

@Override
public Dashboard unassignDashboardFromCustomer(DashboardId dashboardId, CustomerId customerId) {
    Dashboard dashboard = findDashboardById(dashboardId);
    Customer customer = customerDao.findById(customerId.getId());
    if (customer == null) {
        throw new DataValidationException("Can't unassign dashboard from non-existent customer!");
    }
    if (dashboard.removeAssignedCustomer(customer)) {
        try {
            deleteRelation(new EntityRelation(customerId, dashboardId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.DASHBOARD));
        } catch (ExecutionException | InterruptedException e) {
            log.warn("[{}] Failed to delete dashboard relation. Customer Id: [{}]", dashboardId, customerId);
            throw new RuntimeException(e);
        }
        return saveDashboard(dashboard);
    } else {
        return dashboard;
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 14 with EntityRelation

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

the class JpaAlarmDao method findAlarms.

@Override
public ListenableFuture<List<AlarmInfo>> findAlarms(AlarmQuery query) {
    log.trace("Try to find alarms by entity [{}], status [{}] and pageLink [{}]", query.getAffectedEntityId(), query.getStatus(), query.getPageLink());
    EntityId affectedEntity = query.getAffectedEntityId();
    String searchStatusName;
    if (query.getSearchStatus() == null && query.getStatus() == null) {
        searchStatusName = AlarmSearchStatus.ANY.name();
    } else if (query.getSearchStatus() != null) {
        searchStatusName = query.getSearchStatus().name();
    } else {
        searchStatusName = query.getStatus().name();
    }
    String relationType = BaseAlarmService.ALARM_RELATION_PREFIX + searchStatusName;
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(affectedEntity, relationType, RelationTypeGroup.ALARM, EntityType.ALARM, query.getPageLink());
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<AlarmInfo>>) input -> {
        List<ListenableFuture<AlarmInfo>> alarmFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            alarmFutures.add(Futures.transform(findAlarmByIdAsync(relation.getTo().getId()), (Function<Alarm, AlarmInfo>) AlarmInfo::new));
        }
        return Futures.successfulAsList(alarmFutures);
    });
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) JpaAbstractDao(org.thingsboard.server.dao.sql.JpaAbstractDao) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Autowired(org.springframework.beans.factory.annotation.Autowired) CrudRepository(org.springframework.data.repository.CrudRepository) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) BaseAlarmService(org.thingsboard.server.dao.alarm.BaseAlarmService) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) AlarmQuery(org.thingsboard.server.common.data.alarm.AlarmQuery) Function(com.google.common.base.Function) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) SqlDao(org.thingsboard.server.dao.util.SqlDao) PageRequest(org.springframework.data.domain.PageRequest) AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) UUID(java.util.UUID) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmDao(org.thingsboard.server.dao.alarm.AlarmDao) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) DaoUtil(org.thingsboard.server.dao.DaoUtil) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) UUIDConverter(org.thingsboard.server.common.data.UUIDConverter) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) AlarmEntity(org.thingsboard.server.dao.model.sql.AlarmEntity) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with EntityRelation

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

the class BaseRelationDao method getEntityRelation.

private EntityRelation getEntityRelation(Row row) {
    EntityRelation relation = new EntityRelation();
    relation.setTypeGroup(row.get(ModelConstants.RELATION_TYPE_GROUP_PROPERTY, relationTypeGroupCodec));
    relation.setType(row.getString(ModelConstants.RELATION_TYPE_PROPERTY));
    relation.setAdditionalInfo(row.get(ModelConstants.ADDITIONAL_INFO_PROPERTY, JsonNode.class));
    relation.setFrom(toEntity(row, ModelConstants.RELATION_FROM_ID_PROPERTY, ModelConstants.RELATION_FROM_TYPE_PROPERTY));
    relation.setTo(toEntity(row, ModelConstants.RELATION_TO_ID_PROPERTY, ModelConstants.RELATION_TO_TYPE_PROPERTY));
    return relation;
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)28 AssetId (org.thingsboard.server.common.data.id.AssetId)13 Test (org.junit.Test)12 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)6 Futures (com.google.common.util.concurrent.Futures)6 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)6 Slf4j (lombok.extern.slf4j.Slf4j)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 EntityId (org.thingsboard.server.common.data.id.EntityId)6 List (java.util.List)5 ExecutionException (java.util.concurrent.ExecutionException)5 EntityType (org.thingsboard.server.common.data.EntityType)5 Function (com.google.common.base.Function)4 ArrayList (java.util.ArrayList)4 UUID (java.util.UUID)4 Component (org.springframework.stereotype.Component)4 CustomerId (org.thingsboard.server.common.data.id.CustomerId)4 TenantId (org.thingsboard.server.common.data.id.TenantId)4 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)4 RelationTypeGroup (org.thingsboard.server.common.data.relation.RelationTypeGroup)4