Skip to content
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where filled and empty rating stars were hard to distinguish in dark and colored themes. [#17819](https://github.com/JabRef/jabref/issues/17819)
- We fixed the `IfPlural` export formatter treating organizations containing “and” as multiple editors. [#17780](https://github.com/JabRef/jabref/issues/17780)
- We fixed an issue where the DocBook export wrote invalid XML for emoji and some math symbols. [#17747](https://github.com/JabRef/jabref/issues/17747)
- We fixed an issue where assigning a color to a group did not update all affected entries. [#7325](https://github.com/JabRef/jabref/issues/7325)
- We fixed an issue where clicking "Generate" in the AUX file import dialog before parsing caused an exception.
- We fixed an issue where it was hard to distinguish between enabled/disabled status of search buttons in dark mode and added tooltips. [#11569](https://github.com/JabRef/jabref/issues/11569)
- We fixed an issue where entries containing dollar signs, backslashes, or unset fields passed to `substring$` caused citation style preview and export to fail. [#12799](https://github.com/JabRef/jabref/issues/12799)
Expand Down
7 changes: 7 additions & 0 deletions docs/requirements/ai/chatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@ Covers:

- feat~ai.chatting.groups~1

### Group AI chats must retain pending messages when the keyword separator changes
`req~ai.chat.groups.separator-migration-preserves-history~1`

This applies to a group at any depth when its definition is replaced during separator migration.

Needs: impl, utest

### AI group chat dialog title must display library and group names
`req~ai.chat.groups.display-names~1`

Expand Down
7 changes: 7 additions & 0 deletions docs/requirements/groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ See <https://github.com/JabRef/jabref/issues/8824#issuecomment-1169216030>.

Needs: impl, utest

## Group appearance edits must preserve entry assignments without scanning the library
`req~groups.edit.preserve-unchanged-assignments~1`

Changing a group's color, icon, or description leaves entry fields untouched when its assignment rule stays the same.

Needs: impl, utest

<!-- markdownlint-disable-file MD022 -->
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import java.util.stream.Stream;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.scene.control.ButtonType;

import org.jabref.gui.DialogService;
import org.jabref.gui.JabRefGuiStateManager;
import org.jabref.gui.StateManager;
import org.jabref.gui.maintable.BibEntryTableViewModel;
import org.jabref.gui.preferences.GuiPreferences;
import org.jabref.gui.testutils.JavaFxExtension;
import org.jabref.gui.undo.HeadlessGuiUndoManager;
Expand Down Expand Up @@ -271,6 +273,32 @@ void groupingMethodChangeUndoesAndRedoesEntryAssignmentsWithGroupDefinition() {
assertEquals(replacement, databaseContext.getMetaData().getGroups().orElseThrow().getChildren().getFirst().getGroup());
}

@Test
void editingGroupColorRefreshesExistingTableBinding() {
BibEntry entry = new BibEntry().withField(StandardField.KEYWORDS, "Books");
WordKeywordGroup original = new WordKeywordGroup("Books", GroupHierarchyType.INDEPENDENT, StandardField.KEYWORDS, "Books", true, ',', true);
original.setColor("#112233");
AbstractGroup replacement = original.deepCopy();
replacement.setColor("#445566");

JavaFxExtension.invokeAndWait(() -> {
databaseContext.getDatabase().insertEntry(entry);
GroupTreeNode root = groupTree.rootGroupProperty().getValue().getGroupNode();
GroupTreeNode node = root.addSubgroup(original);
groupTree.writeGroupChangesToMetaData();

var matchedGroups = new BibEntryTableViewModel(entry, databaseContext, new SimpleObjectProperty<>()).getMatchedGroups();
assertEquals(Optional.of("#112233"), matchedGroups.getValue().getFirst().getColor());
when(dialogService.showCustomDialogAndWait(any(GroupDialogView.class))).thenReturn(Optional.of(replacement));

try (MockedConstruction<GroupDialogView> _ = mockConstruction(GroupDialogView.class)) {
groupTree.editGroup(groupTree.rootGroupProperty().getValue().findGroupNodeViewModel(node).orElseThrow());
}

assertEquals(Optional.of("#445566"), matchedGroups.getValue().getFirst().getColor());
});
}

private void editGroup(AbstractGroup original, AbstractGroup replacement, ButtonType assignmentChoice) {
JavaFxExtension.invokeAndWait(() -> {
GroupTreeNode root = groupTree.rootGroupProperty().getValue().getGroupNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,12 @@ public synchronized CompletableFuture<ObservableList<ChatMessage>> getForGroupAs
return CompletableFuture.completedFuture(FXCollections.observableArrayList());
}
libraryListeners.computeIfAbsent(databaseContext, LibraryListener::new);
// [impl->req~ai.chat.groups.separator-migration-preserves-history~1]
CachedChat cached = groupChats.asMap().compute(group, (_, existing) -> Optional.ofNullable(existing)
.filter(chat -> !chat.removed)
.orElseGet(() -> createChat(databaseContext, ChatType.WITH_GROUP,
() -> ChatIdentifier.from(databaseContext, group),
() -> databaseContext.getMetaData().getGroups().map(root -> root.containsGroup(group.getGroup())).orElse(false))));
() -> databaseContext.getMetaData().getGroups().filter(root -> root == group.getRoot()).isPresent())));
retainChat(cached);
return cached.loaded.copy();
}
Expand Down
12 changes: 11 additions & 1 deletion jablib/src/main/java/org/jabref/model/groups/GroupTreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ public ObjectProperty<AbstractGroup> getGroupProperty() {
/// @param newGroup the new group (has to be non-null)
public void setGroup(@NonNull AbstractGroup newGroup) {
this.groupProperty.set(newGroup);
notifyAboutDescendantChange(this);
}

/// Associates the specified group with this node while also providing the possibility to modify previous matched entries so that they are now matched by the new group.
/// The group editor publishes the update when it writes the edited tree back to the library metadata.
///
/// @param newGroup the new group (has to be non-null)
/// @param shouldKeepPreviousAssignments specifies whether previous matched entries should be added to the new group
Expand All @@ -75,9 +77,17 @@ public List<FieldChange> setGroup(@NonNull AbstractGroup newGroup,
AbstractGroup oldGroup = getGroup();
groupProperty.set(newGroup);

List<FieldChange> changes = new ArrayList<>();
boolean shouldRemoveFromOldGroup = shouldRemovePreviousAssignments && (oldGroup instanceof GroupEntryChanger);
boolean shouldAddToNewGroup = shouldKeepPreviousAssignments && (newGroup instanceof GroupEntryChanger);
// [impl->req~groups.edit.preserve-unchanged-assignments~1]
if (shouldRemoveFromOldGroup && shouldAddToNewGroup
&& oldGroup instanceof WordKeywordGroup previous && newGroup instanceof WordKeywordGroup replacement
&& previous.getClass() == replacement.getClass()
&& previous.hasSameAssignmentRule(replacement)) {
return List.of();
}

List<FieldChange> changes = new ArrayList<>();
if (shouldAddToNewGroup || shouldRemoveFromOldGroup) {
List<BibEntry> entriesMatchedByOldGroup = entriesInDatabase.stream().filter(oldGroup::isMatch)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ public boolean equals(Object o) {
}
return Objects.equals(getName(), other.getName())
&& Objects.equals(getHierarchicalContext(), other.getHierarchicalContext())
&& Objects.equals(searchField, other.searchField)
&& hasSameAssignmentRule(other);
}

boolean hasSameAssignmentRule(WordKeywordGroup other) {
return Objects.equals(searchField, other.searchField)
&& Objects.equals(searchExpression, other.searchExpression)
&& Objects.equals(caseSensitive, other.caseSensitive)
&& Objects.equals(keywordSeparator, other.keywordSeparator)
Expand Down
3 changes: 3 additions & 0 deletions jablib/src/main/java/org/jabref/model/metadata/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ public void setGroups(@NonNull GroupTreeNode root, MetaDataChangeSource source)
groupsRoot.setValue(root);
root.subscribeToDescendantChanged(_ -> groupsRootBinding.invalidate());
root.subscribeToDescendantChanged(_ -> eventBus.post(new GroupUpdatedEvent(this)));
} else {
// The editor can replace a group definition while keeping the same root.
groupsRootBinding.invalidate();
}
eventBus.post(new GroupUpdatedEvent(this));
postChange(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@

import org.jabref.logic.ai.chatting.repositories.ChatHistoryRepository;
import org.jabref.logic.ai.chatting.util.ChatHistoryUtils;
import org.jabref.logic.formatter.bibtexfields.KeywordSeparatorMigration;
import org.jabref.model.ai.chatting.ChatIdentifier;
import org.jabref.model.ai.chatting.ChatMessage;
import org.jabref.model.ai.chatting.ChatType;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.groups.AllEntriesGroup;
import org.jabref.model.groups.ExplicitGroup;
import org.jabref.model.groups.GroupHierarchyType;
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.model.metadata.MetaData;

Expand Down Expand Up @@ -488,6 +491,31 @@ void closeFlushesGroupChatsToRepository() {
assertEquals("group message", persisted.getFirst().content());
}

@Test
// [utest->req~ai.chat.groups.separator-migration-preserves-history~1]
void nestedGroupChatKeepsPendingMessagesDuringSeparatorMigration() {
cache.close();
cache = new InMemoryChatHistoryCache(fakeRepository, 60_000);
GroupTreeNode root = GroupTreeNode.fromGroup(new AllEntriesGroup("All Entries"));
GroupTreeNode parent = root.addSubgroup(new ExplicitGroup("Projects", GroupHierarchyType.INDEPENDENT, ','));
GroupTreeNode nested = parent.addSubgroup(new ExplicitGroup("Paper A", GroupHierarchyType.INDEPENDENT, ','));
MetaData metaData = databaseContext.getMetaData();
metaData.setGroups(root);

var history = cache.getForGroup(databaseContext, nested);
history.add(ChatMessage.userMessage("before migration"));

KeywordSeparatorMigration.migrateGroupSeparators(metaData, ';');

assertSame(history, cache.getForGroup(databaseContext, nested));
history.add(ChatMessage.userMessage("after migration"));
cache.close();

ChatIdentifier id = new ChatIdentifier(LIBRARY_ID, ChatType.WITH_GROUP, "Paper A");
assertEquals(List.of("before migration", "after migration"),
fakeRepository.getAllMessages(id).stream().map(ChatMessage::content).toList());
}

@Test
void deletedGroupIsNotPersistedByDelayedSave() {
cache.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

import org.jabref.model.FieldChange;
import org.jabref.model.entry.BibEntry;
Expand All @@ -19,6 +20,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verifyNoInteractions;

public class GroupTreeNodeTest {

Expand Down Expand Up @@ -207,6 +210,38 @@ void setGroupChangesUnderlyingGroup() {
assertEquals(newGroup, node.getGroup());
}

@Test
void setGroupNotifiesParentAboutDescendantChange() {
GroupTreeNode root = GroupTreeNode.fromGroup(
new ExplicitGroup("Root", GroupHierarchyType.INDEPENDENT, ','));
GroupTreeNode child = root.addSubgroup(
new ExplicitGroup("OldGroup", GroupHierarchyType.INDEPENDENT, ','));

AtomicInteger descendantChanges = new AtomicInteger();
root.subscribeToDescendantChanged(_ -> descendantChanges.incrementAndGet());

child.setGroup(
new ExplicitGroup("NewGroup", GroupHierarchyType.INDEPENDENT, ','));
assertEquals(1, descendantChanges.get());
}

@Test
// [utest->req~groups.edit.preserve-unchanged-assignments~1]
void setGroupChangesColorAndIconWithoutScanningEntries() {
ExplicitGroup original = new ExplicitGroup("Books", GroupHierarchyType.INDEPENDENT, ',');
GroupTreeNode node = GroupTreeNode.fromGroup(original);
AbstractGroup replacement = original.deepCopy();
replacement.setColor("#445566");
replacement.setIconName("book");
BibEntry member = new BibEntry().withField(StandardField.GROUPS, "Books, Other");
List<BibEntry> libraryEntries = spy(new ArrayList<>(List.of(member)));

assertEquals(List.of(), node.setGroup(replacement, true, true, libraryEntries));
assertEquals(replacement, node.getGroup());
assertEquals(Optional.of("Books, Other"), member.getField(StandardField.GROUPS));
verifyNoInteractions(libraryEntries);
}

@Test
void setGroupAddsPreviousAssignmentsExplicitToExplicit() {
ExplicitGroup oldGroup = new ExplicitGroup("OldGroup", GroupHierarchyType.INDEPENDENT, ',');
Expand Down
45 changes: 45 additions & 0 deletions jablib/src/test/java/org/jabref/model/metadata/MetaDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -70,6 +71,50 @@ public void listen(GroupUpdatedEvent event) {
assertEquals(1, events.size(), "one edit posted an event per write-back");
}

@Test
void editorPathColorChangeEmitsExactlyOneGroupUpdatedEvent() {
List<GroupUpdatedEvent> events = new ArrayList<>();
metaData.registerListener(new Object() {
@Subscribe
public void listen(GroupUpdatedEvent event) {
events.add(event);
}
});
GroupTreeNode root = GroupTreeNode.fromGroup(new ExplicitGroup("All", GroupHierarchyType.INDEPENDENT, ','));
ExplicitGroup original = new ExplicitGroup("Books", GroupHierarchyType.INDEPENDENT, ',');
GroupTreeNode child = root.addSubgroup(original);
metaData.setGroups(root);
events.clear();

ExplicitGroup coloured = new ExplicitGroup("Books", GroupHierarchyType.INDEPENDENT, ',');
coloured.setColor("#ff0000");
child.setGroup(coloured, true, true, List.of());
metaData.setGroups(root, MetaDataChangeSource.JOURNAL);

assertEquals(1, events.size(), "colour-only edit via the editor path posted more than one event");
}

@Test
void writingBackSortedGroupsEmitsGroupUpdatedEvent() {
GroupTreeNode root = GroupTreeNode.fromGroup(new ExplicitGroup("All", GroupHierarchyType.INDEPENDENT, ','));
root.addSubgroup(new ExplicitGroup("Books", GroupHierarchyType.INDEPENDENT, ','));
root.addSubgroup(new ExplicitGroup("Articles", GroupHierarchyType.INDEPENDENT, ','));
metaData.setGroups(root);
root.sortChildren(Comparator.comparing(GroupTreeNode::getName), false);

List<GroupUpdatedEvent> events = new ArrayList<>();
metaData.registerListener(new Object() {
@Subscribe
public void listen(GroupUpdatedEvent event) {
events.add(event);
}
});

metaData.setGroups(root, MetaDataChangeSource.JOURNAL);

assertEquals(1, events.size());
}

/// Group operations mutate nodes in place, so a shared tree would let a later edit rewrite what
/// a recorded change is supposed to restore.
@Test
Expand Down
Loading