64 changed files with 1788 additions and 109 deletions
@ -0,0 +1,28 @@ |
|||
-- |
|||
-- 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. |
|||
-- |
|||
|
|||
ALTER TABLE device |
|||
ADD COLUMN IF NOT EXISTS external_id UUID; |
|||
ALTER TABLE device_profile |
|||
ADD COLUMN IF NOT EXISTS external_id UUID; |
|||
ALTER TABLE asset |
|||
ADD COLUMN IF NOT EXISTS external_id UUID; |
|||
ALTER TABLE rule_chain |
|||
ADD COLUMN IF NOT EXISTS external_id UUID; |
|||
ALTER TABLE dashboard |
|||
ADD COLUMN IF NOT EXISTS external_id UUID; |
|||
ALTER TABLE customer |
|||
ADD COLUMN IF NOT EXISTS external_id UUID; |
|||
@ -0,0 +1,99 @@ |
|||
/** |
|||
* 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.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.RestController; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.export.EntitiesExportRequest; |
|||
import org.thingsboard.server.common.data.export.EntitiesExportResponse; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.EntityIdFactory; |
|||
import org.thingsboard.server.common.data.id.HasId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.expimp.EntitiesExportImportService; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.LinkedList; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.UUID; |
|||
|
|||
@RestController |
|||
@RequestMapping("/api/entities") |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class EntitiesExportImportController extends BaseController { |
|||
|
|||
private final EntitiesExportImportService exportImportService; |
|||
|
|||
|
|||
@PostMapping("/export/{entityType}/{entityId}") |
|||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|||
public EntityExportData<?> exportEntity(@PathVariable EntityType entityType, |
|||
@PathVariable("entityId") UUID entityUuid) throws ThingsboardException { |
|||
EntityId entityId = EntityIdFactory.getByTypeAndUuid(entityType, entityUuid); |
|||
try { |
|||
return exportImportService.exportEntity(getTenantId(), entityId); |
|||
} catch (Exception e) { |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@PostMapping("/export/batch") |
|||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|||
public EntitiesExportResponse exportEntities(@RequestBody EntitiesExportRequest exportRequest) throws ThingsboardException { |
|||
TenantId tenantId = getTenantId(); |
|||
|
|||
EntitiesExportResponse exportResponse = new EntitiesExportResponse(); |
|||
|
|||
Map<EntityType, List<EntityExportData<HasId<EntityId>>>> result = new HashMap<>(); |
|||
exportRequest.getEntities().forEach((entityType, entityIds) -> { |
|||
List<EntityExportData<HasId<EntityId>>> exportDataForEntityType = new LinkedList<>(); |
|||
entityIds.forEach(entityId -> { |
|||
EntityExportData<HasId<EntityId>> exportData = exportImportService.exportEntity(tenantId, entityId); |
|||
exportDataForEntityType.add(exportData); |
|||
}); |
|||
result.put(entityType, exportDataForEntityType); |
|||
}); |
|||
|
|||
exportResponse.setExportData(result); |
|||
return exportResponse; |
|||
} |
|||
// TODO: api to export and import whole customer, whole tenant
|
|||
|
|||
|
|||
@PostMapping("/import") |
|||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|||
public <E extends HasId<I>, I extends EntityId, D extends EntityExportData<E>> E importEntity(@RequestBody D exportData) throws ThingsboardException { |
|||
try { |
|||
return exportImportService.importEntity(getTenantId(), exportData); |
|||
} catch (Exception e) { |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
// public void importEntities(@RequestBody )
|
|||
|
|||
} |
|||
@ -0,0 +1,86 @@ |
|||
/** |
|||
* 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.expimp; |
|||
|
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.HasId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.expimp.exp.EntityExportService; |
|||
import org.thingsboard.server.service.expimp.imp.EntityImportService; |
|||
|
|||
import java.util.Collection; |
|||
import java.util.EnumMap; |
|||
import java.util.Map; |
|||
|
|||
// FIXME: review packages and classes naming
|
|||
@Service |
|||
@TbCoreComponent |
|||
public class DefaultEntitiesExportImportService implements EntitiesExportImportService { |
|||
|
|||
private final Map<EntityType, EntityExportService<?, ?>> exportServices = new EnumMap<>(EntityType.class); |
|||
private final Map<EntityType, EntityImportService<?, ?, ?>> importServices = new EnumMap<>(EntityType.class); |
|||
|
|||
|
|||
// TODO: export and import of the whole tenant
|
|||
// TODO: export and import of the whole customer ?
|
|||
// TODO: relations export and import
|
|||
@Override |
|||
public <E extends HasId<I>, I extends EntityId> EntityExportData<E> exportEntity(TenantId tenantId, I entityId) { |
|||
EntityType entityType = entityId.getEntityType(); |
|||
EntityExportService<I, E> exportService = getExportService(entityType); |
|||
|
|||
return exportService.getExportData(tenantId, entityId); |
|||
} |
|||
|
|||
// FIXME: somehow validate export data
|
|||
// FIXME: validate permissions for create or update
|
|||
// FIXME: send entity lifecycle event
|
|||
@Override |
|||
public <E extends HasId<I>, I extends EntityId, D extends EntityExportData<E>> E importEntity(TenantId tenantId, D exportData) { |
|||
EntityType entityType = exportData.getEntityType(); |
|||
EntityImportService<I, E, D> importService = getImportService(entityType); |
|||
|
|||
return importService.importEntity(tenantId, exportData); |
|||
} |
|||
|
|||
|
|||
@SuppressWarnings("unchecked") |
|||
private <I extends EntityId, E extends HasId<I>> EntityExportService<I, E> getExportService(EntityType entityType) { |
|||
return (EntityExportService<I, E>) exportServices.get(entityType); |
|||
} |
|||
|
|||
@SuppressWarnings("unchecked") |
|||
private <I extends EntityId, E extends HasId<I>, D extends EntityExportData<E>> EntityImportService<I, E, D> getImportService(EntityType entityType) { |
|||
return (EntityImportService<I, E, D>) importServices.get(entityType); |
|||
} |
|||
|
|||
@Autowired |
|||
private void setExportImportServices(Collection<EntityExportService<?, ?>> exportServices, |
|||
Collection<EntityImportService<?, ?, ?>> importServices) { |
|||
exportServices.forEach(entityExportService -> { |
|||
this.exportServices.put(entityExportService.getEntityType(), entityExportService); |
|||
}); |
|||
importServices.forEach(entityImportService -> { |
|||
this.importServices.put(entityImportService.getEntityType(), entityImportService); |
|||
}); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
/** |
|||
* 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.expimp; |
|||
|
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.HasId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
|
|||
public interface EntitiesExportImportService { |
|||
|
|||
<E extends HasId<I>, I extends EntityId> EntityExportData<E> exportEntity(TenantId tenantId, I entityId); |
|||
|
|||
<E extends HasId<I>, I extends EntityId, D extends EntityExportData<E>> E importEntity(TenantId tenantId, D exportData); |
|||
|
|||
} |
|||
@ -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.expimp.exp; |
|||
|
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.HasId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
|
|||
public interface EntityExportService<I extends EntityId, E extends HasId<I>> { |
|||
|
|||
// FIXME: export relations
|
|||
// FIXME: get rid of boilerplate
|
|||
EntityExportData<E> getExportData(TenantId tenantId, I entityId); |
|||
|
|||
EntityType getEntityType(); |
|||
|
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
/** |
|||
* 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.expimp.exp.impl; |
|||
|
|||
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.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.export.impl.AssetExportData; |
|||
import org.thingsboard.server.common.data.id.AssetId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.asset.AssetService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.expimp.exp.EntityExportService; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class AssetExportService implements EntityExportService<AssetId, Asset> { |
|||
|
|||
private final AssetService assetService; |
|||
|
|||
|
|||
@Override |
|||
public EntityExportData<Asset> getExportData(TenantId tenantId, AssetId assetId) { |
|||
AssetExportData exportData = new AssetExportData(); |
|||
exportData.setAsset(assetService.findAssetById(tenantId, assetId)); |
|||
return exportData; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.ASSET; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
/** |
|||
* 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.expimp.exp.impl; |
|||
|
|||
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.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.export.impl.CustomerExportData; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.customer.CustomerService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.expimp.exp.EntityExportService; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class CustomerExportService implements EntityExportService<CustomerId, Customer> { |
|||
|
|||
private final CustomerService customerService; |
|||
|
|||
|
|||
@Override |
|||
public EntityExportData<Customer> getExportData(TenantId tenantId, CustomerId customerId) { |
|||
CustomerExportData exportData = new CustomerExportData(); |
|||
exportData.setCustomer(customerService.findCustomerById(tenantId, customerId)); |
|||
return exportData; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.CUSTOMER; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
/** |
|||
* 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.expimp.exp.impl; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.Dashboard; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.export.impl.DashboardExportData; |
|||
import org.thingsboard.server.common.data.id.DashboardId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.dashboard.DashboardService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.expimp.exp.EntityExportService; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class DashboardExportService implements EntityExportService<DashboardId, Dashboard> { |
|||
|
|||
private final DashboardService dashboardService; |
|||
|
|||
|
|||
@Override |
|||
public EntityExportData<Dashboard> getExportData(TenantId tenantId, DashboardId dashboardId) { |
|||
DashboardExportData exportData = new DashboardExportData(); |
|||
exportData.setDashboard(dashboardService.findDashboardById(tenantId, dashboardId)); |
|||
return exportData; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.DASHBOARD; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,53 @@ |
|||
/** |
|||
* 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.expimp.exp.impl; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.export.impl.DeviceExportData; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.device.DeviceCredentialsService; |
|||
import org.thingsboard.server.dao.device.DeviceService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.expimp.exp.EntityExportService; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class DeviceExportService implements EntityExportService<DeviceId, Device> { |
|||
|
|||
private final DeviceService deviceService; |
|||
private final DeviceCredentialsService deviceCredentialsService; |
|||
|
|||
|
|||
@Override |
|||
public EntityExportData<Device> getExportData(TenantId tenantId, DeviceId deviceId) { |
|||
DeviceExportData exportData = new DeviceExportData(); |
|||
exportData.setDevice(deviceService.findDeviceById(tenantId, deviceId)); |
|||
exportData.setCredentials(deviceCredentialsService.findDeviceCredentialsByDeviceId(TenantId.SYS_TENANT_ID, deviceId)); |
|||
return exportData; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.DEVICE; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
/** |
|||
* 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.expimp.exp.impl; |
|||
|
|||
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.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.export.impl.DeviceProfileExportData; |
|||
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.device.DeviceProfileService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.expimp.exp.EntityExportService; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class DeviceProfileExportService implements EntityExportService<DeviceProfileId, DeviceProfile> { |
|||
|
|||
private final DeviceProfileService deviceProfileService; |
|||
|
|||
|
|||
@Override |
|||
public EntityExportData<DeviceProfile> getExportData(TenantId tenantId, DeviceProfileId deviceProfileId) { |
|||
DeviceProfileExportData exportData = new DeviceProfileExportData(); |
|||
exportData.setDeviceProfile(deviceProfileService.findDeviceProfileById(tenantId, deviceProfileId)); |
|||
return exportData; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.DEVICE_PROFILE; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
/** |
|||
* 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.expimp.exp.impl; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.export.impl.RuleChainExportData; |
|||
import org.thingsboard.server.common.data.id.RuleChainId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.rule.RuleChain; |
|||
import org.thingsboard.server.dao.rule.RuleChainService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.expimp.exp.EntityExportService; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class RuleChainExportService implements EntityExportService<RuleChainId, RuleChain> { |
|||
|
|||
private final RuleChainService ruleChainService; |
|||
|
|||
|
|||
@Override |
|||
public EntityExportData<RuleChain> getExportData(TenantId tenantId, RuleChainId ruleChainId) { |
|||
RuleChainExportData exportData = new RuleChainExportData(); |
|||
exportData.setRuleChain(ruleChainService.findRuleChainById(tenantId, ruleChainId)); |
|||
exportData.setMetaData(ruleChainService.loadRuleChainMetaData(tenantId, ruleChainId)); |
|||
return exportData; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.RULE_CHAIN; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
/** |
|||
* 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.expimp.imp; |
|||
|
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.HasId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
|
|||
public interface EntityImportService<I extends EntityId, E extends HasId<I>, D extends EntityExportData<E>> { |
|||
|
|||
E importEntity(TenantId tenantId, D exportData); |
|||
|
|||
EntityType getEntityType(); |
|||
|
|||
} |
|||
@ -0,0 +1,73 @@ |
|||
/** |
|||
* 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.expimp.imp.impl; |
|||
|
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.HasId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.ExportableEntityDao; |
|||
import org.thingsboard.server.service.expimp.imp.EntityImportService; |
|||
|
|||
import java.util.Collection; |
|||
import java.util.EnumMap; |
|||
import java.util.Map; |
|||
import java.util.Optional; |
|||
|
|||
public abstract class AbstractEntityImportService<I extends EntityId, E extends HasId<I>, D extends EntityExportData<E>> implements EntityImportService<I, E, D> { |
|||
|
|||
private final Map<EntityType, ExportableEntityDao<?>> daos = new EnumMap<>(EntityType.class); |
|||
|
|||
|
|||
public final E findByExternalId(TenantId tenantId, I externalId) { |
|||
return findByExternalOrInternalId(tenantId, externalId); |
|||
} |
|||
|
|||
|
|||
protected final <ID extends EntityId> ID getInternalId(TenantId tenantId, ID externalId) { |
|||
if (externalId == null) { |
|||
return null; |
|||
} |
|||
HasId<ID> entity = findByExternalOrInternalId(tenantId, externalId); |
|||
if (entity == null) { |
|||
throw new IllegalStateException("Cannot find " + externalId.getEntityType() + " by external id " + externalId); |
|||
} |
|||
return entity.getId(); |
|||
} |
|||
|
|||
private <T extends HasId<ID>, ID extends EntityId> T findByExternalOrInternalId(TenantId tenantId, ID externalOrInternalId) { |
|||
ExportableEntityDao<T> dao = getDao(externalOrInternalId.getEntityType()); |
|||
return Optional.ofNullable(dao.findByTenantIdAndExternalId(tenantId.getId(), externalOrInternalId.getId())) |
|||
.orElseGet(() -> dao.findByTenantIdAndId(tenantId.getId(), externalOrInternalId.getId())); |
|||
} |
|||
|
|||
|
|||
@SuppressWarnings("unchecked") |
|||
private <T> ExportableEntityDao<T> getDao(EntityType entityType) { |
|||
return (ExportableEntityDao<T>) daos.get(entityType); |
|||
} |
|||
|
|||
@Autowired |
|||
private void setDaos(Collection<ExportableEntityDao<?>> daos) { |
|||
daos.forEach(dao -> this.daos.put(dao.getEntityType(), dao)); |
|||
if (!this.daos.containsKey(getEntityType())) { |
|||
throw new IllegalStateException(getClass().getSimpleName() + " requires ExportableEntityDao for entity type " + getEntityType()); |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
/** |
|||
* 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.expimp.imp.impl; |
|||
|
|||
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.export.impl.AssetExportData; |
|||
import org.thingsboard.server.common.data.id.AssetId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.asset.AssetService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class AssetImportService extends AbstractEntityImportService<AssetId, Asset, AssetExportData> { |
|||
|
|||
private final AssetService assetService; |
|||
|
|||
|
|||
@Override |
|||
public Asset importEntity(TenantId tenantId, AssetExportData exportData) { |
|||
Asset asset = exportData.getAsset(); |
|||
Asset existingAsset = findByExternalId(tenantId, asset.getId()); // TODO: extract boiler plate to abstract class ...
|
|||
|
|||
asset.setExternalId(asset.getId()); |
|||
asset.setTenantId(tenantId); |
|||
|
|||
if (existingAsset == null) { |
|||
asset.setId(null); |
|||
} else { |
|||
asset.setId(existingAsset.getId()); |
|||
} |
|||
|
|||
asset.setCustomerId(getInternalId(tenantId, asset.getCustomerId())); |
|||
|
|||
Asset savedAsset = assetService.saveAsset(asset); |
|||
|
|||
return savedAsset; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.ASSET; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,60 @@ |
|||
/** |
|||
* 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.expimp.imp.impl; |
|||
|
|||
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.export.impl.CustomerExportData; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.customer.CustomerService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class CustomerImportService extends AbstractEntityImportService<CustomerId, Customer, CustomerExportData> { |
|||
|
|||
private final CustomerService customerService; |
|||
|
|||
|
|||
@Override |
|||
public Customer importEntity(TenantId tenantId, CustomerExportData exportData) { |
|||
Customer customer = exportData.getCustomer(); |
|||
Customer existingCustomer = findByExternalId(tenantId, customer.getId()); |
|||
|
|||
customer.setExternalId(customer.getId()); |
|||
customer.setTenantId(tenantId); |
|||
|
|||
if (existingCustomer == null) { |
|||
customer.setId(null); |
|||
} else { |
|||
customer.setId(existingCustomer.getId()); |
|||
} |
|||
|
|||
Customer savedCustomer = customerService.saveCustomer(customer); |
|||
|
|||
return savedCustomer; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.CUSTOMER; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
/** |
|||
* 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.expimp.imp.impl; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.Dashboard; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.impl.DashboardExportData; |
|||
import org.thingsboard.server.common.data.id.DashboardId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.dashboard.DashboardService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class DashboardImportService extends AbstractEntityImportService<DashboardId, Dashboard, DashboardExportData> { |
|||
|
|||
private final DashboardService dashboardService; |
|||
|
|||
|
|||
@Override |
|||
public Dashboard importEntity(TenantId tenantId, DashboardExportData exportData) { |
|||
Dashboard dashboard = exportData.getDashboard(); |
|||
Dashboard existingDashboard = findByExternalId(tenantId, dashboard.getId()); |
|||
|
|||
dashboard.setExternalId(dashboard.getId()); |
|||
dashboard.setTenantId(tenantId); |
|||
|
|||
if (existingDashboard == null) { |
|||
dashboard.setId(null); |
|||
dashboard.setAssignedCustomers(null); // FIXME: need to assign dashboard to customers ?
|
|||
} else { |
|||
dashboard.setId(existingDashboard.getId()); |
|||
dashboard.setAssignedCustomers(existingDashboard.getAssignedCustomers()); // we left them untouched (FIXME)
|
|||
} |
|||
|
|||
Dashboard savedDashboard = dashboardService.saveDashboard(dashboard); |
|||
|
|||
return savedDashboard; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.DASHBOARD; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,81 @@ |
|||
/** |
|||
* 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.expimp.imp.impl; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
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.export.impl.DeviceExportData; |
|||
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.action.EntityActionService; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class DeviceImportService extends AbstractEntityImportService<DeviceId, Device, DeviceExportData> { |
|||
|
|||
private final DeviceService deviceService; |
|||
|
|||
|
|||
@Transactional |
|||
@Override |
|||
public Device importEntity(TenantId tenantId, DeviceExportData exportData) { |
|||
Device device = exportData.getDevice(); |
|||
Device existingDevice = findByExternalId(tenantId, device.getId()); // FIXME: !!!
|
|||
// what if exporting and importing back already exported entity ? (save version and then load it back)
|
|||
/* |
|||
* export entity -> id from env1 -> import this entity -> ... |
|||
* |
|||
* maybe find not only by external id but by internal too ? but then what if we will try |
|||
* */ |
|||
|
|||
device.setExternalId(device.getId()); |
|||
device.setTenantId(tenantId); |
|||
|
|||
if (existingDevice == null) { |
|||
device.setId(null); |
|||
device.setCustomerId(null); // FIXME: find and set customer
|
|||
} else { |
|||
device.setId(existingDevice.getId()); |
|||
device.setCustomerId(existingDevice.getCustomerId()); |
|||
} |
|||
|
|||
// TODO or maybe set as additional config whether to update related entities when device already exists ?
|
|||
// TODO: or also whether to ignore not found internal ids
|
|||
|
|||
// FIXME: review use cases for version controlling: in the same tenant, between tenants, between environments and different tenants
|
|||
|
|||
device.setDeviceProfileId(getInternalId(tenantId, device.getDeviceProfileId())); |
|||
device.setFirmwareId(getInternalId(tenantId, device.getFirmwareId())); |
|||
device.setSoftwareId(getInternalId(tenantId, device.getSoftwareId())); |
|||
|
|||
Device savedDevice = deviceService.saveDeviceWithCredentials(device, exportData.getCredentials()); |
|||
|
|||
return savedDevice; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.DEVICE; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
/** |
|||
* 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.expimp.imp.impl; |
|||
|
|||
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.export.impl.DeviceProfileExportData; |
|||
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.device.DeviceProfileService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class DeviceProfileImportService extends AbstractEntityImportService<DeviceProfileId, DeviceProfile, DeviceProfileExportData> { |
|||
|
|||
private final DeviceProfileService deviceProfileService; |
|||
|
|||
|
|||
@Override |
|||
public DeviceProfile importEntity(TenantId tenantId, DeviceProfileExportData exportData) { |
|||
DeviceProfile deviceProfile = exportData.getDeviceProfile(); |
|||
DeviceProfile existingDeviceProfile = findByExternalId(tenantId, deviceProfile.getId()); |
|||
|
|||
deviceProfile.setExternalId(deviceProfile.getId()); |
|||
deviceProfile.setTenantId(tenantId); |
|||
|
|||
if (existingDeviceProfile == null) { |
|||
deviceProfile.setId(null); |
|||
} else { |
|||
deviceProfile.setId(existingDeviceProfile.getId()); |
|||
} |
|||
|
|||
deviceProfile.setDefaultRuleChainId(getInternalId(tenantId, deviceProfile.getDefaultRuleChainId())); |
|||
deviceProfile.setDefaultDashboardId(getInternalId(tenantId, deviceProfile.getDefaultDashboardId())); |
|||
deviceProfile.setFirmwareId(getInternalId(tenantId, deviceProfile.getFirmwareId())); |
|||
deviceProfile.setSoftwareId(getInternalId(tenantId, deviceProfile.getSoftwareId())); |
|||
|
|||
DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile); |
|||
|
|||
return savedDeviceProfile; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.DEVICE_PROFILE; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,80 @@ |
|||
/** |
|||
* 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.expimp.imp.impl; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.impl.RuleChainExportData; |
|||
import org.thingsboard.server.common.data.id.RuleChainId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.rule.RuleChain; |
|||
import org.thingsboard.server.common.data.rule.RuleChainMetaData; |
|||
import org.thingsboard.server.dao.rule.RuleChainService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class RuleChainImportService extends AbstractEntityImportService<RuleChainId, RuleChain, RuleChainExportData> { |
|||
|
|||
private final RuleChainService ruleChainService; |
|||
|
|||
|
|||
@Transactional |
|||
@Override |
|||
public RuleChain importEntity(TenantId tenantId, RuleChainExportData exportData) { |
|||
RuleChain ruleChain = exportData.getRuleChain(); |
|||
RuleChain existingRuleChain = findByExternalId(tenantId, ruleChain.getId()); |
|||
|
|||
ruleChain.setExternalId(ruleChain.getId()); |
|||
ruleChain.setTenantId(tenantId); |
|||
ruleChain.setFirstRuleNodeId(null); // will be set during metadata persisting
|
|||
|
|||
if (existingRuleChain == null) { |
|||
ruleChain.setId(null); |
|||
} else { |
|||
ruleChain.setId(existingRuleChain.getId()); |
|||
} |
|||
|
|||
RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain); |
|||
|
|||
if (ruleChain.getId() != null) { |
|||
ruleChainService.deleteRuleNodes(tenantId, ruleChain.getId()); |
|||
} |
|||
RuleChainMetaData metaData = exportData.getMetaData(); |
|||
metaData.setRuleChainId(savedRuleChain.getId()); |
|||
metaData.getNodes().forEach(ruleNode -> { |
|||
ruleNode.setId(null); |
|||
ruleNode.setRuleChainId(null); |
|||
}); |
|||
metaData.getRuleChainConnections().forEach(ruleChainConnectionInfo -> { |
|||
// ruleChainConnectionInfo.setTargetRuleChainId();
|
|||
// TODO: check if this thing is needed for "Other Rule Chain Node"
|
|||
// TODO: and check import of tenant rule chains
|
|||
}); |
|||
ruleChainService.saveRuleChainMetaData(tenantId, metaData); |
|||
|
|||
return savedRuleChain; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.RULE_CHAIN; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
/** |
|||
* 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.common.data.export; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
@Data |
|||
public class EntitiesExportRequest { |
|||
// todo: should be processed in a specific order, e.g. device profiles, then devices, then relations, etc.
|
|||
private Map<EntityType, List<EntityId>> entities; |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
/** |
|||
* 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.common.data.export; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.HasId; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
@Data |
|||
public class EntitiesExportResponse { |
|||
private Map<EntityType, List<EntityExportData<HasId<EntityId>>>> exportData; |
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
/** |
|||
* 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.common.data.export; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonIgnore; |
|||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|||
import com.fasterxml.jackson.annotation.JsonSubTypes; |
|||
import com.fasterxml.jackson.annotation.JsonSubTypes.Type; |
|||
import com.fasterxml.jackson.annotation.JsonTypeInfo; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.impl.AssetExportData; |
|||
import org.thingsboard.server.common.data.export.impl.CustomerExportData; |
|||
import org.thingsboard.server.common.data.export.impl.DashboardExportData; |
|||
import org.thingsboard.server.common.data.export.impl.DeviceExportData; |
|||
import org.thingsboard.server.common.data.export.impl.DeviceProfileExportData; |
|||
import org.thingsboard.server.common.data.export.impl.RuleChainExportData; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.HasId; |
|||
|
|||
@JsonIgnoreProperties(ignoreUnknown = true) |
|||
@JsonTypeInfo(property = "entityType", use = JsonTypeInfo.Id.NAME) |
|||
@JsonSubTypes({ |
|||
@Type(name = "DEVICE", value = DeviceExportData.class), |
|||
@Type(name = "DEVICE_PROFILE", value = DeviceProfileExportData.class), |
|||
@Type(name = "ASSET", value = AssetExportData.class), |
|||
@Type(name = "RULE_CHAIN", value = RuleChainExportData.class), |
|||
@Type(name = "DASHBOARD", value = DashboardExportData.class), |
|||
@Type(name = "CUSTOMER", value = CustomerExportData.class) |
|||
}) |
|||
public interface EntityExportData<E extends HasId<? extends EntityId>> { |
|||
|
|||
@JsonIgnore |
|||
EntityType getEntityType(); // fixme: maybe remove if not needed, as well as generic
|
|||
|
|||
} |
|||
@ -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.common.data.export.impl; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.asset.Asset; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
|
|||
@Data |
|||
public class AssetExportData implements EntityExportData<Asset> { |
|||
|
|||
private Asset asset; |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.ASSET; |
|||
} |
|||
|
|||
} |
|||
@ -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.common.data.export.impl; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.Customer; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
|
|||
@Data |
|||
public class CustomerExportData implements EntityExportData<Customer> { |
|||
|
|||
private Customer customer; |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.CUSTOMER; |
|||
} |
|||
|
|||
} |
|||
@ -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.common.data.export.impl; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.Dashboard; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
|
|||
@Data |
|||
public class DashboardExportData implements EntityExportData<Dashboard> { |
|||
|
|||
private Dashboard dashboard; |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.DASHBOARD; |
|||
} |
|||
|
|||
} |
|||
@ -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.common.data.export.impl; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.security.DeviceCredentials; |
|||
|
|||
@Data |
|||
public class DeviceExportData implements EntityExportData<Device> { |
|||
|
|||
private Device device; |
|||
private DeviceCredentials credentials; |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.DEVICE; |
|||
} |
|||
|
|||
} |
|||
@ -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.common.data.export.impl; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.DeviceProfile; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
|
|||
@Data |
|||
public class DeviceProfileExportData implements EntityExportData<DeviceProfile> { |
|||
|
|||
private DeviceProfile deviceProfile; |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.DEVICE_PROFILE; |
|||
} |
|||
|
|||
} |
|||
@ -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.common.data.export.impl; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.export.EntityExportData; |
|||
import org.thingsboard.server.common.data.rule.RuleChain; |
|||
import org.thingsboard.server.common.data.rule.RuleChainMetaData; |
|||
|
|||
@Data |
|||
public class RuleChainExportData implements EntityExportData<RuleChain> { |
|||
|
|||
private RuleChain ruleChain; |
|||
private RuleChainMetaData metaData; |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.RULE_CHAIN; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
/** |
|||
* 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.dao; |
|||
|
|||
import org.thingsboard.server.common.data.EntityType; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
public interface ExportableEntityDao<T> { |
|||
|
|||
T findByTenantIdAndExternalId(UUID tenantId, UUID externalId); |
|||
|
|||
T findByTenantIdAndId(UUID tenantId, UUID id); |
|||
// fixme: get rid of boilerplate ?
|
|||
|
|||
EntityType getEntityType(); |
|||
|
|||
/* |
|||
* default <D extends BaseEntity<T>> ExportableEntityRepository<D> getExportableEntityRepository() { |
|||
* ((ExportableEntityRepository) getJpaRepository).find... |
|||
* */ |
|||
|
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
/** |
|||
* 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.dao; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
public interface ExportableEntityRepository<D> { |
|||
|
|||
D findByTenantIdAndExternalId(UUID tenantId, UUID externalId); |
|||
|
|||
D findByTenantIdAndId(UUID tenantId, UUID id); |
|||
|
|||
} |
|||
Loading…
Reference in new issue