Skip to content

Commit

Permalink
Add manual migration for search syntax when automatic migration fails (
Browse files Browse the repository at this point in the history
…#11777)

* Add manual migration for search syntax when automatic migration fails

* Fix search syntax version, after adding new search group
  • Loading branch information
LoayGhreeb authored Sep 16, 2024
1 parent 276d54c commit 4a2cf03
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
24 changes: 24 additions & 0 deletions src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.importer.actions.SearchGroupsMigrationAction;
import org.jabref.gui.preferences.GuiPreferences;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.auxparser.DefaultAuxParser;
Expand Down Expand Up @@ -319,6 +320,17 @@ public AbstractGroup resultConverter(ButtonType button) {
searchGroupSearchTermProperty.getValue().trim(),
searchFlagsProperty.getValue());

if (currentDatabase.getMetaData().getGroupSearchSyntaxVersion().isEmpty()) {
// If the syntax version for search groups is not present, it indicates that the groups
// have not been migrated to the new syntax, or this is the first search group in the library.
// If this is the first search group, set the syntax version to the new version.
// Otherwise, it means that the user did not accept the migration to the new version.
Optional<GroupTreeNode> groups = currentDatabase.getMetaData().getGroups();
if (groups.filter(this::groupOrSubgroupIsSearchGroup).isEmpty()) {
currentDatabase.getMetaData().setGroupSearchSyntaxVersion(SearchGroupsMigrationAction.VERSION_6_0_ALPHA);
}
}

Optional<LuceneManager> luceneManager = stateManager.getLuceneManager(currentDatabase);
if (luceneManager.isPresent()) {
SearchGroup searchGroup = (SearchGroup) resultingGroup;
Expand Down Expand Up @@ -620,4 +632,16 @@ public StringProperty autoGroupPersonsFieldProperty() {
public StringProperty texGroupFilePathProperty() {
return texGroupFilePathProperty;
}

private boolean groupOrSubgroupIsSearchGroup(GroupTreeNode groupTreeNode) {
if (groupTreeNode.getGroup() instanceof SearchGroup) {
return true;
}
for (GroupTreeNode child : groupTreeNode.getChildren()) {
if (groupOrSubgroupIsSearchGroup(child)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.jabref.model.groups.SearchGroup;
import org.jabref.model.search.SearchFlags;

import org.antlr.v4.runtime.misc.ParseCancellationException;

/**
* This action checks whether the syntax for SearchGroups is the new one.
* If not we ask the user whether to migrate.
Expand Down Expand Up @@ -54,18 +56,26 @@ public void performAction(ParserResult parserResult, DialogService dialogService
return;
}

parserResult.getMetaData().getGroups().ifPresent(this::migrateGroups);
parserResult.getMetaData().getGroups().ifPresent(groupTreeNode -> migrateGroups(groupTreeNode, dialogService));
parserResult.getMetaData().setGroupSearchSyntaxVersion(VERSION_6_0_ALPHA);
parserResult.setChangedOnMigration(true);
}

private void migrateGroups(GroupTreeNode node) {
private void migrateGroups(GroupTreeNode node, DialogService dialogService) {
if (node.getGroup() instanceof SearchGroup searchGroup) {
String luceneSearchExpression = SearchToLuceneMigration.migrateToLuceneSyntax(searchGroup.getSearchExpression(), searchGroup.getSearchFlags().contains(SearchFlags.REGULAR_EXPRESSION));
searchGroup.setSearchExpression(luceneSearchExpression);
try {
String luceneSearchExpression = SearchToLuceneMigration.migrateToLuceneSyntax(searchGroup.getSearchExpression(), searchGroup.getSearchFlags().contains(SearchFlags.REGULAR_EXPRESSION));
searchGroup.setSearchExpression(luceneSearchExpression);
} catch (ParseCancellationException e) {
Optional<String> luceneSearchExpression = dialogService.showInputDialogWithDefaultAndWait(
Localization.lang("Search group migration failed"),
Localization.lang("The search group '%0' could not be migrated. Please enter the new search expression.", searchGroup.getName()),
searchGroup.getSearchExpression());
luceneSearchExpression.ifPresent(searchGroup::setSearchExpression);
}
}
for (GroupTreeNode child : node.getChildren()) {
migrateGroups(child);
migrateGroups(child, dialogService);
}
}
}
4 changes: 3 additions & 1 deletion src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,11 @@ Dynamically\ group\ entries\ by\ searching\ a\ field\ for\ a\ keyword=Dynamicall
Each\ line\ must\ be\ of\ the\ following\ form\:\ \'tab\:field1;field2;...;fieldN\'.=Each line must be of the following form\: 'tab\:field1;field2;...;fieldN'.

Search\ groups\ migration\ of\ %0=Search groups migration of %0
The\ search\ groups\ syntax\ is\ outdated.\ Do\ you\ want\ to\ migrate\ to\ the\ new\ syntax?= The search groups syntax is outdated. Do you want to migrate to the new syntax?
The\ search\ groups\ syntax\ is\ outdated.\ Do\ you\ want\ to\ migrate\ to\ the\ new\ syntax?=The search groups syntax is outdated. Do you want to migrate to the new syntax?
Migrate=Migrate
Keep\ as\ is=Keep as is
Search\ group\ migration\ failed=Search group migration failed
The\ search\ group\ '%0'\ could\ not\ be\ migrated.\ Please\ enter\ the\ new\ search\ expression.=The search group '%0' could not be migrated. Please enter the new search expression.
Edit=Edit

Edit\ file\ type=Edit file type
Expand Down

0 comments on commit 4a2cf03

Please sign in to comment.