Skip to content
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

Added option in preferences to use custom URL base for DOI generation #7480

Merged
merged 11 commits into from
Mar 8, 2021
12 changes: 11 additions & 1 deletion src/main/java/org/jabref/gui/fieldeditors/IdentifierEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class IdentifierEditor extends HBox implements FieldEditorFX {
@FXML private Button fetchInformationByIdentifierButton;
@FXML private Button lookupIdentifierButton;
private Optional<BibEntry> entry;
private PreferencesService preferences;
private Field field;

public IdentifierEditor(Field field,
TaskExecutor taskExecutor,
Expand All @@ -37,6 +39,8 @@ public IdentifierEditor(Field field,
FieldCheckers fieldCheckers,
PreferencesService preferences) {
this.viewModel = new IdentifierEditorViewModel(field, suggestionProvider, taskExecutor, dialogService, fieldCheckers);
this.preferences = preferences;
this.field = field;

ViewLoader.view(this)
.root(this)
Expand All @@ -50,6 +54,7 @@ public IdentifierEditor(Field field,
new Tooltip(Localization.lang("Look up %0", field.getDisplayName())));

if (field.equals(StandardField.DOI)) {

textArea.initContextMenu(EditorMenus.getDOIMenu(textArea));
} else {
textArea.initContextMenu(new DefaultMenu(textArea));
Expand Down Expand Up @@ -85,6 +90,11 @@ private void lookupIdentifier() {

@FXML
private void openExternalLink() {
viewModel.openExternalLink();
if (StandardField.DOI.equals(field) && preferences.getDOIPreferences().isUseCustom()) {
String baseURL = preferences.getDOIPreferences().getDefaultBaseURI();
viewModel.openDOIWithCustomBase(baseURL);
} else {
viewModel.openExternalLink();
}
BJaroszkowski marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.identifier.DOI;
import org.jabref.model.entry.identifier.Identifier;

import com.tobiasdiez.easybind.EasyBind;
Expand Down Expand Up @@ -78,6 +79,18 @@ public void openExternalLink() {
);
}

public void openDOIWithCustomBase(String baseURI) {
identifier.get().map(x -> (DOI) x).flatMap(x -> x.getExternalURIWithCustomBase(baseURI)).ifPresent(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
identifier.get().map(x -> (DOI) x).flatMap(x -> x.getExternalURIWithCustomBase(baseURI)).ifPresent(
identifier.get().map(identifier-> (DOI) identifier).flatMap(DOI::getExternalURIWithCustomBase(baseURI)).ifPresent(

please no single char variable name like x, use a meaninfgul variable and inside the flatmap you should be able to use a static syntax like DOI::getExternalUriWithCustomBase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong but since flatMap requires Function and we have two input arguments (String baseUri and DOI) is it not impossible to implement the static syntax? IntelliJ seems to agree with that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, overlooked that, you are right

uri -> {
try {
JabRefDesktop.openBrowser(uri);
} catch (IOException ex) {
dialogService.showErrorDialogAndWait(Localization.lang("Unable to open link."), ex);
}
}
);
}

public boolean getIdentifierLookupInProgress() {
return identifierLookupInProgress.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<CheckBox fx:id="showAdvancedHints"
text="%Show advanced hints (i.e. helpful tooltips, suggestions and explanation)"/>

<Label styleClass="sectionHeader" text="DOI"/>
<HBox alignment="CENTER_LEFT" spacing="10.0">
<CheckBox fx:id="useCustomDOI" text="%Use custom DOI base URI for article access"/>
<TextField fx:id="useCustomDOIName" HBox.hgrow="ALWAYS"/>
</HBox>

<Label styleClass="sectionHeader" text="%Entry owner"/>
<HBox alignment="CENTER_LEFT" spacing="10.0">
<CheckBox fx:id="markOwner" text="%Mark new entries with owner name"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

public class GeneralTab extends AbstractPreferenceTabView<GeneralTabViewModel> implements PreferencesTab {

private final ControlsFxVisualizer validationVisualizer = new ControlsFxVisualizer();
@FXML private ComboBox<Language> language;
@FXML private ComboBox<Charset> defaultEncoding;
@FXML private ComboBox<BibDatabaseMode> biblatexMode;
Expand All @@ -35,15 +36,15 @@ public class GeneralTab extends AbstractPreferenceTabView<GeneralTabViewModel> i
@FXML private CheckBox memoryStickMode;
@FXML private CheckBox collectTelemetry;
@FXML private CheckBox showAdvancedHints;
@FXML private CheckBox useCustomDOI;
@FXML private TextField useCustomDOIName;
@FXML private CheckBox markOwner;
@FXML private TextField markOwnerName;
@FXML private CheckBox markOwnerOverwrite;
@FXML private Button markOwnerHelp;
@FXML private CheckBox addCreationDate;
@FXML private CheckBox addModificationDate;

private final ControlsFxVisualizer validationVisualizer = new ControlsFxVisualizer();

public GeneralTab() {
ViewLoader.view(this)
.root(this)
Expand Down Expand Up @@ -83,6 +84,10 @@ public void initialize() {
collectTelemetry.selectedProperty().bindBidirectional(viewModel.collectTelemetryProperty());
showAdvancedHints.selectedProperty().bindBidirectional(viewModel.showAdvancedHintsProperty());

useCustomDOI.selectedProperty().bindBidirectional(viewModel.markDOIProperty());
useCustomDOIName.textProperty().bindBidirectional(viewModel.markDOINameProperty());
useCustomDOIName.disableProperty().bind(useCustomDOI.selectedProperty().not());

markOwner.selectedProperty().bindBidirectional(viewModel.markOwnerProperty());
markOwnerName.textProperty().bindBidirectional(viewModel.markOwnerNameProperty());
markOwnerName.disableProperty().bind(markOwner.selectedProperty().not());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.jabref.logic.l10n.Encodings;
import org.jabref.logic.l10n.Language;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.preferences.DOIPreferences;
import org.jabref.logic.preferences.OwnerPreferences;
import org.jabref.logic.preferences.TimestampPreferences;
import org.jabref.model.database.BibDatabaseMode;
Expand All @@ -40,6 +41,8 @@ public class GeneralTabViewModel implements PreferenceTabViewModel {
private final BooleanProperty collectTelemetryProperty = new SimpleBooleanProperty();
private final BooleanProperty allowIntegerEditionProperty = new SimpleBooleanProperty();
private final BooleanProperty showAdvancedHintsProperty = new SimpleBooleanProperty();
private final BooleanProperty useCustomDOIProperty = new SimpleBooleanProperty();
private final StringProperty useCustomDOINameProperty = new SimpleStringProperty("");
private final BooleanProperty markOwnerProperty = new SimpleBooleanProperty();
private final StringProperty markOwnerNameProperty = new SimpleStringProperty("");
private final BooleanProperty markOwnerOverwriteProperty = new SimpleBooleanProperty();
Expand All @@ -50,6 +53,7 @@ public class GeneralTabViewModel implements PreferenceTabViewModel {
private final PreferencesService preferencesService;
private final GeneralPreferences initialGeneralPreferences;
private final TelemetryPreferences initialTelemetryPreferences;
private final DOIPreferences initialDOIPreferences;
private final OwnerPreferences initialOwnerPreferences;
private final TimestampPreferences initialTimestampPreferences;

Expand All @@ -61,6 +65,7 @@ public GeneralTabViewModel(DialogService dialogService, PreferencesService prefe
this.preferencesService = preferencesService;
this.initialGeneralPreferences = preferencesService.getGeneralPreferences();
this.initialTelemetryPreferences = preferencesService.getTelemetryPreferences();
this.initialDOIPreferences = preferencesService.getDOIPreferences();
this.initialOwnerPreferences = preferencesService.getOwnerPreferences();
this.initialTimestampPreferences = preferencesService.getTimestampPreferences();
}
Expand All @@ -82,6 +87,9 @@ public void setValues() {
collectTelemetryProperty.setValue(initialTelemetryPreferences.shouldCollectTelemetry());
showAdvancedHintsProperty.setValue(initialGeneralPreferences.shouldShowAdvancedHints());

useCustomDOIProperty.setValue(initialDOIPreferences.isUseCustom());
useCustomDOINameProperty.setValue(initialDOIPreferences.getDefaultBaseURI());

markOwnerProperty.setValue(initialOwnerPreferences.isUseOwner());
markOwnerNameProperty.setValue(initialOwnerPreferences.getDefaultOwner());
markOwnerOverwriteProperty.setValue(initialOwnerPreferences.isOverwriteOwner());
Expand Down Expand Up @@ -116,6 +124,10 @@ public void storeSettings() {
preferencesService.storeTelemetryPreferences(
initialTelemetryPreferences.withCollectTelemetry(collectTelemetryProperty.getValue()));

preferencesService.storeDOIPreferences(new DOIPreferences(
useCustomDOIProperty.getValue(),
useCustomDOINameProperty.getValue().trim()));

preferencesService.storeOwnerPreferences(new OwnerPreferences(
markOwnerProperty.getValue(),
markOwnerNameProperty.getValue().trim(),
Expand Down Expand Up @@ -184,6 +196,16 @@ public BooleanProperty showAdvancedHintsProperty() {
return this.showAdvancedHintsProperty;
}

// DOI

public BooleanProperty markDOIProperty() {
return this.useCustomDOIProperty;
}

public StringProperty markDOINameProperty() {
return this.useCustomDOINameProperty;
}

// Entry owner

public BooleanProperty markOwnerProperty() {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/jabref/logic/preferences/DOIPreferences.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jabref.logic.preferences;

public class DOIPreferences {
private final boolean useCustom;
private final String defaultBaseURI;

public DOIPreferences(boolean useCustom, String defaultBaseURI) {
this.useCustom = useCustom;
this.defaultBaseURI = defaultBaseURI;
}

public boolean isUseCustom() {
return useCustom;
}

public String getDefaultBaseURI() {
return defaultBaseURI;
}
}
19 changes: 12 additions & 7 deletions src/main/java/org/jabref/model/entry/identifier/DOI.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
import org.slf4j.LoggerFactory;

/**
* Class for working with <a href="https://en.wikipedia.org/wiki/Digital_object_identifier">Digital object identifiers
* (DOIs)</a> and <a href="http://shortdoi.org">Short DOIs</a>
* Class for working with <a href="https://en.wikipedia.org/wiki/Digital_object_identifier">Digital object identifiers (DOIs)</a> and <a href="http://shortdoi.org">Short DOIs</a>
*/
public class DOI implements Identifier {

public static final URI AGENCY_RESOLVER = URI.create("https://doi.org/doiRA");

private static final Logger LOGGER = LoggerFactory.getLogger(DOI.class);

// DOI/Short DOI resolver
private static final URI RESOLVER = URI.create("https://doi.org");
private static final URI RESOLVER = URI.create("https://doi.org/");

// Regex
// (see http://www.doi.org/doi_handbook/2_Numbering.html)
Expand Down Expand Up @@ -147,8 +145,7 @@ public DOI(String doi) {
/**
* Creates an Optional&lt;DOI> from various schemes including URL, URN, and plain DOIs.
* <p>
* Useful for suppressing the <c>IllegalArgumentException</c> of the Constructor and checking for
* Optional.isPresent() instead.
* Useful for suppressing the <c>IllegalArgumentException</c> of the Constructor and checking for Optional.isPresent() instead.
*
* @param doi the DOI/Short DOI string
* @return an Optional containing the DOI or an empty Optional
Expand Down Expand Up @@ -233,8 +230,16 @@ public boolean isShortDoi() {
*/
@Override
public Optional<URI> getExternalURI() {
return getExternalURIFromBase(RESOLVER);
}

public Optional<URI> getExternalURIWithCustomBase(String customBase) {
return getExternalURIFromBase(URI.create(customBase));
}

private Optional<URI> getExternalURIFromBase(URI base) {
try {
URI uri = new URI(RESOLVER.getScheme(), RESOLVER.getHost(), "/" + doi, null);
URI uri = new URI(base.getScheme(), base.getHost(), "/" + doi, null);
return Optional.of(uri);
} catch (URISyntaxException e) {
// should never happen
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.jabref.logic.net.ProxyPreferences;
import org.jabref.logic.openoffice.OpenOfficePreferences;
import org.jabref.logic.openoffice.StyleLoader;
import org.jabref.logic.preferences.DOIPreferences;
import org.jabref.logic.preferences.OwnerPreferences;
import org.jabref.logic.preferences.TimestampPreferences;
import org.jabref.logic.preview.PreviewLayout;
Expand Down Expand Up @@ -201,6 +202,9 @@ public class JabRefPreferences implements PreferencesService {
public static final String SHOW_ADVANCED_HINTS = "showAdvancedHints";
public static final String DEFAULT_ENCODING = "defaultEncoding";

public static final String BASE_DOI_URI = "baseDOIURI";
public static final String USE_CUSTOM_DOI_URI = "useCustomDOIURI";

public static final String USE_OWNER = "useOwner";
public static final String DEFAULT_OWNER = "defaultOwner";
public static final String OVERWRITE_OWNER = "overwriteOwner";
Expand Down Expand Up @@ -444,6 +448,9 @@ private JabRefPreferences() {
// Set DOI to be the default ID entry generator
defaults.put(ID_ENTRY_GENERATOR, DoiFetcher.NAME);

defaults.put(USE_CUSTOM_DOI_URI, Boolean.FALSE);
defaults.put(BASE_DOI_URI, "https://doi.org");

if (OS.OS_X) {
defaults.put(FONT_FAMILY, "SansSerif");
defaults.put(PUSH_EMACS_PATH, "emacsclient");
Expand Down Expand Up @@ -1354,6 +1361,19 @@ public void storeTelemetryPreferences(TelemetryPreferences preferences) {
putBoolean(ALREADY_ASKED_TO_COLLECT_TELEMETRY, !preferences.shouldAskToCollectTelemetry()); // mind the !
}

@Override
public DOIPreferences getDOIPreferences() {
return new DOIPreferences(
getBoolean(USE_CUSTOM_DOI_URI),
get(BASE_DOI_URI));
}

@Override
public void storeDOIPreferences(DOIPreferences preferences) {
putBoolean(USE_CUSTOM_DOI_URI, preferences.isUseCustom());
put(BASE_DOI_URI, preferences.getDefaultBaseURI());
}

@Override
public OwnerPreferences getOwnerPreferences() {
return new OwnerPreferences(
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/jabref/preferences/PreferencesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.jabref.logic.layout.format.NameFormatterPreferences;
import org.jabref.logic.net.ProxyPreferences;
import org.jabref.logic.openoffice.OpenOfficePreferences;
import org.jabref.logic.preferences.DOIPreferences;
import org.jabref.logic.preferences.OwnerPreferences;
import org.jabref.logic.preferences.TimestampPreferences;
import org.jabref.logic.protectedterms.ProtectedTermsPreferences;
Expand Down Expand Up @@ -144,6 +145,10 @@ public interface PreferencesService {

void storeTelemetryPreferences(TelemetryPreferences preferences);

DOIPreferences getDOIPreferences();

void storeDOIPreferences(DOIPreferences preferences);

OwnerPreferences getOwnerPreferences();

void storeOwnerPreferences(OwnerPreferences preferences);
Expand Down