deleted redundant constraint check, refactoring
This commit is contained in:
parent
374b6cf22f
commit
3fa9b877ab
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user