SemanticRepositoryRest.java

package de.dlr.shepard.context.semantic.endpoints;

import de.dlr.shepard.common.filters.Subscribable;
import de.dlr.shepard.common.util.Constants;
import de.dlr.shepard.common.util.QueryParamHelper;
import de.dlr.shepard.context.semantic.io.SemanticRepositoryIO;
import de.dlr.shepard.context.semantic.services.SemanticRepositoryService;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PositiveOrZero;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import java.util.ArrayList;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path(Constants.SEMANTIC_REPOSITORIES)
@RequestScoped
public class SemanticRepositoryRest {

  @Inject
  SemanticRepositoryService semanticRepositoryService;

  @GET
  @Tag(name = Constants.SEMANTIC_REPOSITORY)
  @Operation(description = "Get all semantic repositories")
  @APIResponse(
    description = "ok",
    responseCode = "200",
    content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = SemanticRepositoryIO.class))
  )
  @APIResponse(responseCode = "400", description = "bad request")
  @APIResponse(responseCode = "401", description = "not authorized")
  @APIResponse(responseCode = "404", description = "not found")
  @Parameter(name = Constants.QP_NAME)
  @Parameter(name = Constants.QP_PAGE)
  @Parameter(name = Constants.QP_SIZE)
  @Parameter(name = Constants.QP_ORDER_BY_ATTRIBUTE)
  @Parameter(name = Constants.QP_ORDER_DESC)
  public Response getAllSemanticRepositories(
    @QueryParam(Constants.QP_NAME) String name,
    @QueryParam(Constants.QP_PAGE) @PositiveOrZero Integer page,
    @QueryParam(Constants.QP_SIZE) @PositiveOrZero Integer size,
    @QueryParam(Constants.QP_ORDER_BY_ATTRIBUTE) SemanticRepositoryAttributes orderBy,
    @QueryParam(Constants.QP_ORDER_DESC) Boolean orderDesc
  ) {
    var params = new QueryParamHelper();
    if (name != null) params = params.withName(name);
    if (page != null && size != null) params = params.withPageAndSize(page, size);
    if (orderBy != null) params = params.withOrderByAttribute(orderBy, orderDesc);
    var repositories = semanticRepositoryService.getAllRepositories(params);

    var result = new ArrayList<SemanticRepositoryIO>(repositories.size());
    for (var repository : repositories) {
      result.add(new SemanticRepositoryIO(repository));
    }
    return Response.ok(result).build();
  }

  @GET
  @Path("/{" + Constants.SEMANTIC_REPOSITORY_ID + "}")
  @Tag(name = Constants.SEMANTIC_REPOSITORY)
  @Operation(description = "Get semantic repository")
  @APIResponse(
    description = "ok",
    responseCode = "200",
    content = @Content(schema = @Schema(implementation = SemanticRepositoryIO.class))
  )
  @APIResponse(responseCode = "400", description = "bad request")
  @APIResponse(responseCode = "401", description = "not authorized")
  @APIResponse(responseCode = "404", description = "not found")
  @Parameter(name = Constants.SEMANTIC_REPOSITORY_ID)
  public Response getSemanticRepository(
    @PathParam(Constants.SEMANTIC_REPOSITORY_ID) @NotNull @PositiveOrZero Long semanticRepositoryId
  ) {
    var result = semanticRepositoryService.getRepository(semanticRepositoryId);
    return Response.ok(new SemanticRepositoryIO(result)).build();
  }

  @POST
  @Tag(name = Constants.SEMANTIC_REPOSITORY)
  @Operation(description = "Create a new semantic repository")
  @APIResponse(
    description = "created",
    responseCode = "201",
    content = @Content(schema = @Schema(implementation = SemanticRepositoryIO.class))
  )
  @APIResponse(responseCode = "400", description = "bad request")
  @APIResponse(responseCode = "401", description = "not authorized")
  @APIResponse(responseCode = "404", description = "not found")
  public Response createSemanticRepository(
    @RequestBody(
      required = true,
      content = @Content(schema = @Schema(implementation = SemanticRepositoryIO.class))
    ) @Valid SemanticRepositoryIO semanticRepository
  ) {
    var result = semanticRepositoryService.createRepository(semanticRepository);
    return Response.ok(new SemanticRepositoryIO(result)).status(Status.CREATED).build();
  }

  @DELETE
  @Path("/{" + Constants.SEMANTIC_REPOSITORY_ID + "}")
  @Subscribable
  @Tag(name = Constants.SEMANTIC_REPOSITORY)
  @Operation(description = "Delete semantic repository")
  @APIResponse(description = "deleted", responseCode = "204")
  @APIResponse(responseCode = "400", description = "bad request")
  @APIResponse(responseCode = "401", description = "not authorized")
  @APIResponse(responseCode = "404", description = "not found")
  @Parameter(name = Constants.SEMANTIC_REPOSITORY_ID)
  public Response deleteSemanticRepository(
    @PathParam(Constants.SEMANTIC_REPOSITORY_ID) @NotNull @PositiveOrZero Long semanticRepositoryId
  ) {
    semanticRepositoryService.deleteRepository(semanticRepositoryId);
    return Response.status(Status.NO_CONTENT).build();
  }
}