Search in sources :

Example 16 with Asset

use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.

the class JpaAssetDaoTest method testFindAssetsByTenantIdAndCustomerId.

@Test
public void testFindAssetsByTenantIdAndCustomerId() {
    UUID tenantId1 = UUIDs.timeBased();
    UUID tenantId2 = UUIDs.timeBased();
    UUID customerId1 = UUIDs.timeBased();
    UUID customerId2 = UUIDs.timeBased();
    for (int i = 0; i < 60; i++) {
        UUID assetId = UUIDs.timeBased();
        UUID tenantId = i % 2 == 0 ? tenantId1 : tenantId2;
        UUID customerId = i % 2 == 0 ? customerId1 : customerId2;
        saveAsset(assetId, tenantId, customerId, "ASSET_" + i, "TYPE_1");
    }
    TextPageLink pageLink1 = new TextPageLink(20, "ASSET_");
    List<Asset> assets1 = assetDao.findAssetsByTenantIdAndCustomerId(tenantId1, customerId1, pageLink1);
    assertEquals(20, assets1.size());
    TextPageLink pageLink2 = new TextPageLink(20, "ASSET_", assets1.get(19).getId().getId(), null);
    List<Asset> assets2 = assetDao.findAssetsByTenantIdAndCustomerId(tenantId1, customerId1, pageLink2);
    assertEquals(10, assets2.size());
    TextPageLink pageLink3 = new TextPageLink(20, "ASSET_", assets2.get(9).getId().getId(), null);
    List<Asset> assets3 = assetDao.findAssetsByTenantIdAndCustomerId(tenantId1, customerId1, pageLink3);
    assertEquals(0, assets3.size());
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Asset(org.thingsboard.server.common.data.asset.Asset) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Example 17 with Asset

use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.

the class JpaAssetDaoTest method saveAsset.

private void saveAsset(UUID id, UUID tenantId, UUID customerId, String name, String type) {
    Asset asset = new Asset();
    asset.setId(new AssetId(id));
    asset.setTenantId(new TenantId(tenantId));
    asset.setCustomerId(new CustomerId(customerId));
    asset.setName(name);
    asset.setType(type);
    assetDao.save(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 18 with Asset

use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.

the class JpaAssetDaoTest method testFindAssetsByTenantIdCustomerIdAndIdsAsync.

@Test
public void testFindAssetsByTenantIdCustomerIdAndIdsAsync() throws ExecutionException, InterruptedException {
    UUID tenantId = UUIDs.timeBased();
    UUID customerId1 = UUIDs.timeBased();
    UUID customerId2 = UUIDs.timeBased();
    List<UUID> searchIds = new ArrayList<>();
    for (int i = 0; i < 30; i++) {
        UUID assetId = UUIDs.timeBased();
        UUID customerId = i % 2 == 0 ? customerId1 : customerId2;
        saveAsset(assetId, tenantId, customerId, "ASSET_" + i, "TYPE_1");
        if (i % 3 == 0) {
            searchIds.add(assetId);
        }
    }
    ListenableFuture<List<Asset>> assetsFuture = assetDao.findAssetsByTenantIdAndCustomerIdAndIdsAsync(tenantId, customerId1, searchIds);
    List<Asset> assets = assetsFuture.get();
    assertNotNull(assets);
    assertEquals(5, assets.size());
}
Also used : ArrayList(java.util.ArrayList) Asset(org.thingsboard.server.common.data.asset.Asset) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Example 19 with Asset

use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.

the class AssetController method assignAssetToPublicCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/public/asset/{assetId}", method = RequestMethod.POST)
@ResponseBody
public Asset assignAssetToPublicCustomer(@PathVariable(ASSET_ID) String strAssetId) throws ThingsboardException {
    checkParameter(ASSET_ID, strAssetId);
    try {
        AssetId assetId = new AssetId(toUUID(strAssetId));
        Asset asset = checkAssetId(assetId);
        Customer publicCustomer = customerService.findOrCreatePublicCustomer(asset.getTenantId());
        Asset savedAsset = checkNotNull(assetService.assignAssetToCustomer(assetId, publicCustomer.getId()));
        logEntityAction(assetId, savedAsset, savedAsset.getCustomerId(), ActionType.ASSIGNED_TO_CUSTOMER, null, strAssetId, publicCustomer.getId().toString(), publicCustomer.getName());
        return savedAsset;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ASSET), null, null, ActionType.ASSIGNED_TO_CUSTOMER, e, strAssetId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) 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 20 with Asset

use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.

the class AssetController method saveAsset.

@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/asset", method = RequestMethod.POST)
@ResponseBody
public Asset saveAsset(@RequestBody Asset asset) throws ThingsboardException {
    try {
        asset.setTenantId(getCurrentUser().getTenantId());
        if (getCurrentUser().getAuthority() == Authority.CUSTOMER_USER) {
            if (asset.getId() == null || asset.getId().isNullUid() || asset.getCustomerId() == null || asset.getCustomerId().isNullUid()) {
                throw new ThingsboardException("You don't have permission to perform this operation!", ThingsboardErrorCode.PERMISSION_DENIED);
            } else {
                checkCustomerId(asset.getCustomerId());
            }
        }
        Asset savedAsset = checkNotNull(assetService.saveAsset(asset));
        logEntityAction(savedAsset.getId(), savedAsset, savedAsset.getCustomerId(), asset.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null);
        return savedAsset;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ASSET), asset, null, asset.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e);
        throw handleException(e);
    }
}
Also used : ThingsboardException(org.thingsboard.server.exception.ThingsboardException) Asset(org.thingsboard.server.common.data.asset.Asset) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

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