Search in sources :

Example 11 with AssetId

use of org.thingsboard.server.common.data.id.AssetId in project thingsboard by thingsboard.

the class AssetController method unassignAssetFromCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/asset/{assetId}", method = RequestMethod.DELETE)
@ResponseBody
public Asset unassignAssetFromCustomer(@PathVariable(ASSET_ID) String strAssetId) throws ThingsboardException {
    checkParameter(ASSET_ID, strAssetId);
    try {
        AssetId assetId = new AssetId(toUUID(strAssetId));
        Asset asset = checkAssetId(assetId);
        if (asset.getCustomerId() == null || asset.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) {
            throw new IncorrectParameterException("Asset isn't assigned to any customer!");
        }
        Customer customer = checkCustomerId(asset.getCustomerId());
        Asset savedAsset = checkNotNull(assetService.unassignAssetFromCustomer(assetId));
        logEntityAction(assetId, asset, asset.getCustomerId(), ActionType.UNASSIGNED_FROM_CUSTOMER, null, strAssetId, customer.getId().toString(), customer.getName());
        return savedAsset;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ASSET), null, null, ActionType.UNASSIGNED_FROM_CUSTOMER, e, strAssetId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) Asset(org.thingsboard.server.common.data.asset.Asset) AssetId(org.thingsboard.server.common.data.id.AssetId) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 12 with AssetId

use of org.thingsboard.server.common.data.id.AssetId in project thingsboard by thingsboard.

the class AssetEntity method toData.

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

Example 13 with AssetId

use of org.thingsboard.server.common.data.id.AssetId in project thingsboard by thingsboard.

the class AssetEntity method toData.

@Override
public Asset toData() {
    Asset asset = new Asset(new AssetId(id));
    asset.setCreatedTime(UUIDs.unixTimestamp(id));
    if (tenantId != null) {
        asset.setTenantId(new TenantId(tenantId));
    }
    if (customerId != null) {
        asset.setCustomerId(new CustomerId(customerId));
    }
    asset.setName(name);
    asset.setType(type);
    asset.setAdditionalInfo(additionalInfo);
    return asset;
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) AssetId(org.thingsboard.server.common.data.id.AssetId)

Example 14 with AssetId

use of org.thingsboard.server.common.data.id.AssetId 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 15 with AssetId

use of org.thingsboard.server.common.data.id.AssetId in project thingsboard by thingsboard.

the class BaseRelationServiceTest method testFindTo.

@Test
public void testFindTo() throws ExecutionException, InterruptedException {
    AssetId parentA = new AssetId(UUIDs.timeBased());
    AssetId parentB = new AssetId(UUIDs.timeBased());
    AssetId childA = new AssetId(UUIDs.timeBased());
    AssetId childB = new AssetId(UUIDs.timeBased());
    EntityRelation relationA1 = new EntityRelation(parentA, childA, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationA2 = new EntityRelation(parentA, childB, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationB1 = new EntityRelation(parentB, childA, EntityRelation.MANAGES_TYPE);
    EntityRelation relationB2 = new EntityRelation(parentB, childB, EntityRelation.MANAGES_TYPE);
    saveRelation(relationA1);
    saveRelation(relationA2);
    saveRelation(relationB1);
    saveRelation(relationB2);
    // Data propagation to views is async
    Thread.sleep(3000);
    List<EntityRelation> relations = relationService.findByTo(childA, RelationTypeGroup.COMMON);
    Assert.assertEquals(2, relations.size());
    for (EntityRelation relation : relations) {
        Assert.assertEquals(childA, relation.getTo());
        Assert.assertTrue(parentA.equals(relation.getFrom()) || parentB.equals(relation.getFrom()));
    }
    relations = relationService.findByToAndType(childA, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(1, relations.size());
    relations = relationService.findByToAndType(childB, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(1, relations.size());
    relations = relationService.findByToAndType(parentA, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(0, relations.size());
    relations = relationService.findByToAndType(parentB, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(0, relations.size());
    relations = relationService.findByTo(childB, RelationTypeGroup.COMMON);
    Assert.assertEquals(2, relations.size());
    for (EntityRelation relation : relations) {
        Assert.assertEquals(childB, relation.getTo());
        Assert.assertTrue(parentA.equals(relation.getFrom()) || parentB.equals(relation.getFrom()));
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Aggregations

AssetId (org.thingsboard.server.common.data.id.AssetId)21 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)13 Test (org.junit.Test)12 Asset (org.thingsboard.server.common.data.asset.Asset)8 CustomerId (org.thingsboard.server.common.data.id.CustomerId)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 TenantId (org.thingsboard.server.common.data.id.TenantId)5 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)5 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)5 Customer (org.thingsboard.server.common.data.Customer)4 EntityRelationsQuery (org.thingsboard.server.common.data.relation.EntityRelationsQuery)2 EntityTypeFilter (org.thingsboard.server.common.data.relation.EntityTypeFilter)2 RelationsSearchParameters (org.thingsboard.server.common.data.relation.RelationsSearchParameters)2 Function (com.google.common.base.Function)1 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1