fix: changed the data structure used in getAllSectionCells for processing the latest version of sectioneCells as it gave some exception (it doesn't like being iterated and modified at the same time)
This commit is contained in:
@ -25,8 +25,9 @@ import java.nio.file.StandardCopyOption;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class SharedApiService {
|
||||
@ -95,31 +96,36 @@ public class SharedApiService {
|
||||
}
|
||||
|
||||
Project project = this.projectService.getProjectById(projectId);
|
||||
List<SectionCell> allSectionCells = new ArrayList<SectionCell>();
|
||||
project.getListSectionCell()
|
||||
|
||||
Map<Long, SectionCell> latestSectionCellsMap =
|
||||
new HashMap<>(); // List for the intermediate result
|
||||
|
||||
// Iterate through all SectionCells associated with the project
|
||||
// This loop iterates over project.getListSectionCell() but does NOT modify it which causes
|
||||
// ConcurrentModificationException.
|
||||
// Modifications are done only on the latestSectionCellsMap (which is safe).
|
||||
project.getListSectionCell() // <-- Iterating over the original list (read-only)
|
||||
.forEach(
|
||||
projectCell -> {
|
||||
AtomicBoolean sameReferenceId =
|
||||
new AtomicBoolean(false); // side effect lambdas
|
||||
allSectionCells.forEach(
|
||||
selectedCell -> {
|
||||
if (projectCell
|
||||
.getIdReference()
|
||||
.equals(selectedCell.getIdReference())) {
|
||||
sameReferenceId.set(true);
|
||||
if (projectCell
|
||||
.getModificationDate()
|
||||
.isAfter(selectedCell.getModificationDate())) {
|
||||
allSectionCells.remove(selectedCell);
|
||||
allSectionCells.add(projectCell);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (!sameReferenceId.get()) {
|
||||
allSectionCells.add(projectCell);
|
||||
Long idReference = projectCell.getIdReference();
|
||||
// Check if we have already seen a SectionCell with this idReference in
|
||||
// our map
|
||||
if (latestSectionCellsMap.containsKey(idReference)) {
|
||||
SectionCell existingCell = latestSectionCellsMap.get(idReference);
|
||||
// Compare modification dates. If the current cell is newer, replace
|
||||
// the one in the map.
|
||||
if (projectCell
|
||||
.getModificationDate()
|
||||
.isAfter(existingCell.getModificationDate())) {
|
||||
latestSectionCellsMap.put(idReference, projectCell);
|
||||
}
|
||||
} else {
|
||||
// If this is the first time we encounter this idReference, add the
|
||||
// cell to the map.
|
||||
latestSectionCellsMap.put(idReference, projectCell);
|
||||
}
|
||||
});
|
||||
return allSectionCells;
|
||||
return new ArrayList<>(latestSectionCellsMap.values());
|
||||
}
|
||||
|
||||
// TODO: test
|
||||
|
Reference in New Issue
Block a user