Skip to content

Commit

Permalink
refactor to use exiting journal info fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr committed Nov 1, 2023
1 parent 1e1347e commit b74f5d1
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package org.jabref.gui.fieldeditors.identifier;

import javax.swing.undo.UndoManager;

import org.jabref.gui.DialogService;
import org.jabref.gui.JabRefGUI;
import org.jabref.gui.StateManager;
import org.jabref.gui.autocompleter.SuggestionProvider;
import org.jabref.gui.mergeentries.FetchAndMergeEntry;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.identifier.ISSN;
import org.jabref.preferences.PreferencesService;

public class ISSNIdentifierEditorViewModel extends BaseIdentifierEditorViewModel<ISSN> {
public ISSNIdentifierEditorViewModel(SuggestionProvider<?> suggestionProvider, FieldCheckers fieldCheckers, DialogService dialogService, TaskExecutor taskExecutor, PreferencesService preferences) {
super(StandardField.ISSN, suggestionProvider, fieldCheckers, dialogService, taskExecutor, preferences);
private final StateManager stateManager;
private final UndoManager undoManager;

public ISSNIdentifierEditorViewModel(SuggestionProvider<?> suggestionProvider, FieldCheckers fieldCheckers, DialogService dialogService, TaskExecutor taskExecutor, PreferencesService preferences, UndoManager undoManager, StateManager stateManager) {
super(StandardField.ISBN, suggestionProvider, fieldCheckers, dialogService, taskExecutor, preferences, undoManager);
this.undoManager = undoManager;
this.stateManager = stateManager;
configure(true, false);
}

@Override
public void fetchBibliographyInformation(BibEntry bibEntry) {
new FetchAndMergeEntry(JabRefGUI.getMainFrame().getCurrentLibraryTab(), taskExecutor, preferences, dialogService).fetchAndMerge(entry, field);
stateManager.getActiveDatabase().ifPresentOrElse(
databaseContext -> new FetchAndMergeEntry(databaseContext, taskExecutor, preferences, dialogService, undoManager)
.fetchAndMerge(entry, field),
() -> dialogService.notify(Localization.lang("No library selected"))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
import com.airhacks.afterburner.views.ViewLoader;
import jakarta.inject.Inject;

import static org.jabref.model.entry.field.StandardField.DOI;
import static org.jabref.model.entry.field.StandardField.EPRINT;
import static org.jabref.model.entry.field.StandardField.ISBN;
import static org.jabref.model.entry.field.StandardField.ISSN;

public class IdentifierEditor extends HBox implements FieldEditorFX {

@FXML private BaseIdentifierEditorViewModel<?> viewModel;
Expand All @@ -53,16 +57,19 @@ public IdentifierEditor(Field field,
// but we need the injected vars to create the viewmodels.
Injector.registerExistingAndInject(this);

if (StandardField.DOI == field) {
this.viewModel = new DoiIdentifierEditorViewModel(suggestionProvider, fieldCheckers, dialogService, taskExecutor, preferencesService, undoManager, stateManager);
} else if (StandardField.ISBN == field) {
this.viewModel = new ISBNIdentifierEditorViewModel(suggestionProvider, fieldCheckers, dialogService, taskExecutor, preferencesService, undoManager, stateManager);
} else if (StandardField.EPRINT == field) {
this.viewModel = new EprintIdentifierEditorViewModel(suggestionProvider, fieldCheckers, dialogService, taskExecutor, preferences);
} else if (StandardField.ISSN == field) {
this.viewModel = new ISSNIdentifierEditorViewModel(suggestionProvider, fieldCheckers, dialogService, taskExecutor, preferences);
} else {
throw new IllegalStateException(String.format("Unable to instantiate a view model for identifier field editor '%s'", field.getDisplayName()));
switch (field) {
case DOI ->
this.viewModel = new DoiIdentifierEditorViewModel(suggestionProvider, fieldCheckers, dialogService, taskExecutor, preferencesService, undoManager, stateManager);
case ISBN ->
this.viewModel = new ISBNIdentifierEditorViewModel(suggestionProvider, fieldCheckers, dialogService, taskExecutor, preferencesService, undoManager, stateManager);
case EPRINT ->
this.viewModel = new EprintIdentifierEditorViewModel(suggestionProvider, fieldCheckers, dialogService, taskExecutor, preferencesService, undoManager);
case ISSN ->
this.viewModel = new ISSNIdentifierEditorViewModel(suggestionProvider, fieldCheckers, dialogService, taskExecutor, preferencesService, undoManager, stateManager);
case null, default -> {
assert field != null;
throw new IllegalStateException(String.format("Unable to instantiate a view model for identifier field editor '%s'", field.getDisplayName()));
}
}

ViewLoader.view(this)
Expand All @@ -76,7 +83,7 @@ public IdentifierEditor(Field field,
lookupIdentifierButton.setTooltip(
new Tooltip(Localization.lang("Look up %0", field.getDisplayName())));

if (field.equals(StandardField.DOI)) {
if (field.equals(DOI)) {
textArea.initContextMenu(EditorMenus.getDOIMenu(textArea, dialogService, preferencesService));
} else {
textArea.initContextMenu(new DefaultMenu(textArea));
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jabref/logic/importer/WebFetchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jabref.logic.importer.fetcher.IEEE;
import org.jabref.logic.importer.fetcher.INSPIREFetcher;
import org.jabref.logic.importer.fetcher.IacrEprintFetcher;
import org.jabref.logic.importer.fetcher.IssnFetcher;
import org.jabref.logic.importer.fetcher.LOBIDFetcher;
import org.jabref.logic.importer.fetcher.LibraryOfCongress;
import org.jabref.logic.importer.fetcher.MathSciNet;
Expand Down Expand Up @@ -135,7 +136,6 @@ public static SortedSet<IdBasedFetcher> getIdBasedFetchers(ImportFormatPreferenc
set.add(new IsbnFetcher(importFormatPreferences));
// .addRetryFetcher(new EbookDeIsbnFetcher(importFormatPreferences)));
// .addRetryFetcher(new DoiToBibtexConverterComIsbnFetcher(importFormatPreferences)));
set.add(new IssnFetcher(importFormatPreferences));
set.add(new DiVA(importFormatPreferences));
set.add(new DoiFetcher(importFormatPreferences));
set.add(new MedlineFetcher());
Expand Down Expand Up @@ -163,7 +163,8 @@ public static SortedSet<EntryBasedFetcher> getEntryBasedFetchers(ImporterPrefere
set.add(new AstrophysicsDataSystem(importFormatPreferences, importerPreferences));
set.add(new DoiFetcher(importFormatPreferences));
set.add(new IsbnFetcher(importFormatPreferences));
// .addRetryFetcher(new EbookDeIsbnFetcher(importFormatPreferences)));
set.add(new IssnFetcher());
// .addRetryFetcher(new EbookDeIsbnFetcher(importFormatPreferences)));
// .addRetryFetcher(new DoiToBibtexConverterComIsbnFetcher(importFormatPreferences)));
set.add(new MathSciNet(importFormatPreferences));
set.add(new CrossRef());
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/jabref/logic/importer/fetcher/DOAJFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public DOAJFetcher(ImportFormatPreferences preferences) {
*/
public static BibEntry parseBibJSONtoBibtex(JSONObject bibJsonEntry, Character keywordSeparator) {
// Fields that are directly accessible at the top level BibJson object
Field[] singleFields = {StandardField.YEAR, StandardField.TITLE, StandardField.ABSTRACT, StandardField.MONTH};
List<Field> singleFields = List.of(StandardField.YEAR, StandardField.TITLE, StandardField.ABSTRACT, StandardField.MONTH);

// Fields that are accessible in the journal part of the BibJson object
Field[] journalSingleFields = {StandardField.PUBLISHER, StandardField.NUMBER, StandardField.VOLUME};
List<Field> journalSingleFields = List.of(StandardField.PUBLISHER, StandardField.NUMBER, StandardField.VOLUME);

BibEntry entry = new BibEntry(StandardEntryType.Article);

Expand Down Expand Up @@ -155,13 +155,10 @@ public static BibEntry parseBibJSONtoBibtex(JSONObject bibJsonEntry, Character k
return entry;
}

public static URIBuilder addPath(URIBuilder base, String subPath) {
public static void addPath(URIBuilder base, String subPath) {
// slightly altered version based on https://gist.github.com/enginer/230e2dc2f1d213a825d5
if (StringUtil.isBlank(subPath) || "/".equals(subPath)) {
return base;
} else {
if (!StringUtil.isBlank(subPath) && !"/".equals(subPath)) {
base.setPath(appendSegmentToPath(base.getPath(), subPath));
return base;
}
}

Expand Down
47 changes: 13 additions & 34 deletions src/main/java/org/jabref/logic/importer/fetcher/IssnFetcher.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.jabref.logic.importer.fetcher;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.jabref.logic.importer.EntryBasedFetcher;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.IdBasedFetcher;
import org.jabref.logic.importer.IdFetcher;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.integrity.ISSNChecker;
import org.jabref.logic.journals.JournalInformation;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.identifier.ISSN;
import org.jabref.model.entry.field.StandardField;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -19,51 +19,30 @@
* The idea is to use the {@link DOAJFetcher} to do a request for a given ISSN number.
*/

public class IssnFetcher implements IdBasedFetcher, IdFetcher<ISSN> {
public class IssnFetcher implements EntryBasedFetcher {

private static final Logger LOGGER = LoggerFactory.getLogger(IssnFetcher.class);
private final DOAJFetcher doajFetcher;
private final JournalInformationFetcher journalInformationFetcher;
// private final String SEARCH_URL = "https://doaj.org/api/search/journals/";
private final ISSNChecker issnChecker;
private final ImportFormatPreferences importFormatPreferences;

public IssnFetcher(ImportFormatPreferences importFormatPreferences) {
this.importFormatPreferences = importFormatPreferences;
this.doajFetcher = new DOAJFetcher(importFormatPreferences);
public IssnFetcher() {
this.journalInformationFetcher = new JournalInformationFetcher();
this.issnChecker = new ISSNChecker();
}

@Override
public Optional<BibEntry> performSearchById(String identifier) throws FetcherException {

Optional<String> checkedId = issnChecker.checkValue(identifier);
public List<BibEntry> performSearch(BibEntry entry) throws FetcherException {
Optional<String> checkedId = entry.getField(StandardField.ISSN).flatMap(issnChecker::checkValue);
if (checkedId.isPresent()) {
String queryString = concatenateIssnWithId(identifier);
List<BibEntry> bibEntries = doajFetcher.performSearch(queryString);
return bibEntries.stream().findFirst();
} else {
LOGGER.warn("Not a valid ISSN");
return Optional.empty();
Optional<JournalInformation> journalInformation = journalInformationFetcher.getJournalInformation(checkedId.get(), "");
return journalInformation.map(journalInfo -> new BibEntry().withField(StandardField.JOURNALTITLE, journalInfo.publisher())).stream().toList();
}
}

@Override
public Optional<ISSN> findIdentifier(BibEntry entry) throws FetcherException {
// Need to create a getIssn() method in BibEntry that returns an Optional
return Optional.empty();
}

@Override
public String getIdentifierName() {
return getName();
return Collections.emptyList();
}

@Override
public String getName() {
return "ISSN";
}

public String concatenateIssnWithId(String identifier) {
return "issn:" + identifier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private JournalInformation parseResponse(JSONObject responseJsonObject) throws F
try {
if (responseJsonObject.has("errors")) {
JSONArray errors = responseJsonObject.optJSONArray("errors");
if (errors != null && errors.length() > 0) {
if (errors != null && !errors.isEmpty()) {
JSONObject error = errors.getJSONObject(0);
String errorMessage = error.optString("message", "");
LOGGER.error("Error accessing catalog: {}", errorMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ public class IsbnFetcher implements EntryBasedFetcher, IdBasedFetcher {
private static final Logger LOGGER = LoggerFactory.getLogger(IsbnFetcher.class);
private static final Pattern NEWLINE_SPACE_PATTERN = Pattern.compile("\\n|\\r\\n|\\s");
protected final ImportFormatPreferences importFormatPreferences;
private final OpenLibraryIsbnFetcher openLibraryIsbnFetcher;
private final List<AbstractIsbnFetcher> retryIsbnFetcher;
private final GvkFetcher gvkIbsnFetcher;

public IsbnFetcher(ImportFormatPreferences importFormatPreferences) {
this.importFormatPreferences = importFormatPreferences;
this.openLibraryIsbnFetcher = new OpenLibraryIsbnFetcher(importFormatPreferences);
OpenLibraryIsbnFetcher openLibraryIsbnFetcher = new OpenLibraryIsbnFetcher(importFormatPreferences);
this.gvkIbsnFetcher = new GvkFetcher(importFormatPreferences);
this.retryIsbnFetcher = new ArrayList<>();
this.addRetryFetcher(openLibraryIsbnFetcher);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jabref.logic.importer.fetcher;

import java.util.Optional;
import java.util.List;

import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.ImportFormatPreferences;
Expand All @@ -27,10 +27,10 @@ void setUp() {
BibEntryPreferences bibEntryPrefs = mock(BibEntryPreferences.class);
when(importPrefs.bibEntryPreferences()).thenReturn(bibEntryPrefs);

fetcher = new IssnFetcher(importPrefs);
fetcher = new IssnFetcher();

bibEntry = new BibEntry(BibEntry.DEFAULT_TYPE)
.withField(StandardField.ISSN, "2579-5341")
.withField(StandardField.ISSN, "2193-1801")
.withField(StandardField.TITLE, "Query: Jurnal Sistem Informasi")
.withField(StandardField.INSTITUTION, "Univesitas Islam Negeri Sumatera Utara, Fakultas Sains dan Teknologi, Program Studi Sistem Informasi")
.withField(StandardField.PUBLISHER, "Univesitas Islam Negeri Sumatera Utara")
Expand All @@ -39,27 +39,13 @@ void setUp() {

@Test
void performSearchById() throws FetcherException {
Optional<BibEntry> fetchedEntry = fetcher.performSearchById("2579-5341");
assertEquals(Optional.of(bibEntry), fetchedEntry);
}

@Test
void findIdentifier() {
}

@Test
void getIdentifierName() {
assertEquals("ISSN", fetcher.getIdentifierName());
List<BibEntry> fetchedEntry = fetcher.performSearch(bibEntry);
assertEquals(List.of(bibEntry), fetchedEntry);
}

@Test
void getName() {
assertEquals("ISSN", fetcher.getName());
}

@Test
void concatenateIssnWithId() {
String modifiedIdentifier = fetcher.concatenateIssnWithId("2579-5341");
assertEquals("issn:2579-5341", modifiedIdentifier);
}
}

0 comments on commit b74f5d1

Please sign in to comment.