refactoring

This commit is contained in:
dashevchenko 2025-10-08 11:20:42 +03:00
parent 2d4b8a85f9
commit cda9cc4782
9 changed files with 36 additions and 36 deletions

View File

@ -78,6 +78,8 @@ import static org.thingsboard.server.controller.ControllerConstants.EDGE_ASSIGN_
import static org.thingsboard.server.controller.ControllerConstants.EDGE_ID_PARAM_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.EDGE_UNASSIGN_ASYNC_FIRST_STEP_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.NAME_CONFLICT_POLICY_DESC;
import static org.thingsboard.server.controller.ControllerConstants.NAME_CONFLICT_SEPARATOR_DESC;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_DATA_PARAMETERS;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_NUMBER_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_SIZE_DESCRIPTION;
@ -140,13 +142,9 @@ public class AssetController extends BaseController {
@RequestMapping(value = "/asset", method = RequestMethod.POST)
@ResponseBody
public Asset saveAsset(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "A JSON value representing the asset.") @RequestBody Asset asset,
@Parameter(description = "Optional value of name conflict policy. Possible values: FAIL or UNIQUIFY. " +
"If omitted, FAIL policy is applied. FAIL policy implies exception will be thrown if an entity with the same name already exists. " +
"UNIQUIFY policy appends a suffix to the entity name, if a name conflict occurs.")
@Parameter(description = NAME_CONFLICT_POLICY_DESC)
@RequestParam(name = "policy", defaultValue = "FAIL") NameConflictPolicy policy,
@Parameter(description = "Optional value of name suffix separator used by UNIQUIFY policy. By default, underscore separator is used. " +
"For example, strategy is UNIQUIFY, separator is '-'; if a name conflict occurs for asset 'Office A', " +
"created asset will have name like 'Office A-7fsh4f'.")
@Parameter(description = NAME_CONFLICT_SEPARATOR_DESC)
@RequestParam(name = "separator", defaultValue = "_") String separator) throws Exception {
asset.setTenantId(getTenantId());
checkEntity(asset.getId(), asset, Resource.ASSET);

View File

@ -1744,4 +1744,12 @@ public class ControllerConstants {
MARKDOWN_CODE_BLOCK_END ;
protected static final String SECURITY_WRITE_CHECK = " Security check is performed to verify that the user has 'WRITE' permission for the entity (entities).";
public static final String NAME_CONFLICT_POLICY_DESC = "Optional value of name conflict policy. Possible values: FAIL or UNIQUIFY. " +
" If omitted, FAIL policy is applied. FAIL policy implies exception will be thrown if an entity with the same name already exists. " +
" UNIQUIFY policy appends a suffix to the entity name, if a name conflict occurs.";
public static final String NAME_CONFLICT_SEPARATOR_DESC = "Optional value of name suffix separator used by UNIQUIFY policy. By default, underscore separator is used. " +
"For example, strategy is UNIQUIFY, separator is '-'; if a name conflict occurs for entity name 'test-name', " +
"created entity will have name like 'test-name-7fsh4f'.";
}

View File

@ -49,6 +49,8 @@ import static org.thingsboard.server.controller.ControllerConstants.CUSTOMER_ID;
import static org.thingsboard.server.controller.ControllerConstants.CUSTOMER_ID_PARAM_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.CUSTOMER_TEXT_SEARCH_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.HOME_DASHBOARD;
import static org.thingsboard.server.controller.ControllerConstants.NAME_CONFLICT_POLICY_DESC;
import static org.thingsboard.server.controller.ControllerConstants.NAME_CONFLICT_SEPARATOR_DESC;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_DATA_PARAMETERS;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_NUMBER_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_SIZE_DESCRIPTION;
@ -131,13 +133,9 @@ public class CustomerController extends BaseController {
@RequestMapping(value = "/customer", method = RequestMethod.POST)
@ResponseBody
public Customer saveCustomer(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "A JSON value representing the customer.") @RequestBody Customer customer,
@Parameter(description = "Optional value of name conflict policy. Possible values: FAIL or UNIQUIFY. " +
"If omitted, FAIL policy is applied. FAIL policy implies exception will be thrown if an entity with the same name already exists. " +
"UNIQUIFY policy appends a suffix to the entity name, if a name conflict occurs.")
@Parameter(description = NAME_CONFLICT_POLICY_DESC)
@RequestParam(name = "policy", defaultValue = "FAIL") NameConflictPolicy policy,
@Parameter(description = "Optional value of name suffix separator used by UNIQUIFY policy. By default, underscore separator is used. " +
"For example, strategy is UNIQUIFY, separator is '-'; if a name conflict occurs for customer name 'Customer A', " +
"created customer will have name like 'Customer A-7fsh4f'.")
@Parameter(description = NAME_CONFLICT_SEPARATOR_DESC)
@RequestParam(name = "separator", defaultValue = "_") String separator) throws Exception {
customer.setTenantId(getTenantId());
checkEntity(customer.getId(), customer, Resource.CUSTOMER);

View File

@ -110,6 +110,8 @@ import static org.thingsboard.server.controller.ControllerConstants.EDGE_ASSIGN_
import static org.thingsboard.server.controller.ControllerConstants.EDGE_ID_PARAM_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.EDGE_UNASSIGN_ASYNC_FIRST_STEP_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.NAME_CONFLICT_POLICY_DESC;
import static org.thingsboard.server.controller.ControllerConstants.NAME_CONFLICT_SEPARATOR_DESC;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_DATA_PARAMETERS;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_NUMBER_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_SIZE_DESCRIPTION;
@ -181,13 +183,9 @@ public class DeviceController extends BaseController {
@Parameter(description = "Optional value of the device credentials to be used during device creation. " +
"If omitted, access token will be auto-generated.")
@RequestParam(name = "accessToken", required = false) String accessToken,
@Parameter(description = "Optional value of name conflict policy. Possible values: FAIL or UNIQUIFY. " +
"If omitted, FAIL policy is applied. FAIL policy implies exception will be thrown if an entity with the same name already exists. " +
"UNIQUIFY policy appends a suffix to the entity name, if a name conflict occurs.")
@Parameter(description = NAME_CONFLICT_POLICY_DESC)
@RequestParam(name = "policy", defaultValue = "FAIL") NameConflictPolicy policy,
@Parameter(description = "Optional value of name suffix separator used by UNIQUIFY policy. By default, underscore separator is used. " +
"For example, strategy is UNIQUIFY, separator is '-'; if a name conflict occurs for device name 'thermostat', " +
"created device will have name like 'thermostat-7fsh4f'.")
@Parameter(description = NAME_CONFLICT_SEPARATOR_DESC)
@RequestParam(name = "separator", defaultValue = "_") String separator) throws Exception {
device.setTenantId(getCurrentUser().getTenantId());
if (device.getId() != null) {

View File

@ -71,6 +71,8 @@ import static org.thingsboard.server.controller.ControllerConstants.ENTITY_VIEW_
import static org.thingsboard.server.controller.ControllerConstants.ENTITY_VIEW_TEXT_SEARCH_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.ENTITY_VIEW_TYPE;
import static org.thingsboard.server.controller.ControllerConstants.MODEL_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.NAME_CONFLICT_POLICY_DESC;
import static org.thingsboard.server.controller.ControllerConstants.NAME_CONFLICT_SEPARATOR_DESC;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_DATA_PARAMETERS;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_NUMBER_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_SIZE_DESCRIPTION;
@ -131,13 +133,9 @@ public class EntityViewController extends BaseController {
public EntityView saveEntityView(
@Parameter(description = "A JSON object representing the entity view.")
@RequestBody EntityView entityView,
@Parameter(description = "Optional value of name conflict policy. Possible values: FAIL or UNIQUIFY. " +
"If omitted, FAIL policy is applied. FAIL policy implies exception will be thrown if an entity with the same name already exists. " +
"UNIQUIFY policy appends a suffix to the entity name, if a name conflict occurs.")
@Parameter(description = NAME_CONFLICT_POLICY_DESC)
@RequestParam(name = "policy", defaultValue = "FAIL") NameConflictPolicy policy,
@Parameter(description = "Optional value of name suffix separator used by UNIQUIFY policy. By default, underscore separator is used. " +
"For example, strategy is UNIQUIFY, separator is '-'; if a name conflict occurs for entity view name 'Device A', " +
"created customer will have name like 'Device A-7fsh4f'.")
@Parameter(description = NAME_CONFLICT_SEPARATOR_DESC)
@RequestParam(name = "separator", defaultValue = "_") String separator) throws Exception {
entityView.setTenantId(getCurrentUser().getTenantId());
EntityView existingEntityView = null;

View File

@ -166,9 +166,6 @@ public class BaseAssetService extends AbstractCachedEntityService<AssetCacheKey,
} else if (asset.getId() != null) {
oldAsset = findAssetById(asset.getTenantId(), asset.getId());
}
if (nameConflictStrategy.policy() == NameConflictPolicy.UNIQUIFY) {
uniquifyEntityName(asset, oldAsset, asset::setName, EntityType.ASSET, nameConflictStrategy);
}
AssetCacheEvictEvent evictEvent = new AssetCacheEvictEvent(asset.getTenantId(), asset.getName(), oldAsset != null ? oldAsset.getName() : null);
Asset savedAsset;
try {
@ -190,6 +187,9 @@ public class BaseAssetService extends AbstractCachedEntityService<AssetCacheKey,
}
}
asset.setType(assetProfile.getName());
if (nameConflictStrategy.policy() == NameConflictPolicy.UNIQUIFY) {
uniquifyEntityName(asset, oldAsset, asset::setName, EntityType.ASSET, nameConflictStrategy);
}
savedAsset = assetDao.saveAndFlush(asset.getTenantId(), asset);
publishEvictEvent(evictEvent);
eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(savedAsset.getTenantId()).entityId(savedAsset.getId())

View File

@ -160,11 +160,11 @@ public class CustomerServiceImpl extends AbstractCachedEntityService<CustomerCac
if (doValidate) {
oldCustomer = customerValidator.validate(customer, Customer::getTenantId);
}
if (nameConflictStrategy.policy() == NameConflictPolicy.UNIQUIFY) {
uniquifyEntityName(customer, oldCustomer, customer::setTitle, EntityType.CUSTOMER, nameConflictStrategy);
}
var evictEvent = new CustomerCacheEvictEvent(customer.getTenantId(), customer.getTitle(), oldCustomer != null ? oldCustomer.getTitle() : null);
try {
if (nameConflictStrategy.policy() == NameConflictPolicy.UNIQUIFY) {
uniquifyEntityName(customer, oldCustomer, customer::setTitle, EntityType.CUSTOMER, nameConflictStrategy);
}
Customer savedCustomer = customerDao.saveAndFlush(customer.getTenantId(), customer);
if (!savedCustomer.isPublic()) {
dashboardService.updateCustomerDashboards(savedCustomer.getTenantId(), savedCustomer.getId());

View File

@ -235,9 +235,6 @@ public class DeviceServiceImpl extends CachedVersionedEntityService<DeviceCacheK
} else if (device.getId() != null) {
oldDevice = findDeviceById(device.getTenantId(), device.getId());
}
if (nameConflictStrategy.policy() == NameConflictPolicy.UNIQUIFY) {
uniquifyEntityName(device, oldDevice, device::setName, EntityType.DEVICE, nameConflictStrategy);
}
DeviceCacheEvictEvent deviceCacheEvictEvent = new DeviceCacheEvictEvent(device.getTenantId(), device.getId(), device.getName(), oldDevice != null ? oldDevice.getName() : null);
try {
DeviceProfile deviceProfile;
@ -259,6 +256,9 @@ public class DeviceServiceImpl extends CachedVersionedEntityService<DeviceCacheK
}
device.setType(deviceProfile.getName());
device.setDeviceData(syncDeviceData(deviceProfile, device.getDeviceData()));
if (nameConflictStrategy.policy() == NameConflictPolicy.UNIQUIFY) {
uniquifyEntityName(device, oldDevice, device::setName, EntityType.DEVICE, nameConflictStrategy);
}
Device savedDevice = deviceDao.saveAndFlush(device.getTenantId(), device);
deviceCacheEvictEvent.setSavedDevice(savedDevice);
publishEvictEvent(deviceCacheEvictEvent);

View File

@ -130,10 +130,10 @@ public class EntityViewServiceImpl extends CachedVersionedEntityService<EntityVi
} else if (entityView.getId() != null) {
old = findEntityViewById(entityView.getTenantId(), entityView.getId(), false);
}
if (nameConflictStrategy.policy() == NameConflictPolicy.UNIQUIFY) {
uniquifyEntityName(entityView, old, entityView::setName, EntityType.ENTITY_VIEW, nameConflictStrategy);
}
try {
if (nameConflictStrategy.policy() == NameConflictPolicy.UNIQUIFY) {
uniquifyEntityName(entityView, old, entityView::setName, EntityType.ENTITY_VIEW, nameConflictStrategy);
}
EntityView saved = entityViewDao.save(entityView.getTenantId(), entityView);
publishEvictEvent(new EntityViewEvictEvent(saved.getTenantId(), saved.getId(), saved.getEntityId(), old != null ? old.getEntityId() : null, saved.getName(), old != null ? old.getName() : null, saved));
eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(saved.getTenantId())