View Javadoc
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.services.CollectionService;
6   import de.dlr.shepard.context.references.basicreference.entities.BasicReference;
7   import de.dlr.shepard.context.references.basicreference.services.BasicReferenceService;
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.BASIC_REFERENCES +
45    "/{" +
46    Constants.BASIC_REFERENCE_ID +
47    "}/" +
48    Constants.SEMANTIC_ANNOTATIONS
49  )
50  @RequestScoped
51  public class BasicReferenceSemanticAnnotationRest extends SemanticAnnotationRest {
52  
53    @Inject
54    CollectionService collectionService;
55  
56    @Inject
57    BasicReferenceService basicReferenceService;
58  
59    @GET
60    @Tag(name = Constants.SEMANTIC_ANNOTATION)
61    @APIResponse(
62      description = "ok",
63      responseCode = "200",
64      content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = SemanticAnnotationIO.class))
65    )
66    @APIResponse(responseCode = "400", description = "bad request")
67    @APIResponse(responseCode = "401", description = "not authorized")
68    @APIResponse(responseCode = "403", description = "forbidden")
69    @APIResponse(responseCode = "404", description = "not found")
70    @Operation(operationId = "getAllReferenceAnnotations", description = "Get all semantic annotations")
71    @Parameter(
72      in = ParameterIn.PATH,
73      name = Constants.COLLECTION_ID,
74      schema = @Schema(type = SchemaType.INTEGER, format = "int64")
75    )
76    @Parameter(
77      in = ParameterIn.PATH,
78      name = Constants.DATA_OBJECT_ID,
79      schema = @Schema(type = SchemaType.INTEGER, format = "int64")
80    )
81    @Parameter(name = Constants.BASIC_REFERENCE_ID)
82    public Response getAllAnnotations(
83      @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
84      @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
85      @PathParam(Constants.BASIC_REFERENCE_ID) @NotNull @PositiveOrZero Long basicReferenceId
86    ) {
87      basicReferenceService.getReference(collectionId, dataObjectId, basicReferenceId);
88      return getAllByShepardId(basicReferenceId);
89    }
90  
91    @GET
92    @Path("{" + Constants.SEMANTIC_ANNOTATION_ID + "}")
93    @Tag(name = Constants.SEMANTIC_ANNOTATION)
94    @APIResponse(
95      description = "ok",
96      responseCode = "200",
97      content = @Content(schema = @Schema(implementation = SemanticAnnotationIO.class))
98    )
99    @APIResponse(responseCode = "400", description = "bad request")
100   @APIResponse(responseCode = "401", description = "not authorized")
101   @APIResponse(responseCode = "403", description = "forbidden")
102   @APIResponse(responseCode = "404", description = "not found")
103   @Operation(operationId = "getReferenceAnnotation", description = "Get semantic annotation")
104   @Parameter(
105     in = ParameterIn.PATH,
106     name = Constants.COLLECTION_ID,
107     schema = @Schema(type = SchemaType.INTEGER, format = "int64")
108   )
109   @Parameter(
110     in = ParameterIn.PATH,
111     name = Constants.DATA_OBJECT_ID,
112     schema = @Schema(type = SchemaType.INTEGER, format = "int64")
113   )
114   @Parameter(name = Constants.BASIC_REFERENCE_ID)
115   @Parameter(name = Constants.SEMANTIC_ANNOTATION_ID)
116   public Response getAnnotation(
117     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
118     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
119     @PathParam(Constants.BASIC_REFERENCE_ID) @NotNull @PositiveOrZero Long basicReferenceId,
120     @PathParam(Constants.SEMANTIC_ANNOTATION_ID) @NotNull @PositiveOrZero Long semanticAnnotationId
121   ) {
122     BasicReference basicReference = basicReferenceService.getReference(collectionId, dataObjectId, basicReferenceId);
123     assertSemanticAnnotationBelongsToEntity(basicReference, semanticAnnotationId);
124     return get(semanticAnnotationId);
125   }
126 
127   @POST
128   @Tag(name = Constants.SEMANTIC_ANNOTATION)
129   @APIResponse(
130     description = "created",
131     responseCode = "201",
132     content = @Content(schema = @Schema(implementation = SemanticAnnotationIO.class))
133   )
134   @APIResponse(responseCode = "400", description = "bad request")
135   @APIResponse(responseCode = "401", description = "not authorized")
136   @APIResponse(responseCode = "403", description = "forbidden")
137   @APIResponse(responseCode = "404", description = "not found")
138   @Subscribable
139   @Operation(operationId = "createReferenceAnnotation", description = "Create a new semantic annotation")
140   @Parameter(
141     in = ParameterIn.PATH,
142     name = Constants.COLLECTION_ID,
143     schema = @Schema(type = SchemaType.INTEGER, format = "int64")
144   )
145   @Parameter(
146     in = ParameterIn.PATH,
147     name = Constants.DATA_OBJECT_ID,
148     schema = @Schema(type = SchemaType.INTEGER, format = "int64")
149   )
150   @Parameter(name = Constants.BASIC_REFERENCE_ID)
151   public Response createAnnotation(
152     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
153     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
154     @PathParam(Constants.BASIC_REFERENCE_ID) @NotNull @PositiveOrZero Long basicReferenceId,
155     @RequestBody(
156       required = true,
157       content = @Content(schema = @Schema(implementation = SemanticAnnotationIO.class))
158     ) @Valid SemanticAnnotationIO semanticAnnotation
159   ) {
160     basicReferenceService.getReference(collectionId, dataObjectId, basicReferenceId);
161     collectionService.assertIsAllowedToEditCollection(collectionId);
162     return createByShepardId(basicReferenceId, semanticAnnotation);
163   }
164 
165   @DELETE
166   @Tag(name = Constants.SEMANTIC_ANNOTATION)
167   @APIResponse(description = "deleted", responseCode = "204")
168   @APIResponse(responseCode = "400", description = "bad request")
169   @APIResponse(responseCode = "401", description = "not authorized")
170   @APIResponse(responseCode = "403", description = "forbidden")
171   @APIResponse(responseCode = "404", description = "not found")
172   @Path("{" + Constants.SEMANTIC_ANNOTATION_ID + "}")
173   @Subscribable
174   @Operation(operationId = "deleteReferenceAnnotation", description = "Delete semantic annotation")
175   @Parameter(
176     in = ParameterIn.PATH,
177     name = Constants.COLLECTION_ID,
178     schema = @Schema(type = SchemaType.INTEGER, format = "int64")
179   )
180   @Parameter(
181     in = ParameterIn.PATH,
182     name = Constants.DATA_OBJECT_ID,
183     schema = @Schema(type = SchemaType.INTEGER, format = "int64")
184   )
185   @Parameter(name = Constants.BASIC_REFERENCE_ID)
186   @Parameter(name = Constants.SEMANTIC_ANNOTATION_ID)
187   public Response deleteAnnotation(
188     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
189     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
190     @PathParam(Constants.BASIC_REFERENCE_ID) @NotNull @PositiveOrZero Long basicReferenceId,
191     @PathParam(Constants.SEMANTIC_ANNOTATION_ID) @NotNull @PositiveOrZero Long semanticAnnotationId
192   ) {
193     BasicReference basicReference = basicReferenceService.getReference(collectionId, dataObjectId, basicReferenceId);
194     collectionService.assertIsAllowedToEditCollection(collectionId);
195     assertSemanticAnnotationBelongsToEntity(basicReference, semanticAnnotationId);
196     return delete(semanticAnnotationId);
197   }
198 }