View Javadoc
1   package de.dlr.shepard.context.references.dataobject.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.io.CollectionIO;
7   import de.dlr.shepard.context.references.dataobject.entities.CollectionReference;
8   import de.dlr.shepard.context.references.dataobject.io.CollectionReferenceIO;
9   import de.dlr.shepard.context.references.dataobject.services.CollectionReferenceService;
10  import jakarta.enterprise.context.RequestScoped;
11  import jakarta.inject.Inject;
12  import jakarta.validation.Valid;
13  import jakarta.validation.constraints.NotNull;
14  import jakarta.validation.constraints.PositiveOrZero;
15  import jakarta.ws.rs.Consumes;
16  import jakarta.ws.rs.DELETE;
17  import jakarta.ws.rs.GET;
18  import jakarta.ws.rs.POST;
19  import jakarta.ws.rs.Path;
20  import jakarta.ws.rs.PathParam;
21  import jakarta.ws.rs.Produces;
22  import jakarta.ws.rs.QueryParam;
23  import jakarta.ws.rs.core.Context;
24  import jakarta.ws.rs.core.MediaType;
25  import jakarta.ws.rs.core.Response;
26  import jakarta.ws.rs.core.Response.Status;
27  import jakarta.ws.rs.core.SecurityContext;
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.UUID;
31  import org.eclipse.microprofile.openapi.annotations.Operation;
32  import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
33  import org.eclipse.microprofile.openapi.annotations.media.Content;
34  import org.eclipse.microprofile.openapi.annotations.media.Schema;
35  import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
36  import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
37  import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
38  import org.eclipse.microprofile.openapi.annotations.tags.Tag;
39  
40  @Consumes(MediaType.APPLICATION_JSON)
41  @Produces(MediaType.APPLICATION_JSON)
42  @Path(
43    Constants.COLLECTIONS +
44    "/{" +
45    Constants.COLLECTION_ID +
46    "}/" +
47    Constants.DATA_OBJECTS +
48    "/{" +
49    Constants.DATA_OBJECT_ID +
50    "}/" +
51    Constants.COLLECTION_REFERENCES
52  )
53  @RequestScoped
54  public class CollectionReferenceRest {
55  
56    @Inject
57    CollectionReferenceService collectionReferenceService;
58  
59    @Context
60    private SecurityContext securityContext;
61  
62    @GET
63    @Tag(name = Constants.COLLECTION_REFERENCE)
64    @Operation(description = "Get all collection references")
65    @APIResponse(
66      description = "ok",
67      responseCode = "200",
68      content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = CollectionReferenceIO.class))
69    )
70    @APIResponse(responseCode = "400", description = "bad request")
71    @APIResponse(responseCode = "401", description = "not authorized")
72    @APIResponse(responseCode = "403", description = "forbidden")
73    @APIResponse(responseCode = "404", description = "not found")
74    @Parameter(name = Constants.COLLECTION_ID)
75    @Parameter(name = Constants.DATA_OBJECT_ID)
76    @Parameter(name = Constants.VERSION_UID)
77    public Response getAllCollectionReferences(
78      @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
79      @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
80      @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID
81    ) {
82      UUID versionUUID = null;
83      if (versionUID != null) {
84        versionUUID = UUID.fromString(versionUID);
85      }
86      List<CollectionReference> references = collectionReferenceService.getAllReferencesByDataObjectId(
87        collectionId,
88        dataObjectId,
89        versionUUID
90      );
91      var result = new ArrayList<CollectionReferenceIO>(references.size());
92      for (var reference : references) {
93        result.add(new CollectionReferenceIO(reference));
94      }
95      return Response.ok(result).build();
96    }
97  
98    @GET
99    @Path("/{" + Constants.COLLECTION_REFERENCE_ID + "}")
100   @Tag(name = Constants.COLLECTION_REFERENCE)
101   @Operation(description = "Get collection reference")
102   @APIResponse(
103     description = "ok",
104     responseCode = "200",
105     content = @Content(schema = @Schema(implementation = CollectionReferenceIO.class))
106   )
107   @APIResponse(responseCode = "400", description = "bad request")
108   @APIResponse(responseCode = "401", description = "not authorized")
109   @APIResponse(responseCode = "403", description = "forbidden")
110   @APIResponse(responseCode = "404", description = "not found")
111   @Parameter(name = Constants.COLLECTION_ID)
112   @Parameter(name = Constants.DATA_OBJECT_ID)
113   @Parameter(name = Constants.COLLECTION_REFERENCE_ID)
114   @Parameter(name = Constants.VERSION_UID)
115   public Response getCollectionReference(
116     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
117     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
118     @PathParam(Constants.COLLECTION_REFERENCE_ID) @NotNull @PositiveOrZero Long collectionReferenceId,
119     @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID
120   ) {
121     UUID versionUUID = null;
122     if (versionUID != null) {
123       versionUUID = UUID.fromString(versionUID);
124     }
125     CollectionReference result = collectionReferenceService.getReference(
126       collectionId,
127       dataObjectId,
128       collectionReferenceId,
129       versionUUID
130     );
131     return Response.ok(new CollectionReferenceIO(result)).build();
132   }
133 
134   @POST
135   @Subscribable
136   @Tag(name = Constants.COLLECTION_REFERENCE)
137   @Operation(description = "Create a new collection reference")
138   @APIResponse(
139     description = "created",
140     responseCode = "201",
141     content = @Content(schema = @Schema(implementation = CollectionReferenceIO.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 createCollectionReference(
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       content = @Content(schema = @Schema(implementation = CollectionReferenceIO.class))
155     ) @Valid CollectionReferenceIO collectionReference
156   ) {
157     CollectionReference result = collectionReferenceService.createReference(
158       collectionId,
159       dataObjectId,
160       collectionReference
161     );
162     return Response.ok(new CollectionReferenceIO(result)).status(Status.CREATED).build();
163   }
164 
165   @DELETE
166   @Path("/{" + Constants.COLLECTION_REFERENCE_ID + "}")
167   @Subscribable
168   @Tag(name = Constants.COLLECTION_REFERENCE)
169   @Operation(description = "Delete collection reference")
170   @APIResponse(description = "deleted", responseCode = "204")
171   @APIResponse(responseCode = "400", description = "bad request")
172   @APIResponse(responseCode = "401", description = "not authorized")
173   @APIResponse(responseCode = "403", description = "forbidden")
174   @APIResponse(responseCode = "404", description = "not found")
175   @Parameter(name = Constants.COLLECTION_ID)
176   @Parameter(name = Constants.DATA_OBJECT_ID)
177   @Parameter(name = Constants.COLLECTION_REFERENCE_ID)
178   public Response deleteCollectionReference(
179     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
180     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
181     @PathParam(Constants.COLLECTION_REFERENCE_ID) @NotNull @PositiveOrZero Long collectionReferenceId
182   ) {
183     collectionReferenceService.deleteReference(collectionId, dataObjectId, collectionReferenceId);
184     return Response.status(Status.NO_CONTENT).build();
185   }
186 
187   @GET
188   @Path("/{" + Constants.COLLECTION_REFERENCE_ID + "}/payload")
189   @Tag(name = Constants.COLLECTION_REFERENCE)
190   @Operation(description = "Get collection reference payload")
191   @APIResponse(
192     description = "ok",
193     responseCode = "200",
194     content = @Content(schema = @Schema(implementation = CollectionIO.class))
195   )
196   @APIResponse(responseCode = "400", description = "bad request")
197   @APIResponse(responseCode = "401", description = "not authorized")
198   @APIResponse(responseCode = "403", description = "forbidden")
199   @APIResponse(responseCode = "404", description = "not found")
200   @Parameter(name = Constants.COLLECTION_ID)
201   @Parameter(name = Constants.DATA_OBJECT_ID)
202   @Parameter(name = Constants.COLLECTION_REFERENCE_ID)
203   @Parameter(name = Constants.VERSION_UID)
204   public Response getCollectionReferencePayload(
205     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
206     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
207     @PathParam(Constants.COLLECTION_REFERENCE_ID) @NotNull @PositiveOrZero Long collectionReferenceId,
208     @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID
209   ) {
210     UUID versionUUID = null;
211     if (versionUID != null) {
212       versionUUID = UUID.fromString(versionUID);
213     }
214     Collection payload = collectionReferenceService.getPayload(
215       collectionId,
216       dataObjectId,
217       collectionReferenceId,
218       versionUUID
219     );
220     return Response.ok(new CollectionIO(payload)).build();
221   }
222 }