@ -15,6 +15,8 @@
* /
package org.thingsboard.server.dao.entityview ;
import com.google.common.base.Function ;
import com.google.common.util.concurrent.Futures ;
import com.google.common.util.concurrent.ListenableFuture ;
import lombok.extern.slf4j.Slf4j ;
import org.apache.commons.lang3.StringUtils ;
@ -25,14 +27,22 @@ import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable ;
import org.springframework.stereotype.Service ;
import org.thingsboard.server.common.data.Customer ;
import org.thingsboard.server.common.data.Device ;
import org.thingsboard.server.common.data.EntitySubtype ;
import org.thingsboard.server.common.data.EntityType ;
import org.thingsboard.server.common.data.EntityView ;
import org.thingsboard.server.common.data.Tenant ;
import org.thingsboard.server.common.data.device.DeviceSearchQuery ;
import org.thingsboard.server.common.data.entityview.EntityViewSearchQuery ;
import org.thingsboard.server.common.data.id.CustomerId ;
import org.thingsboard.server.common.data.id.DeviceId ;
import org.thingsboard.server.common.data.id.EntityId ;
import org.thingsboard.server.common.data.id.EntityViewId ;
import org.thingsboard.server.common.data.id.TenantId ;
import org.thingsboard.server.common.data.page.TextPageData ;
import org.thingsboard.server.common.data.page.TextPageLink ;
import org.thingsboard.server.common.data.relation.EntityRelation ;
import org.thingsboard.server.common.data.relation.EntitySearchDirection ;
import org.thingsboard.server.dao.customer.CustomerDao ;
import org.thingsboard.server.dao.entity.AbstractEntityService ;
import org.thingsboard.server.dao.exception.DataValidationException ;
@ -40,8 +50,12 @@ import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.service.PaginatedRemover ;
import org.thingsboard.server.dao.tenant.TenantDao ;
import javax.annotation.Nullable ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Comparator ;
import java.util.List ;
import java.util.stream.Collectors ;
import static org.thingsboard.server.common.data.CacheConstants.DEVICE_CACHE ;
import static org.thingsboard.server.common.data.CacheConstants.ENTITY_VIEW_CACHE ;
@ -55,8 +69,7 @@ import static org.thingsboard.server.dao.service.Validator.validateString;
* /
@Service
@Slf4j
public class EntityViewServiceImpl extends AbstractEntityService
implements EntityViewService {
public class EntityViewServiceImpl extends AbstractEntityService implements EntityViewService {
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId " ;
public static final String INCORRECT_PAGE_LINK = "Incorrect page link " ;
@ -75,7 +88,7 @@ public class EntityViewServiceImpl extends AbstractEntityService
@Autowired
private CacheManager cacheManager ;
@Cacheable ( cacheNames = ENTITY_VIEW_CACHE , key = "{#entityViewId}" )
// @Cacheable(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}")
@Override
public EntityView findEntityViewById ( EntityViewId entityViewId ) {
log . trace ( "Executing findEntityViewById [{}]" , entityViewId ) ;
@ -91,7 +104,7 @@ public class EntityViewServiceImpl extends AbstractEntityService
. orElse ( null ) ;
}
@CacheEvict ( cacheNames = ENTITY_VIEW_CACHE , key = "{#entityView.id}" )
// @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityView.id}")
@Override
public EntityView saveEntityView ( EntityView entityView ) {
log . trace ( "Executing save entity view [{}]" , entityView ) ;
@ -136,7 +149,7 @@ public class EntityViewServiceImpl extends AbstractEntityService
return new TextPageData < > ( entityViews , pageLink ) ;
}
@Cacheable ( cacheNames = ENTITY_VIEW_CACHE , key = "{#tenantId, #entityId, #pageLink}" )
// @Cacheable(cacheNames = ENTITY_VIEW_CACHE, key = "{#tenantId, #entityId, #pageLink}")
@Override
public TextPageData < EntityView > findEntityViewByTenantIdAndEntityId ( TenantId tenantId , EntityId entityId ,
TextPageLink pageLink ) {
@ -176,7 +189,7 @@ public class EntityViewServiceImpl extends AbstractEntityService
return new TextPageData < > ( entityViews , pageLink ) ;
}
@Cacheable ( cacheNames = ENTITY_VIEW_CACHE , key = "{#tenantId, #customerId, #entityId, #pageLink}" )
// @Cacheable(cacheNames = ENTITY_VIEW_CACHE, key = "{#tenantId, #customerId, #entityId, #pageLink}")
@Override
public TextPageData < EntityView > findEntityViewsByTenantIdAndCustomerIdAndEntityId ( TenantId tenantId ,
CustomerId customerId ,
@ -211,6 +224,24 @@ public class EntityViewServiceImpl extends AbstractEntityService
return entityViewDao . findByIdAsync ( entityViewId . getId ( ) ) ;
}
@Override
public ListenableFuture < List < EntityView > > findEntityViewsByQuery ( EntityViewSearchQuery query ) {
ListenableFuture < List < EntityRelation > > relations = relationService . findByQuery ( query . toEntitySearchQuery ( ) ) ;
ListenableFuture < List < EntityView > > entityViews = Futures . transformAsync ( relations , r - > {
EntitySearchDirection direction = query . toEntitySearchQuery ( ) . getParameters ( ) . getDirection ( ) ;
List < ListenableFuture < EntityView > > futures = new ArrayList < > ( ) ;
for ( EntityRelation relation : r ) {
EntityId entityId = direction = = EntitySearchDirection . FROM ? relation . getTo ( ) : relation . getFrom ( ) ;
if ( entityId . getEntityType ( ) = = EntityType . ENTITY_VIEW ) {
futures . add ( findEntityViewByIdAsync ( new EntityViewId ( entityId . getId ( ) ) ) ) ;
}
}
return Futures . successfulAsList ( futures ) ;
} ) ;
return entityViews ;
}
private DataValidator < EntityView > entityViewValidator =
new DataValidator < EntityView > ( ) {