View Javadoc
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 get(long semanticAnnotationId) {
28      var result = semanticAnnotationService.getAnnotationByNeo4jId(semanticAnnotationId);
29      return Response.ok(new SemanticAnnotationIO(result)).build();
30    }
31  
32    protected Response createByShepardId(long entityShepardId, SemanticAnnotationIO semanticAnnotation) {
33      var result = semanticAnnotationService.createAnnotationByShepardId(entityShepardId, semanticAnnotation);
34      return Response.ok(new SemanticAnnotationIO(result)).status(Status.CREATED).build();
35    }
36  
37    protected Response delete(long semanticAnnotationId) {
38      var result = semanticAnnotationService.deleteAnnotationByNeo4jId(semanticAnnotationId);
39      return result ? Response.status(Status.NO_CONTENT).build() : Response.status(Status.INTERNAL_SERVER_ERROR).build();
40    }
41  
42    /**
43     * Assert that provided semantic annotation id actually belongs to this BasicEntity (collection, ...)
44     *
45     * @param entity
46     * @param semanticAnnotationId
47     * @throws InvalidPathException
48     */
49    protected void assertSemanticAnnotationBelongsToEntity(BasicEntity entity, long semanticAnnotationId) {
50      semanticAnnotationService.getAnnotationByNeo4jId(semanticAnnotationId);
51      if (
52        !entity
53          .getAnnotations()
54          .stream()
55          .filter(annotation -> annotation.getId().equals(semanticAnnotationId))
56          .findFirst()
57          .isPresent()
58      ) {
59        String errorMsg = "ID ERROR - There is no association between annotation and entity";
60        Log.error(errorMsg);
61        throw new InvalidPathException(errorMsg);
62      }
63    }
64  }