Browse Source

Introduct AssetInfo and EntityViewInfo dtos

pull/1954/head
Igor Kulikov 7 years ago
parent
commit
7ff599f7c4
  1. 64
      application/src/main/java/org/thingsboard/server/controller/AssetController.java
  2. 25
      application/src/main/java/org/thingsboard/server/controller/BaseController.java
  3. 69
      application/src/main/java/org/thingsboard/server/controller/EntityViewController.java
  4. 1
      application/src/main/resources/thingsboard.yml
  5. 43
      application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java
  6. 11
      common/dao-api/src/main/java/org/thingsboard/server/dao/asset/AssetService.java
  7. 11
      common/dao-api/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java
  8. 40
      common/data/src/main/java/org/thingsboard/server/common/data/EntityViewInfo.java
  9. 40
      common/data/src/main/java/org/thingsboard/server/common/data/asset/AssetInfo.java
  10. 50
      dao/src/main/java/org/thingsboard/server/dao/asset/AssetDao.java
  11. 44
      dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java
  12. 2
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceDao.java
  13. 51
      dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java
  14. 53
      dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java
  15. 129
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractAssetEntity.java
  16. 172
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractEntityViewEntity.java
  17. 77
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetEntity.java
  18. 58
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetInfoEntity.java
  19. 9
      dao/src/main/java/org/thingsboard/server/dao/model/sql/DeviceEntity.java
  20. 118
      dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java
  21. 58
      dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewInfoEntity.java
  22. 52
      dao/src/main/java/org/thingsboard/server/dao/sql/asset/AssetRepository.java
  23. 47
      dao/src/main/java/org/thingsboard/server/dao/sql/asset/JpaAssetDao.java
  24. 51
      dao/src/main/java/org/thingsboard/server/dao/sql/entityview/EntityViewRepository.java
  25. 51
      dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java
  26. 2
      dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java
  27. 35
      dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetServiceTest.java
  28. 1
      dao/src/test/resources/nosql-test.properties
  29. 1
      dao/src/test/resources/sql-test.properties

64
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<AssetInfo> 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<AssetInfo> 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

25
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);

69
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<EntityViewInfo> 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<EntityViewInfo> 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

1
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:

43
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<EntityView> views = new ArrayList<>();
List<EntityViewInfo> 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<EntityView> loadedViews = loadListOf(new PageLink(23), urlTemplate);
List<EntityViewInfo> 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<EntityView> views = new ArrayList<>();
List<EntityViewInfo> 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<EntityView> loadedViews = loadListOf(new PageLink(23), "/api/tenant/entityViews?");
List<EntityViewInfo> 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<EntityViewInfo> loadListOfInfo(PageLink pageLink, String urlTemplate) throws Exception {
List<EntityViewInfo> loadedItems = new ArrayList<>();
PageData<EntityViewInfo> pageData;
do {
pageData = doGetTypedWithPageLink(urlTemplate, new TypeReference<PageData<EntityViewInfo>>() {
}, pageLink);
loadedItems.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
return loadedItems;
}
}

11
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<Asset> findAssetByIdAsync(TenantId tenantId, AssetId assetId);
@ -46,16 +49,24 @@ public interface AssetService {
PageData<Asset> findAssetsByTenantId(TenantId tenantId, PageLink pageLink);
PageData<AssetInfo> findAssetInfosByTenantId(TenantId tenantId, PageLink pageLink);
PageData<Asset> findAssetsByTenantIdAndType(TenantId tenantId, String type, PageLink pageLink);
PageData<AssetInfo> findAssetInfosByTenantIdAndType(TenantId tenantId, String type, PageLink pageLink);
ListenableFuture<List<Asset>> findAssetsByTenantIdAndIdsAsync(TenantId tenantId, List<AssetId> assetIds);
void deleteAssetsByTenantId(TenantId tenantId);
PageData<Asset> findAssetsByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink);
PageData<AssetInfo> findAssetInfosByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink);
PageData<Asset> findAssetsByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, PageLink pageLink);
PageData<AssetInfo> findAssetInfosByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, PageLink pageLink);
ListenableFuture<List<Asset>> findAssetsByTenantIdCustomerIdAndIdsAsync(TenantId tenantId, CustomerId customerId, List<AssetId> assetIds);
void unassignCustomerAssets(TenantId tenantId, CustomerId customerId);

11
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<EntityView> findEntityViewByTenantId(TenantId tenantId, PageLink pageLink);
PageData<EntityViewInfo> findEntityViewInfosByTenantId(TenantId tenantId, PageLink pageLink);
PageData<EntityView> findEntityViewByTenantIdAndType(TenantId tenantId, PageLink pageLink, String type);
PageData<EntityViewInfo> findEntityViewInfosByTenantIdAndType(TenantId tenantId, String type, PageLink pageLink);
PageData<EntityView> findEntityViewsByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink);
PageData<EntityViewInfo> findEntityViewInfosByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink);
PageData<EntityView> findEntityViewsByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, PageLink pageLink, String type);
PageData<EntityViewInfo> findEntityViewInfosByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, PageLink pageLink);
ListenableFuture<List<EntityView>> findEntityViewsByQuery(TenantId tenantId, EntityViewSearchQuery query);
ListenableFuture<EntityView> findEntityViewByIdAsync(TenantId tenantId, EntityViewId entityViewId);

40
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;
}
}

40
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;
}
}

50
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<Asset> {
/**
* 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<Asset> {
*/
PageData<Asset> 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<AssetInfo> findAssetInfosByTenantId(UUID tenantId, PageLink pageLink);
/**
* Find assets by tenantId, type and page link.
*
@ -60,6 +79,16 @@ public interface AssetDao extends Dao<Asset> {
*/
PageData<Asset> 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<AssetInfo> findAssetInfosByTenantIdAndType(UUID tenantId, String type, PageLink pageLink);
/**
* Find assets by tenantId and assets Ids.
*
@ -79,6 +108,16 @@ public interface AssetDao extends Dao<Asset> {
*/
PageData<Asset> 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<AssetInfo> 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<Asset> {
*/
PageData<Asset> 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<AssetInfo> findAssetInfosByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, PageLink pageLink);
/**
* Find assets by tenantId, customerId and assets Ids.
*

44
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<AssetInfo> 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<Asset> 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<AssetInfo> 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<List<Asset>> findAssetsByTenantIdAndIdsAsync(TenantId tenantId, List<AssetId> 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<AssetInfo> 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<Asset> 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<AssetInfo> 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<List<Asset>> findAssetsByTenantIdCustomerIdAndIdsAsync(TenantId tenantId, CustomerId customerId, List<AssetId> assetIds) {
log.trace("Executing findAssetsByTenantIdAndCustomerIdAndIdsAsync, tenantId [{}], customerId [{}], assetIds [{}]", tenantId, customerId, assetIds);

2
dao/src/main/java/org/thingsboard/server/dao/device/DeviceDao.java

@ -85,7 +85,7 @@ public interface DeviceDao extends Dao<Device> {
* @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<DeviceInfo> findDeviceInfosByTenantIdAndType(UUID tenantId, String type, PageLink pageLink);

51
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<EntityView> {
/**
* 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<EntityView> {
*/
PageData<EntityView> 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<EntityViewInfo> findEntityViewInfosByTenantId(UUID tenantId, PageLink pageLink);
/**
* Find entity views by tenantId, type and page link.
*
@ -60,6 +78,16 @@ public interface EntityViewDao extends Dao<EntityView> {
*/
PageData<EntityView> 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<EntityViewInfo> 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<EntityView> {
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<EntityViewInfo> 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<EntityView> {
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<EntityViewInfo> findEntityViewInfosByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, PageLink pageLink);
ListenableFuture<List<EntityView>> findEntityViewsByTenantIdAndEntityIdAsync(UUID tenantId, UUID entityId);
/**

53
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<EntityViewInfo> 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<EntityView> 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<EntityViewInfo> 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<EntityView> findEntityViewsByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId,
PageLink pageLink) {
@ -170,6 +190,17 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti
customerId.getId(), pageLink);
}
@Override
public PageData<EntityViewInfo> 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<EntityView> 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<EntityViewInfo> 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<List<EntityView>> findEntityViewsByQuery(TenantId tenantId, EntityViewSearchQuery query) {
ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(tenantId, query.toEntitySearchQuery());

129
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<T extends Asset> extends BaseSqlEntity<T> implements SearchTextEntity<T> {
@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;
}
}

172
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<T extends EntityView> extends BaseSqlEntity<T> implements SearchTextEntity<T> {
@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;
}
}

77
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<Asset> implements SearchTextEntity<Asset> {
@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<Asset> {
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();
}
}

58
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<AssetInfo> {
public static final Map<String,String> 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);
}
}

9
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;

118
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<EntityView> implements SearchTextEntity<EntityView> {
@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<EntityView> {
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();
}
}

58
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<EntityViewInfo> {
public static final Map<String,String> 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);
}
}

52
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<AssetEntity, String> {
@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<AssetEntity> 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<AssetInfoEntity> 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<AssetEntity,
@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 a.customerId = :customerId " +
"AND LOWER(a.searchText) LIKE LOWER(CONCAT(:searchText, '%'))")
Page<AssetInfoEntity> findAssetInfosByTenantIdAndCustomerId(@Param("tenantId") String tenantId,
@Param("customerId") String customerId,
@Param("searchText") String searchText,
Pageable pageable);
List<AssetEntity> findByTenantIdAndIdIn(String tenantId, List<String> assetIds);
List<AssetEntity> findByTenantIdAndCustomerIdAndIdIn(String tenantId, String customerId, List<String> assetIds);
@ -60,6 +87,18 @@ public interface AssetRepository extends PagingAndSortingRepository<AssetEntity,
@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 a.type = :type " +
"AND LOWER(a.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))")
Page<AssetInfoEntity> 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<AssetEntity,
@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 a.customerId = :customerId " +
"AND a.type = :type " +
"AND LOWER(a.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))")
Page<AssetInfoEntity> 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<String> findTenantAssetTypes(@Param("tenantId") String tenantId);

47
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<AssetEntity, Asset> im
return assetRepository;
}
@Override
public AssetInfo findAssetInfoById(TenantId tenantId, UUID assetId) {
return DaoUtil.getData(assetRepository.findAssetInfoById(fromTimeUUID(assetId)));
}
@Override
public PageData<Asset> findAssetsByTenantId(UUID tenantId, PageLink pageLink) {
return DaoUtil.toPageData(assetRepository
@ -72,6 +79,15 @@ public class JpaAssetDao extends JpaAbstractSearchTextDao<AssetEntity, Asset> im
DaoUtil.toPageable(pageLink)));
}
@Override
public PageData<AssetInfo> findAssetInfosByTenantId(UUID tenantId, PageLink pageLink) {
return DaoUtil.toPageData(
assetRepository.findAssetInfosByTenantId(
fromTimeUUID(tenantId),
Objects.toString(pageLink.getTextSearch(), ""),
DaoUtil.toPageable(pageLink, AssetInfoEntity.assetInfoColumnMap)));
}
@Override
public ListenableFuture<List<Asset>> findAssetsByTenantIdAndIdsAsync(UUID tenantId, List<UUID> assetIds) {
return service.submit(() ->
@ -88,6 +104,16 @@ public class JpaAssetDao extends JpaAbstractSearchTextDao<AssetEntity, Asset> im
DaoUtil.toPageable(pageLink)));
}
@Override
public PageData<AssetInfo> 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<List<Asset>> findAssetsByTenantIdAndCustomerIdAndIdsAsync(UUID tenantId, UUID customerId, List<UUID> assetIds) {
return service.submit(() ->
@ -110,6 +136,16 @@ public class JpaAssetDao extends JpaAbstractSearchTextDao<AssetEntity, Asset> im
DaoUtil.toPageable(pageLink)));
}
@Override
public PageData<AssetInfo> 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<Asset> findAssetsByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, PageLink pageLink) {
return DaoUtil.toPageData(assetRepository
@ -121,6 +157,17 @@ public class JpaAssetDao extends JpaAbstractSearchTextDao<AssetEntity, Asset> im
DaoUtil.toPageable(pageLink)));
}
@Override
public PageData<AssetInfo> 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<List<EntitySubtype>> findTenantAssetTypesAsync(UUID tenantId) {
return service.submit(() -> convertTenantAssetTypesToDto(tenantId, assetRepository.findTenantAssetTypes(fromTimeUUID(tenantId))));

51
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<EntityViewEntity, String> {
@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<EntityViewEntity> 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<EntityViewInfoEntity> 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<EntityV
@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 e.type = :type " +
"AND LOWER(e.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))")
Page<EntityViewInfoEntity> 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<EntityV
@Param("searchText") String searchText,
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 e.customerId = :customerId " +
"AND LOWER(e.searchText) LIKE LOWER(CONCAT(:searchText, '%'))")
Page<EntityViewInfoEntity> 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<EntityV
@Param("searchText") String searchText,
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 e.customerId = :customerId " +
"AND e.type = :type " +
"AND LOWER(e.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))")
Page<EntityViewInfoEntity> 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<EntityViewEntity> findAllByTenantIdAndEntityId(String tenantId, String entityId);

51
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<EntityViewEntity,
return entityViewRepository;
}
@Override
public EntityViewInfo findEntityViewInfoById(TenantId tenantId, UUID entityViewId) {
return DaoUtil.getData(entityViewRepository.findEntityViewInfoById(fromTimeUUID(entityViewId)));
}
@Override
public PageData<EntityView> findEntityViewsByTenantId(UUID tenantId, PageLink pageLink) {
return DaoUtil.toPageData(
@ -73,6 +76,15 @@ public class JpaEntityViewDao extends JpaAbstractSearchTextDao<EntityViewEntity,
DaoUtil.toPageable(pageLink)));
}
@Override
public PageData<EntityViewInfo> findEntityViewInfosByTenantId(UUID tenantId, PageLink pageLink) {
return DaoUtil.toPageData(
entityViewRepository.findEntityViewInfosByTenantId(
fromTimeUUID(tenantId),
Objects.toString(pageLink.getTextSearch(), ""),
DaoUtil.toPageable(pageLink, EntityViewInfoEntity.entityViewInfoColumnMap)));
}
@Override
public PageData<EntityView> findEntityViewsByTenantIdAndType(UUID tenantId, String type, PageLink pageLink) {
return DaoUtil.toPageData(
@ -83,6 +95,16 @@ public class JpaEntityViewDao extends JpaAbstractSearchTextDao<EntityViewEntity,
DaoUtil.toPageable(pageLink)));
}
@Override
public PageData<EntityViewInfo> 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<EntityView> findEntityViewByTenantIdAndName(UUID tenantId, String name) {
return Optional.ofNullable(
@ -102,6 +124,16 @@ public class JpaEntityViewDao extends JpaAbstractSearchTextDao<EntityViewEntity,
));
}
@Override
public PageData<EntityViewInfo> 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<EntityView> findEntityViewsByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, PageLink pageLink) {
return DaoUtil.toPageData(
@ -114,6 +146,17 @@ public class JpaEntityViewDao extends JpaAbstractSearchTextDao<EntityViewEntity,
));
}
@Override
public PageData<EntityViewInfo> 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<List<EntityView>> findEntityViewsByTenantIdAndEntityIdAsync(UUID tenantId, UUID entityId) {
return service.submit(() -> DaoUtil.convertDataList(

2
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 {

35
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<Asset> assetsTitle1 = new ArrayList<>();
List<AssetInfo> 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<Asset> assetsTitle2 = new ArrayList<>();
List<AssetInfo> 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<Asset> loadedAssetsTitle1 = new ArrayList<>();
List<AssetInfo> loadedAssetsTitle1 = new ArrayList<>();
PageLink pageLink = new PageLink(15, 0, title1);
PageData<Asset> pageData = null;
PageData<AssetInfo> 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<Asset> loadedAssetsTitle2 = new ArrayList<>();
List<AssetInfo> 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<Asset> assets = new ArrayList<>();
List<AssetInfo> 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<Asset> loadedAssets = new ArrayList<>();
List<AssetInfo> loadedAssets = new ArrayList<>();
PageLink pageLink = new PageLink(23);
PageData<Asset> pageData = null;
PageData<AssetInfo> 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());

1
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

1
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

Loading…
Cancel
Save