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());
}
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);
}
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());
}
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);
}
}
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);
}
}
Aggregations