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

Moved cleanup out of background thread to stick with the architecture… #4823

Merged
Merged
6 changes: 2 additions & 4 deletions src/main/java/org/jabref/gui/actions/CleanupAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.jabref.gui.cleanup.CleanupDialog;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.logic.cleanup.CleanupPreset;
import org.jabref.logic.cleanup.CleanupWorker;
import org.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -59,9 +58,8 @@ public void action() {

preferences.setCleanupPreset(chosenPreset.get());

BackgroundTask.wrap(() -> cleanup(chosenPreset.get()))
.onSuccess(result -> showResults())
.executeWith(Globals.TASK_EXECUTOR);
this.cleanup(chosenPreset.get());
this.showResults();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.jabref.logic.externalfiles.LinkedFileHandler;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.net.URLUtil;
import org.jabref.logic.xmp.XmpPreferences;
import org.jabref.logic.xmp.XmpUtilWriter;
import org.jabref.model.database.BibDatabaseContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jabref.logic.net;
package org.jabref.gui.fieldeditors;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -80,30 +80,6 @@ public static boolean isURL(String url) {
return url.contains("://");
}

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

private static String appendSegmentToPath(String path, String segment) {
if (StringUtil.isBlank(path)) {
path = "/";
}

if (path.charAt(path.length() - 1) == '/' || segment.startsWith("/")) {
return path + segment;
}

return path + "/" + segment;
}

/**
* Look for the last '.' in the link, and return the following characters.
* This gives the extension for most reasonably named links.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.net.URLUtil;
import org.jabref.model.strings.StringUtil;

import org.fxmisc.easybind.EasyBind;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/jabref/logic/cleanup/RenamePdfCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.Objects;
import java.util.Optional;

import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.logic.externalfiles.LinkedFileHandler;
import org.jabref.model.FieldChange;
import org.jabref.model.cleanup.CleanupJob;
Expand Down Expand Up @@ -55,8 +54,8 @@ public List<FieldChange> cleanup(BibEntry entry) {
}

if (changed) {
Optional<FieldChange> fileFieldChanged = DefaultTaskExecutor.runInJavaFXThread(() -> entry.setFiles(files));
return OptionalUtil.toList(fileFieldChanged);
Optional<FieldChange> changes = entry.setFiles(files);
return OptionalUtil.toList(changes);
}

return Collections.emptyList();
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/org/jabref/logic/importer/fetcher/DOAJFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.Parser;
import org.jabref.logic.importer.SearchBasedParserFetcher;
import org.jabref.logic.net.URLUtil;
import org.jabref.logic.util.OS;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FieldName;

import org.apache.http.client.utils.URIBuilder;
import org.jabref.model.strings.StringUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
Expand Down Expand Up @@ -165,12 +165,36 @@ public Optional<HelpFile> getHelpPage() {
@Override
public URL getURLForQuery(String query) throws URISyntaxException, MalformedURLException, FetcherException {
URIBuilder uriBuilder = new URIBuilder(SEARCH_URL);
URLUtil.addPath(uriBuilder, query);
DOAJFetcher.addPath(uriBuilder, query);
uriBuilder.addParameter("pageSize", "30"); // Number of results
//uriBuilder.addParameter("page", "1"); // Page (not needed so far)
return uriBuilder.build().toURL();
}

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

private static String appendSegmentToPath(String path, String segment) {
if (StringUtil.isBlank(path)) {
path = "/";
}

if (path.charAt(path.length() - 1) == '/' || segment.startsWith("/")) {
return path + segment;
}

return path + "/" + segment;
}

@Override
public Parser getParser() {
return inputStream -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.Optional;

import org.apache.http.client.utils.URIBuilder;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
Expand Down Expand Up @@ -73,4 +74,32 @@ void testBibJSONConverter() {
public void searchByEmptyQuery() throws Exception {
assertEquals(Collections.emptyList(), fetcher.performSearch(""));
}

@Test
void appendSingleWord() throws Exception {
URIBuilder builder = new URIBuilder("http://example.com/test");
DOAJFetcher.addPath(builder, "/example");
assertEquals("http://example.com/test/example", builder.build().toASCIIString());
}

@Test
void appendSingleWordWithSlash() throws Exception {
URIBuilder builder = new URIBuilder("http://example.com/test");
DOAJFetcher.addPath(builder, "/example");
assertEquals("http://example.com/test/example", builder.build().toASCIIString());
}

@Test
void appendSlash() throws Exception {
URIBuilder builder = new URIBuilder("http://example.com/test");
DOAJFetcher.addPath(builder, "/");
assertEquals("http://example.com/test", builder.build().toASCIIString());
}

@Test
void appendTwoWords() throws Exception {
URIBuilder builder = new URIBuilder("http://example.com/test");
DOAJFetcher.addPath(builder, "example two");
assertEquals("http://example.com/test/example%20two", builder.build().toASCIIString());
}
}
29 changes: 1 addition & 28 deletions src/test/java/org/jabref/logic/net/URLUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jabref.logic.net;

import org.apache.http.client.utils.URIBuilder;
import org.jabref.gui.fieldeditors.URLUtil;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -73,31 +73,4 @@ void isURLshouldRejectInvalidURL() {
assertFalse(URLUtil.isURL("google.com"));
}

@Test
void appendSingleWord() throws Exception {
URIBuilder builder = new URIBuilder("http://example.com/test");
URLUtil.addPath(builder, "/example");
assertEquals("http://example.com/test/example", builder.build().toASCIIString());
}

@Test
void appendSingleWordWithSlash() throws Exception {
URIBuilder builder = new URIBuilder("http://example.com/test");
URLUtil.addPath(builder, "/example");
assertEquals("http://example.com/test/example", builder.build().toASCIIString());
}

@Test
void appendSlash() throws Exception {
URIBuilder builder = new URIBuilder("http://example.com/test");
URLUtil.addPath(builder, "/");
assertEquals("http://example.com/test", builder.build().toASCIIString());
}

@Test
void appendTwoWords() throws Exception {
URIBuilder builder = new URIBuilder("http://example.com/test");
URLUtil.addPath(builder, "example two");
assertEquals("http://example.com/test/example%20two", builder.build().toASCIIString());
}
}