From 7ff599f7c40b15202137dcc5def2e286f0bad9cd Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Fri, 16 Aug 2019 19:40:20 +0300 Subject: [PATCH] Introduct AssetInfo and EntityViewInfo dtos --- .../server/controller/AssetController.java | 64 +++++++ .../server/controller/BaseController.java | 25 +++ .../controller/EntityViewController.java | 69 ++++++- .../src/main/resources/thingsboard.yml | 1 + .../BaseEntityViewControllerTest.java | 43 +++-- .../server/dao/asset/AssetService.java | 11 ++ .../dao/entityview/EntityViewService.java | 11 ++ .../server/common/data/EntityViewInfo.java | 40 ++++ .../server/common/data/asset/AssetInfo.java | 40 ++++ .../server/dao/asset/AssetDao.java | 50 +++++ .../server/dao/asset/BaseAssetService.java | 44 +++++ .../server/dao/device/DeviceDao.java | 2 +- .../server/dao/entityview/EntityViewDao.java | 51 +++++- .../dao/entityview/EntityViewServiceImpl.java | 53 +++++- .../dao/model/sql/AbstractAssetEntity.java | 129 +++++++++++++ .../model/sql/AbstractEntityViewEntity.java | 172 ++++++++++++++++++ .../server/dao/model/sql/AssetEntity.java | 77 +------- .../server/dao/model/sql/AssetInfoEntity.java | 58 ++++++ .../server/dao/model/sql/DeviceEntity.java | 9 - .../dao/model/sql/EntityViewEntity.java | 118 +----------- .../dao/model/sql/EntityViewInfoEntity.java | 58 ++++++ .../server/dao/sql/asset/AssetRepository.java | 52 ++++++ .../server/dao/sql/asset/JpaAssetDao.java | 47 +++++ .../sql/entityview/EntityViewRepository.java | 51 ++++++ .../dao/sql/entityview/JpaEntityViewDao.java | 51 +++++- .../server/dao/SqlDaoServiceTestSuite.java | 2 +- .../dao/service/BaseAssetServiceTest.java | 35 ++-- dao/src/test/resources/nosql-test.properties | 1 + dao/src/test/resources/sql-test.properties | 1 + 29 files changed, 1119 insertions(+), 246 deletions(-) create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/EntityViewInfo.java create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/asset/AssetInfo.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractAssetEntity.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractEntityViewEntity.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetInfoEntity.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewInfoEntity.java diff --git a/application/src/main/java/org/thingsboard/server/controller/AssetController.java b/application/src/main/java/org/thingsboard/server/controller/AssetController.java index 060ce10cb7..639f8da948 100644 --- a/application/src/main/java/org/thingsboard/server/controller/AssetController.java +++ b/application/src/main/java/org/thingsboard/server/controller/AssetController.java @@ -30,6 +30,7 @@ import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.asset.AssetInfo; import org.thingsboard.server.common.data.asset.AssetSearchQuery; import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; @@ -69,6 +70,19 @@ public class AssetController extends BaseController { } } + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") + @RequestMapping(value = "/asset/info/{assetId}", method = RequestMethod.GET) + @ResponseBody + public AssetInfo getAssetInfoById(@PathVariable(ASSET_ID) String strAssetId) throws ThingsboardException { + checkParameter(ASSET_ID, strAssetId); + try { + AssetId assetId = new AssetId(toUUID(strAssetId)); + return checkAssetInfoId(assetId, Operation.READ); + } catch (Exception e) { + throw handleException(e); + } + } + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") @RequestMapping(value = "/asset", method = RequestMethod.POST) @ResponseBody @@ -229,6 +243,29 @@ public class AssetController extends BaseController { } } + @PreAuthorize("hasAuthority('TENANT_ADMIN')") + @RequestMapping(value = "/tenant/assetInfos", params = {"pageSize", "page"}, method = RequestMethod.GET) + @ResponseBody + public PageData getTenantAssetInfos( + @RequestParam int pageSize, + @RequestParam int page, + @RequestParam(required = false) String type, + @RequestParam(required = false) String textSearch, + @RequestParam(required = false) String sortProperty, + @RequestParam(required = false) String sortOrder) throws ThingsboardException { + try { + TenantId tenantId = getCurrentUser().getTenantId(); + PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); + if (type != null && type.trim().length() > 0) { + return checkNotNull(assetService.findAssetInfosByTenantIdAndType(tenantId, type, pageLink)); + } else { + return checkNotNull(assetService.findAssetInfosByTenantId(tenantId, pageLink)); + } + } catch (Exception e) { + throw handleException(e); + } + } + @PreAuthorize("hasAuthority('TENANT_ADMIN')") @RequestMapping(value = "/tenant/assets", params = {"assetName"}, method = RequestMethod.GET) @ResponseBody @@ -269,6 +306,33 @@ public class AssetController extends BaseController { } } + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") + @RequestMapping(value = "/customer/{customerId}/assetInfos", params = {"pageSize", "page"}, method = RequestMethod.GET) + @ResponseBody + public PageData getCustomerAssetInfos( + @PathVariable("customerId") String strCustomerId, + @RequestParam int pageSize, + @RequestParam int page, + @RequestParam(required = false) String type, + @RequestParam(required = false) String textSearch, + @RequestParam(required = false) String sortProperty, + @RequestParam(required = false) String sortOrder) throws ThingsboardException { + checkParameter("customerId", strCustomerId); + try { + TenantId tenantId = getCurrentUser().getTenantId(); + CustomerId customerId = new CustomerId(toUUID(strCustomerId)); + checkCustomerId(customerId, Operation.READ); + PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); + if (type != null && type.trim().length() > 0) { + return checkNotNull(assetService.findAssetInfosByTenantIdAndCustomerIdAndType(tenantId, customerId, type, pageLink)); + } else { + return checkNotNull(assetService.findAssetInfosByTenantIdAndCustomerId(tenantId, customerId, pageLink)); + } + } catch (Exception e) { + throw handleException(e); + } + } + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") @RequestMapping(value = "/assets", params = {"assetIds"}, method = RequestMethod.GET) @ResponseBody diff --git a/application/src/main/java/org/thingsboard/server/controller/BaseController.java b/application/src/main/java/org/thingsboard/server/controller/BaseController.java index b0c505b575..c363a11a56 100644 --- a/application/src/main/java/org/thingsboard/server/controller/BaseController.java +++ b/application/src/main/java/org/thingsboard/server/controller/BaseController.java @@ -33,6 +33,7 @@ import org.thingsboard.server.common.data.alarm.Alarm; import org.thingsboard.server.common.data.alarm.AlarmId; import org.thingsboard.server.common.data.alarm.AlarmInfo; import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.asset.AssetInfo; import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; import org.thingsboard.server.common.data.exception.ThingsboardException; @@ -396,6 +397,18 @@ public abstract class BaseController { } } + EntityViewInfo checkEntityViewInfoId(EntityViewId entityViewId, Operation operation) throws ThingsboardException { + try { + validateId(entityViewId, "Incorrect entityViewId " + entityViewId); + EntityViewInfo entityView = entityViewService.findEntityViewInfoById(getCurrentUser().getTenantId(), entityViewId); + checkNotNull(entityView); + accessControlService.checkPermission(getCurrentUser(), Resource.DEVICE, operation, entityViewId, entityView); + return entityView; + } catch (Exception e) { + throw handleException(e, false); + } + } + Asset checkAssetId(AssetId assetId, Operation operation) throws ThingsboardException { try { validateId(assetId, "Incorrect assetId " + assetId); @@ -408,6 +421,18 @@ public abstract class BaseController { } } + AssetInfo checkAssetInfoId(AssetId assetId, Operation operation) throws ThingsboardException { + try { + validateId(assetId, "Incorrect assetId " + assetId); + AssetInfo asset = assetService.findAssetInfoById(getCurrentUser().getTenantId(), assetId); + checkNotNull(asset); + accessControlService.checkPermission(getCurrentUser(), Resource.DEVICE, operation, assetId, asset); + return asset; + } catch (Exception e) { + throw handleException(e, false); + } + } + Alarm checkAlarmId(AlarmId alarmId, Operation operation) throws ThingsboardException { try { validateId(alarmId, "Incorrect alarmId " + alarmId); diff --git a/application/src/main/java/org/thingsboard/server/controller/EntityViewController.java b/application/src/main/java/org/thingsboard/server/controller/EntityViewController.java index e8438e0703..c0476aa93b 100644 --- a/application/src/main/java/org/thingsboard/server/controller/EntityViewController.java +++ b/application/src/main/java/org/thingsboard/server/controller/EntityViewController.java @@ -29,11 +29,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; -import org.thingsboard.server.common.data.Customer; -import org.thingsboard.server.common.data.DataConstants; -import org.thingsboard.server.common.data.EntitySubtype; -import org.thingsboard.server.common.data.EntityType; -import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.*; import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.entityview.EntityViewSearchQuery; import org.thingsboard.server.common.data.exception.ThingsboardException; @@ -82,6 +78,19 @@ public class EntityViewController extends BaseController { } } + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") + @RequestMapping(value = "/entityView/info/{entityViewId}", method = RequestMethod.GET) + @ResponseBody + public EntityViewInfo getEntityViewInfoById(@PathVariable(ENTITY_VIEW_ID) String strEntityViewId) throws ThingsboardException { + checkParameter(ENTITY_VIEW_ID, strEntityViewId); + try { + EntityViewId entityViewId = new EntityViewId(toUUID(strEntityViewId)); + return checkEntityViewInfoId(entityViewId, Operation.READ); + } catch (Exception e) { + throw handleException(e); + } + } + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") @RequestMapping(value = "/entityView", method = RequestMethod.POST) @ResponseBody @@ -282,6 +291,33 @@ public class EntityViewController extends BaseController { } } + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") + @RequestMapping(value = "/customer/{customerId}/entityViewInfos", params = {"pageSize", "page"}, method = RequestMethod.GET) + @ResponseBody + public PageData getCustomerEntityViewInfos( + @PathVariable("customerId") String strCustomerId, + @RequestParam int pageSize, + @RequestParam int page, + @RequestParam(required = false) String type, + @RequestParam(required = false) String textSearch, + @RequestParam(required = false) String sortProperty, + @RequestParam(required = false) String sortOrder) throws ThingsboardException { + checkParameter("customerId", strCustomerId); + try { + TenantId tenantId = getCurrentUser().getTenantId(); + CustomerId customerId = new CustomerId(toUUID(strCustomerId)); + checkCustomerId(customerId, Operation.READ); + PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); + if (type != null && type.trim().length() > 0) { + return checkNotNull(entityViewService.findEntityViewInfosByTenantIdAndCustomerIdAndType(tenantId, customerId, type, pageLink)); + } else { + return checkNotNull(entityViewService.findEntityViewInfosByTenantIdAndCustomerId(tenantId, customerId, pageLink)); + } + } catch (Exception e) { + throw handleException(e); + } + } + @PreAuthorize("hasAuthority('TENANT_ADMIN')") @RequestMapping(value = "/tenant/entityViews", params = {"pageSize", "page"}, method = RequestMethod.GET) @ResponseBody @@ -306,6 +342,29 @@ public class EntityViewController extends BaseController { } } + @PreAuthorize("hasAuthority('TENANT_ADMIN')") + @RequestMapping(value = "/tenant/entityViewInfos", params = {"pageSize", "page"}, method = RequestMethod.GET) + @ResponseBody + public PageData getTenantEntityViewInfos( + @RequestParam int pageSize, + @RequestParam int page, + @RequestParam(required = false) String type, + @RequestParam(required = false) String textSearch, + @RequestParam(required = false) String sortProperty, + @RequestParam(required = false) String sortOrder) throws ThingsboardException { + try { + TenantId tenantId = getCurrentUser().getTenantId(); + PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); + if (type != null && type.trim().length() > 0) { + return checkNotNull(entityViewService.findEntityViewInfosByTenantIdAndType(tenantId, type, pageLink)); + } else { + return checkNotNull(entityViewService.findEntityViewInfosByTenantId(tenantId, pageLink)); + } + } catch (Exception e) { + throw handleException(e); + } + } + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") @RequestMapping(value = "/entityViews", method = RequestMethod.POST) @ResponseBody diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index 01f6f130ee..7fdabc443f 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -308,6 +308,7 @@ spring.resources.chain: enabled: "true" spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation: "true" +spring.jpa.properties.hibernate.order_by.default_null_ordering: "last" # SQL DAO Configuration spring: diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java index 04f315bfc9..982d925d6b 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java @@ -25,11 +25,7 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.thingsboard.server.common.data.Customer; -import org.thingsboard.server.common.data.Device; -import org.thingsboard.server.common.data.EntityView; -import org.thingsboard.server.common.data.Tenant; -import org.thingsboard.server.common.data.User; +import org.thingsboard.server.common.data.*; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.objects.AttributesEntityView; import org.thingsboard.server.common.data.objects.TelemetryEntityView; @@ -214,16 +210,20 @@ public abstract class BaseEntityViewControllerTest extends AbstractControllerTes @Test public void testGetCustomerEntityViews() throws Exception { - CustomerId customerId = doPost("/api/customer", getNewCustomer("Test customer"), Customer.class).getId(); - String urlTemplate = "/api/customer/" + customerId.getId().toString() + "/entityViews?"; + Customer customer = doPost("/api/customer", getNewCustomer("Test customer"), Customer.class); + CustomerId customerId = customer.getId(); + String urlTemplate = "/api/customer/" + customerId.getId().toString() + "/entityViewInfos?"; - List views = new ArrayList<>(); + List views = new ArrayList<>(); for (int i = 0; i < 128; i++) { - views.add(doPost("/api/customer/" + customerId.getId().toString() + "/entityView/" - + getNewSavedEntityView("Test entity view " + i).getId().getId().toString(), EntityView.class)); + views.add( + new EntityViewInfo(doPost("/api/customer/" + customerId.getId().toString() + "/entityView/" + + getNewSavedEntityView("Test entity view " + i).getId().getId().toString(), EntityView.class), + customer.getTitle(), customer.isPublic()) + ); } - List loadedViews = loadListOf(new PageLink(23), urlTemplate); + List loadedViews = loadListOfInfo(new PageLink(23), urlTemplate); Collections.sort(views, idComparator); Collections.sort(loadedViews, idComparator); @@ -274,11 +274,11 @@ public abstract class BaseEntityViewControllerTest extends AbstractControllerTes @Test public void testGetTenantEntityViews() throws Exception { - List views = new ArrayList<>(); + List views = new ArrayList<>(); for (int i = 0; i < 178; i++) { - views.add(getNewSavedEntityView("Test entity view" + i)); + views.add(new EntityViewInfo(getNewSavedEntityView("Test entity view" + i), null, false)); } - List loadedViews = loadListOf(new PageLink(23), "/api/tenant/entityViews?"); + List loadedViews = loadListOfInfo(new PageLink(23), "/api/tenant/entityViewInfos?"); Collections.sort(views, idComparator); Collections.sort(loadedViews, idComparator); @@ -530,4 +530,19 @@ public abstract class BaseEntityViewControllerTest extends AbstractControllerTes return loadedItems; } + + private List loadListOfInfo(PageLink pageLink, String urlTemplate) throws Exception { + List loadedItems = new ArrayList<>(); + PageData pageData; + do { + pageData = doGetTypedWithPageLink(urlTemplate, new TypeReference>() { + }, pageLink); + loadedItems.addAll(pageData.getData()); + if (pageData.hasNext()) { + pageLink = pageLink.nextPageLink(); + } + } while (pageData.hasNext()); + + return loadedItems; + } } diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/asset/AssetService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/asset/AssetService.java index 64803654fd..2a9cdcc690 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/asset/AssetService.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/asset/AssetService.java @@ -18,6 +18,7 @@ package org.thingsboard.server.dao.asset; import com.google.common.util.concurrent.ListenableFuture; import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.asset.AssetInfo; import org.thingsboard.server.common.data.asset.AssetSearchQuery; import org.thingsboard.server.common.data.id.AssetId; import org.thingsboard.server.common.data.id.CustomerId; @@ -30,6 +31,8 @@ import java.util.Optional; public interface AssetService { + AssetInfo findAssetInfoById(TenantId tenantId, AssetId assetId); + Asset findAssetById(TenantId tenantId, AssetId assetId); ListenableFuture findAssetByIdAsync(TenantId tenantId, AssetId assetId); @@ -46,16 +49,24 @@ public interface AssetService { PageData findAssetsByTenantId(TenantId tenantId, PageLink pageLink); + PageData findAssetInfosByTenantId(TenantId tenantId, PageLink pageLink); + PageData findAssetsByTenantIdAndType(TenantId tenantId, String type, PageLink pageLink); + PageData findAssetInfosByTenantIdAndType(TenantId tenantId, String type, PageLink pageLink); + ListenableFuture> findAssetsByTenantIdAndIdsAsync(TenantId tenantId, List assetIds); void deleteAssetsByTenantId(TenantId tenantId); PageData findAssetsByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink); + PageData findAssetInfosByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink); + PageData findAssetsByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, PageLink pageLink); + PageData findAssetInfosByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, PageLink pageLink); + ListenableFuture> findAssetsByTenantIdCustomerIdAndIdsAsync(TenantId tenantId, CustomerId customerId, List assetIds); void unassignCustomerAssets(TenantId tenantId, CustomerId customerId); diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java index 310f302725..3c9d6db690 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java @@ -18,6 +18,7 @@ package org.thingsboard.server.dao.entityview; import com.google.common.util.concurrent.ListenableFuture; import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.EntityViewInfo; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.entityview.EntityViewSearchQuery; import org.thingsboard.server.common.data.id.CustomerId; @@ -42,18 +43,28 @@ public interface EntityViewService { void unassignCustomerEntityViews(TenantId tenantId, CustomerId customerId); + EntityViewInfo findEntityViewInfoById(TenantId tenantId, EntityViewId entityViewId); + EntityView findEntityViewById(TenantId tenantId, EntityViewId entityViewId); EntityView findEntityViewByTenantIdAndName(TenantId tenantId, String name); PageData findEntityViewByTenantId(TenantId tenantId, PageLink pageLink); + PageData findEntityViewInfosByTenantId(TenantId tenantId, PageLink pageLink); + PageData findEntityViewByTenantIdAndType(TenantId tenantId, PageLink pageLink, String type); + PageData findEntityViewInfosByTenantIdAndType(TenantId tenantId, String type, PageLink pageLink); + PageData findEntityViewsByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink); + PageData findEntityViewInfosByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink); + PageData findEntityViewsByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, PageLink pageLink, String type); + PageData findEntityViewInfosByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, PageLink pageLink); + ListenableFuture> findEntityViewsByQuery(TenantId tenantId, EntityViewSearchQuery query); ListenableFuture findEntityViewByIdAsync(TenantId tenantId, EntityViewId entityViewId); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/EntityViewInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/EntityViewInfo.java new file mode 100644 index 0000000000..7cf66faff4 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/EntityViewInfo.java @@ -0,0 +1,40 @@ +/** + * Copyright © 2016-2019 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; + +import lombok.Data; +import org.thingsboard.server.common.data.id.EntityViewId; + +@Data +public class EntityViewInfo extends EntityView { + + private String customerTitle; + private boolean customerIsPublic; + + public EntityViewInfo() { + super(); + } + + public EntityViewInfo(EntityViewId entityViewId) { + super(entityViewId); + } + + public EntityViewInfo(EntityView entityView, String customerTitle, boolean customerIsPublic) { + super(entityView); + this.customerTitle = customerTitle; + this.customerIsPublic = customerIsPublic; + } +} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/asset/AssetInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/asset/AssetInfo.java new file mode 100644 index 0000000000..ece667bc54 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/asset/AssetInfo.java @@ -0,0 +1,40 @@ +/** + * Copyright © 2016-2019 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.asset; + +import lombok.Data; +import org.thingsboard.server.common.data.id.AssetId; + +@Data +public class AssetInfo extends Asset { + + private String customerTitle; + private boolean customerIsPublic; + + public AssetInfo() { + super(); + } + + public AssetInfo(AssetId assetId) { + super(assetId); + } + + public AssetInfo(Asset asset, String customerTitle, boolean customerIsPublic) { + super(asset); + this.customerTitle = customerTitle; + this.customerIsPublic = customerIsPublic; + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/asset/AssetDao.java b/dao/src/main/java/org/thingsboard/server/dao/asset/AssetDao.java index 4caffc43d5..656dd96e3b 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/asset/AssetDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/asset/AssetDao.java @@ -18,6 +18,7 @@ package org.thingsboard.server.dao.asset; import com.google.common.util.concurrent.ListenableFuture; import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.asset.AssetInfo; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; @@ -33,6 +34,15 @@ import java.util.UUID; */ public interface AssetDao extends Dao { + /** + * Find asset info by id. + * + * @param tenantId the tenant id + * @param assetId the asset id + * @return the asset info object + */ + AssetInfo findAssetInfoById(TenantId tenantId, UUID assetId); + /** * Save or update asset object * @@ -50,6 +60,15 @@ public interface AssetDao extends Dao { */ PageData findAssetsByTenantId(UUID tenantId, PageLink pageLink); + /** + * Find asset infos by tenantId and page link. + * + * @param tenantId the tenantId + * @param pageLink the page link + * @return the list of asset info objects + */ + PageData findAssetInfosByTenantId(UUID tenantId, PageLink pageLink); + /** * Find assets by tenantId, type and page link. * @@ -60,6 +79,16 @@ public interface AssetDao extends Dao { */ PageData findAssetsByTenantIdAndType(UUID tenantId, String type, PageLink pageLink); + /** + * Find asset infos by tenantId, type and page link. + * + * @param tenantId the tenantId + * @param type the type + * @param pageLink the page link + * @return the list of asset info objects + */ + PageData findAssetInfosByTenantIdAndType(UUID tenantId, String type, PageLink pageLink); + /** * Find assets by tenantId and assets Ids. * @@ -79,6 +108,16 @@ public interface AssetDao extends Dao { */ PageData findAssetsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, PageLink pageLink); + /** + * Find asset infos by tenantId, customerId and page link. + * + * @param tenantId the tenantId + * @param customerId the customerId + * @param pageLink the page link + * @return the list of asset info objects + */ + PageData findAssetInfosByTenantIdAndCustomerId(UUID tenantId, UUID customerId, PageLink pageLink); + /** * Find assets by tenantId, customerId, type and page link. * @@ -90,6 +129,17 @@ public interface AssetDao extends Dao { */ PageData findAssetsByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, PageLink pageLink); + /** + * Find asset infos by tenantId, customerId, type and page link. + * + * @param tenantId the tenantId + * @param customerId the customerId + * @param type the type + * @param pageLink the page link + * @return the list of asset info objects + */ + PageData findAssetInfosByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, PageLink pageLink); + /** * Find assets by tenantId, customerId and assets Ids. * diff --git a/dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java b/dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java index c57bd8d937..c45892139e 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java @@ -33,6 +33,7 @@ import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityView; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.asset.AssetInfo; import org.thingsboard.server.common.data.asset.AssetSearchQuery; import org.thingsboard.server.common.data.id.AssetId; import org.thingsboard.server.common.data.id.CustomerId; @@ -85,6 +86,13 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ @Autowired private CacheManager cacheManager; + @Override + public AssetInfo findAssetInfoById(TenantId tenantId, AssetId assetId) { + log.trace("Executing findAssetInfoById [{}]", assetId); + validateId(assetId, INCORRECT_ASSET_ID + assetId); + return assetDao.findAssetInfoById(tenantId, assetId.getId()); + } + @Override public Asset findAssetById(TenantId tenantId, AssetId assetId) { log.trace("Executing findAssetById [{}]", assetId); @@ -164,6 +172,14 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ return assetDao.findAssetsByTenantId(tenantId.getId(), pageLink); } + @Override + public PageData findAssetInfosByTenantId(TenantId tenantId, PageLink pageLink) { + log.trace("Executing findAssetInfosByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validatePageLink(pageLink); + return assetDao.findAssetInfosByTenantId(tenantId.getId(), pageLink); + } + @Override public PageData findAssetsByTenantIdAndType(TenantId tenantId, String type, PageLink pageLink) { log.trace("Executing findAssetsByTenantIdAndType, tenantId [{}], type [{}], pageLink [{}]", tenantId, type, pageLink); @@ -173,6 +189,15 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ return assetDao.findAssetsByTenantIdAndType(tenantId.getId(), type, pageLink); } + @Override + public PageData findAssetInfosByTenantIdAndType(TenantId tenantId, String type, PageLink pageLink) { + log.trace("Executing findAssetInfosByTenantIdAndType, tenantId [{}], type [{}], pageLink [{}]", tenantId, type, pageLink); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validateString(type, "Incorrect type " + type); + validatePageLink(pageLink); + return assetDao.findAssetInfosByTenantIdAndType(tenantId.getId(), type, pageLink); + } + @Override public ListenableFuture> findAssetsByTenantIdAndIdsAsync(TenantId tenantId, List assetIds) { log.trace("Executing findAssetsByTenantIdAndIdsAsync, tenantId [{}], assetIds [{}]", tenantId, assetIds); @@ -197,6 +222,15 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ return assetDao.findAssetsByTenantIdAndCustomerId(tenantId.getId(), customerId.getId(), pageLink); } + @Override + public PageData findAssetInfosByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink) { + log.trace("Executing findAssetInfosByTenantIdAndCustomerId, tenantId [{}], customerId [{}], pageLink [{}]", tenantId, customerId, pageLink); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validateId(customerId, INCORRECT_CUSTOMER_ID + customerId); + validatePageLink(pageLink); + return assetDao.findAssetInfosByTenantIdAndCustomerId(tenantId.getId(), customerId.getId(), pageLink); + } + @Override public PageData findAssetsByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, PageLink pageLink) { log.trace("Executing findAssetsByTenantIdAndCustomerIdAndType, tenantId [{}], customerId [{}], type [{}], pageLink [{}]", tenantId, customerId, type, pageLink); @@ -207,6 +241,16 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ return assetDao.findAssetsByTenantIdAndCustomerIdAndType(tenantId.getId(), customerId.getId(), type, pageLink); } + @Override + public PageData findAssetInfosByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, PageLink pageLink) { + log.trace("Executing findAssetInfosByTenantIdAndCustomerIdAndType, tenantId [{}], customerId [{}], type [{}], pageLink [{}]", tenantId, customerId, type, pageLink); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validateId(customerId, INCORRECT_CUSTOMER_ID + customerId); + validateString(type, "Incorrect type " + type); + validatePageLink(pageLink); + return assetDao.findAssetInfosByTenantIdAndCustomerIdAndType(tenantId.getId(), customerId.getId(), type, pageLink); + } + @Override public ListenableFuture> findAssetsByTenantIdCustomerIdAndIdsAsync(TenantId tenantId, CustomerId customerId, List assetIds) { log.trace("Executing findAssetsByTenantIdAndCustomerIdAndIdsAsync, tenantId [{}], customerId [{}], assetIds [{}]", tenantId, customerId, assetIds); diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceDao.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceDao.java index ff567b7233..932d5ad845 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceDao.java @@ -85,7 +85,7 @@ public interface DeviceDao extends Dao { * @param tenantId the tenantId * @param type the type * @param pageLink the page link - * @return the list of device onfo objects + * @return the list of device info objects */ PageData findDeviceInfosByTenantIdAndType(UUID tenantId, String type, PageLink pageLink); diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java index ebc101861f..5465ac320d 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java @@ -16,9 +16,9 @@ package org.thingsboard.server.dao.entityview; import com.google.common.util.concurrent.ListenableFuture; -import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.EntityViewInfo; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; @@ -33,6 +33,15 @@ import java.util.UUID; */ public interface EntityViewDao extends Dao { + /** + * Find entity view info by id. + * + * @param tenantId the tenant id + * @param assetId the asset id + * @return the entity view info object + */ + EntityViewInfo findEntityViewInfoById(TenantId tenantId, UUID entityViewId); + /** * Save or update device object * @@ -50,6 +59,15 @@ public interface EntityViewDao extends Dao { */ PageData findEntityViewsByTenantId(UUID tenantId, PageLink pageLink); + /** + * Find entity view infos by tenantId and page link. + * + * @param tenantId the tenantId + * @param pageLink the page link + * @return the list of entity view info objects + */ + PageData findEntityViewInfosByTenantId(UUID tenantId, PageLink pageLink); + /** * Find entity views by tenantId, type and page link. * @@ -60,6 +78,16 @@ public interface EntityViewDao extends Dao { */ PageData findEntityViewsByTenantIdAndType(UUID tenantId, String type, PageLink pageLink); + /** + * Find entity view infos by tenantId, type and page link. + * + * @param tenantId the tenantId + * @param type the type + * @param pageLink the page link + * @return the list of entity view info objects + */ + PageData findEntityViewInfosByTenantIdAndType(UUID tenantId, String type, PageLink pageLink); + /** * Find entity views by tenantId and entity view name. * @@ -81,6 +109,16 @@ public interface EntityViewDao extends Dao { UUID customerId, PageLink pageLink); + /** + * Find entity view infos by tenantId, customerId and page link. + * + * @param tenantId the tenantId + * @param customerId the customerId + * @param pageLink the page link + * @return the list of entity view info objects + */ + PageData findEntityViewInfosByTenantIdAndCustomerId(UUID tenantId, UUID customerId, PageLink pageLink); + /** * Find entity views by tenantId, customerId, type and page link. * @@ -95,6 +133,17 @@ public interface EntityViewDao extends Dao { String type, PageLink pageLink); + /** + * Find entity view infos by tenantId, customerId, type and page link. + * + * @param tenantId the tenantId + * @param customerId the customerId + * @param type the type + * @param pageLink the page link + * @return the list of entity view info objects + */ + PageData findEntityViewInfosByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, PageLink pageLink); + ListenableFuture> findEntityViewsByTenantIdAndEntityIdAsync(UUID tenantId, UUID entityId); /** diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java index b0f323b2a6..08ffe5be51 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java @@ -28,11 +28,7 @@ import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Caching; import org.springframework.stereotype.Service; -import org.thingsboard.server.common.data.Customer; -import org.thingsboard.server.common.data.EntitySubtype; -import org.thingsboard.server.common.data.EntityType; -import org.thingsboard.server.common.data.EntityView; -import org.thingsboard.server.common.data.Tenant; +import org.thingsboard.server.common.data.*; import org.thingsboard.server.common.data.entityview.EntityViewSearchQuery; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.EntityId; @@ -124,6 +120,13 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti customerEntityViewsUnAssigner.removeEntities(tenantId, customerId); } + @Override + public EntityViewInfo findEntityViewInfoById(TenantId tenantId, EntityViewId entityViewId) { + log.trace("Executing findEntityViewInfoById [{}]", entityViewId); + validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); + return entityViewDao.findEntityViewInfoById(tenantId, entityViewId.getId()); + } + @Cacheable(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}") @Override public EntityView findEntityViewById(TenantId tenantId, EntityViewId entityViewId) { @@ -149,6 +152,14 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti return entityViewDao.findEntityViewsByTenantId(tenantId.getId(), pageLink); } + @Override + public PageData findEntityViewInfosByTenantId(TenantId tenantId, PageLink pageLink) { + log.trace("Executing findEntityViewInfosByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validatePageLink(pageLink); + return entityViewDao.findEntityViewInfosByTenantId(tenantId.getId(), pageLink); + } + @Override public PageData findEntityViewByTenantIdAndType(TenantId tenantId, PageLink pageLink, String type) { log.trace("Executing findEntityViewByTenantIdAndType, tenantId [{}], pageLink [{}], type [{}]", tenantId, pageLink, type); @@ -158,6 +169,15 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti return entityViewDao.findEntityViewsByTenantIdAndType(tenantId.getId(), type, pageLink); } + @Override + public PageData findEntityViewInfosByTenantIdAndType(TenantId tenantId, String type, PageLink pageLink) { + log.trace("Executing findEntityViewInfosByTenantIdAndType, tenantId [{}], pageLink [{}], type [{}]", tenantId, pageLink, type); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validatePageLink(pageLink); + validateString(type, "Incorrect type " + type); + return entityViewDao.findEntityViewInfosByTenantIdAndType(tenantId.getId(), type, pageLink); + } + @Override public PageData findEntityViewsByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink) { @@ -170,6 +190,17 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti customerId.getId(), pageLink); } + @Override + public PageData findEntityViewInfosByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink) { + log.trace("Executing findEntityViewInfosByTenantIdAndCustomerId, tenantId [{}], customerId [{}]," + + " pageLink [{}]", tenantId, customerId, pageLink); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validateId(customerId, INCORRECT_CUSTOMER_ID + customerId); + validatePageLink(pageLink); + return entityViewDao.findEntityViewInfosByTenantIdAndCustomerId(tenantId.getId(), + customerId.getId(), pageLink); + } + @Override public PageData findEntityViewsByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, PageLink pageLink, String type) { log.trace("Executing findEntityViewsByTenantIdAndCustomerIdAndType, tenantId [{}], customerId [{}]," + @@ -182,6 +213,18 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti customerId.getId(), type, pageLink); } + @Override + public PageData findEntityViewInfosByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, PageLink pageLink) { + log.trace("Executing findEntityViewInfosByTenantIdAndCustomerIdAndType, tenantId [{}], customerId [{}]," + + " pageLink [{}], type [{}]", tenantId, customerId, pageLink, type); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validateId(customerId, INCORRECT_CUSTOMER_ID + customerId); + validatePageLink(pageLink); + validateString(type, "Incorrect type " + type); + return entityViewDao.findEntityViewInfosByTenantIdAndCustomerIdAndType(tenantId.getId(), + customerId.getId(), type, pageLink); + } + @Override public ListenableFuture> findEntityViewsByQuery(TenantId tenantId, EntityViewSearchQuery query) { ListenableFuture> relations = relationService.findByQuery(tenantId, query.toEntitySearchQuery()); diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractAssetEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractAssetEntity.java new file mode 100644 index 0000000000..19c38f84d1 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractAssetEntity.java @@ -0,0 +1,129 @@ +/** + * Copyright © 2016-2019 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.dao.model.sql; + +import com.datastax.driver.core.utils.UUIDs; +import com.fasterxml.jackson.databind.JsonNode; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.thingsboard.server.common.data.UUIDConverter; +import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.id.AssetId; +import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.model.BaseSqlEntity; +import org.thingsboard.server.dao.model.ModelConstants; +import org.thingsboard.server.dao.model.SearchTextEntity; +import org.thingsboard.server.dao.util.mapping.JsonStringType; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.MappedSuperclass; +import javax.persistence.Table; + +import static org.thingsboard.server.dao.model.ModelConstants.ASSET_COLUMN_FAMILY_NAME; +import static org.thingsboard.server.dao.model.ModelConstants.ASSET_CUSTOMER_ID_PROPERTY; +import static org.thingsboard.server.dao.model.ModelConstants.ASSET_NAME_PROPERTY; +import static org.thingsboard.server.dao.model.ModelConstants.ASSET_TENANT_ID_PROPERTY; +import static org.thingsboard.server.dao.model.ModelConstants.ASSET_TYPE_PROPERTY; +import static org.thingsboard.server.dao.model.ModelConstants.SEARCH_TEXT_PROPERTY; + +@Data +@EqualsAndHashCode(callSuper = true) +@TypeDef(name = "json", typeClass = JsonStringType.class) +@MappedSuperclass +public abstract class AbstractAssetEntity extends BaseSqlEntity implements SearchTextEntity { + + @Column(name = ASSET_TENANT_ID_PROPERTY) + private String tenantId; + + @Column(name = ASSET_CUSTOMER_ID_PROPERTY) + private String customerId; + + @Column(name = ASSET_NAME_PROPERTY) + private String name; + + @Column(name = ASSET_TYPE_PROPERTY) + private String type; + + @Column(name = SEARCH_TEXT_PROPERTY) + private String searchText; + + @Type(type = "json") + @Column(name = ModelConstants.ASSET_ADDITIONAL_INFO_PROPERTY) + private JsonNode additionalInfo; + + public AbstractAssetEntity() { + super(); + } + + public AbstractAssetEntity(Asset asset) { + if (asset.getId() != null) { + this.setId(asset.getId().getId()); + } + if (asset.getTenantId() != null) { + this.tenantId = UUIDConverter.fromTimeUUID(asset.getTenantId().getId()); + } + if (asset.getCustomerId() != null) { + this.customerId = UUIDConverter.fromTimeUUID(asset.getCustomerId().getId()); + } + this.name = asset.getName(); + this.type = asset.getType(); + this.additionalInfo = asset.getAdditionalInfo(); + } + + public AbstractAssetEntity(AssetEntity assetEntity) { + this.setId(assetEntity.getId()); + this.tenantId = assetEntity.getTenantId(); + this.customerId = assetEntity.getCustomerId(); + this.type = assetEntity.getType(); + this.name = assetEntity.getName(); + this.searchText = assetEntity.getSearchText(); + this.additionalInfo = assetEntity.getAdditionalInfo(); + } + + @Override + public String getSearchTextSource() { + return name; + } + + @Override + public void setSearchText(String searchText) { + this.searchText = searchText; + } + + public String getSearchText() { + return searchText; + } + + protected Asset toAsset() { + Asset asset = new Asset(new AssetId(UUIDConverter.fromString(id))); + asset.setCreatedTime(UUIDs.unixTimestamp(UUIDConverter.fromString(id))); + if (tenantId != null) { + asset.setTenantId(new TenantId(UUIDConverter.fromString(tenantId))); + } + if (customerId != null) { + asset.setCustomerId(new CustomerId(UUIDConverter.fromString(customerId))); + } + asset.setName(name); + asset.setType(type); + asset.setAdditionalInfo(additionalInfo); + return asset; + } + +} \ No newline at end of file diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractEntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractEntityViewEntity.java new file mode 100644 index 0000000000..4aec387ae3 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractEntityViewEntity.java @@ -0,0 +1,172 @@ +/** + * Copyright © 2016-2019 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.dao.model.sql; + +import com.datastax.driver.core.utils.UUIDs; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.extern.slf4j.Slf4j; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.EntityIdFactory; +import org.thingsboard.server.common.data.id.EntityViewId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.objects.TelemetryEntityView; +import org.thingsboard.server.dao.model.BaseSqlEntity; +import org.thingsboard.server.dao.model.ModelConstants; +import org.thingsboard.server.dao.model.SearchTextEntity; +import org.thingsboard.server.dao.util.mapping.JsonStringType; + +import javax.persistence.*; +import java.io.IOException; + +import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_TYPE_PROPERTY; + +/** + * Created by Victor Basanets on 8/30/2017. + */ + +@Data +@EqualsAndHashCode(callSuper = true) +@TypeDef(name = "json", typeClass = JsonStringType.class) +@MappedSuperclass +@Slf4j +public abstract class AbstractEntityViewEntity extends BaseSqlEntity implements SearchTextEntity { + + @Column(name = ModelConstants.ENTITY_VIEW_ENTITY_ID_PROPERTY) + private String entityId; + + @Enumerated(EnumType.STRING) + @Column(name = ENTITY_TYPE_PROPERTY) + private EntityType entityType; + + @Column(name = ModelConstants.ENTITY_VIEW_TENANT_ID_PROPERTY) + private String tenantId; + + @Column(name = ModelConstants.ENTITY_VIEW_CUSTOMER_ID_PROPERTY) + private String customerId; + + @Column(name = ModelConstants.DEVICE_TYPE_PROPERTY) + private String type; + + @Column(name = ModelConstants.ENTITY_VIEW_NAME_PROPERTY) + private String name; + + @Column(name = ModelConstants.ENTITY_VIEW_KEYS_PROPERTY) + private String keys; + + @Column(name = ModelConstants.ENTITY_VIEW_START_TS_PROPERTY) + private long startTs; + + @Column(name = ModelConstants.ENTITY_VIEW_END_TS_PROPERTY) + private long endTs; + + @Column(name = ModelConstants.SEARCH_TEXT_PROPERTY) + private String searchText; + + @Type(type = "json") + @Column(name = ModelConstants.ENTITY_VIEW_ADDITIONAL_INFO_PROPERTY) + private JsonNode additionalInfo; + + private static final ObjectMapper mapper = new ObjectMapper(); + + public AbstractEntityViewEntity() { + super(); + } + + public AbstractEntityViewEntity(EntityView entityView) { + if (entityView.getId() != null) { + this.setId(entityView.getId().getId()); + } + if (entityView.getEntityId() != null) { + this.entityId = toString(entityView.getEntityId().getId()); + this.entityType = entityView.getEntityId().getEntityType(); + } + if (entityView.getTenantId() != null) { + this.tenantId = toString(entityView.getTenantId().getId()); + } + if (entityView.getCustomerId() != null) { + this.customerId = toString(entityView.getCustomerId().getId()); + } + this.type = entityView.getType(); + this.name = entityView.getName(); + try { + this.keys = mapper.writeValueAsString(entityView.getKeys()); + } catch (IOException e) { + log.error("Unable to serialize entity view keys!", e); + } + this.startTs = entityView.getStartTimeMs(); + this.endTs = entityView.getEndTimeMs(); + this.searchText = entityView.getSearchText(); + this.additionalInfo = entityView.getAdditionalInfo(); + } + + public AbstractEntityViewEntity(EntityViewEntity entityViewEntity) { + this.setId(entityViewEntity.getId()); + this.entityId = entityViewEntity.getEntityId(); + this.entityType = entityViewEntity.getEntityType(); + this.tenantId = entityViewEntity.getTenantId(); + this.customerId = entityViewEntity.getCustomerId(); + this.type = entityViewEntity.getType(); + this.name = entityViewEntity.getName(); + this.keys = entityViewEntity.getKeys(); + this.startTs = entityViewEntity.getStartTs(); + this.endTs = entityViewEntity.getEndTs(); + this.searchText = entityViewEntity.getSearchText(); + this.additionalInfo = entityViewEntity.getAdditionalInfo(); + } + + @Override + public String getSearchTextSource() { + return name; + } + + @Override + public void setSearchText(String searchText) { + this.searchText = searchText; + } + + protected EntityView toEntityView() { + EntityView entityView = new EntityView(new EntityViewId(getId())); + entityView.setCreatedTime(UUIDs.unixTimestamp(getId())); + + if (entityId != null) { + entityView.setEntityId(EntityIdFactory.getByTypeAndId(entityType.name(), toUUID(entityId).toString())); + } + if (tenantId != null) { + entityView.setTenantId(new TenantId(toUUID(tenantId))); + } + if (customerId != null) { + entityView.setCustomerId(new CustomerId(toUUID(customerId))); + } + entityView.setType(type); + entityView.setName(name); + try { + entityView.setKeys(mapper.readValue(keys, TelemetryEntityView.class)); + } catch (IOException e) { + log.error("Unable to read entity view keys!", e); + } + entityView.setStartTimeMs(startTs); + entityView.setEndTimeMs(endTs); + entityView.setAdditionalInfo(additionalInfo); + return entityView; + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetEntity.java index 043a02b813..920d58ebd2 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetEntity.java @@ -15,106 +15,35 @@ */ package org.thingsboard.server.dao.model.sql; -import com.datastax.driver.core.utils.UUIDs; -import com.fasterxml.jackson.databind.JsonNode; import lombok.Data; import lombok.EqualsAndHashCode; -import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; -import org.thingsboard.server.common.data.UUIDConverter; import org.thingsboard.server.common.data.asset.Asset; -import org.thingsboard.server.common.data.id.AssetId; -import org.thingsboard.server.common.data.id.CustomerId; -import org.thingsboard.server.common.data.id.TenantId; -import org.thingsboard.server.dao.model.BaseSqlEntity; -import org.thingsboard.server.dao.model.ModelConstants; -import org.thingsboard.server.dao.model.SearchTextEntity; import org.thingsboard.server.dao.util.mapping.JsonStringType; -import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import static org.thingsboard.server.dao.model.ModelConstants.ASSET_COLUMN_FAMILY_NAME; -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_CUSTOMER_ID_PROPERTY; -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_NAME_PROPERTY; -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_TENANT_ID_PROPERTY; -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_TYPE_PROPERTY; -import static org.thingsboard.server.dao.model.ModelConstants.SEARCH_TEXT_PROPERTY; @Data @EqualsAndHashCode(callSuper = true) @Entity @TypeDef(name = "json", typeClass = JsonStringType.class) @Table(name = ASSET_COLUMN_FAMILY_NAME) -public final class AssetEntity extends BaseSqlEntity implements SearchTextEntity { - - @Column(name = ASSET_TENANT_ID_PROPERTY) - private String tenantId; - - @Column(name = ASSET_CUSTOMER_ID_PROPERTY) - private String customerId; - - @Column(name = ASSET_NAME_PROPERTY) - private String name; - - @Column(name = ASSET_TYPE_PROPERTY) - private String type; - - @Column(name = SEARCH_TEXT_PROPERTY) - private String searchText; - - @Type(type = "json") - @Column(name = ModelConstants.ASSET_ADDITIONAL_INFO_PROPERTY) - private JsonNode additionalInfo; +public final class AssetEntity extends AbstractAssetEntity { public AssetEntity() { super(); } public AssetEntity(Asset asset) { - if (asset.getId() != null) { - this.setId(asset.getId().getId()); - } - if (asset.getTenantId() != null) { - this.tenantId = UUIDConverter.fromTimeUUID(asset.getTenantId().getId()); - } - if (asset.getCustomerId() != null) { - this.customerId = UUIDConverter.fromTimeUUID(asset.getCustomerId().getId()); - } - this.name = asset.getName(); - this.type = asset.getType(); - this.additionalInfo = asset.getAdditionalInfo(); - } - - @Override - public String getSearchTextSource() { - return name; - } - - @Override - public void setSearchText(String searchText) { - this.searchText = searchText; - } - - public String getSearchText() { - return searchText; + super(asset); } @Override public Asset toData() { - Asset asset = new Asset(new AssetId(UUIDConverter.fromString(id))); - asset.setCreatedTime(UUIDs.unixTimestamp(UUIDConverter.fromString(id))); - if (tenantId != null) { - asset.setTenantId(new TenantId(UUIDConverter.fromString(tenantId))); - } - if (customerId != null) { - asset.setCustomerId(new CustomerId(UUIDConverter.fromString(customerId))); - } - asset.setName(name); - asset.setType(type); - asset.setAdditionalInfo(additionalInfo); - return asset; + return super.toAsset(); } } \ No newline at end of file diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetInfoEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetInfoEntity.java new file mode 100644 index 0000000000..bb292c8cec --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetInfoEntity.java @@ -0,0 +1,58 @@ +/** + * Copyright © 2016-2019 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.dao.model.sql; + +import com.fasterxml.jackson.databind.JsonNode; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.thingsboard.server.common.data.asset.AssetInfo; + +import java.util.HashMap; +import java.util.Map; + +@Data +@EqualsAndHashCode(callSuper = true) +public class AssetInfoEntity extends AbstractAssetEntity { + + public static final Map assetInfoColumnMap = new HashMap<>(); + static { + assetInfoColumnMap.put("customerTitle", "c.title"); + } + + private String customerTitle; + private boolean customerIsPublic; + + public AssetInfoEntity() { + super(); + } + + public AssetInfoEntity(AssetEntity assetEntity, + String customerTitle, + Object customerAdditionalInfo) { + super(assetEntity); + this.customerTitle = customerTitle; + if (customerAdditionalInfo != null && ((JsonNode)customerAdditionalInfo).has("isPublic")) { + this.customerIsPublic = ((JsonNode)customerAdditionalInfo).get("isPublic").asBoolean(); + } else { + this.customerIsPublic = false; + } + } + + @Override + public AssetInfo toData() { + return new AssetInfo(super.toAsset(), customerTitle, customerIsPublic); + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/DeviceEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/DeviceEntity.java index f2b06e091c..7008312125 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/DeviceEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/DeviceEntity.java @@ -15,22 +15,13 @@ */ package org.thingsboard.server.dao.model.sql; -import com.datastax.driver.core.utils.UUIDs; -import com.fasterxml.jackson.databind.JsonNode; import lombok.Data; import lombok.EqualsAndHashCode; -import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.thingsboard.server.common.data.Device; -import org.thingsboard.server.common.data.id.CustomerId; -import org.thingsboard.server.common.data.id.DeviceId; -import org.thingsboard.server.common.data.id.TenantId; -import org.thingsboard.server.dao.model.BaseSqlEntity; import org.thingsboard.server.dao.model.ModelConstants; -import org.thingsboard.server.dao.model.SearchTextEntity; import org.thingsboard.server.dao.util.mapping.JsonStringType; -import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java index 4eddeba6a9..e8138d0ce7 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java @@ -15,34 +15,15 @@ */ package org.thingsboard.server.dao.model.sql; -import com.datastax.driver.core.utils.UUIDs; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data; import lombok.EqualsAndHashCode; -import lombok.extern.slf4j.Slf4j; -import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; -import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityView; -import org.thingsboard.server.common.data.id.CustomerId; -import org.thingsboard.server.common.data.id.EntityIdFactory; -import org.thingsboard.server.common.data.id.EntityViewId; -import org.thingsboard.server.common.data.id.TenantId; -import org.thingsboard.server.common.data.objects.TelemetryEntityView; -import org.thingsboard.server.dao.model.BaseSqlEntity; import org.thingsboard.server.dao.model.ModelConstants; -import org.thingsboard.server.dao.model.SearchTextEntity; import org.thingsboard.server.dao.util.mapping.JsonStringType; -import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.EnumType; -import javax.persistence.Enumerated; import javax.persistence.Table; -import java.io.IOException; - -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_TYPE_PROPERTY; /** * Created by Victor Basanets on 8/30/2017. @@ -53,111 +34,18 @@ import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_TYPE_PROPER @Entity @TypeDef(name = "json", typeClass = JsonStringType.class) @Table(name = ModelConstants.ENTITY_VIEW_TABLE_FAMILY_NAME) -@Slf4j -public class EntityViewEntity extends BaseSqlEntity implements SearchTextEntity { - - @Column(name = ModelConstants.ENTITY_VIEW_ENTITY_ID_PROPERTY) - private String entityId; - - @Enumerated(EnumType.STRING) - @Column(name = ENTITY_TYPE_PROPERTY) - private EntityType entityType; - - @Column(name = ModelConstants.ENTITY_VIEW_TENANT_ID_PROPERTY) - private String tenantId; - - @Column(name = ModelConstants.ENTITY_VIEW_CUSTOMER_ID_PROPERTY) - private String customerId; - - @Column(name = ModelConstants.DEVICE_TYPE_PROPERTY) - private String type; - - @Column(name = ModelConstants.ENTITY_VIEW_NAME_PROPERTY) - private String name; - - @Column(name = ModelConstants.ENTITY_VIEW_KEYS_PROPERTY) - private String keys; - - @Column(name = ModelConstants.ENTITY_VIEW_START_TS_PROPERTY) - private long startTs; - - @Column(name = ModelConstants.ENTITY_VIEW_END_TS_PROPERTY) - private long endTs; - - @Column(name = ModelConstants.SEARCH_TEXT_PROPERTY) - private String searchText; - - @Type(type = "json") - @Column(name = ModelConstants.ENTITY_VIEW_ADDITIONAL_INFO_PROPERTY) - private JsonNode additionalInfo; - - private static final ObjectMapper mapper = new ObjectMapper(); +public class EntityViewEntity extends AbstractEntityViewEntity { public EntityViewEntity() { super(); } public EntityViewEntity(EntityView entityView) { - if (entityView.getId() != null) { - this.setId(entityView.getId().getId()); - } - if (entityView.getEntityId() != null) { - this.entityId = toString(entityView.getEntityId().getId()); - this.entityType = entityView.getEntityId().getEntityType(); - } - if (entityView.getTenantId() != null) { - this.tenantId = toString(entityView.getTenantId().getId()); - } - if (entityView.getCustomerId() != null) { - this.customerId = toString(entityView.getCustomerId().getId()); - } - this.type = entityView.getType(); - this.name = entityView.getName(); - try { - this.keys = mapper.writeValueAsString(entityView.getKeys()); - } catch (IOException e) { - log.error("Unable to serialize entity view keys!", e); - } - this.startTs = entityView.getStartTimeMs(); - this.endTs = entityView.getEndTimeMs(); - this.searchText = entityView.getSearchText(); - this.additionalInfo = entityView.getAdditionalInfo(); - } - - @Override - public String getSearchTextSource() { - return name; - } - - @Override - public void setSearchText(String searchText) { - this.searchText = searchText; + super(entityView); } @Override public EntityView toData() { - EntityView entityView = new EntityView(new EntityViewId(getId())); - entityView.setCreatedTime(UUIDs.unixTimestamp(getId())); - - if (entityId != null) { - entityView.setEntityId(EntityIdFactory.getByTypeAndId(entityType.name(), toUUID(entityId).toString())); - } - if (tenantId != null) { - entityView.setTenantId(new TenantId(toUUID(tenantId))); - } - if (customerId != null) { - entityView.setCustomerId(new CustomerId(toUUID(customerId))); - } - entityView.setType(type); - entityView.setName(name); - try { - entityView.setKeys(mapper.readValue(keys, TelemetryEntityView.class)); - } catch (IOException e) { - log.error("Unable to read entity view keys!", e); - } - entityView.setStartTimeMs(startTs); - entityView.setEndTimeMs(endTs); - entityView.setAdditionalInfo(additionalInfo); - return entityView; + return super.toEntityView(); } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewInfoEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewInfoEntity.java new file mode 100644 index 0000000000..de07405e23 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewInfoEntity.java @@ -0,0 +1,58 @@ +/** + * Copyright © 2016-2019 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.dao.model.sql; + +import com.fasterxml.jackson.databind.JsonNode; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.thingsboard.server.common.data.EntityViewInfo; + +import java.util.HashMap; +import java.util.Map; + +@Data +@EqualsAndHashCode(callSuper = true) +public class EntityViewInfoEntity extends AbstractEntityViewEntity { + + public static final Map entityViewInfoColumnMap = new HashMap<>(); + static { + entityViewInfoColumnMap.put("customerTitle", "c.title"); + } + + private String customerTitle; + private boolean customerIsPublic; + + public EntityViewInfoEntity() { + super(); + } + + public EntityViewInfoEntity(EntityViewEntity entityViewEntity, + String customerTitle, + Object customerAdditionalInfo) { + super(entityViewEntity); + this.customerTitle = customerTitle; + if (customerAdditionalInfo != null && ((JsonNode)customerAdditionalInfo).has("isPublic")) { + this.customerIsPublic = ((JsonNode)customerAdditionalInfo).get("isPublic").asBoolean(); + } else { + this.customerIsPublic = false; + } + } + + @Override + public EntityViewInfo toData() { + return new EntityViewInfo(super.toEntityView(), customerTitle, customerIsPublic); + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/asset/AssetRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/asset/AssetRepository.java index de21137286..d0d7e5deb5 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/asset/AssetRepository.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/asset/AssetRepository.java @@ -22,6 +22,7 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; import org.thingsboard.server.dao.model.sql.AssetEntity; +import org.thingsboard.server.dao.model.sql.AssetInfoEntity; import org.thingsboard.server.dao.util.SqlDao; import java.util.List; @@ -32,12 +33,27 @@ import java.util.List; @SqlDao public interface AssetRepository extends PagingAndSortingRepository { + @Query("SELECT new org.thingsboard.server.dao.model.sql.AssetInfoEntity(a, c.title, c.additionalInfo) " + + "FROM AssetEntity a " + + "LEFT JOIN CustomerEntity c on c.id = a.customerId " + + "WHERE a.id = :assetId") + AssetInfoEntity findAssetInfoById(@Param("assetId") String assetId); + @Query("SELECT a FROM AssetEntity a WHERE a.tenantId = :tenantId " + "AND LOWER(a.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))") Page findByTenantId(@Param("tenantId") String tenantId, @Param("textSearch") String textSearch, Pageable pageable); + @Query("SELECT new org.thingsboard.server.dao.model.sql.AssetInfoEntity(a, c.title, c.additionalInfo) " + + "FROM AssetEntity a " + + "LEFT JOIN CustomerEntity c on c.id = a.customerId " + + "WHERE a.tenantId = :tenantId " + + "AND LOWER(a.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))") + Page findAssetInfosByTenantId(@Param("tenantId") String tenantId, + @Param("textSearch") String textSearch, + Pageable pageable); + @Query("SELECT a FROM AssetEntity a WHERE a.tenantId = :tenantId " + "AND a.customerId = :customerId " + "AND LOWER(a.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))") @@ -46,6 +62,17 @@ public interface AssetRepository extends PagingAndSortingRepository findAssetInfosByTenantIdAndCustomerId(@Param("tenantId") String tenantId, + @Param("customerId") String customerId, + @Param("searchText") String searchText, + Pageable pageable); + List findByTenantIdAndIdIn(String tenantId, List assetIds); List findByTenantIdAndCustomerIdAndIdIn(String tenantId, String customerId, List assetIds); @@ -60,6 +87,18 @@ public interface AssetRepository extends PagingAndSortingRepository findAssetInfosByTenantIdAndType(@Param("tenantId") String tenantId, + @Param("type") String type, + @Param("textSearch") String textSearch, + Pageable pageable); + + @Query("SELECT a FROM AssetEntity a WHERE a.tenantId = :tenantId " + "AND a.customerId = :customerId AND a.type = :type " + "AND LOWER(a.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))") @@ -69,6 +108,19 @@ public interface AssetRepository extends PagingAndSortingRepository findAssetInfosByTenantIdAndCustomerIdAndType(@Param("tenantId") String tenantId, + @Param("customerId") String customerId, + @Param("type") String type, + @Param("textSearch") String textSearch, + Pageable pageable); + @Query("SELECT DISTINCT a.type FROM AssetEntity a WHERE a.tenantId = :tenantId") List findTenantAssetTypes(@Param("tenantId") String tenantId); diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/asset/JpaAssetDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/asset/JpaAssetDao.java index 576df99566..3db672fb74 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/asset/JpaAssetDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/asset/JpaAssetDao.java @@ -23,12 +23,14 @@ import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.asset.AssetInfo; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.asset.AssetDao; import org.thingsboard.server.dao.model.sql.AssetEntity; +import org.thingsboard.server.dao.model.sql.AssetInfoEntity; import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao; import org.thingsboard.server.dao.util.SqlDao; @@ -63,6 +65,11 @@ public class JpaAssetDao extends JpaAbstractSearchTextDao im return assetRepository; } + @Override + public AssetInfo findAssetInfoById(TenantId tenantId, UUID assetId) { + return DaoUtil.getData(assetRepository.findAssetInfoById(fromTimeUUID(assetId))); + } + @Override public PageData findAssetsByTenantId(UUID tenantId, PageLink pageLink) { return DaoUtil.toPageData(assetRepository @@ -72,6 +79,15 @@ public class JpaAssetDao extends JpaAbstractSearchTextDao im DaoUtil.toPageable(pageLink))); } + @Override + public PageData findAssetInfosByTenantId(UUID tenantId, PageLink pageLink) { + return DaoUtil.toPageData( + assetRepository.findAssetInfosByTenantId( + fromTimeUUID(tenantId), + Objects.toString(pageLink.getTextSearch(), ""), + DaoUtil.toPageable(pageLink, AssetInfoEntity.assetInfoColumnMap))); + } + @Override public ListenableFuture> findAssetsByTenantIdAndIdsAsync(UUID tenantId, List assetIds) { return service.submit(() -> @@ -88,6 +104,16 @@ public class JpaAssetDao extends JpaAbstractSearchTextDao im DaoUtil.toPageable(pageLink))); } + @Override + public PageData findAssetInfosByTenantIdAndCustomerId(UUID tenantId, UUID customerId, PageLink pageLink) { + return DaoUtil.toPageData( + assetRepository.findAssetInfosByTenantIdAndCustomerId( + fromTimeUUID(tenantId), + fromTimeUUID(customerId), + Objects.toString(pageLink.getTextSearch(), ""), + DaoUtil.toPageable(pageLink, AssetInfoEntity.assetInfoColumnMap))); + } + @Override public ListenableFuture> findAssetsByTenantIdAndCustomerIdAndIdsAsync(UUID tenantId, UUID customerId, List assetIds) { return service.submit(() -> @@ -110,6 +136,16 @@ public class JpaAssetDao extends JpaAbstractSearchTextDao im DaoUtil.toPageable(pageLink))); } + @Override + public PageData findAssetInfosByTenantIdAndType(UUID tenantId, String type, PageLink pageLink) { + return DaoUtil.toPageData( + assetRepository.findAssetInfosByTenantIdAndType( + fromTimeUUID(tenantId), + type, + Objects.toString(pageLink.getTextSearch(), ""), + DaoUtil.toPageable(pageLink, AssetInfoEntity.assetInfoColumnMap))); + } + @Override public PageData findAssetsByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, PageLink pageLink) { return DaoUtil.toPageData(assetRepository @@ -121,6 +157,17 @@ public class JpaAssetDao extends JpaAbstractSearchTextDao im DaoUtil.toPageable(pageLink))); } + @Override + public PageData findAssetInfosByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, PageLink pageLink) { + return DaoUtil.toPageData( + assetRepository.findAssetInfosByTenantIdAndCustomerIdAndType( + fromTimeUUID(tenantId), + fromTimeUUID(customerId), + type, + Objects.toString(pageLink.getTextSearch(), ""), + DaoUtil.toPageable(pageLink, AssetInfoEntity.assetInfoColumnMap))); + } + @Override public ListenableFuture> findTenantAssetTypesAsync(UUID tenantId) { return service.submit(() -> convertTenantAssetTypesToDto(tenantId, assetRepository.findTenantAssetTypes(fromTimeUUID(tenantId)))); diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/EntityViewRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/EntityViewRepository.java index 7d9d224f2b..e944f9a786 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/EntityViewRepository.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/EntityViewRepository.java @@ -22,6 +22,7 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; import org.thingsboard.server.dao.model.sql.EntityViewEntity; +import org.thingsboard.server.dao.model.sql.EntityViewInfoEntity; import org.thingsboard.server.dao.util.SqlDao; import java.util.List; @@ -32,12 +33,27 @@ import java.util.List; @SqlDao public interface EntityViewRepository extends PagingAndSortingRepository { + @Query("SELECT new org.thingsboard.server.dao.model.sql.EntityViewInfoEntity(e, c.title, c.additionalInfo) " + + "FROM EntityViewEntity e " + + "LEFT JOIN CustomerEntity c on c.id = e.customerId " + + "WHERE e.id = :entityViewId") + EntityViewInfoEntity findEntityViewInfoById(@Param("entityViewId") String entityViewId); + @Query("SELECT e FROM EntityViewEntity e WHERE e.tenantId = :tenantId " + "AND LOWER(e.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))") Page findByTenantId(@Param("tenantId") String tenantId, @Param("textSearch") String textSearch, Pageable pageable); + @Query("SELECT new org.thingsboard.server.dao.model.sql.EntityViewInfoEntity(e, c.title, c.additionalInfo) " + + "FROM EntityViewEntity e " + + "LEFT JOIN CustomerEntity c on c.id = e.customerId " + + "WHERE e.tenantId = :tenantId " + + "AND LOWER(e.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))") + Page findEntityViewInfosByTenantId(@Param("tenantId") String tenantId, + @Param("textSearch") String textSearch, + Pageable pageable); + @Query("SELECT e FROM EntityViewEntity e WHERE e.tenantId = :tenantId " + "AND e.type = :type " + "AND LOWER(e.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))") @@ -46,6 +62,17 @@ public interface EntityViewRepository extends PagingAndSortingRepository findEntityViewInfosByTenantIdAndType(@Param("tenantId") String tenantId, + @Param("type") String type, + @Param("textSearch") String textSearch, + Pageable pageable); + @Query("SELECT e FROM EntityViewEntity e WHERE e.tenantId = :tenantId " + "AND e.customerId = :customerId " + "AND LOWER(e.searchText) LIKE LOWER(CONCAT(:searchText, '%'))") @@ -54,6 +81,17 @@ public interface EntityViewRepository extends PagingAndSortingRepository findEntityViewInfosByTenantIdAndCustomerId(@Param("tenantId") String tenantId, + @Param("customerId") String customerId, + @Param("searchText") String searchText, + Pageable pageable); + @Query("SELECT e FROM EntityViewEntity e WHERE e.tenantId = :tenantId " + "AND e.customerId = :customerId " + "AND e.type = :type " + @@ -64,6 +102,19 @@ public interface EntityViewRepository extends PagingAndSortingRepository findEntityViewInfosByTenantIdAndCustomerIdAndType(@Param("tenantId") String tenantId, + @Param("customerId") String customerId, + @Param("type") String type, + @Param("textSearch") String textSearch, + Pageable pageable); + EntityViewEntity findByTenantIdAndName(String tenantId, String name); List findAllByTenantIdAndEntityId(String tenantId, String entityId); diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java index a668cbda60..8caccfafac 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java @@ -20,16 +20,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Component; -import org.thingsboard.server.common.data.EntitySubtype; -import org.thingsboard.server.common.data.EntityType; -import org.thingsboard.server.common.data.EntityView; -import org.thingsboard.server.common.data.UUIDConverter; +import org.thingsboard.server.common.data.*; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.entityview.EntityViewDao; import org.thingsboard.server.dao.model.sql.EntityViewEntity; +import org.thingsboard.server.dao.model.sql.EntityViewInfoEntity; import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao; import org.thingsboard.server.dao.util.SqlDao; @@ -64,6 +62,11 @@ public class JpaEntityViewDao extends JpaAbstractSearchTextDao findEntityViewsByTenantId(UUID tenantId, PageLink pageLink) { return DaoUtil.toPageData( @@ -73,6 +76,15 @@ public class JpaEntityViewDao extends JpaAbstractSearchTextDao findEntityViewInfosByTenantId(UUID tenantId, PageLink pageLink) { + return DaoUtil.toPageData( + entityViewRepository.findEntityViewInfosByTenantId( + fromTimeUUID(tenantId), + Objects.toString(pageLink.getTextSearch(), ""), + DaoUtil.toPageable(pageLink, EntityViewInfoEntity.entityViewInfoColumnMap))); + } + @Override public PageData findEntityViewsByTenantIdAndType(UUID tenantId, String type, PageLink pageLink) { return DaoUtil.toPageData( @@ -83,6 +95,16 @@ public class JpaEntityViewDao extends JpaAbstractSearchTextDao findEntityViewInfosByTenantIdAndType(UUID tenantId, String type, PageLink pageLink) { + return DaoUtil.toPageData( + entityViewRepository.findEntityViewInfosByTenantIdAndType( + fromTimeUUID(tenantId), + type, + Objects.toString(pageLink.getTextSearch(), ""), + DaoUtil.toPageable(pageLink, EntityViewInfoEntity.entityViewInfoColumnMap))); + } + @Override public Optional findEntityViewByTenantIdAndName(UUID tenantId, String name) { return Optional.ofNullable( @@ -102,6 +124,16 @@ public class JpaEntityViewDao extends JpaAbstractSearchTextDao findEntityViewInfosByTenantIdAndCustomerId(UUID tenantId, UUID customerId, PageLink pageLink) { + return DaoUtil.toPageData( + entityViewRepository.findEntityViewInfosByTenantIdAndCustomerId( + fromTimeUUID(tenantId), + fromTimeUUID(customerId), + Objects.toString(pageLink.getTextSearch(), ""), + DaoUtil.toPageable(pageLink, EntityViewInfoEntity.entityViewInfoColumnMap))); + } + @Override public PageData findEntityViewsByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, PageLink pageLink) { return DaoUtil.toPageData( @@ -114,6 +146,17 @@ public class JpaEntityViewDao extends JpaAbstractSearchTextDao findEntityViewInfosByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, PageLink pageLink) { + return DaoUtil.toPageData( + entityViewRepository.findEntityViewInfosByTenantIdAndCustomerIdAndType( + fromTimeUUID(tenantId), + fromTimeUUID(customerId), + type, + Objects.toString(pageLink.getTextSearch(), ""), + DaoUtil.toPageable(pageLink, EntityViewInfoEntity.entityViewInfoColumnMap))); + } + @Override public ListenableFuture> findEntityViewsByTenantIdAndEntityIdAsync(UUID tenantId, UUID entityId) { return service.submit(() -> DaoUtil.convertDataList( diff --git a/dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java b/dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java index bde567bd7e..6dee664716 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java +++ b/dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java @@ -24,7 +24,7 @@ import java.util.Arrays; @RunWith(ClasspathSuite.class) @ClassnameFilters({ - "org.thingsboard.server.dao.service.*DeviceServiceSqlTest" + "org.thingsboard.server.dao.service.*ServiceSqlTest" }) public class SqlDaoServiceTestSuite { diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetServiceTest.java index 8fdeeb1e11..4082dbeff7 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetServiceTest.java @@ -25,6 +25,7 @@ import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.asset.AssetInfo; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; @@ -252,7 +253,7 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { @Test public void testFindAssetsByTenantIdAndName() { String title1 = "Asset title 1"; - List assetsTitle1 = new ArrayList<>(); + List assetsTitle1 = new ArrayList<>(); for (int i=0;i<143;i++) { Asset asset = new Asset(); asset.setTenantId(tenantId); @@ -261,10 +262,10 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); asset.setName(name); asset.setType("default"); - assetsTitle1.add(assetService.saveAsset(asset)); + assetsTitle1.add(new AssetInfo(assetService.saveAsset(asset), null, false)); } String title2 = "Asset title 2"; - List assetsTitle2 = new ArrayList<>(); + List assetsTitle2 = new ArrayList<>(); for (int i=0;i<175;i++) { Asset asset = new Asset(); asset.setTenantId(tenantId); @@ -273,14 +274,14 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); asset.setName(name); asset.setType("default"); - assetsTitle2.add(assetService.saveAsset(asset)); + assetsTitle2.add(new AssetInfo(assetService.saveAsset(asset), null, false)); } - List loadedAssetsTitle1 = new ArrayList<>(); + List loadedAssetsTitle1 = new ArrayList<>(); PageLink pageLink = new PageLink(15, 0, title1); - PageData pageData = null; + PageData pageData = null; do { - pageData = assetService.findAssetsByTenantId(tenantId, pageLink); + pageData = assetService.findAssetInfosByTenantId(tenantId, pageLink); loadedAssetsTitle1.addAll(pageData.getData()); if (pageData.hasNext()) { pageLink = pageLink.nextPageLink(); @@ -292,10 +293,10 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { Assert.assertEquals(assetsTitle1, loadedAssetsTitle1); - List loadedAssetsTitle2 = new ArrayList<>(); + List loadedAssetsTitle2 = new ArrayList<>(); pageLink = new PageLink(4, 0, title2); do { - pageData = assetService.findAssetsByTenantId(tenantId, pageLink); + pageData = assetService.findAssetInfosByTenantId(tenantId, pageLink); loadedAssetsTitle2.addAll(pageData.getData()); if (pageData.hasNext()) { pageLink = pageLink.nextPageLink(); @@ -312,7 +313,7 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { } pageLink = new PageLink(4, 0, title1); - pageData = assetService.findAssetsByTenantId(tenantId, pageLink); + pageData = assetService.findAssetInfosByTenantId(tenantId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertEquals(0, pageData.getData().size()); @@ -321,7 +322,7 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { } pageLink = new PageLink(4, 0, title2); - pageData = assetService.findAssetsByTenantId(tenantId, pageLink); + pageData = assetService.findAssetInfosByTenantId(tenantId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertEquals(0, pageData.getData().size()); } @@ -419,21 +420,21 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { customer = customerService.saveCustomer(customer); CustomerId customerId = customer.getId(); - List assets = new ArrayList<>(); + List assets = new ArrayList<>(); for (int i=0;i<278;i++) { Asset asset = new Asset(); asset.setTenantId(tenantId); asset.setName("Asset"+i); asset.setType("default"); asset = assetService.saveAsset(asset); - assets.add(assetService.assignAssetToCustomer(tenantId, asset.getId(), customerId)); + assets.add(new AssetInfo(assetService.assignAssetToCustomer(tenantId, asset.getId(), customerId), customer.getTitle(), customer.isPublic())); } - List loadedAssets = new ArrayList<>(); + List loadedAssets = new ArrayList<>(); PageLink pageLink = new PageLink(23); - PageData pageData = null; + PageData pageData = null; do { - pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink); + pageData = assetService.findAssetInfosByTenantIdAndCustomerId(tenantId, customerId, pageLink); loadedAssets.addAll(pageData.getData()); if (pageData.hasNext()) { pageLink = pageLink.nextPageLink(); @@ -448,7 +449,7 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { assetService.unassignCustomerAssets(tenantId, customerId); pageLink = new PageLink(33); - pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink); + pageData = assetService.findAssetInfosByTenantIdAndCustomerId(tenantId, customerId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); diff --git a/dao/src/test/resources/nosql-test.properties b/dao/src/test/resources/nosql-test.properties index 556a024c7d..07d1f3dd3d 100644 --- a/dao/src/test/resources/nosql-test.properties +++ b/dao/src/test/resources/nosql-test.properties @@ -4,6 +4,7 @@ sql.ts_inserts_executor_type=fixed sql.ts_inserts_fixed_thread_pool_size=10 spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true +spring.jpa.properties.hibernate.order_by.default_null_ordering=last spring.jpa.show-sql=false spring.jpa.hibernate.ddl-auto=none spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect diff --git a/dao/src/test/resources/sql-test.properties b/dao/src/test/resources/sql-test.properties index 121f1b9a7a..ae2f217181 100644 --- a/dao/src/test/resources/sql-test.properties +++ b/dao/src/test/resources/sql-test.properties @@ -4,6 +4,7 @@ sql.ts_inserts_executor_type=fixed sql.ts_inserts_fixed_thread_pool_size=10 spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true +spring.jpa.properties.hibernate.order_by.default_null_ordering=last spring.jpa.show-sql=false spring.jpa.hibernate.ddl-auto=validate spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect