Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[METASVC-133] Digipress endpoint #140

Merged
merged 7 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### Added

- New endpoint `/v6/digipress` for faster retrieval of the newspaper list (manifestations):
supports both filtering and sorting

## [9.2.1](https://github.com/dbmdz/metadata-service/releases/tag/9.2.1) - 2024-06-11

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion metasvc-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.dbmdz.metadata</groupId>
<artifactId>metasvc</artifactId>
<version>9.2.2-SNAPSHOT</version>
<version>9.3.0-SNAPSHOT</version>
</parent>

<name>Metadata-Service: Repository Client</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package de.digitalcollections.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import de.digitalcollections.model.exception.TechnicalException;
import de.digitalcollections.model.exception.http.HttpErrorDecoder;
import de.digitalcollections.model.list.ListRequest;
import de.digitalcollections.model.list.ListResponse;
import de.digitalcollections.model.list.filtering.FilterCriteria;
import de.digitalcollections.model.list.filtering.FilterCriterion;
import de.digitalcollections.model.list.filtering.FilterLogicalOperator;
Expand Down Expand Up @@ -202,11 +204,41 @@ protected String doDeleteRequestForString(String requestUrl) throws TechnicalExc
}
}

protected <U> ListResponse<U, ListRequest> doGetRequestForObjectListResponse(
String requestUrl, Class<U> targetType, ListRequest listRequest) throws TechnicalException {
if (listRequest != null) {
if (listRequest.hasFiltering())
requestUrl +=
(requestUrl.contains("?") ? "&" : "?")
+ getFilterParamsAsString(listRequest.getFiltering());
if (listRequest.hasSorting())
requestUrl += (requestUrl.contains("?") ? "&" : "?") + getSortParams(listRequest);
}
HttpRequest req = createGetRequest(requestUrl);
try {
HttpResponse<byte[]> response = http.send(req, HttpResponse.BodyHandlers.ofByteArray());
Integer statusCode = response.statusCode();
if (statusCode >= 400) {
throw HttpErrorDecoder.decode("GET " + requestUrl, statusCode, response);
}
// This is the most performant approach for Jackson
final byte[] body = response.body();
if (body == null || body.length == 0) {
return null;
}
ListResponse<U, ListRequest> result =
mapper.readerFor(new TypeReference<ListResponse<U, ListRequest>>() {}).readValue(body);
return result;
} catch (IOException | InterruptedException e) {
throw new TechnicalException("Failed to retrieve response due to connection error", e);
}
}

protected T doGetRequestForObject(String requestUrl) throws TechnicalException {
return (T) doGetRequestForObject(requestUrl, targetType);
return doGetRequestForObject(requestUrl, targetType);
}

protected Object doGetRequestForObject(String requestUrl, Class<?> targetType)
protected <U> U doGetRequestForObject(String requestUrl, Class<U> targetType)
throws TechnicalException {
HttpRequest req = createGetRequest(requestUrl);
try {
Expand All @@ -220,26 +252,36 @@ protected Object doGetRequestForObject(String requestUrl, Class<?> targetType)
if (body == null || body.length == 0) {
return null;
}
T result = mapper.readerFor(targetType).readValue(body);
U result = mapper.readerFor(targetType).readValue(body);
return result;
} catch (IOException | InterruptedException e) {
throw new TechnicalException("Failed to retrieve response due to connection error", e);
}
}

protected List<T> doGetRequestForObjectList(String requestUrl) throws TechnicalException {
return doGetRequestForObjectList(requestUrl, targetType, null);
return doGetRequestForObjectList(requestUrl, targetType, (ListRequest) null);
}

protected List doGetRequestForObjectList(String requestUrl, Class<?> targetType)
protected <U> List<U> doGetRequestForObjectList(String requestUrl, Class<U> targetType)
throws TechnicalException {
return doGetRequestForObjectList(requestUrl, targetType, null);
return doGetRequestForObjectList(requestUrl, targetType, (ListRequest) null);
}

protected List doGetRequestForObjectList(
String requestUrl, Class<?> targetType, Filtering filtering) throws TechnicalException {
if (filtering != null) {
requestUrl += (requestUrl.contains("?") ? "&" : "?") + getFilterParamsAsString(filtering);
protected <U> List<U> doGetRequestForObjectList(
String requestUrl, Class<U> targetType, Filtering filtering) throws TechnicalException {
return doGetRequestForObjectList(requestUrl, targetType, new ListRequest(filtering));
}

protected <U> List<U> doGetRequestForObjectList(
String requestUrl, Class<U> targetType, ListRequest listRequest) throws TechnicalException {
if (listRequest != null) {
if (listRequest.hasFiltering())
requestUrl +=
(requestUrl.contains("?") ? "&" : "?")
+ getFilterParamsAsString(listRequest.getFiltering());
if (listRequest.hasSorting())
requestUrl += (requestUrl.contains("?") ? "&" : "?") + getSortParams(listRequest);
}
HttpRequest req = createGetRequest(requestUrl);
// TODO add creation of a request id if needed
Expand All @@ -255,7 +297,7 @@ protected List doGetRequestForObjectList(
if (body == null || body.length == 0) {
return null;
}
List result = mapper.readerForListOf(targetType).readValue(body);
List<U> result = mapper.readerForListOf(targetType).readValue(body);
return result;
} catch (IOException | InterruptedException e) {
throw new TechnicalException("Failed to retrieve response due to connection error", e);
Expand Down Expand Up @@ -295,6 +337,7 @@ protected PageResponse<T> doGetRequestForPagedObjectList(
}
}

// FIXME: targetType is ignored.
protected PageResponse doGetRequestForPagedObjectList(
String requestUrl, PageRequest pageRequest, Class<?> targetType) throws TechnicalException {
return doGetRequestForPagedObjectList(requestUrl, pageRequest);
Expand Down Expand Up @@ -524,7 +567,7 @@ protected T doPutRequestForObject(String requestUrl, T object) throws TechnicalE
}
}

protected Object doPutRequestForObject(String requestUrl, Object bodyObject, Class<?> targetType)
protected <U> U doPutRequestForObject(String requestUrl, Object bodyObject, Class<U> targetType)
throws TechnicalException {
try {
HttpRequest req = createPutRequest(requestUrl, bodyObject);
Expand All @@ -538,15 +581,15 @@ protected Object doPutRequestForObject(String requestUrl, Object bodyObject, Cla
if (body == null) {
return null;
}
Object result = mapper.readerFor(targetType).readValue(body);
U result = mapper.readerFor(targetType).readValue(body);
return result;
} catch (IOException | InterruptedException e) {
throw new TechnicalException("Failed to retrieve response due to error", e);
}
}

protected List<Class<?>> doPutRequestForObjectList(
String requestUrl, List<Class<?>> list, Class<?> targetType) throws TechnicalException {
protected <U> List<U> doPutRequestForObjectList(
String requestUrl, List<?> list, Class<U> targetType) throws TechnicalException {
try {
HttpRequest req = createPutRequest(requestUrl, list);
HttpResponse<byte[]> response = http.send(req, HttpResponse.BodyHandlers.ofByteArray());
Expand All @@ -559,7 +602,7 @@ protected List<Class<?>> doPutRequestForObjectList(
if (body == null || body.length == 0) {
return null;
}
List<Class<?>> result = mapper.readerForListOf(targetType).readValue(body);
List<U> result = mapper.readerForListOf(targetType).readValue(body);
return result;
} catch (IOException | InterruptedException e) {
throw new TechnicalException("Failed to retrieve response due to error", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@
import de.digitalcollections.model.exception.TechnicalException;
import de.digitalcollections.model.identifiable.entity.item.Item;
import de.digitalcollections.model.identifiable.entity.manifestation.Manifestation;
import de.digitalcollections.model.list.ListRequest;
import de.digitalcollections.model.list.ListResponse;
import de.digitalcollections.model.list.paging.PageRequest;
import de.digitalcollections.model.list.paging.PageResponse;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.http.HttpClient;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

public class CudamiManifestationsClient extends CudamiEntitiesClient<Manifestation> {

@SuppressFBWarnings(value = "SS_SHOULD_BE_STATIC", justification = "non-static is fine, though")
private final String digiPressBaseEndpoint = API_VERSION_PREFIX + "/digipress";
daforster marked this conversation as resolved.
Show resolved Hide resolved

public CudamiManifestationsClient(HttpClient http, String serverUrl, ObjectMapper mapper) {
super(http, serverUrl, Manifestation.class, mapper, API_VERSION_PREFIX + "/manifestations");
}
Expand All @@ -34,4 +40,13 @@ public List<Locale> getLanguagesOfItems(UUID uuid) throws TechnicalException {
return doGetRequestForObjectList(
String.format("%s/%s/items/languages", baseEndpoint, uuid), Locale.class);
}

public ListResponse<Manifestation, ListRequest> findNewspapers(ListRequest listRequest)
throws TechnicalException {
return doGetRequestForObjectListResponse(digiPressBaseEndpoint, targetType, listRequest);
}

public void refreshNewspapers() throws TechnicalException {
doPutRequestForObject(digiPressBaseEndpoint + "/refresh", null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import de.digitalcollections.cudami.client.identifiable.entity.CudamiEntitiesClient;
import de.digitalcollections.model.exception.TechnicalException;
import de.digitalcollections.model.identifiable.entity.agent.Agent;
import de.digitalcollections.model.identifiable.entity.digitalobject.DigitalObject;
import de.digitalcollections.model.identifiable.entity.manifestation.Manifestation;
import de.digitalcollections.model.identifiable.entity.work.Work;
import de.digitalcollections.model.list.paging.PageRequest;
Expand All @@ -23,8 +22,7 @@ public CudamiWorksClient(HttpClient http, String serverUrl, ObjectMapper mapper)

public Set<Agent> getCreators(UUID uuid) throws TechnicalException {
return (Set<Agent>)
doGetRequestForObjectList(
String.format("%s/%s/creators", baseEndpoint, uuid), DigitalObject.class);
doGetRequestForObjectList(String.format("%s/%s/creators", baseEndpoint, uuid), Agent.class);
}

public PageResponse<Work> findChildren(UUID uuid, PageRequest pageRequest)
Expand Down
2 changes: 1 addition & 1 deletion metasvc-lobid-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.dbmdz.metadata</groupId>
<artifactId>metasvc</artifactId>
<version>9.2.2-SNAPSHOT</version>
<version>9.3.0-SNAPSHOT</version>
</parent>

<name>Metadata-Service: lobid.org Client</name>
Expand Down
2 changes: 1 addition & 1 deletion metasvc-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.dbmdz.metadata</groupId>
<artifactId>metasvc</artifactId>
<version>9.2.2-SNAPSHOT</version>
<version>9.3.0-SNAPSHOT</version>
</parent>

<name>Metadata-Service: Model</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public ListRequest(Sorting sorting) {
this(sorting, null, null);
}

public ListRequest(Filtering filtering) {
this(null, filtering, null);
}

/**
* Creates a new {@link ListRequest} with sorting parameters applied.
*
Expand Down
2 changes: 1 addition & 1 deletion metasvc-server/backend-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.dbmdz.metadata</groupId>
<artifactId>metasvc-server</artifactId>
<version>9.2.2-SNAPSHOT</version>
<version>9.3.0-SNAPSHOT</version>
</parent>

<name>Metadata-Service: Repository Server (Backend API)</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.dbmdz.metadata.server.backend.api.repository.identifiable.entity.work;

import de.digitalcollections.model.identifiable.entity.manifestation.Manifestation;
import de.digitalcollections.model.list.ListRequest;
import de.digitalcollections.model.list.ListResponse;
import io.github.dbmdz.metadata.server.backend.api.repository.exceptions.RepositoryException;

public interface DigipressManifestationRepository {

void refreshTable();

ListResponse<Manifestation, ListRequest> getNewspapers(ListRequest listRequest)
throws RepositoryException;
}
2 changes: 1 addition & 1 deletion metasvc-server/backend-file/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.github.dbmdz.metadata</groupId>
<artifactId>metasvc-server</artifactId>
<version>9.2.2-SNAPSHOT</version>
<version>9.3.0-SNAPSHOT</version>
</parent>

<name>Metadata-Service: Repository Server (Backend IMPL File)</name>
Expand Down
2 changes: 1 addition & 1 deletion metasvc-server/backend-inmemory/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.dbmdz.metadata</groupId>
<artifactId>metasvc-server</artifactId>
<version>9.2.2-SNAPSHOT</version>
<version>9.3.0-SNAPSHOT</version>
</parent>

<name>Metadata-Service: Repository Server (Backend IMPL InMemory)</name>
Expand Down
2 changes: 1 addition & 1 deletion metasvc-server/backend-jdbi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.dbmdz.metadata</groupId>
<artifactId>metasvc-server</artifactId>
<version>9.2.2-SNAPSHOT</version>
<version>9.3.0-SNAPSHOT</version>
</parent>

<name>Metadata-Service: Repository Server (Backend IMPL JDBI PostgreSql)</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ private void addOffset(PageRequest pageRequest, StringBuilder sqlQuery) {
}
}

protected void addOrderBy(PageRequest pageRequest, StringBuilder sqlQuery) {
if (pageRequest != null) {
// Sorting
Sorting sorting = pageRequest.getSorting();
String orderBy = getOrderBy(sorting);
if (StringUtils.hasText(orderBy)) {
if (!sqlQuery.toString().matches("(?i).* order by .*")) {
sqlQuery.append(" ORDER BY ");
} else {
sqlQuery.append(", ");
}
sqlQuery.append(orderBy);
}
protected void addOrderBy(Sorting sorting, StringBuilder sqlQuery) {
Optional<String> orderBy =
Optional.ofNullable(sorting).map(this::getOrderBy).filter(StringUtils::hasText);
if (orderBy.isEmpty()) return;
if (!sqlQuery.toString().matches("(?i).* order by .*")) {
sqlQuery.append(" ORDER BY ");
} else {
sqlQuery.append(", ");
}
sqlQuery.append(orderBy.get());
}

protected void addOrderBy(PageRequest pageRequest, StringBuilder sqlQuery) {
if (pageRequest != null) addOrderBy(pageRequest.getSorting(), sqlQuery);
}

/*
Expand Down
Loading