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.common.util.QueryParamHelper;
6   import de.dlr.shepard.context.semantic.io.SemanticRepositoryIO;
7   import de.dlr.shepard.context.semantic.services.SemanticRepositoryService;
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.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.parameters.RequestBody;
31  import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
32  import org.eclipse.microprofile.openapi.annotations.tags.Tag;
33  
34  @Consumes(MediaType.APPLICATION_JSON)
35  @Produces(MediaType.APPLICATION_JSON)
36  @Path(Constants.SEMANTIC_REPOSITORIES)
37  @RequestScoped
38  public class SemanticRepositoryRest {
39  
40    @Inject
41    SemanticRepositoryService semanticRepositoryService;
42  
43    @GET
44    @Tag(name = Constants.SEMANTIC_REPOSITORY)
45    @Operation(description = "Get all semantic repositories")
46    @APIResponse(
47      description = "ok",
48      responseCode = "200",
49      content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = SemanticRepositoryIO.class))
50    )
51    @APIResponse(responseCode = "400", description = "bad request")
52    @APIResponse(responseCode = "401", description = "not authorized")
53    @APIResponse(responseCode = "404", description = "not found")
54    @Parameter(name = Constants.QP_NAME)
55    @Parameter(name = Constants.QP_PAGE)
56    @Parameter(name = Constants.QP_SIZE)
57    @Parameter(name = Constants.QP_ORDER_BY_ATTRIBUTE)
58    @Parameter(name = Constants.QP_ORDER_DESC)
59    public Response getAllSemanticRepositories(
60      @QueryParam(Constants.QP_NAME) String name,
61      @QueryParam(Constants.QP_PAGE) @PositiveOrZero Integer page,
62      @QueryParam(Constants.QP_SIZE) @PositiveOrZero Integer size,
63      @QueryParam(Constants.QP_ORDER_BY_ATTRIBUTE) SemanticRepositoryAttributes orderBy,
64      @QueryParam(Constants.QP_ORDER_DESC) Boolean orderDesc
65    ) {
66      var params = new QueryParamHelper();
67      if (name != null) params = params.withName(name);
68      if (page != null && size != null) params = params.withPageAndSize(page, size);
69      if (orderBy != null) params = params.withOrderByAttribute(orderBy, orderDesc);
70      var repositories = semanticRepositoryService.getAllRepositories(params);
71  
72      var result = new ArrayList<SemanticRepositoryIO>(repositories.size());
73      for (var repository : repositories) {
74        result.add(new SemanticRepositoryIO(repository));
75      }
76      return Response.ok(result).build();
77    }
78  
79    @GET
80    @Path("/{" + Constants.SEMANTIC_REPOSITORY_ID + "}")
81    @Tag(name = Constants.SEMANTIC_REPOSITORY)
82    @Operation(description = "Get semantic repository")
83    @APIResponse(
84      description = "ok",
85      responseCode = "200",
86      content = @Content(schema = @Schema(implementation = SemanticRepositoryIO.class))
87    )
88    @APIResponse(responseCode = "400", description = "bad request")
89    @APIResponse(responseCode = "401", description = "not authorized")
90    @APIResponse(responseCode = "404", description = "not found")
91    @Parameter(name = Constants.SEMANTIC_REPOSITORY_ID)
92    public Response getSemanticRepository(
93      @PathParam(Constants.SEMANTIC_REPOSITORY_ID) @NotNull @PositiveOrZero Long semanticRepositoryId
94    ) {
95      var result = semanticRepositoryService.getRepository(semanticRepositoryId);
96      return Response.ok(new SemanticRepositoryIO(result)).build();
97    }
98  
99    @POST
100   @Tag(name = Constants.SEMANTIC_REPOSITORY)
101   @Operation(description = "Create a new semantic repository")
102   @APIResponse(
103     description = "created",
104     responseCode = "201",
105     content = @Content(schema = @Schema(implementation = SemanticRepositoryIO.class))
106   )
107   @APIResponse(responseCode = "400", description = "bad request")
108   @APIResponse(responseCode = "401", description = "not authorized")
109   @APIResponse(responseCode = "404", description = "not found")
110   public Response createSemanticRepository(
111     @RequestBody(
112       required = true,
113       content = @Content(schema = @Schema(implementation = SemanticRepositoryIO.class))
114     ) @Valid SemanticRepositoryIO semanticRepository
115   ) {
116     var result = semanticRepositoryService.createRepository(semanticRepository);
117     return Response.ok(new SemanticRepositoryIO(result)).status(Status.CREATED).build();
118   }
119 
120   @DELETE
121   @Path("/{" + Constants.SEMANTIC_REPOSITORY_ID + "}")
122   @Subscribable
123   @Tag(name = Constants.SEMANTIC_REPOSITORY)
124   @Operation(description = "Delete semantic repository")
125   @APIResponse(description = "deleted", responseCode = "204")
126   @APIResponse(responseCode = "400", description = "bad request")
127   @APIResponse(responseCode = "401", description = "not authorized")
128   @APIResponse(responseCode = "404", description = "not found")
129   @Parameter(name = Constants.SEMANTIC_REPOSITORY_ID)
130   public Response deleteSemanticRepository(
131     @PathParam(Constants.SEMANTIC_REPOSITORY_ID) @NotNull @PositiveOrZero Long semanticRepositoryId
132   ) {
133     semanticRepositoryService.deleteRepository(semanticRepositoryId);
134     return Response.status(Status.NO_CONTENT).build();
135   }
136 }