1 package de.dlr.shepard.context.semantic.endpoints;
2
3 import de.dlr.shepard.common.exceptions.InvalidPathException;
4 import de.dlr.shepard.common.neo4j.entities.BasicEntity;
5 import de.dlr.shepard.context.semantic.io.SemanticAnnotationIO;
6 import de.dlr.shepard.context.semantic.services.SemanticAnnotationService;
7 import io.quarkus.logging.Log;
8 import jakarta.inject.Inject;
9 import jakarta.ws.rs.core.Response;
10 import jakarta.ws.rs.core.Response.Status;
11 import java.util.ArrayList;
12
13 public abstract class SemanticAnnotationRest {
14
15 @Inject
16 SemanticAnnotationService semanticAnnotationService;
17
18 protected Response getAllByShepardId(long shepardId) {
19 var annotations = semanticAnnotationService.getAllAnnotationsByShepardId(shepardId);
20 var result = new ArrayList<SemanticAnnotationIO>(annotations.size());
21 for (var reference : annotations) {
22 result.add(new SemanticAnnotationIO(reference));
23 }
24 return Response.ok(result).build();
25 }
26
27 protected Response getAllByNeo4jId(long shepardId) {
28 var annotations = semanticAnnotationService.getAllAnnotationsByNeo4jId(shepardId);
29 var result = new ArrayList<SemanticAnnotationIO>(annotations.size());
30 for (var reference : annotations) {
31 result.add(new SemanticAnnotationIO(reference));
32 }
33 return Response.ok(result).build();
34 }
35
36 protected Response get(long semanticAnnotationId) {
37 var result = semanticAnnotationService.getAnnotationByNeo4jId(semanticAnnotationId);
38 return Response.ok(new SemanticAnnotationIO(result)).build();
39 }
40
41 protected Response createByNeo4jId(long entityNeo4jId, SemanticAnnotationIO annotation) {
42 var result = semanticAnnotationService.createAnnotationByNeo4jId(entityNeo4jId, annotation);
43 return Response.ok(new SemanticAnnotationIO(result)).status(Status.CREATED).build();
44 }
45
46 protected Response createByShepardId(long entityShepardId, SemanticAnnotationIO semanticAnnotation) {
47 var result = semanticAnnotationService.createAnnotationByShepardId(entityShepardId, semanticAnnotation);
48 return Response.ok(new SemanticAnnotationIO(result)).status(Status.CREATED).build();
49 }
50
51 protected Response delete(long semanticAnnotationId) {
52 var result = semanticAnnotationService.deleteAnnotationByNeo4jId(semanticAnnotationId);
53 return result ? Response.status(Status.NO_CONTENT).build() : Response.status(Status.INTERNAL_SERVER_ERROR).build();
54 }
55
56
57
58
59
60
61
62
63 protected void assertSemanticAnnotationBelongsToEntity(BasicEntity entity, long semanticAnnotationId) {
64 semanticAnnotationService.getAnnotationByNeo4jId(semanticAnnotationId);
65 if (
66 entity
67 .getAnnotations()
68 .stream()
69 .filter(annotation -> annotation.getId().equals(semanticAnnotationId))
70 .findFirst()
71 .isEmpty()
72 ) {
73 String errorMsg = "ID ERROR - There is no association between annotation and entity";
74 Log.error(errorMsg);
75 throw new InvalidPathException(errorMsg);
76 }
77 }
78 }