Skip to content

Commit

Permalink
attempt to fix root cause of https://github.com/darktaxon/mfn-malaise…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorrit Poelen committed Sep 10, 2024
1 parent 3a1ab71 commit 3dadf3d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
import java.util.regex.Matcher;
import java.util.stream.Collectors;

import static bio.guoda.preston.cmd.ZenodoMetaUtil.IMAGE_TYPE_PHOTO;
import static bio.guoda.preston.cmd.ZenodoMetaUtil.RESOURCE_TYPE_PHOTO;

public class ZenodoUtils {


Expand Down Expand Up @@ -245,13 +248,22 @@ public static IRI getQueryForExistingDepositions(ZenodoConfig ctx, List<String>
public static IRI getQueryForExistingDepositions(ZenodoConfig ctx, List<String> contentIds, String method, String type) {
String prefix = communitiesPrefix(ctx);
String query = prefix + "q=" + getQueryForIds(contentIds);
query = appendTypeClause(type, query);
query = appendTypeClause(type, query, method);
return getQuery(ctx.getEndpoint(), query, method);
}

private static String appendTypeClause(String type, String query) {
if (StringUtils.isNotBlank(type)) {
query = query + "&f=resource_type%3A" + JavaScriptAndPythonFriendlyURLEncodingUtil.urlEncode(type);
private static String appendTypeClause(String type, String query, String method) {
if ( StringUtils.isNotBlank(type)) {
if (StringUtils.startsWith(method, "/search")) {
query = query + "&f=resource_type%3A" + JavaScriptAndPythonFriendlyURLEncodingUtil.urlEncode(type);
} else {
// see https://github.com/zenodo/zenodo/issues/2545
if (RESOURCE_TYPE_PHOTO.equals(type) || "image-photo".equals(type)) {
query = query + "&type=image&subtype=photo";
} else {
query = query + "&type=" + JavaScriptAndPythonFriendlyURLEncodingUtil.urlEncode(type);
}
}
}
return query;
}
Expand All @@ -268,7 +280,8 @@ private static void findExistingRecords(ZenodoConfig ctx, List<String> ids, Coll
public static IRI getQueryForExistingRecords(ZenodoConfig ctx, List<String> ids, String type) {
String prefix = communitiesPrefix(ctx);
String queryPath = prefix + "all_versions=false&q=" + getQueryForIds(ids);
return getQuery(ctx.getEndpoint(), appendTypeClause(type, queryPath), "/api/records");
String method = "/api/records";
return getQuery(ctx.getEndpoint(), appendTypeClause(type, queryPath, method), method);
}

private static String communitiesPrefix(ZenodoConfig ctx) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bio.guoda.preston.zenodo;

import bio.guoda.preston.RefNodeFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.rdf.api.IRI;
Expand Down Expand Up @@ -99,4 +100,65 @@ public void extractFileIdsNoMatching() throws IOException {

}

@Test
public void queryForExistingRecordsPhysicalObject() {
IRI queryForExistingRecords = ZenodoUtils.getQueryForExistingRecords(
new ZenodoContext("secret", "https://sandbox.zenodo.org"),
Arrays.asList("urn:lsid:MfN:Ento:BMT0004596"),
"physicalobject"
);

assertThat(queryForExistingRecords, Is.is(RefNodeFactory.toIRI(
"https://sandbox.zenodo.org/api/records" +
"?all_versions=false" +
"&q=alternate.identifier:%22urn%3Alsid%3AMfN%3AEnto%3ABMT0004596%22" +
"&type=physicalobject"))
);
}

@Test
public void searchPageForExistingRecordsPhysicalObject() {
IRI queryForExistingRecords = ZenodoUtils.getSearchPageForExistingRecords(
new ZenodoContext("secret", "https://sandbox.zenodo.org"),
Arrays.asList("urn:lsid:MfN:Ento:BMT0004596"),
"physicalobject"
);

assertThat(queryForExistingRecords, Is.is(RefNodeFactory.toIRI(
"https://sandbox.zenodo.org/search" +
"?q=alternate.identifier:%22urn%3Alsid%3AMfN%3AEnto%3ABMT0004596%22" +
"&f=resource_type%3Aphysicalobject")));
}

@Test
public void queryForExistingRecordsPhoto() {
IRI queryForExistingRecords = ZenodoUtils.getQueryForExistingRecords(
new ZenodoContext("secret", "https://sandbox.zenodo.org"),
Arrays.asList("urn:lsid:MfN:Ento:BMT0004596"),
"image-photo"
);

assertThat(queryForExistingRecords, Is.is(RefNodeFactory.toIRI(
"https://sandbox.zenodo.org/api/records" +
"?all_versions=false" +
"&q=alternate.identifier:%22urn%3Alsid%3AMfN%3AEnto%3ABMT0004596%22" +
"&type=image" +
"&subtype=photo")));
}


@Test
public void searchPageForExistingRecords() {
IRI queryForExistingRecords = ZenodoUtils.getSearchPageForExistingRecords(
new ZenodoContext("secret", "https://sandbox.zenodo.org"),
Arrays.asList("urn:lsid:MfN:Ento:BMT0004596"),
"image-photo"
);

assertThat(queryForExistingRecords.getIRIString(), Is.is("https://sandbox.zenodo.org/search" +
"?q=alternate.identifier:%22urn%3Alsid%3AMfN%3AEnto%3ABMT0004596%22" +
"&f=resource_type%3Aimage-photo"));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public void findDepositByBothContentIdAndTaxoDrosId() throws IOException {
assertOneRecordWithMatchingId(Arrays.asList(getContentId(), getLsid()));
}



private void assertOneRecordWithMatchingId(List<String> contentId) throws IOException {
Collection<Pair<Long, String>> ids = ZenodoUtils.findByAlternateIds(ctx, contentId, "");
assertThat(ids, not(nullValue()));
Expand Down

0 comments on commit 3dadf3d

Please sign in to comment.