use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.
the class BaseAssetService method findAssetsByQuery.
@Override
public ListenableFuture<List<Asset>> findAssetsByQuery(AssetSearchQuery query) {
ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(query.toEntitySearchQuery());
ListenableFuture<List<Asset>> assets = Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<Asset>>) relations1 -> {
EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
List<ListenableFuture<Asset>> futures = new ArrayList<>();
for (EntityRelation relation : relations1) {
EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
if (entityId.getEntityType() == EntityType.ASSET) {
futures.add(findAssetByIdAsync(new AssetId(entityId.getId())));
}
}
return Futures.successfulAsList(futures);
});
assets = Futures.transform(assets, (Function<List<Asset>, List<Asset>>) assetList -> assetList == null ? Collections.emptyList() : assetList.stream().filter(asset -> query.getAssetTypes().contains(asset.getType())).collect(Collectors.toList()));
return assets;
}
use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.
the class BaseAssetService method unassignAssetFromCustomer.
@Override
public Asset unassignAssetFromCustomer(AssetId assetId) {
Asset asset = findAssetById(assetId);
asset.setCustomerId(null);
return saveAsset(asset);
}
use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.
the class CassandraAssetDao method save.
@Override
public Asset save(Asset domain) {
Asset savedAsset = super.save(domain);
EntitySubtype entitySubtype = new EntitySubtype(savedAsset.getTenantId(), EntityType.ASSET, savedAsset.getType());
EntitySubtypeEntity entitySubtypeEntity = new EntitySubtypeEntity(entitySubtype);
Statement saveStatement = cluster.getMapper(EntitySubtypeEntity.class).saveQuery(entitySubtypeEntity);
executeWrite(saveStatement);
return savedAsset;
}
use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.
the class BaseAssetServiceTest method testFindAssetsByTenantId.
@Test
public void testFindAssetsByTenantId() {
Tenant tenant = new Tenant();
tenant.setTitle("Test tenant");
tenant = tenantService.saveTenant(tenant);
TenantId tenantId = tenant.getId();
List<Asset> assets = new ArrayList<>();
for (int i = 0; i < 178; i++) {
Asset asset = new Asset();
asset.setTenantId(tenantId);
asset.setName("Asset" + i);
asset.setType("default");
assets.add(assetService.saveAsset(asset));
}
List<Asset> loadedAssets = new ArrayList<>();
TextPageLink pageLink = new TextPageLink(23);
TextPageData<Asset> pageData = null;
do {
pageData = assetService.findAssetsByTenantId(tenantId, pageLink);
loadedAssets.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageData.getNextPageLink();
}
} while (pageData.hasNext());
Collections.sort(assets, idComparator);
Collections.sort(loadedAssets, idComparator);
Assert.assertEquals(assets, loadedAssets);
assetService.deleteAssetsByTenantId(tenantId);
pageLink = new TextPageLink(33);
pageData = assetService.findAssetsByTenantId(tenantId, pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertTrue(pageData.getData().isEmpty());
tenantService.deleteTenant(tenantId);
}
use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.
the class BaseAssetServiceTest method testFindAssetsByTenantIdCustomerIdAndName.
@Test
public void testFindAssetsByTenantIdCustomerIdAndName() {
Customer customer = new Customer();
customer.setTitle("Test customer");
customer.setTenantId(tenantId);
customer = customerService.saveCustomer(customer);
CustomerId customerId = customer.getId();
String title1 = "Asset title 1";
List<Asset> assetsTitle1 = new ArrayList<>();
for (int i = 0; i < 175; i++) {
Asset asset = new Asset();
asset.setTenantId(tenantId);
String suffix = RandomStringUtils.randomAlphanumeric(15);
String name = title1 + suffix;
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
asset.setName(name);
asset.setType("default");
asset = assetService.saveAsset(asset);
assetsTitle1.add(assetService.assignAssetToCustomer(asset.getId(), customerId));
}
String title2 = "Asset title 2";
List<Asset> assetsTitle2 = new ArrayList<>();
for (int i = 0; i < 143; i++) {
Asset asset = new Asset();
asset.setTenantId(tenantId);
String suffix = RandomStringUtils.randomAlphanumeric(15);
String name = title2 + suffix;
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
asset.setName(name);
asset.setType("default");
asset = assetService.saveAsset(asset);
assetsTitle2.add(assetService.assignAssetToCustomer(asset.getId(), customerId));
}
List<Asset> loadedAssetsTitle1 = new ArrayList<>();
TextPageLink pageLink = new TextPageLink(15, title1);
TextPageData<Asset> pageData = null;
do {
pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink);
loadedAssetsTitle1.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageData.getNextPageLink();
}
} while (pageData.hasNext());
Collections.sort(assetsTitle1, idComparator);
Collections.sort(loadedAssetsTitle1, idComparator);
Assert.assertEquals(assetsTitle1, loadedAssetsTitle1);
List<Asset> loadedAssetsTitle2 = new ArrayList<>();
pageLink = new TextPageLink(4, title2);
do {
pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink);
loadedAssetsTitle2.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageData.getNextPageLink();
}
} while (pageData.hasNext());
Collections.sort(assetsTitle2, idComparator);
Collections.sort(loadedAssetsTitle2, idComparator);
Assert.assertEquals(assetsTitle2, loadedAssetsTitle2);
for (Asset asset : loadedAssetsTitle1) {
assetService.deleteAsset(asset.getId());
}
pageLink = new TextPageLink(4, title1);
pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(0, pageData.getData().size());
for (Asset asset : loadedAssetsTitle2) {
assetService.deleteAsset(asset.getId());
}
pageLink = new TextPageLink(4, title2);
pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(0, pageData.getData().size());
customerService.deleteCustomer(customerId);
}
Aggregations