Skip to content

Commit

Permalink
Backward compatibility for 4.3.1 (#6296)
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus authored Apr 26, 2020
1 parent 41b1705 commit d7a57b8
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 4 deletions.
Binary file added JabRef-downgrade-regpatch.reg
Binary file not shown.
66 changes: 66 additions & 0 deletions src/main/java/org/jabref/migrations/PreferencesMigrations.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.migrations;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -48,7 +49,12 @@ public static void runMigrations() {
upgradeKeyBindingsToJavaFX(Globals.prefs);
addCrossRefRelatedFieldsForAutoComplete(Globals.prefs);
upgradePreviewStyleFromReviewToComment(Globals.prefs);
// changeColumnVariableNamesFor51 needs to be run before upgradeColumnPre50Preferences to ensure
// backwardcompatibility, as it copies the old values to new variable names and keeps th old sored with the old
// variable names. However, the variables from 5.0 need to be copied to the new variable name too.
changeColumnVariableNamesFor51(Globals.prefs);
upgradeColumnPreferences(Globals.prefs);
restoreVariablesForBackwardCompatibility(Globals.prefs);
upgradePreviewStyleAllowMarkdown(Globals.prefs);
}

Expand Down Expand Up @@ -316,8 +322,13 @@ static void upgradePreviewStyleAllowMarkdown(JabRefPreferences prefs) {
*
* Simple strings are by default parsed as a FieldColumn, so there is nothing to do there, but the formerly hard
* wired columns need to be added.
*
* In 5.1 variable names in JabRefPreferences have changed to offer backward compatibility with pre 5.0 releases
* Pre 5.1: columnNames, columnWidths, columnSortTypes, columnSortOrder
* Since 5.1: mainTableColumnNames, mainTableColumnWidths, mainTableColumnSortTypes, mainTableColumnSortOrder
*/
static void upgradeColumnPreferences(JabRefPreferences preferences) {
// Variable names have to be hardcoded here, since they are already changed in JabRefPreferences
List<String> columnNames = preferences.getStringList(JabRefPreferences.COLUMN_NAMES);
List<Double> columnWidths = preferences.getStringList(JabRefPreferences.COLUMN_WIDTHS)
.stream()
Expand Down Expand Up @@ -371,4 +382,59 @@ static void upgradeColumnPreferences(JabRefPreferences preferences) {
.collect(Collectors.toList()));
}
}

static void changeColumnVariableNamesFor51(JabRefPreferences preferences) {
// The variable names have to be hardcoded, because they have changed between 5.0 and 5.1
List<String> oldColumnNames = preferences.getStringList("columnNames");
List<String> columnNames = preferences.getStringList(JabRefPreferences.COLUMN_NAMES);
if (!oldColumnNames.isEmpty() && columnNames.isEmpty()) {
preferences.putStringList(JabRefPreferences.COLUMN_NAMES, preferences.getStringList("columnNames"));
preferences.putStringList(JabRefPreferences.COLUMN_WIDTHS, preferences.getStringList("columnWidths"));
preferences.putStringList(JabRefPreferences.COLUMN_SORT_TYPES, preferences.getStringList("columnSortTypes"));
preferences.putStringList(JabRefPreferences.COLUMN_SORT_ORDER, preferences.getStringList("columnSortOrder"));
}
}

/**
* In 5.0 the format of column names have changed. That made newer versions of JabRef preferences incompatible with
* earlier versions of JabRef. As some complains came up, we decided to change the variable names and to clear the
* variable contents if they are unreadable, so former versions of JabRef would automatically create preferences
* they can deal with.
*/
static void restoreVariablesForBackwardCompatibility(JabRefPreferences preferences) {
List<String> oldColumnNames = preferences.getStringList(JabRefPreferences.COLUMN_NAMES);
List<String> fieldColumnNames = oldColumnNames.stream()
.filter(columnName -> columnName.startsWith("field:") || columnName.startsWith("special:"))
.map(columnName -> {
if (columnName.startsWith("field:")) {
return columnName.substring(6);
} else { // special
return columnName.substring(8);
}
}).collect(Collectors.toList());

if (!fieldColumnNames.isEmpty()) {
preferences.putStringList("columnNames", fieldColumnNames);

List<String> fieldColumnWidths = new ArrayList<>(Collections.emptyList());
for (int i = 0; i < fieldColumnNames.size(); i++) {
fieldColumnWidths.add("100");
}
preferences.putStringList("columnWidths", fieldColumnWidths);

preferences.put("columnSortTypes", "");
preferences.put("columnSortOrder", "");
}

// Ensure font size is a parsable int variable
try {
// some versions stored the font size as double to the **same** key
// since the preference store is type-safe, we need to add this workaround
String fontSizeAsString = preferences.get(JabRefPreferences.MAIN_FONT_SIZE);
int fontSizeAsInt = (int) Math.round(Double.parseDouble(fontSizeAsString));
preferences.putInt(JabRefPreferences.MAIN_FONT_SIZE, fontSizeAsInt);
} catch (ClassCastException e) {
// already an integer
}
}
}
12 changes: 8 additions & 4 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,14 @@ public class JabRefPreferences implements PreferencesService {
public static final String EXPORT_TERTIARY_SORT_FIELD = "exportTerSort";
public static final String EXPORT_TERTIARY_SORT_DESCENDING = "exportTerDescending";
public static final String NEWLINE = "newline";
public static final String COLUMN_NAMES = "columnNames";
public static final String COLUMN_WIDTHS = "columnWidths";
public static final String COLUMN_SORT_TYPES = "columnSortTypes";
public static final String COLUMN_SORT_ORDER = "columnSortOrder";

// Variable names have changed to ensure backward compatibility with pre 5.0 releases of JabRef
// Pre 5.1: columnNames, columnWidths, columnSortTypes, columnSortOrder
public static final String COLUMN_NAMES = "mainTableColumnNames";
public static final String COLUMN_WIDTHS = "mainTableColumnWidths";
public static final String COLUMN_SORT_TYPES = "mainTableColumnSortTypes";
public static final String COLUMN_SORT_ORDER = "mainTableColumnSortOrder";

public static final String SIDE_PANE_COMPONENT_PREFERRED_POSITIONS = "sidePaneComponentPreferredPositions";
public static final String SIDE_PANE_COMPONENT_NAMES = "sidePaneComponentNames";
public static final String XMP_PRIVACY_FILTERS = "xmpPrivacyFilters";
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/org/jabref/migrations/PreferencesMigrationsTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.migrations;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.prefs.Preferences;

Expand Down Expand Up @@ -219,4 +220,72 @@ void testUpgradeColumnPreferencesFromWithoutTypes() {
verify(prefs).putStringList(JabRefPreferences.COLUMN_WIDTHS, updatedWidths);
verify(prefs).putStringList(JabRefPreferences.COLUMN_SORT_TYPES, newSortTypes);
}

@Test
void testChangeColumnPreferencesVariableNamesFor51() {
List<String> columnNames = Arrays.asList("entrytype", "author/editor", "title", "year", "journal/booktitle", "bibtexkey", "printed");
List<String> columnWidths = Arrays.asList("75", "300", "470", "60", "130", "100", "30");

// The variable names have to be hardcoded, because they have changed between 5.0 and 5.1
when(prefs.getStringList("columnNames")).thenReturn(columnNames);
when(prefs.getStringList("columnWidths")).thenReturn(columnWidths);
when(prefs.getStringList("mainTableColumnSortTypes")).thenReturn(columnNames);
when(prefs.getStringList("mainTableColumnSortOrder")).thenReturn(columnWidths);

when(prefs.getStringList(JabRefPreferences.COLUMN_NAMES)).thenReturn(Collections.emptyList());
when(prefs.getStringList(JabRefPreferences.COLUMN_WIDTHS)).thenReturn(Collections.emptyList());
when(prefs.getStringList(JabRefPreferences.COLUMN_SORT_TYPES)).thenReturn(Collections.emptyList());
when(prefs.getStringList(JabRefPreferences.COLUMN_SORT_ORDER)).thenReturn(Collections.emptyList());

PreferencesMigrations.changeColumnVariableNamesFor51(prefs);

verify(prefs).putStringList(JabRefPreferences.COLUMN_NAMES, columnNames);
verify(prefs).putStringList(JabRefPreferences.COLUMN_WIDTHS, columnWidths);
verify(prefs).putStringList(JabRefPreferences.COLUMN_NAMES, columnNames);
verify(prefs).putStringList(JabRefPreferences.COLUMN_WIDTHS, columnWidths);
}

@Test
void testChangeColumnPreferencesVariableNamesBackwardsCompatibility() {
List<String> columnNames = Arrays.asList("entrytype", "author/editor", "title", "year", "journal/booktitle", "bibtexkey", "printed");
List<String> columnWidths = Arrays.asList("75", "300", "470", "60", "130", "100", "30");

// The variable names have to be hardcoded, because they have changed between 5.0 and 5.1
when(prefs.getStringList("columnNames")).thenReturn(columnNames);
when(prefs.getStringList("columnWidths")).thenReturn(columnWidths);
when(prefs.getStringList("mainTableColumnSortTypes")).thenReturn(columnNames);
when(prefs.getStringList("mainTableColumnSortOrder")).thenReturn(columnWidths);

when(prefs.getStringList(JabRefPreferences.COLUMN_NAMES)).thenReturn(Collections.emptyList());
when(prefs.getStringList(JabRefPreferences.COLUMN_WIDTHS)).thenReturn(Collections.emptyList());
when(prefs.getStringList(JabRefPreferences.COLUMN_SORT_TYPES)).thenReturn(Collections.emptyList());
when(prefs.getStringList(JabRefPreferences.COLUMN_SORT_ORDER)).thenReturn(Collections.emptyList());

PreferencesMigrations.upgradeColumnPreferences(prefs);

verify(prefs, never()).put("columnNames", "anyString");
verify(prefs, never()).put("columnWidths", "anyString");
verify(prefs, never()).put("mainTableColumnSortTypes", "anyString");
verify(prefs, never()).put("mainTableColumnSortOrder", "anyString");
}

@Test
void testRestoreColumnVariablesForBackwardCompatibility() {
List<String> updatedNames = Arrays.asList("groups", "files", "linked_id", "field:entrytype", "field:author/editor", "field:title", "field:year", "field:journal/booktitle", "field:bibtexkey", "special:printed");
List<String> columnNames = Arrays.asList("entrytype", "author/editor", "title", "year", "journal/booktitle", "bibtexkey", "printed");
List<String> columnWidths = Arrays.asList("100", "100", "100", "100", "100", "100", "100");

when(prefs.getStringList(JabRefPreferences.COLUMN_NAMES)).thenReturn(updatedNames);

when(prefs.get(JabRefPreferences.MAIN_FONT_SIZE)).thenReturn("11.2");

PreferencesMigrations.restoreVariablesForBackwardCompatibility(prefs);

verify(prefs).putStringList("columnNames", columnNames);
verify(prefs).putStringList("columnWidths", columnWidths);
verify(prefs).put("columnSortTypes", "");
verify(prefs).put("columnSortOrder", "");

verify(prefs).putInt(JabRefPreferences.MAIN_FONT_SIZE, 11);
}
}

0 comments on commit d7a57b8

Please sign in to comment.