Skip to content

Commit

Permalink
improve update method to replace the old annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
SrdjanStevanetic committed Feb 10, 2025
1 parent 90c5ca8 commit 91d1430
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.europeana.annotation.tests.web;

import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

Expand Down Expand Up @@ -75,6 +76,8 @@ public void updateAnnotation() throws Exception {
assertNotNull(response.getBody());
assertEquals(TAG_STANDARD_TEST_VALUE_BODY, updatedAnnotation.getBody().getValue());
assertEquals(get_TAG_STANDARD_TEST_VALUE_TARGET(AnnotationTestsConfiguration.getInstance().getPropAnnotationItemDataEndpoint()), updatedAnnotation.getTarget().get(0).getHttpUri());
assertNotNull(anno.getEquivalentTo());
assertNull(updatedAnnotation.getEquivalentTo());

addToCreatedAnnotations(anno.getIdentifier());
//TODO: search annotation in solr and verify body and target values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ protected void validateAnnotationIdForModerationRecord(ModerationRecord newModer
@Override
public Annotation updateAnnotation(PersistentAnnotation persistentAnnotation,
Annotation webAnnotation) throws AnnotationServiceException, HttpException {
mergeAnnotationProperties(persistentAnnotation, webAnnotation);
replaceAnnotationProperties(persistentAnnotation, webAnnotation);
// check that the updated annotation is unique
Set<String> duplicateAnnotationIds = checkDuplicateAnnotations(persistentAnnotation, true);
if (!duplicateAnnotationIds.isEmpty()) {
Expand All @@ -223,64 +223,31 @@ public Annotation updateAnnotation(PersistentAnnotation persistentAnnotation,
}

@SuppressWarnings("deprecation")
private void mergeAnnotationProperties(PersistentAnnotation annotation,
private void replaceAnnotationProperties(PersistentAnnotation annotation,
Annotation webAnnotation) {
if (webAnnotation.getType() != null) {
annotation.setType(webAnnotation.getType());
}

if (webAnnotation.getGenerated() != null) {
annotation.setType(webAnnotation.getType());
if(webAnnotation.getGenerated() != null) {
annotation.setGenerated(webAnnotation.getGenerated());
}
annotation.setBody(webAnnotation.getBody());
annotation.setTarget(webAnnotation.getTarget());
annotation.setDisabled(webAnnotation.getDisabled());
annotation.setEquivalentTo(webAnnotation.getEquivalentTo());
annotation.setInternalType(webAnnotation.getInternalType());
annotation.setStatus(webAnnotation.getStatus());
annotation.setStyledBy(webAnnotation.getStyledBy());

if (webAnnotation.getBody() != null) {
annotation.setBody(webAnnotation.getBody());
}

if (webAnnotation.getTarget() != null) {
annotation.setTarget(webAnnotation.getTarget());
}

if (annotation.isDisabled() != webAnnotation.isDisabled()) {
annotation.setDisabled(webAnnotation.getDisabled());
}

if (webAnnotation.getEquivalentTo() != null) {
annotation.setEquivalentTo(webAnnotation.getEquivalentTo());
}
if (webAnnotation.getInternalType() != null) {
annotation.setInternalType(webAnnotation.getInternalType());
}

if (webAnnotation.getStatus() != null) {
annotation.setStatus(webAnnotation.getStatus());
}

if (webAnnotation.getStyledBy() != null) {
annotation.setStyledBy(webAnnotation.getStyledBy());
}

mergeReferenceFields(annotation, webAnnotation);
mergeOrSetLastUpdate(annotation, webAnnotation);
replaceReferenceFields(annotation, webAnnotation);
replaceOrSetLastUpdate(annotation, webAnnotation);
}

private void mergeReferenceFields(PersistentAnnotation annotation, Annotation webAnnotation) {
if (webAnnotation.getSameAs() != null) {
annotation.setSameAs(webAnnotation.getSameAs());
}

if (webAnnotation.getCanonical() != null) {
// TODO: #404 must never be overwritten
if (StringUtils.isEmpty(annotation.getCanonical())) {
annotation.setCanonical(webAnnotation.getCanonical());
}
}
if (webAnnotation.getVia() != null) {
annotation.setVia(webAnnotation.getVia());
}
private void replaceReferenceFields(PersistentAnnotation annotation, Annotation webAnnotation) {
annotation.setSameAs(webAnnotation.getSameAs());
annotation.setCanonical(webAnnotation.getCanonical());
annotation.setVia(webAnnotation.getVia());
}

private void mergeOrSetLastUpdate(PersistentAnnotation annotation, Annotation webAnnotation) {
private void replaceOrSetLastUpdate(PersistentAnnotation annotation, Annotation webAnnotation) {
// So my decision for the moment would be to only keep the "id" and "created" immutable.
//
// With regards to the logic when each of the fields is missing:
Expand Down Expand Up @@ -504,7 +471,7 @@ public void updateExistingAnnotations(BatchReportable batchReportable,
continue;

// merge update annotation (web anno) into existing annotation (db anno)
this.mergeAnnotationProperties((PersistentAnnotation) existingAnno, updateAnno);
this.replaceAnnotationProperties((PersistentAnnotation) existingAnno, updateAnno);

// set last update
existingAnno.setLastUpdate(new Date());
Expand Down

0 comments on commit 91d1430

Please sign in to comment.