Browse Source

Add more options for entity export and import settings; refactoring

pull/6357/head
Viacheslav Klimov 5 years ago
parent
commit
6dc962882e
  1. 0
      application/src/main/data/upgrade/3.3.4/schema_update.sql
  2. 2
      application/src/main/java/org/thingsboard/server/controller/BaseController.java
  3. 49
      application/src/main/java/org/thingsboard/server/controller/EntitiesExportImportController.java
  4. 1
      application/src/main/java/org/thingsboard/server/service/exportimport/exporting/EntityExportSettings.java
  5. 3
      application/src/main/java/org/thingsboard/server/service/exportimport/exporting/data/EntityExportData.java
  6. 12
      application/src/main/java/org/thingsboard/server/service/exportimport/exporting/impl/AbstractEntityExportService.java
  7. 6
      application/src/main/java/org/thingsboard/server/service/exportimport/importing/EntityImportService.java
  8. 4
      application/src/main/java/org/thingsboard/server/service/exportimport/importing/EntityImportSettings.java
  9. 83
      application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/AbstractEntityImportService.java
  10. 9
      application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/AssetImportService.java
  11. 4
      application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/CustomerImportService.java
  12. 17
      application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/DashboardImportService.java
  13. 15
      application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/DeviceImportService.java
  14. 15
      application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/DeviceProfileImportService.java
  15. 8
      application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/RuleChainImportService.java
  16. 194
      dao/src/main/resources/sql/schema-entities.sql

0
application/src/main/data/upgrade/3.4/schema_update.sql → application/src/main/data/upgrade/3.3.4/schema_update.sql

2
application/src/main/java/org/thingsboard/server/controller/BaseController.java

@ -924,7 +924,7 @@ public abstract class BaseController {
} }
} }
public <E extends HasName & HasId<I> & HasTenantId, I extends EntityId> void onEntityUpdatedOrCreated(User user, E savedEntity, E oldEntity, boolean isNewEntity) { protected <E extends HasName & HasId<I> & HasTenantId, I extends EntityId> void onEntityUpdatedOrCreated(User user, E savedEntity, E oldEntity, boolean isNewEntity) {
boolean notifyEdgeService = false; boolean notifyEdgeService = false;
EntityType entityType = savedEntity.getId().getEntityType(); EntityType entityType = savedEntity.getId().getEntityType();

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

@ -72,9 +72,28 @@ public class EntitiesExportImportController extends BaseController {
// TODO [viacheslav]: api to export and import whole customer, whole tenant // TODO [viacheslav]: api to export and import whole customer, whole tenant
@PostMapping("/export/{entityType}/{entityId}")
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
public EntityExportData<?> exportEntity(@ApiParam(allowableValues = "DEVICE, DEVICE_PROFILE, ASSET, RULE_CHAIN, DASHBOARD, CUSTOMER")
@PathVariable EntityType entityType,
@PathVariable("entityId") UUID entityUuid,
@RequestParam(defaultValue = "false") boolean exportInboundRelations) throws ThingsboardException {
EntityId entityId = EntityIdFactory.getByTypeAndUuid(entityType, entityUuid);
checkEntityId(entityId, Operation.READ);
SecurityUser user = getCurrentUser();
EntityExportSettings exportSettings = toExportSettings(exportInboundRelations);
try { // FIXME [viacheslav]: check read permission for relation fromId
return exportImportService.exportEntity(user.getTenantId(), entityId, exportSettings);
} catch (Exception e) {
throw handleException(e);
}
}
@PostMapping("/exportByFilter") @PostMapping("/exportByFilter")
@PreAuthorize("hasAuthority('TENANT_ADMIN')") @PreAuthorize("hasAuthority('TENANT_ADMIN')")
public List<EntityExportData<?>> exportEntitiesByFilter(@RequestBody EntityFilter filter, public List<EntityExportData<?>> exportEntitiesByFilter(@RequestBody EntityFilter filter, // TODO [viacheslav]: exportInboundRelations, exportOutboundRelations
@RequestParam(defaultValue = "false") boolean exportInboundRelations, @RequestParam(defaultValue = "false") boolean exportInboundRelations,
@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "100") int pageSize) throws ThingsboardException { @RequestParam(defaultValue = "100") int pageSize) throws ThingsboardException {
@ -118,25 +137,6 @@ public class EntitiesExportImportController extends BaseController {
} }
} }
@PostMapping("/export/{entityType}/{entityId}")
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
public EntityExportData<?> exportEntity(@ApiParam(allowableValues = "DEVICE, DEVICE_PROFILE, ASSET, RULE_CHAIN, DASHBOARD, CUSTOMER")
@PathVariable EntityType entityType,
@PathVariable("entityId") UUID entityUuid,
@RequestParam(defaultValue = "false") boolean exportInboundRelations) throws ThingsboardException {
EntityId entityId = EntityIdFactory.getByTypeAndUuid(entityType, entityUuid);
checkEntityId(entityId, Operation.READ);
SecurityUser user = getCurrentUser();
EntityExportSettings exportSettings = toExportSettings(exportInboundRelations);
try { // FIXME [viacheslav]: check read permission for relation fromId
return exportImportService.exportEntity(user.getTenantId(), entityId, exportSettings);
} catch (Exception e) {
throw handleException(e);
}
}
@PostMapping("/import") @PostMapping("/import")
@PreAuthorize("hasAuthority('TENANT_ADMIN')") @PreAuthorize("hasAuthority('TENANT_ADMIN')")
@ -150,7 +150,11 @@ public class EntitiesExportImportController extends BaseController {
} }
try { try {
return exportImportService.importEntities(user.getTenantId(), exportDataList, importSettings); List<EntityImportResult<ExportableEntity<EntityId>>> importResultList = exportImportService.importEntities(user.getTenantId(), exportDataList, importSettings);
importResultList.forEach(entityImportResult -> {
onEntityUpdatedOrCreated(user, entityImportResult.getSavedEntity(), entityImportResult.getOldEntity(), entityImportResult.getOldEntity() == null);
});
return importResultList;
} catch (Exception e) { } catch (Exception e) {
throw handleException(e); throw handleException(e);
} }
@ -188,10 +192,9 @@ public class EntitiesExportImportController extends BaseController {
private EntityImportSettings toImportSettings(boolean importInboundRelations) { private EntityImportSettings toImportSettings(boolean importInboundRelations) {
EntityImportSettings importSettings = EntityImportSettings.builder() return EntityImportSettings.builder()
.importInboundRelations(importInboundRelations) .importInboundRelations(importInboundRelations)
.build(); .build();
return importSettings;
} }
private EntityExportSettings toExportSettings(boolean exportInboundRelations) { private EntityExportSettings toExportSettings(boolean exportInboundRelations) {

1
application/src/main/java/org/thingsboard/server/service/exportimport/exporting/EntityExportSettings.java

@ -26,4 +26,5 @@ import lombok.NoArgsConstructor;
@Builder @Builder
public class EntityExportSettings { public class EntityExportSettings {
private boolean exportInboundRelations; private boolean exportInboundRelations;
private boolean exportOutboundRelations;
} }

3
application/src/main/java/org/thingsboard/server/service/exportimport/exporting/data/EntityExportData.java

@ -39,12 +39,13 @@ import java.util.List;
@Type(name = "DASHBOARD", value = DashboardExportData.class), @Type(name = "DASHBOARD", value = DashboardExportData.class),
@Type(name = "CUSTOMER", value = CustomerExportData.class) @Type(name = "CUSTOMER", value = CustomerExportData.class)
}) })
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data @Data
public abstract class EntityExportData<E extends ExportableEntity<? extends EntityId>> { public abstract class EntityExportData<E extends ExportableEntity<? extends EntityId>> {
private E mainEntity; private E mainEntity;
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<EntityRelation> inboundRelations; private List<EntityRelation> inboundRelations;
private List<EntityRelation> outboundRelations;
@JsonIgnore @JsonIgnore
public abstract EntityType getEntityType(); public abstract EntityType getEntityType();

12
application/src/main/java/org/thingsboard/server/service/exportimport/exporting/impl/AbstractEntityExportService.java

@ -45,17 +45,19 @@ public abstract class AbstractEntityExportService<I extends EntityId, E extends
E mainEntity = exportableEntitiesService.findEntityById(tenantId, entityId); E mainEntity = exportableEntitiesService.findEntityById(tenantId, entityId);
exportData.setMainEntity(mainEntity); exportData.setMainEntity(mainEntity);
setRelatedEntities(tenantId, mainEntity, exportData); setRelatedEntities(tenantId, mainEntity, exportData);
if (exportSettings.isExportInboundRelations()) { if (exportSettings.isExportInboundRelations()) {
exportData.setInboundRelations(getInboundRelations(tenantId, entityId)); List<EntityRelation> inboundRelations = relationService.findByTo(tenantId, entityId, RelationTypeGroup.COMMON);
exportData.setInboundRelations(inboundRelations);
}
if (exportSettings.isExportOutboundRelations()) {
List<EntityRelation> outboundRelations = relationService.findByFrom(tenantId, entityId, RelationTypeGroup.COMMON);
exportData.setOutboundRelations(outboundRelations);
} }
return exportData; return exportData;
} }
protected List<EntityRelation> getInboundRelations(TenantId tenantId, I entityId) {
return relationService.findByTo(tenantId, entityId, RelationTypeGroup.COMMON); // FIXME [viacheslav]: RelationTypeGroup
}
protected void setRelatedEntities(TenantId tenantId, E mainEntity, D exportData) {} protected void setRelatedEntities(TenantId tenantId, E mainEntity, D exportData) {}
protected abstract D newExportData(); protected abstract D newExportData();

6
application/src/main/java/org/thingsboard/server/service/exportimport/importing/EntityImportService.java

@ -23,12 +23,6 @@ import org.thingsboard.server.service.exportimport.exporting.data.EntityExportDa
public interface EntityImportService<I extends EntityId, E extends ExportableEntity<I>, D extends EntityExportData<E>> { public interface EntityImportService<I extends EntityId, E extends ExportableEntity<I>, D extends EntityExportData<E>> {
/*
* TODO [viacheslav]: should be as options:
* to update related entities e.g. firmware or device profile if entity already exists
* to delete current relations when importing new
* to ignore when cannot find linked entity by external id
* */
EntityImportResult<E> importEntity(TenantId tenantId, D exportData, EntityImportSettings importSettings); EntityImportResult<E> importEntity(TenantId tenantId, D exportData, EntityImportSettings importSettings);
EntityType getEntityType(); EntityType getEntityType();

4
application/src/main/java/org/thingsboard/server/service/exportimport/importing/EntityImportSettings.java

@ -26,4 +26,8 @@ import lombok.NoArgsConstructor;
@Builder @Builder
public class EntityImportSettings { public class EntityImportSettings {
private boolean importInboundRelations; private boolean importInboundRelations;
private boolean importOutboundRelations;
private boolean removeExistingRelationsAndSaveNew;
private boolean updateReferencesToOtherEntities;
} }

83
application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/AbstractEntityImportService.java

@ -18,10 +18,13 @@ package org.thingsboard.server.service.exportimport.importing.impl;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;
import org.thingsboard.server.common.data.ExportableEntity; import org.thingsboard.server.common.data.ExportableEntity;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.HasId; import org.thingsboard.server.common.data.id.HasId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.relation.RelationTypeGroup;
import org.thingsboard.server.dao.relation.RelationService; import org.thingsboard.server.dao.relation.RelationService;
import org.thingsboard.server.service.exportimport.ExportableEntitiesService; import org.thingsboard.server.service.exportimport.ExportableEntitiesService;
import org.thingsboard.server.service.exportimport.exporting.data.EntityExportData; import org.thingsboard.server.service.exportimport.exporting.data.EntityExportData;
@ -29,6 +32,11 @@ import org.thingsboard.server.service.exportimport.importing.EntityImportResult;
import org.thingsboard.server.service.exportimport.importing.EntityImportService; import org.thingsboard.server.service.exportimport.importing.EntityImportService;
import org.thingsboard.server.service.exportimport.importing.EntityImportSettings; import org.thingsboard.server.service.exportimport.importing.EntityImportSettings;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class AbstractEntityImportService<I extends EntityId, E extends ExportableEntity<I>, D extends EntityExportData<E>> implements EntityImportService<I, E, D> { public abstract class AbstractEntityImportService<I extends EntityId, E extends ExportableEntity<I>, D extends EntityExportData<E>> implements EntityImportService<I, E, D> {
@Autowired @Lazy @Autowired @Lazy
@ -36,11 +44,11 @@ public abstract class AbstractEntityImportService<I extends EntityId, E extends
@Autowired @Autowired
private RelationService relationService; private RelationService relationService;
@Transactional
@Override @Override
public final EntityImportResult<E> importEntity(TenantId tenantId, D exportData, EntityImportSettings importSettings) { public EntityImportResult<E> importEntity(TenantId tenantId, D exportData, EntityImportSettings importSettings) {
E entity = exportData.getMainEntity(); E entity = exportData.getMainEntity();
E existingEntity = findByExternalId(tenantId, entity.getId()); E existingEntity = exportableEntitiesService.findEntityByExternalId(tenantId, entity.getId());
entity.setExternalId(entity.getId()); entity.setExternalId(entity.getId());
entity.setTenantId(tenantId); entity.setTenantId(tenantId);
@ -51,18 +59,19 @@ public abstract class AbstractEntityImportService<I extends EntityId, E extends
entity.setId(existingEntity.getId()); entity.setId(existingEntity.getId());
} }
E savedEntity = prepareAndSaveEntity(tenantId, entity, existingEntity, exportData, importSettings); setLinkedEntitiesIds(tenantId, entity, new IdProvider<E>() {
@Override
if (importSettings.isImportInboundRelations() && CollectionUtils.isNotEmpty(exportData.getInboundRelations())) { public <ID extends EntityId> ID get(TenantId tenantId, Function<E, ID> idExtractor) {
if (existingEntity != null) { if (existingEntity == null || importSettings.isUpdateReferencesToOtherEntities()) {
relationService.deleteEntityRelations(tenantId, savedEntity.getId()); return getInternalId(tenantId, idExtractor.apply(entity));
} else {
return idExtractor.apply(existingEntity);
}
} }
exportData.getInboundRelations().forEach(relation -> { });
relation.setTo(savedEntity.getId());
relation.setFrom(getInternalId(tenantId, relation.getFrom())); E savedEntity = saveEntity(tenantId, entity, existingEntity, exportData);
relationService.saveRelation(tenantId, relation); importRelations(tenantId, savedEntity, existingEntity, exportData, importSettings);
});
}
EntityImportResult<E> importResult = new EntityImportResult<>(); EntityImportResult<E> importResult = new EntityImportResult<>();
importResult.setSavedEntity(savedEntity); importResult.setSavedEntity(savedEntity);
@ -70,16 +79,46 @@ public abstract class AbstractEntityImportService<I extends EntityId, E extends
return importResult; return importResult;
} }
protected abstract E prepareAndSaveEntity(TenantId tenantId, E entity, E existingEntity, D exportData, EntityImportSettings importSettings); protected void setLinkedEntitiesIds(TenantId tenantId, E entity, IdProvider<E> idProvider) {}
protected abstract E saveEntity(TenantId tenantId, E entity, E existingEntity, D exportData);
private void importRelations(TenantId tenantId, E savedEntity, E existingEntity, D exportData, EntityImportSettings importSettings) {
List<EntityRelation> newRelations = new LinkedList<>();
private E findByExternalId(TenantId tenantId, I externalId) { if (importSettings.isImportInboundRelations() && CollectionUtils.isNotEmpty(exportData.getInboundRelations())) {
return exportableEntitiesService.findEntityByExternalId(tenantId, externalId); newRelations.addAll(exportData.getInboundRelations().stream()
.peek(relation -> {
relation.setTo(savedEntity.getId());
relation.setFrom(getInternalId(tenantId, relation.getFrom()));
})
.collect(Collectors.toList()));
if (importSettings.isRemoveExistingRelationsAndSaveNew() && existingEntity != null) {
relationService.findByTo(tenantId, savedEntity.getId(), RelationTypeGroup.COMMON).forEach(existingRelation -> {
relationService.deleteRelation(tenantId, existingRelation);
});
}
}
if (importSettings.isImportOutboundRelations() && CollectionUtils.isNotEmpty(exportData.getOutboundRelations())) {
newRelations.addAll(exportData.getOutboundRelations().stream()
.peek(relation -> {
relation.setTo(getInternalId(tenantId, relation.getTo()));
relation.setFrom(savedEntity.getId());
})
.collect(Collectors.toList()));
if (importSettings.isRemoveExistingRelationsAndSaveNew() && existingEntity != null) {
relationService.findByFrom(tenantId, savedEntity.getId(), RelationTypeGroup.COMMON).forEach(existingRelation -> {
relationService.deleteRelation(tenantId, existingRelation);
});
}
}
newRelations.forEach(relation -> relationService.saveRelation(tenantId, relation));
} }
// FIXME [viacheslav]: review use cases for version controlling: in the same tenant, between tenants, between environments and different tenants private <ID extends EntityId> ID getInternalId(TenantId tenantId, ID externalId) {
protected final <ID extends EntityId> ID getInternalId(TenantId tenantId, ID externalId) { if (externalId == null || externalId.isNullUid()) {
if (externalId == null) {
return null; return null;
} }
HasId<ID> entity = exportableEntitiesService.findEntityByExternalId(tenantId, externalId); HasId<ID> entity = exportableEntitiesService.findEntityByExternalId(tenantId, externalId);
@ -89,4 +128,8 @@ public abstract class AbstractEntityImportService<I extends EntityId, E extends
return entity.getId(); return entity.getId();
} }
protected interface IdProvider<E> {
<I extends EntityId> I get(TenantId tenantId, Function<E, I> idExtractor);
}
} }

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

@ -24,7 +24,6 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.asset.AssetService; import org.thingsboard.server.dao.asset.AssetService;
import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.exportimport.exporting.data.AssetExportData; import org.thingsboard.server.service.exportimport.exporting.data.AssetExportData;
import org.thingsboard.server.service.exportimport.importing.EntityImportSettings;
@Service @Service
@TbCoreComponent @TbCoreComponent
@ -33,11 +32,13 @@ public class AssetImportService extends AbstractEntityImportService<AssetId, Ass
private final AssetService assetService; private final AssetService assetService;
@Override @Override
protected Asset prepareAndSaveEntity(TenantId tenantId, Asset asset, Asset existingAsset, AssetExportData exportData, EntityImportSettings importSettings) { protected void setLinkedEntitiesIds(TenantId tenantId, Asset asset, IdProvider<Asset> idProvider) {
asset.setCustomerId(getInternalId(tenantId, asset.getCustomerId())); asset.setCustomerId(idProvider.get(tenantId, Asset::getCustomerId));
}
@Override
protected Asset saveEntity(TenantId tenantId, Asset asset, Asset existingAsset, AssetExportData exportData) {
return assetService.saveAsset(asset); return assetService.saveAsset(asset);
} }

4
application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/CustomerImportService.java

@ -24,7 +24,6 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.customer.CustomerService; import org.thingsboard.server.dao.customer.CustomerService;
import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.exportimport.exporting.data.CustomerExportData; import org.thingsboard.server.service.exportimport.exporting.data.CustomerExportData;
import org.thingsboard.server.service.exportimport.importing.EntityImportSettings;
@Service @Service
@TbCoreComponent @TbCoreComponent
@ -33,9 +32,8 @@ public class CustomerImportService extends AbstractEntityImportService<CustomerI
private final CustomerService customerService; private final CustomerService customerService;
@Override @Override
protected Customer prepareAndSaveEntity(TenantId tenantId, Customer customer, Customer existingCustomer, CustomerExportData exportData, EntityImportSettings importSettings) { protected Customer saveEntity(TenantId tenantId, Customer customer, Customer existingCustomer, CustomerExportData exportData) {
return customerService.saveCustomer(customer); return customerService.saveCustomer(customer);
} }

17
application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/DashboardImportService.java

@ -24,7 +24,6 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.dashboard.DashboardService; import org.thingsboard.server.dao.dashboard.DashboardService;
import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.exportimport.exporting.data.DashboardExportData; import org.thingsboard.server.service.exportimport.exporting.data.DashboardExportData;
import org.thingsboard.server.service.exportimport.importing.EntityImportSettings;
@Service @Service
@TbCoreComponent @TbCoreComponent
@ -33,15 +32,17 @@ public class DashboardImportService extends AbstractEntityImportService<Dashboar
private final DashboardService dashboardService; private final DashboardService dashboardService;
@Override @Override
protected Dashboard prepareAndSaveEntity(TenantId tenantId, Dashboard dashboard, Dashboard existingDashboard, DashboardExportData exportData, EntityImportSettings importSettings) { protected void setLinkedEntitiesIds(TenantId tenantId, Dashboard dashboard, IdProvider<Dashboard> idProvider) {
if (existingDashboard == null) { // if (existingDashboard == null) {
dashboard.setAssignedCustomers(null); // FIXME [viacheslav]: need to assign dashboard to customers ? // dashboard.setAssignedCustomers(null); // FIXME [viacheslav]: need to assign dashboard to customers ?
} else { // } else {
dashboard.setAssignedCustomers(existingDashboard.getAssignedCustomers()); // dashboard.setAssignedCustomers(existingDashboard.getAssignedCustomers());
} // }
}
@Override
protected Dashboard saveEntity(TenantId tenantId, Dashboard dashboard, Dashboard existingDashboard, DashboardExportData exportData) {
return dashboardService.saveDashboard(dashboard); return dashboardService.saveDashboard(dashboard);
} }

15
application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/DeviceImportService.java

@ -24,7 +24,6 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.device.DeviceService; import org.thingsboard.server.dao.device.DeviceService;
import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.exportimport.exporting.data.DeviceExportData; import org.thingsboard.server.service.exportimport.exporting.data.DeviceExportData;
import org.thingsboard.server.service.exportimport.importing.EntityImportSettings;
@Service @Service
@TbCoreComponent @TbCoreComponent
@ -33,14 +32,16 @@ public class DeviceImportService extends AbstractEntityImportService<DeviceId, D
private final DeviceService deviceService; private final DeviceService deviceService;
@Override @Override
protected Device prepareAndSaveEntity(TenantId tenantId, Device device, Device existingDevice, DeviceExportData exportData, EntityImportSettings importSettings) { protected void setLinkedEntitiesIds(TenantId tenantId, Device device, IdProvider<Device> idProvider) {
device.setCustomerId(getInternalId(tenantId, device.getCustomerId())); device.setCustomerId(idProvider.get(tenantId, Device::getCustomerId));
device.setDeviceProfileId(getInternalId(tenantId, device.getDeviceProfileId())); device.setDeviceProfileId(idProvider.get(tenantId, Device::getDeviceProfileId));
device.setFirmwareId(getInternalId(tenantId, device.getFirmwareId())); device.setFirmwareId(idProvider.get(tenantId, Device::getFirmwareId));
device.setSoftwareId(getInternalId(tenantId, device.getSoftwareId())); device.setSoftwareId(idProvider.get(tenantId, Device::getSoftwareId));
}
@Override
protected Device saveEntity(TenantId tenantId, Device device, Device existingDevice, DeviceExportData exportData) {
return deviceService.saveDeviceWithCredentials(device, exportData.getCredentials()); return deviceService.saveDeviceWithCredentials(device, exportData.getCredentials());
} }

15
application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/DeviceProfileImportService.java

@ -24,7 +24,6 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.device.DeviceProfileService; import org.thingsboard.server.dao.device.DeviceProfileService;
import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.exportimport.exporting.data.DeviceProfileExportData; import org.thingsboard.server.service.exportimport.exporting.data.DeviceProfileExportData;
import org.thingsboard.server.service.exportimport.importing.EntityImportSettings;
@Service @Service
@TbCoreComponent @TbCoreComponent
@ -33,14 +32,16 @@ public class DeviceProfileImportService extends AbstractEntityImportService<Devi
private final DeviceProfileService deviceProfileService; private final DeviceProfileService deviceProfileService;
@Override @Override
protected DeviceProfile prepareAndSaveEntity(TenantId tenantId, DeviceProfile deviceProfile, DeviceProfile existingDeviceProfile, DeviceProfileExportData exportData, EntityImportSettings importSettings) { protected void setLinkedEntitiesIds(TenantId tenantId, DeviceProfile deviceProfile, IdProvider<DeviceProfile> idProvider) {
deviceProfile.setDefaultRuleChainId(getInternalId(tenantId, deviceProfile.getDefaultRuleChainId())); deviceProfile.setDefaultRuleChainId(idProvider.get(tenantId, DeviceProfile::getDefaultRuleChainId));
deviceProfile.setDefaultDashboardId(getInternalId(tenantId, deviceProfile.getDefaultDashboardId())); deviceProfile.setDefaultDashboardId(idProvider.get(tenantId, DeviceProfile::getDefaultDashboardId));
deviceProfile.setFirmwareId(getInternalId(tenantId, deviceProfile.getFirmwareId())); deviceProfile.setFirmwareId(idProvider.get(tenantId, DeviceProfile::getFirmwareId));
deviceProfile.setSoftwareId(getInternalId(tenantId, deviceProfile.getSoftwareId())); deviceProfile.setSoftwareId(idProvider.get(tenantId, DeviceProfile::getSoftwareId));
}
@Override
protected DeviceProfile saveEntity(TenantId tenantId, DeviceProfile deviceProfile, DeviceProfile existingDeviceProfile, DeviceProfileExportData exportData) {
return deviceProfileService.saveDeviceProfile(deviceProfile); return deviceProfileService.saveDeviceProfile(deviceProfile);
} }

8
application/src/main/java/org/thingsboard/server/service/exportimport/importing/impl/RuleChainImportService.java

@ -17,7 +17,6 @@ package org.thingsboard.server.service.exportimport.importing.impl;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.RuleChainId; import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
@ -26,7 +25,6 @@ import org.thingsboard.server.common.data.rule.RuleChainMetaData;
import org.thingsboard.server.dao.rule.RuleChainService; import org.thingsboard.server.dao.rule.RuleChainService;
import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.exportimport.exporting.data.RuleChainExportData; import org.thingsboard.server.service.exportimport.exporting.data.RuleChainExportData;
import org.thingsboard.server.service.exportimport.importing.EntityImportSettings;
@Service @Service
@TbCoreComponent @TbCoreComponent
@ -35,10 +33,12 @@ public class RuleChainImportService extends AbstractEntityImportService<RuleChai
private final RuleChainService ruleChainService; private final RuleChainService ruleChainService;
@Override
protected void setLinkedEntitiesIds(TenantId tenantId, RuleChain ruleChain, IdProvider<RuleChain> idProvider) {
}
@Transactional
@Override @Override
protected RuleChain prepareAndSaveEntity(TenantId tenantId, RuleChain ruleChain, RuleChain existingRuleChain, RuleChainExportData exportData, EntityImportSettings importSettings) { protected RuleChain saveEntity(TenantId tenantId, RuleChain ruleChain, RuleChain existingRuleChain, RuleChainExportData exportData) {
ruleChain.setFirstRuleNodeId(null); // will be set during metadata persisting ruleChain.setFirstRuleNodeId(null); // will be set during metadata persisting
if (existingRuleChain != null) { if (existingRuleChain != null) {
ruleChainService.deleteRuleNodes(tenantId, existingRuleChain.getId()); ruleChainService.deleteRuleNodes(tenantId, existingRuleChain.getId());

194
dao/src/main/resources/sql/schema-entities.sql

@ -72,19 +72,18 @@ CREATE TABLE IF NOT EXISTS entity_alarm (
CONSTRAINT fk_entity_alarm_id FOREIGN KEY (alarm_id) REFERENCES alarm(id) ON DELETE CASCADE CONSTRAINT fk_entity_alarm_id FOREIGN KEY (alarm_id) REFERENCES alarm(id) ON DELETE CASCADE
); );
CREATE TABLE IF NOT EXISTS asset( CREATE TABLE IF NOT EXISTS asset (
id uuid NOT NULL id uuid NOT NULL CONSTRAINT asset_pkey PRIMARY KEY,
CONSTRAINT asset_pkey PRIMARY KEY, created_time bigint NOT NULL,
created_time bigint NOT NULL, additional_info varchar,
additional_info varchar, customer_id uuid,
customer_id uuid, name varchar(255),
name varchar(255), label varchar(255),
label varchar(255), search_text varchar(255),
search_text varchar(255), tenant_id uuid,
tenant_id uuid, type varchar(255),
type varchar(255), external_id uuid,
external_id uuid, CONSTRAINT asset_name_unq_key UNIQUE (tenant_id, name)
CONSTRAINT asset_name_unq_key UNIQUE (tenant_id, name)
); );
CREATE TABLE IF NOT EXISTS audit_log ( CREATE TABLE IF NOT EXISTS audit_log (
@ -130,53 +129,50 @@ CREATE TABLE IF NOT EXISTS component_descriptor (
); );
CREATE TABLE IF NOT EXISTS customer ( CREATE TABLE IF NOT EXISTS customer (
id uuid NOT NULL CONSTRAINT customer_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT customer_pkey PRIMARY KEY,
created_time bigint NOT NULL, created_time bigint NOT NULL,
additional_info varchar, additional_info varchar,
address varchar, address varchar,
address2 varchar, address2 varchar,
city varchar(255), city varchar(255),
country varchar(255), country varchar(255),
email varchar(255), email varchar(255),
phone varchar(255), phone varchar(255),
search_text varchar(255), search_text varchar(255),
state varchar(255), state varchar(255),
tenant_id uuid, tenant_id uuid,
title varchar(255), title varchar(255),
zip varchar(255), zip varchar(255),
external_id uuid external_id uuid
); );
CREATE TABLE IF NOT EXISTS dashboard CREATE TABLE IF NOT EXISTS dashboard (
( id uuid NOT NULL CONSTRAINT dashboard_pkey PRIMARY KEY,
id uuid NOT NULL created_time bigint NOT NULL,
CONSTRAINT dashboard_pkey PRIMARY KEY, configuration varchar,
created_time bigint NOT NULL,
configuration varchar,
assigned_customers varchar(1000000), assigned_customers varchar(1000000),
search_text varchar(255), search_text varchar(255),
tenant_id uuid, tenant_id uuid,
title varchar(255), title varchar(255),
mobile_hide boolean DEFAULT false, mobile_hide boolean DEFAULT false,
mobile_order int, mobile_order int,
image varchar(1000000), image varchar(1000000),
external_id uuid external_id uuid
); );
CREATE TABLE IF NOT EXISTS rule_chain ( CREATE TABLE IF NOT EXISTS rule_chain (
id uuid NOT NULL id uuid NOT NULL CONSTRAINT rule_chain_pkey PRIMARY KEY,
CONSTRAINT rule_chain_pkey PRIMARY KEY, created_time bigint NOT NULL,
created_time bigint NOT NULL, additional_info varchar,
additional_info varchar, configuration varchar(10000000),
configuration varchar(10000000), name varchar(255),
name varchar(255), type varchar(255),
type varchar(255), first_rule_node_id uuid,
first_rule_node_id uuid, root boolean,
root boolean, debug_mode boolean,
debug_mode boolean, search_text varchar(255),
search_text varchar(255), tenant_id uuid,
tenant_id uuid, external_id uuid
external_id uuid
); );
CREATE TABLE IF NOT EXISTS rule_node ( CREATE TABLE IF NOT EXISTS rule_node (
@ -224,31 +220,31 @@ CREATE TABLE IF NOT EXISTS ota_package (
); );
CREATE TABLE IF NOT EXISTS device_profile ( CREATE TABLE IF NOT EXISTS device_profile (
id uuid NOT NULL CONSTRAINT device_profile_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT device_profile_pkey PRIMARY KEY,
created_time bigint NOT NULL, created_time bigint NOT NULL,
name varchar(255), name varchar(255),
type varchar(255), type varchar(255),
image varchar(1000000), image varchar(1000000),
transport_type varchar(255), transport_type varchar(255),
provision_type varchar(255), provision_type varchar(255),
profile_data jsonb, profile_data jsonb,
description varchar, description varchar,
search_text varchar(255), search_text varchar(255),
is_default boolean, is_default boolean,
tenant_id uuid, tenant_id uuid,
firmware_id uuid, firmware_id uuid,
software_id uuid, software_id uuid,
default_rule_chain_id uuid, default_rule_chain_id uuid,
default_dashboard_id uuid, default_dashboard_id uuid,
default_queue_name varchar(255), default_queue_name varchar(255),
provision_device_key varchar, provision_device_key varchar,
external_id uuid, external_id uuid,
CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name), CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key), CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key),
CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain (id), CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id),
CONSTRAINT fk_default_dashboard_device_profile FOREIGN KEY (default_dashboard_id) REFERENCES dashboard (id), CONSTRAINT fk_default_dashboard_device_profile FOREIGN KEY (default_dashboard_id) REFERENCES dashboard(id),
CONSTRAINT fk_firmware_device_profile FOREIGN KEY (firmware_id) REFERENCES ota_package (id), CONSTRAINT fk_firmware_device_profile FOREIGN KEY (firmware_id) REFERENCES ota_package(id),
CONSTRAINT fk_software_device_profile FOREIGN KEY (software_id) REFERENCES ota_package (id) CONSTRAINT fk_software_device_profile FOREIGN KEY (software_id) REFERENCES ota_package(id)
); );
ALTER TABLE ota_package ALTER TABLE ota_package
@ -266,24 +262,24 @@ ALTER TABLE ota_package
-- ); -- );
CREATE TABLE IF NOT EXISTS device ( CREATE TABLE IF NOT EXISTS device (
id uuid NOT NULL CONSTRAINT device_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT device_pkey PRIMARY KEY,
created_time bigint NOT NULL, created_time bigint NOT NULL,
additional_info varchar, additional_info varchar,
customer_id uuid, customer_id uuid,
device_profile_id uuid NOT NULL, device_profile_id uuid NOT NULL,
device_data jsonb, device_data jsonb,
type varchar(255), type varchar(255),
name varchar(255), name varchar(255),
label varchar(255), label varchar(255),
search_text varchar(255), search_text varchar(255),
tenant_id uuid, tenant_id uuid,
firmware_id uuid, firmware_id uuid,
software_id uuid, software_id uuid,
external_id uuid, external_id uuid,
CONSTRAINT device_name_unq_key UNIQUE (tenant_id, name), CONSTRAINT device_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT fk_device_profile FOREIGN KEY (device_profile_id) REFERENCES device_profile (id), CONSTRAINT fk_device_profile FOREIGN KEY (device_profile_id) REFERENCES device_profile(id),
CONSTRAINT fk_firmware_device FOREIGN KEY (firmware_id) REFERENCES ota_package (id), CONSTRAINT fk_firmware_device FOREIGN KEY (firmware_id) REFERENCES ota_package(id),
CONSTRAINT fk_software_device FOREIGN KEY (software_id) REFERENCES ota_package (id) CONSTRAINT fk_software_device FOREIGN KEY (software_id) REFERENCES ota_package(id)
); );
CREATE TABLE IF NOT EXISTS device_credentials ( CREATE TABLE IF NOT EXISTS device_credentials (

Loading…
Cancel
Save