From 1f3d20af561d2aa138d44ef82048b9d42e4a5683 Mon Sep 17 00:00:00 2001 From: InAnYan Date: Wed, 7 Aug 2024 12:15:48 +0300 Subject: [PATCH] Fix for https://github.com/InAnYan/jabref/issues/113 --- ...istenForCitationKeyChangeForAiAction.java} | 4 +- .../importer/actions/OpenDatabaseAction.java | 2 +- .../ai/summarization/SummariesStorage.java | 46 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) rename src/main/java/org/jabref/gui/importer/actions/{ListenForCitationKeyChangeForChatHistoryAction.java => ListenForCitationKeyChangeForAiAction.java} (77%) diff --git a/src/main/java/org/jabref/gui/importer/actions/ListenForCitationKeyChangeForChatHistoryAction.java b/src/main/java/org/jabref/gui/importer/actions/ListenForCitationKeyChangeForAiAction.java similarity index 77% rename from src/main/java/org/jabref/gui/importer/actions/ListenForCitationKeyChangeForChatHistoryAction.java rename to src/main/java/org/jabref/gui/importer/actions/ListenForCitationKeyChangeForAiAction.java index 695b5751d4f..095e2840cce 100644 --- a/src/main/java/org/jabref/gui/importer/actions/ListenForCitationKeyChangeForChatHistoryAction.java +++ b/src/main/java/org/jabref/gui/importer/actions/ListenForCitationKeyChangeForAiAction.java @@ -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 @@ -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()); } } diff --git a/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java b/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java index 0a5c7f5ce70..f94a8595eec 100644 --- a/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java +++ b/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java @@ -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; diff --git a/src/main/java/org/jabref/logic/ai/summarization/SummariesStorage.java b/src/main/java/org/jabref/logic/ai/summarization/SummariesStorage.java index 027dd483cbf..582b65b28fa 100644 --- a/src/main/java/org/jabref/logic/ai/summarization/SummariesStorage.java +++ b/src/main/java/org/jabref/logic/ai/summarization/SummariesStorage.java @@ -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; } @@ -39,4 +52,37 @@ public Optional 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 = 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 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 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()); + } }