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 281f45e98..2197908e9 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 @@ -3,10 +3,13 @@ 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; @@ -27,38 +30,51 @@ public class DataFetcher { } /** - * This method extracts authors (contributors) from a given JSON + * This method extracts authors from a given JSON + * * @param jsonObject * @return */ - static List getContributorsFromJson(JSONObject jsonObject){ - List contributors; + static List getAuthorsFromJson(JSONObject jsonObject) { + + List authors = new ArrayList<>(); // count of authors - int countAuthors = jsonObject.getJSONArray("data").length(); + int countOfAuthors = jsonObject.getJSONArray("data").length(); + for (int i = 0; i < countOfAuthors; i++) { -// jsonObject.getJSONArray("data").forEach(json1 -> System.out.println(json1)); - - - -// String x = testJsonObj -// .getJSONArray("data") -// .getJSONObject(0) -// .getJSONObject("embeds") -// .getJSONObject("users") -// .getJSONObject("data") -// .getJSONObject("attributes") -// .getString("full_name"); - - return null; - + authors.add(jsonObject + .getJSONArray("data") + .getJSONObject(i) + .getJSONObject("embeds") + .getJSONObject("users") + .getJSONObject("data") + .getJSONObject("attributes") + .getString("full_name")); + } + return authors; } + // transform list of authors into Dublin Core + static List transformListToDublinCore(List authors) { - // transform it into Dublin Core - private void transformToDublinCore(JSON jsonData) { - System.out.println(jsonData); + List dublinCoreAuthors = new ArrayList<>(); + for (String author : authors){ + + //splitting full name into first and last names according to OpenAIRE v3 guidelines at: + // https://guidelines.openaire.eu/en/latest/literature/field_creator.html + // “surname”, “initials” (“first name”) “prefix”. + String[] parts = author.split(" "); + String firstName = parts[0]; + String lastName = parts[1]; + char initialOfFirstName = firstName.charAt(0); + + 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 aa8e987b2..03382328d 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 @@ -1,18 +1,16 @@ package eu.dnetlib.dhp.transformation.xslt; -import org.json.JSONArray; import org.json.JSONObject; 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; import java.net.URL; import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.IntStream; import static org.junit.jupiter.api.Assertions.*; @@ -39,13 +37,19 @@ class DataFetcherTest { .getJSONObject("data") .getJSONObject("attributes") .getString("full_name"); - System.out.println(x); - System.out.println(testJsonObj.getJSONArray("data").length()); - testJsonObj.getJSONArray("data").forEach(json1 -> System.out.println(json1)); + } + @Test + void getAuthorsFromJson() throws IOException, URISyntaxException { + URL contributorsUrl = new URI("https://api.osf.io/v2/preprints/mrwqb/contributors/?format=json").toURL(); + JSONObject testJsonObj = DataFetcher.getJson(contributorsUrl); + List authors = DataFetcher.getAuthorsFromJson(testJsonObj); + System.out.println(authors); + + System.out.println(DataFetcher.transformListToDublinCore(authors)); } } \ No newline at end of file