Search in sources :

Example 1 with RELATIONS_CACHE

use of org.thingsboard.server.common.data.CacheConstants.RELATIONS_CACHE in project thingsboard by thingsboard.

the class BaseRelationService method deleteEntityRelationsAsync.

@Override
public ListenableFuture<Boolean> deleteEntityRelationsAsync(EntityId entity) {
    Cache cache = cacheManager.getCache(RELATIONS_CACHE);
    log.trace("Executing deleteEntityRelationsAsync [{}]", entity);
    validate(entity);
    List<ListenableFuture<List<EntityRelation>>> inboundRelationsListTo = new ArrayList<>();
    for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) {
        inboundRelationsListTo.add(relationDao.findAllByTo(entity, typeGroup));
    }
    ListenableFuture<List<List<EntityRelation>>> inboundRelationsTo = Futures.allAsList(inboundRelationsListTo);
    ListenableFuture<List<Boolean>> inboundDeletions = Futures.transform(inboundRelationsTo, (AsyncFunction<List<List<EntityRelation>>, List<Boolean>>) relations -> {
        List<ListenableFuture<Boolean>> results = getListenableFutures(relations, cache, true);
        return Futures.allAsList(results);
    });
    ListenableFuture<Boolean> inboundFuture = Futures.transform(inboundDeletions, getListToBooleanFunction());
    List<ListenableFuture<List<EntityRelation>>> inboundRelationsListFrom = new ArrayList<>();
    for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) {
        inboundRelationsListFrom.add(relationDao.findAllByTo(entity, typeGroup));
    }
    ListenableFuture<List<List<EntityRelation>>> inboundRelationsFrom = Futures.allAsList(inboundRelationsListFrom);
    Futures.transform(inboundRelationsFrom, (AsyncFunction<List<List<EntityRelation>>, List<Boolean>>) relations -> {
        List<ListenableFuture<Boolean>> results = getListenableFutures(relations, cache, false);
        return Futures.allAsList(results);
    });
    ListenableFuture<Boolean> outboundFuture = relationDao.deleteOutboundRelationsAsync(entity);
    return Futures.transform(Futures.allAsList(Arrays.asList(inboundFuture, outboundFuture)), getListToBooleanFunction());
}
Also used : java.util(java.util) Function(com.google.common.base.Function) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Cache(org.springframework.cache.Cache) org.thingsboard.server.common.data.relation(org.thingsboard.server.common.data.relation) Cacheable(org.springframework.cache.annotation.Cacheable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Autowired(org.springframework.beans.factory.annotation.Autowired) CacheEvict(org.springframework.cache.annotation.CacheEvict) Caching(org.springframework.cache.annotation.Caching) ExecutionException(java.util.concurrent.ExecutionException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) CacheManager(org.springframework.cache.CacheManager) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) BiConsumer(java.util.function.BiConsumer) EntityService(org.thingsboard.server.dao.entity.EntityService) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RELATIONS_CACHE(org.thingsboard.server.common.data.CacheConstants.RELATIONS_CACHE) Nullable(javax.annotation.Nullable) StringUtils(org.springframework.util.StringUtils) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Cache(org.springframework.cache.Cache)

Example 2 with RELATIONS_CACHE

use of org.thingsboard.server.common.data.CacheConstants.RELATIONS_CACHE in project thingsboard by thingsboard.

the class BaseRelationService method deleteEntityRelations.

@Override
public boolean deleteEntityRelations(EntityId entity) {
    Cache cache = cacheManager.getCache(RELATIONS_CACHE);
    log.trace("Executing deleteEntityRelations [{}]", entity);
    validate(entity);
    List<ListenableFuture<List<EntityRelation>>> inboundRelationsListTo = new ArrayList<>();
    for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) {
        inboundRelationsListTo.add(relationDao.findAllByTo(entity, typeGroup));
    }
    ListenableFuture<List<List<EntityRelation>>> inboundRelationsTo = Futures.allAsList(inboundRelationsListTo);
    ListenableFuture<List<Boolean>> inboundDeletions = Futures.transform(inboundRelationsTo, (List<List<EntityRelation>> relations) -> getBooleans(relations, cache, true));
    ListenableFuture<Boolean> inboundFuture = Futures.transform(inboundDeletions, getListToBooleanFunction());
    boolean inboundDeleteResult = false;
    try {
        inboundDeleteResult = inboundFuture.get();
    } catch (InterruptedException | ExecutionException e) {
        log.error("Error deleting entity inbound relations", e);
    }
    List<ListenableFuture<List<EntityRelation>>> inboundRelationsListFrom = new ArrayList<>();
    for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) {
        inboundRelationsListFrom.add(relationDao.findAllByFrom(entity, typeGroup));
    }
    ListenableFuture<List<List<EntityRelation>>> inboundRelationsFrom = Futures.allAsList(inboundRelationsListFrom);
    Futures.transform(inboundRelationsFrom, (Function<List<List<EntityRelation>>, List<Boolean>>) relations -> getBooleans(relations, cache, false));
    boolean outboundDeleteResult = relationDao.deleteOutboundRelations(entity);
    return inboundDeleteResult && outboundDeleteResult;
}
Also used : java.util(java.util) Function(com.google.common.base.Function) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Cache(org.springframework.cache.Cache) org.thingsboard.server.common.data.relation(org.thingsboard.server.common.data.relation) Cacheable(org.springframework.cache.annotation.Cacheable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Autowired(org.springframework.beans.factory.annotation.Autowired) CacheEvict(org.springframework.cache.annotation.CacheEvict) Caching(org.springframework.cache.annotation.Caching) ExecutionException(java.util.concurrent.ExecutionException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) CacheManager(org.springframework.cache.CacheManager) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) BiConsumer(java.util.function.BiConsumer) EntityService(org.thingsboard.server.dao.entity.EntityService) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RELATIONS_CACHE(org.thingsboard.server.common.data.CacheConstants.RELATIONS_CACHE) Nullable(javax.annotation.Nullable) StringUtils(org.springframework.util.StringUtils) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ExecutionException(java.util.concurrent.ExecutionException) Cache(org.springframework.cache.Cache)

Aggregations

Function (com.google.common.base.Function)2 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)2 Futures (com.google.common.util.concurrent.Futures)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 java.util (java.util)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 BiConsumer (java.util.function.BiConsumer)2 Nullable (javax.annotation.Nullable)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Cache (org.springframework.cache.Cache)2 CacheManager (org.springframework.cache.CacheManager)2 CacheEvict (org.springframework.cache.annotation.CacheEvict)2 Cacheable (org.springframework.cache.annotation.Cacheable)2 Caching (org.springframework.cache.annotation.Caching)2 Service (org.springframework.stereotype.Service)2 StringUtils (org.springframework.util.StringUtils)2 RELATIONS_CACHE (org.thingsboard.server.common.data.CacheConstants.RELATIONS_CACHE)2 EntityId (org.thingsboard.server.common.data.id.EntityId)2