Search in sources :

Example 26 with Asset

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;
}
Also used : java.util(java.util) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Customer(org.thingsboard.server.common.data.Customer) AssetId(org.thingsboard.server.common.data.id.AssetId) Autowired(org.springframework.beans.factory.annotation.Autowired) Tenant(org.thingsboard.server.common.data.Tenant) TextPageData(org.thingsboard.server.common.data.page.TextPageData) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) Function(com.google.common.base.Function) DaoUtil.toUUIDs(org.thingsboard.server.dao.DaoUtil.toUUIDs) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) Validator(org.thingsboard.server.dao.service.Validator) Collectors(java.util.stream.Collectors) AssetSearchQuery(org.thingsboard.server.common.data.asset.AssetSearchQuery) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) CustomerDao(org.thingsboard.server.dao.customer.CustomerDao) PaginatedRemover(org.thingsboard.server.dao.service.PaginatedRemover) TenantDao(org.thingsboard.server.dao.tenant.TenantDao) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) StringUtils(org.springframework.util.StringUtils) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Function(com.google.common.base.Function) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Asset(org.thingsboard.server.common.data.asset.Asset) AssetId(org.thingsboard.server.common.data.id.AssetId)

Example 27 with Asset

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);
}
Also used : Asset(org.thingsboard.server.common.data.asset.Asset)

Example 28 with 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;
}
Also used : EntitySubtypeEntity(org.thingsboard.server.dao.model.EntitySubtypeEntity) Statement(com.datastax.driver.core.Statement) Asset(org.thingsboard.server.common.data.asset.Asset) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype)

Example 29 with Asset

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);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) ArrayList(java.util.ArrayList) Asset(org.thingsboard.server.common.data.asset.Asset) Test(org.junit.Test)

Example 30 with Asset

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);
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Aggregations

Asset (org.thingsboard.server.common.data.asset.Asset)49 Test (org.junit.Test)35 ArrayList (java.util.ArrayList)16 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)15 Customer (org.thingsboard.server.common.data.Customer)13 CustomerId (org.thingsboard.server.common.data.id.CustomerId)12 AssetId (org.thingsboard.server.common.data.id.AssetId)8 TenantId (org.thingsboard.server.common.data.id.TenantId)7 TypeReference (com.fasterxml.jackson.core.type.TypeReference)6 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)6 UUID (java.util.UUID)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 Tenant (org.thingsboard.server.common.data.Tenant)5 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)5 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 EntitySubtype (org.thingsboard.server.common.data.EntitySubtype)4 List (java.util.List)3 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)2 Statement (com.datastax.driver.core.Statement)1