created validateNumberOfEntitiesPerTenant

This commit is contained in:
YevhenBondarenko 2020-11-13 15:32:49 +02:00
parent 4eccf7f916
commit a1d9ce6e45
22 changed files with 68 additions and 80 deletions

View File

@ -0,0 +1,8 @@
package org.thingsboard.server.dao;
import org.thingsboard.server.common.data.id.TenantId;
public interface TenantEntityDao {
Long countByTenantId(TenantId tenantId);
}

View File

@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.dao.Dao;
import org.thingsboard.server.dao.TenantEntityDao;
import java.util.List;
import java.util.Optional;
@ -32,7 +33,7 @@ import java.util.UUID;
* The Interface AssetDao.
*
*/
public interface AssetDao extends Dao<Asset> {
public interface AssetDao extends Dao<Asset>, TenantEntityDao {
/**
* Find asset info by id.
@ -166,6 +167,4 @@ public interface AssetDao extends Dao<Asset> {
*/
ListenableFuture<List<EntitySubtype>> findTenantAssetTypesAsync(UUID tenantId);
Long countAssetsByTenantId(TenantId tenantId);
}

View File

@ -330,12 +330,7 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
DefaultTenantProfileConfiguration profileConfiguration =
(DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
long maxAssets = profileConfiguration.getMaxAssets();
if (maxAssets > 0) {
long currentAssetsCount = assetDao.countAssetsByTenantId(tenantId);
if (currentAssetsCount >= maxAssets) {
throw new DataValidationException("Can't create assets more then " + maxAssets);
}
}
validateNumberOfEntitiesPerTenant(tenantId, assetDao, maxAssets, EntityType.ASSET);
}
@Override

View File

@ -20,6 +20,7 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.dao.Dao;
import org.thingsboard.server.dao.TenantEntityDao;
import java.util.Optional;
import java.util.UUID;
@ -27,7 +28,7 @@ import java.util.UUID;
/**
* The Interface CustomerDao.
*/
public interface CustomerDao extends Dao<Customer> {
public interface CustomerDao extends Dao<Customer>, TenantEntityDao {
/**
* Save or update customer object
@ -55,6 +56,4 @@ public interface CustomerDao extends Dao<Customer> {
*/
Optional<Customer> findCustomersByTenantIdAndTitle(UUID tenantId, String title);
Long countCustomersByTenantId(TenantId tenantId);
}

View File

@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.TenantId;
@ -172,12 +173,8 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom
DefaultTenantProfileConfiguration profileConfiguration =
(DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
long maxCustomers = profileConfiguration.getMaxCustomers();
if (maxCustomers > 0) {
long currentCustomersCount = customerDao.countCustomersByTenantId(tenantId);
if (currentCustomersCount >= maxCustomers) {
throw new DataValidationException("Can't create customers more then " + maxCustomers);
}
}
validateNumberOfEntitiesPerTenant(tenantId, customerDao, maxCustomers, EntityType.CUSTOMER);
customerDao.findCustomersByTenantIdAndTitle(customer.getTenantId().getId(), customer.getTitle()).ifPresent(
c -> {
throw new DataValidationException("Customer with such title already exists!");

View File

@ -18,11 +18,12 @@ package org.thingsboard.server.dao.dashboard;
import org.thingsboard.server.common.data.Dashboard;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.Dao;
import org.thingsboard.server.dao.TenantEntityDao;
/**
* The Interface DashboardDao.
*/
public interface DashboardDao extends Dao<Dashboard> {
public interface DashboardDao extends Dao<Dashboard>, TenantEntityDao {
/**
* Save or update dashboard object
@ -31,6 +32,4 @@ public interface DashboardDao extends Dao<Dashboard> {
* @return saved dashboard object
*/
Dashboard save(TenantId tenantId, Dashboard dashboard);
Long countDashboardsByTenantId(TenantId tenantId);
}

View File

@ -24,6 +24,7 @@ import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Dashboard;
import org.thingsboard.server.common.data.DashboardInfo;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DashboardId;
@ -226,12 +227,7 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb
DefaultTenantProfileConfiguration profileConfiguration =
(DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
long maxDashboards = profileConfiguration.getMaxDashboards();
if (maxDashboards > 0) {
long currentDashboardsCount = dashboardDao.countDashboardsByTenantId(tenantId);
if (currentDashboardsCount >= maxDashboards) {
throw new DataValidationException("Can't create dashboards more then " + maxDashboards);
}
}
validateNumberOfEntitiesPerTenant(tenantId, dashboardDao, maxDashboards, EntityType.DASHBOARD);
}
@Override

View File

@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.dao.Dao;
import org.thingsboard.server.dao.TenantEntityDao;
import java.util.List;
import java.util.Optional;
@ -32,7 +33,7 @@ import java.util.UUID;
* The Interface DeviceDao.
*
*/
public interface DeviceDao extends Dao<Device> {
public interface DeviceDao extends Dao<Device>, TenantEntityDao {
/**
* Find device info by id.
@ -203,8 +204,6 @@ public interface DeviceDao extends Dao<Device> {
*/
ListenableFuture<Device> findDeviceByTenantIdAndIdAsync(TenantId tenantId, UUID id);
Long countDevicesByTenantId(TenantId tenantId);
Long countDevicesByDeviceProfileId(TenantId tenantId, UUID deviceProfileId);
/**

View File

@ -530,12 +530,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
DefaultTenantProfileConfiguration profileConfiguration =
(DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
long maxDevices = profileConfiguration.getMaxDevices();
if (maxDevices > 0) {
long currentDevicesCount = deviceDao.countDevicesByTenantId(tenantId);
if (currentDevicesCount >= maxDevices) {
throw new DataValidationException("Can't create devices more then " + maxDevices);
}
}
validateNumberOfEntitiesPerTenant(tenantId, deviceDao, maxDevices, EntityType.DEVICE);
}
@Override

View File

@ -37,12 +37,11 @@ public abstract class AbstractEntityService {
protected Optional<ConstraintViolationException> extractConstraintViolationException(Exception t) {
if (t instanceof ConstraintViolationException) {
return Optional.of ((ConstraintViolationException) t);
return Optional.of((ConstraintViolationException) t);
} else if (t.getCause() instanceof ConstraintViolationException) {
return Optional.of ((ConstraintViolationException) (t.getCause()));
return Optional.of((ConstraintViolationException) (t.getCause()));
} else {
return Optional.empty();
}
}
}

View File

@ -592,12 +592,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
DefaultTenantProfileConfiguration profileConfiguration =
(DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
long maxRuleChains = profileConfiguration.getMaxRuleChains();
if (maxRuleChains > 0) {
long currentRuleChainsCount = ruleChainDao.countRuleChainsByTenantId(tenantId);
if (currentRuleChainsCount >= maxRuleChains) {
throw new DataValidationException("Can't create rule chains more then " + maxRuleChains);
}
}
validateNumberOfEntitiesPerTenant(tenantId, ruleChainDao, maxRuleChains, EntityType.RULE_CHAIN);
}
@Override

View File

@ -15,18 +15,18 @@
*/
package org.thingsboard.server.dao.rule;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.rule.RuleChain;
import org.thingsboard.server.dao.Dao;
import org.thingsboard.server.dao.TenantEntityDao;
import java.util.UUID;
/**
* Created by igor on 3/12/18.
*/
public interface RuleChainDao extends Dao<RuleChain> {
public interface RuleChainDao extends Dao<RuleChain>, TenantEntityDao {
/**
* Find rule chains by tenantId and page link.
@ -36,6 +36,4 @@ public interface RuleChainDao extends Dao<RuleChain> {
* @return the list of rule chain objects
*/
PageData<RuleChain> findRuleChainsByTenantId(UUID tenantId, PageLink pageLink);
Long countRuleChainsByTenantId(TenantId tenantId);
}

View File

@ -18,7 +18,9 @@ package org.thingsboard.server.dao.service;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.server.common.data.BaseData;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.TenantEntityDao;
import org.thingsboard.server.dao.exception.DataValidationException;
import java.util.HashSet;
@ -79,6 +81,19 @@ public abstract class DataValidator<D extends BaseData<?>> {
return emailMatcher.matches();
}
protected void validateNumberOfEntitiesPerTenant(TenantId tenantId,
TenantEntityDao tenantEntityDao,
long maxEntities,
EntityType entityType) {
if (maxEntities > 0) {
long currentEntitiesCount = tenantEntityDao.countByTenantId(tenantId);
if (currentEntitiesCount >= maxEntities) {
throw new DataValidationException(String.format("Can't create more then %d %ss!",
maxEntities, entityType.name().toLowerCase().replaceAll("_", " ")));
}
}
}
protected static void validateJsonStructure(JsonNode expectedNode, JsonNode actualNode) {
Set<String> expectedFields = new HashSet<>();
Iterator<String> fieldsIterator = expectedNode.fieldNames();

View File

@ -178,8 +178,7 @@ public class JpaAssetDao extends JpaAbstractSearchTextDao<AssetEntity, Asset> im
}
@Override
public Long countAssetsByTenantId(TenantId tenantId) {
public Long countByTenantId(TenantId tenantId) {
return assetRepository.countByTenantId(tenantId.getId());
}
}

View File

@ -65,7 +65,7 @@ public class JpaCustomerDao extends JpaAbstractSearchTextDao<CustomerEntity, Cus
}
@Override
public Long countCustomersByTenantId(TenantId tenantId) {
public Long countByTenantId(TenantId tenantId) {
return customerRepository.countByTenantId(tenantId.getId());
}
}

View File

@ -46,7 +46,7 @@ public class JpaDashboardDao extends JpaAbstractSearchTextDao<DashboardEntity, D
}
@Override
public Long countDashboardsByTenantId(TenantId tenantId) {
public Long countByTenantId(TenantId tenantId) {
return dashboardRepository.countByTenantId(tenantId.getId());
}
}

View File

@ -220,7 +220,7 @@ public class JpaDeviceDao extends JpaAbstractSearchTextDao<DeviceEntity, Device>
}
@Override
public Long countDevicesByTenantId(TenantId tenantId) {
public Long countByTenantId(TenantId tenantId) {
return deviceRepository.countByTenantId(tenantId.getId());
}

View File

@ -58,7 +58,7 @@ public class JpaRuleChainDao extends JpaAbstractSearchTextDao<RuleChainEntity, R
}
@Override
public Long countRuleChainsByTenantId(TenantId tenantId) {
public Long countByTenantId(TenantId tenantId) {
return ruleChainRepository.countByTenantId(tenantId.getId());
}
}

View File

@ -93,7 +93,7 @@ public class JpaUserDao extends JpaAbstractSearchTextDao<UserEntity, User> imple
}
@Override
public Long countUsersByTenantId(TenantId tenantId) {
public Long countByTenantId(TenantId tenantId) {
return userRepository.countByTenantId(tenantId.getId());
}
}

View File

@ -20,10 +20,11 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.dao.Dao;
import org.thingsboard.server.dao.TenantEntityDao;
import java.util.UUID;
public interface UserDao extends Dao<User> {
public interface UserDao extends Dao<User>, TenantEntityDao {
/**
* Save or update user object
@ -68,6 +69,4 @@ public interface UserDao extends Dao<User> {
* @return the list of user entities
*/
PageData<User> findCustomerUsers(UUID tenantId, UUID customerId, PageLink pageLink);
Long countUsersByTenantId(TenantId tenantId);
}

View File

@ -27,6 +27,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.id.CustomerId;
@ -377,12 +378,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
DefaultTenantProfileConfiguration profileConfiguration =
(DefaultTenantProfileConfiguration) tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
long maxUsers = profileConfiguration.getMaxUsers();
if (maxUsers > 0) {
long currentUsersCount = userDao.countUsersByTenantId(tenantId);
if (currentUsersCount >= maxUsers) {
throw new DataValidationException("Can't create users more then " + maxUsers);
}
}
validateNumberOfEntitiesPerTenant(tenantId, userDao, maxUsers, EntityType.USER);
}
}