|
|
|
@ -39,7 +39,6 @@ import org.thingsboard.server.common.data.query.EntityKey; |
|
|
|
import org.thingsboard.server.common.data.query.EntityKeyType; |
|
|
|
import org.thingsboard.server.common.data.query.EntityTypeFilter; |
|
|
|
import org.thingsboard.server.common.data.relation.EntityRelation; |
|
|
|
import org.thingsboard.server.common.data.relation.RelationTypeGroup; |
|
|
|
import org.thingsboard.server.dao.entity.EntityService; |
|
|
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
|
|
|
import org.thingsboard.server.service.sync.EntitiesExportImportService; |
|
|
|
@ -81,9 +80,10 @@ public class EntitiesExportImportController extends BaseController { |
|
|
|
public EntityExportData<ExportableEntity<EntityId>> exportSingleEntity(@PathVariable EntityType entityType, |
|
|
|
@PathVariable UUID id, |
|
|
|
@RequestParam Map<String, String> exportSettingsParams) throws ThingsboardException { |
|
|
|
SecurityUser user = getCurrentUser(); |
|
|
|
EntityId entityId = EntityIdFactory.getByTypeAndUuid(entityType, id); |
|
|
|
try { |
|
|
|
return exportEntity(getTenantId(), entityId, toExportSettings(exportSettingsParams)); |
|
|
|
return exportEntity(user, entityId, toExportSettings(exportSettingsParams)); |
|
|
|
} catch (Exception e) { |
|
|
|
throw handleException(e); |
|
|
|
} |
|
|
|
@ -93,11 +93,12 @@ public class EntitiesExportImportController extends BaseController { |
|
|
|
public List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByIds(@PathVariable EntityType entityType, |
|
|
|
@RequestParam UUID[] ids, |
|
|
|
@RequestParam Map<String, String> exportSettingsParams) throws ThingsboardException { |
|
|
|
SecurityUser user = getCurrentUser(); |
|
|
|
List<EntityId> entitiesIds = Arrays.stream(ids) |
|
|
|
.map(id -> EntityIdFactory.getByTypeAndUuid(entityType, id)) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
try { |
|
|
|
return exportEntitiesByIds(getTenantId(), entitiesIds, toExportSettings(exportSettingsParams)); |
|
|
|
return exportEntitiesByIds(user, entitiesIds, toExportSettings(exportSettingsParams)); |
|
|
|
} catch (Exception e) { |
|
|
|
throw handleException(e); |
|
|
|
} |
|
|
|
@ -109,13 +110,13 @@ public class EntitiesExportImportController extends BaseController { |
|
|
|
@RequestParam(defaultValue = "0") int page, |
|
|
|
@RequestParam(defaultValue = "2147483647") int pageSize, |
|
|
|
@RequestParam(name = "customerId", required = false) UUID customerUuid) throws ThingsboardException { |
|
|
|
TenantId tenantId = getTenantId(); |
|
|
|
SecurityUser user = getCurrentUser(); |
|
|
|
CustomerId customerId = toCustomerId(customerUuid); |
|
|
|
|
|
|
|
EntityTypeFilter entityTypeFilter = new EntityTypeFilter(); |
|
|
|
entityTypeFilter.setEntityType(entityType); |
|
|
|
try { |
|
|
|
return exportEntitiesByFilter(tenantId, customerId, entityTypeFilter, page, pageSize, toExportSettings(exportSettingsParams)); |
|
|
|
return exportEntitiesByFilter(user, customerId, entityTypeFilter, page, pageSize, toExportSettings(exportSettingsParams)); |
|
|
|
} catch (Exception e) { |
|
|
|
throw handleException(e); |
|
|
|
} |
|
|
|
@ -127,10 +128,10 @@ public class EntitiesExportImportController extends BaseController { |
|
|
|
@RequestParam(defaultValue = "0") int page, |
|
|
|
@RequestParam(defaultValue = "2147483647") int pageSize, |
|
|
|
@RequestParam(name = "customerId", required = false) UUID customerUuid) throws ThingsboardException { |
|
|
|
TenantId tenantId = getTenantId(); |
|
|
|
SecurityUser user = getCurrentUser(); |
|
|
|
CustomerId customerId = toCustomerId(customerUuid); |
|
|
|
try { |
|
|
|
return exportEntitiesByFilter(tenantId, customerId, filter, page, pageSize, toExportSettings(exportSettingsParams)); |
|
|
|
return exportEntitiesByFilter(user, customerId, filter, page, pageSize, toExportSettings(exportSettingsParams)); |
|
|
|
} catch (Exception e) { |
|
|
|
throw handleException(e); |
|
|
|
} |
|
|
|
@ -141,12 +142,12 @@ public class EntitiesExportImportController extends BaseController { |
|
|
|
public List<EntityExportData<ExportableEntity<EntityId>>> exportAllEntitiesByFilters(@RequestBody List<EntityFilter> filters, |
|
|
|
@RequestParam Map<String, String> exportSettingsParams, |
|
|
|
@RequestParam(name = "customerId", required = false) UUID customerUuid) throws ThingsboardException { |
|
|
|
TenantId tenantId = getTenantId(); |
|
|
|
SecurityUser user = getCurrentUser(); |
|
|
|
CustomerId customerId = toCustomerId(customerUuid); |
|
|
|
try { |
|
|
|
List<EntityExportData<ExportableEntity<EntityId>>> exportDataList = new ArrayList<>(); |
|
|
|
for (EntityFilter filter : filters) { |
|
|
|
exportDataList.addAll(exportEntitiesByFilter(tenantId, customerId, filter, 0, Integer.MAX_VALUE, toExportSettings(exportSettingsParams))); |
|
|
|
exportDataList.addAll(exportEntitiesByFilter(user, customerId, filter, 0, Integer.MAX_VALUE, toExportSettings(exportSettingsParams))); |
|
|
|
} |
|
|
|
return exportDataList; |
|
|
|
} catch (Exception e) { |
|
|
|
@ -158,17 +159,17 @@ public class EntitiesExportImportController extends BaseController { |
|
|
|
public List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByQuery(@RequestBody EntityDataQuery entitiesQuery, |
|
|
|
@RequestParam Map<String, String> exportSettingsParams, |
|
|
|
@RequestParam(name = "customerId", required = false) UUID customerUuid) throws ThingsboardException { |
|
|
|
TenantId tenantId = getTenantId(); |
|
|
|
SecurityUser user = getCurrentUser(); |
|
|
|
CustomerId customerId = toCustomerId(customerUuid); |
|
|
|
try { |
|
|
|
return exportEntitiesByQuery(tenantId, customerId, entitiesQuery, toExportSettings(exportSettingsParams)); |
|
|
|
return exportEntitiesByQuery(user, customerId, entitiesQuery, toExportSettings(exportSettingsParams)); |
|
|
|
} catch (Exception e) { |
|
|
|
throw handleException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByFilter(TenantId tenantId, CustomerId customerId, EntityFilter filter, int page, int pageSize, EntityExportSettings exportSettings) throws ThingsboardException { |
|
|
|
private List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByFilter(SecurityUser user, CustomerId customerId, EntityFilter filter, int page, int pageSize, EntityExportSettings exportSettings) throws ThingsboardException { |
|
|
|
EntityDataPageLink pageLink = new EntityDataPageLink(); |
|
|
|
pageLink.setPage(page); |
|
|
|
pageLink.setPageSize(pageSize); |
|
|
|
@ -176,43 +177,26 @@ public class EntitiesExportImportController extends BaseController { |
|
|
|
pageLink.setSortOrder(new EntityDataSortOrder(sortProperty, EntityDataSortOrder.Direction.DESC)); |
|
|
|
|
|
|
|
EntityDataQuery query = new EntityDataQuery(filter, pageLink, List.of(sortProperty), Collections.emptyList(), Collections.emptyList()); |
|
|
|
return exportEntitiesByQuery(tenantId, customerId, query, exportSettings); |
|
|
|
return exportEntitiesByQuery(user, customerId, query, exportSettings); |
|
|
|
} |
|
|
|
|
|
|
|
private List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query, EntityExportSettings exportSettings) throws ThingsboardException { |
|
|
|
List<EntityId> entitiesIds = entityService.findEntityDataByQuery(tenantId, customerId, query).getData().stream() |
|
|
|
private List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByQuery(SecurityUser user, CustomerId customerId, EntityDataQuery query, EntityExportSettings exportSettings) throws ThingsboardException { |
|
|
|
List<EntityId> entitiesIds = entityService.findEntityDataByQuery(user.getTenantId(), customerId, query).getData().stream() |
|
|
|
.map(EntityData::getEntityId) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
return exportEntitiesByIds(tenantId, entitiesIds, exportSettings); |
|
|
|
return exportEntitiesByIds(user, entitiesIds, exportSettings); |
|
|
|
} |
|
|
|
|
|
|
|
private List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByIds(TenantId tenantId, List<EntityId> entitiesIds, EntityExportSettings exportSettings) throws ThingsboardException { |
|
|
|
private List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByIds(SecurityUser user, List<EntityId> entitiesIds, EntityExportSettings exportSettings) throws ThingsboardException { |
|
|
|
List<EntityExportData<ExportableEntity<EntityId>>> exportDataList = new ArrayList<>(); |
|
|
|
for (EntityId entityId : entitiesIds) { |
|
|
|
exportDataList.add(exportEntity(tenantId, entityId, exportSettings)); |
|
|
|
exportDataList.add(exportEntity(user, entityId, exportSettings)); |
|
|
|
} |
|
|
|
return exportDataList; |
|
|
|
} |
|
|
|
|
|
|
|
private <E extends ExportableEntity<I>, I extends EntityId> EntityExportData<E> exportEntity(TenantId tenantId, I entityId, EntityExportSettings exportSettings) throws ThingsboardException { |
|
|
|
checkEntityId(entityId, Operation.READ); |
|
|
|
|
|
|
|
List<EntityRelation> relations = new LinkedList<>(); |
|
|
|
if (exportSettings.isExportInboundRelations()) { |
|
|
|
relations.addAll(relationService.findByTo(tenantId, entityId, RelationTypeGroup.COMMON)); |
|
|
|
} |
|
|
|
if (exportSettings.isExportOutboundRelations()) { |
|
|
|
relations.addAll(relationService.findByFrom(tenantId, entityId, RelationTypeGroup.COMMON)); |
|
|
|
} |
|
|
|
for (EntityRelation relation : relations) { |
|
|
|
if (!relation.getFrom().equals(entityId)) { |
|
|
|
checkEntityId(relation.getFrom(), Operation.READ); |
|
|
|
} else if (!relation.getTo().equals(entityId)) { |
|
|
|
checkEntityId(relation.getTo(), Operation.READ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return exportImportService.exportEntity(tenantId, entityId, exportSettings); |
|
|
|
private <E extends ExportableEntity<I>, I extends EntityId> EntityExportData<E> exportEntity(SecurityUser user, I entityId, EntityExportSettings exportSettings) throws ThingsboardException { |
|
|
|
return exportImportService.exportEntity(user, entityId, exportSettings); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -235,41 +219,7 @@ public class EntitiesExportImportController extends BaseController { |
|
|
|
|
|
|
|
|
|
|
|
public List<EntityImportResult<ExportableEntity<EntityId>>> importEntities(SecurityUser user, List<EntityExportData<ExportableEntity<EntityId>>> exportDataList, EntityImportSettings importSettings) throws ThingsboardException { |
|
|
|
for (EntityExportData<ExportableEntity<EntityId>> exportData : exportDataList) { |
|
|
|
ExportableEntity<EntityId> existingEntity = exportableEntitiesService.findEntityByExternalId(user.getTenantId(), exportData.getEntity().getId()); |
|
|
|
if (existingEntity != null) { |
|
|
|
accessControlService.checkPermission(user, Resource.of(exportData.getEntityType()), Operation.WRITE, existingEntity.getId(), existingEntity); |
|
|
|
} else { |
|
|
|
exportData.getEntity().setTenantId(user.getTenantId()); |
|
|
|
accessControlService.checkPermission(user, Resource.of(exportData.getEntityType()), Operation.CREATE, null, exportData.getEntity()); |
|
|
|
} |
|
|
|
|
|
|
|
List<EntityRelation> relations = new LinkedList<>(); |
|
|
|
if (importSettings.isImportInboundRelations() && exportData.getInboundRelations() != null) { |
|
|
|
relations.addAll(exportData.getInboundRelations()); |
|
|
|
} |
|
|
|
if (importSettings.isImportOutboundRelations() && exportData.getOutboundRelations() != null) { |
|
|
|
relations.addAll(exportData.getOutboundRelations()); |
|
|
|
} |
|
|
|
for (EntityRelation relation : relations) { |
|
|
|
EntityId otherEntityId = null; |
|
|
|
if (!relation.getFrom().equals(exportData.getEntity().getId())) { |
|
|
|
otherEntityId = relation.getFrom(); |
|
|
|
} else if (!relation.getTo().equals(exportData.getEntity().getId())) { |
|
|
|
otherEntityId = relation.getTo(); |
|
|
|
} |
|
|
|
if (otherEntityId != null) { |
|
|
|
ExportableEntity<EntityId> otherEntity = exportableEntitiesService.findEntityByExternalId(user.getTenantId(), otherEntityId); |
|
|
|
if (otherEntity != null) { |
|
|
|
checkEntityId(otherEntity.getId(), Operation.WRITE); |
|
|
|
} else { |
|
|
|
throw new IllegalArgumentException("Relation is referencing non-existing entity"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return exportImportService.importEntities(user.getTenantId(), exportDataList, importSettings); |
|
|
|
return exportImportService.importEntities(user, exportDataList, importSettings); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|