-
-
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 4 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.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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.jabref.logic.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