View Javadoc
1   package de.dlr.shepard.context.references.basicreference.endpoints;
2   
3   import de.dlr.shepard.common.filters.Subscribable;
4   import de.dlr.shepard.common.util.Constants;
5   import de.dlr.shepard.common.util.QueryParamHelper;
6   import de.dlr.shepard.context.references.basicreference.entities.BasicReference;
7   import de.dlr.shepard.context.references.basicreference.io.BasicReferenceIO;
8   import de.dlr.shepard.context.references.basicreference.services.BasicReferenceService;
9   import jakarta.enterprise.context.RequestScoped;
10  import jakarta.inject.Inject;
11  import jakarta.validation.constraints.NotNull;
12  import jakarta.validation.constraints.Positive;
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.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.MediaType;
22  import jakarta.ws.rs.core.Response;
23  import jakarta.ws.rs.core.Response.Status;
24  import java.util.ArrayList;
25  import org.eclipse.microprofile.openapi.annotations.Operation;
26  import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
27  import org.eclipse.microprofile.openapi.annotations.media.Content;
28  import org.eclipse.microprofile.openapi.annotations.media.Schema;
29  import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
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  @RequestScoped
47  public class BasicReferenceRest {
48  
49    @Inject
50    BasicReferenceService basicReferenceService;
51  
52    @GET
53    @Tag(name = Constants.BASIC_REFERENCE)
54    @Operation(description = "Get all references")
55    @APIResponse(
56      description = "ok",
57      responseCode = "200",
58      content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = BasicReferenceIO.class))
59    )
60    @APIResponse(responseCode = "400", description = "bad request")
61    @APIResponse(responseCode = "401", description = "not authorized")
62    @APIResponse(responseCode = "403", description = "forbidden")
63    @APIResponse(responseCode = "404", description = "not found")
64    @Parameter(name = Constants.COLLECTION_ID)
65    @Parameter(name = Constants.DATA_OBJECT_ID)
66    @Parameter(name = Constants.QP_NAME)
67    @Parameter(name = Constants.QP_PAGE)
68    @Parameter(name = Constants.QP_SIZE)
69    @Parameter(name = Constants.QP_ORDER_BY_ATTRIBUTE)
70    @Parameter(name = Constants.QP_ORDER_DESC)
71    public Response getAllReferences(
72      @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
73      @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
74      @QueryParam(Constants.QP_NAME) String name,
75      @QueryParam(Constants.QP_PAGE) @PositiveOrZero Integer page,
76      @QueryParam(Constants.QP_SIZE) @Positive Integer size,
77      @QueryParam(Constants.QP_ORDER_BY_ATTRIBUTE) BasicReferenceAttributes orderBy,
78      @QueryParam(Constants.QP_ORDER_DESC) Boolean orderDesc
79    ) {
80      var params = new QueryParamHelper();
81      if (name != null) params = params.withName(name);
82      if (page != null && size != null) params = params.withPageAndSize(page, size);
83      if (orderBy != null) params = params.withOrderByAttribute(orderBy, orderDesc);
84      var references = basicReferenceService.getAllBasicReferences(collectionId, dataObjectId, params);
85      var result = new ArrayList<BasicReferenceIO>(references.size());
86  
87      for (var ref : references) {
88        result.add(new BasicReferenceIO(ref));
89      }
90      return Response.ok(result).build();
91    }
92  
93    @GET
94    @Path("/{" + Constants.BASIC_REFERENCE_ID + "}")
95    @Tag(name = Constants.BASIC_REFERENCE)
96    @Operation(description = "Get reference")
97    @APIResponse(
98      description = "ok",
99      responseCode = "200",
100     content = @Content(schema = @Schema(implementation = BasicReferenceIO.class))
101   )
102   @APIResponse(responseCode = "400", description = "bad request")
103   @APIResponse(responseCode = "401", description = "not authorized")
104   @APIResponse(responseCode = "403", description = "forbidden")
105   @APIResponse(responseCode = "404", description = "not found")
106   @Parameter(name = Constants.COLLECTION_ID)
107   @Parameter(name = Constants.DATA_OBJECT_ID)
108   @Parameter(name = Constants.BASIC_REFERENCE_ID)
109   public Response getBasicReference(
110     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
111     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
112     @PathParam(Constants.BASIC_REFERENCE_ID) @NotNull @PositiveOrZero Long referenceId
113   ) {
114     BasicReference basicReference = basicReferenceService.getReference(collectionId, dataObjectId, referenceId);
115     return Response.ok(new BasicReferenceIO(basicReference)).build();
116   }
117 
118   @DELETE
119   @Path("/{" + Constants.BASIC_REFERENCE_ID + "}")
120   @Subscribable
121   @Tag(name = Constants.BASIC_REFERENCE)
122   @Operation(description = "Delete reference")
123   @APIResponse(description = "deleted", responseCode = "204")
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   @Parameter(name = Constants.COLLECTION_ID)
129   @Parameter(name = Constants.DATA_OBJECT_ID)
130   @Parameter(name = Constants.BASIC_REFERENCE_ID)
131   public Response deleteBasicReference(
132     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
133     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
134     @PathParam(Constants.BASIC_REFERENCE_ID) @NotNull @PositiveOrZero Long basicReferenceId
135   ) {
136     basicReferenceService.deleteReference(collectionId, dataObjectId, basicReferenceId);
137     return Response.status(Status.NO_CONTENT).build();
138   }
139 }