|
|
|
@ -39,6 +39,7 @@ import org.thingsboard.server.common.data.id.EntityIdFactory; |
|
|
|
import org.thingsboard.server.common.data.id.HasId; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
|
import org.thingsboard.server.common.data.page.PageDataIterable; |
|
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
|
import org.thingsboard.server.common.data.sync.ie.EntityExportData; |
|
|
|
import org.thingsboard.server.common.data.sync.ie.EntityExportSettings; |
|
|
|
@ -89,6 +90,7 @@ import java.util.Collections; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.UUID; |
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
import java.util.function.Function; |
|
|
|
@ -331,7 +333,6 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont |
|
|
|
sw.startNew("Entities " + entityType.name()); |
|
|
|
ctx.setSettings(getEntityImportSettings(request, entityType)); |
|
|
|
importEntities(ctx, entityType); |
|
|
|
persistToCache(ctx); |
|
|
|
} |
|
|
|
|
|
|
|
sw.startNew("Reimport"); |
|
|
|
@ -343,7 +344,6 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont |
|
|
|
.filter(entityType -> request.getEntityTypes().get(entityType).isRemoveOtherEntities()) |
|
|
|
.sorted(exportImportService.getEntityTypeComparatorForImport().reversed()) |
|
|
|
.forEach(entityType -> removeOtherEntities(ctx, entityType)); |
|
|
|
persistToCache(ctx); |
|
|
|
|
|
|
|
sw.startNew("References and Relations"); |
|
|
|
exportImportService.saveReferencesAndRelations(ctx); |
|
|
|
@ -396,6 +396,8 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont |
|
|
|
ctx.getImportedEntities().computeIfAbsent(entityType, t -> new HashSet<>()) |
|
|
|
.add(importResult.getSavedEntity().getId()); |
|
|
|
} |
|
|
|
|
|
|
|
persistToCache(ctx); |
|
|
|
log.debug("Imported {} pack ({}) for tenant {}", entityType, entityDataList.size(), ctx.getTenantId()); |
|
|
|
offset += limit; |
|
|
|
} while (entityDataList.size() == limit); |
|
|
|
@ -420,19 +422,34 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont |
|
|
|
} |
|
|
|
|
|
|
|
private void removeOtherEntities(EntitiesImportCtx ctx, EntityType entityType) { |
|
|
|
DaoUtil.processInBatches(pageLink -> { |
|
|
|
return exportableEntitiesService.findEntitiesByTenantId(ctx.getTenantId(), entityType, pageLink); |
|
|
|
}, 100, entity -> { |
|
|
|
if (ctx.getImportedEntities().get(entityType) == null || !ctx.getImportedEntities().get(entityType).contains(entity.getId())) { |
|
|
|
exportableEntitiesService.removeById(ctx.getTenantId(), entity.getId()); |
|
|
|
|
|
|
|
ctx.addEventCallback(() -> { |
|
|
|
entityNotificationService.logEntityAction(ctx.getTenantId(), entity.getId(), entity, null, |
|
|
|
ActionType.DELETED, ctx.getUser()); |
|
|
|
}); |
|
|
|
ctx.registerDeleted(entityType); |
|
|
|
var entities = new PageDataIterable<>(link -> exportableEntitiesService.findEntitiesIdsByTenantId(ctx.getTenantId(), entityType, link), 100); |
|
|
|
Set<EntityId> toRemove = new HashSet<>(); |
|
|
|
for (EntityId entityId : entities) { |
|
|
|
if (ctx.getImportedEntities().get(entityType) == null || !ctx.getImportedEntities().get(entityType).contains(entityId)) { |
|
|
|
toRemove.add(entityId); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
for (EntityId entityId : toRemove) { |
|
|
|
ExportableEntity<EntityId> entity = exportableEntitiesService.findEntityById(entityId); |
|
|
|
exportableEntitiesService.removeById(ctx.getTenantId(), entityId); |
|
|
|
|
|
|
|
ThrowingRunnable callback = () -> { |
|
|
|
entityNotificationService.logEntityAction(ctx.getTenantId(), entity.getId(), entity, null, |
|
|
|
ActionType.DELETED, ctx.getUser()); |
|
|
|
}; |
|
|
|
if (ctx.isRollbackOnError()) { |
|
|
|
ctx.addEventCallback(callback); |
|
|
|
} else { |
|
|
|
try { |
|
|
|
callback.run(); |
|
|
|
} catch (ThingsboardException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
ctx.registerDeleted(entityType); |
|
|
|
} |
|
|
|
persistToCache(ctx); |
|
|
|
} |
|
|
|
|
|
|
|
private VersionLoadResult onError(EntityId externalId, Throwable e) { |
|
|
|
|