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.DataObject;
6 import de.dlr.shepard.context.collection.services.CollectionService;
7 import de.dlr.shepard.context.collection.services.DataObjectService;
8 import de.dlr.shepard.context.semantic.io.SemanticAnnotationIO;
9 import jakarta.enterprise.context.RequestScoped;
10 import jakarta.inject.Inject;
11 import jakarta.validation.Valid;
12 import jakarta.validation.constraints.NotNull;
13 import jakarta.validation.constraints.PositiveOrZero;
14 import jakarta.ws.rs.Consumes;
15 import jakarta.ws.rs.DELETE;
16 import jakarta.ws.rs.GET;
17 import jakarta.ws.rs.POST;
18 import jakarta.ws.rs.Path;
19 import jakarta.ws.rs.PathParam;
20 import jakarta.ws.rs.Produces;
21 import jakarta.ws.rs.core.MediaType;
22 import jakarta.ws.rs.core.Response;
23 import org.eclipse.microprofile.openapi.annotations.Operation;
24 import org.eclipse.microprofile.openapi.annotations.enums.ParameterIn;
25 import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
26 import org.eclipse.microprofile.openapi.annotations.media.Content;
27 import org.eclipse.microprofile.openapi.annotations.media.Schema;
28 import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
29 import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
30 import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
31 import org.eclipse.microprofile.openapi.annotations.tags.Tag;
32
33 @Consumes(MediaType.APPLICATION_JSON)
34 @Produces(MediaType.APPLICATION_JSON)
35 @Path(
36 Constants.COLLECTIONS +
37 "/{" +
38 Constants.COLLECTION_ID +
39 "}/" +
40 Constants.DATA_OBJECTS +
41 "/{" +
42 Constants.DATA_OBJECT_ID +
43 "}/" +
44 Constants.SEMANTIC_ANNOTATIONS
45 )
46 @RequestScoped
47 public class DataObjectSemanticAnnotationRest extends SemanticAnnotationRest {
48
49 @Inject
50 CollectionService collectionService;
51
52 @Inject
53 DataObjectService dataObjectService;
54
55 @GET
56 @Tag(name = Constants.SEMANTIC_ANNOTATION)
57 @APIResponse(
58 description = "ok",
59 responseCode = "200",
60 content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = SemanticAnnotationIO.class))
61 )
62 @APIResponse(responseCode = "400", description = "bad request")
63 @APIResponse(responseCode = "401", description = "not authorized")
64 @APIResponse(responseCode = "403", description = "forbidden")
65 @APIResponse(responseCode = "404", description = "not found")
66 @Operation(operationId = "getAllDataObjectAnnotations", description = "Get all semantic annotations")
67 @Parameter(
68 in = ParameterIn.PATH,
69 name = Constants.COLLECTION_ID,
70 schema = @Schema(type = SchemaType.INTEGER, format = "int64")
71 )
72 @Parameter(name = Constants.DATA_OBJECT_ID)
73 public Response getAllAnnotations(
74 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
75 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId
76 ) {
77 dataObjectService.getDataObject(collectionId, dataObjectId);
78 return getAllByShepardId(dataObjectId);
79 }
80
81 @GET
82 @Tag(name = Constants.SEMANTIC_ANNOTATION)
83 @APIResponse(
84 description = "ok",
85 responseCode = "200",
86 content = @Content(schema = @Schema(implementation = SemanticAnnotationIO.class))
87 )
88 @APIResponse(responseCode = "400", description = "bad request")
89 @APIResponse(responseCode = "401", description = "not authorized")
90 @APIResponse(responseCode = "403", description = "forbidden")
91 @APIResponse(responseCode = "404", description = "not found")
92 @Path("{" + Constants.SEMANTIC_ANNOTATION_ID + "}")
93 @Operation(operationId = "getDataObjectAnnotation", description = "Get semantic annotation")
94 @Parameter(
95 in = ParameterIn.PATH,
96 name = Constants.COLLECTION_ID,
97 schema = @Schema(type = SchemaType.INTEGER, format = "int64")
98 )
99 @Parameter(
100 in = ParameterIn.PATH,
101 name = Constants.DATA_OBJECT_ID,
102 schema = @Schema(type = SchemaType.INTEGER, format = "int64")
103 )
104 @Parameter(name = Constants.SEMANTIC_ANNOTATION_ID)
105 public Response getAnnotation(
106 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
107 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
108 @PathParam(Constants.SEMANTIC_ANNOTATION_ID) @NotNull @PositiveOrZero Long semanticAnnotationId
109 ) {
110
111 DataObject dataObject = dataObjectService.getDataObject(collectionId, dataObjectId);
112
113 assertSemanticAnnotationBelongsToEntity(dataObject, semanticAnnotationId);
114 return get(semanticAnnotationId);
115 }
116
117 @POST
118 @Tag(name = Constants.SEMANTIC_ANNOTATION)
119 @APIResponse(
120 description = "created",
121 responseCode = "201",
122 content = @Content(schema = @Schema(implementation = SemanticAnnotationIO.class))
123 )
124 @APIResponse(responseCode = "400", description = "bad request")
125 @APIResponse(responseCode = "401", description = "not authorized")
126 @APIResponse(responseCode = "403", description = "forbidden")
127 @APIResponse(responseCode = "404", description = "not found")
128 @Subscribable
129 @Operation(operationId = "createDataObjectAnnotation", description = "Create a new semantic annotation")
130 @Parameter(
131 in = ParameterIn.PATH,
132 name = Constants.COLLECTION_ID,
133 schema = @Schema(type = SchemaType.INTEGER, format = "int64")
134 )
135 @Parameter(name = Constants.DATA_OBJECT_ID)
136 public Response createAnnotation(
137 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
138 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
139 @RequestBody(
140 required = true,
141 content = @Content(schema = @Schema(implementation = SemanticAnnotationIO.class))
142 ) @Valid SemanticAnnotationIO semanticAnnotation
143 ) {
144 dataObjectService.getDataObject(collectionId, dataObjectId);
145 collectionService.assertIsAllowedToEditCollection(collectionId);
146 return createByShepardId(dataObjectId, semanticAnnotation);
147 }
148
149 @DELETE
150 @Tag(name = Constants.SEMANTIC_ANNOTATION)
151 @APIResponse(description = "deleted", responseCode = "204")
152 @APIResponse(responseCode = "400", description = "bad request")
153 @APIResponse(responseCode = "401", description = "not authorized")
154 @APIResponse(responseCode = "403", description = "forbidden")
155 @APIResponse(responseCode = "404", description = "not found")
156 @Path("{" + Constants.SEMANTIC_ANNOTATION_ID + "}")
157 @Subscribable
158 @Operation(operationId = "deleteDataObjectAnnotation", description = "Delete semantic annotation")
159 @Parameter(
160 in = ParameterIn.PATH,
161 name = Constants.COLLECTION_ID,
162 schema = @Schema(type = SchemaType.INTEGER, format = "int64")
163 )
164 @Parameter(
165 in = ParameterIn.PATH,
166 name = Constants.DATA_OBJECT_ID,
167 schema = @Schema(type = SchemaType.INTEGER, format = "int64")
168 )
169 @Parameter(name = Constants.SEMANTIC_ANNOTATION_ID)
170 public Response deleteAnnotation(
171 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
172 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
173 @PathParam(Constants.SEMANTIC_ANNOTATION_ID) @NotNull @PositiveOrZero Long semanticAnnotationId
174 ) {
175 DataObject dataObject = dataObjectService.getDataObject(collectionId, dataObjectId);
176 collectionService.assertIsAllowedToEditCollection(collectionId);
177 assertSemanticAnnotationBelongsToEntity(dataObject, semanticAnnotationId);
178 return delete(semanticAnnotationId);
179 }
180 }