Search in sources :

Example 1 with ResourceDocument

use of com.b2international.snowowl.core.internal.ResourceDocument in project snow-owl by b2ihealthcare.

the class CodeSystemUpgradeSynchronizationRequest method execute.

@Override
public Boolean execute(RepositoryContext context) {
    final String message = String.format("Merge %s into %s", source, codeSystemId);
    CodeSystem codeSystem = CodeSystemRequests.prepareGetCodeSystem(codeSystemId.getResourceId()).build().execute(context);
    if (codeSystem.getUpgradeOf() == null) {
        throw new BadRequestException("Code System '%s' is not an Upgrade Code System. It cannot be synchronized with '%s'.", codeSystemId, source);
    }
    final String sourceBranchPath = context.service(ResourceURIPathResolver.class).resolve(context, List.of(source)).stream().findFirst().get();
    // merge all changes from the source to the current upgrade of branch
    final Merge merge = RepositoryRequests.merging().prepareCreate().setSource(sourceBranchPath).setTarget(codeSystem.getBranchPath()).setUserId(context.service(User.class).getUsername()).setCommitComment(message).setSquash(false).build(codeSystem.getToolingId()).getRequest().execute(context);
    if (merge.getStatus() != Merge.Status.COMPLETED) {
        // report conflicts
        ApiError apiError = merge.getApiError();
        Collection<MergeConflict> conflicts = merge.getConflicts();
        context.log().error("Failed to sync source CodeSystem content to upgrade CodeSystem. Error: {}. Conflicts: {}", apiError.getMessage(), conflicts);
        throw new ConflictException("Upgrade code system synchronization can not be performed due to conflicting content errors.").withAdditionalInfo(Map.of("conflicts", conflicts, "mergeError", apiError.getMessage()));
    }
    if (!codeSystem.getUpgradeOf().equals(source)) {
        return new BranchRequest<>(Branch.MAIN_PATH, new ResourceRepositoryCommitRequestBuilder().setBody((tx) -> {
            ResourceDocument entry = tx.lookup(codeSystemId.getResourceId(), ResourceDocument.class);
            tx.add(ResourceDocument.builder(entry).upgradeOf(source).build());
            tx.commit(String.format("Update upgradeOf from '%s' to '%s'", codeSystem.getUpgradeOf(), source));
            return Boolean.TRUE;
        }).setCommitComment(String.format("Complete upgrade of %s to %s", codeSystem.getUpgradeOf().getResourceId(), codeSystem.getExtensionOf())).build()).execute(context).getResultAs(Boolean.class);
    } else {
        return Boolean.TRUE;
    }
}
Also used : MergeConflict(com.b2international.snowowl.core.merge.MergeConflict) BadRequestException(com.b2international.commons.exceptions.BadRequestException) BranchRequest(com.b2international.snowowl.core.request.BranchRequest) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ConflictException(com.b2international.commons.exceptions.ConflictException) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) MergeConflict(com.b2international.snowowl.core.merge.MergeConflict) Collection(java.util.Collection) Request(com.b2international.snowowl.core.events.Request) ResourceURIPathResolver(com.b2international.snowowl.core.uri.ResourceURIPathResolver) Merge(com.b2international.snowowl.core.merge.Merge) NotNull(javax.validation.constraints.NotNull) Branch(com.b2international.snowowl.core.branch.Branch) RepositoryRequests(com.b2international.snowowl.core.repository.RepositoryRequests) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) List(java.util.List) Map(java.util.Map) ApiError(com.b2international.commons.exceptions.ApiError) ResourceRepositoryCommitRequestBuilder(com.b2international.snowowl.core.context.ResourceRepositoryCommitRequestBuilder) User(com.b2international.snowowl.core.identity.User) ResourceURI(com.b2international.snowowl.core.ResourceURI) User(com.b2international.snowowl.core.identity.User) ConflictException(com.b2international.commons.exceptions.ConflictException) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) Merge(com.b2international.snowowl.core.merge.Merge) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ApiError(com.b2international.commons.exceptions.ApiError) ResourceRepositoryCommitRequestBuilder(com.b2international.snowowl.core.context.ResourceRepositoryCommitRequestBuilder) BranchRequest(com.b2international.snowowl.core.request.BranchRequest)

Example 2 with ResourceDocument

use of com.b2international.snowowl.core.internal.ResourceDocument in project snow-owl by b2ihealthcare.

the class BundleDeleteRequest method execute.

@Override
public Boolean execute(TransactionContext context) {
    try {
        final ResourceDocument bundleToDelete = context.lookup(resourceId, ResourceDocument.class);
        updateResourceBundles(context, bundleToDelete);
        context.delete(bundleToDelete);
    } catch (ComponentNotFoundException e) {
    // ignore, probably already deleted
    }
    return Boolean.TRUE;
}
Also used : ComponentNotFoundException(com.b2international.snowowl.core.exceptions.ComponentNotFoundException) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument)

Example 3 with ResourceDocument

use of com.b2international.snowowl.core.internal.ResourceDocument in project snow-owl by b2ihealthcare.

the class BaseResourceUpdateRequest method updateBundle.

private boolean updateBundle(TransactionContext context, String resourceId, String oldBundleId, Builder updated) {
    if (bundleId == null || bundleId.equals(oldBundleId)) {
        return false;
    }
    final List<String> bundleAncestorIds = getBundleAncestorIds(context, resourceId);
    updated.bundleId(bundleId);
    updated.bundleAncestorIds(bundleAncestorIds);
    // Update bundle ancestor IDs on all descendants of the resource (their bundle ID does not change)
    final Iterator<Resource> descendants = ResourceRequests.prepareSearch().filterByBundleAncestorId(resourceId).setLimit(5_000).stream(context).flatMap(Resources::stream).iterator();
    final Multimap<String, Resource> resourcesByParentId = Multimaps.index(descendants, Resource::getBundleId);
    // Calculate new ancestor ID list for direct children of this resource first
    final ImmutableList.Builder<String> ancestorIdsOfParent = ImmutableList.<String>builder().addAll(bundleAncestorIds);
    if (!IComponent.ROOT_ID.equals(bundleId)) {
        ancestorIdsOfParent.add(bundleId);
    }
    final Map<String, List<String>> newAncestorIdsByParentId = newHashMap(Map.of(resourceId, ancestorIdsOfParent.build()));
    // Start processing ancestor ID lists with the direct children of the resource
    final Deque<Map.Entry<String, Collection<Resource>>> toProcess = new ArrayDeque<>();
    toProcess.addLast(new AbstractMap.SimpleImmutableEntry<>(resourceId, resourcesByParentId.get(resourceId)));
    while (!toProcess.isEmpty()) {
        final Entry<String, Collection<Resource>> entry = toProcess.removeFirst();
        final String parentId = entry.getKey();
        final Collection<Resource> resources = entry.getValue();
        /*
			 * XXX: We will use the same ancestor ID list for all sibling resources. The
			 * call removes the entry from the map as it will never be read again after this
			 * iteration.
			 */
        final List<String> newAncestorIds = newAncestorIdsByParentId.remove(parentId);
        for (final Resource current : resources) {
            final String id = current.getId();
            final ResourceDocument resource = context.lookup(id, ResourceDocument.class);
            final ResourceDocument.Builder resourceBuilder = ResourceDocument.builder(resource);
            if (!Objects.equals(resource.getBundleAncestorIds(), newAncestorIds)) {
                resourceBuilder.bundleAncestorIds(newAncestorIds);
                context.update(resource, resourceBuilder.build());
            }
            final Collection<Resource> next = resourcesByParentId.get(id);
            if (!next.isEmpty()) {
                /*
					 * If the current resource has any children, make a note that we have to update
					 * bundleAncestorIds for them as well. The bundleId for these resources remains
					 * "id".
					 */
                final List<String> nextAncestorIds = ImmutableList.<String>builder().addAll(newAncestorIds).add(parentId).build();
                newAncestorIdsByParentId.put(id, nextAncestorIds);
                toProcess.add(new AbstractMap.SimpleImmutableEntry<>(id, next));
            }
        }
    }
    return true;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Resource(com.b2international.snowowl.core.Resource) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) Builder(com.b2international.snowowl.core.internal.ResourceDocument.Builder) Entry(java.util.Map.Entry) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

ResourceDocument (com.b2international.snowowl.core.internal.ResourceDocument)3 ApiError (com.b2international.commons.exceptions.ApiError)1 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 ConflictException (com.b2international.commons.exceptions.ConflictException)1 Resource (com.b2international.snowowl.core.Resource)1 ResourceURI (com.b2international.snowowl.core.ResourceURI)1 Branch (com.b2international.snowowl.core.branch.Branch)1 ResourceRepositoryCommitRequestBuilder (com.b2international.snowowl.core.context.ResourceRepositoryCommitRequestBuilder)1 RepositoryContext (com.b2international.snowowl.core.domain.RepositoryContext)1 Request (com.b2international.snowowl.core.events.Request)1 ComponentNotFoundException (com.b2international.snowowl.core.exceptions.ComponentNotFoundException)1 User (com.b2international.snowowl.core.identity.User)1 Builder (com.b2international.snowowl.core.internal.ResourceDocument.Builder)1 Merge (com.b2international.snowowl.core.merge.Merge)1 MergeConflict (com.b2international.snowowl.core.merge.MergeConflict)1 RepositoryRequests (com.b2international.snowowl.core.repository.RepositoryRequests)1 BranchRequest (com.b2international.snowowl.core.request.BranchRequest)1 ResourceURIPathResolver (com.b2international.snowowl.core.uri.ResourceURIPathResolver)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 ImmutableList (com.google.common.collect.ImmutableList)1