1 package de.dlr.shepard.context.semantic.endpoints;
2
3 import de.dlr.shepard.common.filters.Subscribable;
4 import de.dlr.shepard.common.util.Constants;
5 import de.dlr.shepard.context.collection.entities.Collection;
6 import de.dlr.shepard.context.collection.services.CollectionService;
7 import de.dlr.shepard.context.semantic.io.SemanticAnnotationIO;
8 import jakarta.enterprise.context.RequestScoped;
9 import jakarta.inject.Inject;
10 import jakarta.validation.Valid;
11 import jakarta.validation.constraints.NotNull;
12 import jakarta.validation.constraints.PositiveOrZero;
13 import jakarta.ws.rs.Consumes;
14 import jakarta.ws.rs.DELETE;
15 import jakarta.ws.rs.GET;
16 import jakarta.ws.rs.POST;
17 import jakarta.ws.rs.Path;
18 import jakarta.ws.rs.PathParam;
19 import jakarta.ws.rs.Produces;
20 import jakarta.ws.rs.core.MediaType;
21 import jakarta.ws.rs.core.Response;
22 import org.eclipse.microprofile.openapi.annotations.Operation;
23 import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
24 import org.eclipse.microprofile.openapi.annotations.media.Content;
25 import org.eclipse.microprofile.openapi.annotations.media.Schema;
26 import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
27 import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
28 import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
29 import org.eclipse.microprofile.openapi.annotations.tags.Tag;
30
31 @Consumes(MediaType.APPLICATION_JSON)
32 @Produces(MediaType.APPLICATION_JSON)
33 @Path(Constants.COLLECTIONS + "/{" + Constants.COLLECTION_ID + "}/" + Constants.SEMANTIC_ANNOTATIONS)
34 @RequestScoped
35 public class CollectionSemanticAnnotationRest extends SemanticAnnotationRest {
36
37 @Inject
38 CollectionService collectionService;
39
40 @GET
41 @Tag(name = Constants.SEMANTIC_ANNOTATION)
42 @APIResponse(
43 description = "ok",
44 responseCode = "200",
45 content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = SemanticAnnotationIO.class))
46 )
47 @APIResponse(responseCode = "400", description = "bad request")
48 @APIResponse(responseCode = "401", description = "not authorized")
49 @APIResponse(responseCode = "403", description = "forbidden")
50 @APIResponse(responseCode = "404", description = "not found")
51 @Operation(operationId = "getAllCollectionAnnotations", description = "Get all semantic annotations")
52 @Parameter(name = Constants.COLLECTION_ID)
53 public Response getAllAnnotations(@PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId) {
54 collectionService.getCollection(collectionId);
55 return getAllByShepardId(collectionId);
56 }
57
58 @GET
59 @Tag(name = Constants.SEMANTIC_ANNOTATION)
60 @APIResponse(
61 description = "ok",
62 responseCode = "200",
63 content = @Content(schema = @Schema(implementation = SemanticAnnotationIO.class))
64 )
65 @APIResponse(responseCode = "400", description = "bad request")
66 @APIResponse(responseCode = "401", description = "not authorized")
67 @APIResponse(responseCode = "403", description = "forbidden")
68 @APIResponse(responseCode = "404", description = "not found")
69 @Path("{" + Constants.SEMANTIC_ANNOTATION_ID + "}")
70 @Operation(operationId = "getCollectionAnnotation", description = "Get semantic annotation")
71 @Parameter(name = Constants.COLLECTION_ID)
72 @Parameter(name = Constants.SEMANTIC_ANNOTATION_ID)
73 public Response getAnnotation(
74 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
75 @PathParam(Constants.SEMANTIC_ANNOTATION_ID) @NotNull @PositiveOrZero Long semanticAnnotationId
76 ) {
77
78 Collection collection = collectionService.getCollectionWithDataObjectsAndIncomingReferences(collectionId);
79
80 assertSemanticAnnotationBelongsToEntity(collection, semanticAnnotationId);
81 return get(semanticAnnotationId);
82 }
83
84 @POST
85 @Tag(name = Constants.SEMANTIC_ANNOTATION)
86 @APIResponse(
87 description = "created",
88 responseCode = "201",
89 content = @Content(schema = @Schema(implementation = SemanticAnnotationIO.class))
90 )
91 @APIResponse(responseCode = "400", description = "bad request")
92 @APIResponse(responseCode = "401", description = "not authorized")
93 @APIResponse(responseCode = "403", description = "forbidden")
94 @APIResponse(responseCode = "404", description = "not found")
95 @Subscribable
96 @Operation(operationId = "createCollectionAnnotation", description = "Create a new semantic annotation")
97 @Parameter(name = Constants.COLLECTION_ID)
98 public Response createAnnotation(
99 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
100 @RequestBody(
101 required = true,
102 content = @Content(schema = @Schema(implementation = SemanticAnnotationIO.class))
103 ) @Valid SemanticAnnotationIO semanticAnnotation
104 ) {
105 collectionService.getCollection(collectionId);
106 collectionService.assertIsAllowedToEditCollection(collectionId);
107 return createByShepardId(collectionId, semanticAnnotation);
108 }
109
110 @DELETE
111 @Tag(name = Constants.SEMANTIC_ANNOTATION)
112 @APIResponse(description = "deleted", responseCode = "204")
113 @APIResponse(responseCode = "400", description = "bad request")
114 @APIResponse(responseCode = "401", description = "not authorized")
115 @APIResponse(responseCode = "403", description = "forbidden")
116 @APIResponse(responseCode = "404", description = "not found")
117 @Path("{" + Constants.SEMANTIC_ANNOTATION_ID + "}")
118 @Subscribable
119 @Operation(operationId = "deleteCollectionAnnotation", description = "Delete semantic annotation")
120 @Parameter(name = Constants.COLLECTION_ID)
121 @Parameter(name = Constants.SEMANTIC_ANNOTATION_ID)
122 public Response deleteAnnotation(
123 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
124 @PathParam(Constants.SEMANTIC_ANNOTATION_ID) @NotNull @PositiveOrZero Long semanticAnnotationId
125 ) {
126 Collection collection = collectionService.getCollectionWithDataObjectsAndIncomingReferences(collectionId);
127 collectionService.assertIsAllowedToEditCollection(collectionId);
128 assertSemanticAnnotationBelongsToEntity(collection, semanticAnnotationId);
129 return delete(semanticAnnotationId);
130 }
131 }