Refactor AbstractEntityImportService
This commit is contained in:
parent
849513541e
commit
99d0197caa
@ -925,7 +925,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) {
|
||||
boolean notifyEdge = false;
|
||||
boolean notifyEdgeService = false;
|
||||
|
||||
EntityType entityType = savedEntity.getId().getEntityType();
|
||||
switch (entityType) {
|
||||
@ -949,7 +949,7 @@ public abstract class BaseController {
|
||||
tbClusterService.broadcastEntityStateChangeEvent(deviceProfile.getTenantId(), deviceProfile.getId(),
|
||||
isNewEntity ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED);
|
||||
otaPackageStateService.update(deviceProfile, isFirmwareChanged, isSoftwareChanged);
|
||||
notifyEdge = true;
|
||||
notifyEdgeService = true;
|
||||
break;
|
||||
case RULE_CHAIN: // FIXME: events for rule chain metadata
|
||||
RuleChainType ruleChainType = ((RuleChain) savedEntity).getType();
|
||||
@ -959,7 +959,7 @@ public abstract class BaseController {
|
||||
}
|
||||
if (RuleChainType.EDGE.equals(ruleChainType)) {
|
||||
if (!isNewEntity) {
|
||||
notifyEdge = true;
|
||||
notifyEdgeService = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -967,7 +967,7 @@ public abstract class BaseController {
|
||||
case CUSTOMER:
|
||||
case DASHBOARD:
|
||||
if (!isNewEntity) {
|
||||
notifyEdge = true;
|
||||
notifyEdgeService = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@ -976,7 +976,7 @@ public abstract class BaseController {
|
||||
|
||||
entityActionService.logEntityAction(user, savedEntity.getId(), savedEntity, savedEntity instanceof HasCustomerId ? ((HasCustomerId) savedEntity).getCustomerId() : null,
|
||||
isNewEntity ? ActionType.ADDED : ActionType.UPDATED, null);
|
||||
if (notifyEdge) {
|
||||
if (notifyEdgeService) {
|
||||
sendEntityNotificationMsg(savedEntity.getTenantId(), savedEntity.getId(), isNewEntity ? EdgeEventActionType.ADDED : EdgeEventActionType.UPDATED);
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,23 +22,25 @@ 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.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.service.expimp.exp.EntityExportService;
|
||||
import org.thingsboard.server.service.expimp.imp.EntityImportResult;
|
||||
import org.thingsboard.server.service.expimp.imp.EntityImportService;
|
||||
import org.thingsboard.server.service.expimp.imp.impl.AbstractEntityImportService;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.EnumMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
// 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);
|
||||
private final Map<EntityType, EntityExportService<?, ?>> exportServices = new HashMap<>();
|
||||
private final Map<EntityType, EntityImportService<?, ?, ?>> importServices = new HashMap<>();
|
||||
private final Map<EntityType, ExportableEntityDao<?>> daos = new HashMap<>();
|
||||
|
||||
|
||||
// TODO: export and import of the whole tenant
|
||||
@ -61,14 +63,15 @@ public class DefaultEntitiesExportImportService implements EntitiesExportImportS
|
||||
return importService.importEntity(tenantId, exportData);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <E extends HasId<I>, I extends EntityId> E findEntityByExternalId(TenantId tenantId, I externalId) {
|
||||
return (E) importServices.values().stream().filter(entityImportService -> entityImportService instanceof AbstractEntityImportService)
|
||||
.findFirst().map(entityImportService -> (AbstractEntityImportService) importServices).get()
|
||||
.findByExternalOrInternalId(tenantId, externalId); // FIXME !!!
|
||||
ExportableEntityDao<E> dao = getDao(externalId.getEntityType());
|
||||
return Optional.ofNullable(dao.findByTenantIdAndExternalId(tenantId.getId(), externalId.getId()))
|
||||
.orElseGet(() -> dao.findByTenantIdAndId(tenantId.getId(), externalId.getId()));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <I extends EntityId, E extends HasId<I>> EntityExportService<I, E> getExportService(EntityType entityType) {
|
||||
return (EntityExportService<I, E>) exportServices.get(entityType);
|
||||
@ -79,15 +82,24 @@ public class DefaultEntitiesExportImportService implements EntitiesExportImportS
|
||||
return (EntityImportService<I, E, D>) importServices.get(entityType);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <E> ExportableEntityDao<E> getDao(EntityType entityType) {
|
||||
return (ExportableEntityDao<E>) daos.get(entityType);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setExportImportServices(Collection<EntityExportService<?, ?>> exportServices,
|
||||
Collection<EntityImportService<?, ?, ?>> importServices) {
|
||||
private void setServices(Collection<EntityExportService<?, ?>> exportServices,
|
||||
Collection<EntityImportService<?, ?, ?>> importServices,
|
||||
Collection<ExportableEntityDao<?>> daos) {
|
||||
exportServices.forEach(entityExportService -> {
|
||||
this.exportServices.put(entityExportService.getEntityType(), entityExportService);
|
||||
});
|
||||
importServices.forEach(entityImportService -> {
|
||||
this.importServices.put(entityImportService.getEntityType(), entityImportService);
|
||||
});
|
||||
daos.forEach(dao -> {
|
||||
this.daos.put(dao.getEntityType(), dao);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -21,12 +21,10 @@ import org.thingsboard.server.common.data.id.HasId;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.service.expimp.imp.EntityImportResult;
|
||||
|
||||
public interface EntitiesExportImportService {
|
||||
public interface EntitiesExportImportService extends ExportableEntitiesService {
|
||||
|
||||
<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>> EntityImportResult<E> importEntity(TenantId tenantId, D exportData);
|
||||
|
||||
<E extends HasId<I>, I extends EntityId> E findEntityByExternalId(TenantId tenantId, I externalId);
|
||||
|
||||
}
|
||||
|
||||
@ -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.service.expimp;
|
||||
|
||||
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 ExportableEntitiesService {
|
||||
|
||||
<E extends HasId<I>, I extends EntityId> E findEntityByExternalId(TenantId tenantId, I externalId);
|
||||
|
||||
}
|
||||
@ -16,57 +16,33 @@
|
||||
package org.thingsboard.server.service.expimp.imp.impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.thingsboard.server.common.data.EntityType;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
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.ExportableEntitiesService;
|
||||
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);
|
||||
@Autowired @Lazy
|
||||
private ExportableEntitiesService exportableEntitiesService;
|
||||
|
||||
|
||||
public final E findByExternalId(TenantId tenantId, I externalId) {
|
||||
return findByExternalOrInternalId(tenantId, externalId);
|
||||
protected final E findByExternalId(TenantId tenantId, I externalId) {
|
||||
return exportableEntitiesService.findEntityByExternalId(tenantId, externalId);
|
||||
}
|
||||
|
||||
protected final <ID extends EntityId> ID getInternalId(TenantId tenantId, ID externalId) {
|
||||
if (externalId == null) {
|
||||
return null;
|
||||
}
|
||||
HasId<ID> entity = findByExternalOrInternalId(tenantId, externalId);
|
||||
HasId<ID> entity = exportableEntitiesService.findEntityByExternalId(tenantId, externalId);
|
||||
if (entity == null) {
|
||||
throw new IllegalStateException("Cannot find " + externalId.getEntityType() + " by external id " + externalId);
|
||||
}
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
public final <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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user