Skip to content

Commit

Permalink
Merge branch 'manifestation-backend-impl' into 'main'
Browse files Browse the repository at this point in the history
  • Loading branch information
daforster committed Nov 15, 2022
2 parents 685fc1a + d3a5ca3 commit beed7f0
Show file tree
Hide file tree
Showing 52 changed files with 1,180 additions and 160 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package de.digitalcollections.cudami.client.relation;

import static de.digitalcollections.cudami.client.CudamiRestClient.API_VERSION_PREFIX;

import com.fasterxml.jackson.databind.ObjectMapper;
import de.digitalcollections.client.BaseRestClient;
import de.digitalcollections.cudami.client.CudamiRestClient;
import de.digitalcollections.model.exception.TechnicalException;
import de.digitalcollections.model.relation.Predicate;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class CudamiPredicatesClient extends BaseRestClient<Predicate> {
public class CudamiPredicatesClient extends CudamiRestClient<Predicate> {

public CudamiPredicatesClient(HttpClient http, String serverUrl, ObjectMapper mapper) {
super(http, serverUrl, Predicate.class, mapper, API_VERSION_PREFIX + "/predicates");
Expand All @@ -21,10 +19,21 @@ public List<Predicate> getAll() throws TechnicalException {
return doGetRequestForObjectList(baseEndpoint, Predicate.class);
}

public Predicate save(Predicate predicate) throws TechnicalException {
return doPutRequestForObject(
String.format(
"%s/%s", baseEndpoint, URLEncoder.encode(predicate.getValue(), StandardCharsets.UTF_8)),
predicate);
public Predicate update(Predicate predicate) throws TechnicalException {
if (predicate.getUuid() == null) {
// Old consumers don't set the UUID, we must provide the value in the request path
return doPutRequestForObject(
String.format(
"%s/%s",
baseEndpoint, URLEncoder.encode(predicate.getValue(), StandardCharsets.UTF_8)),
predicate);
}

return super.update(predicate.getUuid(), predicate);
}

public Predicate getByValue(String value) throws TechnicalException {
return doGetRequestForObject(
String.format("%s/%s", baseEndpoint, URLEncoder.encode(value, StandardCharsets.UTF_8)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.digitalcollections.cudami.client.relation;

import de.digitalcollections.cudami.client.BaseCudamiRestClientTest;
import de.digitalcollections.model.relation.Predicate;
import java.util.UUID;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("The PredicatesClient")
class CudamiPredicatesClientTest
extends BaseCudamiRestClientTest<Predicate, CudamiPredicatesClient> {

@Test
@DisplayName("can update without uuid (old behaviour)")
public void testUpdateWithoutUuid() throws Exception {
Predicate predicate = Predicate.builder().value("foo").build();
client.update(predicate);

verifyHttpRequestByMethodRelativeUrlAndRequestBody("put", "/foo", predicate);
}

@Test
@DisplayName("can update with uuid")
public void testUpdateWithUuid() throws Exception {
UUID uuid = UUID.randomUUID();
Predicate predicate = Predicate.builder().value("foo").uuid(uuid).build();
client.update(uuid, predicate);

verifyHttpRequestByMethodRelativeUrlAndRequestBody("put", "/" + uuid, predicate);
}

@Test
@DisplayName("can find all")
public void testFindAll() throws Exception {
client.getAll();
verifyHttpRequestByMethodAndRelativeURL("get", "");
}
}
6 changes: 6 additions & 0 deletions dc-cudami-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
<artifactId>dc-cudami-model</artifactId>
<packaging>jar</packaging>

<properties>
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.github.spotbugs</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.digitalcollections.cudami.server.backend.api.repository.identifiable.entity.relation;

import de.digitalcollections.cudami.server.backend.api.repository.exceptions.RepositoryException;
import de.digitalcollections.model.identifiable.entity.Entity;
import de.digitalcollections.model.identifiable.entity.relation.EntityRelation;
import de.digitalcollections.model.list.paging.PageRequest;
Expand All @@ -16,6 +17,12 @@ default void addRelation(EntityRelation relation) {

void addRelation(UUID subjectEntityUuid, String predicate, UUID objectEntityUuid);

default void deleteByObject(Entity objectEntity) {
deleteByObject(objectEntity.getUuid());
}

void deleteByObject(UUID objectEntityUuid);

default void deleteBySubject(Entity subjectEntity) {
deleteBySubject(subjectEntity.getUuid());
}
Expand Down Expand Up @@ -47,6 +54,7 @@ default void save(EntityRelation relation) {
*
* @param entityRelations list of entity-predicate-entity relations to be persisted
* @return list of persisted EntityRelations
* @throws RepositoryException in case of an error, e.g. a referenced predicate does not yet exist
*/
List<EntityRelation> save(List<EntityRelation> entityRelations);
List<EntityRelation> save(List<EntityRelation> entityRelations) throws RepositoryException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,29 @@ public PageResponse<Publisher> find(PageRequest pageRequest) throws RepositoryEx
// order of
// array items
+ " LEFT JOIN "
+ corporateBodyRepository.tableName
+ corporateBodyRepository.getTableName()
+ " "
+ corporateBodyRepository.tableAlias
+ corporateBodyRepository.getTableAlias()
+ " ON "
+ corporateBodyRepository.tableAlias
+ corporateBodyRepository.getTableAlias()
+ ".uuid="
+ tableAlias
+ ".agent_uuid"
+ " LEFT JOIN "
+ personRepository.tableName
+ personRepository.getTableName()
+ " "
+ personRepository.tableAlias
+ personRepository.getTableAlias()
+ " ON "
+ personRepository.tableAlias
+ personRepository.getTableAlias()
+ ".uuid="
+ tableAlias
+ ".agent_uuid"
+ " LEFT JOIN "
+ humanSettlementRepository.tableName
+ humanSettlementRepository.getTableName()
+ " "
+ humanSettlementRepository.tableAlias
+ humanSettlementRepository.getTableAlias()
+ " ON "
+ humanSettlementRepository.tableAlias
+ humanSettlementRepository.getTableAlias()
+ ".uuid=l.id");

Map argumentMappings = new HashMap<>();
Expand Down Expand Up @@ -150,29 +150,29 @@ public Publisher getByUuid(UUID uuid) throws RepositoryException {
+ " "
+ tableAlias
+ " LEFT JOIN "
+ corporateBodyRepository.tableName
+ corporateBodyRepository.getTableName()
+ " "
+ corporateBodyRepository.tableAlias
+ corporateBodyRepository.getTableAlias()
+ " ON "
+ corporateBodyRepository.tableAlias
+ corporateBodyRepository.getTableAlias()
+ ".uuid="
+ tableAlias
+ ".agent_uuid"
+ " LEFT JOIN "
+ personRepository.tableName
+ personRepository.getTableName()
+ " "
+ personRepository.tableAlias
+ personRepository.getTableAlias()
+ " ON "
+ personRepository.tableAlias
+ personRepository.getTableAlias()
+ ".uuid="
+ tableAlias
+ ".agent_uuid"
+ " LEFT JOIN "
+ humanSettlementRepository.tableName
+ humanSettlementRepository.getTableName()
+ " "
+ humanSettlementRepository.tableAlias
+ humanSettlementRepository.getTableAlias()
+ " ON "
+ humanSettlementRepository.tableAlias
+ humanSettlementRepository.getTableAlias()
+ ".uuid="
+ " ANY("
+ tableAlias
Expand Down Expand Up @@ -208,18 +208,19 @@ public Publisher getByUuid(UUID uuid) throws RepositoryException {
});

UUID corporateBodyUuid =
rowView.getColumn(corporateBodyRepository.mappingPrefix + "_uuid", UUID.class);
rowView.getColumn(corporateBodyRepository.getMappingPrefix() + "_uuid", UUID.class);
if (corporateBodyUuid != null) {
publisher.setAgent(rowView.getRow(CorporateBody.class));
}

UUID personUuid = rowView.getColumn(personRepository.mappingPrefix + "_uuid", UUID.class);
UUID personUuid =
rowView.getColumn(personRepository.getMappingPrefix() + "_uuid", UUID.class);
if (personUuid != null) {
publisher.setAgent(rowView.getRow(Person.class));
}

UUID locationUuid =
rowView.getColumn(humanSettlementRepository.mappingPrefix + "_uuid", UUID.class);
rowView.getColumn(humanSettlementRepository.getMappingPrefix() + "_uuid", UUID.class);
if (locationUuid != null) {
publisher.addLocation(rowView.getRow(HumanSettlement.class));
}
Expand Down Expand Up @@ -338,11 +339,9 @@ public String getColumnName(String modelProperty) {
case "uuid":
return tableAlias + ".uuid";
case "agent_uuid":
return tableAlias + ".agent_uuid::varchar";
case "location_uuid":
return tableAlias + ".location_uuids::varchar";
return tableAlias + ".agent_uuid::UUID";
case "location_uuids":
return tableAlias + ".location_uuids::varchar[]";
return tableAlias + ".location_uuids::UUID[]";
case "publisherPresentation":
return tableAlias + ".publisherPresentation";
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ protected String getWhereClause(
return super.getWhereClause(fc, argumentMappings, criterionCount);
}

@Override
public long retrieveCount(StringBuilder sqlCount, final Map<String, Object> argumentMappings) {
long total =
dbi.withHandle(
Expand Down Expand Up @@ -795,6 +796,7 @@ public I retrieveOne(
+ (StringUtils.hasText(innerSelect) ? innerSelect : tableName)
+ " AS "
+ tableAlias
+ " "
+ (sqlSelectAllFieldsJoins != null ? sqlSelectAllFieldsJoins : "")
+ " LEFT JOIN "
+ IdentifierRepositoryImpl.TABLE_NAME
Expand Down Expand Up @@ -847,6 +849,13 @@ public I retrieveOne(

@Override
public I save(I identifiable, Map<String, Object> bindings) {
return save(identifiable, bindings, null);
}

public I save(
I identifiable,
Map<String, Object> bindings,
BiFunction<String, Map<String, Object>, String> sqlModifier) {
if (bindings == null) {
bindings = new HashMap<>(0);
}
Expand All @@ -857,7 +866,6 @@ public I save(I identifiable, Map<String, Object> bindings) {
// split label
bindings.put("split_label", splitToArray(identifiable.getLabel()));
bindings.put("tags_uuids", extractUuids(identifiable.getTags()));
final Map<String, Object> finalBindings = new HashMap<>(bindings);
if (identifiable.getUuid() == null) {
// in case of fileresource the uuid is created on binary upload (before metadata save)
// to make saving on storage using uuid is possible
Expand All @@ -870,17 +878,25 @@ public I save(I identifiable, Map<String, Object> bindings) {
identifiable.setLastModified(LocalDateTime.now());
}

final String sql =
final String finalSql;
String sql =
"INSERT INTO "
+ tableName
+ "("
+ getSqlInsertFields()
+ ") VALUES ("
+ getSqlInsertValues()
+ ")";
if (sqlModifier != null) {
finalSql = sqlModifier.apply(sql, bindings);
} else {
finalSql = sql;
}

final Map<String, Object> finalBindings = new HashMap<>(bindings);

dbi.withHandle(
h -> h.createUpdate(sql).bindMap(finalBindings).bindBean(identifiable).execute());
h -> h.createUpdate(finalSql).bindMap(finalBindings).bindBean(identifiable).execute());

return identifiable;
}
Expand Down Expand Up @@ -970,6 +986,13 @@ protected boolean supportsCaseSensitivityForProperty(String modelProperty) {

@Override
public I update(I identifiable, Map<String, Object> bindings) {
return update(identifiable, bindings, null);
}

public I update(
I identifiable,
Map<String, Object> bindings,
BiFunction<String, Map<String, Object>, String> sqlModifier) {
if (bindings == null) {
bindings = new HashMap<>(0);
}
Expand All @@ -979,17 +1002,23 @@ public I update(I identifiable, Map<String, Object> bindings) {
// split label
bindings.put("split_label", splitToArray(identifiable.getLabel()));
bindings.put("tags_uuids", extractUuids(identifiable.getTags()));
final Map<String, Object> finalBindings = new HashMap<>(bindings);

identifiable.setLastModified(LocalDateTime.now());
// do not update/left out from statement (not changed since insert):
// uuid, created, identifiable_type, identifiable_objecttype, refid

final String sql =
"UPDATE " + tableName + " SET" + getSqlUpdateFieldValues() + " WHERE uuid=:uuid";
final String finalSql;
String sql = "UPDATE " + tableName + " SET" + getSqlUpdateFieldValues() + " WHERE uuid=:uuid";

if (sqlModifier != null) {
finalSql = sqlModifier.apply(sql, bindings);
} else {
finalSql = sql;
}

final Map<String, Object> finalBindings = new HashMap<>(bindings);
dbi.withHandle(
h -> h.createUpdate(sql).bindMap(finalBindings).bindBean(identifiable).execute());
h -> h.createUpdate(finalSql).bindMap(finalBindings).bindBean(identifiable).execute());

return identifiable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

// TODO: Neues Feld additional_predicates: UUID[] einführen und befüllen.

@Repository
public class EntityRelationRepositoryImpl extends JdbiRepositoryImpl
implements EntityRelationRepository {
Expand All @@ -45,6 +47,15 @@ public void addRelation(UUID subjectEntityUuid, String predicate, UUID objectEnt
save(subjectEntityUuid, predicate, objectEntityUuid);
}

@Override
public void deleteByObject(UUID objectEntityUuid) {
dbi.withHandle(
h ->
h.createUpdate("DELETE FROM " + tableName + " WHERE object_uuid = :uuid")
.bind("uuid", objectEntityUuid)
.execute());
}

@Override
public void deleteBySubject(UUID subjectEntityUuid) {
dbi.withHandle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ public class SubjectRepositoryImpl extends JdbiRepositoryImpl implements Subject
TABLE_ALIAS, MAPPING_PREFIX);
public static final String SQL_FULL_FIELDS_SUBJECTS = SQL_REDUCED_FIELDS_SUBJECTS;

public SubjectRepositoryImpl(Jdbi dbi, CudamiConfig cudamiConfig) {
public SubjectRepositoryImpl(
Jdbi dbi, CudamiConfig cudamiConfig, DbIdentifierMapper dbIdentifierMapper) {
super(
dbi, TABLE_NAME, TABLE_ALIAS, MAPPING_PREFIX, cudamiConfig.getOffsetForAlternativePaging());
this.dbi.registerRowMapper(BeanMapper.factory(Subject.class, MAPPING_PREFIX));
this.dbi.registerArrayType(new DbIdentifierMapper());
this.dbi.registerColumnMapper(Identifier.class, new DbIdentifierMapper());
this.dbi.registerArrayType(dbIdentifierMapper);
this.dbi.registerColumnMapper(Identifier.class, dbIdentifierMapper);
}

@Override
Expand Down
Loading

0 comments on commit beed7f0

Please sign in to comment.