-
Notifications
You must be signed in to change notification settings - Fork 0
merge some modifications into a new composite #810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0a24e14
166a8bc
cf68049
5e7311f
72a768a
49a522a
028a31c
db56d65
63f60b8
261f807
b9b7e05
97b0370
790ce10
59a9005
53ea896
4f216da
abf3768
3771f0b
daabea4
9e03562
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -909,6 +909,76 @@ public List<ModificationInfos> insertCompositeModifications( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return newEntities.stream().map(ModificationEntity::toModificationInfos).toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public CompositeModificationEntity assembleNetworkModificationsIntoNewComposite(List<UUID> assembledModificationsUuids) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // get the target (groupUuid or composite Uuid of the first assembled modification + its index in this target) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final UUID firstModifUuid = assembledModificationsUuids.getFirst(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final ModificationEntity firstModificationEntity = getModificationEntity(firstModifUuid); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final int targetIndex = firstModificationEntity.getModificationsOrder(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ModificationGroupEntity targetGroup = firstModificationEntity.getGroup(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CompositeModificationEntity targetComposite = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (targetGroup == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // the first modification is inside a composite | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UUID targetCompositeUuid = modificationRepository.findCompositeIdByContainedModificationId(firstModifUuid); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| targetComposite = compositeModificationRepository.findById(targetCompositeUuid).orElse(null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion : |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // get all the modifications to be assembled, remove previous assignment | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<ModificationEntity> assembledModifications = assembledModificationsUuids.stream() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map(modificationRepository::findById).filter(Optional::isPresent).map(Optional::get).toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 1. cleans and reorders the origin group if there is one : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+915
to
+929
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate input list and fail on missing UUIDs instead of partial assembly. Current flow throws on empty list ( Suggested validation guard+ if (assembledModificationsUuids == null || assembledModificationsUuids.isEmpty()) {
+ throw new NetworkModificationException(MODIFICATION_ERROR, "No modifications provided for assembly");
+ }
...
- List<ModificationEntity> assembledModifications = assembledModificationsUuids.stream()
- .map(modificationRepository::findById).filter(Optional::isPresent).map(Optional::get).toList();
+ List<ModificationEntity> assembledModifications = assembledModificationsUuids.stream()
+ .map(uuid -> modificationRepository.findById(uuid)
+ .orElseThrow(() -> new NetworkModificationException(MODIFICATION_NOT_FOUND,
+ String.format(MODIFICATION_NOT_FOUND_MESSAGE, uuid))))
+ .toList();📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ModificationGroupEntity originGroup = assembledModifications.stream() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map(ModificationEntity::getGroup) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter(Objects::nonNull) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .findFirst() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .orElse(null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (originGroup != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<ModificationEntity> originGroupModifications = originGroup.getModifications(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| originGroupModifications.removeIf(mod -> assembledModificationsUuids.contains(mod.getId())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| originGroup.setModifications(originGroupModifications); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assembledModifications.forEach(modificationEntity -> modificationEntity.setGroup(null)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 2. cleans the composites whose submodifications are assembled into a new one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (ModificationEntity assembledModification : assembledModifications.stream().filter(mod -> mod.getGroup() == null).toList()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UUID compositeUuid = modificationRepository.findCompositeIdByContainedModificationId(assembledModification.getId()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (compositeUuid != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CompositeModificationEntity previousOwner = compositeModificationRepository.findById(compositeUuid).orElse(null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (previousOwner != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<ModificationEntity> modificationsLeft = previousOwner.getModifications() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .stream() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter(mod -> !assembledModificationsUuids.contains(mod.getId())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| previousOwner.setModifications(modificationsLeft); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // create the new composite | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CompositeModificationInfos newCompositeInfos = CompositeModificationInfos.builder() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .modificationsInfos(List.of()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .name("Composite modification") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .build(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CompositeModificationEntity newCompositeEntity = (CompositeModificationEntity) ModificationEntity.fromDTO(newCompositeInfos); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newCompositeEntity.setModificationsOrder(targetIndex); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // assign modifications | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newCompositeEntity.setModifications(assembledModifications); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // put the new composite in the target group or composite | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (targetGroup != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<ModificationEntity> modifications = targetGroup.getModifications(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| modifications.add(targetIndex, newCompositeEntity); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| targetGroup.setModifications(modifications); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (targetComposite != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<ModificationEntity> modifications = targetComposite.getModifications(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| modifications.add(targetIndex, newCompositeEntity); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < targetComposite.getModifications().size(); i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+917
to
+974
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recompute insertion index after removals to avoid
Suggested fix pattern- final int targetIndex = firstModificationEntity.getModificationsOrder();
+ final int originalTargetIndex = firstModificationEntity.getModificationsOrder();
...
- modifications.add(targetIndex, newCompositeEntity);
+ long removedBefore = assembledModifications.stream()
+ .filter(m -> m.getGroup() != null && m.getGroup().getId().equals(targetGroup.getId()))
+ .filter(m -> m.getModificationsOrder() < originalTargetIndex)
+ .count();
+ int insertionIndex = Math.max(0, originalTargetIndex - (int) removedBefore);
+ modifications.add(Math.min(insertionIndex, modifications.size()), newCompositeEntity);Apply equivalent logic in the 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| targetComposite.getModifications().get(i).setModificationsOrder(i); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return modificationRepository.save(newCompositeEntity); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void moveSubModification(@NonNull UUID groupUuid, UUID sourceCompositeUuid, UUID targetCompositeUuid, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @NonNull UUID modificationUuid, UUID beforeUuid) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -959,10 +1029,6 @@ public void moveSubModification(@NonNull UUID groupUuid, UUID sourceCompositeUui | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group.setModifications(notMovedMods); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| movedMods.forEach(entity -> entity.setGroup(null)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // reordering the composite modifications left (not moved) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < notMovedMods.size(); i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| notMovedMods.get(i).setModificationsOrder(i); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (targetCompositeUuid != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check if targeted composite isn't already inside modificationUuid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle ambiguous ownership explicitly in
findCompositeIdByContainedModificationId.This query can return multiple
sm.idvalues for onemodification_id, but the method returns a singleUUID. That creates a runtime failure path and hides data-integrity issues.Suggested change
Then validate in caller:
0result: no owner1result: expected>1result: throw a domain error (MODIFICATION_ERROR) with clear message🤖 Prompt for AI Agents