Search in sources :

Example 1 with ApiErrorException

use of com.b2international.commons.exceptions.ApiErrorException in project snow-owl by b2ihealthcare.

the class VersionRestService method createVersion.

@Operation(summary = "Create a new resource version", description = "Creates a new resource version. " + "The version tag (represented by an empty branch) is created on the resource's current working branch. " + "Where applicable, effective times are set on the unpublished content as part of this operation.")
@ApiResponses({ @ApiResponse(responseCode = "201", description = "Created"), @ApiResponse(responseCode = "404", description = "Not found"), @ApiResponse(responseCode = "409", description = "Code system version conflicts with existing branch") })
@PostMapping(consumes = { AbstractRestService.JSON_MEDIA_TYPE })
@ResponseStatus(value = HttpStatus.CREATED)
public ResponseEntity<Void> createVersion(@Parameter(description = "Version parameters") @RequestBody final ResourceRequest<VersionRestInput> input) {
    final VersionRestInput change = input.getChange();
    ApiValidation.checkInput(change);
    String newVersionUri = String.join(Branch.SEPARATOR, change.getResource().toString(), change.getVersion());
    String jobId = ResourceRequests.prepareNewVersion().setResource(change.getResource()).setVersion(change.getVersion()).setDescription(change.getDescription()).setEffectiveTime(change.getEffectiveTime()).setForce(change.isForce()).setCommitComment(input.getCommitComment()).buildAsync().runAsJobWithRestart(ResourceRequests.versionJobKey(change.getResource()), "Creating version " + newVersionUri).execute(getBus()).getSync(1, TimeUnit.MINUTES);
    RemoteJobEntry job = JobRequests.waitForJob(getBus(), jobId, 500);
    if (job.isSuccessful()) {
        final URI location = MvcUriComponentsBuilder.fromMethodName(VersionRestService.class, "getVersion", newVersionUri).build().toUri();
        return ResponseEntity.created(location).build();
    } else if (!Strings.isNullOrEmpty(job.getResult())) {
        ApiError error = job.getResultAs(ApplicationContext.getServiceForClass(ObjectMapper.class), ApiError.class);
        throw new ApiErrorException(error.withMessage(error.getMessage().replace("Branch name", "Version")));
    } else {
        throw new SnowowlRuntimeException("Version creation failed.");
    }
}
Also used : ApiError(com.b2international.commons.exceptions.ApiError) URI(java.net.URI) ApiErrorException(com.b2international.commons.exceptions.ApiErrorException) RemoteJobEntry(com.b2international.snowowl.core.jobs.RemoteJobEntry) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses)

Aggregations

ApiError (com.b2international.commons.exceptions.ApiError)1 ApiErrorException (com.b2international.commons.exceptions.ApiErrorException)1 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)1 RemoteJobEntry (com.b2international.snowowl.core.jobs.RemoteJobEntry)1 Operation (io.swagger.v3.oas.annotations.Operation)1 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)1 URI (java.net.URI)1