Search in sources :

Example 1 with NotFoundException

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

the class BaseResourceCreateRequest method execute.

@Override
public final String execute(TransactionContext context) {
    // validate ID before use, IDs sometimes being used as branch paths, so must be a valid branch path
    try {
        BranchNameValidator.DEFAULT.checkName(id);
    } catch (BadRequestException e) {
        throw new BadRequestException(e.getMessage().replace("Branch name", getClass().getSimpleName().replace("CreateRequest", ".id")));
    }
    // validate URL format
    getResourceURLSchemaSupport(context).validate(url);
    // id checked against all resources
    final boolean existingId = ResourceRequests.prepareSearch().setLimit(0).filterById(getId()).build().execute(context).getTotal() > 0;
    if (existingId) {
        throw new AlreadyExistsException("Resource", getId());
    }
    // url checked against all resources
    final boolean existingUrl = ResourceRequests.prepareSearch().setLimit(0).filterByUrl(getUrl()).build().execute(context).getTotal() > 0;
    if (existingUrl) {
        throw new AlreadyExistsException("Resource", ResourceDocument.Fields.URL, getUrl());
    }
    preExecute(context);
    final List<String> bundleAncestorIds;
    if (IComponent.ROOT_ID.equals(bundleId)) {
        // "-1" is the only key that will show up both as the parent and as an ancestor
        bundleAncestorIds = List.of(IComponent.ROOT_ID);
    } else {
        final Bundles bundles = ResourceRequests.bundles().prepareSearch().filterById(bundleId).one().build().execute(context);
        if (bundles.getTotal() == 0) {
            throw new NotFoundException("Bundle parent", bundleId).toBadRequestException();
        }
        final Bundle bundleParent = bundles.first().get();
        bundleAncestorIds = bundleParent.getResourcePathSegments();
    }
    context.add(createResourceDocument(context, bundleAncestorIds));
    return id;
}
Also used : AlreadyExistsException(com.b2international.commons.exceptions.AlreadyExistsException) Bundle(com.b2international.snowowl.core.bundle.Bundle) BadRequestException(com.b2international.commons.exceptions.BadRequestException) NotFoundException(com.b2international.commons.exceptions.NotFoundException) Bundles(com.b2international.snowowl.core.bundle.Bundles)

Example 2 with NotFoundException

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

the class CodeSystemApiTest method codesystem15_CreateWithDedicatedBranchPath.

@Test
public void codesystem15_CreateWithDedicatedBranchPath() {
    final String codeSystemId = "cs10";
    final String expectedBranchPath = Branch.get(Branch.MAIN_PATH, codeSystemId);
    try {
        RepositoryRequests.branching().prepareGet(expectedBranchPath).build(TOOLING_ID).execute(Services.bus()).getSync();
        fail("Branch " + expectedBranchPath + " already exists");
    } catch (NotFoundException expected) {
    // Branch does not exist, continue
    }
    final Json requestBody = prepareCodeSystemCreateRequestBody(codeSystemId).without("branchPath");
    assertCodeSystemCreated(requestBody);
    assertCodeSystemGet(codeSystemId).statusCode(200);
    try {
        // Check if the branch has been created
        RepositoryRequests.branching().prepareGet(expectedBranchPath).build(TOOLING_ID).execute(Services.bus()).getSync();
    } catch (NotFoundException e) {
        fail("Branch " + expectedBranchPath + " did not get created as part of code system creation");
    }
}
Also used : NotFoundException(com.b2international.commons.exceptions.NotFoundException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Json(com.b2international.commons.json.Json) Test(org.junit.Test) BaseResourceApiTest(com.b2international.snowowl.core.rest.BaseResourceApiTest)

Example 3 with NotFoundException

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

the class CodeSystemApiTest method codesystem16_CreateWithExtensionOf.

@Test
public void codesystem16_CreateWithExtensionOf() {
    final String parentCodeSystemId = "cs11";
    final Json parentRequestBody = prepareCodeSystemCreateRequestBody(parentCodeSystemId);
    assertCodeSystemCreated(parentRequestBody);
    assertCodeSystemGet(parentCodeSystemId).statusCode(200);
    final Json versionRequestBody = prepareVersionCreateRequestBody(CodeSystem.uri(parentCodeSystemId), "v1", "2020-04-15");
    assertVersionCreated(versionRequestBody).statusCode(201);
    final String codeSystemId = "cs12";
    final Json requestBody = prepareCodeSystemCreateRequestBody(codeSystemId).without("branchPath").with("extensionOf", CodeSystem.uri("cs11/v1"));
    assertCodeSystemCreated(requestBody);
    final String expectedBranchPath = Branch.get(Branch.MAIN_PATH, "cs11", "v1", codeSystemId);
    try {
        // Check if the branch has been created
        RepositoryRequests.branching().prepareGet(expectedBranchPath).build(TOOLING_ID).execute(Services.bus()).getSync();
    } catch (NotFoundException e) {
        fail("Branch " + expectedBranchPath + " did not get created as part of code system creation");
    }
}
Also used : NotFoundException(com.b2international.commons.exceptions.NotFoundException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Json(com.b2international.commons.json.Json) Test(org.junit.Test) BaseResourceApiTest(com.b2international.snowowl.core.rest.BaseResourceApiTest)

Example 4 with NotFoundException

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

the class BundleRestService method delete.

@Operation(summary = "Delete a bundle", description = "Delete a bundle with the given parameters")
@ApiResponses({ @ApiResponse(responseCode = "204", description = "Deletion successful"), @ApiResponse(responseCode = "409", description = "Bundle cannot be deleted") })
@DeleteMapping(value = "/{bundleId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@Parameter(description = "The bundle identifier") @PathVariable(value = "bundleId") final String bundleId, @RequestHeader(value = X_AUTHOR, required = false) final String author) {
    try {
        final Bundle bundle = ResourceRequests.bundles().prepareGet(bundleId).buildAsync().execute(getBus()).getSync(1, TimeUnit.MINUTES);
        ResourceRequests.bundles().prepareDelete(bundleId).commit().setAuthor(author).setCommitComment(String.format("Deleted Bundle %s", bundle.getTitle())).buildAsync().execute(getBus()).getSync(COMMIT_TIMEOUT, TimeUnit.MINUTES);
    } catch (NotFoundException e) {
    // already deleted, ignore error
    }
}
Also used : Bundle(com.b2international.snowowl.core.bundle.Bundle) NotFoundException(com.b2international.commons.exceptions.NotFoundException) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses)

Example 5 with NotFoundException

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

the class BranchReopenRequest method execute.

@Override
public Boolean execute(RepositoryContext context) {
    final BaseRevisionBranching branching = context.service(BaseRevisionBranching.class);
    final RevisionBranch branch = branching.getBranch(getBranchPath());
    try {
        final RevisionBranch parentBranch = branching.getBranch(branch.getParentPath());
        branching.reopen(parentBranch, branch.getName(), branch.metadata());
        return true;
    } catch (NotFoundException e) {
        // if parent not found, convert it to BadRequestException
        throw e.toBadRequestException();
    }
}
Also used : RevisionBranch(com.b2international.index.revision.RevisionBranch) NotFoundException(com.b2international.commons.exceptions.NotFoundException) BaseRevisionBranching(com.b2international.index.revision.BaseRevisionBranching)

Aggregations

NotFoundException (com.b2international.commons.exceptions.NotFoundException)18 Operation (io.swagger.v3.oas.annotations.Operation)5 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)5 BadRequestException (com.b2international.commons.exceptions.BadRequestException)3 Branch (com.b2international.snowowl.core.branch.Branch)3 Bundle (com.b2international.snowowl.core.bundle.Bundle)3 Json (com.b2international.commons.json.Json)2 RevisionBranch (com.b2international.index.revision.RevisionBranch)2 ResourceURI (com.b2international.snowowl.core.ResourceURI)2 Bundles (com.b2international.snowowl.core.bundle.Bundles)2 CodeSystem (com.b2international.snowowl.core.codesystem.CodeSystem)2 BaseResourceApiTest (com.b2international.snowowl.core.rest.BaseResourceApiTest)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Test (org.junit.Test)2 CompareUtils (com.b2international.commons.CompareUtils)1 AlreadyExistsException (com.b2international.commons.exceptions.AlreadyExistsException)1 ApiException (com.b2international.commons.exceptions.ApiException)1 ConflictException (com.b2international.commons.exceptions.ConflictException)1 CycleDetectedException (com.b2international.commons.exceptions.CycleDetectedException)1 BaseRevisionBranching (com.b2international.index.revision.BaseRevisionBranching)1