-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Refactor BibDatabase Migrations #3733
Changes from 3 commits
863813a
a25040a
9e86f5b
e02d633
ce034cb
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.jabref.gui.importer.actions; | ||
|
||
import org.jabref.gui.BasePanel; | ||
import org.jabref.logic.importer.ParserResult; | ||
import org.jabref.logic.importer.migrations.MergeReviewIntoCommentMigration; | ||
|
||
public class MergeReviewIntoComment implements GUIPostOpenAction { | ||
|
||
@Override | ||
public boolean isActionNecessary(ParserResult parserResult) { | ||
return MergeReviewIntoCommentMigration.needsMigration(parserResult); | ||
} | ||
|
||
@Override | ||
public void performAction(BasePanel basePanel, ParserResult parserResult) { | ||
MergeReviewIntoCommentMigration migration = new MergeReviewIntoCommentMigration(); | ||
|
||
migration.performMigration(parserResult); | ||
if (new MergeReviewIntoCommentConfirmation(basePanel).askUserForMerge(MergeReviewIntoCommentMigration.collectConflicts(parserResult))) { | ||
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. I would split that into assignment and declaration. It's unforunately not so easy distinguishable what what ist, because all is named very similar |
||
migration.performConflictingMigration(parserResult); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,33 @@ | ||
package org.jabref.gui.dialogs; | ||
package org.jabref.gui.importer.actions; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.swing.JOptionPane; | ||
|
||
import org.jabref.gui.BasePanel; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.entry.BibEntry; | ||
|
||
public class MergeReviewIntoCommentUIManager { | ||
public class MergeReviewIntoCommentConfirmation { | ||
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. I would add the Dialog suffix to better see that above |
||
|
||
private final BasePanel panel; | ||
|
||
public MergeReviewIntoCommentConfirmation(BasePanel panel) { | ||
this.panel = panel; | ||
} | ||
|
||
public boolean askUserForMerge(List<BibEntry> conflicts) { | ||
List<String> bibKeys = conflicts.stream() | ||
String bibKeys = conflicts.stream() | ||
.map(BibEntry::getCiteKeyOptional) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.collect(Collectors.toList()); | ||
.collect(Collectors.joining(",\n")); | ||
|
||
int answer = JOptionPane.showConfirmDialog( | ||
null, | ||
String.join(",\n", bibKeys) + " " + | ||
panel, | ||
bibKeys + " " + | ||
Localization.lang("has/have both a 'Comment' and a 'Review' field.") + "\n" + | ||
Localization.lang("Since the 'Review' field was deprecated in JabRef 4.2, these two fields are about to be merged into the 'Comment' field.") + "\n" + | ||
Localization.lang("The conflicting fields of these entries will be merged into the 'Comment' field."), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.jabref.migrations; | ||
package org.jabref.logic.importer.migrations; | ||
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. This class has to many UI-dependencies to be in |
||
|
||
import java.util.List; | ||
|
||
|
@@ -43,7 +43,7 @@ | |
*/ | ||
public class FileLinksUpgradeWarning implements GUIPostOpenAction { | ||
|
||
private static final String[] FIELDS_TO_LOOK_FOR = new String[] {FieldName.PDF, FieldName.PS}; | ||
private static final String[] FIELDS_TO_LOOK_FOR = new String[]{FieldName.PDF, FieldName.PS}; | ||
|
||
private boolean offerChangeSettings; | ||
|
||
|
@@ -56,7 +56,6 @@ public class FileLinksUpgradeWarning implements GUIPostOpenAction { | |
* GUIGlobals.FILE_FIELD. | ||
* | ||
* @param database The database to modify. | ||
* @param fields The fields to find links in. | ||
* @return A CompoundEdit specifying the undo operation for the whole operation. | ||
*/ | ||
private static NamedCompound upgradePdfPsToFile(BibDatabase database) { | ||
|
@@ -78,7 +77,7 @@ private static NamedCompound upgradePdfPsToFile(BibDatabase database) { | |
/** | ||
* This method should be performed if the major/minor versions recorded in the ParserResult | ||
* are less than or equal to 2.2. | ||
* @param pr | ||
* | ||
* @return true if the file was written by a jabref version <=2.2 | ||
*/ | ||
@Override | ||
|
@@ -91,18 +90,16 @@ public boolean isActionNecessary(ParserResult pr) { | |
// If the "file" directory is not set, offer to migrate pdf/ps dir: | ||
offerSetFileDir = !Globals.prefs.hasKey(FieldName.FILE + FileDirectoryPreferences.DIR_SUFFIX) | ||
&& (Globals.prefs.hasKey(FieldName.PDF + FileDirectoryPreferences.DIR_SUFFIX) | ||
|| Globals.prefs.hasKey(FieldName.PS + FileDirectoryPreferences.DIR_SUFFIX)); | ||
|| Globals.prefs.hasKey(FieldName.PS + FileDirectoryPreferences.DIR_SUFFIX)); | ||
|
||
// First check if this warning is disabled: | ||
return Globals.prefs.getBoolean(JabRefPreferences.SHOW_FILE_LINKS_UPGRADE_WARNING) | ||
&& isThereSomethingToBeDone(); | ||
} | ||
|
||
/** | ||
/* | ||
* This method presents a dialog box explaining and offering to make the | ||
* changes. If the user confirms, the changes are performed. | ||
* @param panel | ||
* @param parserResult | ||
*/ | ||
@Override | ||
public void performAction(BasePanel panel, ParserResult parserResult) { | ||
|
@@ -130,7 +127,7 @@ public void performAction(BasePanel panel, ParserResult parserResult) { | |
int row = 1; | ||
formBuilder.add(new JLabel("<html>" + Localization.lang("This library uses outdated file links.") + "<br><br>" | ||
+ Localization | ||
.lang("JabRef no longer supports 'ps' or 'pdf' fields.<br>File links are now stored in the 'file' field and files are stored in an external file directory.<br>To make use of this feature, JabRef needs to upgrade file links.<br><br>") | ||
.lang("JabRef no longer supports 'ps' or 'pdf' fields.<br>File links are now stored in the 'file' field and files are stored in an external file directory.<br>To make use of this feature, JabRef needs to upgrade file links.<br><br>") | ||
+ "<p>" | ||
+ Localization.lang("Do you want JabRef to do the following operations?") + "</html>")).xy(1, row); | ||
|
||
|
@@ -191,10 +188,11 @@ private boolean isThereSomethingToBeDone() { | |
/** | ||
* Check the database to find out whether any of a set of fields are used | ||
* for any of the entries. | ||
* | ||
* @param database The BIB database. | ||
* @param fields The set of fields to look for. | ||
* @param fields The set of fields to look for. | ||
* @return true if at least one of the given fields is set in at least one entry, | ||
* false otherwise. | ||
* false otherwise. | ||
*/ | ||
private boolean linksFound(BibDatabase database, String[] fields) { | ||
for (BibEntry entry : database.getEntries()) { | ||
|
@@ -209,12 +207,11 @@ private boolean linksFound(BibDatabase database, String[] fields) { | |
|
||
/** | ||
* This method performs the actual changes. | ||
* @param panel | ||
* @param pr | ||
* | ||
* @param fileDir The path to the file directory to set, or null if it should not be set. | ||
*/ | ||
private void makeChanges(BasePanel panel, ParserResult pr, boolean upgradePrefs, | ||
boolean upgradeDatabase, String fileDir) { | ||
boolean upgradeDatabase, String fileDir) { | ||
|
||
if (upgradeDatabase) { | ||
// Update file links links in the database: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.jabref.logic.importer.migrations; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import org.jabref.logic.importer.ParserResult; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.FieldName; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class MergeReviewIntoCommentMigration { | ||
public static final Logger LOGGER = LoggerFactory.getLogger(MergeReviewIntoCommentMigration.class); | ||
|
||
public static boolean needsMigration(ParserResult parserResult) { | ||
return parserResult.getDatabase().getEntries().stream() | ||
.anyMatch(bibEntry -> bibEntry.getField(FieldName.REVIEW).isPresent()); | ||
} | ||
|
||
public void performMigration(ParserResult parserResult) { | ||
/* This migration only handles the non-conflicting entries. | ||
* For the other see this.performConflictingMigration(). | ||
*/ | ||
List<BibEntry> entries = Objects.requireNonNull(parserResult).getDatabase().getEntries(); | ||
|
||
entries.stream() | ||
.filter(MergeReviewIntoCommentMigration::hasReviewField) | ||
.filter(entry -> !MergeReviewIntoCommentMigration.hasCommentField(entry)) | ||
.forEach(entry -> migrate(entry, parserResult)); | ||
} | ||
|
||
public static List<BibEntry> collectConflicts(ParserResult parserResult) { | ||
List<BibEntry> entries = Objects.requireNonNull(parserResult).getDatabase().getEntries(); | ||
|
||
return entries.stream() | ||
.filter(MergeReviewIntoCommentMigration::hasReviewField) | ||
.filter(MergeReviewIntoCommentMigration::hasCommentField) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public void performConflictingMigration(ParserResult parserResult) { | ||
collectConflicts(parserResult).forEach(entry -> migrate(entry, parserResult)); | ||
} | ||
|
||
private String mergeCommentFieldIfPresent(BibEntry entry, String review) { | ||
if (entry.getField(FieldName.COMMENT).isPresent()) { | ||
LOGGER.info(String.format("Both Comment and Review fields are present in %s! Merging them into the comment field.", entry.getAuthorTitleYear(150))); | ||
return String.format("%s\n%s:\n%s", entry.getField(FieldName.COMMENT).get().trim(), Localization.lang("Review"), review.trim()); | ||
} | ||
return review; | ||
} | ||
|
||
private static boolean hasCommentField(BibEntry entry) { | ||
return entry.getField(FieldName.COMMENT).isPresent(); | ||
} | ||
|
||
private static boolean hasReviewField(BibEntry entry) { | ||
return entry.getField(FieldName.REVIEW).isPresent(); | ||
} | ||
|
||
private void migrate(BibEntry entry, ParserResult parserResult) { | ||
if (hasReviewField(entry)) { | ||
updateFields(entry, mergeCommentFieldIfPresent(entry, entry.getField(FieldName.REVIEW).get())); | ||
parserResult.wasChangedOnMigration(); | ||
} | ||
} | ||
|
||
private void updateFields(BibEntry entry, String review) { | ||
entry.setField(FieldName.COMMENT, review); | ||
entry.clearField(FieldName.REVIEW); | ||
} | ||
} |
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.
Maybe rename that with the action suffix so that it is better distingusishable from the other migration