Added getting NameLabelAndCustomerDetails to EntityService

This commit is contained in:
zbeacon 2023-02-16 13:55:46 +02:00
parent 74fca60cd2
commit dda627b63e
4 changed files with 63 additions and 13 deletions

View File

@ -15,7 +15,7 @@
*/ */
package org.thingsboard.server.dao.entity; package org.thingsboard.server.dao.entity;
import org.springframework.data.util.Pair; import org.thingsboard.server.common.data.id.NameLabelAndCustomerDetails;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
@ -34,8 +34,9 @@ public interface EntityService {
Optional<CustomerId> fetchEntityCustomerId(TenantId tenantId, EntityId entityId); Optional<CustomerId> fetchEntityCustomerId(TenantId tenantId, EntityId entityId);
Optional<NameLabelAndCustomerDetails> fetchNameLabelAndCustomerDetails(TenantId tenantId, EntityId entityId);
long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query); long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query);
PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query); PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query);
} }

View File

@ -0,0 +1,27 @@
/**
* Copyright © 2016-2023 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.id;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class NameLabelAndCustomerDetails {
private final String name;
private final String label;
private final CustomerId customerId;
}

View File

@ -38,6 +38,7 @@ import org.thingsboard.server.common.data.alarm.AlarmStatus;
import org.thingsboard.server.common.data.alarm.EntityAlarm; import org.thingsboard.server.common.data.alarm.EntityAlarm;
import org.thingsboard.server.common.data.exception.ApiUsageLimitsExceededException; import org.thingsboard.server.common.data.exception.ApiUsageLimitsExceededException;
import org.thingsboard.server.common.data.id.AlarmId; import org.thingsboard.server.common.data.id.AlarmId;
import org.thingsboard.server.common.data.id.NameLabelAndCustomerDetails;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
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;
@ -359,10 +360,15 @@ public class BaseAlarmService extends AbstractEntityService implements AlarmServ
private ListenableFuture<PageData<AlarmInfo>> fetchAlarmsOriginators(TenantId tenantId, PageData<AlarmInfo> alarms) { private ListenableFuture<PageData<AlarmInfo>> fetchAlarmsOriginators(TenantId tenantId, PageData<AlarmInfo> alarms) {
List<ListenableFuture<AlarmInfo>> alarmFutures = new ArrayList<>(alarms.getData().size()); List<ListenableFuture<AlarmInfo>> alarmFutures = new ArrayList<>(alarms.getData().size());
for (AlarmInfo alarmInfo : alarms.getData()) { for (AlarmInfo alarmInfo : alarms.getData()) {
alarmInfo.setOriginatorName( Optional<NameLabelAndCustomerDetails> detailsOpt = entityService.fetchNameLabelAndCustomerDetails(tenantId, alarmInfo.getOriginator());
entityService.fetchEntityName(tenantId, alarmInfo.getOriginator()).orElse("Deleted")); if (detailsOpt.isPresent() && detailsOpt.get().getName() != null) {
alarmInfo.setOriginatorLabel( NameLabelAndCustomerDetails details = detailsOpt.get();
entityService.fetchEntityLabel(tenantId, alarmInfo.getOriginator()).orElse(null)); alarmInfo.setOriginatorName(details.getName());
alarmInfo.setOriginatorLabel(details.getLabel());
} else {
alarmInfo.setOriginatorName("Deleted");
alarmInfo.setOriginatorLabel("Deleted");
}
alarmFutures.add(Futures.immediateFuture(alarmInfo)); alarmFutures.add(Futures.immediateFuture(alarmInfo));
} }
return Futures.transform(Futures.successfulAsList(alarmFutures), return Futures.transform(Futures.successfulAsList(alarmFutures),
@ -465,8 +471,15 @@ public class BaseAlarmService extends AbstractEntityService implements AlarmServ
String assigneeLastName = null; String assigneeLastName = null;
String assigneeEmail = null; String assigneeEmail = null;
originatorName = entityService.fetchEntityName(tenantId, alarm.getOriginator()).orElse("Deleted"); Optional<NameLabelAndCustomerDetails> detailsOpt = entityService.fetchNameLabelAndCustomerDetails(tenantId, alarm.getOriginator());
originatorLabel = entityService.fetchEntityLabel(tenantId, alarm.getOriginator()).orElse(null); if (detailsOpt.isPresent() && detailsOpt.get().getName() != null) {
NameLabelAndCustomerDetails details = detailsOpt.get();
originatorName = details.getName();
originatorLabel = details.getLabel();
} else {
originatorName = "Deleted";
originatorLabel = "Deleted";
}
if (alarm.getAssigneeId() != null) { if (alarm.getAssigneeId() != null) {
User assignedUser = userService.findUserById(tenantId, alarm.getAssigneeId()); User assignedUser = userService.findUserById(tenantId, alarm.getAssigneeId());

View File

@ -16,9 +16,7 @@
package org.thingsboard.server.dao.entity; package org.thingsboard.server.dao.entity;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.thingsboard.server.common.data.HasCustomerId; import org.thingsboard.server.common.data.HasCustomerId;
@ -26,6 +24,7 @@ import org.thingsboard.server.common.data.HasEmail;
import org.thingsboard.server.common.data.HasLabel; import org.thingsboard.server.common.data.HasLabel;
import org.thingsboard.server.common.data.HasName; import org.thingsboard.server.common.data.HasName;
import org.thingsboard.server.common.data.HasTitle; import org.thingsboard.server.common.data.HasTitle;
import org.thingsboard.server.common.data.id.NameLabelAndCustomerDetails;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
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;
@ -98,6 +97,12 @@ public class BaseEntityService extends AbstractEntityService implements EntitySe
return fetchAndConvert(tenantId, entityId, this::getCustomerId); return fetchAndConvert(tenantId, entityId, this::getCustomerId);
} }
@Override
public Optional<NameLabelAndCustomerDetails> fetchNameLabelAndCustomerDetails(TenantId tenantId, EntityId entityId) {
log.trace("Executing fetchNameLabelAndCustomerDetails [{}]", entityId);
return fetchAndConvert(tenantId, entityId, this::getNameLabelAndCustomerDetails);
}
private <T> Optional<T> fetchAndConvert(TenantId tenantId, EntityId entityId, Function<HasId<?>, T> converter) { private <T> Optional<T> fetchAndConvert(TenantId tenantId, EntityId entityId, Function<HasId<?>, T> converter) {
EntityDaoService entityDaoService = entityServiceRegistry.getServiceByEntityType(entityId.getEntityType()); EntityDaoService entityDaoService = entityServiceRegistry.getServiceByEntityType(entityId.getEntityType());
Optional<HasId<?>> entityOpt = entityDaoService.findEntity(tenantId, entityId); Optional<HasId<?>> entityOpt = entityDaoService.findEntity(tenantId, entityId);
@ -125,9 +130,9 @@ public class BaseEntityService extends AbstractEntityService implements EntitySe
return entityLabel; return entityLabel;
} }
private CustomerId getCustomerId(HasId<?> hasId) { private CustomerId getCustomerId(HasId<?> entity) {
if (hasId instanceof HasCustomerId) { if (entity instanceof HasCustomerId) {
HasCustomerId hasCustomerId = (HasCustomerId) hasId; HasCustomerId hasCustomerId = (HasCustomerId) entity;
CustomerId customerId = hasCustomerId.getCustomerId(); CustomerId customerId = hasCustomerId.getCustomerId();
if (customerId == null) { if (customerId == null) {
customerId = NULL_CUSTOMER_ID; customerId = NULL_CUSTOMER_ID;
@ -137,6 +142,10 @@ public class BaseEntityService extends AbstractEntityService implements EntitySe
return NULL_CUSTOMER_ID; return NULL_CUSTOMER_ID;
} }
private NameLabelAndCustomerDetails getNameLabelAndCustomerDetails(HasId<?> entity) {
return new NameLabelAndCustomerDetails(getName(entity), getLabel(entity), getCustomerId(entity));
}
private static void validateEntityCountQuery(EntityCountQuery query) { private static void validateEntityCountQuery(EntityCountQuery query) {
if (query == null) { if (query == null) {
throw new IncorrectParameterException("Query must be specified."); throw new IncorrectParameterException("Query must be specified.");