Refactoring to keep EntityService

This commit is contained in:
zbeacon 2022-12-30 12:51:45 +02:00
parent 022aee36d0
commit a812a88db1
4 changed files with 10 additions and 6 deletions

View File

@ -561,7 +561,7 @@ public class DefaultDataUpdateService implements DataUpdateService {
while (hasNext) {
for (Alarm alarm : alarms.getData()) {
if (alarm.getCustomerId() == null && alarm.getOriginator() != null) {
alarm.setCustomerId(entityService.fetchEntityCustomerId(tenantId, alarm.getOriginator()));
alarm.setCustomerId(entityService.fetchEntityCustomerId(tenantId, alarm.getOriginator()).get());
alarmDao.save(tenantId, alarm);
}
if (processed.incrementAndGet() % 1000 == 0) {

View File

@ -31,7 +31,7 @@ public interface EntityService {
Optional<String> fetchEntityLabel(TenantId tenantId, EntityId entityId);
CustomerId fetchEntityCustomerId(TenantId tenantId, EntityId entityId);
Optional<CustomerId> fetchEntityCustomerId(TenantId tenantId, EntityId entityId);
long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query);

View File

@ -122,7 +122,7 @@ public class BaseAlarmService extends AbstractEntityService implements AlarmServ
if (alarm.getEndTs() == 0L) {
alarm.setEndTs(alarm.getStartTs());
}
alarm.setCustomerId(entityService.fetchEntityCustomerId(alarm.getTenantId(), alarm.getOriginator()));
alarm.setCustomerId(entityService.fetchEntityCustomerId(alarm.getTenantId(), alarm.getOriginator()).orElse(null));
if (alarm.getId() == null) {
Alarm existing = alarmDao.findLatestByOriginatorAndType(alarm.getTenantId(), alarm.getOriginator(), alarm.getType());
if (existing == null || existing.getStatus().isCleared()) {

View File

@ -117,7 +117,7 @@ public class BaseEntityService extends AbstractEntityService implements EntitySe
}
@Override
public CustomerId fetchEntityCustomerId(TenantId tenantId, EntityId entityId) {
public Optional<CustomerId> fetchEntityCustomerId(TenantId tenantId, EntityId entityId) {
log.trace("Executing fetchEntityCustomerId [{}]", entityId);
EntityDaoService entityDaoService = entityServiceRegistry.getServiceByEntityType(entityId.getEntityType());
Optional<HasId<?>> hasIdOpt = entityDaoService.findEntity(tenantId, entityId);
@ -125,10 +125,14 @@ public class BaseEntityService extends AbstractEntityService implements EntitySe
HasId<?> hasId = hasIdOpt.get();
if (hasId instanceof HasCustomerId) {
HasCustomerId hasCustomerId = (HasCustomerId) hasId;
return hasCustomerId.getCustomerId();
CustomerId customerId = hasCustomerId.getCustomerId();
if (customerId == null) {
customerId = NULL_CUSTOMER_ID;
}
return Optional.of(customerId);
}
}
return null;
return Optional.of(NULL_CUSTOMER_ID);
}
private static void validateEntityCountQuery(EntityCountQuery query) {