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

View File

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