Skip to content

Commit

Permalink
depositions queries do not appears to support type queries; related to
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorrit Poelen committed Sep 10, 2024
1 parent a398289 commit 78dc7d7
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,12 @@ public static ZenodoContext upload(ZenodoContext ctx, String filename, HttpEntit
}
}

public static Collection<Pair<Long, String>> findByAlternateIds(ZenodoConfig ctx, List<String> ids, String type, Dereferencer<InputStream> dereferencer) throws IOException {
public static Collection<Pair<Long, String>> findRecordsByAlternateIds(ZenodoConfig ctx, List<String> ids, String type, Dereferencer<InputStream> dereferencer) throws IOException {
Collection<Pair<Long, String>> foundIds = new TreeSet<>();
findExistingRecords(ctx, ids, foundIds, type, dereferencer);
findExistingDepositions(ctx, ids, foundIds, type, dereferencer);
return foundIds;
}

private static void findExistingDepositions(ZenodoConfig ctx, List<String> ids, Collection<Pair<Long, String>> foundIds, String type, Dereferencer<InputStream> dereferencer) throws IOException {
IRI query1 = getQueryForExistingDepositions(ctx, ids, type);
executeQueryAndCollectIds(foundIds, query1, dereferencer);
}

public static IRI getQueryForExistingDepositions(ZenodoConfig ctx, List<String> contentIds, String type) {
return getQueryForExistingDepositions(ctx, contentIds, "/api/deposit/depositions", type);
}

public static IRI getQueryForExistingDepositions(ZenodoConfig ctx, List<String> contentIds, String method, String type) {
String prefix = communitiesPrefix(ctx);
Expand All @@ -271,7 +262,10 @@ private static String appendTypeClause(String type, String query, String method)
}

public static IRI getSearchPageForExistingRecords(ZenodoConfig ctx, List<String> contentIds, String type) {
return getQueryForExistingDepositions(ctx, contentIds, "/search", type);
String prefix = communitiesPrefix(ctx);
String query = prefix + "q=" + getQueryForIds(contentIds);
query = appendTypeClause(type, query, "/search");
return getQuery(ctx.getEndpoint(), query, "/search");
}

private static void findExistingRecords(ZenodoConfig ctx, List<String> ids, Collection<Pair<Long, String>> foundIds, String type, Dereferencer<InputStream> dereferencer) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,12 @@ private void attemptToHandleJSON(String line, IRI coordinate) throws IOException
}

Dereferencer<InputStream> adHocQueryDereferencer = ResourcesHTTP::asInputStream;
Collection<Pair<Long, String>> foundDeposits = ZenodoUtils.findByAlternateIds(
Collection<Pair<Long, String>> foundDeposits = ZenodoUtils.findRecordsByAlternateIds(
ctx,
recordIds,
resourceType,
adHocQueryDereferencer
);
deleteDraftsIfPresent(foundDeposits, recordIds);
createNewVersion(coordinate, zenodoMetadata, recordIds, contentIds, origins, foundDeposits);

}
Expand Down Expand Up @@ -146,29 +145,6 @@ private void collectIds(JsonNode zenodoMetadata, List<String> recordIds, List<St
}
}

private void deleteDraftsIfPresent(Collection<Pair<Long, String>> foundDeposits, List<String> recordIds) throws IOException {
List<Long> draftIds = foundDeposits
.stream()
.filter(x -> StringUtils.equals(x.getValue(), "unsubmitted"))
.map(Pair::getKey)
.distinct()
.collect(Collectors.toList());

ZenodoContext ctxLocal = new ZenodoContext(this.ctx);
for (Long draftId : draftIds) {
ctxLocal.setDepositId(draftId);
try {
String msg = "deleting draft deposit [" + ctxLocal.getDepositId() + "] associated with [{" + StringUtils.join(recordIds, "\"}, {\"") + "}]";
LOG.info(msg + " ...");
ZenodoUtils.delete(ctxLocal);
LOG.info(msg + " done.");
} catch (IOException ex) {
throw new IOException("failed to delete draft/unsubmitted deposit id [" + draftId + "]");
}

}
}

private void createNewVersion(IRI coordinate, JsonNode zenodoMetadata, List<String> recordIds, List<String> contentIds, List<String> origins, Collection<Pair<Long, String>> foundDeposits) throws IOException {
List<Long> existingIds = foundDeposits
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void delete() throws IOException {
}

private void cleanupPreExisting() throws IOException {
Collection<Pair<Long, String>> byAlternateIds = ZenodoUtils.findByAlternateIds(ctx, Arrays.asList(getContentId(), getLsid()), "", uri -> ResourcesHTTP.asInputStream(uri));
Collection<Pair<Long, String>> byAlternateIds = ZenodoUtils.findRecordsByAlternateIds(ctx, Arrays.asList(getContentId(), getLsid()), "", uri -> ResourcesHTTP.asInputStream(uri));
byAlternateIds
.stream()
.filter(d -> StringUtils.equals(d.getValue(), "unsubmitted"))
Expand Down Expand Up @@ -116,7 +116,7 @@ public void findDepositByBothContentIdAndTaxoDrosId() throws IOException {

private void assertOneRecordWithMatchingId(List<String> contentId) throws IOException {

Collection<Pair<Long, String>> ids = ZenodoUtils.findByAlternateIds(ctx, contentId, "", uri -> ResourcesHTTP.asInputStream(uri));
Collection<Pair<Long, String>> ids = ZenodoUtils.findRecordsByAlternateIds(ctx, contentId, "", uri -> ResourcesHTTP.asInputStream(uri));
assertThat(ids, not(nullValue()));
List<Long> filteredIds = ids
.stream()
Expand Down Expand Up @@ -253,7 +253,7 @@ public void findDepositByBothContentIdAndTaxoDrosIdPickMostRecentVersion() throw
List<String> contentIds = Arrays.asList(
"hash://md5/8ec637e349f8bbce57cd0054d20d9d8f",
"urn:lsid:taxodros.uzh.ch:id:aboul-nasr,%201954b");
Collection<Pair<Long, String>> ids = ZenodoUtils.findByAlternateIds(ctx, contentIds, "", uri -> ResourcesHTTP.asInputStream(uri));
Collection<Pair<Long, String>> ids = ZenodoUtils.findRecordsByAlternateIds(ctx, contentIds, "", uri -> ResourcesHTTP.asInputStream(uri));
assertThat(ids, not(nullValue()));
List<Long> filteredIds = ids
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,6 @@

public class ZenodoUtilsTest {

@Test
public void queryForSpecificDeposition() {
ZenodoContext ctx = new ZenodoContext("secret");
IRI queryForExistingDepositions = ZenodoUtils.getQueryForExistingDepositions(ctx, Arrays.asList("urn:lsid:biodiversitylibrary.org:part:79807"), "");

assertThat(
queryForExistingDepositions.getIRIString(),
is("https://sandbox.zenodo.org/api/deposit/depositions?q=alternate.identifier:%22urn%3Alsid%3Abiodiversitylibrary.org%3Apart%3A79807%22")
);
}

@Test
public void htmlSearchResultPageForSpecificDeposition() {
ZenodoContext ctx = new ZenodoContext("secret");
IRI queryForExistingDepositions = ZenodoUtils.getSearchPageForExistingRecords(ctx, Arrays.asList("urn:lsid:biodiversitylibrary.org:part:79807"), "");

assertThat(
queryForExistingDepositions.getIRIString(),
is("https://sandbox.zenodo.org/search?q=alternate.identifier:%22urn%3Alsid%3Abiodiversitylibrary.org%3Apart%3A79807%22")
);
}

@Test
public void queryForExistingDepositions() {
ZenodoContext ctx = new ZenodoContext("secret");
IRI queryForExistingDepositions = ZenodoUtils.getQueryForExistingDepositions(ctx, Arrays.asList("foo:bar", "foo:bar"), "");

assertThat(
queryForExistingDepositions.getIRIString(),
is("https://sandbox.zenodo.org/api/deposit/depositions?q=alternate.identifier:%22foo%3Abar%22%20AND%20alternate.identifier:%22foo%3Abar%22")
);
}


@Test
public void queryForExistingRecords() {
Expand Down Expand Up @@ -116,21 +83,6 @@ public void queryForExistingRecordsPhysicalObject() {
);
}

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

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

@Test
public void searchPageForExistingRecordsPhysicalObject() {
IRI queryForExistingRecords = ZenodoUtils.getSearchPageForExistingRecords(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void delete() throws IOException {
}

private void cleanupPreExisting() throws IOException {
Collection<Pair<Long, String>> byAlternateIds = ZenodoUtils.findByAlternateIds(ctx, Arrays.asList(getContentId(), getLsid()), "", uri -> ResourcesHTTP.asInputStream(uri));
Collection<Pair<Long, String>> byAlternateIds = ZenodoUtils.findRecordsByAlternateIds(ctx, Arrays.asList(getContentId(), getLsid()), "", uri -> ResourcesHTTP.asInputStream(uri));
byAlternateIds
.stream()
.filter(d -> StringUtils.equals(d.getValue(), "unsubmitted"))
Expand Down Expand Up @@ -117,7 +117,7 @@ public void findDepositByBothContentIdAndTaxoDrosId() throws IOException {


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

0 comments on commit 78dc7d7

Please sign in to comment.