Skip to content

Commit

Permalink
Fix for InAnYan#113
Browse files Browse the repository at this point in the history
  • Loading branch information
InAnYan committed Aug 7, 2024
1 parent 32c4943 commit 1f3d20a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import com.airhacks.afterburner.injection.Injector;

public class ListenForCitationKeyChangeForChatHistoryAction implements GUIPostOpenAction {
public class ListenForCitationKeyChangeForAiAction implements GUIPostOpenAction {
private final AiService aiService = Injector.instantiateModelOrService(AiService.class);

@Override
Expand All @@ -17,7 +17,7 @@ public boolean isActionNecessary(ParserResult pr, PreferencesService preferences

@Override
public void performAction(ParserResult pr, DialogService dialogService, PreferencesService preferencesService) {
// pr.getDatabase().getEntries().forEach(entry -> entry.registerListener(aiService.getChatHistoryManager()));
pr.getDatabase().registerListener(aiService.getChatHistoryManager());
pr.getDatabase().registerListener(aiService.getSummariesStorage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class OpenDatabaseAction extends SimpleCommand {
// Check for new custom entry types loaded from the BIB file:
new CheckForNewEntryTypesAction(),
// AI chat history links BibEntry with citation key. When citation key is changed, chat history should be transferred from old citation key to new citation key
new ListenForCitationKeyChangeForChatHistoryAction());
new ListenForCitationKeyChangeForAiAction());

private final LibraryTabContainer tabContainer;
private final PreferencesService preferencesService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@
import java.nio.file.Path;
import java.util.Optional;

import org.jabref.gui.StateManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.event.FieldChangedEvent;
import org.jabref.model.entry.field.InternalField;

import com.airhacks.afterburner.injection.Injector;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import jakarta.inject.Inject;
import org.h2.mvstore.MVMap;
import org.h2.mvstore.MVStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SummariesStorage {
private final static Logger LOGGER = LoggerFactory.getLogger(SummariesStorage.class);

private final MVStore mvStore;

private final EventBus eventBus = new EventBus();

@Inject private StateManager stateManager = Injector.instantiateModelOrService(StateManager.class);

public SummariesStorage(MVStore mvStore) {
this.mvStore = mvStore;
}
Expand All @@ -39,4 +52,37 @@ public Optional<String> get(Path bibDatabasePath, String citationKey) {
public void clear(Path bibDatabasePath, String citationKey) {
getMap(bibDatabasePath).remove(citationKey);
}

@Subscribe
private void fieldChangedEventListener(FieldChangedEvent event) {
// TODO: This methods doesn't take into account if the new citation key is valid.

if (event.getField() != InternalField.KEY_FIELD) {
return;
}

Optional<BibDatabaseContext> bibDatabaseContext = stateManager.getOpenDatabases().stream().filter(dbContext -> dbContext.getDatabase().getEntries().contains(event.getBibEntry())).findFirst();

if (bibDatabaseContext.isEmpty()) {
LOGGER.error("Could not listen to field change event because no database context was found. BibEntry: {}", event.getBibEntry());
return;
}

Optional<Path> bibDatabasePath = bibDatabaseContext.get().getDatabasePath();

if (bibDatabasePath.isEmpty()) {
LOGGER.error("Could not listen to field change event because no database path was found. BibEntry: {}", event.getBibEntry());
return;
}

Optional<String> oldSummary = get(bibDatabasePath.get(), event.getOldValue());

if (oldSummary.isEmpty()) {
LOGGER.info("Old summary not found for {}", event.getNewValue());
return;
}

set(bibDatabasePath.get(), event.getNewValue(), oldSummary.get());
clear(bibDatabasePath.get(), event.getOldValue());
}
}

0 comments on commit 1f3d20a

Please sign in to comment.