Skip to content

Commit

Permalink
Added paging support for ADS fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Hupe committed Nov 5, 2019
1 parent 82b785d commit 40f5a44
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.IdBasedParserFetcher;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.PagedSearchBasedParserFetcher;
import org.jabref.logic.importer.ParseException;
import org.jabref.logic.importer.Parser;
import org.jabref.logic.importer.SearchBasedParserFetcher;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.net.URLDownload;
import org.jabref.model.cleanup.FieldFormatterCleanup;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.paging.Page;
import org.jabref.model.strings.StringUtil;
import org.jabref.model.util.DummyFileUpdateMonitor;

Expand All @@ -42,7 +43,7 @@
/**
* Fetches data from the SAO/NASA Astrophysics Data System (https://ui.adsabs.harvard.edu/)
*/
public class AstrophysicsDataSystem implements IdBasedParserFetcher, SearchBasedParserFetcher, EntryBasedParserFetcher {
public class AstrophysicsDataSystem implements IdBasedParserFetcher, PagedSearchBasedParserFetcher, EntryBasedParserFetcher {

private static final String API_SEARCH_URL = "https://api.adsabs.harvard.edu/v1/search/query";
private static final String API_EXPORT_URL = "https://api.adsabs.harvard.edu/v1/export/bibtexabs";
Expand Down Expand Up @@ -87,6 +88,16 @@ public URL getURLForQuery(String query) throws URISyntaxException, MalformedURLE
return builder.build().toURL();
}

@Override
public URL getURLForQuery(String query, int size, int pageNumber) throws URISyntaxException, MalformedURLException, FetcherException {
URIBuilder builder = new URIBuilder(API_SEARCH_URL);
builder.addParameter("q", query);
builder.addParameter("fl", "bibcode");
builder.addParameter("rows", String.valueOf(size));
builder.addParameter("start", String.valueOf(size * pageNumber));
return builder.build().toURL();
}

/**
* @param entry BibEntry for which a search URL is created
* @return URL which points to a search request for given entry
Expand Down Expand Up @@ -276,4 +287,22 @@ private List<BibEntry> performSearchByIds(Collection<String> identifiers) throws
throw new FetcherException("An internal parser error occurred", e);
}
}

@Override
public Page<BibEntry> performSearchPaged(Page<BibEntry> page) throws FetcherException {
page.setContent(Collections.emptyList());

if (StringUtil.isBlank(page.getQuery())) {
return page;
}
try {
List<String> bibcodes = fetchBibcodes(getURLForQuery(page.getQuery(), getPageSize(), page.getPageNumber()));
page.setContent(performSearchByIds(bibcodes));
return page;
} catch (URISyntaxException e) {
throw new FetcherException("Search URI is malformed", e);
} catch (IOException e) {
throw new FetcherException("A network error occurred", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.paging.Page;
import org.jabref.testutils.category.FetcherTest;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -198,4 +200,25 @@ public void testPerformSearchByLuceyPaulEntry() throws Exception {
Optional<BibEntry> fetchedEntry = fetcher.performSearchById("2000JGR...10520297L");
assertEquals(Optional.of(luceyPaulEntry), fetchedEntry);
}

@Test
public void performSearchByQueryPaged_searchLimitsSize() throws Exception {
Page<BibEntry> page = fetcher.performSearchPaged("author:\"A\"", 0);
assertEquals(fetcher.getPageSize(), page.getSize(), "fetcher return wrong page size");
}

@Test
public void performSearchByQueryPaged_twoPagesNotEqual() throws Exception {
Page<BibEntry> page = fetcher.performSearchPaged("author:\"A\"", 0);
Page<BibEntry> page2 = fetcher.performSearchPaged("author:\"A\"", 1);
//This tests if the fetcher actually performs paging
assertNotEquals(page.getContent(), page2.getContent(), "Two conseecutive pages shouldn't be equal");
}

@Test
public void performSearchByQueryPaged_pageAsParameter() throws Exception {
Page<BibEntry> searchPage = new Page<>("author:\"A\"", 0);
Page<BibEntry> page = fetcher.performSearchPaged(searchPage);
assertEquals(fetcher.getPageSize(), page.getSize(), "fetcher returns wrong page size");
}
}

0 comments on commit 40f5a44

Please sign in to comment.