View Javadoc
1   package de.dlr.shepard.context.references.spatialdata.endpoints;
2   
3   import de.dlr.shepard.common.filters.Subscribable;
4   import de.dlr.shepard.common.util.Constants;
5   import de.dlr.shepard.context.references.spatialdata.io.SpatialDataReferenceIO;
6   import de.dlr.shepard.context.references.spatialdata.services.SpatialDataReferenceService;
7   import de.dlr.shepard.data.spatialdata.io.SpatialDataPointIO;
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.QueryParam;
21  import jakarta.ws.rs.core.Context;
22  import jakarta.ws.rs.core.MediaType;
23  import jakarta.ws.rs.core.Response;
24  import jakarta.ws.rs.core.SecurityContext;
25  import java.util.List;
26  import java.util.UUID;
27  import java.util.stream.Collectors;
28  import org.eclipse.microprofile.openapi.annotations.Operation;
29  import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
30  import org.eclipse.microprofile.openapi.annotations.media.Content;
31  import org.eclipse.microprofile.openapi.annotations.media.Schema;
32  import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
33  import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
34  import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
35  import org.eclipse.microprofile.openapi.annotations.tags.Tag;
36  
37  @Consumes(MediaType.APPLICATION_JSON)
38  @Produces(MediaType.APPLICATION_JSON)
39  @Path(
40    Constants.COLLECTIONS +
41    "/{" +
42    Constants.COLLECTION_ID +
43    "}/" +
44    Constants.DATA_OBJECTS +
45    "/{" +
46    Constants.DATA_OBJECT_ID +
47    "}/" +
48    Constants.SPATIAL_DATA_REFERENCES
49  )
50  @RequestScoped
51  public class SpatialDataReferenceRest {
52  
53    @Inject
54    SpatialDataReferenceService spatialDataReferenceService;
55  
56    @Context
57    private SecurityContext securityContext;
58  
59    @GET
60    @Tag(name = Constants.SPATIAL_DATA_REFERENCE)
61    @Operation(description = "Get all spatial data references")
62    @APIResponse(
63      description = "ok",
64      responseCode = "200",
65      content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = SpatialDataReferenceIO.class))
66    )
67    @APIResponse(responseCode = "400", description = "bad request")
68    @APIResponse(responseCode = "401", description = "not authorized")
69    @APIResponse(responseCode = "403", description = "forbidden")
70    @APIResponse(responseCode = "404", description = "not found")
71    @Parameter(name = Constants.COLLECTION_ID)
72    @Parameter(name = Constants.DATA_OBJECT_ID)
73    @Parameter(name = Constants.VERSION_UID)
74    public Response getAllSpatialDataReferences(
75      @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
76      @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
77      @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID
78    ) {
79      UUID versionUUID = null;
80      if (versionUID != null) {
81        versionUUID = UUID.fromString(versionUID);
82      }
83  
84      var references = spatialDataReferenceService.getAllReferencesByDataObjectId(
85        collectionId,
86        dataObjectId,
87        versionUUID
88      );
89      List<SpatialDataReferenceIO> result = references
90        .stream()
91        .map(SpatialDataReferenceIO::new)
92        .collect(Collectors.toList());
93      return Response.ok(result).build();
94    }
95  
96    @GET
97    @Path("/{" + Constants.SPATIAL_DATA_REFERENCE_ID + "}")
98    @Tag(name = Constants.SPATIAL_DATA_REFERENCE)
99    @Operation(description = "Get spatialData reference")
100   @APIResponse(
101     description = "ok",
102     responseCode = "200",
103     content = @Content(schema = @Schema(implementation = SpatialDataReferenceIO.class))
104   )
105   @APIResponse(responseCode = "400", description = "bad request")
106   @APIResponse(responseCode = "401", description = "not authorized")
107   @APIResponse(responseCode = "403", description = "forbidden")
108   @APIResponse(responseCode = "404", description = "not found")
109   @Parameter(name = Constants.COLLECTION_ID)
110   @Parameter(name = Constants.DATA_OBJECT_ID)
111   @Parameter(name = Constants.SPATIAL_DATA_REFERENCE_ID)
112   @Parameter(name = Constants.VERSION_UID)
113   public Response getSpatialDataReference(
114     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
115     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
116     @PathParam(Constants.SPATIAL_DATA_REFERENCE_ID) @NotNull @PositiveOrZero Long spatialDataReferenceId,
117     @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID
118   ) {
119     UUID versionUUID = null;
120     if (versionUID != null) {
121       versionUUID = UUID.fromString(versionUID);
122     }
123 
124     var result = spatialDataReferenceService.getReference(
125       collectionId,
126       dataObjectId,
127       spatialDataReferenceId,
128       versionUUID
129     );
130 
131     return Response.ok(new SpatialDataReferenceIO(result)).build();
132   }
133 
134   @POST
135   @Subscribable
136   @Tag(name = Constants.SPATIAL_DATA_REFERENCE)
137   @Operation(description = "Create a new spatialData reference")
138   @APIResponse(
139     description = "created",
140     responseCode = "201",
141     content = @Content(schema = @Schema(implementation = SpatialDataReferenceIO.class))
142   )
143   @APIResponse(responseCode = "400", description = "bad request")
144   @APIResponse(responseCode = "401", description = "not authorized")
145   @APIResponse(responseCode = "403", description = "forbidden")
146   @APIResponse(responseCode = "404", description = "not found")
147   @Parameter(name = Constants.COLLECTION_ID)
148   @Parameter(name = Constants.DATA_OBJECT_ID)
149   public Response createSpatialDataReference(
150     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
151     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
152     @RequestBody(
153       required = true,
154       description = "For more examples take a look at [Get spatial data by container id](#/spatialDataContainer/getSpatialDataPoints).",
155       content = @Content(schema = @Schema(implementation = SpatialDataReferenceIO.class))
156     ) @Valid SpatialDataReferenceIO spatialDataReference
157   ) {
158     var result = spatialDataReferenceService.createReference(collectionId, dataObjectId, spatialDataReference);
159     return Response.ok(new SpatialDataReferenceIO(result)).status(Response.Status.CREATED).build();
160   }
161 
162   @DELETE
163   @Path("/{" + Constants.SPATIAL_DATA_REFERENCE_ID + "}")
164   @Subscribable
165   @Tag(name = Constants.SPATIAL_DATA_REFERENCE)
166   @Operation(description = "Delete spatialData reference")
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   @Parameter(name = Constants.COLLECTION_ID)
173   @Parameter(name = Constants.DATA_OBJECT_ID)
174   @Parameter(name = Constants.SPATIAL_DATA_REFERENCE_ID)
175   public Response deleteSpatialDataReference(
176     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
177     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
178     @PathParam(Constants.SPATIAL_DATA_REFERENCE_ID) @NotNull @PositiveOrZero Long spatialDataReferenceId
179   ) {
180     spatialDataReferenceService.deleteReference(collectionId, dataObjectId, spatialDataReferenceId);
181     return Response.status(Response.Status.NO_CONTENT).build();
182   }
183 
184   @GET
185   @Path("/{" + Constants.SPATIAL_DATA_REFERENCE_ID + "}/" + Constants.PAYLOAD)
186   @Tag(name = Constants.SPATIAL_DATA_REFERENCE)
187   @Operation(description = "Get spatialData reference payload")
188   @APIResponse(
189     description = "ok",
190     responseCode = "200",
191     content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = SpatialDataPointIO.class))
192   )
193   @APIResponse(responseCode = "400", description = "bad request")
194   @APIResponse(responseCode = "401", description = "not authorized")
195   @APIResponse(responseCode = "403", description = "forbidden")
196   @APIResponse(responseCode = "404", description = "not found")
197   @Parameter(name = Constants.COLLECTION_ID)
198   @Parameter(name = Constants.DATA_OBJECT_ID)
199   @Parameter(name = Constants.SPATIAL_DATA_REFERENCE_ID)
200   public Response getSpatialDataPayload(
201     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
202     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
203     @PathParam(Constants.SPATIAL_DATA_REFERENCE_ID) @NotNull @PositiveOrZero Long spatialDataReferenceId
204   ) {
205     return Response.ok(
206       spatialDataReferenceService.getReferencePayload(collectionId, dataObjectId, spatialDataReferenceId)
207     ).build();
208   }
209 }