Browse Source

Refactor events sending on import; export api with ExportRequest of different types

pull/6357/head
Viacheslav Klimov 4 years ago
parent
commit
12840c81e9
  1. 20
      application/src/main/java/org/thingsboard/server/controller/AssetController.java
  2. 10
      application/src/main/java/org/thingsboard/server/controller/DashboardController.java
  3. 28
      application/src/main/java/org/thingsboard/server/controller/DeviceController.java
  4. 199
      application/src/main/java/org/thingsboard/server/controller/EntitiesExportImportController.java
  5. 17
      application/src/main/java/org/thingsboard/server/controller/RuleChainController.java
  6. 61
      application/src/main/java/org/thingsboard/server/service/action/EntityActionService.java
  7. 2
      application/src/main/java/org/thingsboard/server/service/sync/DefaultEntitiesExportImportService.java
  8. 2
      application/src/main/java/org/thingsboard/server/service/sync/EntitiesExportImportService.java
  9. 1
      application/src/main/java/org/thingsboard/server/service/sync/exporting/EntityExportService.java
  10. 8
      application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/EntityExportSettings.java
  11. 37
      application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/EntityFilterExportRequest.java
  12. 35
      application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/EntityListExportRequest.java
  13. 35
      application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/EntityQueryExportRequest.java
  14. 37
      application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/EntityTypeExportRequest.java
  15. 41
      application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/ExportRequest.java
  16. 25
      application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/ExportRequestType.java
  17. 33
      application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/SingleEntityExportRequest.java
  18. 2
      application/src/main/java/org/thingsboard/server/service/sync/exporting/impl/BaseEntityExportService.java
  19. 32
      application/src/main/java/org/thingsboard/server/service/sync/importing/data/request/ImportRequest.java
  20. 9
      application/src/main/java/org/thingsboard/server/service/sync/importing/impl/AssetImportService.java
  21. 10
      application/src/main/java/org/thingsboard/server/service/sync/importing/impl/BaseEntityImportService.java
  22. 9
      application/src/main/java/org/thingsboard/server/service/sync/importing/impl/CustomerImportService.java
  23. 12
      application/src/main/java/org/thingsboard/server/service/sync/importing/impl/DashboardImportService.java
  24. 8
      application/src/main/java/org/thingsboard/server/service/sync/importing/impl/DeviceImportService.java
  25. 19
      application/src/main/java/org/thingsboard/server/service/sync/importing/impl/DeviceProfileImportService.java
  26. 14
      application/src/main/java/org/thingsboard/server/service/sync/importing/impl/RuleChainImportService.java
  27. 7
      application/src/main/java/org/thingsboard/server/utils/ThrowingRunnable.java
  28. 2
      application/src/test/java/org/thingsboard/server/controller/sql/EntitiesExportImportControllerSqlTest.java

20
application/src/main/java/org/thingsboard/server/controller/AssetController.java

@ -155,7 +155,9 @@ public class AssetController extends BaseController {
checkEntity(asset.getId(), asset, Resource.ASSET);
Asset savedAsset = checkNotNull(assetService.saveAsset(asset));
entityActionService.onAssetCreatedOrUpdated(getCurrentUser(), savedAsset, asset.getId() == null);
onAssetCreatedOrUpdated(savedAsset, asset.getId() != null, getCurrentUser());
return savedAsset;
} catch (Exception e) {
logEntityAction(emptyId(EntityType.ASSET), asset,
@ -164,6 +166,20 @@ public class AssetController extends BaseController {
}
}
private void onAssetCreatedOrUpdated(Asset asset, boolean updated, SecurityUser user) {
try {
logEntityAction(user, asset.getId(), asset,
asset.getCustomerId(),
updated ? ActionType.UPDATED : ActionType.ADDED, null);
} catch (ThingsboardException e) {
log.error("Failed to log entity action", e);
}
if (updated) {
sendEntityNotificationMsg(asset.getTenantId(), asset.getId(), EdgeEventActionType.UPDATED);
}
}
@ApiOperation(value = "Delete asset (deleteAsset)",
notes = "Deletes the asset and all the relations (from and to the asset). Referencing non-existing asset Id will cause an error." + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@ -665,7 +681,7 @@ public class AssetController extends BaseController {
public BulkImportResult<Asset> processAssetsBulkImport(@RequestBody BulkImportRequest request) throws Exception {
SecurityUser user = getCurrentUser();
return assetBulkImportService.processBulkImport(request, user, importedAssetInfo -> {
entityActionService.onAssetCreatedOrUpdated(user, importedAssetInfo.getEntity(), !importedAssetInfo.isUpdated());
onAssetCreatedOrUpdated(importedAssetInfo.getEntity(), importedAssetInfo.isUpdated(), user);
});
}

10
application/src/main/java/org/thingsboard/server/controller/DashboardController.java

@ -186,7 +186,15 @@ public class DashboardController extends BaseController {
checkEntity(dashboard.getId(), dashboard, Resource.DASHBOARD);
Dashboard savedDashboard = checkNotNull(dashboardService.saveDashboard(dashboard));
entityActionService.onDashboardCreatedOrUpdated(getCurrentUser(), savedDashboard, dashboard.getId() == null);
logEntityAction(savedDashboard.getId(), savedDashboard,
null,
dashboard.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null);
if (dashboard.getId() != null) {
sendEntityNotificationMsg(savedDashboard.getTenantId(), savedDashboard.getId(), EdgeEventActionType.UPDATED);
}
return savedDashboard;
} catch (Exception e) {
logEntityAction(emptyId(EntityType.DASHBOARD), dashboard,

28
application/src/main/java/org/thingsboard/server/controller/DeviceController.java

@ -74,11 +74,11 @@ import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.device.DeviceBulkImportService;
import org.thingsboard.server.service.gateway_device.GatewayNotificationsService;
import org.thingsboard.server.service.sync.importing.csv.BulkImportRequest;
import org.thingsboard.server.service.sync.importing.csv.BulkImportResult;
import org.thingsboard.server.service.security.model.SecurityUser;
import org.thingsboard.server.service.security.permission.Operation;
import org.thingsboard.server.service.security.permission.Resource;
import org.thingsboard.server.service.sync.importing.csv.BulkImportRequest;
import org.thingsboard.server.service.sync.importing.csv.BulkImportResult;
import javax.annotation.Nullable;
import java.io.IOException;
@ -194,7 +194,9 @@ public class DeviceController extends BaseController {
}
Device savedDevice = checkNotNull(deviceService.saveDeviceWithAccessToken(device, accessToken));
entityActionService.onDeviceCreatedOrUpdated(getCurrentUser(), savedDevice, oldDevice, created);
onDeviceCreatedOrUpdated(savedDevice, oldDevice, !created, getCurrentUser());
return savedDevice;
} catch (Exception e) {
logEntityAction(emptyId(EntityType.DEVICE), device,
@ -222,7 +224,11 @@ public class DeviceController extends BaseController {
checkEntity(device.getId(), device, Resource.DEVICE);
Device savedDevice = deviceService.saveDeviceWithCredentials(device, credentials);
checkNotNull(savedDevice);
entityActionService.onDeviceCreatedOrUpdated(getCurrentUser(), savedDevice, device, created);
tbClusterService.onDeviceUpdated(savedDevice, device);
logEntityAction(savedDevice.getId(), savedDevice,
savedDevice.getCustomerId(),
device.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null);
return savedDevice;
} catch (Exception e) {
logEntityAction(emptyId(EntityType.DEVICE), device,
@ -231,6 +237,18 @@ public class DeviceController extends BaseController {
}
}
private void onDeviceCreatedOrUpdated(Device savedDevice, Device oldDevice, boolean updated, SecurityUser user) {
tbClusterService.onDeviceUpdated(savedDevice, oldDevice);
try {
logEntityAction(user, savedDevice.getId(), savedDevice,
savedDevice.getCustomerId(),
updated ? ActionType.UPDATED : ActionType.ADDED, null);
} catch (ThingsboardException e) {
log.error("Failed to log entity action", e);
}
}
@ApiOperation(value = "Delete device (deleteDevice)",
notes = "Deletes the device, it's credentials and all the relations (from and to the device). Referencing non-existing device Id will cause an error." + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@ -997,7 +1015,7 @@ public class DeviceController extends BaseController {
public BulkImportResult<Device> processDevicesBulkImport(@RequestBody BulkImportRequest request) throws Exception {
SecurityUser user = getCurrentUser();
return deviceBulkImportService.processBulkImport(request, user, importedDeviceInfo -> {
entityActionService.onDeviceCreatedOrUpdated(user, importedDeviceInfo.getEntity(), importedDeviceInfo.getOldEntity(), !importedDeviceInfo.isUpdated());
onDeviceCreatedOrUpdated(importedDeviceInfo.getEntity(), importedDeviceInfo.getOldEntity(), importedDeviceInfo.isUpdated(), user);
});
}

199
application/src/main/java/org/thingsboard/server/controller/EntitiesExportImportController.java

@ -17,18 +17,16 @@ package org.thingsboard.server.controller;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.ExportableEntity;
import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.EntityIdFactory;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.query.EntityData;
import org.thingsboard.server.common.data.query.EntityDataPageLink;
import org.thingsboard.server.common.data.query.EntityDataQuery;
@ -41,13 +39,17 @@ import org.thingsboard.server.dao.entity.EntityService;
import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.security.model.SecurityUser;
import org.thingsboard.server.service.sync.EntitiesExportImportService;
import org.thingsboard.server.service.sync.exporting.EntityExportSettings;
import org.thingsboard.server.service.sync.exporting.data.EntityExportData;
import org.thingsboard.server.service.sync.exporting.data.request.EntityFilterExportRequest;
import org.thingsboard.server.service.sync.exporting.data.request.EntityListExportRequest;
import org.thingsboard.server.service.sync.exporting.data.request.EntityQueryExportRequest;
import org.thingsboard.server.service.sync.exporting.data.request.EntityTypeExportRequest;
import org.thingsboard.server.service.sync.exporting.data.request.ExportRequest;
import org.thingsboard.server.service.sync.exporting.data.request.SingleEntityExportRequest;
import org.thingsboard.server.service.sync.importing.EntityImportResult;
import org.thingsboard.server.service.sync.importing.EntityImportSettings;
import org.thingsboard.server.service.sync.importing.data.request.ImportRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -60,7 +62,6 @@ import static org.thingsboard.server.dao.sql.query.EntityKeyMapping.CREATED_TIME
@RestController
@RequestMapping("/api/entities")
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@TbCoreComponent
@RequiredArgsConstructor
public class EntitiesExportImportController extends BaseController {
@ -68,101 +69,79 @@ public class EntitiesExportImportController extends BaseController {
private final EntitiesExportImportService exportImportService;
private final EntityService entityService;
@PostMapping("/export/{entityType}/{id}")
public EntityExportData<ExportableEntity<EntityId>> exportSingleEntity(@PathVariable EntityType entityType,
@PathVariable UUID id,
@RequestParam Map<String, String> exportSettingsParams) throws ThingsboardException {
@PostMapping("/export")
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
public List<EntityExportData<ExportableEntity<EntityId>>> exportEntities(@RequestBody ExportRequest exportRequest) throws ThingsboardException {
SecurityUser user = getCurrentUser();
EntityId entityId = EntityIdFactory.getByTypeAndUuid(entityType, id);
try {
return exportEntity(user, entityId, toExportSettings(exportSettingsParams));
return exportEntitiesByRequest(user, exportRequest);
} catch (Exception e) {
throw handleException(e);
}
}
@PostMapping(value = "/export/{entityType}", params = "ids")
public List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByIds(@PathVariable EntityType entityType,
@RequestParam UUID[] ids,
@RequestParam Map<String, String> exportSettingsParams) throws ThingsboardException {
@PostMapping(value = "/export", params = {"multiple"})
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
public List<EntityExportData<ExportableEntity<EntityId>>> exportEntities(@RequestBody List<ExportRequest> exportRequests) throws ThingsboardException {
SecurityUser user = getCurrentUser();
List<EntityId> entitiesIds = Arrays.stream(ids)
.map(id -> EntityIdFactory.getByTypeAndUuid(entityType, id))
.collect(Collectors.toList());
try {
return exportEntitiesByIds(user, entitiesIds, toExportSettings(exportSettingsParams));
List<EntityExportData<ExportableEntity<EntityId>>> exportDataList = new ArrayList<>();
for (ExportRequest exportRequest : exportRequests) {
exportDataList.addAll(exportEntitiesByRequest(user, exportRequest));
}
return exportDataList;
} catch (Exception e) {
throw handleException(e);
}
}
@PostMapping(value = "/export/{entityType}")
public List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByEntityType(@PathVariable EntityType entityType,
@RequestParam Map<String, String> exportSettingsParams,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "2147483647") int pageSize,
@RequestParam(name = "customerId", required = false) UUID customerUuid) throws ThingsboardException {
SecurityUser user = getCurrentUser();
CustomerId customerId = toCustomerId(customerUuid);
EntityTypeFilter entityTypeFilter = new EntityTypeFilter();
entityTypeFilter.setEntityType(entityType);
try {
return exportEntitiesByFilter(user, customerId, entityTypeFilter, page, pageSize, toExportSettings(exportSettingsParams));
} catch (Exception e) {
throw handleException(e);
}
}
private List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByRequest(SecurityUser user, ExportRequest request) throws ThingsboardException {
List<EntityId> entitiesIds = findEntitiesForRequest(user, request);
@PostMapping("/exportByFilter")
public List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByFilter(@RequestBody EntityFilter filter,
@RequestParam Map<String, String> exportSettingsParams,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "2147483647") int pageSize,
@RequestParam(name = "customerId", required = false) UUID customerUuid) throws ThingsboardException {
SecurityUser user = getCurrentUser();
CustomerId customerId = toCustomerId(customerUuid);
try {
return exportEntitiesByFilter(user, customerId, filter, page, pageSize, toExportSettings(exportSettingsParams));
} catch (Exception e) {
throw handleException(e);
List<EntityExportData<ExportableEntity<EntityId>>> exportDataList = new ArrayList<>();
for (EntityId entityId : entitiesIds) {
exportDataList.add(exportImportService.exportEntity(user, entityId, request.getExportSettings()));
}
return exportDataList;
}
// FIXME: too aggressive
@PostMapping("/exportByFilters")
public List<EntityExportData<ExportableEntity<EntityId>>> exportAllEntitiesByFilters(@RequestBody List<EntityFilter> filters,
@RequestParam Map<String, String> exportSettingsParams,
@RequestParam(name = "customerId", required = false) UUID customerUuid) throws ThingsboardException {
SecurityUser user = getCurrentUser();
CustomerId customerId = toCustomerId(customerUuid);
try {
List<EntityExportData<ExportableEntity<EntityId>>> exportDataList = new ArrayList<>();
for (EntityFilter filter : filters) {
exportDataList.addAll(exportEntitiesByFilter(user, customerId, filter, 0, Integer.MAX_VALUE, toExportSettings(exportSettingsParams)));
private List<EntityId> findEntitiesForRequest(SecurityUser user, ExportRequest request) {
switch (request.getType()) {
case SINGLE_ENTITY: {
return List.of(((SingleEntityExportRequest) request).getEntityId());
}
return exportDataList;
} catch (Exception e) {
throw handleException(e);
}
}
case ENTITY_LIST: {
return ((EntityListExportRequest) request).getEntitiesIds();
}
case ENTITY_TYPE: {
EntityTypeExportRequest exportRequest = (EntityTypeExportRequest) request;
EntityTypeFilter entityTypeFilter = new EntityTypeFilter();
entityTypeFilter.setEntityType(exportRequest.getEntityType());
@PostMapping("/exportByQuery")
public List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByQuery(@RequestBody EntityDataQuery entitiesQuery,
@RequestParam Map<String, String> exportSettingsParams,
@RequestParam(name = "customerId", required = false) UUID customerUuid) throws ThingsboardException {
SecurityUser user = getCurrentUser();
CustomerId customerId = toCustomerId(customerUuid);
try {
return exportEntitiesByQuery(user, customerId, entitiesQuery, toExportSettings(exportSettingsParams));
} catch (Exception e) {
throw handleException(e);
CustomerId customerId = Optional.ofNullable(exportRequest.getCustomerId()).orElse(emptyId(EntityType.CUSTOMER));
return findEntitiesByFilter(user.getTenantId(), customerId, entityTypeFilter, exportRequest.getPage(), exportRequest.getPageSize());
}
case ENTITY_FILTER: {
EntityFilterExportRequest exportRequest = (EntityFilterExportRequest) request;
EntityFilter filter = exportRequest.getFilter();
CustomerId customerId = Optional.ofNullable(exportRequest.getCustomerId()).orElse(emptyId(EntityType.CUSTOMER));
return findEntitiesByFilter(user.getTenantId(), customerId, filter, exportRequest.getPage(), exportRequest.getPageSize());
}
case ENTITY_QUERY:{
EntityQueryExportRequest exportRequest = (EntityQueryExportRequest) request;
EntityDataQuery query = exportRequest.getQuery();
CustomerId customerId = Optional.ofNullable(exportRequest.getCustomerId()).orElse(emptyId(EntityType.CUSTOMER));
return findEntitiesByQuery(user.getTenantId(), customerId, query);
}
default:
throw new IllegalArgumentException("Export request is not supported");
}
}
private List<EntityExportData<ExportableEntity<EntityId>>> exportEntitiesByFilter(SecurityUser user, CustomerId customerId, EntityFilter filter, int page, int pageSize, EntityExportSettings exportSettings) throws ThingsboardException {
private List<EntityId> findEntitiesByFilter(TenantId tenantId, CustomerId customerId, EntityFilter filter, int page, int pageSize) {
EntityDataPageLink pageLink = new EntityDataPageLink();
pageLink.setPage(page);
pageLink.setPageSize(pageSize);
@ -170,37 +149,23 @@ 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(user, customerId, query, exportSettings);
return findEntitiesByQuery(tenantId, customerId, query);
}
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()
private List<EntityId> findEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query) {
return entityService.findEntityDataByQuery(tenantId, customerId, query).getData().stream()
.map(EntityData::getEntityId)
.collect(Collectors.toList());
return exportEntitiesByIds(user, entitiesIds, exportSettings);
}
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(user, entityId, exportSettings));
}
return exportDataList;
}
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);
}
@PostMapping("/import")
public List<EntityImportResult<ExportableEntity<EntityId>>> importEntities(@RequestBody List<EntityExportData<ExportableEntity<EntityId>>> exportDataList,
@RequestParam Map<String, String> importSettingsParams) throws ThingsboardException {
public List<EntityImportResult<ExportableEntity<EntityId>>> importEntities(@RequestBody ImportRequest importRequest) throws ThingsboardException {
SecurityUser user = getCurrentUser();
EntityImportSettings importSettings = toImportSettings(importSettingsParams);
try {
List<EntityImportResult<ExportableEntity<EntityId>>> importResults = importEntities(user, exportDataList, importSettings);
List<EntityImportResult<ExportableEntity<EntityId>>> importResults = exportImportService.importEntities(user,
importRequest.getExportDataList(), importRequest.getImportSettings());
for (EntityImportResult<ExportableEntity<EntityId>> entityImportResult : importResults) {
if (entityImportResult.getCallback() != null) {
entityImportResult.getCallback().run();
@ -212,40 +177,4 @@ public class EntitiesExportImportController extends BaseController {
}
}
public List<EntityImportResult<ExportableEntity<EntityId>>> importEntities(SecurityUser user, List<EntityExportData<ExportableEntity<EntityId>>> exportDataList, EntityImportSettings importSettings) throws ThingsboardException {
return exportImportService.importEntities(user, exportDataList, importSettings);
}
private EntityExportSettings toExportSettings(Map<String, String> exportSettingsParams) {
return EntityExportSettings.builder()
.exportInboundRelations(getBooleanParam(exportSettingsParams, "exportInboundRelations", false))
.exportOutboundRelations(getBooleanParam(exportSettingsParams, "exportOutboundRelations", false))
.build();
}
private EntityImportSettings toImportSettings(Map<String, String> importSettingsParams) {
return EntityImportSettings.builder()
.findExistingByName(getBooleanParam(importSettingsParams, "findExistingByName", false))
.importInboundRelations(getBooleanParam(importSettingsParams, "importInboundRelations", false))
.importOutboundRelations(getBooleanParam(importSettingsParams, "importOutboundRelations", false))
.removeExistingRelations(getBooleanParam(importSettingsParams, "removeExistingRelations", true))
.updateReferencesToOtherEntities(getBooleanParam(importSettingsParams, "updateReferencesToOtherEntities", true))
.build();
}
protected static boolean getBooleanParam(Map<String, String> requestParams, String key, boolean defaultValue) {
return getParam(requestParams, key, defaultValue, Boolean::parseBoolean);
}
protected static <T> T getParam(Map<String, String> requestParams, String key, T defaultValue, Function<String, T> parsingFunction) {
return parsingFunction.apply(requestParams.getOrDefault(key, defaultValue.toString()));
}
private static CustomerId toCustomerId(UUID customerUuid) {
return new CustomerId(Optional.ofNullable(customerUuid).orElse(EntityId.NULL_UUID));
}
}

17
application/src/main/java/org/thingsboard/server/controller/RuleChainController.java

@ -249,10 +249,25 @@ public class RuleChainController extends BaseController {
try {
boolean created = ruleChain.getId() == null;
ruleChain.setTenantId(getCurrentUser().getTenantId());
checkEntity(ruleChain.getId(), ruleChain, Resource.RULE_CHAIN);
RuleChain savedRuleChain = checkNotNull(ruleChainService.saveRuleChain(ruleChain));
entityActionService.onRuleChainCreatedOrUpdated(getCurrentUser(), savedRuleChain, created);
if (RuleChainType.CORE.equals(savedRuleChain.getType())) {
tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), savedRuleChain.getId(),
created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED);
}
logEntityAction(savedRuleChain.getId(), savedRuleChain,
null,
created ? ActionType.ADDED : ActionType.UPDATED, null);
if (RuleChainType.EDGE.equals(savedRuleChain.getType())) {
if (!created) {
sendEntityNotificationMsg(savedRuleChain.getTenantId(), savedRuleChain.getId(), EdgeEventActionType.UPDATED);
}
}
return savedRuleChain;
} catch (Exception e) {

61
application/src/main/java/org/thingsboard/server/service/action/EntityActionService.java

@ -22,18 +22,14 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Dashboard;
import org.thingsboard.server.cluster.TbClusterService;
import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.HasName;
import org.thingsboard.server.common.data.HasTenantId;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.audit.ActionType;
import org.thingsboard.server.common.data.edge.EdgeEventActionType;
import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.TenantId;
@ -41,20 +37,12 @@ import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.DataType;
import org.thingsboard.server.common.data.kv.KvEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
import org.thingsboard.server.common.data.rule.RuleChain;
import org.thingsboard.server.common.data.rule.RuleChainMetaData;
import org.thingsboard.server.common.data.rule.RuleChainType;
import org.thingsboard.server.common.data.rule.RuleChainUpdateResult;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgDataType;
import org.thingsboard.server.common.msg.TbMsgMetaData;
import org.thingsboard.server.dao.audit.AuditLogService;
import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.cluster.TbClusterService;
import org.thingsboard.server.service.security.model.SecurityUser;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -235,6 +223,9 @@ public class EntityActionService {
auditLogService.logEntityAction(user.getTenantId(), customerId, user.getId(), user.getName(), entityId, entity, actionType, e, additionalInfo);
}
public void sendEntityNotificationMsgToEdgeService(TenantId tenantId, EntityId entityId, EdgeEventActionType action) {
tbClusterService.sendNotificationMsgToEdgeService(tenantId, null, entityId, null, null, action);
}
private <T> T extractParameter(Class<T> clazz, int index, Object... additionalInfo) {
T result = null;
@ -280,48 +271,4 @@ public class EntityActionService {
}
}
public void onDeviceCreatedOrUpdated(SecurityUser user, Device savedDevice, Device oldDevice, boolean newEntity) {
tbClusterService.onDeviceUpdated(savedDevice, oldDevice);
logEntityAction(user, savedDevice.getId(), savedDevice, savedDevice.getCustomerId(),
newEntity ? ActionType.ADDED : ActionType.UPDATED, null);
}
public void onAssetCreatedOrUpdated(SecurityUser user, Asset savedAsset, boolean newEntity) {
logEntityAction(user, savedAsset.getId(), savedAsset, savedAsset.getCustomerId(),
newEntity ? ActionType.ADDED : ActionType.UPDATED, null);
if (!newEntity) {
tbClusterService.sendNotificationMsgToEdgeService(user.getTenantId(), null, savedAsset.getId(),
null, null, EdgeEventActionType.UPDATED);
}
}
public void onDashboardCreatedOrUpdated(SecurityUser user, Dashboard savedDashboard, boolean newEntity) {
logEntityAction(user, savedDashboard.getId(), savedDashboard, null,
newEntity ? ActionType.ADDED : ActionType.UPDATED, null);
if (!newEntity) {
tbClusterService.sendNotificationMsgToEdgeService(user.getTenantId(), null, savedDashboard.getId(),
null, null, EdgeEventActionType.UPDATED);
}
}
public void onRuleChainCreatedOrUpdated(SecurityUser user, RuleChain savedRuleChain, boolean newEntity) {
if (RuleChainType.CORE.equals(savedRuleChain.getType())) {
tbClusterService.broadcastEntityStateChangeEvent(user.getTenantId(), savedRuleChain.getId(),
newEntity ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED);
}
logEntityAction(user, savedRuleChain.getId(), savedRuleChain, null,
newEntity ? ActionType.ADDED : ActionType.UPDATED, null);
if (RuleChainType.EDGE.equals(savedRuleChain.getType())) {
if (!newEntity) {
tbClusterService.sendNotificationMsgToEdgeService(user.getTenantId(), null, savedRuleChain.getId(),
null, null, EdgeEventActionType.UPDATED);
}
}
}
public void onCustomerCreatedOrUpdated(SecurityUser user, Customer savedCustomer) {
}
}

2
application/src/main/java/org/thingsboard/server/service/sync/DefaultEntitiesExportImportService.java

@ -35,7 +35,7 @@ import org.thingsboard.server.service.security.permission.AccessControlService;
import org.thingsboard.server.service.security.permission.Operation;
import org.thingsboard.server.service.security.permission.Resource;
import org.thingsboard.server.service.sync.exporting.EntityExportService;
import org.thingsboard.server.service.sync.exporting.EntityExportSettings;
import org.thingsboard.server.service.sync.exporting.data.request.EntityExportSettings;
import org.thingsboard.server.service.sync.exporting.ExportableEntitiesService;
import org.thingsboard.server.service.sync.exporting.data.EntityExportData;
import org.thingsboard.server.service.sync.importing.EntityImportResult;

2
application/src/main/java/org/thingsboard/server/service/sync/EntitiesExportImportService.java

@ -19,7 +19,7 @@ import org.thingsboard.server.common.data.ExportableEntity;
import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.service.security.model.SecurityUser;
import org.thingsboard.server.service.sync.exporting.EntityExportSettings;
import org.thingsboard.server.service.sync.exporting.data.request.EntityExportSettings;
import org.thingsboard.server.service.sync.exporting.data.EntityExportData;
import org.thingsboard.server.service.sync.importing.EntityImportResult;
import org.thingsboard.server.service.sync.importing.EntityImportSettings;

1
application/src/main/java/org/thingsboard/server/service/sync/exporting/EntityExportService.java

@ -21,6 +21,7 @@ import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.service.security.model.SecurityUser;
import org.thingsboard.server.service.sync.exporting.data.EntityExportData;
import org.thingsboard.server.service.sync.exporting.data.request.EntityExportSettings;
public interface EntityExportService<I extends EntityId, E extends ExportableEntity<I>, D extends EntityExportData<E>> {

8
application/src/main/java/org/thingsboard/server/service/sync/exporting/EntityExportSettings.java → application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/EntityExportSettings.java

@ -13,17 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.service.sync.exporting;
package org.thingsboard.server.service.sync.exporting.data.request;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class EntityExportSettings {
private boolean exportInboundRelations;
private boolean exportOutboundRelations;

37
application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/EntityFilterExportRequest.java

@ -0,0 +1,37 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.service.sync.exporting.data.request;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.query.EntityFilter;
@EqualsAndHashCode(callSuper = true)
@Data
public class EntityFilterExportRequest extends ExportRequest {
private EntityFilter filter;
private int page;
private int pageSize;
private CustomerId customerId;
@Override
public ExportRequestType getType() {
return ExportRequestType.ENTITY_FILTER;
}
}

35
application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/EntityListExportRequest.java

@ -0,0 +1,35 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.service.sync.exporting.data.request;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.id.EntityId;
import java.util.List;
@EqualsAndHashCode(callSuper = true)
@Data
public class EntityListExportRequest extends ExportRequest {
private List<EntityId> entitiesIds;
@Override
public ExportRequestType getType() {
return ExportRequestType.ENTITY_LIST;
}
}

35
application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/EntityQueryExportRequest.java

@ -0,0 +1,35 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.service.sync.exporting.data.request;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.query.EntityDataQuery;
@EqualsAndHashCode(callSuper = true)
@Data
public class EntityQueryExportRequest extends ExportRequest {
private EntityDataQuery query;
private CustomerId customerId;
@Override
public ExportRequestType getType() {
return ExportRequestType.ENTITY_QUERY;
}
}

37
application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/EntityTypeExportRequest.java

@ -0,0 +1,37 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.service.sync.exporting.data.request;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.CustomerId;
@EqualsAndHashCode(callSuper = true)
@Data
public class EntityTypeExportRequest extends ExportRequest {
private EntityType entityType;
private int page;
private int pageSize;
private CustomerId customerId;
@Override
public ExportRequestType getType() {
return ExportRequestType.ENTITY_TYPE;
}
}

41
application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/ExportRequest.java

@ -0,0 +1,41 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.service.sync.exporting.data.request;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import lombok.Data;
@JsonTypeInfo(use = Id.NAME, property = "type")
@JsonSubTypes({
@Type(name = "SINGLE_ENTITY", value = SingleEntityExportRequest.class),
@Type(name = "ENTITY_LIST", value = EntityListExportRequest.class),
@Type(name = "ENTITY_TYPE", value = EntityTypeExportRequest.class),
@Type(name = "ENTITY_FILTER", value = EntityFilterExportRequest.class),
@Type(name = "ENTITY_QUERY", value = EntityQueryExportRequest.class)
})
@Data
public abstract class ExportRequest {
private EntityExportSettings exportSettings;
@JsonIgnore
public abstract ExportRequestType getType();
}

25
application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/ExportRequestType.java

@ -0,0 +1,25 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.service.sync.exporting.data.request;
public enum ExportRequestType {
SINGLE_ENTITY,
ENTITY_LIST,
ENTITY_TYPE,
ENTITY_FILTER,
ENTITY_QUERY
}

33
application/src/main/java/org/thingsboard/server/service/sync/exporting/data/request/SingleEntityExportRequest.java

@ -0,0 +1,33 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.service.sync.exporting.data.request;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.id.EntityId;
@EqualsAndHashCode(callSuper = true)
@Data
public class SingleEntityExportRequest extends ExportRequest {
private EntityId entityId;
@Override
public ExportRequestType getType() {
return ExportRequestType.SINGLE_ENTITY;
}
}

2
application/src/main/java/org/thingsboard/server/service/sync/exporting/impl/BaseEntityExportService.java

@ -27,7 +27,7 @@ import org.thingsboard.server.dao.relation.RelationService;
import org.thingsboard.server.service.security.model.SecurityUser;
import org.thingsboard.server.service.security.permission.Operation;
import org.thingsboard.server.service.sync.exporting.EntityExportService;
import org.thingsboard.server.service.sync.exporting.EntityExportSettings;
import org.thingsboard.server.service.sync.exporting.data.request.EntityExportSettings;
import org.thingsboard.server.service.sync.exporting.ExportableEntitiesService;
import org.thingsboard.server.service.sync.exporting.data.EntityExportData;

32
application/src/main/java/org/thingsboard/server/service/sync/importing/data/request/ImportRequest.java

@ -0,0 +1,32 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.service.sync.importing.data.request;
import lombok.Data;
import org.thingsboard.server.common.data.ExportableEntity;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.service.sync.exporting.data.EntityExportData;
import org.thingsboard.server.service.sync.importing.EntityImportSettings;
import java.util.List;
@Data
public class ImportRequest {
private List<EntityExportData<ExportableEntity<EntityId>>> exportDataList;
private EntityImportSettings importSettings;
}

9
application/src/main/java/org/thingsboard/server/service/sync/importing/impl/AssetImportService.java

@ -19,6 +19,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.edge.EdgeEventActionType;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.asset.AssetService;
@ -47,9 +48,11 @@ public class AssetImportService extends BaseEntityImportService<AssetId, Asset,
@Override
protected ThrowingRunnable getCallback(SecurityUser user, Asset savedAsset, Asset oldAsset) {
return () -> {
entityActionService.onAssetCreatedOrUpdated(user, savedAsset, oldAsset == null);
};
return super.getCallback(user, savedAsset, oldAsset).andThen(() -> {
if (oldAsset != null) {
entityActionService.sendEntityNotificationMsgToEdgeService(user.getTenantId(), savedAsset.getId(), EdgeEventActionType.UPDATED);
}
});
}
@Override

10
application/src/main/java/org/thingsboard/server/service/sync/importing/impl/BaseEntityImportService.java

@ -20,8 +20,11 @@ import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;
import org.thingsboard.server.cluster.TbClusterService;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.ExportableEntity;
import org.thingsboard.server.common.data.HasCustomerId;
import org.thingsboard.server.common.data.audit.ActionType;
import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.HasId;
@ -56,6 +59,8 @@ public abstract class BaseEntityImportService<I extends EntityId, E extends Expo
private RelationService relationService;
@Autowired
protected EntityActionService entityActionService;
@Autowired
protected TbClusterService clusterService;
@Transactional(rollbackFor = Exception.class)
@Override
@ -139,7 +144,10 @@ public abstract class BaseEntityImportService<I extends EntityId, E extends Expo
}
protected ThrowingRunnable getCallback(SecurityUser user, E savedEntity, E oldEntity) {
return () -> {};
return () -> {
entityActionService.logEntityAction(user, savedEntity.getId(), savedEntity, savedEntity instanceof HasCustomerId ? ((HasCustomerId) savedEntity).getCustomerId() : user.getCustomerId(),
oldEntity == null ? ActionType.ADDED : ActionType.UPDATED, null);
};
}

9
application/src/main/java/org/thingsboard/server/service/sync/importing/impl/CustomerImportService.java

@ -19,6 +19,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.edge.EdgeEventActionType;
import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.TenantId;
@ -48,9 +49,11 @@ public class CustomerImportService extends BaseEntityImportService<CustomerId, C
@Override
protected ThrowingRunnable getCallback(SecurityUser user, Customer savedCustomer, Customer oldCustomer) {
return () -> {
entityActionService.onCustomerCreatedOrUpdated(user, savedCustomer);
};
return super.getCallback(user, savedCustomer, oldCustomer).andThen(() -> {
if (oldCustomer != null) {
entityActionService.sendEntityNotificationMsgToEdgeService(user.getTenantId(), savedCustomer.getId(), EdgeEventActionType.UPDATED);
}
});
}
@Override

12
application/src/main/java/org/thingsboard/server/service/sync/importing/impl/DashboardImportService.java

@ -23,7 +23,7 @@ import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.Dashboard;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.ShortCustomerInfo;
import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.edge.EdgeEventActionType;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.EntityId;
@ -35,14 +35,12 @@ import org.thingsboard.server.dao.sql.query.DefaultEntityQueryRepository;
import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.security.model.SecurityUser;
import org.thingsboard.server.service.sync.exporting.data.DashboardExportData;
import org.thingsboard.server.service.sync.importing.EntityImportSettings;
import org.thingsboard.server.utils.ThrowingRunnable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -114,9 +112,11 @@ public class DashboardImportService extends BaseEntityImportService<DashboardId,
@Override
protected ThrowingRunnable getCallback(SecurityUser user, Dashboard savedDashboard, Dashboard oldDashboard) {
return () -> {
entityActionService.onDashboardCreatedOrUpdated(user, savedDashboard, oldDashboard == null);
};
return super.getCallback(user, savedDashboard, oldDashboard).andThen(() -> {
if (oldDashboard != null) {
entityActionService.sendEntityNotificationMsgToEdgeService(user.getTenantId(), savedDashboard.getId(), EdgeEventActionType.UPDATED);
}
});
}
@Override

8
application/src/main/java/org/thingsboard/server/service/sync/importing/impl/DeviceImportService.java

@ -17,17 +17,14 @@ package org.thingsboard.server.service.sync.importing.impl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.thingsboard.server.cluster.TbClusterService;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.device.DeviceService;
import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.security.model.SecurityUser;
import org.thingsboard.server.service.sync.exporting.data.DeviceExportData;
import org.thingsboard.server.service.sync.importing.EntityImportSettings;
import org.thingsboard.server.utils.ThrowingRunnable;
@Service
@ -36,7 +33,6 @@ import org.thingsboard.server.utils.ThrowingRunnable;
public class DeviceImportService extends BaseEntityImportService<DeviceId, Device, DeviceExportData> {
private final DeviceService deviceService;
private final TbClusterService clusterService;
@Override
protected void setOwner(TenantId tenantId, Device device, NewIdProvider idProvider) {
@ -60,9 +56,9 @@ public class DeviceImportService extends BaseEntityImportService<DeviceId, Devic
@Override
protected ThrowingRunnable getCallback(SecurityUser user, Device savedDevice, Device oldDevice) {
return () -> {
return super.getCallback(user, savedDevice, oldDevice).andThen(() -> {
clusterService.onDeviceUpdated(savedDevice, oldDevice);
};
});
}
@Override

19
application/src/main/java/org/thingsboard/server/service/sync/importing/impl/DeviceProfileImportService.java

@ -19,20 +19,26 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.edge.EdgeEventActionType;
import org.thingsboard.server.common.data.id.DeviceProfileId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
import org.thingsboard.server.dao.device.DeviceProfileService;
import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.ota.OtaPackageStateService;
import org.thingsboard.server.service.security.model.SecurityUser;
import org.thingsboard.server.service.sync.exporting.data.DeviceProfileExportData;
import org.thingsboard.server.utils.ThrowingRunnable;
import java.util.Objects;
@Service
@TbCoreComponent
@RequiredArgsConstructor
public class DeviceProfileImportService extends BaseEntityImportService<DeviceProfileId, DeviceProfile, DeviceProfileExportData> {
private final DeviceProfileService deviceProfileService;
private final OtaPackageStateService otaPackageStateService;
@Override
protected void setOwner(TenantId tenantId, DeviceProfile deviceProfile, NewIdProvider idProvider) {
@ -50,9 +56,16 @@ public class DeviceProfileImportService extends BaseEntityImportService<DevicePr
@Override
protected ThrowingRunnable getCallback(SecurityUser user, DeviceProfile savedDeviceProfile, DeviceProfile oldDeviceProfile) {
return () -> {
};
return super.getCallback(user, savedDeviceProfile, oldDeviceProfile).andThen(() -> {
clusterService.onDeviceProfileChange(savedDeviceProfile, null);
clusterService.broadcastEntityStateChangeEvent(user.getTenantId(), savedDeviceProfile.getId(),
oldDeviceProfile == null ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED);
entityActionService.sendEntityNotificationMsgToEdgeService(user.getTenantId(), savedDeviceProfile.getId(),
oldDeviceProfile == null ? EdgeEventActionType.ADDED : EdgeEventActionType.UPDATED);
otaPackageStateService.update(savedDeviceProfile,
oldDeviceProfile != null && !Objects.equals(oldDeviceProfile.getFirmwareId(), savedDeviceProfile.getFirmwareId()),
oldDeviceProfile != null && !Objects.equals(oldDeviceProfile.getSoftwareId(), savedDeviceProfile.getSoftwareId()));
});
}
@Override

14
application/src/main/java/org/thingsboard/server/service/sync/importing/impl/RuleChainImportService.java

@ -21,10 +21,13 @@ import com.fasterxml.jackson.databind.node.TextNode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.edge.EdgeEventActionType;
import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
import org.thingsboard.server.common.data.rule.RuleChain;
import org.thingsboard.server.common.data.rule.RuleChainMetaData;
import org.thingsboard.server.common.data.rule.RuleChainType;
import org.thingsboard.server.dao.rule.RuleChainService;
import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.security.model.SecurityUser;
@ -83,9 +86,14 @@ public class RuleChainImportService extends BaseEntityImportService<RuleChainId,
@Override
protected ThrowingRunnable getCallback(SecurityUser user, RuleChain savedRuleChain, RuleChain oldRuleChain) {
return () -> {
};
return super.getCallback(user, savedRuleChain, oldRuleChain).andThen(() -> {
if (savedRuleChain.getType() == RuleChainType.CORE) {
clusterService.broadcastEntityStateChangeEvent(user.getTenantId(), savedRuleChain.getId(),
oldRuleChain == null ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED);
} else if (savedRuleChain.getType() == RuleChainType.EDGE && oldRuleChain != null) {
entityActionService.sendEntityNotificationMsgToEdgeService(user.getTenantId(), savedRuleChain.getId(), EdgeEventActionType.UPDATED);
}
});
}
@Override

7
application/src/main/java/org/thingsboard/server/utils/ThrowingRunnable.java

@ -21,4 +21,11 @@ public interface ThrowingRunnable {
void run() throws ThingsboardException;
default ThrowingRunnable andThen(ThrowingRunnable after) {
return () -> {
this.run();
after.run();
};
}
}

2
application/src/test/java/org/thingsboard/server/controller/sql/EntitiesExportImportControllerSqlTest.java

@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.JavaType;
import lombok.SneakyThrows;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.ResultActions;
@ -54,6 +55,7 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@Ignore
@DaoSqlTest
public class EntitiesExportImportControllerSqlTest extends BaseEntitiesExportImportControllerTest {

Loading…
Cancel
Save