Search in sources :

Example 1 with Bundles

use of com.b2international.snowowl.core.bundle.Bundles 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 Bundles

use of com.b2international.snowowl.core.bundle.Bundles in project snow-owl by b2ihealthcare.

the class BaseResourceUpdateRequest method getBundleAncestorIds.

private List<String> getBundleAncestorIds(final TransactionContext context, final String resourceId) {
    if (IComponent.ROOT_ID.equals(bundleId)) {
        return List.of(bundleId);
    }
    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 parentBundle = bundles.first().get();
    if (parentBundle.getBundleId().equals(resourceId) || parentBundle.getBundleAncestorIds().contains(resourceId)) {
        throw new CycleDetectedException("Setting parent bundle ID to '" + bundleId + "' would create a loop.");
    }
    return parentBundle.getResourcePathSegments();
}
Also used : Bundle(com.b2international.snowowl.core.bundle.Bundle) CycleDetectedException(com.b2international.commons.exceptions.CycleDetectedException) NotFoundException(com.b2international.commons.exceptions.NotFoundException) Bundles(com.b2international.snowowl.core.bundle.Bundles)

Aggregations

NotFoundException (com.b2international.commons.exceptions.NotFoundException)2 Bundle (com.b2international.snowowl.core.bundle.Bundle)2 Bundles (com.b2international.snowowl.core.bundle.Bundles)2 AlreadyExistsException (com.b2international.commons.exceptions.AlreadyExistsException)1 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 CycleDetectedException (com.b2international.commons.exceptions.CycleDetectedException)1