Skip to content

Commit

Permalink
Use mainTitle, then, key title then name array
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Jun 30, 2023
1 parent 3e8ffa6 commit 0f4e2db
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,8 @@ private IssnData extractIssnData(String issn, String json) throws JSONException
JSONArray jsonArray = jsonObject.getJSONArray("@graph");
if (jsonArray != null) {
IssnData issnData = new IssnData();
// Look for the KeyTitle element
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
if (obj.has("@id")) {
String idName = obj.getString("@id");
if (idName.equals(RESOURCE_KEY_TITLE.replace("%issn", issn))) {
String title = obj.getString("value");
String cleanTitle = cleanText(title);
issnData.setMainTitle(cleanTitle);
LOG.debug("Found KeyTitle for '" + issn + "' " + cleanTitle);
return issnData;
}
}
}

// If it can't find it, look for the main resource and extract the
// mainTitle from it or the first element in the name array
String name0 = null;
// Look for mainTitle first
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
if (obj.has("@id")) {
Expand All @@ -104,25 +89,51 @@ private IssnData extractIssnData(String issn, String json) throws JSONException
String title = jsonArray.getJSONObject(i).getJSONArray("name").getString(0);
String cleanTitle = cleanText(title);
issnData.setMainTitle(cleanTitle);
LOG.debug("Found KeyTitle for '" + issn + "' " + cleanTitle);
return issnData;
LOG.debug("Found name[0] for '" + issn + "' " + cleanTitle);
// Save the name[0] in case we can't find the KeyTitle
name0 = cleanTitle;
} else if (nameObject instanceof String) {
String title = jsonArray.getJSONObject(i).getString("name");
String cleanTitle = cleanText(title);
issnData.setMainTitle(cleanTitle);
LOG.debug("Found name for '" + issn + "' " + cleanTitle);
return issnData;
LOG.debug("Found name[0] for '" + issn + "' " + cleanTitle);
// Save the name[0] in case we can't find the KeyTitle
name0 = cleanTitle;
} else {
LOG.warn("Unable to extract name, it is not a string nor an array for " + issn);
throw new IllegalArgumentException("Unable to extract name, it is not a string nor an array for " + issn);
LOG.warn("Unable to extract name[0], it is not a string nor an array for " + issn);
throw new IllegalArgumentException("Unable to extract name[0], it is not a string nor an array for " + issn);
}
} else {
LOG.warn("Unable to extract name, couldn't find the mainTitle nor the name for " + issn);
throw new IllegalArgumentException("Unable to extract name, couldn't find the mainTitle nor the name for " + issn);
LOG.warn("Unable to extract name, couldn't find the mainTitle nor the name[0] for " + issn);
throw new IllegalArgumentException("Unable to extract name, couldn't find the mainTitle nor the name[0] for " + issn);
}
}
}
}

// If mainTitle is not found, Look for the KeyTitle element
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
if (obj.has("@id")) {
String idName = obj.getString("@id");
if (idName.equals(RESOURCE_KEY_TITLE.replace("%issn", issn))) {
String title = obj.getString("value");
String cleanTitle = cleanText(title);
issnData.setMainTitle(cleanTitle);
LOG.debug("Found KeyTitle for '" + issn + "' " + cleanTitle);
return issnData;
}
}
}

// If mainTitle and keyTitle are not available, return the name[0]
if(StringUtils.isNotEmpty(name0)) {
String cleanTitle = cleanText(name0);
issnData.setMainTitle(cleanTitle);
LOG.debug("Found name[0] for '" + issn + "' " + cleanTitle);
return issnData;
}

}
throw new IllegalArgumentException("Unable to extract name, couldn't find the Key Title nor the main resource for " + issn);
}
Expand Down
20 changes: 10 additions & 10 deletions orcid-core/src/test/java/org/orcid/core/issn/IssnClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void testGetIssnDataUseKeyTitle() throws IOException, JSONException, Inte
@Test
public void testGetIssnDataUseMainTitle() throws IOException, JSONException, InterruptedException, URISyntaxException {
when(mockHttpRequestUtils.doGet(any())).thenReturn(mockResponse);
when(mockResponse.body()).thenReturn(getJsonInputStreamFallbackToMainTitle());
when(mockResponse.body()).thenReturn(getJsonInputStreamMainTitle());
when(mockResponse.statusCode()).thenReturn(200);

IssnData data = issnClient.getIssnData("0260-8774");
Expand All @@ -69,7 +69,7 @@ public void testGetIssnDataUseMainTitle() throws IOException, JSONException, Int
@Test
public void testGetIssnDataUseNameArray() throws IOException, JSONException, InterruptedException, URISyntaxException {
when(mockHttpRequestUtils.doGet(any())).thenReturn(mockResponse);
when(mockResponse.body()).thenReturn(getJsonInputStreamFallbackToNameArray());
when(mockResponse.body()).thenReturn(getJsonInputStreamNameArray());
when(mockResponse.statusCode()).thenReturn(200);

IssnData data = issnClient.getIssnData("0260-8774");
Expand All @@ -80,7 +80,7 @@ public void testGetIssnDataUseNameArray() throws IOException, JSONException, Int
@Test
public void testGetIssnDataUseNameArrayAsString() throws IOException, JSONException, InterruptedException, URISyntaxException {
when(mockHttpRequestUtils.doGet(any())).thenReturn(mockResponse);
when(mockResponse.body()).thenReturn(getJsonInputStreamFallbackToNameArrayAsString());
when(mockResponse.body()).thenReturn(getJsonInputStreamNameArrayAsString());
when(mockResponse.statusCode()).thenReturn(200);

IssnData data = issnClient.getIssnData("0260-8774");
Expand Down Expand Up @@ -135,22 +135,22 @@ public void testGetIssnDataBadCharactersNameArray() throws IOException, JSONExce
}

private String getJsonInputStreamKeyTitle() throws IOException {
InputStream is = getClass().getResourceAsStream("/issn-response-key-title.json");
InputStream is = getClass().getResourceAsStream("/issn-response-use-key-title.json");
return new String(is.readAllBytes(), StandardCharsets.UTF_8);
}

private String getJsonInputStreamFallbackToMainTitle() throws IOException {
InputStream is = getClass().getResourceAsStream("/issn-response-fallback-to-main-title.json");
private String getJsonInputStreamMainTitle() throws IOException {
InputStream is = getClass().getResourceAsStream("/issn-response-use-main-title.json");
return new String(is.readAllBytes(), StandardCharsets.UTF_8);
}

private String getJsonInputStreamFallbackToNameArray() throws IOException {
InputStream is = getClass().getResourceAsStream("/issn-response-fallback-to-name-array.json");
private String getJsonInputStreamNameArray() throws IOException {
InputStream is = getClass().getResourceAsStream("/issn-response-use-name-array.json");
return new String(is.readAllBytes(), StandardCharsets.UTF_8);
}

private String getJsonInputStreamFallbackToNameArrayAsString() throws IOException {
InputStream is = getClass().getResourceAsStream("/issn-response-fallback-to-name-array-as-string.json");
private String getJsonInputStreamNameArrayAsString() throws IOException {
InputStream is = getClass().getResourceAsStream("/issn-response-use-name-array-as-string.json");
return new String(is.readAllBytes(), StandardCharsets.UTF_8);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"@id" : "resource/ISSN/0021-9525",
"@type" : [ "http://id.loc.gov/ontologies/bibframe/Work", "http://id.loc.gov/ontologies/bibframe/Instance", "http://schema.org/Periodical" ],
"identifiedBy" : [ "resource/ISSN/0021-9525#KeyTitle", "resource/ISSN/0021-9525#ISSN", "resource/ISSN/0021-9525#ISSN-L" ],
"mainTitle" : "˜\u0098The \u009CJournal of cell biology - Main Title",
"otherPhysicalFormat" : "resource/ISSN/1540-8140",
"title" : "resource/ISSN/0021-9525#KeyTitle",
"format" : "vocabularies/medium#Print",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"@id" : "resource/ISSN/1755-4349",
"@type" : [ "http://id.loc.gov/ontologies/bibframe/Work", "http://schema.org/Periodical", "http://id.loc.gov/ontologies/bibframe/Instance" ],
"identifiedBy" : [ "resource/ISSN/1755-4349#ISSN", "resource/ISSN/1755-4349#KeyTitle", "resource/ISSN/1755-4349#ISSN-L" ],
"mainTitle" : "Nature chemistry.",
"otherPhysicalFormat" : "resource/ISSN/1755-4330",
"title" : "resource/ISSN/1755-4349#KeyTitle",
"hasIncorrectISSN" : "1755-4330",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
"@id" : "resource/ISSN/0260-8774#ReferencePublicationEvent",
"@type" : "http://schema.org/PublicationEvent",
"location" : "http://id.loc.gov/vocabulary/countries/xxk"
}, {
"@id" : "resource/ISSN/1755-4349#KeyTitle",
"@type" : [ "http://id.loc.gov/ontologies/bibframe/Identifier", "http://id.loc.gov/ontologies/bibframe/KeyTitle" ],
"value" : "Journal of food engineering - Key Title"
} ],
"@context" : {
"wasAttributedTo" : {
Expand Down

0 comments on commit 0f4e2db

Please sign in to comment.