diff --git a/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/transformation/xslt/DataFetcher.java b/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/transformation/xslt/DataFetcher.java index 2197908e9..ef5269927 100644 --- a/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/transformation/xslt/DataFetcher.java +++ b/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/transformation/xslt/DataFetcher.java @@ -1,18 +1,11 @@ package eu.dnetlib.dhp.transformation.xslt; - -import com.mongodb.util.JSON; import org.apache.commons.io.IOUtils; import org.json.JSONObject; -import scala.Char; - import java.io.IOException; import java.net.URL; -import java.nio.charset.Charset; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; - /** * This class fetches JSON from a provided link and returns * a Dublin Core. This functionality is particularly needed for OSF Preprints @@ -21,8 +14,26 @@ import java.util.List; /** * this method fetches JSON from a provided URL and returns it as JSON Object */ + + public class DataFetcher { - // fetch data + + /** + * this method fetches JSON from a provided URL and returns it as Dublin Core + */ + public static List fetchAndTransform(URL url) throws IOException { + + JSONObject jsonObject = getJson(url); + List authors = getAuthorsFromJson(jsonObject); + return transformListToDublinCore(authors); + } + + /** + * This method fetches JSON object from a given URL + * @param url + * @return + * @throws IOException + */ static JSONObject getJson(URL url) throws IOException { String json = IOUtils.toString(url); @@ -36,12 +47,9 @@ public class DataFetcher { * @return */ static List getAuthorsFromJson(JSONObject jsonObject) { - List authors = new ArrayList<>(); - // count of authors int countOfAuthors = jsonObject.getJSONArray("data").length(); - for (int i = 0; i < countOfAuthors; i++) { authors.add(jsonObject @@ -52,12 +60,15 @@ public class DataFetcher { .getJSONObject("data") .getJSONObject("attributes") .getString("full_name")); - } return authors; } - // transform list of authors into Dublin Core + /** + * This method transforms list of authors into Dublin Core + * @param authors + * @return Dublin Core list of authors + */ static List transformListToDublinCore(List authors) { List dublinCoreAuthors = new ArrayList<>(); @@ -73,7 +84,6 @@ public class DataFetcher { dublinCoreAuthors.add( "" + lastName + ", " + initialOfFirstName + ". (" + firstName + ")" + ""); - } return dublinCoreAuthors; } diff --git a/dhp-workflows/dhp-aggregation/src/test/java/eu/dnetlib/dhp/transformation/xslt/DataFetcherTest.java b/dhp-workflows/dhp-aggregation/src/test/java/eu/dnetlib/dhp/transformation/xslt/DataFetcherTest.java index 03382328d..95fef81de 100644 --- a/dhp-workflows/dhp-aggregation/src/test/java/eu/dnetlib/dhp/transformation/xslt/DataFetcherTest.java +++ b/dhp-workflows/dhp-aggregation/src/test/java/eu/dnetlib/dhp/transformation/xslt/DataFetcherTest.java @@ -5,7 +5,6 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import javax.xml.crypto.Data; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @@ -48,7 +47,6 @@ class DataFetcherTest { JSONObject testJsonObj = DataFetcher.getJson(contributorsUrl); List authors = DataFetcher.getAuthorsFromJson(testJsonObj); System.out.println(authors); - System.out.println(DataFetcher.transformListToDublinCore(authors)); }