From eac7ce25a8ceb524efaebc7581a0030e5118f803 Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Fri, 17 Mar 2023 14:17:24 +0200 Subject: [PATCH] Add tenantId to device and asset profile info objects --- .../BaseAssetProfileControllerTest.java | 1 + .../BaseDeviceProfileControllerTest.java | 1 + .../server/common/data/DeviceProfileInfo.java | 11 +++++++++-- .../common/data/asset/AssetProfileInfo.java | 17 ++++++++++++----- .../dao/asset/AssetProfileServiceImpl.java | 2 +- .../dao/device/DeviceProfileServiceImpl.java | 2 +- .../dao/sql/asset/AssetProfileRepository.java | 6 +++--- .../dao/sql/device/DeviceProfileRepository.java | 8 ++++---- .../service/BaseAssetProfileServiceTest.java | 2 +- .../service/BaseDeviceProfileServiceTest.java | 2 +- 10 files changed, 34 insertions(+), 18 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseAssetProfileControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseAssetProfileControllerTest.java index caeaf83c4b..2f7c51c3a2 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseAssetProfileControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseAssetProfileControllerTest.java @@ -425,6 +425,7 @@ public abstract class BaseAssetProfileControllerTest extends AbstractControllerT Collections.sort(loadedAssetProfileInfos, assetProfileInfoIdComparator); List assetProfileInfos = assetProfiles.stream().map(assetProfile -> new AssetProfileInfo(assetProfile.getId(), + assetProfile.getTenantId(), assetProfile.getName(), assetProfile.getImage(), assetProfile.getDefaultDashboardId())).collect(Collectors.toList()); Assert.assertEquals(assetProfileInfos, loadedAssetProfileInfos); diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java index 1546e0814e..4d7c614ddb 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java @@ -546,6 +546,7 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController Collections.sort(loadedDeviceProfileInfos, deviceProfileInfoIdComparator); List deviceProfileInfos = deviceProfiles.stream().map(deviceProfile -> new DeviceProfileInfo(deviceProfile.getId(), + deviceProfile.getTenantId(), deviceProfile.getName(), deviceProfile.getImage(), deviceProfile.getDefaultDashboardId(), deviceProfile.getType(), deviceProfile.getTransportType())).collect(Collectors.toList()); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfileInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfileInfo.java index fd68bbfae4..295b9f6b99 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfileInfo.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfileInfo.java @@ -24,6 +24,7 @@ import lombok.Value; import org.thingsboard.server.common.data.id.DashboardId; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityIdFactory; +import org.thingsboard.server.common.data.id.TenantId; import java.util.UUID; @@ -41,22 +42,28 @@ public class DeviceProfileInfo extends EntityInfo { @ApiModelProperty(position = 6, value = "Type of the transport used to connect the device. Default transport supports HTTP, CoAP and MQTT.") private final DeviceTransportType transportType; + @ApiModelProperty(position = 7, value = "Tenant id.") + private final TenantId tenantId; + @JsonCreator public DeviceProfileInfo(@JsonProperty("id") EntityId id, + @JsonProperty("tenantId") TenantId tenantId, @JsonProperty("name") String name, @JsonProperty("image") String image, @JsonProperty("defaultDashboardId") DashboardId defaultDashboardId, @JsonProperty("type") DeviceProfileType type, @JsonProperty("transportType") DeviceTransportType transportType) { super(id, name); + this.tenantId = tenantId; this.image = image; this.defaultDashboardId = defaultDashboardId; this.type = type; this.transportType = transportType; } - public DeviceProfileInfo(UUID uuid, String name, String image, UUID defaultDashboardId, DeviceProfileType type, DeviceTransportType transportType) { + public DeviceProfileInfo(UUID uuid, UUID tenantId, String name, String image, UUID defaultDashboardId, DeviceProfileType type, DeviceTransportType transportType) { super(EntityIdFactory.getByTypeAndUuid(EntityType.DEVICE_PROFILE, uuid), name); + this.tenantId = new TenantId(tenantId); this.image = image; this.defaultDashboardId = defaultDashboardId != null ? new DashboardId(defaultDashboardId) : null; this.type = type; @@ -64,7 +71,7 @@ public class DeviceProfileInfo extends EntityInfo { } public DeviceProfileInfo(DeviceProfile profile) { - this(profile.getId(), profile.getName(), profile.getImage(), profile.getDefaultDashboardId(), + this(profile.getId(), profile.getTenantId(), profile.getName(), profile.getImage(), profile.getDefaultDashboardId(), profile.getType(), profile.getTransportType()); } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/asset/AssetProfileInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/asset/AssetProfileInfo.java index 0b6fa3a612..e590c4c094 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/asset/AssetProfileInfo.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/asset/AssetProfileInfo.java @@ -26,6 +26,7 @@ import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.id.DashboardId; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityIdFactory; +import org.thingsboard.server.common.data.id.TenantId; import java.util.UUID; @@ -39,24 +40,30 @@ public class AssetProfileInfo extends EntityInfo { @ApiModelProperty(position = 4, value = "Reference to the dashboard. Used in the mobile application to open the default dashboard when user navigates to asset details.") private final DashboardId defaultDashboardId; + @ApiModelProperty(position = 5, value = "Tenant id.") + private final TenantId tenantId; + @JsonCreator public AssetProfileInfo(@JsonProperty("id") EntityId id, - @JsonProperty("name") String name, - @JsonProperty("image") String image, - @JsonProperty("defaultDashboardId") DashboardId defaultDashboardId) { + @JsonProperty("tenantId") TenantId tenantId, + @JsonProperty("name") String name, + @JsonProperty("image") String image, + @JsonProperty("defaultDashboardId") DashboardId defaultDashboardId) { super(id, name); + this.tenantId = tenantId; this.image = image; this.defaultDashboardId = defaultDashboardId; } - public AssetProfileInfo(UUID uuid, String name, String image, UUID defaultDashboardId) { + public AssetProfileInfo(UUID uuid, UUID tenantId, String name, String image, UUID defaultDashboardId) { super(EntityIdFactory.getByTypeAndUuid(EntityType.ASSET_PROFILE, uuid), name); + this.tenantId = new TenantId(tenantId); this.image = image; this.defaultDashboardId = defaultDashboardId != null ? new DashboardId(defaultDashboardId) : null; } public AssetProfileInfo(AssetProfile profile) { - this(profile.getId(), profile.getName(), profile.getImage(), profile.getDefaultDashboardId()); + this(profile.getId(), profile.getTenantId(), profile.getName(), profile.getImage(), profile.getDefaultDashboardId()); } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/asset/AssetProfileServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/asset/AssetProfileServiceImpl.java index f34941521a..c464d06857 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/asset/AssetProfileServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/asset/AssetProfileServiceImpl.java @@ -293,7 +293,7 @@ public class AssetProfileServiceImpl extends AbstractCachedEntityService, ExportableEntityRepository { - @Query("SELECT new org.thingsboard.server.common.data.asset.AssetProfileInfo(a.id, a.name, a.image, a.defaultDashboardId) " + + @Query("SELECT new org.thingsboard.server.common.data.asset.AssetProfileInfo(a.id, a.tenantId, a.name, a.image, a.defaultDashboardId) " + "FROM AssetProfileEntity a " + "WHERE a.id = :assetProfileId") AssetProfileInfo findAssetProfileInfoById(@Param("assetProfileId") UUID assetProfileId); @@ -39,7 +39,7 @@ public interface AssetProfileRepository extends JpaRepository findAssetProfileInfos(@Param("tenantId") UUID tenantId, @@ -50,7 +50,7 @@ public interface AssetProfileRepository extends JpaRepository, ExportableEntityRepository { - @Query("SELECT new org.thingsboard.server.common.data.DeviceProfileInfo(d.id, d.name, d.image, d.defaultDashboardId, d.type, d.transportType) " + + @Query("SELECT new org.thingsboard.server.common.data.DeviceProfileInfo(d.id, d.tenantId, d.name, d.image, d.defaultDashboardId, d.type, d.transportType) " + "FROM DeviceProfileEntity d " + "WHERE d.id = :deviceProfileId") DeviceProfileInfo findDeviceProfileInfoById(@Param("deviceProfileId") UUID deviceProfileId); @@ -40,14 +40,14 @@ public interface DeviceProfileRepository extends JpaRepository findDeviceProfileInfos(@Param("tenantId") UUID tenantId, @Param("textSearch") String textSearch, Pageable pageable); - @Query("SELECT new org.thingsboard.server.common.data.DeviceProfileInfo(d.id, d.name, d.image, d.defaultDashboardId, d.type, d.transportType) " + + @Query("SELECT new org.thingsboard.server.common.data.DeviceProfileInfo(d.id, d.tenantId, d.name, d.image, d.defaultDashboardId, d.type, d.transportType) " + "FROM DeviceProfileEntity d WHERE " + "d.tenantId = :tenantId AND d.transportType = :transportType AND LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%'))") Page findDeviceProfileInfos(@Param("tenantId") UUID tenantId, @@ -59,7 +59,7 @@ public interface DeviceProfileRepository extends JpaRepository assetProfileInfos = assetProfiles.stream() - .map(assetProfile -> new AssetProfileInfo(assetProfile.getId(), + .map(assetProfile -> new AssetProfileInfo(assetProfile.getId(), assetProfile.getTenantId(), assetProfile.getName(), assetProfile.getImage(), assetProfile.getDefaultDashboardId())).collect(Collectors.toList()); Assert.assertEquals(assetProfileInfos, loadedAssetProfileInfos); diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceProfileServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceProfileServiceTest.java index d669c24cd6..744de8f027 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceProfileServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceProfileServiceTest.java @@ -352,7 +352,7 @@ public abstract class BaseDeviceProfileServiceTest extends AbstractServiceTest { Collections.sort(loadedDeviceProfileInfos, deviceProfileInfoIdComparator); List deviceProfileInfos = deviceProfiles.stream() - .map(deviceProfile -> new DeviceProfileInfo(deviceProfile.getId(), + .map(deviceProfile -> new DeviceProfileInfo(deviceProfile.getId(), deviceProfile.getTenantId(), deviceProfile.getName(), deviceProfile.getImage(), deviceProfile.getDefaultDashboardId(), deviceProfile.getType(), deviceProfile.getTransportType())).collect(Collectors.toList());