|
8 | 8 |
|
9 | 9 | import jakarta.persistence.EntityNotFoundException;
|
10 | 10 | import lombok.RequiredArgsConstructor;
|
| 11 | +import org.apache.commons.lang3.tuple.Pair; |
11 | 12 | import org.gridsuite.studyconfig.server.dto.*;
|
12 | 13 | import org.gridsuite.studyconfig.server.entities.ColumnEntity;
|
13 | 14 | import org.gridsuite.studyconfig.server.entities.GlobalFilterEntity;
|
@@ -371,16 +372,48 @@ private static void reorderColumns(List<UUID> columnOrder, List<ColumnEntity> co
|
371 | 372 | columns.sort(Comparator.comparingInt(column -> columnOrder.indexOf(column.getUuid())));
|
372 | 373 | }
|
373 | 374 |
|
| 375 | + private String newCandidate(String base, int n) { |
| 376 | + return base + '_' + n; |
| 377 | + } |
| 378 | + |
| 379 | + /** |
| 380 | + * Generates a unique value by appending a numeric suffix if the original value already exists. |
| 381 | + * |
| 382 | + * @param originalValue the original value to make unique |
| 383 | + * @param existingValues set of existing values to avoid conflicts with |
| 384 | + * @return a unique value, either the original or with a numeric suffix |
| 385 | + */ |
| 386 | + private String getUniqueValue(String originalValue, Set<String> existingValues) { |
| 387 | + if (!existingValues.contains(originalValue)) { |
| 388 | + return originalValue; |
| 389 | + } |
| 390 | + |
| 391 | + int i = 1; |
| 392 | + while (existingValues.contains(newCandidate(originalValue, i))) { |
| 393 | + ++i; |
| 394 | + } |
| 395 | + return newCandidate(originalValue, i); |
| 396 | + } |
| 397 | + |
| 398 | + private Pair<String, String> getDuplicateIdAndNameCandidate(SpreadsheetConfigEntity entity, String columnId, String columnName) { |
| 399 | + var existingColumnIds = entity.getColumns().stream().map(ColumnEntity::getId).collect(Collectors.toSet()); |
| 400 | + var existingColumnNames = entity.getColumns().stream().map(ColumnEntity::getName).collect(Collectors.toSet()); |
| 401 | + String newColumnId = getUniqueValue(columnId, existingColumnIds); |
| 402 | + String newColumnName = getUniqueValue(columnName, existingColumnNames); |
| 403 | + |
| 404 | + return Pair.of(newColumnId, newColumnName); |
| 405 | + } |
| 406 | + |
374 | 407 | @Transactional
|
375 | 408 | public void duplicateColumn(UUID id, UUID columnId) {
|
376 | 409 | SpreadsheetConfigEntity entity = findEntityById(id);
|
377 | 410 | ColumnEntity columnEntity = entity.getColumns().stream().filter(col -> col.getUuid().equals(columnId))
|
378 | 411 | .findFirst().orElseThrow(() -> new EntityNotFoundException(COLUMN_NOT_FOUND + columnId));
|
379 | 412 | ColumnEntity columnCopy = columnEntity.toBuilder().build();
|
380 | 413 | columnCopy.setUuid(UUID.randomUUID());
|
381 |
| - columnCopy.setId(columnCopy.getId() + "copy"); |
382 |
| - columnCopy.setName(columnCopy.getName() + "-copy"); |
383 |
| - |
| 414 | + Pair<String, String> idAndName = getDuplicateIdAndNameCandidate(entity, columnCopy.getId(), columnCopy.getName()); |
| 415 | + columnCopy.setId(idAndName.getLeft()); |
| 416 | + columnCopy.setName(idAndName.getRight()); |
384 | 417 | List<ColumnEntity> columns = entity.getColumns();
|
385 | 418 | columns.add(columns.indexOf(columnEntity) + 1, columnCopy);
|
386 | 419 | entity.setColumns(columns);
|
|
0 commit comments