deleted redundant constraint check, refactoring

This commit is contained in:
dashevchenko 2025-10-14 15:57:24 +03:00
parent 374b6cf22f
commit 3fa9b877ab
2 changed files with 19 additions and 13 deletions

View File

@ -178,18 +178,25 @@ public abstract class AbstractEntityService {
.filter(e -> (oldEntity == null || !e.getId().equals(oldEntity.getId())))
.map(EntityInfo::getName)
.collect(Collectors.toSet());
if (!existingNames.isEmpty()) {
int idx = 1;
String suffix = (strategy.uniquifyStrategy() == RANDOM) ? StringUtils.randomAlphanumeric(6) : String.valueOf(idx);
while (true) {
String newName = entity.getName() + strategy.separator() + suffix;
if (!existingNames.contains(newName)) {
setName.accept(newName);
break;
}
suffix = (strategy.uniquifyStrategy() == RANDOM) ? StringUtils.randomAlphanumeric(6) : String.valueOf(idx++);
}
if (existingNames.contains(entity.getName())) {
String uniqueName = generateUniqueName(entity.getName(), existingNames, strategy);
setName.accept(uniqueName);
}
}
private String generateUniqueName(String baseName, Set<String> existingNames, NameConflictStrategy strategy) {
String newName;
int index = 1;
String separator = strategy.separator();
boolean isRandom = strategy.uniquifyStrategy() == RANDOM;
do {
String suffix = isRandom ? StringUtils.randomAlphanumeric(6) : String.valueOf(index++);
newName = baseName + separator + suffix;
} while (existingNames.contains(newName));
return newName;
}
}

View File

@ -139,8 +139,7 @@ public class EntityViewServiceImpl extends CachedVersionedEntityService<EntityVi
return saved;
} catch (Exception t) {
checkConstraintViolation(t,
"entity_view_external_id_unq_key", "Entity View with such external id already exists!",
"entity_view_name_unq_key", "Entity View with such name already exists!");
"entity_view_external_id_unq_key", "Entity View with such external id already exists!");
throw t;
}
}